diff --git a/src/main/java/org/maxgamer/quickshop/Command/QS.java b/src/main/java/org/maxgamer/quickshop/Command/QS.java index a7c3b62b8..5c7ae0ec0 100644 --- a/src/main/java/org/maxgamer/quickshop/Command/QS.java +++ b/src/main/java/org/maxgamer/quickshop/Command/QS.java @@ -1,9 +1,12 @@ package org.maxgamer.quickshop.Command; import java.io.File; +import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.UUID; import java.util.logging.Level; import org.bukkit.Bukkit; @@ -40,11 +43,13 @@ import org.maxgamer.quickshop.Util.Util; public class QS implements CommandExecutor { QuickShop plugin; - + public static HashMap> signPlayerCache = new HashMap<>(); public QS(QuickShop plugin) { this.plugin = plugin; } - + public void signGUIApi(ArrayList data, String arg) { + + } private void setUnlimited(CommandSender sender) { if (sender instanceof Player && sender.hasPermission("quickshop.unlimited")) { BlockIterator bIt = new BlockIterator((Player) sender, 10); @@ -66,7 +71,34 @@ public class QS implements CommandExecutor { return; } } - + private void sign(CommandSender sender, String[] args) { + if(!(sender instanceof Player)) + return; + if(args.length<10) //sign world x y z type line1 line2 line3 line4 + return; + ShopManager manager = new ShopManager(plugin); + Shop shop = manager.getShop(new Location(Bukkit.getWorld(args[1]), Integer.valueOf(args[2]), Integer.valueOf(args[3]), Integer.valueOf(args[4]))); + if(shop==null) + return; + String[] texts = new String[3]; + String type = args[5]; + for (int i = 0; i < args.length; i++) { + String string = args[i]; + if(i<6) + return; + texts[i-6]=string; + } + try { + ArrayList data = new ArrayList<>(); + data.add(shop); + data.add(type); + signPlayerCache.put(((Player)sender).getUniqueId(),data); + Util.sendSignEditForGUI((Player)sender, texts); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } private void remove(CommandSender sender, String[] args) { if (sender instanceof Player == false) { sender.sendMessage(ChatColor.RED + "Only players may use that command."); @@ -558,6 +590,9 @@ public class QS implements CommandExecutor { } else if (subArg.startsWith("amount")) { amount(sender, args); return true; + } else if (subArg.startsWith("sign")) { + sign(sender, args); + return true; } else if (subArg.startsWith("buy")) { setBuy(sender); return true; diff --git a/src/main/java/org/maxgamer/quickshop/Listeners/PlayerListener.java b/src/main/java/org/maxgamer/quickshop/Listeners/PlayerListener.java index 2494b17ef..d880f1188 100644 --- a/src/main/java/org/maxgamer/quickshop/Listeners/PlayerListener.java +++ b/src/main/java/org/maxgamer/quickshop/Listeners/PlayerListener.java @@ -141,16 +141,18 @@ public class PlayerListener implements Listener { p.sendMessage( MsgUtil.getMessage("how-much-to-trade-for", MsgUtil.getItemi18n(Util.getName(info.getItem())))); } - } else if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)&&e.getClickedBlock().getType()==Material.WALL_SIGN) { + } else if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) + && e.getClickedBlock().getType() == Material.WALL_SIGN) { /** @TODO: ControlPanel **/ Block block; - if(e.getClickedBlock().getType()==Material.WALL_SIGN) { + if (e.getClickedBlock().getType() == Material.WALL_SIGN) { block = Util.getAttached(e.getClickedBlock()); - }else { + } else { block = e.getClickedBlock(); } if (plugin.getShopManager().getShop(block.getLocation()) != null) { - MsgUtil.sendControlPanelInfo((CommandSender)e.getPlayer(), plugin.getShopManager().getShop(block.getLocation())); + MsgUtil.sendControlPanelInfo((CommandSender) e.getPlayer(), + plugin.getShopManager().getShop(block.getLocation())); } } diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index 67acad23b..cd64a2e55 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -1,8 +1,11 @@ package org.maxgamer.quickshop; +import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLib; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketEvent; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.*; import org.bukkit.configuration.ConfigurationSection; @@ -30,6 +33,7 @@ import org.maxgamer.quickshop.Util.Util; import org.maxgamer.quickshop.Watcher.ItemWatcher; import org.maxgamer.quickshop.Watcher.LogWatcher; import org.maxgamer.quickshop.Watcher.UpdateWatcher; +import org.maxgamer.quickshop.wrapper.WrapperPlayClientUpdateSign; import java.io.File; import java.io.FileInputStream; @@ -93,7 +97,7 @@ public class QuickShop extends JavaPlugin { /** Use SpoutPlugin to get item / block names */ public boolean useSpout = false; // private Metrics metrics; - + QS commandExecutor =null; MultiverseCore mPlugin = null; private int displayItemCheckTicks; private boolean noopDisable; @@ -165,6 +169,15 @@ public class QuickShop extends JavaPlugin { } protocolLibPlugin = (ProtocolLib) Bukkit.getPluginManager().getPlugin("ProtocolLib"); if (protocolLibPlugin != null) { + getProtocolLib().addPacketListener(new PacketAdapter(this, PacketType.Play.Client.UPDATE_SIGN) { + @Override + public void onPacketReceiving(PacketEvent event) { + WrapperPlayClientUpdateSign wrapper = new WrapperPlayClientUpdateSign(event.getPacket()); + //BlockPosition blockPos = wrapper.getLocation(); + ArrayList data = QS.signPlayerCache.get(event.getPlayer().getUniqueId()); + QuickShop.instance.commandExecutor.signGUIApi(data,wrapper.getLines()[0]); + } + }); getLogger().info("Successfully loaded ProtocolLib support!"); } if(getConfig().getInt("config-version")==0) @@ -542,7 +555,7 @@ public class QuickShop extends JavaPlugin { } Bukkit.getServer().getPluginManager().registerEvents(worldListener, this); // Command handlers - QS commandExecutor = new QS(this); + commandExecutor = new QS(this); getCommand("qs").setExecutor(commandExecutor); if (getConfig().getInt("shop.find-distance") > 100) { getLogger().severe("Shop.find-distance is too high! It may cause lag! Pick a number under 100!"); diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java index 361519165..c36e28586 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java @@ -36,6 +36,7 @@ public class ShopManager { private HashMap actions = new HashMap(); private HashMap>> shops = new HashMap>>(); final private static ItemStack AIR = new ItemStack(Material.AIR); + public ShopManager(QuickShop plugin) { this.plugin = plugin; diff --git a/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java b/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java index 94be37be7..5897518c9 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java +++ b/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java @@ -126,6 +126,92 @@ public class MsgUtil { loadCfgMessages(true); } + public static void sendNormalControlPanelInfo(CommandSender sender, Shop shop) { + if (!sender.hasPermission("quickshop.use")) { + return; + } + + if (plugin.getConfig().getBoolean("sneak-to-control")) + if (sender instanceof Player) + if (!((Player) sender).isSneaking()) + return; + sender.sendMessage(""); + sender.sendMessage(""); + sender.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+"); + sender.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("controlpanel.infomation")); + // Owner + if (!sender.hasPermission("quickshop.setowner")) { + sender.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.owner", shop.ownerName())); + } else { + String Text = MsgUtil.getMessage("controlpanel.setowner",shop.ownerName()); + String hoverText = MsgUtil.getMessage("controlpanel.setowner-hover"); + String clickCommand = "/qs setowner [Player]"; + TextComponent message = new TextComponent(ChatColor.DARK_PURPLE + "| " + Text); + message.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, clickCommand)); + message.setHoverEvent( + new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(hoverText).create())); + sender.spigot().sendMessage(message); + } + // Unlimited + if (sender.hasPermission("quickshop.unlimited")) { + String Text = MsgUtil.getMessage("controlpanel.unlimited", bool2String(shop.isUnlimited())); + String hoverText = MsgUtil.getMessage("controlpanel.unlimited-hover"); + String clickCommand = "/qs unlimited"; + MsgUtil.sendPanelMessage(sender, Text, hoverText, clickCommand); + } + // Buying/Selling Mode + if (sender.hasPermission("quickshop.create.buy") && sender.hasPermission("quickshop.create.sell")) { + if (shop.isSelling()) { + String Text = MsgUtil.getMessage("controlpanel.mode-selling"); + String hoverText = MsgUtil.getMessage("controlpanel.mode-selling-hover"); + String clickCommand = "/qs buy"; + MsgUtil.sendPanelMessage(sender, Text, hoverText, clickCommand); + } else if (shop.isBuying()) { + String Text = MsgUtil.getMessage("controlpanel.mode-buying"); + String hoverText = MsgUtil.getMessage("controlpanel.mode-buying-hover"); + String clickCommand = "/qs sell"; + MsgUtil.sendPanelMessage(sender, Text, hoverText, clickCommand); + } + } + // Set Price + if (sender.hasPermission("quickshop.other.price")) { + String Text = MsgUtil.getMessage("controlpanel.price", String.valueOf(shop.getPrice())); + String hoverText = MsgUtil.getMessage("controlpanel.mode-buying-hover"); + String clickCommand = "/qs price [New Price]"; + TextComponent message = new TextComponent(ChatColor.DARK_PURPLE + "| " + Text); + message.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, clickCommand)); + message.setHoverEvent( + new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(hoverText).create())); + sender.spigot().sendMessage(message); + } + // Refill + if (sender.hasPermission("quickshop.refill")) { + String Text = MsgUtil.getMessage("controlpanel.refill", String.valueOf(shop.getPrice())); + String hoverText = MsgUtil.getMessage("controlpanel.refill-hover"); + String clickCommand = "/qs refill [Amount]"; + TextComponent message = new TextComponent(ChatColor.DARK_PURPLE + "| " + Text); + message.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, clickCommand)); + message.setHoverEvent( + new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(hoverText).create())); + sender.spigot().sendMessage(message); + } + // Refill + if (sender.hasPermission("quickshop.empty")) { + String Text = MsgUtil.getMessage("controlpanel.empty", String.valueOf(shop.getPrice())); + String hoverText = MsgUtil.getMessage("controlpanel.empty-hover"); + String clickCommand = "/qs empty"; + MsgUtil.sendPanelMessage(sender, Text, hoverText, clickCommand); + } + // Remove + if (sender.hasPermission("quickshop.other.destroy") || shop.getOwner().equals(((Player)sender).getUniqueId())) { + String Text = MsgUtil.getMessage("controlpanel.remove", String.valueOf(shop.getPrice())); + String hoverText = MsgUtil.getMessage("controlpanel.remove-hover"); + String clickCommand = "/qs remove "+shop.getLocation().getBlockX()+" "+shop.getLocation().getBlockY()+" "+shop.getLocation().getBlockZ(); + MsgUtil.sendPanelMessage(sender, Text, hoverText, clickCommand); + } + + sender.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+"); + } public static void sendControlPanelInfo(CommandSender sender, Shop shop) { if (!sender.hasPermission("quickshop.use")) { return; diff --git a/src/main/java/org/maxgamer/quickshop/Util/Util.java b/src/main/java/org/maxgamer/quickshop/Util/Util.java index e80091c41..8fe33e3b5 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/Util.java +++ b/src/main/java/org/maxgamer/quickshop/Util/Util.java @@ -26,7 +26,14 @@ import org.bukkit.potion.PotionEffect; import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.Shop.DisplayItem; import org.maxgamer.quickshop.Shop.Shop; +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.wrappers.BlockPosition; +import com.comphenix.protocol.wrappers.WrappedBlockData; +import com.comphenix.protocol.wrappers.nbt.NbtCompound; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.DecimalFormat; import java.util.AbstractMap.SimpleEntry; @@ -178,6 +185,33 @@ public class Util { } return null; } + + public static void sendSignEditForGUI(Player player, String[] texts) throws InvocationTargetException { + ProtocolManager manager = plugin.getProtocolLib(); + BlockPosition position = new BlockPosition(player.getLocation().getBlockX(), 255, + player.getLocation().getBlockZ()); + + PacketContainer blockPacket = manager.createPacket(PacketType.Play.Server.BLOCK_CHANGE); + blockPacket.getBlockPositionModifier().write(0, position); + blockPacket.getBlockData().write(0, WrappedBlockData.createData(Material.WALL_SIGN)); + + PacketContainer dataPacket = manager.createPacket(PacketType.Play.Server.TILE_ENTITY_DATA); + dataPacket.getBlockPositionModifier().write(0, position); + dataPacket.getIntegers().write(0, 9); + + NbtCompound compound = (NbtCompound) dataPacket.getNbtModifier().read(0); + for (int index = 0; index < 4; index++) { + compound.put("Text" + (index + 1), "{\"extra\":[{\"text\":\"" + texts[index] + "\"}],\"text\":\"\"}"); + } + dataPacket.getNbtModifier().write(0, compound); + + PacketContainer editorPacket = manager.createPacket(PacketType.Play.Server.OPEN_SIGN_EDITOR); + editorPacket.getBlockPositionModifier().write(0, position); + + manager.sendServerPacket(player, blockPacket); + manager.sendServerPacket(player, dataPacket); + manager.sendServerPacket(player, editorPacket); + } /** * Checks whether someone else's shop is within reach of a hopper being placed by a player. diff --git a/src/main/java/org/maxgamer/quickshop/wrapper/AbstractPacket.java b/src/main/java/org/maxgamer/quickshop/wrapper/AbstractPacket.java new file mode 100644 index 000000000..3aafa9313 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/wrapper/AbstractPacket.java @@ -0,0 +1,96 @@ +package org.maxgamer.quickshop.wrapper; + +import java.lang.reflect.InvocationTargetException; + +import org.bukkit.entity.Player; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketContainer; +import com.google.common.base.Objects; + +public abstract 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) { + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(receiver, + getHandle()); + } catch (InvocationTargetException e) { + throw new RuntimeException("Cannot send packet.", e); + } + } + + /** + * 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().recieveClientPacket(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().recieveClientPacket(sender, + getHandle()); + } catch (Exception e) { + throw new RuntimeException("Cannot receive packet.", e); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/maxgamer/quickshop/wrapper/WrapperPlayClientUpdateSign.java b/src/main/java/org/maxgamer/quickshop/wrapper/WrapperPlayClientUpdateSign.java new file mode 100644 index 000000000..f74604c89 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/wrapper/WrapperPlayClientUpdateSign.java @@ -0,0 +1,61 @@ +package org.maxgamer.quickshop.wrapper; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.wrappers.BlockPosition; + +public class WrapperPlayClientUpdateSign extends AbstractPacket { + public static final PacketType TYPE = PacketType.Play.Client.UPDATE_SIGN; + + public WrapperPlayClientUpdateSign() { + super(new PacketContainer(TYPE), TYPE); + handle.getModifier().writeDefaults(); + } + + public WrapperPlayClientUpdateSign(PacketContainer packet) { + super(packet, TYPE); + } + + /** + * Retrieve Location. + *

+ * Notes: block Coordinates + * + * @return The current Location + */ + public BlockPosition getLocation() { + return handle.getBlockPositionModifier().read(0); + } + + /** + * Set Location. + * + * @param value - new value. + */ + public void setLocation(BlockPosition value) { + handle.getBlockPositionModifier().write(0, value); + } + + /** + * Retrieve this sign's lines of text. + * + * @return The current lines + */ + public String[] getLines() { + return handle.getStringArrays().read(0); + } + + /** + * Set this sign's lines of text. + * + * @param value - Lines, must be 4 elements long + */ + public void setLines(String[] value) { + if (value == null) + throw new IllegalArgumentException("value cannot be null!"); + if (value.length != 4) + throw new IllegalArgumentException("value must have 4 elements!"); + + handle.getStringArrays().write(0, value); + } +} \ No newline at end of file