From 1a0d103320c131905136de5f990709782009f100 Mon Sep 17 00:00:00 2001 From: NicholasJBond Date: Mon, 25 Nov 2024 20:54:47 +1000 Subject: [PATCH] Fix #2212 Added per-item-transaction-permissions and enchant-sign-restrictions to config file. When true, it requires users to have specific commands to enchant or to buy and sell. Enchant sign tweaked a bit to make it auto adjust. --- .../java/com/earth2me/essentials/ISettings.java | 4 ++++ .../java/com/earth2me/essentials/Settings.java | 10 ++++++++++ .../earth2me/essentials/commands/Commandsell.java | 7 +++++++ .../com/earth2me/essentials/signs/SignBuy.java | 3 +++ .../earth2me/essentials/signs/SignEnchant.java | 15 +++++++++++++-- .../com/earth2me/essentials/signs/SignFree.java | 4 ++++ .../com/earth2me/essentials/signs/SignSell.java | 4 ++++ Essentials/src/main/resources/config.yml | 7 +++++++ .../src/main/resources/messages_en.properties | 3 +++ 9 files changed, 55 insertions(+), 2 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/ISettings.java b/Essentials/src/main/java/com/earth2me/essentials/ISettings.java index 41eb5531c94..b76f1f2b171 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/ISettings.java +++ b/Essentials/src/main/java/com/earth2me/essentials/ISettings.java @@ -193,6 +193,8 @@ public interface ISettings extends IConf { boolean useBukkitPermissions(); + boolean perItemTransactionPermissions(); + boolean addPrefixSuffix(); boolean disablePrefix(); @@ -355,6 +357,8 @@ public interface ISettings extends IConf { List getUnprotectedSignNames(); + boolean isEnchantSignRestricted(); + boolean isKitAutoEquip(); boolean isPastebinCreateKit(); diff --git a/Essentials/src/main/java/com/earth2me/essentials/Settings.java b/Essentials/src/main/java/com/earth2me/essentials/Settings.java index d57e5d913b4..c6d6e68f684 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Settings.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Settings.java @@ -1091,6 +1091,16 @@ public boolean useBukkitPermissions() { return config.getBoolean("use-bukkit-permissions", false); } + @Override + public boolean perItemTransactionPermissions() { + return config.getBoolean("per-item-transaction-permissions", false); + } + + @Override + public boolean isEnchantSignRestricted(){ + return config.getBoolean("enchant-sign-restrictions", false); + } + private boolean _addPrefixSuffix() { return config.getBoolean("add-prefix-suffix", false); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsell.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsell.java index 4110de1024b..6c015c67681 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsell.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsell.java @@ -56,6 +56,13 @@ public void run(final Server server, final User user, final String commandLabel, } } try { + if (!user.isAuthorized("essentials.item.sell."+ stack.getType()) && ess.getSettings().perItemTransactionPermissions()){ + if (isBulk) { + notSold.add(stack); + continue; + } + throw new TranslatableException("sellSpecificItemPermission", stack.getType().toString()); + } if (stack.getAmount() > 0) { totalWorth = totalWorth.add(sellItem(user, stack, args, isBulk)); stack = stack.clone(); diff --git a/Essentials/src/main/java/com/earth2me/essentials/signs/SignBuy.java b/Essentials/src/main/java/com/earth2me/essentials/signs/SignBuy.java index 8b59f79f62b..bc9f105b2cc 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/signs/SignBuy.java +++ b/Essentials/src/main/java/com/earth2me/essentials/signs/SignBuy.java @@ -26,6 +26,9 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri Trade items = getTrade(sign, 1, 2, player, ess); Trade charge = getTrade(sign, 3, ess); + if (!player.isAuthorized("essentials.item.buy."+ items.getItemStack().getType()) && ess.getSettings().perItemTransactionPermissions()){ + throw new SignException("buySpecificItemPermission", items.getItemStack().getType()); + } // Check if the player is trying to buy in bulk. if (ess.getSettings().isAllowBulkBuySell() && player.getBase().isSneaking()) { final ItemStack heldItem = player.getItemInHand(); diff --git a/Essentials/src/main/java/com/earth2me/essentials/signs/SignEnchant.java b/Essentials/src/main/java/com/earth2me/essentials/signs/SignEnchant.java index fe67342302c..722c7a9df45 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/signs/SignEnchant.java +++ b/Essentials/src/main/java/com/earth2me/essentials/signs/SignEnchant.java @@ -5,11 +5,13 @@ import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import com.earth2me.essentials.craftbukkit.Inventories; +import com.earth2me.essentials.utils.NumberUtil; import net.ess3.api.IEssentials; import net.ess3.provider.MaterialTagProvider; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; +import java.math.BigDecimal; import java.util.Locale; public class SignEnchant extends EssentialsSign { @@ -46,8 +48,8 @@ protected boolean onSignCreate(final ISign sign, final User player, final String final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && player.isAuthorized("essentials.enchantments.allowunsafe") && player.isAuthorized("essentials.signs.enchant.allowunsafe"); if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel())) { level = enchantment.getMaxLevel(); - sign.setLine(2, enchantLevel[0] + ":" + level); } + sign.setLine(2, enchantLevel[0].toLowerCase().replaceFirst("^[a-zA-Z]", enchantLevel[0].substring(0,1).toUpperCase()) + ":" + level); try { if (stack != null) { if (allowUnsafe) { @@ -59,7 +61,11 @@ protected boolean onSignCreate(final ISign sign, final User player, final String } catch (final Throwable ex) { throw new SignException(ex, "errorWithMessage", ex.getMessage()); } - getTrade(sign, 3, ess); + final Trade trade = getTrade(sign, 3, 0, ess); + final BigDecimal money = trade.getMoney(); + if (money != null) { + sign.setLine(3, NumberUtil.shortCurrency(money, ess)); + } return true; } @@ -76,6 +82,11 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri if (enchantment == null) { throw new SignException("enchantmentNotFound"); } + + if (!player.isAuthorized("essentials.enchantments."+enchantment.getKey().getKey()) && ess.getSettings().isEnchantSignRestricted()){ + throw new SignException("enchantmentPerm", enchantment.getKey().getKey()); + } + int level = 1; if (enchantLevel.length > 1) { try { diff --git a/Essentials/src/main/java/com/earth2me/essentials/signs/SignFree.java b/Essentials/src/main/java/com/earth2me/essentials/signs/SignFree.java index 8cbcaf3e2ab..dc5cf037ffe 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/signs/SignFree.java +++ b/Essentials/src/main/java/com/earth2me/essentials/signs/SignFree.java @@ -32,6 +32,10 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri itemStack = getItemMeta(player.getSource(), itemStack, sign.getLine(2), ess); final ItemStack item = getItemMeta(player.getSource(), itemStack, sign.getLine(3), ess); + if (!player.isAuthorized("essentials.item.free."+ item.getType()) && ess.getSettings().perItemTransactionPermissions()){ + throw new SignException("freeSpecificItemPermission", item.getType().toString()); + } + if (item.getType() == Material.AIR) { throw new SignException("cantSpawnItem", "Air"); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/signs/SignSell.java b/Essentials/src/main/java/com/earth2me/essentials/signs/SignSell.java index 1e1079f2588..f230affbcf6 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/signs/SignSell.java +++ b/Essentials/src/main/java/com/earth2me/essentials/signs/SignSell.java @@ -27,6 +27,10 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri Trade charge = getTrade(sign, 1, 2, player, ess); Trade money = getTrade(sign, 3, ess); + if (!player.isAuthorized("essentials.item.sell."+ charge.getItemStack().getType()) && ess.getSettings().perItemTransactionPermissions()){ + throw new SignException("sellSpecificItemPermission", charge.getItemStack().getType().toString()); + } + // Check if the player is trying to sell in bulk. if (ess.getSettings().isAllowBulkBuySell() && player.getBase().isSneaking()) { final ItemStack heldItem = player.getItemInHand(); diff --git a/Essentials/src/main/resources/config.yml b/Essentials/src/main/resources/config.yml index cec3295461c..8892067ad5a 100644 --- a/Essentials/src/main/resources/config.yml +++ b/Essentials/src/main/resources/config.yml @@ -334,6 +334,10 @@ player-commands: # Default is true. use-bukkit-permissions: true +# When set to true, a player will need the permission essentials.item..[itemName] in order to buy and +# sell items by command and sign. +per-item-transaction-permissions: false + # When this option is enabled, one-time use kits (ie. delay < 0) will be # removed from the /kit list when a player can no longer use it skip-used-one-time-kits-from-kit-list: false @@ -408,6 +412,9 @@ allow-old-id-signs: false unprotected-sign-names: #- kit +# When set to true, users will also need the permission essentials.enchantments.[enchantmentName] to use an enchantment sign. +enchant-sign-restrictions: false + # Backup runs a custom batch/bash command at a specified interval. # The server will save the world before executing the backup command, and disable # saving during the backup to prevent world corruption or other conflicts. diff --git a/Essentials/src/main/resources/messages_en.properties b/Essentials/src/main/resources/messages_en.properties index c096beb8b30..b1f616d8e44 100644 --- a/Essentials/src/main/resources/messages_en.properties +++ b/Essentials/src/main/resources/messages_en.properties @@ -97,6 +97,7 @@ burnCommandDescription=Set a player on fire. burnCommandUsage=/ burnCommandUsage1Description=Sets the specified player on fire for the specified amount of seconds burnMsg=You set {0} on fire for {1} seconds. +buySpecificItemPermission=You do not have permission to buy {0} 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. @@ -375,6 +376,7 @@ flyCommandUsage1Description=Toggles fly for yourself or another player if specif flying=flying flyMode=Set fly mode {0} for {1}. foreverAlone=You have nobody to whom you can reply. +freeSpecificItemPermission=You do not have permission to accept the free offer of {0} fullStack=You already have a full stack. fullStackDefault=Your stack has been set to its default size, {0}. fullStackDefaultOversize=Your stack has been set to its maximum size, {0}. @@ -980,6 +982,7 @@ sellCommandUsage3Description=Sells all possible items in your inventory sellCommandUsage4=/ blocks [amount] sellCommandUsage4Description=Sells all (or the given amount, if specified) of blocks in your inventory sellHandPermission=You do not have permission to hand sell. +sellSpecificItemPermission=You do not have permission to sell {0} serverFull=Server is full\! serverReloading=There''s a good chance you''re reloading your server right now. If that''s the case, why do you hate yourself? Expect no support from the EssentialsX team when using /reload. serverTotal=Server Total\: {0}