diff --git a/pom.xml b/pom.xml index 98c93c4..7df3611 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ org.apache.maven.plugins maven-shade-plugin - 2.3 + 2.4.3 false true @@ -57,8 +57,8 @@ §b1.9.5 - §a1.10+兼容虚拟悬浮物...; UTF-8 - 1.7 - 1.7 + 1.8 + 1.8 @@ -78,7 +78,7 @@ pw.yumc YumCore jar - 1.6 + 1.8 io.github.Cnly.WowSuchCleaner diff --git a/src/main/java/org/maxgamer/QuickShop/Config/ConfigManager.java b/src/main/java/org/maxgamer/QuickShop/Config/ConfigManager.java index 05c5100..d540351 100644 --- a/src/main/java/org/maxgamer/QuickShop/Config/ConfigManager.java +++ b/src/main/java/org/maxgamer/QuickShop/Config/ConfigManager.java @@ -12,7 +12,8 @@ import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; import org.maxgamer.QuickShop.QuickShop; -import org.maxgamer.QuickShop.Shop.Item.FakeItem_17_18; +import org.maxgamer.QuickShop.Shop.Item.DisplayItem; +import org.maxgamer.QuickShop.Shop.Item.FakeItem_18; import org.maxgamer.QuickShop.Shop.Item.FakeItem_19_111; import pw.yumc.YumCore.bukkit.Log; @@ -92,26 +93,7 @@ public class ConfigManager { this.warnings = Collections.emptySet(); this.prevent = config.getStringList("prevent"); if (config.getBoolean("fakeitem", true)) { - try { - plugin.getLogger().info("启用虚拟悬浮物 尝试启动中..."); - FakeItem_19_111.register(plugin); - new FakeItem_19_111(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), new ItemStack(Material.STONE)).spawn(); - plugin.getLogger().info("虚拟悬浮物功能测试正常(1.9-1.11.2)..."); - fakeItem = true; - } catch (final Throwable e) { - Log.d(e); - try { - FakeItem_17_18.register(plugin); - new FakeItem_17_18(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), new ItemStack(Material.STONE)).spawn(); - plugin.getLogger().info("虚拟悬浮物功能测试正常(1.7-1.8)..."); - fakeItem = true; - } catch (final Throwable e2) { - plugin.getLogger().warning("+========================================="); - plugin.getLogger().warning("| 警告: 启动虚拟物品失败 使用原版悬浮物品..."); - plugin.getLogger().warning("+========================================="); - Log.d(e2); - } - } + DisplayItem.init(); } if (Bukkit.getVersion().contains("Paper") || Bukkit.getVersion().contains("Torch")) { Log.d("辣鸡 Paper 毁我异步命令!"); diff --git a/src/main/java/org/maxgamer/QuickShop/Listeners/BlockListener.java b/src/main/java/org/maxgamer/QuickShop/Listeners/BlockListener.java index fc6e53d..87a7bf7 100644 --- a/src/main/java/org/maxgamer/QuickShop/Listeners/BlockListener.java +++ b/src/main/java/org/maxgamer/QuickShop/Listeners/BlockListener.java @@ -22,6 +22,8 @@ import org.maxgamer.QuickShop.Shop.ShopAction; import org.maxgamer.QuickShop.Util.MsgUtil; import org.maxgamer.QuickShop.Util.Util; +import pw.yumc.YumCore.bukkit.Log; + public class BlockListener implements Listener { private final QuickShop plugin; @@ -88,14 +90,13 @@ public class BlockListener implements Listener { public void onPlace(final BlockPlaceEvent e) { if (e.isCancelled()) { return; } final BlockState bs = e.getBlock().getState(); - if (!(bs instanceof DoubleChest)) { return; } final Block b = e.getBlock(); final Player p = e.getPlayer(); final Block chest = Util.getSecondHalf(b); if (chest != null) { Shop shop = plugin.getShopManager().getShop(chest.getLocation()); if (shop != null) { - if (!shop.getOwner().equals(p.getName())) { + if (!p.getName().equals(shop.getOwner())) { e.setCancelled(true); p.sendMessage(MsgUtil.p("no-double-chests")); return; diff --git a/src/main/java/org/maxgamer/QuickShop/Listeners/LockListener.java b/src/main/java/org/maxgamer/QuickShop/Listeners/LockListener.java index c88ee1f..0d4d670 100644 --- a/src/main/java/org/maxgamer/QuickShop/Listeners/LockListener.java +++ b/src/main/java/org/maxgamer/QuickShop/Listeners/LockListener.java @@ -63,7 +63,7 @@ public class LockListener implements Listener { Shop shop = plugin.getShopManager().getShop(b.getLocation()); // Make sure they're not using the non-shop half of a double chest. if (!hasSecondHalf(shop, b)) { return; } - if (shop != null && !shop.getOwner().equals(p.getName())) { + if (shop != null && !p.getName().equals(shop.getOwner())) { if (p.hasPermission("quickshop.other.open")) { p.sendMessage(MsgUtil.p("bypassing-lock")); return; diff --git a/src/main/java/org/maxgamer/QuickShop/Shop/Item/DisplayItem.java b/src/main/java/org/maxgamer/QuickShop/Shop/Item/DisplayItem.java index 44b226c..eeb8ed9 100644 --- a/src/main/java/org/maxgamer/QuickShop/Shop/Item/DisplayItem.java +++ b/src/main/java/org/maxgamer/QuickShop/Shop/Item/DisplayItem.java @@ -1,36 +1,61 @@ package org.maxgamer.QuickShop.Shop.Item; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.entity.Item; +import org.bukkit.inventory.ItemStack; import org.maxgamer.QuickShop.QuickShop; import org.maxgamer.QuickShop.Shop.ContainerShop; import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.P; -import pw.yumc.YumCore.bukkit.compatible.C; /** * @author Netherfoam A display item, that spawns a block above the chest and * cannot be interacted with. */ public abstract class DisplayItem { - public static QuickShop plugin = P.getPlugin(); + private static QuickShop plugin = P.getPlugin(); + + private static Class displayItemClass; + + private static Constructor constructor; + + public static void init() { + List> fakeItems = Arrays.asList(FakeItem_19_111.class, FakeItem_18.class, FakeItem_17.class); + Log.i("启用虚拟悬浮物 尝试启动中..."); + FakeItem.register(plugin); + fakeItems.forEach(c -> { + try { + c.getConstructor(Location.class, ItemStack.class).newInstance(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), new ItemStack(Material.STONE)).spawn(); + displayItemClass = c; + Log.i("虚拟悬浮物功能测试正常(%s)...", c.getSimpleName()); + } catch (Throwable e) { + Log.d(e); + } + }); + if (displayItemClass == null) { + displayItemClass = NormalItem.class; + Log.w("+========================================="); + Log.w("| 警告: 启动虚拟物品失败 使用原版悬浮物品..."); + Log.w("+========================================="); + } + try { + constructor = displayItemClass.getConstructor(Location.class, ItemStack.class); + } catch (NoSuchMethodException ignored) { + } + } public static DisplayItem create(final ContainerShop shop) { - String ver = C.getNMSVersion(); - if (plugin.getConfigManager().isDisplay()) { - if (plugin.getConfigManager().isFakeItem()) { - try { - if (Integer.parseInt(ver.split("_")[1]) > 8) { - return new FakeItem_19_111(shop.getLocation(), shop.getItem()); - } else { - return new FakeItem_17_18(shop.getLocation(), shop.getItem()); - } - } catch (final Throwable e) { - Log.d(e); - } - } - return new NormalItem(shop, shop.getItem()); + if (plugin.getConfigManager().isDisplay()) try { + return constructor.newInstance(shop.getLocation(), shop.getItem()); + } catch (InstantiationException | InvocationTargetException | IllegalAccessException ignored) { } return null; } 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.java similarity index 90% rename from src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_17_18.java rename to src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_17.java index 6dc9fa0..6ad5053 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.java @@ -1,49 +1,49 @@ -package org.maxgamer.QuickShop.Shop.Item; - -import java.util.ArrayList; -import java.util.List; - -import org.bukkit.Location; -import org.bukkit.inventory.ItemStack; - -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.reflect.StructureModifier; -import com.comphenix.protocol.wrappers.WrappedWatchableObject; - -/** - * Minecraft 虚拟悬浮物品工具类 - * 需要depend ProtocolLib - * - * @author 橙子(chengzi) - * @version 1.0.1 - */ -public class FakeItem_17_18 extends FakeItem { - - public FakeItem_17_18(Location loc, final ItemStack item) { - super(loc, item); - } - - private static int getNormalizedDistance(final double value) { - return (int) Math.floor(value * 32.0D); - } - - @Override - protected PacketContainer setMetadataPacket(PacketContainer fakePacket) { - fakePacket.getIntegers().write(0, eid); - final WrappedWatchableObject itemMeta = new WrappedWatchableObject(10, itemStack); - final List entityMetaList = new ArrayList<>(1); - entityMetaList.add(itemMeta); - fakePacket.getWatchableCollectionModifier().write(0, entityMetaList); - return fakePacket; - } - - @Override - 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; - } -} +package org.maxgamer.QuickShop.Shop.Item; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.inventory.ItemStack; + +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.reflect.StructureModifier; +import com.comphenix.protocol.wrappers.WrappedWatchableObject; + +/** + * Minecraft 虚拟悬浮物品工具类 + * 需要depend ProtocolLib + * + * @author 橙子(chengzi) + * @version 1.0.1 + */ +public class FakeItem_17 extends FakeItem { + + public FakeItem_17(Location loc, final ItemStack item) { + super(loc, item); + } + + private static int getNormalizedDistance(final double value) { + return (int) Math.floor(value * 32.0D); + } + + @Override + protected PacketContainer setMetadataPacket(PacketContainer fakePacket) { + fakePacket.getIntegers().write(0, eid); + final WrappedWatchableObject itemMeta = new WrappedWatchableObject(10, itemStack); + final List entityMetaList = new ArrayList<>(1); + entityMetaList.add(itemMeta); + fakePacket.getWatchableCollectionModifier().write(0, entityMetaList); + return fakePacket; + } + + @Override + 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_18.java b/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_18.java new file mode 100644 index 0000000..c624b26 --- /dev/null +++ b/src/main/java/org/maxgamer/QuickShop/Shop/Item/FakeItem_18.java @@ -0,0 +1,49 @@ +package org.maxgamer.QuickShop.Shop.Item; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.inventory.ItemStack; + +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.reflect.StructureModifier; +import com.comphenix.protocol.wrappers.WrappedWatchableObject; + +/** + * Minecraft 虚拟悬浮物品工具类 + * 需要depend ProtocolLib + * + * @author 橙子(chengzi) + * @version 1.0.1 + */ +public class FakeItem_18 extends FakeItem { + + public FakeItem_18(Location loc, final ItemStack item) { + super(loc, item); + } + + private static int getNormalizedDistance(final double value) { + return (int) Math.floor(value * 32.0D); + } + + @Override + protected PacketContainer setMetadataPacket(PacketContainer fakePacket) { + fakePacket.getIntegers().write(0, eid); + final WrappedWatchableObject itemMeta = new WrappedWatchableObject(10, itemStack); + final List entityMetaList = new ArrayList<>(1); + entityMetaList.add(itemMeta); + fakePacket.getWatchableCollectionModifier().write(0, entityMetaList); + return fakePacket; + } + + @Override + 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/NormalItem.java b/src/main/java/org/maxgamer/QuickShop/Shop/Item/NormalItem.java index 9e85217..9cec126 100644 --- a/src/main/java/org/maxgamer/QuickShop/Shop/Item/NormalItem.java +++ b/src/main/java/org/maxgamer/QuickShop/Shop/Item/NormalItem.java @@ -16,21 +16,21 @@ import org.maxgamer.QuickShop.Util.NMS; public class NormalItem extends DisplayItem { private final ItemStack iStack; private Item item; - private final Shop shop; + private final Location location; // private Location displayLoc; /** * Creates a new display item. * - * @param shop + * @param location * The shop (See Shop) * @param iStack * The item stack to clone properties of the display item from. */ - public NormalItem(final Shop shop, final ItemStack iStack) { - this.shop = shop; + public NormalItem(final Location location, final ItemStack iStack) { + this.location = location; this.iStack = iStack.clone(); - // this.displayLoc = shop.getLocation().clone().add(0.5, 1.2, 0.5); + // this.displayLoc = location().clone().add(0.5, 1.2, 0.5); } /** @@ -39,7 +39,7 @@ public class NormalItem extends DisplayItem { */ @Override public Location getDisplayLocation() { - return this.shop.getLocation().clone().add(0.5, 1.2, 0.5); + return this.location.clone().add(0.5, 1.2, 0.5); } /** @@ -65,8 +65,8 @@ public class NormalItem extends DisplayItem { */ @Override public boolean removeDupe() { - if (shop.getLocation().getWorld() == null) { return false; } - final Location displayLoc = shop.getLocation().getBlock().getRelative(0, 1, 0).getLocation(); + if (location.getWorld() == null) { return false; } + final Location displayLoc = location.getBlock().getRelative(0, 1, 0).getLocation(); boolean removed = false; final Chunk c = displayLoc.getChunk(); for (final Entity e : c.getEntities()) { @@ -77,7 +77,7 @@ public class NormalItem extends DisplayItem { continue; } final Location eLoc = e.getLocation().getBlock().getLocation(); - if (eLoc.equals(displayLoc) || eLoc.equals(shop.getLocation())) { + if (eLoc.equals(displayLoc) || eLoc.equals(location)) { e.remove(); removed = true; } @@ -99,10 +99,10 @@ public class NormalItem extends DisplayItem { */ @Override public void spawn() { - if (shop.getLocation().getWorld() == null) { return; } + if (location.getWorld() == null) { return; } final Location dispLoc = this.getDisplayLocation(); try { - this.item = shop.getLocation().getWorld().dropItem(dispLoc, this.iStack); + this.item = location.getWorld().dropItem(dispLoc, this.iStack); this.item.setVelocity(new Vector(0, 0.1, 0)); NMS.safeGuard(this.item); } catch (final Exception ignored) { diff --git a/src/main/java/org/maxgamer/QuickShop/Util/Util.java b/src/main/java/org/maxgamer/QuickShop/Util/Util.java index 92ead99..a7229b8 100644 --- a/src/main/java/org/maxgamer/QuickShop/Util/Util.java +++ b/src/main/java/org/maxgamer/QuickShop/Util/Util.java @@ -16,6 +16,8 @@ import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.material.MaterialData; import org.bukkit.material.Sign; import org.maxgamer.QuickShop.QuickShop; + +import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.global.L10N; import java.text.DecimalFormat; @@ -176,6 +178,7 @@ public class Util { * @return the block which is also a chest and connected to b. */ public static Block getSecondHalf(final Block b) { + Log.d("%s", b); if (!b.getType().toString().contains("CHEST")) { return null; } final Block[] blocks = new Block[4]; blocks[0] = b.getRelative(1, 0, 0); @@ -483,7 +486,7 @@ public class Util { } if (book1) { // They are the same here (both true or both // false). So if one is true, the other is - // true. + // true. final Map ench1 = ((EnchantmentStorageMeta) stack1.getItemMeta()).getStoredEnchants(); final Map ench2 = ((EnchantmentStorageMeta) stack2.getItemMeta()).getStoredEnchants(); if (!ench1.equals(ench2)) { return false; // Enchants aren't the same.