From 49e1ae849f116d755ccbddb38c2200656fd9adbf Mon Sep 17 00:00:00 2001 From: TinyTank800 Date: Tue, 28 Mar 2023 20:51:18 -0700 Subject: [PATCH] Addition of sell-all= tag Format: sell-all= [enchanted:KNOCKBACK:1] [potion:JUMP] --- .../tags/economy/SellItemTags.java | 95 ++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/src/me/rockyhawk/commandpanels/commandtags/tags/economy/SellItemTags.java b/src/me/rockyhawk/commandpanels/commandtags/tags/economy/SellItemTags.java index 10a9827..5b0dee1 100644 --- a/src/me/rockyhawk/commandpanels/commandtags/tags/economy/SellItemTags.java +++ b/src/me/rockyhawk/commandpanels/commandtags/tags/economy/SellItemTags.java @@ -37,7 +37,28 @@ public class SellItemTags implements Listener { plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.item.failure")).replaceAll("%cp-args%", e.args[1])); } else { plugin.econ.depositPlayer(e.p, Double.parseDouble(e.args[0])); - plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.item.success")).replaceAll("%cp-args%", e.args[1])); + plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.item.success")).replaceAll("%cp-args%", e.args[1]).replaceAll("%cp-args2%", "$" + e.args[0])); + } + } else { + plugin.tex.sendMessage(e.p, ChatColor.RED + "Selling Requires Vault and an Economy to work!"); + } + } catch (Exception sell) { + plugin.debug(sell,e.p); + plugin.tex.sendMessage(e.p, plugin.config.getString("config.format.error") + " " + "commands: " + e.name); + } + return; + } + if(e.name.equalsIgnoreCase("sell-all=")){ + e.commandTagUsed(); + //if player uses sell-all= it will be eg. sell-all= [enchanted:KNOCKBACK:1] [potion:JUMP] + try { + if (plugin.econ != null) { + int sold = removeAllItem(e.p, e.args); + if (sold <= 0) { + plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.item.failure")).replaceAll("%cp-args%", e.args[1])); + } else { + plugin.econ.depositPlayer(e.p, Double.parseDouble(e.args[0]) * sold); + plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.item.success")).replaceAll("%cp-args%", e.args[1]).replaceAll("%cp-args2%", "$" + String.valueOf(Double.parseDouble(e.args[0]) * sold))); } } else { plugin.tex.sendMessage(e.p, ChatColor.RED + "Selling Requires Vault and an Economy to work!"); @@ -157,4 +178,76 @@ public class SellItemTags implements Listener { } return false; } + + //returns 0 if player does not have items. Returns number of sold items. + private int removeAllItem(Player p, String[] args){ + List cont = new ArrayList<>(Arrays.asList(plugin.inventorySaver.getNormalInventory(p))); + List remCont = new ArrayList<>(); + + ItemStack sellItem = new ItemStack(Objects.requireNonNull(Material.matchMaterial(args[1])), 0); + //int RemainingAmount = sellItem.getAmount(); + for (int f = 0; f < 36; f++) { + ItemStack itm = cont.get(f); + ItemStack remItm; + if (itm != null && itm.getType().equals(sellItem.getType())) { + remItm = new ItemStack(itm.getType(), itm.getAmount(), (short)f); + //determine if the command contains parameters for extensions + String potion = "false"; + for(String argsTemp : args){ + if(argsTemp.startsWith("potion:")){ + potion = argsTemp.replace("potion:",""); + } + } + //legacy ID + byte id = -1; + if(plugin.legacy.LOCAL_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_15)) { + for (String argsTemp : args) { + if (argsTemp.startsWith("id:")) { + id = Byte.parseByte(argsTemp.replace("id:", "")); + break; + } + } + } + //check to ensure any extensions are checked + try { + if (!potion.equals("false")) { + PotionMeta potionMeta = (PotionMeta) itm.getItemMeta(); + assert potionMeta != null; + if (!potionMeta.getBasePotionData().getType().name().equalsIgnoreCase(potion)) { + p.sendMessage(plugin.tex.colour( plugin.tag + ChatColor.RED + "Your item has the wrong potion effect")); + return 0; + } + } + if (id != -1) { + if (itm.getDurability() != id) { + continue; + } + } + }catch(Exception exc){ + //skip if it cannot do unless plugin.debug is enabled + plugin.debug(exc,p); + } + + remCont.add(remItm); + sellItem.setAmount(sellItem.getAmount() + remItm.getAmount()); + } + } + + if(sellItem.getAmount() >= 1){ + for (int f = 0; f <= remCont.size() - 1; f++) { + ItemStack remItm = remCont.get(f); + + if(plugin.inventorySaver.hasNormalInventory(p)){ + p.getInventory().getItem((int)remItm.getDurability()).setAmount(0); + p.updateInventory(); + }else{ + cont.get((int)remItm.getDurability()).setAmount(0); + plugin.inventorySaver.inventoryConfig.set(p.getUniqueId().toString(), plugin.itemSerializer.itemStackArrayToBase64(cont.toArray(new ItemStack[0]))); + } + } + + return sellItem.getAmount(); + } + return 0; + } }