From aea7c5f7a2707e133d1a2f5328709b2fa188da19 Mon Sep 17 00:00:00 2001 From: Tofaa <82680183+Tofaa2@users.noreply.github.com> Date: Fri, 23 Feb 2024 01:37:33 +0400 Subject: [PATCH] fix WrapperPlayer --- .idea/workspace.xml | 10 ++ .../java/me/tofaa/entitylib/EntityLibAPI.java | 3 +- .../me/tofaa/entitylib/meta/EntityMeta.java | 9 ++ .../entitylib/wrapper/WrapperEntity.java | 87 +++++++++----- .../wrapper/WrapperLivingEntity.java | 9 +- .../entitylib/wrapper/WrapperPlayer.java | 109 ++++++++++-------- .../entitylib/wrapper/hologram/Hologram.java | 8 ++ .../wrapper/hologram/ModernHologram.java | 11 ++ .../entitylib/spigot/ExtraConversionUtil.java | 51 ++++++-- .../spigot/InternalRegistryListener.java | 28 +++-- .../entitylib/spigot/SpigotEntityLibAPI.java | 8 +- .../spigot/SpigotEntityLibPlatform.java | 16 ++- .../testentitylib/TestEntityLibPlugin.java | 45 +++----- .../testentitylib/TestPlayerCommand.java | 84 ++++++++++++++ .../testentitylib/TestTextDisplayCommand.java | 42 +++++++ 15 files changed, 380 insertions(+), 140 deletions(-) create mode 100644 test-plugin/src/main/java/me/tofaa/testentitylib/TestPlayerCommand.java create mode 100644 test-plugin/src/main/java/me/tofaa/testentitylib/TestTextDisplayCommand.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 72223c6..7183e2c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,7 +5,15 @@ + + + + + + + + diff --git a/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java b/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java index 95dc7e5..6b77308 100644 --- a/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java +++ b/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java @@ -34,7 +34,6 @@ public interface EntityLibAPI { @NotNull T createEntity(EntityType type); - @NotNull WrapperPlayer spawnPlayer(UserProfile profile, Location location); @NotNull T spawnEntity(@NotNull Class wrapperClass, @NotNull EntityType entityType, @NotNull Location location); @@ -68,4 +67,6 @@ public interface EntityLibAPI { * @param tickContainer the TickContainer to add. */ void addTickContainer(@NotNull TickContainer tickContainer); + + void runLater(@NotNull Runnable runnable, long delayInTicks); } diff --git a/api/src/main/java/me/tofaa/entitylib/meta/EntityMeta.java b/api/src/main/java/me/tofaa/entitylib/meta/EntityMeta.java index 145cc8b..8d58ddc 100644 --- a/api/src/main/java/me/tofaa/entitylib/meta/EntityMeta.java +++ b/api/src/main/java/me/tofaa/entitylib/meta/EntityMeta.java @@ -3,6 +3,7 @@ package me.tofaa.entitylib.meta; import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.manager.server.VersionComparison; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; +import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.protocol.entity.data.EntityMetadataProvider; import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose; @@ -214,6 +215,14 @@ public class EntityMeta implements EntityMetadataProvider { return (byte) (value + amount); } + public void setIndex(byte index, @NotNull EntityDataType dataType, T value) { + this.metadata.setIndex(index, dataType, value); + } + + public T getIndex(byte index, @Nullable T defaultValue) { + return this.metadata.getIndex(index, defaultValue); + } + public byte getMask(byte index) { return this.metadata.getIndex(index, (byte) 0); } diff --git a/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java index dfcab76..8714fed 100644 --- a/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java +++ b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java @@ -24,7 +24,7 @@ public class WrapperEntity implements Tickable, TrackedEntity { private EntityType entityType; private EntityMeta entityMeta; private boolean ticking; - private Location location; + protected Location location; private Location preRidingLocation; private final Set viewers; private boolean onGround; @@ -48,12 +48,35 @@ public class WrapperEntity implements Tickable, TrackedEntity { if (spawned) return false; this.location = location; this.spawned = true; - int data = 0; + sendPacketToViewers( + new WrapperPlayServerSpawnEntity( + entityId, + Optional.of(this.uuid), + entityType, + location.getPosition(), + location.getPitch(), + location.getYaw(), + location.getYaw(), + getObjectData(), + createVeloPacket() + ) + ); + sendPacketToViewers(entityMeta.createPacket()); + return true; + } + + protected int getObjectData() { + if (entityMeta instanceof ObjectData) { + return ((ObjectData) entityMeta).getObjectData(); + } + return 0; + } + + protected Optional createVeloPacket() { Optional velocity; double veloX = 0, veloY = 0, veloZ = 0; if (entityMeta instanceof ObjectData) { ObjectData od = (ObjectData) entityMeta; - data = od.getObjectData(); if (od.requiresVelocityPacketAtSpawn()) { final WrapperPlayServerEntityVelocity veloPacket = getVelocityPacket(); veloX = veloPacket.getVelocity().getX(); @@ -66,21 +89,7 @@ public class WrapperEntity implements Tickable, TrackedEntity { } else { velocity = Optional.of(new Vector3d(veloX, veloY, veloZ)); } - sendPacketToViewers( - new WrapperPlayServerSpawnEntity( - entityId, - Optional.of(this.uuid), - entityType, - location.getPosition(), - location.getPitch(), - location.getYaw(), - location.getYaw(), - data, - velocity - ) - ); - sendPacketToViewers(entityMeta.createPacket()); - return true; + return velocity; } public void setLocation(Location location) { @@ -94,6 +103,10 @@ public class WrapperEntity implements Tickable, TrackedEntity { public void despawn() { if (!spawned) return; spawned = false; + if (this instanceof WrapperPlayer) { + WrapperPlayer p = (WrapperPlayer) this; + sendPacketsToViewers(p.tabListRemovePacket()); + } sendPacketToViewers(new WrapperPlayServerDestroyEntities(entityId)); } @@ -133,20 +146,30 @@ public class WrapperEntity implements Tickable, TrackedEntity { return; } if (spawned) { - WrapperPlayServerSpawnEntity packet = new WrapperPlayServerSpawnEntity( - entityId, - Optional.of(this.uuid), - entityType, - location.getPosition(), - location.getPitch(), - location.getYaw(), - location.getYaw(), - 0, - Optional.empty() - ); - sendPacket(uuid, packet); + if (this instanceof WrapperPlayer) { + WrapperPlayer p = (WrapperPlayer) this; + sendPacket(uuid, p.tabListPacket()); + } + sendPacket(uuid, createSpawnPacket()); sendPacket(uuid, entityMeta.createPacket()); } + if (EntityLib.getApi().getSettings().isDebugMode()) { + EntityLib.getPlatform().getLogger().info("Added viewer " + uuid + " to entity " + entityId); + } + } + + protected WrapperPlayServerSpawnEntity createSpawnPacket() { + return new WrapperPlayServerSpawnEntity( + entityId, + Optional.of(this.uuid), + entityType, + location.getPosition(), + location.getPitch(), + location.getYaw(), + location.getYaw(), + getObjectData(), + createVeloPacket() + ); } public void addViewer(User user) { @@ -177,6 +200,10 @@ public class WrapperEntity implements Tickable, TrackedEntity { if (!viewers.remove(uuid)) { return; } + if (this instanceof WrapperPlayer) { + WrapperPlayer p = (WrapperPlayer) this; + sendPacket(uuid, p.tabListRemovePacket()); + } sendPacket(uuid, new WrapperPlayServerDestroyEntities(entityId)); } diff --git a/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperLivingEntity.java b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperLivingEntity.java index 835be99..27dc8f1 100644 --- a/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperLivingEntity.java +++ b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperLivingEntity.java @@ -7,10 +7,15 @@ import java.util.UUID; public class WrapperLivingEntity extends WrapperEntity{ - - + private final WrapperEntityEquipment equipment; public WrapperLivingEntity(int entityId, UUID uuid, EntityType entityType, EntityMeta entityMeta) { super(entityId, uuid, entityType, entityMeta); + this.equipment = new WrapperEntityEquipment(this); + } + + + public WrapperEntityEquipment getEquipment() { + return equipment; } } diff --git a/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperPlayer.java b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperPlayer.java index b78970e..d48e5a9 100644 --- a/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperPlayer.java +++ b/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperPlayer.java @@ -3,40 +3,55 @@ package me.tofaa.entitylib.wrapper; import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import com.github.retrooper.packetevents.protocol.player.*; import com.github.retrooper.packetevents.protocol.world.Location; -import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfo; -import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoRemove; -import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoUpdate; -import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnPlayer; +import com.github.retrooper.packetevents.util.Vector3d; +import com.github.retrooper.packetevents.wrapper.PacketWrapper; +import com.github.retrooper.packetevents.wrapper.play.server.*; +import me.tofaa.entitylib.EntityLib; import me.tofaa.entitylib.meta.EntityMeta; import net.kyori.adventure.text.Component; -import java.util.ArrayList; -import java.util.List; +import java.util.*; public class WrapperPlayer extends WrapperLivingEntity { - private final UserProfile profile; + private UserProfile profile; private GameMode gameMode = GameMode.CREATIVE; private Component displayName; private boolean tablist = true; + private int latency = -1; public WrapperPlayer(UserProfile profile, int entityId) { super(entityId, profile.getUUID(), EntityTypes.PLAYER, EntityMeta.createMeta(entityId, EntityTypes.PLAYER)); this.profile = profile; } + public WrapperPlayServerPlayerInfoUpdate tabListPacket() { + EnumSet actions = EnumSet.of( + WrapperPlayServerPlayerInfoUpdate.Action.ADD_PLAYER, + WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_LISTED + ); + return new WrapperPlayServerPlayerInfoUpdate( + actions, + createInfo() + ); + } + + public List getTextures() { + return profile.getTextureProperties(); + } + + public WrapperPlayServerPlayerInfoRemove tabListRemovePacket() { + return new WrapperPlayServerPlayerInfoRemove(getUuid()); + } + public void setGameMode(GameMode gameMode) { this.gameMode = gameMode; - sendPacketsToViewers(new WrapperPlayServerPlayerInfo( - WrapperPlayServerPlayerInfo.Action.UPDATE_GAME_MODE, - new WrapperPlayServerPlayerInfo.PlayerData(displayName, profile, gameMode, null, -1))); + sendPacketsToViewers(new WrapperPlayServerPlayerInfoUpdate(WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_GAME_MODE, createInfo())); } public void setDisplayName(Component displayName) { this.displayName = displayName; - sendPacketsToViewers(new WrapperPlayServerPlayerInfo( - WrapperPlayServerPlayerInfo.Action.UPDATE_DISPLAY_NAME, - new WrapperPlayServerPlayerInfo.PlayerData(displayName, profile, gameMode, null, -1))); + sendPacketsToViewers(new WrapperPlayServerPlayerInfoUpdate(WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_DISPLAY_NAME, createInfo())); } public Component getDisplayName() { @@ -51,56 +66,54 @@ public class WrapperPlayer extends WrapperLivingEntity { return profile.getTextureProperties(); } + public void setTextureProperties(List textureProperties) { + profile.setTextureProperties(textureProperties); + despawn(); + System.out.println("Despawning"); + EntityLib.getApi().runLater(() -> spawn(getLocation()), 2L); + + } + public GameMode getGameMode() { return gameMode; } - @Override - public void addViewer(User user) { - //user.sendPacket(createAddPacket()); - sendJoiningPackets(); - super.addViewer(user); + public boolean isInTablist() { + return tablist; } - @Override - public void removeViewer(User user) { - user.sendPacket(createRemovePacket()); - super.removeViewer(user); + public void setInTablist(boolean tablist) { + this.tablist = tablist; + sendPacketToViewers(tabListPacket()); + if (!tablist) { + sendPacketToViewers(tabListRemovePacket()); + } } - @Override - public boolean spawn(Location location) { - this.setLocation(location); - WrapperPlayServerSpawnPlayer packet = new WrapperPlayServerSpawnPlayer(getEntityId(), getUuid(), location, getEntityMeta()); - sendPacketsToViewers(packet); - return true; - //return super.spawn(location); + public int getLatency() { + return latency; } - private void sendJoiningPackets() { - List data = new ArrayList<>(); - data.add(new WrapperPlayServerPlayerInfo.PlayerData(displayName, profile, gameMode, null, -1)); - WrapperPlayServerPlayerInfo p1 = new WrapperPlayServerPlayerInfo( - WrapperPlayServerPlayerInfo.Action.ADD_PLAYER, - data - ); - sendPacketsToViewers(p1); - } - - private WrapperPlayServerPlayerInfoUpdate createAddPacket() { - return new WrapperPlayServerPlayerInfoUpdate( - WrapperPlayServerPlayerInfoUpdate.Action.ADD_PLAYER, - new WrapperPlayServerPlayerInfoUpdate.PlayerInfo( - profile, - true, -1, gameMode, displayName, null + public void setLatency(int latency) { + this.latency = latency; + sendPacketsToViewers( + new WrapperPlayServerPlayerInfoUpdate( + WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_LATENCY, + createInfo() ) ); } - private WrapperPlayServerPlayerInfoRemove createRemovePacket() { - return new WrapperPlayServerPlayerInfoRemove(getUuid()); + protected WrapperPlayServerPlayerInfoUpdate.PlayerInfo createInfo() { + return new WrapperPlayServerPlayerInfoUpdate.PlayerInfo( + profile, + tablist, + latency, + gameMode, + displayName, + null + ); } - } diff --git a/api/src/main/java/me/tofaa/entitylib/wrapper/hologram/Hologram.java b/api/src/main/java/me/tofaa/entitylib/wrapper/hologram/Hologram.java index 0e05cd4..88ab529 100644 --- a/api/src/main/java/me/tofaa/entitylib/wrapper/hologram/Hologram.java +++ b/api/src/main/java/me/tofaa/entitylib/wrapper/hologram/Hologram.java @@ -19,6 +19,14 @@ public interface Hologram { return new LegacyHologram<>(location, lines); } + static Hologram.@NotNull Modern modern(@NotNull Location location) { + return new ModernHologram<>(location); + } + + static Hologram.@NotNull Modern modern(@NotNull Location location, List lines) { + return new ModernHologram<>(location, lines); + } + @NotNull Location getLocation(); diff --git a/api/src/main/java/me/tofaa/entitylib/wrapper/hologram/ModernHologram.java b/api/src/main/java/me/tofaa/entitylib/wrapper/hologram/ModernHologram.java index 5c35552..c8ebb75 100644 --- a/api/src/main/java/me/tofaa/entitylib/wrapper/hologram/ModernHologram.java +++ b/api/src/main/java/me/tofaa/entitylib/wrapper/hologram/ModernHologram.java @@ -19,6 +19,17 @@ final class ModernHologram implements Hologram.Modern { private List lines = new ArrayList<>(3); private Consumer modifier; + ModernHologram(@NotNull Location location) { + this.location = location; + } + + ModernHologram(@NotNull Location location, List lines) { + this(location); + for (Component line : lines) { + addLine(line); + } + } + @Override public void show() { for (WrapperEntity line : lines) { diff --git a/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/ExtraConversionUtil.java b/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/ExtraConversionUtil.java index 6f9eaf9..6701af8 100644 --- a/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/ExtraConversionUtil.java +++ b/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/ExtraConversionUtil.java @@ -4,27 +4,60 @@ import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose; import com.github.retrooper.packetevents.protocol.player.HumanoidArm; import com.github.retrooper.packetevents.protocol.player.TextureProperty; import com.github.retrooper.packetevents.protocol.player.UserProfile; +import com.google.common.collect.Multimap; +import io.github.retrooper.packetevents.util.SpigotConversionUtil; +import io.github.retrooper.packetevents.util.SpigotReflectionUtil; import org.bukkit.entity.Player; import org.bukkit.entity.Pose; import org.bukkit.inventory.MainHand; import org.bukkit.profile.PlayerProfile; -import org.bukkit.profile.PlayerTextures; +import org.jetbrains.annotations.Nullable; -import java.util.Collections; -import java.util.List; -import java.util.UUID; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; public final class ExtraConversionUtil { + private static Method PLAYER_PROFILE_METHOD; + private static Class PROFILE_CLASS; + private static Method GET_PROPERTIES_METHOD; + static { + try { + PLAYER_PROFILE_METHOD = SpigotReflectionUtil.CRAFT_PLAYER_CLASS.getMethod("getProfile"); + PLAYER_PROFILE_METHOD.setAccessible(true); // Dont care :D + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + private ExtraConversionUtil() { } - public static UserProfile fromBukkitPlayerProfile(PlayerProfile player) { - UUID uuid = player.getUniqueId(); - String name = player.getName(); - // TODO: Textures - return new UserProfile(uuid, name, Collections.emptyList()); + public static @Nullable UserProfile getProfileFromBukkitPlayer(Player player) { + + try { + Object profile = PLAYER_PROFILE_METHOD.invoke(player); // GameProfile + if (PROFILE_CLASS == null) { + PROFILE_CLASS = profile.getClass(); + GET_PROPERTIES_METHOD = PROFILE_CLASS.getMethod("getProperties"); + GET_PROPERTIES_METHOD.setAccessible(true); // Again dont care; + } + Multimap properties = (Multimap) GET_PROPERTIES_METHOD.invoke(profile); + Collection textures = properties.get("textures"); + if (textures == null || textures.isEmpty()) return new UserProfile(player.getUniqueId(), player.getName()); + Object texture = textures.iterator().next(); + + String value = (String) texture.getClass().getDeclaredMethod("value").invoke(texture); + String signature = (String) texture.getClass().getDeclaredMethod("signature").invoke(texture); + + ArrayList t = new ArrayList<>(); + t.add(new TextureProperty("textures", value, signature)); + return new UserProfile(player.getUniqueId(), player.getName(), t); + } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { + throw new RuntimeException(e); + } } public static EntityPose fromBukkitPose(Pose pose) { diff --git a/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/InternalRegistryListener.java b/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/InternalRegistryListener.java index adc5213..f376dc4 100644 --- a/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/InternalRegistryListener.java +++ b/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/InternalRegistryListener.java @@ -11,6 +11,7 @@ import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSp import me.tofaa.entitylib.TrackedEntity; import me.tofaa.entitylib.event.types.UserStopTrackingEntityEvent; import me.tofaa.entitylib.event.types.UserTrackingEntityEvent; +import org.bukkit.Bukkit; import org.bukkit.entity.Entity; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -32,13 +33,15 @@ final class InternalRegistryListener extends PacketListenerAbstract implements L if (type == PacketType.Play.Server.DESTROY_ENTITIES) { WrapperPlayServerDestroyEntities packet = new WrapperPlayServerDestroyEntities(event); int[] ids = packet.getEntityIds(); - for (int id : ids) { - TrackedEntity tracked = findTracker(id); - if (tracked == null) { - continue; + Bukkit.getScheduler().runTaskLater(platform.getHandle(), () -> { + for (int id : ids) { + TrackedEntity tracked = findTracker(id); + if (tracked == null) { + continue; + } + platform.getEventHandler().callEvent(UserStopTrackingEntityEvent.class, new UserStopTrackingEntityEvent(user, tracked)); } - platform.getEventHandler().callEvent(UserStopTrackingEntityEvent.class, new UserStopTrackingEntityEvent(user, tracked)); - } + }, 2L); } else if (type == PacketType.Play.Server.SPAWN_ENTITY) { WrapperPlayServerSpawnEntity packet = new WrapperPlayServerSpawnEntity(event); @@ -73,11 +76,13 @@ final class InternalRegistryListener extends PacketListenerAbstract implements L } private void trackEntity(User user, int id) { - TrackedEntity entity = findTracker(id); - if (entity == null) { - return; - } - platform.getEventHandler().callEvent(UserTrackingEntityEvent.class, new UserTrackingEntityEvent(user, entity)); + Bukkit.getScheduler().runTaskLater(platform.getHandle(), () -> { + TrackedEntity entity = findTracker(id); + if (entity == null) { + return; + } + platform.getEventHandler().callEvent(UserTrackingEntityEvent.class, new UserTrackingEntityEvent(user, entity)); + }, 2L); } private TrackedEntity findTracker(int id) { @@ -97,7 +102,6 @@ final class InternalRegistryListener extends PacketListenerAbstract implements L public void onEntitySpawn(EntitySpawnEvent event) { Entity e = event.getEntity(); platform.getPlatformEntities().put(e.getEntityId(), e); - System.out.println("Entity spawned: " + e.getEntityId() + " " + e.getType() + " " + e.getLocation()); } } diff --git a/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibAPI.java b/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibAPI.java index f32b86c..274dd71 100644 --- a/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibAPI.java +++ b/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibAPI.java @@ -75,7 +75,7 @@ public class SpigotEntityLibAPI extends AbstractEntityLibAPI { @Override public @Nullable TrackedEntity findPlatformEntity(final int entityId) { if (!api.getSettings().shouldTrackPlatformEntities()) return null; - Entity e = platformEntities.get(entityId); - if (e == null) return null; - return new SpigotEntity(e); + + for (World world : Bukkit.getWorlds()) { + Entity e = world.getEntities().stream().filter(entity -> entity.getEntityId() == entityId).findFirst().orElse(null); + if (e != null) { + return new SpigotEntity(e); + } + } + return null; +// Entity e = platformEntities.get(entityId); +// if (e == null) return null; +// return new SpigotEntity(e); } @Override diff --git a/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java b/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java index ad65255..3f4a3da 100644 --- a/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java +++ b/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java @@ -6,26 +6,25 @@ import com.github.retrooper.packetevents.protocol.player.UserProfile; import io.github.retrooper.packetevents.util.SpigotConversionUtil; import me.tofaa.entitylib.APIConfig; import me.tofaa.entitylib.EntityLib; -import me.tofaa.entitylib.meta.mobs.passive.ChickenMeta; +import me.tofaa.entitylib.event.types.UserTrackingEntityEvent; import me.tofaa.entitylib.spigot.SpigotEntityLibAPI; import me.tofaa.entitylib.spigot.SpigotEntityLibPlatform; -import me.tofaa.entitylib.wrapper.WrapperEntity; -import me.tofaa.entitylib.event.types.*; import me.tofaa.entitylib.wrapper.WrapperPlayer; +import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.InvocationTargetException; import java.util.UUID; -public class TestEntityLibPlugin extends JavaPlugin implements CommandExecutor { - +public class TestEntityLibPlugin extends JavaPlugin { private SpigotEntityLibAPI api; - private WrapperPlayer e; @Override public void onEnable() { @@ -39,34 +38,16 @@ public class TestEntityLibPlugin extends JavaPlugin implements CommandExecutor { EntityLib.init(platform, settings); api = platform.getAPI(); - getCommand("testapi").setExecutor(this); - platform.getEventHandler().addEventCallback(UserTrackingEntityEvent.class, event -> { - event.getUser().sendMessage("Tracking: " + event.getEntity().getEntityId()); - event.getUser().sendMessage("Size: " + platform.queryPlatformEntities().toArray().length); - }); - platform.getEventHandler().addEventCallback(UserStopTrackingEntityEvent.class, event -> { - event.getUser().sendMessage("Stop Tracking: " + event.getEntity().getEntityId()); - event.getUser().sendMessage("Size: " + platform.queryPlatformEntities().toArray().length); - }); - } - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if (!(sender instanceof Player)) return false; - Player player = (Player) sender; - if (e != null) { - e.remove(); - player.sendMessage("Removed"); - e = null; - return true; + CommandMap commandMap; + try { + commandMap = (CommandMap) Bukkit.getServer().getClass().getMethod("getCommandMap").invoke(Bukkit.getServer()); + commandMap.register("testapi", new TestTextDisplayCommand()); + commandMap.register("testplayer", new TestPlayerCommand()); + } + catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + e.printStackTrace(); } - UUID uuid = UUID.randomUUID(); - UserProfile profile = new UserProfile(uuid, "RandomGoon"); - e = new WrapperPlayer(profile, EntityLib.getPlatform().getEntityIdProvider().provide(uuid, EntityTypes.PLAYER)); - e.addViewer(player.getUniqueId()); - api.spawnEntity(e, SpigotConversionUtil.fromBukkitLocation(player.getLocation())); - player.sendMessage("Spawned"); - return true; } } diff --git a/test-plugin/src/main/java/me/tofaa/testentitylib/TestPlayerCommand.java b/test-plugin/src/main/java/me/tofaa/testentitylib/TestPlayerCommand.java new file mode 100644 index 0000000..c53111d --- /dev/null +++ b/test-plugin/src/main/java/me/tofaa/testentitylib/TestPlayerCommand.java @@ -0,0 +1,84 @@ +package me.tofaa.testentitylib; + +import com.github.retrooper.packetevents.PacketEvents; +import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; +import com.github.retrooper.packetevents.protocol.item.ItemStack; +import com.github.retrooper.packetevents.protocol.item.type.ItemTypes; +import com.github.retrooper.packetevents.protocol.player.GameMode; +import com.github.retrooper.packetevents.protocol.player.TextureProperty; +import com.github.retrooper.packetevents.protocol.player.UserProfile; +import com.github.retrooper.packetevents.wrapper.PacketWrapper; +import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfo; +import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoUpdate; +import io.github.retrooper.packetevents.util.SpigotConversionUtil; +import me.tofaa.entitylib.EntityLib; +import me.tofaa.entitylib.spigot.ExtraConversionUtil; +import me.tofaa.entitylib.wrapper.WrapperPlayer; +import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.*; + +public class TestPlayerCommand extends BukkitCommand { + + private WrapperPlayer p; + public TestPlayerCommand() { + super("testplayer"); + } + + @Override + public boolean execute(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] strings) { + Player player = (Player) commandSender; + if (strings.length != 1) { + player.sendMessage("Usage: /testplayer "); + return false; + } + String arg = strings[0].toLowerCase(); + if (arg.equals("spawn")) { + UserProfile profile = new UserProfile(UUID.randomUUID(), "randomname", new ArrayList<>()); + p = new WrapperPlayer(profile, EntityLib.getPlatform().getEntityIdProvider().provide(profile.getUUID(), EntityTypes.PLAYER)); + p.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation())); + p.addViewer(player.getUniqueId()); + ItemStack stack = ItemStack.builder().type(ItemTypes.DIAMOND_BOOTS).build(); + p.getEquipment().setBoots(stack); + } + else if (arg.equals( "texture")) { + p.setTextureProperties(ExtraConversionUtil.getProfileFromBukkitPlayer(player).getTextureProperties()); + player.sendMessage("texture"); + } + else if (arg.equals( "ping")) { + p.setLatency(100); + player.sendMessage("Pong"); + } + else if (arg.equals( "gamemode")) { + p.setGameMode(GameMode.CREATIVE); + player.sendMessage("Gamemode set to creative"); + } + else if (arg.equals( "displayname")) { + p.setDisplayName(Component.text("Hello")); + player.sendMessage("Display name set to Hello"); + } + else if (arg.equals( "tablist")) { + p.setInTablist(!p.isInTablist()); + player.sendMessage("Tablist " + (p.isInTablist() ? "enabled" : "disabled")); + } + else if (arg.equals("remove")) { + p.remove(); + player.sendMessage("Entity removed"); + } + return true; + } + + @NotNull + @Override + public List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException { + if (args.length == 1) { + return Arrays.asList(new String[]{"spawn", "texture", "ping", "gamemode", "displayname", "tablist", "remove"}); + } + return Collections.emptyList(); + } +} diff --git a/test-plugin/src/main/java/me/tofaa/testentitylib/TestTextDisplayCommand.java b/test-plugin/src/main/java/me/tofaa/testentitylib/TestTextDisplayCommand.java new file mode 100644 index 0000000..e547767 --- /dev/null +++ b/test-plugin/src/main/java/me/tofaa/testentitylib/TestTextDisplayCommand.java @@ -0,0 +1,42 @@ +package me.tofaa.testentitylib; + +import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; +import io.github.retrooper.packetevents.util.SpigotConversionUtil; +import me.tofaa.entitylib.EntityLib; +import me.tofaa.entitylib.meta.display.TextDisplayMeta; +import me.tofaa.entitylib.wrapper.WrapperEntity; +import net.kyori.adventure.text.Component; +import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class TestTextDisplayCommand extends BukkitCommand { + + private WrapperEntity e; + + public TestTextDisplayCommand() { + super("textdisplay"); + } + + @Override + public boolean execute(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] strings) { + if (!(commandSender instanceof Player)) return true; + Player player = (Player) commandSender; + if (e == null) { + e = EntityLib.getApi().createEntity(EntityTypes.TEXT_DISPLAY); + if (e == null) { + player.sendMessage("Failed to spawn entity"); + return true; + } + e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation())); + e.addViewer(player.getUniqueId()); + player.sendMessage("Spawned"); + } + String msg = String.join(" ", strings); + TextDisplayMeta meta = (TextDisplayMeta) e.getEntityMeta(); + meta.setText(Component.text(msg)); + player.sendMessage("Set text to: " + msg); + return true; + } +}