From ec2498fc48e1a16a70f8d5f2f6322cf800971f7f Mon Sep 17 00:00:00 2001 From: 502647092 Date: Fri, 17 Mar 2017 17:40:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4=E8=99=9A=E6=8B=9F?= =?UTF-8?q?=E6=82=AC=E6=B5=AE=E7=89=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../QuickShop/Shop/Item/FakeItem.java | 32 +++++++++---------- .../QuickShop/Shop/Item/FakeItem_17_18.java | 20 +++++------- .../QuickShop/Shop/Item/FakeItem_19_111.java | 21 ++++++------ 3 files changed, 32 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem.java b/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem.java index f420768..8f0f9f7 100644 --- a/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem.java +++ b/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem.java @@ -46,23 +46,20 @@ public abstract class FakeItem extends DisplayItem { this.eid = getFakeEntityId(); this.uuid = UUID.randomUUID(); } + public static boolean isRegistered() { return registered; } public static void register(final Plugin plugin) { - if (registered) { - return; - } + if (registered) { return; } final PluginManager pm = Bukkit.getPluginManager(); final Plugin p = pm.getPlugin("ProtocolLib"); if (p != null) { if (!p.isEnabled()) { pm.enablePlugin(p); } - if (!p.isEnabled()) { - throw new IllegalStateException("前置插件ProtocolLib启动失败 请检查版本."); - } + if (!p.isEnabled()) { throw new IllegalStateException("前置插件ProtocolLib启动失败 请检查版本."); } } else { throw new IllegalStateException("服务器未找到前置插件ProtocolLib."); } @@ -154,12 +151,8 @@ public abstract class FakeItem extends DisplayItem { } private void create() { - if (!registered) { - throw new IllegalStateException("You have to call the register method first."); - } - if (created) { - return; - } + if (!registered) { throw new IllegalStateException("You have to call the register method first."); } + if (created) { return; } ProtocolLibrary.getProtocolManager().broadcastServerPacket(getSpawnPacket()); ProtocolLibrary.getProtocolManager().broadcastServerPacket(getVelocityPacket()); ProtocolLibrary.getProtocolManager().broadcastServerPacket(getMetadataPacket()); @@ -175,9 +168,7 @@ public abstract class FakeItem extends DisplayItem { } private void destory() { - if (!created) { - return; - } + if (!created) { return; } ProtocolLibrary.getProtocolManager().broadcastServerPacket(getDestoryPacket()); final String chunkId = getChunkIdentifyString(location.getChunk()); @@ -204,8 +195,15 @@ public abstract class FakeItem extends DisplayItem { return fakePacket; } - protected abstract PacketContainer getMetadataPacket(); + protected PacketContainer getMetadataPacket() { + return setMetadataPacket(ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_METADATA)); + } - protected abstract PacketContainer getSpawnPacket(); + protected abstract PacketContainer setMetadataPacket(PacketContainer fakePacket); + protected PacketContainer getSpawnPacket() { + return setSpawnPacket(ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SPAWN_ENTITY)); + } + + protected abstract PacketContainer setSpawnPacket(PacketContainer fakePacket); } diff --git a/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_17_18.java b/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_17_18.java index ca710c8..6dc9fa0 100644 --- a/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_17_18.java +++ b/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_17_18.java @@ -6,9 +6,8 @@ import java.util.List; import org.bukkit.Location; import org.bukkit.inventory.ItemStack; -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.wrappers.WrappedWatchableObject; /** @@ -29,8 +28,7 @@ public class FakeItem_17_18 extends FakeItem { } @Override - protected PacketContainer getMetadataPacket() { - final PacketContainer fakePacket = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_METADATA); + protected PacketContainer setMetadataPacket(PacketContainer fakePacket) { fakePacket.getIntegers().write(0, eid); final WrappedWatchableObject itemMeta = new WrappedWatchableObject(10, itemStack); final List entityMetaList = new ArrayList<>(1); @@ -40,14 +38,12 @@ public class FakeItem_17_18 extends FakeItem { } @Override - protected PacketContainer getSpawnPacket() { - final PacketContainer fakePacket = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SPAWN_ENTITY); - fakePacket.getIntegers().write(0, eid); - fakePacket.getIntegers().write(1, getNormalizedDistance(location.getX())); - fakePacket.getIntegers().write(2, getNormalizedDistance(location.getY())); - fakePacket.getIntegers().write(3, getNormalizedDistance(location.getZ())); - fakePacket.getIntegers().write(9, 2); + protected PacketContainer setSpawnPacket(PacketContainer fakePacket) { + StructureModifier is = fakePacket.getIntegers(); + is.write(0, eid); + is.write(1, getNormalizedDistance(location.getX())); + is.write(2, getNormalizedDistance(location.getY())); + is.write(3, getNormalizedDistance(location.getZ())); return fakePacket; } - } diff --git a/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_19_111.java b/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_19_111.java index d7072d6..23d6703 100644 --- a/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_19_111.java +++ b/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_19_111.java @@ -3,9 +3,8 @@ package org.maxgamer.QuickShop.Shop.Item; import org.bukkit.Location; import org.bukkit.inventory.ItemStack; -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject; @@ -25,8 +24,7 @@ public class FakeItem_19_111 extends FakeItem { } @Override - protected PacketContainer getMetadataPacket() { - final PacketContainer fakePacket = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_METADATA); + protected PacketContainer setMetadataPacket(PacketContainer fakePacket) { fakePacket.getIntegers().write(0, eid); final WrappedDataWatcher wr = new WrappedDataWatcher(); final Serializer serializer = WrappedDataWatcher.Registry.getItemStackSerializer(true); @@ -37,14 +35,13 @@ public class FakeItem_19_111 extends FakeItem { } @Override - protected PacketContainer getSpawnPacket() { - final PacketContainer fakePacket = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SPAWN_ENTITY); - fakePacket.getIntegers().write(0, eid); - fakePacket.getModifier().write(1, uuid); - fakePacket.getDoubles().write(0, location.getX()); - fakePacket.getDoubles().write(1, location.getY()); - fakePacket.getDoubles().write(2, location.getZ()); - fakePacket.getIntegers().write(6, 2); + protected PacketContainer setSpawnPacket(PacketContainer fakePacket) { + StructureModifier mdf = fakePacket.getModifier(); + mdf.write(0, eid); + mdf.write(1, uuid); + mdf.write(2, location.getX()); + mdf.write(3, location.getY()); + mdf.write(4, location.getZ()); return fakePacket; } }