From 61e226b82675320587cf3af5da03cfd9685cbbf1 Mon Sep 17 00:00:00 2001 From: LoJoSho Date: Tue, 9 Aug 2022 22:16:35 -0500 Subject: [PATCH] Compiling without PacketEvents --- common/build.gradle.kts | 6 +- .../fisher2911/hmccosmetics/HMCCosmetics.java | 3 +- .../hmccosmetics/config/SoundData.java | 8 +- .../hmccosmetics/config/SoundSerializer.java | 4 +- .../hmccosmetics/database/Database.java | 4 +- .../hmccosmetics/gui/ArmorItem.java | 12 - .../listener/CosmeticFixListener.java | 68 +-- .../listener/WardrobeListener.java | 8 +- .../hmccosmetics/packet/PacketManager.java | 387 ++++++++++++------ .../packet/WrapperPlayServerCamera.java | 37 -- .../packet/wrappers/AbstractPacket.java | 94 +++++ .../WrapperPlayServerEntityEquipment.java | 96 +++++ .../WrapperPlayServerRelEntityMove.java | 170 ++++++++ .../WrapperPlayServerRelEntityMoveLook.java | 170 ++++++++ .../WrapperPlayServerSpawnEntity.java | 313 ++++++++++++++ .../WrapperPlayServerWindowItems.java | 61 +++ .../hmccosmetics/user/Backpack.java | 34 +- .../hmccosmetics/user/BaseUser.java | 3 +- .../hmccosmetics/user/Equipment.java | 26 ++ .../hmccosmetics/user/UserManager.java | 20 +- .../hmccosmetics/user/Wardrobe.java | 4 +- 21 files changed, 1281 insertions(+), 247 deletions(-) delete mode 100644 common/src/main/java/io/github/fisher2911/hmccosmetics/packet/WrapperPlayServerCamera.java create mode 100644 common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/AbstractPacket.java create mode 100644 common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerEntityEquipment.java create mode 100644 common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerRelEntityMove.java create mode 100644 common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerRelEntityMoveLook.java create mode 100644 common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerSpawnEntity.java create mode 100644 common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerWindowItems.java diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 6cc91479..207d20b6 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -39,13 +39,13 @@ dependencies { compileOnly("com.mojang:authlib:1.5.25") compileOnly("org.spigotmc:spigot:1.16.5-R0.1-SNAPSHOT") compileOnly("org.jetbrains:annotations:22.0.0") - compileOnly("com.comphenix.protocol:ProtocolLib:4.8.0") + compileOnly("com.comphenix.protocol:ProtocolLib:5.0.0-SNAPSHOT") compileOnly("me.clip:placeholderapi:2.11.1") compileOnly("com.github.oraxen:oraxen:-SNAPSHOT") compileOnly("com.github.LoneDev6:API-ItemsAdder:2.5.4") compileOnly("net.citizensnpcs:citizens-main:2.0.29-SNAPSHOT") compileOnly("com.ticxo.modelengine:api:R2.5.0") - compileOnly("com.github.retrooper.packetevents:spigot:2.0-SNAPSHOT") + //compileOnly("com.github.retrooper.packetevents:spigot:2.0-SNAPSHOT") implementation("net.kyori:adventure-api:4.10.0") implementation ("net.kyori:adventure-text-minimessage:4.10.0-SNAPSHOT") implementation("net.kyori:adventure-platform-bukkit:4.0.1") @@ -111,7 +111,7 @@ bukkit { name = "HMCCosmetics" authors = listOf("MasterOfTheFish") softDepend = listOf("Multiverse", "PlaceholderAPI", "Oraxen", "ItemsAdder", "Citizens", "ModelEngine") - depend = listOf("packetevents") + depend = listOf("ProtocolLib") permissions { register("hmccosmetics.cmd.default") { default = BukkitPluginDescription.Permission.Default.OP diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/HMCCosmetics.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/HMCCosmetics.java index 1f216a94..9213f874 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/HMCCosmetics.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/HMCCosmetics.java @@ -1,6 +1,5 @@ package io.github.fisher2911.hmccosmetics; -import com.github.retrooper.packetevents.PacketEvents; import io.github.fisher2911.hmccosmetics.command.CosmeticsCommand; import io.github.fisher2911.hmccosmetics.concurrent.Threads; import io.github.fisher2911.hmccosmetics.config.Settings; @@ -107,7 +106,7 @@ public class HMCCosmetics extends JavaPlugin { @Override public void onDisable() { - PacketEvents.getAPI().terminate(); + //PacketEvents.getAPI().terminate(); this.saveTask.cancel(); this.database.saveAll(); this.messageHandler.close(); diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/config/SoundData.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/config/SoundData.java index 25ee2b66..fd6167a7 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/config/SoundData.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/config/SoundData.java @@ -1,17 +1,17 @@ package io.github.fisher2911.hmccosmetics.config; -import com.github.retrooper.packetevents.protocol.sound.SoundCategory; +import com.comphenix.protocol.wrappers.EnumWrappers; import net.minecraft.server.v1_16_R3.MinecraftKey; import org.bukkit.entity.Player; public class SoundData { private final String name; - private final SoundCategory soundCategory; + private final EnumWrappers.SoundCategory soundCategory; private final float volume; private final float pitch; - public SoundData(final String name, final SoundCategory soundCategory, final float volume, final float pitch) { + public SoundData(final String name, final EnumWrappers.SoundCategory soundCategory, final float volume, final float pitch) { this.name = name; this.soundCategory = soundCategory; this.volume = volume; @@ -30,7 +30,7 @@ public class SoundData { return pitch; } - public SoundCategory getSoundCategory() { + public EnumWrappers.SoundCategory getSoundCategory() { return soundCategory; } diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/config/SoundSerializer.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/config/SoundSerializer.java index d53cc877..28ed3bee 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/config/SoundSerializer.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/config/SoundSerializer.java @@ -1,6 +1,6 @@ package io.github.fisher2911.hmccosmetics.config; -import com.github.retrooper.packetevents.protocol.sound.SoundCategory; +import com.comphenix.protocol.wrappers.EnumWrappers; import org.checkerframework.checker.nullness.qual.Nullable; import org.spongepowered.configurate.ConfigurationNode; import org.spongepowered.configurate.serialize.SerializationException; @@ -37,7 +37,7 @@ public class SoundSerializer implements TypeSerializer { } else { soundData = new SoundData( soundName, - SoundCategory.valueOf(category), + EnumWrappers.SoundCategory.valueOf(category), volume, pitch ); diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/database/Database.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/database/Database.java index 71d2fdfa..f5624dd2 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/database/Database.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/database/Database.java @@ -16,13 +16,13 @@ import io.github.fisher2911.hmccosmetics.user.EntityIds; import io.github.fisher2911.hmccosmetics.user.NPCUser; import io.github.fisher2911.hmccosmetics.user.User; import io.github.fisher2911.hmccosmetics.user.Wardrobe; -import io.github.retrooper.packetevents.util.SpigotReflectionUtil; import org.bukkit.Bukkit; import org.bukkit.entity.Entity; import java.sql.SQLException; import java.util.List; import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; public class Database { @@ -234,6 +234,6 @@ public class Database { } public static int getNextEntityId() { - return SpigotReflectionUtil.generateEntityId(); + return new AtomicInteger().incrementAndGet(); } } diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/gui/ArmorItem.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/gui/ArmorItem.java index 3f95ec76..1b2625db 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/gui/ArmorItem.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/gui/ArmorItem.java @@ -348,18 +348,6 @@ public class ArmorItem extends WrappedGuiItem { }; } - @Nullable - public static Type fromPacketSlot(final com.github.retrooper.packetevents.protocol.player.EquipmentSlot slot) { - return switch (slot) { - case HELMET -> Type.HAT; - case CHEST_PLATE -> Type.CHEST_PLATE; - case LEGGINGS -> Type.PANTS; - case BOOTS -> Type.BOOTS; - case OFF_HAND -> Type.OFF_HAND; - default -> null; - }; - } - @Nullable public static Type fromEquipmentSlot(final EquipmentSlot slot) { for (final Type type : values()) { diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/listener/CosmeticFixListener.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/listener/CosmeticFixListener.java index dbbf931a..acb96c6e 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/listener/CosmeticFixListener.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/listener/CosmeticFixListener.java @@ -1,26 +1,13 @@ package io.github.fisher2911.hmccosmetics.listener; -import com.github.retrooper.packetevents.PacketEvents; -import com.github.retrooper.packetevents.event.PacketListenerAbstract; -import com.github.retrooper.packetevents.event.PacketReceiveEvent; -import com.github.retrooper.packetevents.event.PacketSendEvent; -import com.github.retrooper.packetevents.protocol.packettype.PacketType; -import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientClickWindow; -import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerWindowItems; import io.github.fisher2911.hmccosmetics.HMCCosmetics; import io.github.fisher2911.hmccosmetics.config.CosmeticSettings; import io.github.fisher2911.hmccosmetics.gui.ArmorItem; -import io.github.fisher2911.hmccosmetics.inventory.PlayerArmor; -import io.github.fisher2911.hmccosmetics.packet.PacketManager; import io.github.fisher2911.hmccosmetics.task.DelayedTask; import io.github.fisher2911.hmccosmetics.task.TaskManager; -import io.github.fisher2911.hmccosmetics.user.Equipment; import io.github.fisher2911.hmccosmetics.user.User; import io.github.fisher2911.hmccosmetics.user.UserManager; -import io.github.fisher2911.hmccosmetics.util.Utils; -import io.github.retrooper.packetevents.util.SpigotConversionUtil; import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -36,10 +23,7 @@ import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.Collections; import java.util.EnumSet; -import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -140,6 +124,7 @@ public class CosmeticFixListener implements Listener { } private void registerInventoryClickListener() { + /* PacketEvents.getAPI().getEventManager().registerListener( new PacketListenerAbstract() { @Override @@ -173,9 +158,11 @@ public class CosmeticFixListener implements Listener { } } ); + */ } private void updateOnClick(final Player player, final EquipmentSlot slot, final User user, final ArmorItem.Type type, final ItemStack current) { + /* final Location location = player.getLocation(); final Equipment equipment = Equipment.fromEntityEquipment(player.getEquipment()); final ItemStack cosmetic = userManager.getCosmeticItem( @@ -186,14 +173,14 @@ public class CosmeticFixListener implements Listener { ); if (cosmetic != null && cosmetic.getType() != Material.AIR) equipment.setItem(slot, cosmetic); - final List items = + final List items = userManager.getItemList(user, equipment, Collections.emptySet()); items.removeIf(e -> { - final com.github.retrooper.packetevents.protocol.player.EquipmentSlot s = e.getSlot(); - final ArmorItem.Type t = ArmorItem.Type.fromPacketSlot(s); + //final EquipmentSlot s = e.getSlot(); + final ArmorItem.Type t = ArmorItem.Type.fromPacketSlot(slot); if (t == null) return false; final ArmorItem armorItem = user.getPlayerArmor().getItem(t); - final ItemStack i = SpigotConversionUtil.toBukkitItemStack(e.getItem()); + final ItemStack i = e.getItem(slot); return armorItem.isEmpty() && i.equals(equipment.getItem(t.getSlot())); }); for (final Player other : Bukkit.getOnlinePlayers()) { @@ -203,9 +190,35 @@ public class CosmeticFixListener implements Listener { items ); } + */ } private void registerMenuChangeListener() { + /* + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(HMCCosmetics.getPlugin(HMCCosmetics.class), ListenerPriority.NORMAL) { + @Override + public void onPacketReceiving(PacketEvent event) { + if (!event.getPacketType().equals(PacketType.Play.Server.WINDOW_ITEMS)) return; + if (event.getPlayer() == null) return; + WrapperPlayServerWindowItems wrapper = new WrapperPlayServerWindowItems(event.getPacket()); + final int windowId = wrapper.getWindowId(); + + List items = new ArrayList<>(); + int count = items.size(); + for (int i = 0; i < count; i++) { + items.add(wrapper.getSlotData().get(count)); + } + + + //final WrapperPlayServerWindowItems packet = new WrapperPlayServerWindowItems(event); + + + + //final int windowId = packet.getWindowId(); + } + }); + */ + /* PacketEvents.getAPI().getEventManager().registerListener( new PacketListenerAbstract() { @Override @@ -214,7 +227,7 @@ public class CosmeticFixListener implements Listener { final WrapperPlayServerWindowItems packet = new WrapperPlayServerWindowItems(event); if (!(event.getPlayer() instanceof final Player player)) return; final int windowId = packet.getWindowId(); - final List itemStacks = packet.getItems(); + final List itemStacks = packet.getItems(); taskManager.submit(() -> { final Optional optionalUser = userManager.get(player.getUniqueId()); if (optionalUser.isEmpty()) return; @@ -222,7 +235,7 @@ public class CosmeticFixListener implements Listener { if (windowId != 0) return; final int size = itemStacks.size(); final PlayerArmor playerArmor = user.getPlayerArmor(); - final List equipmentList = new ArrayList<>(); + final List equipmentList = new ArrayList<>(); for (final ArmorItem armorItem : playerArmor.getArmorItems()) { final ArmorItem.Type type = armorItem.getType(); final EquipmentSlot slot = type.getSlot(); @@ -231,15 +244,15 @@ public class CosmeticFixListener implements Listener { if (packetSlot == -1) continue; if (packetSlot >= size) continue; - final ItemStack current = SpigotConversionUtil.toBukkitItemStack(itemStacks.get(packetSlot)); - final com.github.retrooper.packetevents.protocol.item.ItemStack setTo = - SpigotConversionUtil.fromBukkitItemStack(userManager.getCosmeticItem( + final ItemStack current = (itemStacks.get(packetSlot)); + final ItemStack setTo = + (userManager.getCosmeticItem( armorItem, current, ArmorItem.Status.APPLIED, slot )); - if (SpigotConversionUtil.fromBukkitItemStack(current).equals(setTo)) continue; + if ((current).equals(setTo)) continue; equipmentList.add(PacketManager.getEquipment(setTo, slot)); } userManager.sendUpdatePacket( @@ -247,10 +260,11 @@ public class CosmeticFixListener implements Listener { equipmentList ); }); -// packet.setItems(itemStacks); + packet.setItems(itemStacks); } } ); + */ } private int getPacketArmorSlot(final EquipmentSlot slot) { diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/listener/WardrobeListener.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/listener/WardrobeListener.java index ac8fcd1a..e15f9cc5 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/listener/WardrobeListener.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/listener/WardrobeListener.java @@ -1,9 +1,5 @@ package io.github.fisher2911.hmccosmetics.listener; -import com.github.retrooper.packetevents.PacketEvents; -import com.github.retrooper.packetevents.event.PacketListenerAbstract; -import com.github.retrooper.packetevents.event.PacketReceiveEvent; -import com.github.retrooper.packetevents.protocol.packettype.PacketType; import io.github.fisher2911.hmccosmetics.HMCCosmetics; import io.github.fisher2911.hmccosmetics.user.UserManager; import org.bukkit.Bukkit; @@ -20,6 +16,8 @@ public class WardrobeListener implements Listener { public WardrobeListener(final HMCCosmetics plugin) { this.plugin = plugin; this.userManager = this.plugin.getUserManager(); + // TODO: REDO this + /* PacketEvents.getAPI().getEventManager().registerListener( new PacketListenerAbstract() { @Override @@ -35,6 +33,8 @@ public class WardrobeListener implements Listener { ); } }); + + */ } @EventHandler diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java index 4ed67824..d5a43ffa 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java @@ -3,28 +3,18 @@ package io.github.fisher2911.hmccosmetics.packet; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.PacketContainer; -import com.github.retrooper.packetevents.PacketEvents; -import com.github.retrooper.packetevents.protocol.entity.data.EntityData; -import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; -import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose; -import com.github.retrooper.packetevents.protocol.entity.type.EntityType; -import com.github.retrooper.packetevents.protocol.player.*; -import com.github.retrooper.packetevents.util.Vector3d; -import com.github.retrooper.packetevents.wrapper.PacketWrapper; -import com.github.retrooper.packetevents.wrapper.play.server.*; +import com.comphenix.protocol.wrappers.*; import io.github.fisher2911.hmccosmetics.HMCCosmetics; -import io.github.retrooper.packetevents.util.SpigotConversionUtil; -import net.kyori.adventure.text.Component; +import io.github.fisher2911.hmccosmetics.packet.wrappers.WrapperPlayServerRelEntityMove; +import io.github.fisher2911.hmccosmetics.packet.wrappers.WrapperPlayServerRelEntityMoveLook; +import io.github.fisher2911.hmccosmetics.user.Equipment; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import java.lang.reflect.InvocationTargetException; -import java.util.Collection; -import java.util.List; -import java.util.Optional; -import java.util.UUID; +import java.util.*; public class PacketManager { // @@ -50,64 +40,81 @@ public class PacketManager { public static void sendArmorStandMetaContainer(final int armorStandId, final Player... sendTo) { for (final Player p : sendTo) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA); - packet.setMeta(String.valueOf(armorStandId), List.of( - new EntityData(0, EntityDataTypes.BYTE, (byte) 0x20), - new EntityData(15, EntityDataTypes.BYTE, (byte) 0x10) - ) - ); + packet.getIntegers().write(0, armorStandId); + WrappedDataWatcher metadata = new WrappedDataWatcher(); + if (metadata == null) return; + // 0x10 & 0x20 + WrappedDataWatcher.Serializer serializer = WrappedDataWatcher.Registry.get(Byte.class); + metadata.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(0, serializer), (byte) 0x20); + metadata.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(0, serializer), (byte) 0x10); + //metadata.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(0, WrappedDataWatcher.Registry.get(Byte.class)), (byte) 0x20); + packet.getWatchableCollectionModifier().write(0, metadata.getWatchableObjects()); + sendPacketAsync(p, packet); } } public static void sendCloudMetaData(int entityId, Player... sendTo) { for (final Player p : sendTo) { - sendPacketAsync(p, new WrapperPlayServerEntityMetadata( - entityId, + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA); + packet.getIntegers().write(0, entityId); + WrappedDataWatcher wrapper = new WrappedDataWatcher(); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(0, WrappedDataWatcher.Registry.get(Byte.class)), (byte) 0x20); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(6, WrappedDataWatcher.Registry.get(EnumWrappers.EntityPose.class)), EnumWrappers.EntityPose.STANDING); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(4, WrappedDataWatcher.Registry.get(Boolean.class)), false); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(8, WrappedDataWatcher.Registry.get(Float.class)), 0.0f); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(9, WrappedDataWatcher.Registry.get(int.class)), 0); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(11, WrappedDataWatcher.Registry.get(EnumWrappers.Particle.class)), 21); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(10, WrappedDataWatcher.Registry.get(Boolean.class)), false); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(3, WrappedDataWatcher.Registry.get(Boolean.class)), false); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(4, WrappedDataWatcher.Registry.get(Boolean.class)), false); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(5, WrappedDataWatcher.Registry.get(Boolean.class)), false); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(1, WrappedDataWatcher.Registry.get(int.class)), 300); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(7, WrappedDataWatcher.Registry.get(int.class)), 0); + packet.getWatchableCollectionModifier().write(0, wrapper.getWatchableObjects()); + /* + packet.setMeta(String.valueOf(entityId), List.of( - new EntityData(0, EntityDataTypes.BYTE, (byte) 0), new EntityData(2, EntityDataTypes.OPTIONAL_COMPONENT, Optional.empty()), - new EntityData(6, EntityDataTypes.ENTITY_POSE, EntityPose.STANDING), - new EntityData(4, EntityDataTypes.BOOLEAN, false), - new EntityData(8, EntityDataTypes.FLOAT, 0.0f), - new EntityData(9, EntityDataTypes.INT, 0), new EntityData(11, EntityDataTypes.PARTICLE, 21), - new EntityData(10, EntityDataTypes.BOOLEAN, false), new EntityData(1, EntityDataTypes.INT, 300), - new EntityData(3, EntityDataTypes.BOOLEAN, false), - new EntityData(7, EntityDataTypes.INT, 0), - new EntityData(5, EntityDataTypes.BOOLEAN, false) - ) - )); + new EntityData(7, EntityDataTypes.INT, 0) + )); + */ + sendPacketAsync(p, packet); } } - + /* public static void sendVelocityPacket(final int entityId, Player... sendTo) { for (final Player p : sendTo) { - sendPacketAsync(p, new WrapperPlayServerEntityVelocity( - entityId, - new Vector3d(0.5, 0.5, 0.5) - )); + + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_VELOCITY); + packet.setMeta(String.valueOf(entityId), new Vector3d(0.5, 0.5, 0.5)); + sendPacketAsync(p, packet); } } + */ public static void sendRelativeMovePacket(final int entityId, Player... sendTo) { for (final Player p : sendTo) { - sendPacketAsync(p, new WrapperPlayServerEntityRelativeMove( - entityId, - 0.0, - 0.0, - 0.0, - false - )); + PacketContainer packet = new PacketContainer(PacketType.Play.Server.REL_ENTITY_MOVE); + WrapperPlayServerRelEntityMove wrapper = new WrapperPlayServerRelEntityMove(packet); + wrapper.setDx(0.0); + wrapper.setDy(0.0); + wrapper.setDz(0.0); + wrapper.setOnGround(false); + + sendPacketAsync(p, wrapper.getHandle()); } } public static void sendHeadLookPacket(int entityId, float yaw, Player... sendTo) { for (final Player p : sendTo) { - sendPacketAsync(p, new WrapperPlayServerEntityHeadLook( - entityId, - yaw - )); + PacketContainer packet = new PacketContainer(PacketType.Play.Server.REL_ENTITY_MOVE_LOOK); + WrapperPlayServerRelEntityMoveLook wrapper = new WrapperPlayServerRelEntityMoveLook(packet); + wrapper.setYaw(yaw); + wrapper.setEntityID(entityId); + sendPacketAsync(p, wrapper.getHandle()); } } @@ -143,6 +150,25 @@ public class PacketManager { final UUID uuid, final Player... sendTo) { for (final Player p : sendTo) { + + PacketContainer packet = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY); + // Why java... why... + io.github.fisher2911.hmccosmetics.packet.wrappers.WrapperPlayServerSpawnEntity wrapper = new io.github.fisher2911.hmccosmetics.packet.wrappers.WrapperPlayServerSpawnEntity(packet); + wrapper.setEntityID(entityId); + wrapper.setUniqueId(uuid); + wrapper.setType(entityType); + wrapper.setYaw(location.getYaw()); + wrapper.setPitch(location.getPitch()); + wrapper.setX(location.getX()); + wrapper.setY(location.getY()); + wrapper.setZ(location.getZ()); + wrapper.setObjectData(0); + wrapper.setOptionalSpeedX(0); + wrapper.setOptionalSpeedY(0); + wrapper.setOptionalSpeedZ(0); + + sendPacketAsync(p, wrapper.getHandle()); + /* sendPacketAsync(p, new WrapperPlayServerSpawnEntity( entityId, Optional.of(uuid), @@ -154,6 +180,7 @@ public class PacketManager { 0, Optional.of(Vector3d.zero()) )); + */ } } @@ -165,17 +192,20 @@ public class PacketManager { int data, final Player... sendTo) { for (final Player p : sendTo) { + + /* sendPacketAsync(p, new WrapperPlayServerSpawnEntity( entityId, Optional.of(uuid), entityType, new Vector3d(location.getX(), location.getY(), location.getZ()), - 0, - 0, - 0f, + 0, // pitch + 0, // yaw + 0f, // headyaw data, Optional.of(Vector3d.zero()) )); + */ } } @@ -185,10 +215,20 @@ public class PacketManager { public static void sendInvisibilityPacket(final int entityId, final Player... sendTo) { for (final Player p : sendTo) { + + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA); + packet.getIntegers().write(0, entityId); + WrappedDataWatcher wrapper = new WrappedDataWatcher(); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(0, WrappedDataWatcher.Registry.get(Byte.class)), (byte) 0x20); + packet.getWatchableCollectionModifier().write(0, wrapper.getWatchableObjects()); + //packet.setMeta(String.valueOf(entityId), List.of(new EntityData(0, EntityDataTypes.BYTE, (byte) 0x20))); + /* sendPacketAsync(p, new WrapperPlayServerEntityMetadata( entityId, List.of(new EntityData(0, EntityDataTypes.BYTE, (byte) 0x20)) )); + */ + sendPacketAsync(p, packet); } } @@ -208,6 +248,16 @@ public class PacketManager { final Player... sendTo ) { for (final Player p : sendTo) { + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_TELEPORT); + packet.getIntegers().write(0, entityId); + packet.getDoubles().write(0, location.getX()); + packet.getDoubles().write(1, location.getY()); + packet.getDoubles().write(2, location.getZ()); + packet.getBytes().write(0, (byte) (location.getYaw() * 256.0F / 360.0F)); + packet.getBytes().write(1, (byte) (location.getPitch() * 256.0F / 360.0F)); + packet.getBooleans().write(0, onGround); + sendPacketAsync(p, packet); + /* sendPacketAsync(p, new WrapperPlayServerEntityTeleport( entityId, new Vector3d(location.getX(), location.getY(), location.getZ()), @@ -215,6 +265,8 @@ public class PacketManager { location.getPitch(), onGround )); + + */ } } @@ -236,13 +288,14 @@ public class PacketManager { final Player... sendTo ) { for (final Player p : sendTo) { - sendPacketAsync(p, new WrapperPlayServerEntityRelativeMove( - entityId, - to.getX() - from.getX(), - to.getY() - from.getY(), - to.getZ() - from.getZ(), - onGround - )); + PacketContainer packet = new PacketContainer(PacketType.Play.Server.REL_ENTITY_MOVE); + WrapperPlayServerRelEntityMove wrapper = new WrapperPlayServerRelEntityMove(packet); + wrapper.setEntityID(entityId); + wrapper.setDx(to.getX() - from.getX()); + wrapper.setDy(to.getY() - from.getY()); + wrapper.setDz(to.getZ() - from.getZ()); + wrapper.setOnGround(onGround); + sendPacketAsync(p, wrapper.getHandle()); } } @@ -260,32 +313,41 @@ public class PacketManager { final Player... sendTo ) { for (final Player p : sendTo) { + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ATTACH_ENTITY); + packet.getIntegers().write(0, balloonId); + packet.getIntegers().write(1, entityId); + // Leash? + packet.getBooleans().write(0, true); + /* sendPacketAsync(p, new WrapperPlayServerAttachEntity( balloonId, entityId, true )); + */ + sendPacketAsync(p, packet); } } public static void sendEquipmentPacket( - final List equipmentList, - final int entityId, - final Collection sendTo - ) { - sendEquipmentPacket(equipmentList, entityId, sendTo.toArray(new Player[0])); - } + final io.github.fisher2911.hmccosmetics.user.Equipment equipment, + final int entityID, + final Player... sendTo) { + List packets = new ArrayList<>(); + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_EQUIPMENT); - public static void sendEquipmentPacket( - final List equipmentList, - final int entityId, - final Player... sendTo - ) { - for (final Player p : sendTo) { - sendPacketAsync(p, new WrapperPlayServerEntityEquipment( - entityId, - equipmentList - )); + for (org.bukkit.inventory.EquipmentSlot slot : equipment.values()) { + io.github.fisher2911.hmccosmetics.packet.wrappers.WrapperPlayServerEntityEquipment wrapper = new io.github.fisher2911.hmccosmetics.packet.wrappers.WrapperPlayServerEntityEquipment(packet); + EnumWrappers.ItemSlot is = itemBukkitSlot(slot); + wrapper.setSlot(is); + wrapper.setItem(equipment.getItem(slot)); + packets.add(wrapper.getHandle()); + } + for (Player p : sendTo) { + for (PacketContainer pack : packets) { + // Why??? + sendPacketAsync(p, pack); + } } } @@ -305,12 +367,12 @@ public class PacketManager { final Player... sendTo ) { for (final Player p : sendTo) { - sendPacketAsync(p, new WrapperPlayServerEntityRotation( - entityId, - location.getYaw(), - location.getPitch(), - onGround - )); + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_LOOK); + packet.getIntegers().write(0, entityId); + packet.getBytes().write(0, (byte) (location.getYaw() * 256.0F / 360.0F)); + packet.getBytes().write(1, (byte) (location.getPitch() * 256.0F / 360.0F)); + packet.getBooleans().write(0, onGround); + sendPacketAsync(p, packet); } } @@ -328,10 +390,10 @@ public class PacketManager { final Player... sendTo ) { for (final Player p : sendTo) { - sendPacketAsync(p, new WrapperPlayServerEntityHeadLook( - entityId, - location.getYaw() - )); + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_HEAD_ROTATION); + packet.getIntegers().write(0, entityId); + packet.getBytes().write(0, (byte) location.getYaw()); + sendPacketAsync(p, packet); } } @@ -349,10 +411,11 @@ public class PacketManager { final Player... sendTo ) { for (final Player p : sendTo) { - sendPacketAsync(p, new WrapperPlayServerSetPassengers( - mountId, - new int[]{passengerId} - )); + + PacketContainer packet = new PacketContainer(PacketType.Play.Server.MOUNT); + packet.getIntegers().write(0, mountId); + packet.getIntegerArrays().write(0, new int[]{passengerId}); + sendPacketAsync(p, packet); } } @@ -362,13 +425,21 @@ public class PacketManager { public static void sendEntityDestroyPacket(final int entityId, final Player... sendTo) { for (final Player p : sendTo) { - sendPacketAsync(p, new WrapperPlayServerDestroyEntities(entityId)); + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY); + if (packet.getIntegers().getValues().size() > 0) { + packet.getIntegers().write(0, entityId); + sendPacketAsync(p, packet); + } + //sendPacketAsync(p, new WrapperPlayServerDestroyEntities(entityId)); } } public static void sendCameraPacket(final int entityId, final Player... sendTo) { for (final Player p : sendTo) { - sendPacketAsync(p, new WrapperPlayServerCamera(entityId)); + PacketContainer packet = new PacketContainer(PacketType.Play.Server.CAMERA); + packet.getIntegers().write(0, entityId); + sendPacketAsync(p, packet); + //sendPacketAsync(p, new WrapperPlayServerCamera(entityId)); } } @@ -430,6 +501,17 @@ public class PacketManager { final Player... sendTo ) { for (final Player p : sendTo) { + // Needs testing!!! + PacketContainer packet = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY); + packet.getIntegers().write(0, entityId); + packet.getUUIDs().write(0, uuid); + packet.getDoubles().write(0, location.getX()) + .write(1, location.getY()) + .write(2, location.getZ()); + packet.getFloat().write(0, location.getYaw()).write(1, location.getPitch()); + + sendPacketAsync(p, packet); + /* sendPacketAsync(p, new WrapperPlayServerSpawnPlayer( entityId, uuid, @@ -441,6 +523,7 @@ public class PacketManager { location.getPitch() ) )); + */ } } @@ -457,6 +540,12 @@ public class PacketManager { final UUID uuid, final Player... sendTo ) { + PacketContainer packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO); + packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.ADD_PLAYER); + packet.getPlayerInfoDataLists().write(0, List.of( + new PlayerInfoData(WrappedGameProfile.fromPlayer(skinnedPlayer), 0, EnumWrappers.NativeGameMode.CREATIVE, WrappedChatComponent.fromText(skinnedPlayer.getName())) + )); + /* final List textures = PacketEvents.getAPI().getPlayerManager().getUser(skinnedPlayer).getProfile().getTextureProperties(); final WrapperPlayServerPlayerInfo.PlayerData data = new WrapperPlayServerPlayerInfo.PlayerData( Component.text(""), @@ -468,11 +557,15 @@ public class PacketManager { GameMode.CREATIVE, 0 ); + */ for (final Player p : sendTo) { + sendPacketAsync(p, packet); + /* sendPacketAsync(p, new WrapperPlayServerPlayerInfo( WrapperPlayServerPlayerInfo.Action.ADD_PLAYER, data )); + */ } } @@ -489,6 +582,23 @@ public class PacketManager { ) { final byte mask = 0x01 | 0x02 | 0x04 | 0x08 | 0x010 | 0x020 | 0x40; for (final Player p : sendTo) { + + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA); + packet.getIntegers().write(0, playerId); + WrappedDataWatcher wrapper = new WrappedDataWatcher(); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(17, WrappedDataWatcher.Registry.get(Byte.class)), (byte) mask); + wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(15, WrappedDataWatcher.Registry.get(Byte.class)), (byte) 0x10); + packet.getWatchableCollectionModifier().write(0, wrapper.getWatchableObjects()); + /* + packet.setMeta(String.valueOf(playerId), List.of( + new EntityData(17, EntityDataTypes.BYTE, mask), + new EntityData(15, EntityDataTypes.BYTE, (byte) 0x10) + ) + ); + + */ + sendPacketAsync(p, packet); + /* sendPacketAsync(p, new WrapperPlayServerEntityMetadata( playerId, List.of( @@ -496,6 +606,7 @@ public class PacketManager { new EntityData(15, EntityDataTypes.BYTE, (byte) 0x10) ) )); + */ } } @@ -513,6 +624,13 @@ public class PacketManager { final Player... sendTo ) { for (final Player p : sendTo) { + PacketContainer packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO); + packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.REMOVE_PLAYER); + packet.getPlayerInfoDataLists().write(0, List.of( + new PlayerInfoData(WrappedGameProfile.fromPlayer(player), 0, EnumWrappers.NativeGameMode.SURVIVAL, WrappedChatComponent.fromText(player.getName())) + )); + sendPacketAsync(p, packet); + /* sendPacketAsync(p, new WrapperPlayServerPlayerInfo( WrapperPlayServerPlayerInfo.Action.REMOVE_PLAYER, new WrapperPlayServerPlayerInfo.PlayerData( @@ -525,50 +643,25 @@ public class PacketManager { 0 ) )); + */ } } - @Deprecated - public static void sendPacketAsync(final Player player, final PacketWrapper packet) { - Bukkit.getScheduler().runTaskAsynchronously(HMCCosmetics.getPlugin(HMCCosmetics.class), () -> - PacketEvents.getAPI().getPlayerManager().sendPacket(player, packet) - ); - } - public static void sendPacketAsync(final Player player, final PacketContainer packet) { Bukkit.getScheduler().runTaskAsynchronously(HMCCosmetics.getPlugin(HMCCosmetics.class), () -> { - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } + ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); }); } - public static void sendPacketAsyncSilently(final Player player, final PacketWrapper packet) { - Bukkit.getScheduler().runTaskAsynchronously(HMCCosmetics.getPlugin(HMCCosmetics.class), () -> - PacketEvents.getAPI().getPlayerManager().sendPacketSilently(player, packet) - ); - } - - - public static com.github.retrooper.packetevents.protocol.player.Equipment getEquipment( + public static io.github.fisher2911.hmccosmetics.user.Equipment getEquipment( final ItemStack itemStack, final org.bukkit.inventory.EquipmentSlot slot ) { - return getEquipment(SpigotConversionUtil.fromBukkitItemStack(itemStack), slot); + Equipment equip = new Equipment(); + equip.setItem(slot, itemStack); + return equip; } - - public static com.github.retrooper.packetevents.protocol.player.Equipment getEquipment( - final com.github.retrooper.packetevents.protocol.item.ItemStack itemStack, - final org.bukkit.inventory.EquipmentSlot slot - ) { - return new com.github.retrooper.packetevents.protocol.player.Equipment( - PacketManager.fromBukkitSlot(slot), - itemStack - ); - } - + /* public static EquipmentSlot fromBukkitSlot(final org.bukkit.inventory.EquipmentSlot slot) { return switch (slot) { case HEAD -> EquipmentSlot.HELMET; @@ -579,5 +672,51 @@ public class PacketManager { case OFF_HAND -> EquipmentSlot.OFF_HAND; }; } + */ + + + public static EnumWrappers.ItemSlot itemBukkitSlot(final org.bukkit.inventory.EquipmentSlot slot) { + return switch (slot) { + case HEAD -> EnumWrappers.ItemSlot.HEAD; + case CHEST -> EnumWrappers.ItemSlot.CHEST; + case LEGS -> EnumWrappers.ItemSlot.LEGS; + case FEET -> EnumWrappers.ItemSlot.FEET; + case HAND -> EnumWrappers.ItemSlot.MAINHAND; + case OFF_HAND -> EnumWrappers.ItemSlot.OFFHAND; + }; + } + + public enum EntityData { + + ON_FIRE((byte) 0x01), + CROUCHING((byte) 0x02), + SPRINTING((byte) 0x08), + SWIMMING((byte) 0x10), + INVISIBLE((byte) 0x20), + GLOWING((byte) 0x40), + ELYTRA_FLY((byte) 0x80); + + final byte bitMask; + + EntityData(byte bitMask) { + this.bitMask = bitMask; + } + + public byte getBitMask() { + return bitMask; + } + + public boolean isPresent(byte bits) { + return (this.bitMask & bits) == this.bitMask; + } + + public byte setBit(byte bits) { + return (byte) (bits | this.bitMask); + } + + public byte unsetBit(byte bits) { + return (byte) (bits & ~this.bitMask); + } + } } diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/WrapperPlayServerCamera.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/WrapperPlayServerCamera.java deleted file mode 100644 index 108dec81..00000000 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/WrapperPlayServerCamera.java +++ /dev/null @@ -1,37 +0,0 @@ -package io.github.fisher2911.hmccosmetics.packet; - -import com.github.retrooper.packetevents.protocol.packettype.PacketType; -import com.github.retrooper.packetevents.wrapper.PacketWrapper; - -public class WrapperPlayServerCamera extends PacketWrapper { - - private int entityID; - - public WrapperPlayServerCamera(final int entityID) { - super(PacketType.Play.Server.CAMERA); - this.entityID = entityID; - } - - @Override - public void read() { - this.entityID = this.readVarInt(); - } - - @Override - public void copy(WrapperPlayServerCamera wrapper) { - this.entityID = wrapper.entityID; - } - - public void write() { - this.writeVarInt(this.entityID); - } - - public int getEntityId() { - return this.entityID; - } - - public void setEntityIds(int entityID) { - this.entityID = entityID; - } - -} diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/AbstractPacket.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/AbstractPacket.java new file mode 100644 index 00000000..31859c81 --- /dev/null +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/AbstractPacket.java @@ -0,0 +1,94 @@ +package io.github.fisher2911.hmccosmetics.packet.wrappers; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketContainer; +import com.google.common.base.Objects; +import org.bukkit.entity.Player; + +import java.lang.reflect.InvocationTargetException; + +// Courtesy of Packet Wrapper +public class AbstractPacket { + + // The packet we will be modifying + protected PacketContainer handle; + + /** + * Constructs a new strongly typed wrapper for the given packet. + * + * @param handle - handle to the raw packet data. + * @param type - the packet type. + */ + protected AbstractPacket(PacketContainer handle, PacketType type) { + // Make sure we're given a valid packet + if (handle == null) + throw new IllegalArgumentException("Packet handle cannot be NULL."); + if (!Objects.equal(handle.getType(), type)) + throw new IllegalArgumentException(handle.getHandle() + + " is not a packet of type " + type); + + this.handle = handle; + } + + /** + * Retrieve a handle to the raw packet data. + * + * @return Raw packet data. + */ + public PacketContainer getHandle() { + return handle; + } + + /** + * Send the current packet to the given receiver. + * + * @param receiver - the receiver. + * @throws RuntimeException If the packet cannot be sent. + */ + public void sendPacket(Player receiver) { + ProtocolLibrary.getProtocolManager().sendServerPacket(receiver, + getHandle()); + } + + /** + * Send the current packet to all online players. + */ + public void broadcastPacket() { + ProtocolLibrary.getProtocolManager().broadcastServerPacket(getHandle()); + } + + /** + * Simulate receiving the current packet from the given sender. + * + * @param sender - the sender. + * @throws RuntimeException If the packet cannot be received. + * @deprecated Misspelled. recieve to receive + * @see #receivePacket(Player) + */ + @Deprecated + public void recievePacket(Player sender) { + try { + ProtocolLibrary.getProtocolManager().receiveClientPacket(sender, + getHandle()); + } catch (Exception e) { + throw new RuntimeException("Cannot recieve packet.", e); + } + } + + /** + * Simulate receiving the current packet from the given sender. + * + * @param sender - the sender. + * @throws RuntimeException if the packet cannot be received. + */ + public void receivePacket(Player sender) { + try { + ProtocolLibrary.getProtocolManager().receiveClientPacket(sender, + getHandle()); + } catch (Exception e) { + throw new RuntimeException("Cannot receive packet.", e); + } + } + +} diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerEntityEquipment.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerEntityEquipment.java new file mode 100644 index 00000000..f42115f4 --- /dev/null +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerEntityEquipment.java @@ -0,0 +1,96 @@ +package io.github.fisher2911.hmccosmetics.packet.wrappers; + +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.inventory.ItemStack; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; +import com.comphenix.protocol.wrappers.EnumWrappers.ItemSlot; + +public class WrapperPlayServerEntityEquipment extends AbstractPacket { + public static final PacketType TYPE = + PacketType.Play.Server.ENTITY_EQUIPMENT; + + public WrapperPlayServerEntityEquipment() { + super(new PacketContainer(TYPE), TYPE); + handle.getModifier().writeDefaults(); + } + + public WrapperPlayServerEntityEquipment(PacketContainer packet) { + super(packet, TYPE); + } + + /** + * Retrieve Entity ID. + *

+ * Notes: entity's ID + * + * @return The current Entity ID + */ + public int getEntityID() { + return handle.getIntegers().read(0); + } + + /** + * Set Entity ID. + * + * @param value - new value. + */ + public void setEntityID(int value) { + handle.getIntegers().write(0, value); + } + + /** + * Retrieve the entity of the painting that will be spawned. + * + * @param world - the current world of the entity. + * @return The spawned entity. + */ + public Entity getEntity(World world) { + return handle.getEntityModifier(world).read(0); + } + + /** + * Retrieve the entity of the painting that will be spawned. + * + * @param event - the packet event. + * @return The spawned entity. + */ + public Entity getEntity(PacketEvent event) { + return getEntity(event.getPlayer().getWorld()); + } + + public ItemSlot getSlot() { + return handle.getItemSlots().read(0); + } + + public void setSlot(ItemSlot value) { + if (handle.getItemSlots().getValues().size() > 0) { + handle.getItemSlots().write(0, value); + } + } + + /** + * Retrieve Item. + *

+ * Notes: item in slot format + * + * @return The current Item + */ + public ItemStack getItem() { + return handle.getItemModifier().read(0); + } + + /** + * Set Item. + * + * @param value - new value. + */ + public void setItem(ItemStack value) { + if (handle.getItemModifier().getValues().size() > 0) { + handle.getItemModifier().write(0, value); + } + } +} diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerRelEntityMove.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerRelEntityMove.java new file mode 100644 index 00000000..690a2a40 --- /dev/null +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerRelEntityMove.java @@ -0,0 +1,170 @@ +package io.github.fisher2911.hmccosmetics.packet.wrappers; + +import org.bukkit.World; +import org.bukkit.entity.Entity; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; + +public class WrapperPlayServerRelEntityMove extends AbstractPacket { + public static final PacketType TYPE = + PacketType.Play.Server.REL_ENTITY_MOVE; + + public WrapperPlayServerRelEntityMove() { + super(new PacketContainer(TYPE), TYPE); + handle.getModifier().writeDefaults(); + } + + public WrapperPlayServerRelEntityMove(PacketContainer packet) { + super(packet, TYPE); + } + + /** + * Retrieve Entity ID. + *

+ * Notes: entity's ID + * + * @return The current Entity ID + */ + public int getEntityID() { + return handle.getIntegers().read(0); + } + + /** + * Set Entity ID. + * + * @param value - new value. + */ + public void setEntityID(int value) { + handle.getIntegers().write(0, value); + } + + /** + * Retrieve the entity of the painting that will be spawned. + * + * @param world - the current world of the entity. + * @return The spawned entity. + */ + public Entity getEntity(World world) { + return handle.getEntityModifier(world).read(0); + } + + /** + * Retrieve the entity of the painting that will be spawned. + * + * @param event - the packet event. + * @return The spawned entity. + */ + public Entity getEntity(PacketEvent event) { + return getEntity(event.getPlayer().getWorld()); + } + + /** + * Retrieve DX. + * + * @return The current DX + */ + public double getDx() { + return handle.getShorts().read(0) / 4096D; + } + + /** + * Set DX. + * + * @param value - new value. + */ + public void setDx(double value) { + handle.getShorts().write(0, (short) (value * 4096)); + } + + /** + * Retrieve DY. + * + * @return The current DY + */ + public double getDy() { + return handle.getShorts().read(1) / 4096D; + } + + /** + * Set DY. + * + * @param value - new value. + */ + public void setDy(double value) { + handle.getShorts().write(1, (short) (value * 4096)); + } + + /** + * Retrieve DZ. + * + * @return The current DZ + */ + public double getDz() { + return handle.getShorts().read(2) / 4096D; + } + + /** + * Set DZ. + * + * @param value - new value. + */ + public void setDz(double value) { + handle.getShorts().write(2, (short) (value * 4096)); + } + + /** + * Retrieve the yaw of the current entity. + * + * @return The current Yaw + */ + public float getYaw() { + return (handle.getBytes().read(0) * 360.F) / 256.0F; + } + + /** + * Set the yaw of the current entity. + * + * @param value - new yaw. + */ + public void setYaw(float value) { + handle.getBytes().write(0, (byte) (value * 256.0F / 360.0F)); + } + + /** + * Retrieve the pitch of the current entity. + * + * @return The current pitch + */ + public float getPitch() { + return (handle.getBytes().read(1) * 360.F) / 256.0F; + } + + /** + * Set the pitch of the current entity. + * + * @param value - new pitch. + */ + public void setPitch(float value) { + handle.getBytes().write(1, (byte) (value * 256.0F / 360.0F)); + } + + /** + * Retrieve On Ground. + * + * @return The current On Ground + */ + public boolean getOnGround() { + return handle.getBooleans().read(0); + } + + /** + * Set On Ground. + * + * @param value - new value. + */ + public void setOnGround(boolean value) { + handle.getBooleans().write(0, value); + } +} diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerRelEntityMoveLook.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerRelEntityMoveLook.java new file mode 100644 index 00000000..373775f0 --- /dev/null +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerRelEntityMoveLook.java @@ -0,0 +1,170 @@ +package io.github.fisher2911.hmccosmetics.packet.wrappers; + +import org.bukkit.World; +import org.bukkit.entity.Entity; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; + +public class WrapperPlayServerRelEntityMoveLook extends AbstractPacket { + public static final PacketType TYPE = + PacketType.Play.Server.REL_ENTITY_MOVE_LOOK; + + public WrapperPlayServerRelEntityMoveLook() { + super(new PacketContainer(TYPE), TYPE); + handle.getModifier().writeDefaults(); + } + + public WrapperPlayServerRelEntityMoveLook(PacketContainer packet) { + super(packet, TYPE); + } + + /** + * Retrieve Entity ID. + *

+ * Notes: entity's ID + * + * @return The current Entity ID + */ + public int getEntityID() { + return handle.getIntegers().read(0); + } + + /** + * Set Entity ID. + * + * @param value - new value. + */ + public void setEntityID(int value) { + handle.getIntegers().write(0, value); + } + + /** + * Retrieve the entity of the painting that will be spawned. + * + * @param world - the current world of the entity. + * @return The spawned entity. + */ + public Entity getEntity(World world) { + return handle.getEntityModifier(world).read(0); + } + + /** + * Retrieve the entity of the painting that will be spawned. + * + * @param event - the packet event. + * @return The spawned entity. + */ + public Entity getEntity(PacketEvent event) { + return getEntity(event.getPlayer().getWorld()); + } + + /** + * Retrieve DX. + * + * @return The current DX + */ + public double getDx() { + return handle.getShorts().read(0) / 4096D; + } + + /** + * Set DX. + * + * @param value - new value. + */ + public void setDx(double value) { + handle.getShorts().write(0, (short) (value * 4096)); + } + + /** + * Retrieve DY. + * + * @return The current DY + */ + public double getDy() { + return handle.getShorts().read(1) / 4096D; + } + + /** + * Set DY. + * + * @param value - new value. + */ + public void setDy(double value) { + handle.getShorts().write(1, (short) (value * 4096)); + } + + /** + * Retrieve DZ. + * + * @return The current DZ + */ + public double getDz() { + return handle.getShorts().read(2) / 4096D; + } + + /** + * Set DZ. + * + * @param value - new value. + */ + public void setDz(double value) { + handle.getShorts().write(2, (short) (value * 4096)); + } + + /** + * Retrieve the yaw of the current entity. + * + * @return The current Yaw + */ + public float getYaw() { + return (handle.getBytes().read(0) * 360.F) / 256.0F; + } + + /** + * Set the yaw of the current entity. + * + * @param value - new yaw. + */ + public void setYaw(float value) { + handle.getBytes().write(0, (byte) (value * 256.0F / 360.0F)); + } + + /** + * Retrieve the pitch of the current entity. + * + * @return The current pitch + */ + public float getPitch() { + return (handle.getBytes().read(1) * 360.F) / 256.0F; + } + + /** + * Set the pitch of the current entity. + * + * @param value - new pitch. + */ + public void setPitch(float value) { + handle.getBytes().write(1, (byte) (value * 256.0F / 360.0F)); + } + + /** + * Retrieve On Ground. + * + * @return The current On Ground + */ + public boolean getOnGround() { + return handle.getBooleans().read(0); + } + + /** + * Set On Ground. + * + * @param value - new value. + */ + public void setOnGround(boolean value) { + handle.getBooleans().write(0, value); + } +} diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerSpawnEntity.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerSpawnEntity.java new file mode 100644 index 00000000..a1c6dfef --- /dev/null +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerSpawnEntity.java @@ -0,0 +1,313 @@ +package io.github.fisher2911.hmccosmetics.packet.wrappers; + +import java.util.UUID; + +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; +import com.comphenix.protocol.injector.PacketConstructor; + +public class WrapperPlayServerSpawnEntity extends AbstractPacket { + public static final PacketType TYPE = PacketType.Play.Server.SPAWN_ENTITY; + + private static PacketConstructor entityConstructor; + + public WrapperPlayServerSpawnEntity() { + super(new PacketContainer(TYPE), TYPE); + handle.getModifier().writeDefaults(); + } + + public WrapperPlayServerSpawnEntity(PacketContainer packet) { + super(packet, TYPE); + } + + public WrapperPlayServerSpawnEntity(Entity entity, int type, int objectData) { + super(fromEntity(entity, type, objectData), TYPE); + } + + // Useful constructor + private static PacketContainer fromEntity(Entity entity, int type, + int objectData) { + if (entityConstructor == null) + entityConstructor = + ProtocolLibrary.getProtocolManager() + .createPacketConstructor(TYPE, entity, type, + objectData); + return entityConstructor.createPacket(entity, type, objectData); + } + + /** + * Retrieve entity ID of the Object. + * + * @return The current EID + */ + public int getEntityID() { + return handle.getIntegers().read(0); + } + + /** + * Retrieve the entity that will be spawned. + * + * @param world - the current world of the entity. + * @return The spawned entity. + */ + public Entity getEntity(World world) { + return handle.getEntityModifier(world).read(0); + } + + /** + * Retrieve the entity that will be spawned. + * + * @param event - the packet event. + * @return The spawned entity. + */ + public Entity getEntity(PacketEvent event) { + return getEntity(event.getPlayer().getWorld()); + } + + /** + * Set entity ID of the Object. + * + * @param value - new value. + */ + public void setEntityID(int value) { + handle.getIntegers().write(0, value); + } + + public UUID getUniqueId() { + return handle.getUUIDs().read(0); + } + + public void setUniqueId(UUID value) { + handle.getUUIDs().write(0, value); + } + + /** + * Retrieve the x position of the object. + *

+ * Note that the coordinate is rounded off to the nearest 1/32 of a meter. + * + * @return The current X + */ + public double getX() { + return handle.getDoubles().read(0); + } + + /** + * Set the x position of the object. + * + * @param value - new value. + */ + public void setX(double value) { + handle.getDoubles().write(0, value); + } + + /** + * Retrieve the y position of the object. + *

+ * Note that the coordinate is rounded off to the nearest 1/32 of a meter. + * + * @return The current y + */ + public double getY() { + return handle.getDoubles().read(1); + } + + /** + * Set the y position of the object. + * + * @param value - new value. + */ + public void setY(double value) { + handle.getDoubles().write(1, value); + } + + /** + * Retrieve the z position of the object. + *

+ * Note that the coordinate is rounded off to the nearest 1/32 of a meter. + * + * @return The current z + */ + public double getZ() { + return handle.getDoubles().read(2); + } + + /** + * Set the z position of the object. + * + * @param value - new value. + */ + public void setZ(double value) { + handle.getDoubles().write(2, value); + } + + /** + * Retrieve the optional speed x. + *

+ * This is ignored if {@link #getObjectData()} is zero. + * + * @return The optional speed x. + */ + public double getOptionalSpeedX() { + return handle.getIntegers().read(1) / 8000.0D; + } + + /** + * Set the optional speed x. + * + * @param value - new value. + */ + public void setOptionalSpeedX(double value) { + handle.getIntegers().write(1, (int) (value * 8000.0D)); + } + + /** + * Retrieve the optional speed y. + *

+ * This is ignored if {@link #getObjectData()} is zero. + * + * @return The optional speed y. + */ + public double getOptionalSpeedY() { + return handle.getIntegers().read(2) / 8000.0D; + } + + /** + * Set the optional speed y. + * + * @param value - new value. + */ + public void setOptionalSpeedY(double value) { + handle.getIntegers().write(2, (int) (value * 8000.0D)); + } + + /** + * Retrieve the optional speed z. + *

+ * This is ignored if {@link #getObjectData()} is zero. + * + * @return The optional speed z. + */ + public double getOptionalSpeedZ() { + return handle.getIntegers().read(3) / 8000.0D; + } + + /** + * Set the optional speed z. + * + * @param value - new value. + */ + public void setOptionalSpeedZ(double value) { + handle.getIntegers().write(3, (int) (value * 8000.0D)); + } + + /** + * Retrieve the pitch. + * + * @return The current pitch. + */ + public float getPitch() { + return (handle.getIntegers().read(4) * 360.F) / 256.0F; + } + + /** + * Set the pitch. + * + * @param value - new pitch. + */ + public void setPitch(float value) { + handle.getIntegers().write(4, (int) (value * 256.0F / 360.0F)); + } + + /** + * Retrieve the yaw. + * + * @return The current Yaw + */ + public float getYaw() { + return (handle.getIntegers().read(5) * 360.F) / 256.0F; + } + + /** + * Set the yaw of the object spawned. + * + * @param value - new yaw. + */ + public void setYaw(float value) { + handle.getIntegers().write(5, (int) (value * 256.0F / 360.0F)); + } + + /** + * Retrieve the type of object. + * + * @return The current Type + */ + public EntityType getType() { + return handle.getEntityTypeModifier().read(0); + } + + /** + * Set the type of object. + * + * @param value - new value. + */ + public void setType(EntityType value) { + handle.getEntityTypeModifier().write(0, value); + } + + /** + * Retrieve object data. + *

+ * The content depends on the object type: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Object Type:Name:Description
ITEM_FRAMEOrientation0-3: South, West, North, East
FALLING_BLOCKBlock TypeBlockID | (Metadata << 0xC)
ProjectilesEntity IDThe entity ID of the thrower
Splash PotionsData ValuePotion data value.
+ * + * @return The current object Data + */ + public int getObjectData() { + return handle.getIntegers().read(6); + } + + /** + * Set object Data. + *

+ * The content depends on the object type. See {@link #getObjectData()} for + * more information. + * + * @param value - new object data. + */ + public void setObjectData(int value) { + handle.getIntegers().write(6, value); + } +} diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerWindowItems.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerWindowItems.java new file mode 100644 index 00000000..b15f054b --- /dev/null +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/wrappers/WrapperPlayServerWindowItems.java @@ -0,0 +1,61 @@ +package io.github.fisher2911.hmccosmetics.packet.wrappers; + +import java.util.List; + +import org.bukkit.inventory.ItemStack; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.events.PacketContainer; + +public class WrapperPlayServerWindowItems extends AbstractPacket { + public static final PacketType TYPE = PacketType.Play.Server.WINDOW_ITEMS; + + public WrapperPlayServerWindowItems() { + super(new PacketContainer(TYPE), TYPE); + handle.getModifier().writeDefaults(); + } + + public WrapperPlayServerWindowItems(PacketContainer packet) { + super(packet, TYPE); + } + + /** + * Retrieve Window ID. + *

+ * Notes: the id of window which items are being sent for. 0 for player + * inventory. + * + * @return The current Window ID + */ + public int getWindowId() { + return handle.getIntegers().read(0); + } + + /** + * Set Window ID. + * + * @param value - new value. + */ + public void setWindowId(int value) { + handle.getIntegers().write(0, value); + } + + /** + * Retrieve Slot data. + * + * @return The current Slot data + */ + public List getSlotData() { + return handle.getItemListModifier().read(0); + } + + /** + * Set Slot data. + * + * @param value - new value. + */ + public void setSlotData(List value) { + handle.getItemListModifier().write(0, value); + } + +} diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Backpack.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Backpack.java index 1eab0dd7..df54f4dd 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Backpack.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Backpack.java @@ -1,21 +1,20 @@ package io.github.fisher2911.hmccosmetics.user; -import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; -import com.github.retrooper.packetevents.protocol.item.type.ItemTypes; -import com.github.retrooper.packetevents.protocol.player.EquipmentSlot; import io.github.fisher2911.hmccosmetics.HMCCosmetics; import io.github.fisher2911.hmccosmetics.config.Settings; import io.github.fisher2911.hmccosmetics.gui.ArmorItem; import io.github.fisher2911.hmccosmetics.packet.PacketManager; -import io.github.retrooper.packetevents.util.SpigotConversionUtil; -import io.github.retrooper.packetevents.util.SpigotReflectionUtil; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.List; import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; public class Backpack { @@ -46,7 +45,7 @@ public class Backpack { if (currentSize == particleCount) return; if (currentSize < particleCount) { for (int i = currentSize; i < particleCount; i++) { - this.particleIDS.add(SpigotReflectionUtil.generateEntityId()); + this.particleIDS.add(new AtomicInteger().incrementAndGet()); } return; } @@ -54,7 +53,7 @@ public class Backpack { } private void spawnForOther(BaseUser owner, Player other, Location location) { - PacketManager.sendEntitySpawnPacket(location, this.armorStandID, EntityTypes.ARMOR_STAND, other); + PacketManager.sendEntitySpawnPacket(location, this.armorStandID, EntityType.ARMOR_STAND, other); PacketManager.sendArmorStandMetaContainer(this.armorStandID, other); PacketManager.sendRidingPacket(owner.getEntityId(), this.armorStandID, other); } @@ -64,13 +63,13 @@ public class Backpack { PacketManager.sendEntityNotLivingSpawnPacket( location, id, - EntityTypes.AREA_EFFECT_CLOUD, + EntityType.AREA_EFFECT_CLOUD, UUID.randomUUID(), 0, other ); } - PacketManager.sendEntitySpawnPacket(location, this.armorStandID, EntityTypes.ARMOR_STAND, other); + PacketManager.sendEntitySpawnPacket(location, this.armorStandID, EntityType.ARMOR_STAND, other); } public void despawn(Player player) { @@ -93,7 +92,7 @@ public class Backpack { final boolean isSelf = owner.getId().equals(other.getUniqueId()); final boolean firstPersonMode = settings.getCosmeticSettings().isFirstPersonBackpackMode(); final ArmorItem.Type type = isSelf && firstPersonMode ? ArmorItem.Type.SELF_BACKPACK : ArmorItem.Type.BACKPACK; - final List equipment = new ArrayList<>(); + //final List equipment = new ArrayList<>(); final int lookDownPitch = settings.getCosmeticSettings().getLookDownPitch(); final boolean hidden = !owner.shouldShow(other); final boolean isLookingDown = @@ -101,23 +100,20 @@ public class Backpack { isSelf && lookDownPitch != -1 && owner.isFacingDown(location, lookDownPitch); + Equipment equipment = Equipment.fromEntityEquipment(other.getEquipment()); if (hidden || isLookingDown) { - equipment.add(new com.github.retrooper.packetevents.protocol.player.Equipment( - EquipmentSlot.HELMET, - new com.github.retrooper.packetevents.protocol.item.ItemStack.Builder(). - type(ItemTypes.AIR). - build() - )); PacketManager.sendEquipmentPacket(equipment, this.armorStandID, other); return; } - final com.github.retrooper.packetevents.protocol.item.ItemStack itemStack = - SpigotConversionUtil.fromBukkitItemStack(owner.getPlayerArmor().getItem(type).getItemStack(ArmorItem.Status.APPLIED)); + final ItemStack itemStack = + owner.getPlayerArmor().getItem(type).getItemStack(ArmorItem.Status.APPLIED); + /* equipment.add(new com.github.retrooper.packetevents.protocol.player.Equipment( EquipmentSlot.HELMET, itemStack )); - + */ + equipment.setItem(EquipmentSlot.HEAD, itemStack); PacketManager.sendEquipmentPacket(equipment, this.armorStandID, other); PacketManager.sendArmorStandMetaContainer(this.armorStandID, other); PacketManager.sendRotationPacket(this.armorStandID, location, false, other); diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/BaseUser.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/BaseUser.java index cc71b18d..9e4df3cc 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/BaseUser.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/BaseUser.java @@ -1,7 +1,6 @@ package io.github.fisher2911.hmccosmetics.user; -import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import io.github.fisher2911.hmccosmetics.config.CosmeticSettings; import io.github.fisher2911.hmccosmetics.config.Settings; import io.github.fisher2911.hmccosmetics.gui.ArmorItem; @@ -139,7 +138,7 @@ public abstract class BaseUser { this.balloon.addPlayerToModel(other, id); } final int balloonId = this.getBalloonId(); - PacketManager.sendEntitySpawnPacket(actual, balloonId, EntityTypes.PUFFERFISH, other); + PacketManager.sendEntitySpawnPacket(actual, balloonId, EntityType.PUFFERFISH, other); PacketManager.sendInvisibilityPacket(balloonId, other); PacketManager.sendLeashPacket(balloonId, this.getEntityId(), other); this.updateBalloon(other, location, settings); diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Equipment.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Equipment.java index e0633e54..2daab3e6 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Equipment.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Equipment.java @@ -1,5 +1,9 @@ package io.github.fisher2911.hmccosmetics.user; +import com.comphenix.protocol.wrappers.EnumWrappers; +import com.google.common.collect.ImmutableList; +import io.github.fisher2911.hmccosmetics.packet.PacketManager; +import org.bukkit.entity.Player; import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; @@ -25,6 +29,24 @@ public class Equipment { return equipment; } + public static Equipment fromEntityEquipment(@Nullable final Player player) { + if (player == null) return new Equipment(); + final Equipment equipment = new Equipment(); + for (final EquipmentSlot slot : VALUES) { + equipment.setItem(slot, player.getInventory().getItem(slot)); + } + return equipment; + } + + public static Equipment fromEntityEquipment(@Nullable final User user) { + if (user == null) return new Equipment(); + final Equipment equipment = new Equipment(); + for (final EquipmentSlot slot : VALUES) { + equipment.setItem(slot, user.getPlayer().getInventory().getItem(slot)); + } + return equipment; + } + @Nullable public ItemStack getItem(final EquipmentSlot slot) { return this.equipment.get(slot); @@ -34,4 +56,8 @@ public class Equipment { this.equipment.put(slot, itemStack); } + public EquipmentSlot[] values() { + return VALUES; + } + } diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java index 7351bbc7..730cbb82 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java @@ -148,13 +148,13 @@ public class UserManager { return itemStack; } - public List getItemList( + public List getItemList( final BaseUser user, final Equipment equipment, final Set ignored ) { final PlayerArmor armor = user.getPlayerArmor(); - final List items = new ArrayList<>(); + final List items = new ArrayList<>(); for (final ArmorItem.Type type : ArmorItem.Type.values()) { final EquipmentSlot slot = type.getSlot(); if (slot == null) continue; @@ -174,8 +174,8 @@ public class UserManager { return items; } - public List getEmptyItemList() { - final List items = new ArrayList<>(); + public List getEmptyItemList() { + final List items = new ArrayList<>(); for (final EquipmentSlot slot : EquipmentSlot.values()) { items.add(PacketManager.getEquipment(new ItemStack(Material.AIR), slot)); } @@ -286,14 +286,14 @@ public class UserManager { final ArmorItem.Type type) { final EquipmentSlot slot = type.getSlot(); final ItemStack itemStack = this.getCosmeticItem(armorItem, wearing, ArmorItem.Status.APPLIED, slot); - final List itemList = new ArrayList<>(); + final List itemList = new ArrayList<>(); itemList.add(PacketManager.getEquipment(itemStack, slot)); this.sendUpdatePacket(user, itemList); } public void sendUpdatePacket( final BaseUser user, - List items + List items ) { // final Player player = user.getPlayer(); // if (player == null) return; @@ -307,18 +307,21 @@ public class UserManager { if (!user.shouldShow(other)) continue; if (!this.settings.getCosmeticSettings().isInViewDistance(location, other.getLocation())) continue; user.updateBackpack(other, this.settings); + PacketManager.sendEquipmentPacket(Equipment.fromEntityEquipment(other.getEquipment()), entityId, other); + /* PacketManager.sendEquipmentPacket( items, entityId, other ); + */ } } public void sendUpdatePacket( final BaseUser user, final Player other, - List items + List items ) { // final Player player = user.getPlayer(); // if (player == null) return; @@ -328,10 +331,13 @@ public class UserManager { if (other == null) return; if (!user.shouldShow(other)) return; if (!this.settings.getCosmeticSettings().isInViewDistance(location, other.getLocation())) return; + PacketManager.sendEquipmentPacket(Equipment.fromEntityEquipment(other.getEquipment()), entityId, other); + /* PacketManager.sendEquipmentPacket( items, entityId, other ); + */ } } diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Wardrobe.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Wardrobe.java index b3f64712..34ae60f4 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Wardrobe.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Wardrobe.java @@ -1,6 +1,5 @@ package io.github.fisher2911.hmccosmetics.user; -import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import io.github.fisher2911.hmccosmetics.HMCCosmetics; import io.github.fisher2911.hmccosmetics.config.WardrobeSettings; import io.github.fisher2911.hmccosmetics.gui.ArmorItem; @@ -11,6 +10,7 @@ import io.github.fisher2911.hmccosmetics.task.Task; import io.github.fisher2911.hmccosmetics.task.TaskChain; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.util.Vector; import org.jetbrains.annotations.Nullable; @@ -61,7 +61,7 @@ public class Wardrobe extends User { PacketManager.sendEntitySpawnPacket( settings.getViewerLocation(), this.entityIds.wardrobeViewer(), - EntityTypes.ARMOR_STAND, + EntityType.ARMOR_STAND, viewer ); PacketManager.sendCameraPacket(