From 1a6ad2fdb0881f284accf4a96e16a727dede7991 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Mon, 6 Jul 2020 14:55:07 -0400 Subject: [PATCH] Allow for specifying amount to increase itemstack in /more (#3302) Fixes #2342. --- .../essentials/commands/Commandmore.java | 29 +++++++++++++++---- .../earth2me/essentials/utils/NumberUtil.java | 7 +++++ Essentials/src/messages.properties | 7 +++-- Essentials/src/plugin.yml | 4 +-- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandmore.java b/Essentials/src/com/earth2me/essentials/commands/Commandmore.java index 2109d176b..63630e39d 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandmore.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandmore.java @@ -1,6 +1,8 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.NumberUtil; +import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.inventory.ItemStack; @@ -17,22 +19,37 @@ public class Commandmore extends EssentialsCommand { @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { final ItemStack stack = user.getItemInHand(); - if (stack == null) { + if (stack == null || stack.getType() == Material.AIR) { throw new Exception(tl("cantSpawnItem", "Air")); } - if (stack.getAmount() >= ((user.isAuthorized("essentials.oversizedstacks")) ? ess.getSettings().getOversizedStackSize() : stack.getMaxStackSize())) { + boolean canOversized = user.isAuthorized("essentials.oversizedstacks"); + if (stack.getAmount() >= ((canOversized) ? ess.getSettings().getOversizedStackSize() : stack.getMaxStackSize())) { throw new Exception(tl("fullStack")); } + final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""); if (!user.canSpawnItem(stack.getType())) { throw new Exception(tl("cantSpawnItem", itemname)); } - if (user.isAuthorized("essentials.oversizedstacks")) { - stack.setAmount(ess.getSettings().getOversizedStackSize()); + + int newStackSize = stack.getAmount(); + if (args.length >= 1) { + if (!NumberUtil.isPositiveInt(args[0])) { + throw new Exception(tl("nonZeroPosNumber")); + } + newStackSize += Integer.parseInt(args[0]); + + if (newStackSize > ((canOversized) ? ess.getSettings().getOversizedStackSize() : stack.getMaxStackSize())) { + user.sendMessage(tl(canOversized ? "fullStackDefaultOversize" : "fullStackDefault", canOversized ? ess.getSettings().getOversizedStackSize() : stack.getMaxStackSize())); + newStackSize = canOversized ? ess.getSettings().getOversizedStackSize() : stack.getMaxStackSize(); + } + } else if (canOversized) { + newStackSize = ess.getSettings().getOversizedStackSize(); } else { - stack.setAmount(stack.getMaxStackSize()); + newStackSize = stack.getMaxStackSize(); } + stack.setAmount(newStackSize); user.getBase().updateInventory(); } -} \ No newline at end of file +} diff --git a/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java b/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java index 000a7f236..3bbed3bb8 100644 --- a/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/NumberUtil.java @@ -105,4 +105,11 @@ public class NumberUtil { } return true; } + + public static boolean isPositiveInt(final String sInt) { + if (!isInt(sInt)) { + return false; + } + return Integer.parseInt(sInt) > 0; + } } diff --git a/Essentials/src/messages.properties b/Essentials/src/messages.properties index e9319724f..0965cdabe 100644 --- a/Essentials/src/messages.properties +++ b/Essentials/src/messages.properties @@ -220,6 +220,8 @@ flying=flying flyMode=\u00a76Set fly mode\u00a7c {0} \u00a76for {1}\u00a76. foreverAlone=\u00a74You have nobody to whom you can reply. fullStack=\u00a74You already have a full stack. +fullStackDefault=\u00a76Your stack has been set to its default size, \u00a7c{0}\u00a76. +fullStackDefaultOversize=\u00a76Your stack has been set to its maximum size, \u00a7c{0}\u00a76. gameMode=\u00a76Set game mode\u00a7c {0} \u00a76for \u00a7c{1}\u00a76. gameModeInvalid=\u00a74You need to specify a valid player/mode. gamemodeCommandDescription=Change player gamemode. @@ -444,8 +446,8 @@ moneyRecievedFrom=\u00a7a{0}\u00a76 has been received from\u00a7a {1}\u00a76. moneySentTo=\u00a7a{0} has been sent to {1}. month=month months=months -moreCommandDescription=Fills the item stack in hand to maximum size. -moreCommandUsage=/ +moreCommandDescription=Fills the item stack in hand to specified amount, or to maximum size if none is specified. +moreCommandUsage=/ [amount] moreThanZero=\u00a74Quantities must be greater than 0. motdCommandDescription=Views the Message Of The Day. motdCommandUsage=/ [chapter] [page] @@ -512,6 +514,7 @@ noMetaJson=JSON Metadata is not supported in this version of Bukkit. noMetaPerm=\u00a74You do not have permission to apply \u00a7c{0}\u00a74 meta to this item. none=none noNewMail=\u00a76You have no new mail. +nonZeroPosNumber=\u00a74A non-zero number is required. noPendingRequest=\u00a74You do not have a pending request. noPerm=\u00a74You do not have the \u00a7c{0}\u00a74 permission. noPermissionSkull=\u00a74You do not have permission to modify that skull. diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml index 3d800a85f..569ddf288 100644 --- a/Essentials/src/plugin.yml +++ b/Essentials/src/plugin.yml @@ -269,8 +269,8 @@ commands: usage: / aliases: [action,eaction,describe,edescribe,eme] more: - description: Fills the item stack in hand to maximum size. - usage: / + description: Fills the item stack in hand to specified amount, or to maximum size if none is specified. + usage: / [amount] aliases: [emore] motd: description: Views the Message Of The Day.