From 2ab858b0c474d121cf8d151f44854ac16b675132 Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Mon, 30 Jul 2018 16:25:17 +0800 Subject: [PATCH] Test code --- .../maxgamer/quickshop/Shop/DisplayItem.java | 240 +++++++++--------- .../java/org/maxgamer/quickshop/Util/NMS.java | 26 +- 2 files changed, 144 insertions(+), 122 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java index 30930045b..10be845fb 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java @@ -1,121 +1,121 @@ -package org.maxgamer.quickshop.Shop; - -import org.bukkit.Chunk; -import org.bukkit.Location; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Item; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; -import org.maxgamer.quickshop.QuickShop; -import org.maxgamer.quickshop.Util.NMS; - -/** - * @author Netherfoam A display item, that spawns a block above the chest and - * cannot be interacted with. - */ -public class DisplayItem { - private Shop shop; - private ItemStack iStack; - private Item item; - - // private Location displayLoc; - /** - * Creates a new display item. - * - * @param shop - * The shop (See Shop) - * @param iStack - * The item stack to clone properties of the display item from. - */ - public DisplayItem(Shop shop, ItemStack iStack) { - this.shop = shop; - this.iStack = iStack.clone(); - // this.displayLoc = shop.getLocation().clone().add(0.5, 1.2, 0.5); - } - - /** - * Spawns the dummy item on top of the shop. - */ - public void spawn() { - if (shop.getLocation().getWorld() == null) - return; - Location dispLoc = this.getDisplayLocation(); - this.item = shop.getLocation().getWorld().dropItem(dispLoc, this.iStack); - this.item.setVelocity(new Vector(0, 0.1, 0)); - if (QuickShop.debug) { - System.out.println("Spawned item. Safeguarding."); - } - try { - NMS.safeGuard(this.item); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("QuickShop version mismatch! This version of QuickShop is incompatible with this version of bukkit! Try update?"); - } - } - - /** - * Spawns the new display item. Does not remove duplicate items. - */ - public void respawn() { - remove(); - spawn(); - } - - /** - * Removes all items floating ontop of the chest that aren't the display - * item. - */ - public boolean removeDupe() { - if (shop.getLocation().getWorld() == null) - return false; - // QuickShop qs = (QuickShop) - // Bukkit.getPluginManager().getPlugin("QuickShop"); - Location displayLoc = shop.getLocation().getBlock().getRelative(0, 1, 0).getLocation(); - boolean removed = false; - Chunk c = displayLoc.getChunk(); - for (Entity e : c.getEntities()) { - if (!(e instanceof Item)) - continue; - if (this.item != null && e.getEntityId() == this.item.getEntityId()) - continue; - Location eLoc = e.getLocation().getBlock().getLocation(); - if (eLoc.equals(displayLoc) || eLoc.equals(shop.getLocation())) { - ItemStack near = ((Item) e).getItemStack(); - // if its the same its a dupe - if (this.shop.matches(near)) { - e.remove(); - removed = true; - if (QuickShop.debug) { - System.out.println("Removed rogue item: " + near.getType()); - } - } - } - } - return removed; - } - - /** - * Removes the display item. - */ - public void remove() { - if (this.item == null) - return; - this.item.remove(); - this.item = null; - } - - /** - * @return Returns the exact location of the display item. (1 above shop - * block, in the center) - */ - public Location getDisplayLocation() { - return this.shop.getLocation().clone().add(0.5, 1.2, 0.5); - } - - /** - * Returns the reference to this shops item. Do not modify. - */ - public Item getItem() { - return this.item; - } +package org.maxgamer.quickshop.Shop; + +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Item; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; +import org.maxgamer.quickshop.QuickShop; +import org.maxgamer.quickshop.Util.NMS; + +/** + * @author Netherfoam A display item, that spawns a block above the chest and + * cannot be interacted with. + */ +public class DisplayItem { + private Shop shop; + private ItemStack iStack; + private Item item; + + // private Location displayLoc; + /** + * Creates a new display item. + * + * @param shop + * The shop (See Shop) + * @param iStack + * The item stack to clone properties of the display item from. + */ + public DisplayItem(Shop shop, ItemStack iStack) { + this.shop = shop; + this.iStack = iStack.clone(); + // this.displayLoc = shop.getLocation().clone().add(0.5, 1.2, 0.5); + } + + /** + * Spawns the dummy item on top of the shop. + */ + public void spawn() { + if (shop.getLocation().getWorld() == null) + return; + Location dispLoc = this.getDisplayLocation(); + this.item = shop.getLocation().getWorld().dropItem(dispLoc, this.iStack); + this.item.setVelocity(new Vector(0, 0.1, 0)); + if (QuickShop.debug) { + System.out.println("Spawned item. Safeguarding."); + } + try { + NMS.safeGuard(this.item); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("QuickShop version mismatch! This version of QuickShop is incompatible with this version of bukkit! Try update?"); + } + } + + /** + * Spawns the new display item. Does not remove duplicate items. + */ + public void respawn() { + remove(); + spawn(); + } + + /** + * Removes all items floating ontop of the chest that aren't the display + * item. + */ + public boolean removeDupe() { + if (shop.getLocation().getWorld() == null) + return false; + // QuickShop qs = (QuickShop) + // Bukkit.getPluginManager().getPlugin("QuickShop"); + Location displayLoc = shop.getLocation().getBlock().getRelative(0, 1, 0).getLocation(); + boolean removed = false; + Chunk c = displayLoc.getChunk(); + for (Entity e : c.getEntities()) { + if (!(e instanceof Item)) + continue; + if (this.item != null && e.getEntityId() == this.item.getEntityId()) + continue; + Location eLoc = e.getLocation().getBlock().getLocation(); + if (eLoc.equals(displayLoc) || eLoc.equals(shop.getLocation())) { + ItemStack near = ((Item) e).getItemStack(); + // if its the same its a dupe + if (this.shop.matches(near)) { + e.remove(); + removed = true; + if (QuickShop.debug) { + System.out.println("Removed rogue item: " + near.getType()); + } + } + } + } + return removed; + } + + /** + * Removes the display item. + */ + public void remove() { + if (this.item == null) + return; + this.item.remove(); + this.item = null; + } + + /** + * @return Returns the exact location of the display item. (1 above shop + * block, in the center) + */ + public Location getDisplayLocation() { + return this.shop.getLocation().clone().add(0.5, 1.2, 0.5); + } + + /** + * Returns the reference to this shops item. Do not modify. + */ + public Item getItem() { + return this.item; + } } \ No newline at end of file diff --git a/src/main/java/org/maxgamer/quickshop/Util/NMS.java b/src/main/java/org/maxgamer/quickshop/Util/NMS.java index cf9c15009..48ac56bc8 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/NMS.java +++ b/src/main/java/org/maxgamer/quickshop/Util/NMS.java @@ -1,10 +1,15 @@ package org.maxgamer.quickshop.Util; +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.DataInput; +import java.io.DataInputStream; import java.io.DataOutput; import java.io.DataOutputStream; import java.util.ArrayList; import java.util.List; +import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.bukkit.Bukkit; @@ -26,7 +31,7 @@ public class NMS { private static NMSDependent nms; static { - nmsDependencies.add(new NMSDependent("v1_13") { + nmsDependencies.add(new NMSDependent("v1_12") { @Override public void safeGuard(Item item) { if(QuickShop.debug)System.out.println("safeGuard"); @@ -57,6 +62,23 @@ public class NMS { } } + @Override + public org.bukkit.inventory.ItemStack getItemStack(byte[] bytes) { + try{ + if(QuickShop.debug)System.out.println("getItemStack"); + DataInputStream datainputstream = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(bytes)))); + net.minecraft.server.v1_13_R1.NBTTagCompound nbttagcompound; + try { + nbttagcompound = net.minecraft.server.v1_13_R1.NBTCompressedStreamTools.a((DataInput) datainputstream, null); + } finally { + datainputstream.close(); + } + net.minecraft.server.v1_13_R1.ItemStack is = new net.minecraft.server.v1_13_R1.ItemStack(nbttagcompound); + return org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack.asBukkitCopy(is); + }catch(Exception e){ + return new ItemStack(Material.AIR); + } + } @Override public GenericPotionData getPotionData(ItemStack potionItemStack) { @@ -203,4 +225,4 @@ public class NMS { throw new UnsupportedOperationException(); } } -} +} \ No newline at end of file