From 927ce047f7f5174ac46c9b412d1f52802106938f Mon Sep 17 00:00:00 2001 From: Alex 'mcmonkey' Goodwin Date: Tue, 8 Sep 2020 01:44:31 -0700 Subject: [PATCH] fix fakeitem translateSlot --- .../commands/item/FakeItemCommand.java | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/FakeItemCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/FakeItemCommand.java index 8f840a485c..b921c0ccd6 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/FakeItemCommand.java +++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/FakeItemCommand.java @@ -17,6 +17,7 @@ import com.denizenscript.denizencore.scripts.commands.AbstractCommand; import com.denizenscript.denizencore.utilities.scheduling.OneTimeSchedulable; import org.bukkit.entity.Player; +import org.bukkit.inventory.CraftingInventory; import org.bukkit.inventory.ItemStack; import java.util.Arrays; @@ -59,10 +60,7 @@ public FakeItemCommand() { @Override public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { - - /* Match arguments to expected variables */ for (Argument arg : scriptEntry.getProcessedArgs()) { - if (!scriptEntry.hasObject("slot") && arg.matchesPrefix("slot")) { scriptEntry.addObject("slot", arg.asElement()); @@ -88,59 +86,46 @@ else if (!scriptEntry.hasObject("player_only") else { arg.reportUnhandled(); } - } - if (!scriptEntry.hasObject("item")) { throw new InvalidArgumentsException("Must specify a valid item to fake!"); } - if (!scriptEntry.hasObject("slot")) { throw new InvalidArgumentsException("Must specify a valid slot!"); } - scriptEntry.defaultObject("duration", DurationTag.ZERO).defaultObject("player_only", new ElementTag(false)) .defaultObject("players", Arrays.asList(Utilities.getEntryPlayer(scriptEntry))); - } @Override public void execute(ScriptEntry scriptEntry) { - List items = (List) scriptEntry.getObject("item"); final ElementTag elSlot = scriptEntry.getElement("slot"); DurationTag duration = scriptEntry.getObjectTag("duration"); final List players = (List) scriptEntry.getObject("players"); final ElementTag player_only = scriptEntry.getElement("player_only"); - if (scriptEntry.dbCallShouldDebug()) { Debug.report(scriptEntry, getName(), ArgumentHelper.debugList("items", items) + elSlot.debug() + duration.debug() + ArgumentHelper.debugList("players", players) + player_only.debug()); } - int slot = SlotHelper.nameToIndex(elSlot.asString()); if (slot == -1) { Debug.echoError(scriptEntry.getResidingQueue(), "The input '" + elSlot.asString() + "' is not a valid slot!"); return; } final boolean playerOnly = player_only.asBoolean(); - final PacketHelper packetHelper = NMSHandler.getPacketHelper(); - for (ItemTag item : items) { if (item == null) { slot++; continue; } - for (PlayerTag player : players) { Player ent = player.getPlayerEntity(); packetHelper.setSlot(ent, translateSlot(ent, slot, playerOnly), item.getItemStack(), playerOnly); } - final int slotSnapshot = slot; slot++; - if (duration.getSeconds() > 0) { DenizenCore.schedule(new OneTimeSchedulable(new Runnable() { @Override @@ -158,11 +143,16 @@ public void run() { } static int translateSlot(Player player, int slot, boolean player_only) { - // This is (probably?) a translation from standard player inventory slots to ones that work with the full crafting inventory system - if (slot < 0) { - return 0; + // This translates Spigot slot standards to vanilla slots. + // The slot order is different when a player is viewing an inventory vs not doing so, leading to this chaos. + int total; + if (player_only || player.getOpenInventory().getTopInventory() instanceof CraftingInventory) { + total = 46; + } + else { + total = 36 + player.getOpenInventory().getTopInventory().getSize(); } - int total = player_only ? 46 : player.getOpenInventory().countSlots(); + int result; if (total == 46) { if (slot == 45) { return slot; @@ -172,10 +162,21 @@ else if (slot > 35) { return slot; } total -= 1; + result = (int) (slot + (total - 9) - (9 * (2 * Math.floor(slot / 9.0)))); + } + else { + int row = (int) Math.floor(slot / 9.0); + int column = slot - (row * 9); + int rowCount = (int) Math.ceil(total / 9.0); + int realRow = rowCount - row - 1; + result = realRow * 9 + column; + } + if (result < 0) { + return 0; } - if (slot > total) { + if (result > total) { return total; } - return (int) (slot + (total - 9) - (9 * (2 * Math.floor(slot / 9.0)))); + return result; } }