diff --git a/src/main/java/com/Acrobot/ChestShop/ChestShop.java b/src/main/java/com/Acrobot/ChestShop/ChestShop.java index c103e32e3..c7a041b05 100644 --- a/src/main/java/com/Acrobot/ChestShop/ChestShop.java +++ b/src/main/java/com/Acrobot/ChestShop/ChestShop.java @@ -19,6 +19,8 @@ import com.Acrobot.Breeze.Configuration.Configuration; import com.Acrobot.ChestShop.Commands.Give; import com.Acrobot.ChestShop.Commands.ItemInfo; +import com.Acrobot.ChestShop.Commands.SetAmount; +import com.Acrobot.ChestShop.Commands.SetPrice; import com.Acrobot.ChestShop.Commands.Toggle; import com.Acrobot.ChestShop.Commands.Version; import com.Acrobot.ChestShop.Configuration.Messages; @@ -136,6 +138,8 @@ public void onEnable() { getCommand("csVersion").setExecutor(new Version()); getCommand("csGive").setExecutor(new Give()); getCommand("cstoggle").setExecutor(new Toggle()); + getCommand("csSetPrice").setExecutor(new SetPrice()); + getCommand("csSetAmount").setExecutor(new SetAmount()); } private void handleMigrations() { diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/SetAmount.java b/src/main/java/com/Acrobot/ChestShop/Commands/SetAmount.java new file mode 100644 index 000000000..8f8c50a79 --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Commands/SetAmount.java @@ -0,0 +1,70 @@ +package com.Acrobot.ChestShop.Commands; + +import com.Acrobot.ChestShop.Configuration.Messages; +import com.Acrobot.ChestShop.Signs.ChestShopSign; +import static com.Acrobot.ChestShop.Permission.ADMIN; + +import com.Acrobot.ChestShop.ChestShop; +import com.Acrobot.ChestShop.Permission; +import org.bukkit.block.Block; +import org.bukkit.block.Sign; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.util.RayTraceResult; + +public class SetAmount implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) { + return false; + } + + if (!Permission.has(sender, Permission.SET_AMOUNT_COMMAND)) { + sender.sendMessage(Messages.ACCESS_DENIED); + return true; + } + + Player player = (Player) sender; + RayTraceResult result = player.rayTraceBlocks(8); + Block signBlock = null; + if (result != null) { + signBlock = result.getHitBlock(); + } + if (signBlock == null || !ChestShopSign.isValid(signBlock)) { + sender.sendMessage(Messages.MUST_LOOK_AT_SHOP_SIGN); + return true; + } + + Sign sign = (Sign) signBlock.getState(); + if (!ChestShopSign.canAccess(player, sign) && !Permission.has(player, ADMIN)) { + sender.sendMessage(Messages.ACCESS_DENIED); + return true; + } + + String newAmountLine = String.join(" ", args); + String[] line = sign.getLines(); + line[1] = newAmountLine; + if (!ChestShopSign.isValidPreparedSign(line)) { + sender.sendMessage(Messages.INVALID_AMOUNT_LINE); + return true; + } + + SignChangeEvent event = new SignChangeEvent(signBlock, player, line); + ChestShop.getPlugin().getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + sender.sendMessage(Messages.SHOP_UPDATE_FAILED); + return true; + } + + line = event.getLines(); + + for (int i = 0; i < line.length; i++) { + sign.setLine(i, line[i]); + } + sign.update(); + return true; + } +} diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/SetPrice.java b/src/main/java/com/Acrobot/ChestShop/Commands/SetPrice.java new file mode 100644 index 000000000..aa680dd0a --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Commands/SetPrice.java @@ -0,0 +1,70 @@ +package com.Acrobot.ChestShop.Commands; + +import com.Acrobot.ChestShop.Configuration.Messages; +import com.Acrobot.ChestShop.Signs.ChestShopSign; +import static com.Acrobot.ChestShop.Permission.ADMIN; + +import com.Acrobot.ChestShop.ChestShop; +import com.Acrobot.ChestShop.Permission; +import org.bukkit.block.Block; +import org.bukkit.block.Sign; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.util.RayTraceResult; + +public class SetPrice implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) { + return false; + } + + if (!Permission.has(sender, Permission.SET_PRICE_COMMAND)) { + sender.sendMessage(Messages.ACCESS_DENIED); + return true; + } + + Player player = (Player) sender; + RayTraceResult result = player.rayTraceBlocks(8); + Block signBlock = null; + if (result != null) { + signBlock = result.getHitBlock(); + } + if (signBlock == null || !ChestShopSign.isValid(signBlock)) { + sender.sendMessage(Messages.MUST_LOOK_AT_SHOP_SIGN); + return true; + } + + Sign sign = (Sign) signBlock.getState(); + if (!ChestShopSign.canAccess(player, sign) && !Permission.has(player, ADMIN)) { + sender.sendMessage(Messages.ACCESS_DENIED); + return true; + } + + String newPriceLine = String.join(" ", args); + String[] line = sign.getLines(); + line[2] = newPriceLine; + if (!ChestShopSign.isValidPreparedSign(line)) { + sender.sendMessage(Messages.INVALID_PRICE_LINE); + return true; + } + + SignChangeEvent event = new SignChangeEvent(signBlock, player, line); + ChestShop.getPlugin().getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + sender.sendMessage(Messages.SHOP_UPDATE_FAILED); + return true; + } + + line = event.getLines(); + + for (int i = 0; i < line.length; i++) { + sign.setLine(i, line[i]); + } + sign.update(); + return true; + } +} diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java index 0c8698a56..67314f4d4 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java @@ -78,6 +78,12 @@ public class Messages { public static String TOGGLE_MESSAGES_OFF = "You will no longer receive messages from your shop(s)."; public static String TOGGLE_MESSAGES_ON = "You will now receive messages from your shop(s)."; + @PrecededBySpace + public static String MUST_LOOK_AT_SHOP_SIGN = "You must look at a shop sign when using this command."; + public static String INVALID_PRICE_LINE = "You have specified an invalid price!"; + public static String INVALID_AMOUNT_LINE = "You have specified an invalid amount!"; + public static String SHOP_UPDATE_FAILED = "This shop cannot be edited."; + public static String prefix(String message) { return PREFIX + message; } diff --git a/src/main/java/com/Acrobot/ChestShop/Permission.java b/src/main/java/com/Acrobot/ChestShop/Permission.java index 1768b8081..39a3cf28a 100644 --- a/src/main/java/com/Acrobot/ChestShop/Permission.java +++ b/src/main/java/com/Acrobot/ChestShop/Permission.java @@ -19,7 +19,9 @@ public enum Permission { NOFEE("ChestShop.nofee"), DISCOUNT("ChestShop.discount."), - NOTIFY_TOGGLE("ChestShop.toggle"); + NOTIFY_TOGGLE("ChestShop.toggle"), SET_PRICE_COMMAND("ChestShop.setpricecommand"), + + SET_AMOUNT_COMMAND("ChestShop.setamountcommand"); private final String permission; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0b918afc1..4fb7d6cbf 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -38,7 +38,13 @@ commands: cstoggle: description: Toggle messages to the owner of a shop usage: / - + csSetPrice: + description: Edit the price of a shop + usage: / + csSetAmount: + description: Edit the amount of a shop + usage: / + permissions: ChestShop.*: description: Gives access to all ChestShop permissions @@ -52,6 +58,8 @@ permissions: ChestShop.shop.create: true ChestShop.shop.buy: true ChestShop.shop.sell: true + ChestShop.setpricecommand: true + ChestShop.setamountcommand: true default: true ChestShop.shop.create: description: Allows the user to create a shop that sells and buys any item @@ -62,6 +70,10 @@ permissions: description: Allows the user to create a shop that sells any item ChestShop.shop.create.sell: description: Allows the user to create a shop that buy any item + ChestShop.setpricecommand: + description: Allows the user modify the price of existing own shops + ChestShop.setamountcommand: + description: Allows the user modify the amount of existing own shops ChestShop.shop.create.(itemType): description: Allows user to create a shop that sells item with itemType like in the permission node (replace (itemType) with Material item name) ChestShop.shop.buy.(itemType):