Skip to content

Commit

Permalink
Spigot supports.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Dec 28, 2024
1 parent 5cd5ba0 commit d1b1cf5
Show file tree
Hide file tree
Showing 30 changed files with 593 additions and 135 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Welcome to BetterModel!

</div>

### Modern lightweight Minecraft model implementation for Paper, Folia
### Modern lightweight Minecraft model implementation for Bukkit, Folia
This plugin implements server-side model by using packet-based item display.

- Importing .bbmodel
Expand Down Expand Up @@ -55,7 +55,7 @@ partvisibility
```

### Version
- Paper of that's fork (with Folia)
- Spigot of that's fork (with Folia)
- Java 21
- Minecraft 1.20.4-1.21.4

Expand All @@ -65,6 +65,8 @@ No
### Command
/bettermodel reload - Reloads this plugin.
/bettermodel spawn <model> - Summons this model to husk.
/bettermodel limb <true/false> - Show/Hide player limb.
/bettermodel play <model> <animation> - Plays player animation.

### Permission
bettermodel - Accesses to command.
Expand Down
15 changes: 15 additions & 0 deletions api/src/main/java/kr/toxicity/model/api/BetterModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import kr.toxicity.model.api.manager.*;
import kr.toxicity.model.api.nms.NMS;
import kr.toxicity.model.api.scheduler.ModelScheduler;
import kr.toxicity.model.api.version.MinecraftVersion;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
Expand All @@ -10,8 +11,21 @@

public abstract class BetterModel extends JavaPlugin {

public static final boolean IS_PAPER;

private static BetterModel instance;

static {
boolean paper;
try {
Class.forName("io.papermc.paper.configuration.PaperConfigurations");
paper = true;
} catch (Exception e) {
paper = false;
}
IS_PAPER = paper;
}

@Override
public final void onLoad() {
if (instance != null) throw new RuntimeException();
Expand All @@ -33,6 +47,7 @@ public final void onLoad() {
public abstract @NotNull CommandManager commandManager();
public abstract @NotNull CompatibilityManager compatibilityManager();
public abstract @NotNull ConfigManager configManager();
public abstract @NotNull ModelScheduler scheduler();

public sealed interface ReloadResult {
record Success(long time) implements ReloadResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@Getter
@Setter
public class ModelDamagedEvent extends EntityEvent implements Cancellable {

public static final HandlerList HANDLER_LIST = new HandlerList();

private final @NotNull HitBox hitBox;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public float bodyYaw() {
return 0;
}

@Override
public float pitch() {
return 0;
}

@Override
public float yaw() {
return 0;
Expand All @@ -33,6 +38,7 @@ public float yaw() {

boolean onWalk();
double scale();
float pitch();
float bodyYaw();
float yaw();
@NotNull Vector3f passengerPosition();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kr.toxicity.model.api.scheduler;

import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;

public interface ModelScheduler {
@NotNull ModelTask task(@NotNull Location location, @NotNull Runnable runnable);
@NotNull ModelTask taskLater(long delay, @NotNull Location location, @NotNull Runnable runnable);
@NotNull ModelTask asyncTask(@NotNull Runnable runnable);
@NotNull ModelTask asyncTaskLater(long delay, @NotNull Runnable runnable);
@NotNull ModelTask asyncTaskTimer(long delay, long period, @NotNull Runnable runnable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kr.toxicity.model.api.scheduler;

public interface ModelTask {
boolean isCancelled();
void cancel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import kr.toxicity.model.api.nms.PlayerChannelHandler;
import kr.toxicity.model.api.util.EntityUtil;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
Expand Down Expand Up @@ -67,7 +66,7 @@ public EntityTracker(@NotNull Entity entity, @NotNull RenderInstance instance) {
r -> r.getName().startsWith("h_"),
a -> {
if (a.rotation() != null && !isRunningSingleAnimation()) {
a.rotation().add(-entity.getPitch(), Math.clamp(
a.rotation().add(-adapter.pitch(), Math.clamp(
-adapter.yaw() + adapter.bodyYaw(),
-45,
45
Expand All @@ -82,7 +81,7 @@ public EntityTracker(@NotNull Entity entity, @NotNull RenderInstance instance) {
));
entity.getPersistentDataContainer().set(TRACKING_ID, PersistentDataType.STRING, instance.getParent().getParent().name());
TRACKER_MAP.put(entity.getUniqueId(), this);
Bukkit.getRegionScheduler().run(BetterModel.inst(), entity.getLocation(), s -> {
BetterModel.inst().scheduler().task(entity.getLocation(), () -> {
if (!closed.get() && !forRemoval()) createHitBox();
});
instance.setup();
Expand Down Expand Up @@ -135,8 +134,9 @@ public void close() throws Exception {
}

public void spawnNearby(@NotNull Location location) {
for (Player nearbyPlayer : location.getNearbyPlayers(EntityUtil.RENDER_DISTANCE, instance.spawnFilter())) {
spawn(nearbyPlayer);
var filter = instance.spawnFilter();
for (Entity e : location.getWorld().getNearbyEntities(location, EntityUtil.RENDER_DISTANCE , EntityUtil.RENDER_DISTANCE , EntityUtil.RENDER_DISTANCE)) {
if (e instanceof Player player && filter.test(player)) spawn(player);
}
}

Expand Down
11 changes: 4 additions & 7 deletions api/src/main/java/kr/toxicity/model/api/tracker/Tracker.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package kr.toxicity.model.api.tracker;

import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import kr.toxicity.model.api.BetterModel;
import kr.toxicity.model.api.data.renderer.AnimationModifier;
import kr.toxicity.model.api.data.renderer.RenderInstance;
import kr.toxicity.model.api.entity.RenderedEntity;
import kr.toxicity.model.api.entity.TrackerMovement;
import kr.toxicity.model.api.nms.PacketBundler;
import kr.toxicity.model.api.scheduler.ModelTask;
import kr.toxicity.model.api.util.EntityUtil;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
Expand All @@ -20,7 +18,6 @@
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;
import java.util.function.Supplier;
Expand All @@ -29,7 +26,7 @@ public abstract class Tracker implements AutoCloseable {
public static final NamespacedKey TRACKING_ID = Objects.requireNonNull(NamespacedKey.fromString("bettermodel_tracker"));

protected final RenderInstance instance;
private final ScheduledTask task;
private final ModelTask task;
private final AtomicBoolean runningSingle = new AtomicBoolean();

private TrackerMovement before;
Expand All @@ -38,14 +35,14 @@ public abstract class Tracker implements AutoCloseable {
private Supplier<TrackerMovement> movement = () -> new TrackerMovement(new Vector3f(), new Vector3f(1), new Vector3f());
public Tracker(@NotNull RenderInstance instance) {
this.instance = instance;
task = Bukkit.getAsyncScheduler().runAtFixedRate(BetterModel.inst(), task -> {
task = BetterModel.inst().scheduler().asyncTaskTimer(1, 1, () -> {
if (viewedPlayerSize() == 0) return;
var bundle = BetterModel.inst().nms().createBundler();
instance.move(isRunningSingleAnimation() && before != null && BetterModel.inst().configManager().lockOnPlayAnimation() ? before : (before = movement.get()), bundle);
if (!bundle.isEmpty()) for (Player player : instance.viewedPlayer()) {
bundle.send(player);
}
}, 50, 50, TimeUnit.MILLISECONDS);
});
tint(false);
instance.filter(p -> EntityUtil.canSee(p.getLocation(), location()));
}
Expand Down
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ allprojects {
apply(plugin = "java")
apply(plugin = "kotlin")
group = "kr.toxicity.model"
version = "1.1"
version = "1.2"
repositories {
mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/")
Expand Down Expand Up @@ -125,7 +125,9 @@ bukkitPluginYaml {
description = "Accesses to command."
children = mapOf(
"reload" to true,
"spawn" to true
"spawn" to true,
"play" to true,
"limb" to true
)
}
}
9 changes: 7 additions & 2 deletions core/src/main/kotlin/kr/toxicity/model/BetterModelImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import kr.toxicity.model.api.BetterModel
import kr.toxicity.model.api.BetterModel.ReloadResult.*
import kr.toxicity.model.api.manager.*
import kr.toxicity.model.api.nms.NMS
import kr.toxicity.model.api.scheduler.ModelScheduler
import kr.toxicity.model.api.version.MinecraftVersion
import kr.toxicity.model.api.version.MinecraftVersion.*
import kr.toxicity.model.manager.*
import kr.toxicity.model.scheduler.PaperScheduler
import kr.toxicity.model.scheduler.StandardScheduler
import kr.toxicity.model.util.warn
import org.bukkit.Bukkit
import java.io.InputStream
Expand All @@ -31,6 +34,8 @@ class BetterModelImpl : BetterModel() {
)
}

private val scheduler = if (IS_PAPER) PaperScheduler() else StandardScheduler()

override fun onEnable() {
nms = when (version) {
V1_21_4 -> kr.toxicity.model.nms.v1_21_R3.NMSImpl()
Expand All @@ -48,7 +53,7 @@ class BetterModelImpl : BetterModel() {
}
}
managers.forEach(GlobalManagerImpl::start)
Bukkit.getAsyncScheduler().runNow(this) {
scheduler.asyncTask {
reload()
}
}
Expand Down Expand Up @@ -84,7 +89,7 @@ class BetterModelImpl : BetterModel() {
}
}


override fun scheduler(): ModelScheduler = scheduler
override fun modelManager(): ModelManager = ModelManagerImpl
override fun playerManager(): PlayerManager = PlayerManagerImpl
override fun entityManager(): EntityManager = EntityManagerImpl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ import dev.jorel.commandapi.arguments.BooleanArgument
import dev.jorel.commandapi.arguments.StringArgument
import dev.jorel.commandapi.executors.CommandExecutionInfo
import dev.jorel.commandapi.executors.PlayerCommandExecutor
import kr.toxicity.model.api.BetterModel
import kr.toxicity.model.api.BetterModel.ReloadResult.*
import kr.toxicity.model.api.manager.CommandManager
import kr.toxicity.model.api.util.EntityUtil
import kr.toxicity.model.util.PLUGIN
import org.bukkit.Bukkit
import org.bukkit.entity.EntityType
import org.bukkit.entity.Player
import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -47,7 +44,7 @@ object CommandManagerImpl : CommandManager, GlobalManagerImpl {
.withAliases("re", "rl")
.withPermission("bettermodel.reload")
.executes(CommandExecutionInfo {
Bukkit.getAsyncScheduler().runNow(BetterModel.inst()) { _ ->
PLUGIN.scheduler().asyncTask {
when (val result = PLUGIN.reload()) {
is OnReload -> it.sender().sendMessage("The plugin still on reload!")
is Success -> it.sender().sendMessage("Reload completed (${result.time} time)")
Expand Down
45 changes: 30 additions & 15 deletions core/src/main/kotlin/kr/toxicity/model/manager/EntityManagerImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,49 @@ package kr.toxicity.model.manager

import com.destroystokyo.paper.event.entity.EntityAddToWorldEvent
import com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent
import kr.toxicity.model.api.BetterModel
import kr.toxicity.model.api.data.renderer.AnimationModifier
import kr.toxicity.model.api.event.ModelInteractEvent
import kr.toxicity.model.api.manager.EntityManager
import kr.toxicity.model.api.tracker.EntityTracker
import kr.toxicity.model.util.PLUGIN
import kr.toxicity.model.util.registerListener
import org.bukkit.Bukkit
import org.bukkit.entity.LivingEntity
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.entity.EntityDamageEvent
import org.bukkit.event.entity.EntityDeathEvent
import org.bukkit.event.entity.EntitySpawnEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.event.world.ChunkLoadEvent
import java.util.concurrent.TimeUnit

object EntityManagerImpl : EntityManager, GlobalManagerImpl {
override fun reload() {
if (BetterModel.IS_PAPER) registerListener(object : Listener {
@EventHandler
fun EntityRemoveFromWorldEvent.remove() {
EntityTracker.tracker(entity)?.let {
if (!it.forRemoval()) it.close()
}
}
@EventHandler
fun EntityAddToWorldEvent.add() {
EntityTracker.tracker(entity)?.refresh()
}
}) else registerListener(object : Listener {
@EventHandler
@Suppress("removal", "DEPRECATION")
fun org.bukkit.event.entity.EntityRemoveEvent.remove() {
EntityTracker.tracker(entity)?.let {
if (!it.forRemoval()) it.close()
}
}
@EventHandler
fun EntitySpawnEvent.add() {
EntityTracker.tracker(entity)?.refresh()
}
})

registerListener(object : Listener {
@EventHandler
fun ModelInteractEvent.interact() {
Expand All @@ -32,26 +57,16 @@ object EntityManagerImpl : EntityManager, GlobalManagerImpl {
EntityTracker.tracker(player.uniqueId)?.close()
}
@EventHandler
fun EntityRemoveFromWorldEvent.remove() {
EntityTracker.tracker(entity)?.let {
if (!it.forRemoval()) it.close()
}
}
@EventHandler
fun ChunkLoadEvent.load() {
chunk.entities.forEach {
EntityTracker.tracker(it)?.refresh()
}
}
@EventHandler
fun EntityAddToWorldEvent.add() {
EntityTracker.tracker(entity)?.refresh()
}
@EventHandler
fun EntityDeathEvent.death() {
EntityTracker.tracker(entity)?.let {
if (!it.animateSingle("death", AnimationModifier.DEFAULT) {
Bukkit.getRegionScheduler().run(PLUGIN, entity.location) { _ ->
PLUGIN.scheduler().task(entity.location) {
it.close()
}
}) it.close()
Expand All @@ -67,9 +82,9 @@ object EntityManagerImpl : EntityManager, GlobalManagerImpl {
}) it.tint(true)
else {
it.tint(true)
Bukkit.getAsyncScheduler().runDelayed(PLUGIN, { _ ->
PLUGIN.scheduler().asyncTaskLater(e.maximumNoDamageTicks.toLong() / 2) {
it.tint(false)
}, e.maximumNoDamageTicks.toLong() * 25 , TimeUnit.MILLISECONDS)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ object PlayerManagerImpl : PlayerManager, GlobalManagerImpl {

private fun Player.showAll() {
val loc = location
Bukkit.getRegionScheduler().runDelayed(PLUGIN, loc, {
loc.getNearbyLivingEntities(EntityUtil.RENDER_DISTANCE).forEach {
PLUGIN.scheduler().taskLater(10, loc) {
loc.world.getNearbyEntities(loc, EntityUtil.RENDER_DISTANCE, EntityUtil.RENDER_DISTANCE, EntityUtil.RENDER_DISTANCE).forEach {
EntityTracker.tracker(it)?.spawn(this)
}
}, 10)
}
}

private fun Player.register() = playerMap.computeIfAbsent(uniqueId) {
Expand Down
Loading

0 comments on commit d1b1cf5

Please sign in to comment.