From 1c4ba540cf38e7b5ca133cfa042bd2c9dcd9308a Mon Sep 17 00:00:00 2001 From: Ed Ratner <38732412+Eejayinq@users.noreply.github.com> Date: Fri, 16 Apr 2021 13:34:45 +0100 Subject: [PATCH] Added error when trying to sell in /emf shop with no sellable items. --- .../oheers/fish/config/messages/Messages.java | 17 ++++ .../oheers/fish/selling/InteractHandler.java | 25 +++++- .../java/com/oheers/fish/selling/SellGUI.java | 89 +++++++++++++++---- main/resources/messages.yml | 9 +- 4 files changed, 116 insertions(+), 24 deletions(-) diff --git a/main/java/com/oheers/fish/config/messages/Messages.java b/main/java/com/oheers/fish/config/messages/Messages.java index 168d31c5..2f7737f1 100644 --- a/main/java/com/oheers/fish/config/messages/Messages.java +++ b/main/java/com/oheers/fish/config/messages/Messages.java @@ -128,7 +128,24 @@ public String getConfirmName() { return config.getString("confirm-gui-name"); } + public String getNoValueName() { + String s = config.getString("error-gui-name"); + if (s != null) return s; + else return "&c&lCan't Sell"; + } + public List sellLore() { return config.getStringList("sell-gui-lore"); } + + public List noValueLore() { + List l = config.getStringList("error-gui-lore"); + if (!l.isEmpty()) return l; + else { + l.add("&c&lValue: &c$0"); + l.add("&cAdd your caught fish to this."); + l.add("&cGUI to sell them."); + return l; + } + } } diff --git a/main/java/com/oheers/fish/selling/InteractHandler.java b/main/java/com/oheers/fish/selling/InteractHandler.java index 5bce06ad..fa31db98 100644 --- a/main/java/com/oheers/fish/selling/InteractHandler.java +++ b/main/java/com/oheers/fish/selling/InteractHandler.java @@ -37,8 +37,25 @@ public void interact(InventoryClickEvent event) { } // makes the player confirm their choice - gui.createConfirmIcon(); - gui.setConfirmIcon(); + gui.createIcon(); + gui.setIcon(); + gui.setFiller(); + + gui.setModified(false); + event.setCancelled(true); + + } else if (clickedItem.isSimilar(gui.getErrorIcon())) { + // cancels on right click + if (event.getAction().equals(InventoryAction.PICKUP_HALF)) { + event.setCancelled(true); + gui.close(false); + return; + } + + // makes the player confirm their choice + gui.createIcon(); + gui.setIcon(); + gui.setFiller(); gui.setModified(false); event.setCancelled(true); @@ -56,8 +73,8 @@ public void interact(InventoryClickEvent event) { if (gui.getModified()) { // the menu has been modified since we last gave the confirmation button, so it sends it again - gui.createConfirmIcon(); - gui.setConfirmIcon(); + gui.createIcon(); + gui.setIcon(); gui.setModified(false); } else { diff --git a/main/java/com/oheers/fish/selling/SellGUI.java b/main/java/com/oheers/fish/selling/SellGUI.java index 6d55d6cf..8a446c7a 100644 --- a/main/java/com/oheers/fish/selling/SellGUI.java +++ b/main/java/com/oheers/fish/selling/SellGUI.java @@ -26,15 +26,18 @@ public class SellGUI { public double value; + private boolean error; + public int fishCount; - private ItemStack sellIcon, filler, confirmIcon; + private ItemStack sellIcon, filler, errorFiller, confirmIcon, noValueIcon; public SellGUI(Player p) { this.player = p; this.modified = false; makeMenu(); - addFiller(); + setFiller(); + addFiller(filler); setSellItem(); this.player.openInventory(menu); } @@ -47,16 +50,23 @@ public Player getPlayer() { return player; } - public void addFiller() { + public void setFiller() { // the gray glass panes at the bottom ItemStack fill = new ItemStack(Material.GRAY_STAINED_GLASS_PANE); + ItemStack error = new ItemStack(Material.RED_STAINED_GLASS_PANE); ItemMeta fillMeta = fill.getItemMeta(); + ItemMeta errMeta = error.getItemMeta(); fillMeta.setDisplayName(ChatColor.RESET + ""); + errMeta.setDisplayName(ChatColor.RESET + ""); fill.setItemMeta(fillMeta); + error.setItemMeta(errMeta); // sets it as a default menu item that won't be dropped in a .close() request this.filler = WorthNBT.attributeDefault(fill); + this.errorFiller = WorthNBT.attributeDefault(error); + } + public void addFiller(ItemStack fill) { menu.setItem(27, fill); menu.setItem(28, fill); menu.setItem(29, fill); @@ -68,6 +78,19 @@ public void addFiller() { menu.setItem(35, fill); } + public void errorFiller() { + // the gray glass panes at the bottom + ItemStack fill = new ItemStack(Material.GRAY_STAINED_GLASS_PANE); + ItemMeta fillMeta = fill.getItemMeta(); + fillMeta.setDisplayName(ChatColor.RESET + ""); + fill.setItemMeta(fillMeta); + + // sets it as a default menu item that won't be dropped in a .close() request + this.errorFiller = WorthNBT.attributeDefault(fill); + + addFiller(this.filler); + } + public void setSellItem() { ItemStack sIcon = new ItemStack(Material.GOLD_INGOT); ItemMeta sellMeta = sIcon.getItemMeta(); @@ -94,26 +117,54 @@ public ItemStack getConfirmIcon() { return this.confirmIcon; } - public void createConfirmIcon() { - ItemStack confirm = new ItemStack(Material.GOLD_BLOCK); - ItemMeta cMeta = confirm.getItemMeta(); - cMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', EvenMoreFish.msgs.getConfirmName())); - // Generates the lore, looping through each line in messages.yml lore thingy, and generating it - List lore = new ArrayList<>(); - for (String line : EvenMoreFish.msgs.sellLore()) { - lore.add(new Message().setMSG(line).setSellPrice(getTotalWorth()).toString()); - } - cMeta.setLore(lore); + public ItemStack getErrorIcon() { + return this.noValueIcon; + } - confirm.setItemMeta(cMeta); - glowify(confirm); + public void createIcon() { + String totalWorth = getTotalWorth(); + if (totalWorth.equals("0.0")) { + ItemStack error = new ItemStack(Material.REDSTONE_BLOCK); + ItemMeta errorMeta = error.getItemMeta(); + errorMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', EvenMoreFish.msgs.getNoValueName())); + List lore = new ArrayList<>(); + for (String line : EvenMoreFish.msgs.noValueLore()) { + lore.add(ChatColor.translateAlternateColorCodes('&', line)); + } + errorMeta.setLore(lore); + error.setItemMeta(errorMeta); + glowify(error); + this.noValueIcon = WorthNBT.attributeDefault(error); + this.error = true; + } else { + ItemStack confirm = new ItemStack(Material.GOLD_BLOCK); + ItemMeta cMeta = confirm.getItemMeta(); + cMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', EvenMoreFish.msgs.getConfirmName())); + // Generates the lore, looping through each line in messages.yml lore thingy, and generating it + List lore = new ArrayList<>(); + for (String line : EvenMoreFish.msgs.sellLore()) { + lore.add(new Message().setMSG(line).setSellPrice(totalWorth).toString()); + } + cMeta.setLore(lore); - this.confirmIcon = WorthNBT.attributeDefault(confirm); + confirm.setItemMeta(cMeta); + glowify(confirm); + this.confirmIcon = WorthNBT.attributeDefault(confirm); + this.error = false; + } } - public void setConfirmIcon() { - this.menu.setItem(31, null); - this.menu.setItem(31, this.confirmIcon); + public void setIcon() { + if (this.error) { + this.menu.setItem(31, null); + this.menu.setItem(31, this.noValueIcon); + this.addFiller(errorFiller); + } else { + this.menu.setItem(31, null); + this.menu.setItem(31, this.confirmIcon); + this.addFiller(filler); + } + } public String getTotalWorth() { diff --git a/main/resources/messages.yml b/main/resources/messages.yml index 35b62d83..0a261e17 100644 --- a/main/resources/messages.yml +++ b/main/resources/messages.yml @@ -43,14 +43,21 @@ worth-gui-name: "&1&lSell Fish" sell-gui-name: "&6&lSELL" # The name found on the confirming item in /emf shop confirm-gui-name: "&6&lCONFIRM" +# The name found on the error item in /emf shop when the gui contains no items of value. +error-gui-name: "&c&lCan't Sell" # The lore found on the selling/confirmation item in /emf shop sell-gui-lore: - "&e&lValue: &e${sell-price}" - "&7LEFT CLICK to sell the fish." - "&7RIGHT CLICK to cancel." +# The lore below the error item in /emf shop when the gui contains no items of value. +error-gui-lore: + - "&c&lValue: &c$0" + - "&cAdd your caught fish to this." + - "&cGUI to sell them." # The message sent to players when they've sold their fish in the /emf shop fish-sale: "&fYou've sold &a{amount} &ffish for &a${sell-price}&f." # ATTENTION ATTENTION ATTENTION # DO NOT EDIT THIS VALUE. -config-version: 2 \ No newline at end of file +config-version: 3 \ No newline at end of file