From 9276fe357882fabadb69b95962f210ca6662a9f1 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Fri, 20 Dec 2024 21:34:24 +0100 Subject: [PATCH 1/3] refactor this ugliness --- .../commands/subcommands/GiveCommand.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java index 5d56959e84..b5d98cebe0 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java @@ -32,28 +32,31 @@ class GiveCommand extends SubCommand { @Override public void onExecute(CommandSender sender, String[] args) { - if (sender.hasPermission("slimefun.cheat.items") || !(sender instanceof Player)) { - if (args.length > 2) { - Optional player = PlayerList.findByName(args[1]); + if (!sender.hasPermission("slimefun.cheat.items") && sender instanceof Player) { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + return; + } - if (player.isPresent()) { - Player p = player.get(); + if (args.length <= 2) { + Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf give [Amount]")); + return; + } - SlimefunItem sfItem = SlimefunItem.getById(args[2].toUpperCase(Locale.ROOT)); + Optional player = PlayerList.findByName(args[1]); - if (sfItem != null) { - giveItem(sender, p, sfItem, args); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.invalid-item", true, msg -> msg.replace(PLACEHOLDER_ITEM, args[2])); - } - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.not-online", true, msg -> msg.replace(PLACEHOLDER_PLAYER, args[1])); - } - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf give [Amount]")); - } + if (player.isEmpty()) { + Slimefun.getLocalization().sendMessage(sender, "messages.not-online", true, msg -> msg.replace(PLACEHOLDER_PLAYER, args[1])); + return; + } + + Player p = player.get(); + + SlimefunItem sfItem = SlimefunItem.getById(args[2].toUpperCase(Locale.ROOT)); + + if (sfItem != null) { + giveItem(sender, p, sfItem, args); } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + Slimefun.getLocalization().sendMessage(sender, "messages.invalid-item", true, msg -> msg.replace(PLACEHOLDER_ITEM, args[2])); } } From 4d29b722fa8febc7cf3ad1dd9a98dbbf30d492c5 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Fri, 20 Dec 2024 21:35:00 +0100 Subject: [PATCH 2/3] More refactor --- .../commands/subcommands/GiveCommand.java | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java index b5d98cebe0..fd4613e1ea 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java @@ -63,23 +63,25 @@ public void onExecute(CommandSender sender, String[] args) { private void giveItem(CommandSender sender, Player p, SlimefunItem sfItem, String[] args) { if (sfItem instanceof MultiBlockMachine) { Slimefun.getLocalization().sendMessage(sender, "guide.cheat.no-multiblocks"); - } else { - int amount = parseAmount(args); - - if (amount > 0) { - Slimefun.getLocalization().sendMessage(p, "messages.given-item", true, msg -> msg.replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount))); - Map excess = p.getInventory().addItem(new CustomItemStack(sfItem.getItem(), amount)); - if (Slimefun.getCfg().getBoolean("options.drop-excess-sf-give-items") && !excess.isEmpty()) { - for (ItemStack is : excess.values()) { - p.getWorld().dropItem(p.getLocation(), is); - } - } - - Slimefun.getLocalization().sendMessage(sender, "messages.give-item", true, msg -> msg.replace(PLACEHOLDER_PLAYER, args[1]).replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount))); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.invalid-amount", true, msg -> msg.replace(PLACEHOLDER_AMOUNT, args[3])); + return; + } + + int amount = parseAmount(args); + + if (amount <= 0) { + Slimefun.getLocalization().sendMessage(sender, "messages.invalid-amount", true, msg -> msg.replace(PLACEHOLDER_AMOUNT, args[3])); + return; + } + + Slimefun.getLocalization().sendMessage(p, "messages.given-item", true, msg -> msg.replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount))); + Map excess = p.getInventory().addItem(new CustomItemStack(sfItem.getItem(), amount)); + if (Slimefun.getCfg().getBoolean("options.drop-excess-sf-give-items") && !excess.isEmpty()) { + for (ItemStack is : excess.values()) { + p.getWorld().dropItem(p.getLocation(), is); } } + + Slimefun.getLocalization().sendMessage(sender, "messages.give-item", true, msg -> msg.replace(PLACEHOLDER_PLAYER, args[1]).replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount))); } private int parseAmount(String[] args) { From 381c89f8272b719fadb3d143b0bca4cdd045b49d Mon Sep 17 00:00:00 2001 From: Intybyte Date: Fri, 20 Dec 2024 22:05:41 +0100 Subject: [PATCH 3/3] Fix the issue --- .../commands/subcommands/GiveCommand.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java index fd4613e1ea..f894f67536 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java @@ -74,7 +74,8 @@ private void giveItem(CommandSender sender, Player p, SlimefunItem sfItem, Strin } Slimefun.getLocalization().sendMessage(p, "messages.given-item", true, msg -> msg.replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount))); - Map excess = p.getInventory().addItem(new CustomItemStack(sfItem.getItem(), amount)); + ItemStack[] items = parseStackArray(sfItem.getItem(), amount); + Map excess = p.getInventory().addItem(items); if (Slimefun.getCfg().getBoolean("options.drop-excess-sf-give-items") && !excess.isEmpty()) { for (ItemStack is : excess.values()) { p.getWorld().dropItem(p.getLocation(), is); @@ -84,6 +85,30 @@ private void giveItem(CommandSender sender, Player p, SlimefunItem sfItem, Strin Slimefun.getLocalization().sendMessage(sender, "messages.give-item", true, msg -> msg.replace(PLACEHOLDER_PLAYER, args[1]).replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount))); } + private ItemStack[] parseStackArray(ItemStack itemStack, int amount) { + int stackSize = itemStack.getMaxStackSize(); + int stackAmount = amount / stackSize; + int excess = amount - stackAmount * stackSize; + + int totalSize = stackAmount; + if (excess != 0) { + totalSize++; + } + + ItemStack[] toGive = new ItemStack[totalSize]; + for (int i = 0; i < stackAmount; i++) { + toGive[i] = itemStack.clone(); + toGive[i].setAmount(stackSize); + } + + if (excess != 0) { + toGive[stackAmount] = itemStack.clone(); + toGive[stackAmount].setAmount(excess); + } + + return toGive; + } + private int parseAmount(String[] args) { int amount = 1;