From 4dd5382108d14ff96f0d338ac608247eb35d9104 Mon Sep 17 00:00:00 2001 From: Superspeed500 Date: Sun, 6 Sep 2026 13:12:59 +0200 Subject: [PATCH 1/6] Started work on buy command --- .../essentials/commands/Commandbuy.java | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java new file mode 100644 index 00000000000..90a4bc3541c --- /dev/null +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java @@ -0,0 +1,149 @@ +package com.earth2me.essentials.commands; + +import com.earth2me.essentials.Trade; +import com.earth2me.essentials.User; +import com.earth2me.essentials.craftbukkit.Inventories; +import com.earth2me.essentials.adventure.AdventureUtil; +import com.earth2me.essentials.utils.NumberUtil; +import com.google.common.collect.Lists; +import net.ess3.api.TranslatableException; +import net.ess3.api.events.UserBalanceUpdateEvent; +import org.bukkit.ChatColor; +import org.bukkit.Server; +import org.bukkit.inventory.ItemStack; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.logging.Level; + +import static com.earth2me.essentials.I18n.tlLiteral; + +// The file commandsell.java has been used as a "template" +// Most of the code in this file is therefor a one-to-one copy from that file +public class Commandbuy extends EssentialsCommand { + public Commandbuy() { + super("buy"); + } + + // Starting point for when the command is run + @Override + public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { + BigDecimal totalWorth = BigDecimal.ZERO; + // Throw an error if the user has not specified enough arguments. + if (args.length < 1) { + throw new NotEnoughArgumentsException(); + } + + // Let's check if the user is authorized to buy more of the stuff in hand + // Consider removing the first if. + if (args[0].equalsIgnoreCase("hand") && !user.isAuthorized("essentials.buy.hand")) { + throw new TranslatableException("buyHandPermission"); + } else if ((args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all")) && !user.isAuthorized("essentials.buy.bulk")) { + throw new TranslatableException("buyBulkPermission"); + } + + final List is = ess.getItemDb().getMatching(user, args); + int count = 0; + + final boolean isBulk = is.size() > 1; + + final List notBought = new ArrayList<>(); + for (ItemStack stack : is) { + if (!ess.getSettings().isAllowBuyNamedItems()) { + if (stack.getItemMeta() != null && stack.getItemMeta().hasDisplayName()) { + if (isBulk) { + notBought.add(stack); + continue; + } + throw new TranslatableException("cannotBuyNamedItem"); + } + } + try { + if (stack.getAmount() > 0) { + totalWorth = totalWorth.add(buyItem(user, stack, args, isBulk)); + stack = stack.clone(); + count++; + for (final ItemStack zeroStack : is) { + if (zeroStack.isSimilar(stack)) { + zeroStack.setAmount(0); + } + } + } + } catch (final Exception e) { + if (!isBulk) { + throw e; + } + } + } + if (!notBought.isEmpty()) { + final List names = new ArrayList<>(); + for (final ItemStack stack : notBought { + if (stack.getItemMeta() != null) { //This was already validated but IDE still freaks out + names.add(stack.getItemMeta().getDisplayName()); + } + } + ess.showError(user.getSource(), new TranslatableException("cannotBuyTheseNamedItems", String.join(ChatColor.RESET + ", ", names)), commandLabel); + } + if (count != 1) { + final AdventureUtil.ParsedPlaceholder totalWorthStr = AdventureUtil.parsed(NumberUtil.displayCurrency(totalWorth, ess)); + if (args[0].equalsIgnoreCase("blocks")) { + user.sendTl("totalWorthBlocks", totalWorthStr, totalWorthStr); + } else { + user.sendTl("totalWorthAll", totalWorthStr, totalWorthStr); + } + } + } + + private BigDecimal buyItem(final User user, final ItemStack is, final String[] args, final boolean isBulkBuy) throws Exception { + final int amount = ess.getWorth().getAmount(ess, user, is, args, isBulkBuy); + final BigDecimal originalWorth = ess.getWorth().getPrice(ess, is); + final BigDecimal worth = originalWorth == null ? null : originalWorth.multiply(ess.getSettings().getMultiplier(user)); + + if (worth == null) { + throw new TranslatableException("itemCannotBeBought"); + } + + if (amount <= 0) { + if (!isBulkSell) { + user.sendTl("itemBought", AdventureUtil.parsed(NumberUtil.displayCurrency(BigDecimal.ZERO, ess)), BigDecimal.ZERO, is.getType().toString().toLowerCase(Locale.ENGLISH), NumberUtil.displayCurrency(worth, ess)); + } + return BigDecimal.ZERO; + } + + final BigDecimal result = worth.multiply(BigDecimal.valueOf(amount)); + + //TODO: Prices for Enchantments + final ItemStack ris = is.clone(); + ris.setAmount(amount); + // This statement is for checking if more items that is in inventory is trying to be sold. Should never happen. + // Needs to be rewritten into a check about if more gold is consumed. + if (!Inventories.containsAtLeast(user.getBase(), ris, amount)) { + // This should never happen. + throw new IllegalStateException("Trying to remove more items than are available."); + } + Inventories.removeItemAmount(user.getBase(), ris, ris.getAmount()); + user.getBase().updateInventory(); + Trade.log("Command", "Buy", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(result, ess), user.getLocation(), user.getMoney(), ess); + // Needs to be changed to a method for taking money + user.takeMoney(result, null, UserBalanceUpdateEvent.Cause.COMMAND_BUY); + final String typeName = is.getType().toString().toLowerCase(Locale.ENGLISH); + final AdventureUtil.ParsedPlaceholder worthDisplay = AdventureUtil.parsed(NumberUtil.displayCurrency(worth, ess)); + user.sendTl("itemBought", AdventureUtil.parsed(NumberUtil.displayCurrency(result, ess)), amount, typeName, worthDisplay); + ess.getLogger().log(Level.INFO, ess.getAdventureFacet().miniToLegacy(tlLiteral("itemBoughtConsole", user.getName(), typeName, ess.getAdventureFacet().miniToLegacy(NumberUtil.displayCurrency(result, ess)), amount, ess.getAdventureFacet().miniToLegacy(worthDisplay.toString()), user.getDisplayName()))); + return result; + } + + @Override + protected List getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) { + if (args.length == 1) { + return getMatchingItems(args[0]); + } else if (args.length == 2) { + return Lists.newArrayList("1", "64"); + } else { + return Collections.emptyList(); + } + } +} From 542073a1b01aa40ae261c86ab22fa2a239b0bb1d Mon Sep 17 00:00:00 2001 From: Superspeed500 Date: Sun, 6 Sep 2026 13:40:29 +0200 Subject: [PATCH 2/6] Further work on buy command --- .../src/main/java/com/earth2me/essentials/ISettings.java | 2 ++ .../src/main/java/com/earth2me/essentials/Settings.java | 5 +++++ .../java/net/ess3/api/events/UserBalanceUpdateEvent.java | 1 + Essentials/src/main/resources/config.yml | 1 + Essentials/src/main/resources/plugin.yml | 6 ++++++ 5 files changed, 15 insertions(+) diff --git a/Essentials/src/main/java/com/earth2me/essentials/ISettings.java b/Essentials/src/main/java/com/earth2me/essentials/ISettings.java index 7542e2b39f8..c74a680e195 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/ISettings.java +++ b/Essentials/src/main/java/com/earth2me/essentials/ISettings.java @@ -387,6 +387,8 @@ public interface ISettings extends IConf { boolean isAllowSellNamedItems(); + boolean isAllowBuyNamedItems(); + boolean isAddingPrefixInPlayerlist(); boolean isAddingSuffixInPlayerlist(); diff --git a/Essentials/src/main/java/com/earth2me/essentials/Settings.java b/Essentials/src/main/java/com/earth2me/essentials/Settings.java index 69c8a0b3e17..88532c55d07 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Settings.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Settings.java @@ -1951,6 +1951,11 @@ public boolean isAllowSellNamedItems() { return config.getBoolean("allow-selling-named-items", false); } + @Override + public boolean isAllowBuyNamedItems() { + return config.getBoolean("allow-buying-named-items", false); + } + @Override public boolean isAddingPrefixInPlayerlist() { return config.getBoolean("add-prefix-in-playerlist", false); diff --git a/Essentials/src/main/java/net/ess3/api/events/UserBalanceUpdateEvent.java b/Essentials/src/main/java/net/ess3/api/events/UserBalanceUpdateEvent.java index fbba422cb88..55b4bdea5f9 100644 --- a/Essentials/src/main/java/net/ess3/api/events/UserBalanceUpdateEvent.java +++ b/Essentials/src/main/java/net/ess3/api/events/UserBalanceUpdateEvent.java @@ -73,6 +73,7 @@ public enum Cause { COMMAND_ECO, COMMAND_PAY, COMMAND_SELL, + COMMAND_BUY, API, SPECIAL, // Reserved for API usage UNKNOWN diff --git a/Essentials/src/main/resources/config.yml b/Essentials/src/main/resources/config.yml index 5b176ffb807..0a053ea350b 100644 --- a/Essentials/src/main/resources/config.yml +++ b/Essentials/src/main/resources/config.yml @@ -279,6 +279,7 @@ player-commands: - balance.others - balancetop - build + - buy - chat.color - chat.format - chat.shout diff --git a/Essentials/src/main/resources/plugin.yml b/Essentials/src/main/resources/plugin.yml index 68496ebbbdf..dc03dfe4574 100644 --- a/Essentials/src/main/resources/plugin.yml +++ b/Essentials/src/main/resources/plugin.yml @@ -76,6 +76,10 @@ commands: description: Set a player on fire. usage: / aliases: [eburn] + buy: + description: Buys item + usage: / <||hand|inventory|blocks> [amount] + aliases: [ebuy] cartographytable: description: Opens up a cartography table. usage: / @@ -703,6 +707,8 @@ permissions: description: Allows access to the /broadcastworld command essentials.burn: description: Allows access to the /burn command + essentials.buy: + description: Allows access to the /buy command essentials.chat.ignoreexempt: default: false description: Someone with this permission will not be ignored, even if they are on another persons ignore list From c7be53b8b9642a992004d8a1f99393bb190c4a60 Mon Sep 17 00:00:00 2001 From: Superspeed500 Date: Sun, 6 Sep 2026 14:55:16 +0200 Subject: [PATCH 3/6] Further experimentation with code to understand code functionality. --- .../essentials/commands/Commandbuy.java | 70 +++---------------- .../src/main/resources/messages_en.properties | 2 + Essentials/src/main/resources/plugin.yml | 2 +- 3 files changed, 12 insertions(+), 62 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java index 90a4bc3541c..ceed709a818 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java @@ -32,61 +32,17 @@ public Commandbuy() { @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { BigDecimal totalWorth = BigDecimal.ZERO; + // Throw an error if the user has not specified enough arguments. if (args.length < 1) { throw new NotEnoughArgumentsException(); } - // Let's check if the user is authorized to buy more of the stuff in hand - // Consider removing the first if. - if (args[0].equalsIgnoreCase("hand") && !user.isAuthorized("essentials.buy.hand")) { - throw new TranslatableException("buyHandPermission"); - } else if ((args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all")) && !user.isAuthorized("essentials.buy.bulk")) { - throw new TranslatableException("buyBulkPermission"); - } - - final List is = ess.getItemDb().getMatching(user, args); + final ItemStack is = ess.getItemDb().get("gold_ingot", 1); int count = 0; - final boolean isBulk = is.size() > 1; - - final List notBought = new ArrayList<>(); - for (ItemStack stack : is) { - if (!ess.getSettings().isAllowBuyNamedItems()) { - if (stack.getItemMeta() != null && stack.getItemMeta().hasDisplayName()) { - if (isBulk) { - notBought.add(stack); - continue; - } - throw new TranslatableException("cannotBuyNamedItem"); - } - } - try { - if (stack.getAmount() > 0) { - totalWorth = totalWorth.add(buyItem(user, stack, args, isBulk)); - stack = stack.clone(); - count++; - for (final ItemStack zeroStack : is) { - if (zeroStack.isSimilar(stack)) { - zeroStack.setAmount(0); - } - } - } - } catch (final Exception e) { - if (!isBulk) { - throw e; - } - } - } - if (!notBought.isEmpty()) { - final List names = new ArrayList<>(); - for (final ItemStack stack : notBought { - if (stack.getItemMeta() != null) { //This was already validated but IDE still freaks out - names.add(stack.getItemMeta().getDisplayName()); - } - } - ess.showError(user.getSource(), new TranslatableException("cannotBuyTheseNamedItems", String.join(ChatColor.RESET + ", ", names)), commandLabel); - } + totalWorth = totalWorth.add(buyItem(user, is, args)); + if (count != 1) { final AdventureUtil.ParsedPlaceholder totalWorthStr = AdventureUtil.parsed(NumberUtil.displayCurrency(totalWorth, ess)); if (args[0].equalsIgnoreCase("blocks")) { @@ -97,8 +53,8 @@ public void run(final Server server, final User user, final String commandLabel, } } - private BigDecimal buyItem(final User user, final ItemStack is, final String[] args, final boolean isBulkBuy) throws Exception { - final int amount = ess.getWorth().getAmount(ess, user, is, args, isBulkBuy); + private BigDecimal buyItem(final User user, final ItemStack is, final String[] args) throws Exception { + final int amount = 1; // Replace this with actual amount final BigDecimal originalWorth = ess.getWorth().getPrice(ess, is); final BigDecimal worth = originalWorth == null ? null : originalWorth.multiply(ess.getSettings().getMultiplier(user)); @@ -107,24 +63,16 @@ private BigDecimal buyItem(final User user, final ItemStack is, final String[] a } if (amount <= 0) { - if (!isBulkSell) { - user.sendTl("itemBought", AdventureUtil.parsed(NumberUtil.displayCurrency(BigDecimal.ZERO, ess)), BigDecimal.ZERO, is.getType().toString().toLowerCase(Locale.ENGLISH), NumberUtil.displayCurrency(worth, ess)); - } return BigDecimal.ZERO; } final BigDecimal result = worth.multiply(BigDecimal.valueOf(amount)); - //TODO: Prices for Enchantments final ItemStack ris = is.clone(); ris.setAmount(amount); - // This statement is for checking if more items that is in inventory is trying to be sold. Should never happen. - // Needs to be rewritten into a check about if more gold is consumed. - if (!Inventories.containsAtLeast(user.getBase(), ris, amount)) { - // This should never happen. - throw new IllegalStateException("Trying to remove more items than are available."); - } - Inventories.removeItemAmount(user.getBase(), ris, ris.getAmount()); + + Inventories.addItem(user.getBase(), user.isAuthorized("essentials.oversizedstacks") ? ess.getSettings().getOversizedStackSize() : 0, ris); + user.getBase().updateInventory(); Trade.log("Command", "Buy", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(result, ess), user.getLocation(), user.getMoney(), ess); // Needs to be changed to a method for taking money diff --git a/Essentials/src/main/resources/messages_en.properties b/Essentials/src/main/resources/messages_en.properties index 00434f17fa7..6509b8a74b9 100644 --- a/Essentials/src/main/resources/messages_en.properties +++ b/Essentials/src/main/resources/messages_en.properties @@ -114,6 +114,8 @@ burnCommandUsage=/ burnCommandUsage1=/ burnCommandUsage1Description=Sets the specified player on fire for the specified amount of seconds burnMsg=You set {0} on fire for {1} seconds. +buyCommandDescription=Buy specified item. +buyCommandUsage=/ <|> [amount] cannotSellNamedItem=You are not allowed to sell named items. cannotSellTheseNamedItems=You are not allowed to sell these named items\: {0} cannotStackMob=You do not have permission to stack multiple mobs. diff --git a/Essentials/src/main/resources/plugin.yml b/Essentials/src/main/resources/plugin.yml index dc03dfe4574..6bd23e667b4 100644 --- a/Essentials/src/main/resources/plugin.yml +++ b/Essentials/src/main/resources/plugin.yml @@ -78,7 +78,7 @@ commands: aliases: [eburn] buy: description: Buys item - usage: / <||hand|inventory|blocks> [amount] + usage: / <|> [amount] aliases: [ebuy] cartographytable: description: Opens up a cartography table. From fade2e811fd69a0997da5a964ae07ca26e64cb7d Mon Sep 17 00:00:00 2001 From: Superspeed500 Date: Sun, 6 Sep 2026 15:46:12 +0200 Subject: [PATCH 4/6] Somewhat functional command. --- .../essentials/commands/Commandbuy.java | 20 ++++++++++++++----- .../src/main/resources/messages_en.properties | 3 +++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java index ceed709a818..ba766e7dbea 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java @@ -29,6 +29,7 @@ public Commandbuy() { } // Starting point for when the command is run + // Args is an array with name of item in postion 0 and amount in position 1. @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { BigDecimal totalWorth = BigDecimal.ZERO; @@ -38,7 +39,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new NotEnoughArgumentsException(); } - final ItemStack is = ess.getItemDb().get("gold_ingot", 1); + final ItemStack is = ess.getItemDb().get(args[0]); int count = 0; totalWorth = totalWorth.add(buyItem(user, is, args)); @@ -54,9 +55,10 @@ public void run(final Server server, final User user, final String commandLabel, } private BigDecimal buyItem(final User user, final ItemStack is, final String[] args) throws Exception { - final int amount = 1; // Replace this with actual amount + final int amount = Integer.parseInt(args[1]); final BigDecimal originalWorth = ess.getWorth().getPrice(ess, is); final BigDecimal worth = originalWorth == null ? null : originalWorth.multiply(ess.getSettings().getMultiplier(user)); + final BigDecimal playerMoney = user.getMoney(); if (worth == null) { throw new TranslatableException("itemCannotBeBought"); @@ -70,13 +72,21 @@ private BigDecimal buyItem(final User user, final ItemStack is, final String[] a final ItemStack ris = is.clone(); ris.setAmount(amount); - + + // Take the money from the account + // But only if the player has enough money + if (playerMoney.compareTo(worth) > 0) { + user.takeMoney(result, null, UserBalanceUpdateEvent.Cause.COMMAND_BUY); + } else { + throw new TranslatableException("notEnoughMoney"); + } + + // Give the items the user is trying to buy Inventories.addItem(user.getBase(), user.isAuthorized("essentials.oversizedstacks") ? ess.getSettings().getOversizedStackSize() : 0, ris); user.getBase().updateInventory(); Trade.log("Command", "Buy", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(result, ess), user.getLocation(), user.getMoney(), ess); - // Needs to be changed to a method for taking money - user.takeMoney(result, null, UserBalanceUpdateEvent.Cause.COMMAND_BUY); + final String typeName = is.getType().toString().toLowerCase(Locale.ENGLISH); final AdventureUtil.ParsedPlaceholder worthDisplay = AdventureUtil.parsed(NumberUtil.displayCurrency(worth, ess)); user.sendTl("itemBought", AdventureUtil.parsed(NumberUtil.displayCurrency(result, ess)), amount, typeName, worthDisplay); diff --git a/Essentials/src/main/resources/messages_en.properties b/Essentials/src/main/resources/messages_en.properties index 6509b8a74b9..2740ccc6d1b 100644 --- a/Essentials/src/main/resources/messages_en.properties +++ b/Essentials/src/main/resources/messages_en.properties @@ -570,6 +570,9 @@ invseeNoSelf=You can only view other players'' inventories. is=is isIpBanned=IP {0} is banned. internalError=An internal error occurred while attempting to perform this command. +itemBought=Bought for {0} ({1} {2} at {3} each). +itemBoughtConsole={0} bought {1} for {2} ({3} items at {4} each). +itemCannotBeBought=That item cannot be bought from the server. itemCannotBeSold=That item cannot be sold to the server. itemCommandDescription=Spawn an item. itemCommandUsage=/ [amount [itemmeta...]] From f158aaf3364995eaedafdd6581f1436ff8631f58 Mon Sep 17 00:00:00 2001 From: Superspeed500 Date: Sun, 6 Sep 2026 18:36:27 +0200 Subject: [PATCH 5/6] Added some logic from Commandgive.java to handle full inventory. --- .../essentials/commands/Commandbuy.java | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java index ba766e7dbea..ec271fc48f3 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java @@ -8,16 +8,15 @@ import com.google.common.collect.Lists; import net.ess3.api.TranslatableException; import net.ess3.api.events.UserBalanceUpdateEvent; -import org.bukkit.ChatColor; import org.bukkit.Server; +import org.bukkit.World; import org.bukkit.inventory.ItemStack; - import java.math.BigDecimal; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.logging.Level; +import java.util.Map; import static com.earth2me.essentials.I18n.tlLiteral; @@ -40,18 +39,8 @@ public void run(final Server server, final User user, final String commandLabel, } final ItemStack is = ess.getItemDb().get(args[0]); - int count = 0; totalWorth = totalWorth.add(buyItem(user, is, args)); - - if (count != 1) { - final AdventureUtil.ParsedPlaceholder totalWorthStr = AdventureUtil.parsed(NumberUtil.displayCurrency(totalWorth, ess)); - if (args[0].equalsIgnoreCase("blocks")) { - user.sendTl("totalWorthBlocks", totalWorthStr, totalWorthStr); - } else { - user.sendTl("totalWorthAll", totalWorthStr, totalWorthStr); - } - } } private BigDecimal buyItem(final User user, final ItemStack is, final String[] args) throws Exception { @@ -59,17 +48,20 @@ private BigDecimal buyItem(final User user, final ItemStack is, final String[] a final BigDecimal originalWorth = ess.getWorth().getPrice(ess, is); final BigDecimal worth = originalWorth == null ? null : originalWorth.multiply(ess.getSettings().getMultiplier(user)); final BigDecimal playerMoney = user.getMoney(); + final boolean isDropItemsIfFull = ess.getSettings().isDropItemsIfFull(); + // Check if the item can be sold if (worth == null) { throw new TranslatableException("itemCannotBeBought"); } + // Input validation, check if the user is trying to buy a valid amount of item if (amount <= 0) { return BigDecimal.ZERO; } + // Get the total worth of all instances of the item final BigDecimal result = worth.multiply(BigDecimal.valueOf(amount)); - final ItemStack ris = is.clone(); ris.setAmount(amount); @@ -82,7 +74,18 @@ private BigDecimal buyItem(final User user, final ItemStack is, final String[] a } // Give the items the user is trying to buy - Inventories.addItem(user.getBase(), user.isAuthorized("essentials.oversizedstacks") ? ess.getSettings().getOversizedStackSize() : 0, ris); + // addITem returns any leftover items that can not be given to the player + final Map leftoverItems = Inventories.addItem(user.getBase(), user.isAuthorized("essentials.oversizedstacks") ? ess.getSettings().getOversizedStackSize() : 0, ris); + + // Only drop items if Essentials is configured to drop items if full + for (final ItemStack item : leftoverItems.values()) { + if (isDropItemsIfFull) { + final World w = user.getWorld(); + w.dropItemNaturally(user.getLocation(), item); + } else { + user.sendTl("giveSpawnFailure", item.getAmount(), args[0], user.getDisplayName()); + } + } user.getBase().updateInventory(); Trade.log("Command", "Buy", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(result, ess), user.getLocation(), user.getMoney(), ess); From 59dc0a86b2845324aa24377fb1fa4215ff3f0cf9 Mon Sep 17 00:00:00 2001 From: Superspeed500 Date: Sun, 6 Sep 2026 19:12:10 +0200 Subject: [PATCH 6/6] Bugfixes and adjustments. --- .../main/java/com/earth2me/essentials/ISettings.java | 2 -- .../main/java/com/earth2me/essentials/Settings.java | 5 ----- .../com/earth2me/essentials/commands/Commandbuy.java | 11 ++++++++++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/ISettings.java b/Essentials/src/main/java/com/earth2me/essentials/ISettings.java index c74a680e195..7542e2b39f8 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/ISettings.java +++ b/Essentials/src/main/java/com/earth2me/essentials/ISettings.java @@ -387,8 +387,6 @@ public interface ISettings extends IConf { boolean isAllowSellNamedItems(); - boolean isAllowBuyNamedItems(); - boolean isAddingPrefixInPlayerlist(); boolean isAddingSuffixInPlayerlist(); diff --git a/Essentials/src/main/java/com/earth2me/essentials/Settings.java b/Essentials/src/main/java/com/earth2me/essentials/Settings.java index 88532c55d07..69c8a0b3e17 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Settings.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Settings.java @@ -1951,11 +1951,6 @@ public boolean isAllowSellNamedItems() { return config.getBoolean("allow-selling-named-items", false); } - @Override - public boolean isAllowBuyNamedItems() { - return config.getBoolean("allow-buying-named-items", false); - } - @Override public boolean isAddingPrefixInPlayerlist() { return config.getBoolean("add-prefix-in-playerlist", false); diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java index ec271fc48f3..090842b4f58 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbuy.java @@ -67,7 +67,7 @@ private BigDecimal buyItem(final User user, final ItemStack is, final String[] a // Take the money from the account // But only if the player has enough money - if (playerMoney.compareTo(worth) > 0) { + if (playerMoney.compareTo(result) > 0) { user.takeMoney(result, null, UserBalanceUpdateEvent.Cause.COMMAND_BUY); } else { throw new TranslatableException("notEnoughMoney"); @@ -97,6 +97,15 @@ private BigDecimal buyItem(final User user, final ItemStack is, final String[] a return result; } + // We need to override the getMatchingItems function because we only want to list out all the possible items that exists. + // The default ArrayList of inventory, hand and block does not make sense with this command at the moment. + @Override + protected List getMatchingItems(final String arg) { + final List items = Lists.newArrayList(); + items.addAll(getItems()); + return items; + } + @Override protected List getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) { if (args.length == 1) {