From e612dd3dd3b042ab7538025da5ab5936d2d8a9d1 Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Fri, 17 Mar 2023 00:10:39 -0600 Subject: [PATCH 01/17] Fix and open EffOpenInventory --- .../njol/skript/effects/EffOpenInventory.java | 268 +++++++++++------- 1 file changed, 173 insertions(+), 95 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java index 4412f69c5b4..53cfd816a80 100644 --- a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java +++ b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java @@ -21,6 +21,8 @@ import java.util.Locale; import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.inventory.InventoryType; @@ -34,125 +36,201 @@ import ch.njol.skript.doc.Since; import ch.njol.skript.lang.Effect; import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.Literal; import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.util.Version; import ch.njol.util.Kleenean; @Name("Open/Close Inventory") -@Description({"Opens an inventory to a player. The player can then access and modify the inventory as if it was a chest that he just opened.", - "Please note that currently 'show' and 'open' have the same effect, but 'show' will eventually show an unmodifiable view of the inventory in the future."}) -@Examples({"show the victim's inventory to the player", - "open the player's inventory for the player"}) -@Since("2.0, 2.1.1 (closing), 2.2-Fixes-V10 (anvil), 2.4 (hopper, dropper, dispenser") +@Description({ + "Opens an inventory to a player. The player can then access and modify the inventory as if it was a chest that they just opened.", + "Please note that currently 'show' and 'open' have different effects, 'show' will show an unmodifiable view of the inventory.", + "Whereas 'open' will attempt to make an inventory interactable." +}) +@Examples({ + "show anvil to player #unmodifiable, use open instead", + "open an anvil to the player", + "open a loom to the player", + "open the player's inventory for the player" +}) +@Since("2.0, 2.1.1 (closing), 2.2-Fixes-V10 (anvil), INSERT VERSION (enchanting, cartography, grindstone, loom)") public class EffOpenInventory extends Effect { - - private final static int WORKBENCH = 0, CHEST = 1, ANVIL = 2, HOPPER = 3, DROPPER = 4, DISPENSER = 5; - + + private static enum OpenableInventorySyntax { + + ANVIL("anvil"), + CARTOGRAPHY("cartography [table]", Skript.methodExists(HumanEntity.class, "openCartographyTable", Location.class, boolean.class), + "Opening a cartography table inventory requires PaperSpigot."), + ENCHANTING("enchant(ment|ing) [table]", new Version(1, 14)), + GRINDSTONE("grindstone", Skript.methodExists(HumanEntity.class, "openGrindstone", Location.class, boolean.class), + "Opening a grindstone inventory requires PaperSpigot."), + LOOM("loom", Skript.methodExists(HumanEntity.class, "openLoom", Location.class, boolean.class), + "Opening a loom inventory requires PaperSpigot."), + SMITHING("smithing [table]", Skript.methodExists(HumanEntity.class, "openSmithingTable", Location.class, boolean.class), + "Opening a smithing table inventory requires PaperSpigot."), + STONECUTTER("stone[ ]cutter", Skript.methodExists(HumanEntity.class, "openSmithingTable", Location.class, boolean.class), + "Opening a stone cutter inventory requires PaperSpigot."), + WORKBENCH("(crafting [table]|workbench)"); + + @Nullable + private String methodError; + + @Nullable + private Version version; + private final String property; + private boolean methodExists = true; + + OpenableInventorySyntax(String property) { + this.property = property; + } + + OpenableInventorySyntax(String property, Version version) { + this.property = property; + this.version = version; + } + + OpenableInventorySyntax(String property, boolean methodExists, String methodError) { + this.methodExists = methodExists; + this.methodError = methodError; + this.property = property; + } + + private String getFormatted() { + return this.toString().toLowerCase(Locale.ENGLISH) + ":" + property; + } + + private Version getVersion() { + return version; + } + + private boolean doesMethodExist() { + return methodExists; + } + + private String getMethodError() { + return methodError; + } + + private static String construct() { + StringBuilder builder = new StringBuilder("("); + OpenableInventorySyntax[] values = OpenableInventorySyntax.values(); + for (int i = 0; i < values.length; i++ ) { + builder.append(values[i].getFormatted()); + if (i + 1 < values.length) + builder.append("|"); + } + return builder.append(")").toString(); + } + } + static { Skript.registerEffect(EffOpenInventory.class, - "(open|show) ((0¦(crafting [table]|workbench)|1¦chest|2¦anvil|3¦hopper|4¦dropper|5¦dispenser) (view|window|inventory|)|%-inventory/inventorytype%) (to|for) %players%", - "close [the] inventory [view] (to|of|for) %players%", "close %players%'[s] inventory [view]"); + "show %inventory/inventorytype% (to|for) %players%", + "open [a[n]] " + OpenableInventorySyntax.construct() + "[view|window|inventory] (to|for) %players%", + "close [the] inventory [view] (of|for) %players%", + "close %players%'[s] inventory [view]"); } - + + private boolean open; + @Nullable - private Expression invi; - - boolean open; - private int invType; - - @SuppressWarnings("null") + private OpenableInventorySyntax syntax; + + @Nullable + private Expression inventory; private Expression players; - - @SuppressWarnings({"unchecked", "null"}) + @Override - public boolean init(final Expression[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { - int openFlag = 0; - if(parseResult.mark >= 5) { - openFlag = parseResult.mark ^ 5; - invType = DISPENSER; - } else if(parseResult.mark >= 4) { - openFlag = parseResult.mark ^ 4; - invType = DROPPER; - } else if(parseResult.mark >= 3) { - openFlag = parseResult.mark ^ 3; - invType = HOPPER; - } else if (parseResult.mark >= 2) { - openFlag = parseResult.mark ^ 2; - invType = ANVIL; - } else if (parseResult.mark >= 1) { - openFlag = parseResult.mark ^ 1; - invType = CHEST; - } else if (parseResult.mark >= 0) { - invType = WORKBENCH; - openFlag = parseResult.mark ^ 0; - } else { - openFlag = parseResult.mark; + @SuppressWarnings("unchecked") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + if (matchedPattern == 1) { + open = true; + assert !parseResult.tags.isEmpty() : "Syntax incorrectly handled parse result tags."; + syntax = OpenableInventorySyntax.valueOf(parseResult.tags.get(0).toUpperCase(Locale.ENGLISH)); + if (!Skript.isRunningMinecraft(syntax.getVersion())) { + Skript.error("Opening an inventory of type '" + syntax.toString().toLowerCase(Locale.ENGLISH) + "' is only present on Minecraft version " + syntax.getVersion()); + return false; + } + if (!syntax.doesMethodExist()) { + Skript.error(syntax.getMethodError()); + return false; + } } - - open = matchedPattern == 0; - invi = open ? exprs[0] : null; + inventory = matchedPattern == 0 ? exprs[0] : null; players = (Expression) exprs[exprs.length - 1]; - if (openFlag == 1 && invi != null) { - Skript.warning("Using 'show' inventory instead of 'open' is not recommended as it will eventually show an unmodifiable view of the inventory in the future."); + if (inventory instanceof Literal) { + Literal literal = (Literal) inventory; + Object object = literal.getSingle(); + if (object instanceof InventoryType && !((InventoryType)object).isCreatable()) { + Skript.error("You can't open a '" + literal.toString() + "' inventory to players. It's not creatable."); + return false; + } } return true; } - + @Override - protected void execute(final Event e) { - if (invi != null) { - Inventory i; - - assert invi != null; - Object o = invi.getSingle(e); - if (o instanceof Inventory) { - i = (Inventory) o; - } else if (o instanceof InventoryType) { - i = Bukkit.createInventory(null, (InventoryType) o); - } else { - return; - } - - if (i == null) + protected void execute(Event event) { + if (inventory != null) { // Show + Inventory inventory = null; + Object object = this.inventory.getSingle(event); + if (object == null) return; - for (final Player p : players.getArray(e)) { - try { - p.openInventory(i); - } catch (IllegalArgumentException ex){ - Skript.error("You can't open a " + i.getType().name().toLowerCase(Locale.ENGLISH).replaceAll("_", "") + " inventory to a player."); + for (Player player : players.getArray(event)) { + if (object instanceof Inventory) { + inventory = (Inventory) object; + } else if (object instanceof InventoryType) { + inventory = Bukkit.createInventory(player, (InventoryType) object); + } else { + assert false; } + if (inventory == null) + continue; + player.openInventory(inventory); } } else { - for (final Player p : players.getArray(e)) { - if (open) { - switch (invType) { - case WORKBENCH: - p.openWorkbench(null, true); - break; - case CHEST: - p.openInventory(Bukkit.createInventory(p, InventoryType.CHEST)); - break; - case ANVIL: - p.openInventory(Bukkit.createInventory(p, InventoryType.ANVIL)); - break; - case HOPPER: - p.openInventory(Bukkit.createInventory(p, InventoryType.HOPPER)); - break; - case DROPPER: - p.openInventory(Bukkit.createInventory(p, InventoryType.DROPPER)); - break; - case DISPENSER: - p.openInventory(Bukkit.createInventory(p, InventoryType.DISPENSER)); - - } - } else - p.closeInventory(); + for (Player player : players.getArray(event)) { + if (!open) { + player.closeInventory(); + continue; + } + switch (syntax) { + case ANVIL: + player.openAnvil(null, true); + break; + case CARTOGRAPHY: + player.openCartographyTable(null, true); + break; + case ENCHANTING: + player.openEnchanting(null, true); + break; + case GRINDSTONE: + player.openGrindstone(null, true); + break; + case LOOM: + player.openLoom(null, true); + break; + case SMITHING: + player.openSmithingTable(null, true); + break; + case STONECUTTER: + player.openStonecutter(null, true); + break; + case WORKBENCH: + player.openWorkbench(null, true); + break; + } } } } - + @Override - public String toString(final @Nullable Event e, final boolean debug) { - return (open ? "open " + (invi != null ? invi.toString(e, debug) : "crafting table") + " to " : "close inventory view of ") + players.toString(e, debug); + public String toString(@Nullable Event event, boolean debug) { + if (inventory != null) + return "show " + inventory.toString(event, debug) + " to " + players.toString(event, debug); + if (open) + return "open " + syntax.name().toLowerCase(Locale.ENGLISH) + " to " + players.toString(event, debug); + return "close inventory of " + players.toString(event, debug); } - + } From 70c0cbbbb8153f1b22ff0c49bd48bc57669071ee Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Fri, 17 Mar 2023 00:18:15 -0600 Subject: [PATCH 02/17] Clean up description --- src/main/java/ch/njol/skript/effects/EffOpenInventory.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java index 53cfd816a80..734f1998ed5 100644 --- a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java +++ b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java @@ -44,12 +44,12 @@ @Name("Open/Close Inventory") @Description({ "Opens an inventory to a player. The player can then access and modify the inventory as if it was a chest that they just opened.", - "Please note that currently 'show' and 'open' have different effects, 'show' will show an unmodifiable view of the inventory.", + "Note that 'show' and 'open' have different effects, 'show' will show an unmodifiable view of the inventory.", "Whereas 'open' will attempt to make an inventory interactable." }) @Examples({ - "show anvil to player #unmodifiable, use open instead", - "open an anvil to the player", + "show crafting table to player #unmodifiable, use open instead to allow for recipes to work", + "open a crafting table to the player", "open a loom to the player", "open the player's inventory for the player" }) From 94578671fee7d86f6b1f59224f0a99712150d545 Mon Sep 17 00:00:00 2001 From: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> Date: Fri, 17 Mar 2023 07:01:16 -0600 Subject: [PATCH 03/17] Update src/main/java/ch/njol/skript/effects/EffOpenInventory.java Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com> --- src/main/java/ch/njol/skript/effects/EffOpenInventory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java index 734f1998ed5..92dcc746423 100644 --- a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java +++ b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java @@ -161,7 +161,7 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye if (inventory instanceof Literal) { Literal literal = (Literal) inventory; Object object = literal.getSingle(); - if (object instanceof InventoryType && !((InventoryType)object).isCreatable()) { + if (object instanceof InventoryType && !((InventoryType) object).isCreatable()) { Skript.error("You can't open a '" + literal.toString() + "' inventory to players. It's not creatable."); return false; } From 7b5535a58207cd08447d2e7ff7e9d802173d9c11 Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Fri, 17 Mar 2023 13:22:36 -0600 Subject: [PATCH 04/17] Allow open %inventory% --- .../njol/skript/effects/EffOpenInventory.java | 58 +++++++++++-------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java index 734f1998ed5..669cab46420 100644 --- a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java +++ b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java @@ -38,14 +38,15 @@ import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.Literal; import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.registrations.Classes; import ch.njol.skript.util.Version; import ch.njol.util.Kleenean; @Name("Open/Close Inventory") @Description({ "Opens an inventory to a player. The player can then access and modify the inventory as if it was a chest that they just opened.", - "Note that 'show' and 'open' have different effects, 'show' will show an unmodifiable view of the inventory.", - "Whereas 'open' will attempt to make an inventory interactable." + "Note that 'show' and 'open' have different effects, 'show' will show just a view of the inventory.", + "Whereas 'open' will attempt to make an inventory real and usable. Like a workbench allowing recipes to work." }) @Examples({ "show crafting table to player #unmodifiable, use open instead to allow for recipes to work", @@ -119,7 +120,7 @@ private static String construct() { if (i + 1 < values.length) builder.append("|"); } - return builder.append(")").toString(); + return builder.append("|%-inventory%)").toString(); } } @@ -143,25 +144,26 @@ private static String construct() { @Override @SuppressWarnings("unchecked") public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + inventory = exprs.length > 1 ? exprs[0] : null; if (matchedPattern == 1) { open = true; - assert !parseResult.tags.isEmpty() : "Syntax incorrectly handled parse result tags."; - syntax = OpenableInventorySyntax.valueOf(parseResult.tags.get(0).toUpperCase(Locale.ENGLISH)); - if (!Skript.isRunningMinecraft(syntax.getVersion())) { - Skript.error("Opening an inventory of type '" + syntax.toString().toLowerCase(Locale.ENGLISH) + "' is only present on Minecraft version " + syntax.getVersion()); - return false; - } - if (!syntax.doesMethodExist()) { - Skript.error(syntax.getMethodError()); - return false; + if (!parseResult.tags.isEmpty()) { // %-inventory% was not used + syntax = OpenableInventorySyntax.valueOf(parseResult.tags.get(0).toUpperCase(Locale.ENGLISH)); + if (syntax.getVersion() != null && !Skript.isRunningMinecraft(syntax.getVersion())) { + Skript.error("Opening an inventory of type '" + syntax.toString().toLowerCase(Locale.ENGLISH) + "' is only present on Minecraft version " + syntax.getVersion()); + return false; + } + if (!syntax.doesMethodExist()) { + Skript.error(syntax.getMethodError()); + return false; + } } } - inventory = matchedPattern == 0 ? exprs[0] : null; players = (Expression) exprs[exprs.length - 1]; - if (inventory instanceof Literal) { + if (inventory instanceof Literal && inventory != null) { Literal literal = (Literal) inventory; Object object = literal.getSingle(); - if (object instanceof InventoryType && !((InventoryType)object).isCreatable()) { + if (object instanceof InventoryType && !((InventoryType) object).isCreatable()) { Skript.error("You can't open a '" + literal.toString() + "' inventory to players. It's not creatable."); return false; } @@ -176,18 +178,24 @@ protected void execute(Event event) { Object object = this.inventory.getSingle(event); if (object == null) return; - for (Player player : players.getArray(event)) { - if (object instanceof Inventory) { - inventory = (Inventory) object; - } else if (object instanceof InventoryType) { - inventory = Bukkit.createInventory(player, (InventoryType) object); - } else { - assert false; + if (object instanceof Inventory) { + inventory = (Inventory) object; + } else if (object instanceof InventoryType) { + try { + inventory = Bukkit.createInventory(null, (InventoryType) object); + } catch (Exception e) { + // Spigot forgot to label some InventoryType's as non creatable in some versions < 1.19.4 + // So this throws NullPointerException aswell ontop of the IllegalArgumentException. + // See https://hub.spigotmc.org/jira/browse/SPIGOT-7301 + Skript.error("You can't open a '" + Classes.toString((InventoryType) object) + "' inventory to players. It's not creatable."); } - if (inventory == null) - continue; - player.openInventory(inventory); + } else { + assert false; } + if (inventory == null) + return; + for (Player player : players.getArray(event)) + player.openInventory(inventory); } else { for (Player player : players.getArray(event)) { if (!open) { From 88fa19711563daa814f99183b303999bed22ab0e Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Sat, 18 Mar 2023 02:19:08 -0600 Subject: [PATCH 05/17] Add open inventory section --- .../skript/sections/SecOpenInventory.java | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/main/java/ch/njol/skript/sections/SecOpenInventory.java diff --git a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java new file mode 100644 index 00000000000..6db01ed1158 --- /dev/null +++ b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java @@ -0,0 +1,151 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.sections; + +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.Inventory; +import org.eclipse.jdt.annotation.Nullable; +import org.jetbrains.annotations.NotNull; + +import ch.njol.skript.Skript; +import ch.njol.skript.config.SectionNode; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.Literal; +import ch.njol.skript.lang.Section; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.Trigger; +import ch.njol.skript.lang.TriggerItem; +import ch.njol.skript.registrations.Classes; +import ch.njol.skript.variables.Variables; +import ch.njol.util.Kleenean; + +@Name("Open/Close Inventory Section") +@Description({ + "Opens an inventory to a player.", + "The section then allows to modify the event-inventory." +}) +@Examples({ + "show crafting table to player #unmodifiable, use open instead to allow for recipes to work", + "open a crafting table to the player", + "open a loom to the player", + "open the player's inventory for the player" +}) +@Since("INSERT VERSION") +public class SecOpenInventory extends Section { + + public static class InventorySectionEvent extends Event { + private final Inventory inventory; + + public InventorySectionEvent(Inventory inventory) { + this.inventory = inventory; + } + + public Inventory getInventory() { + return inventory; + } + + @Override + @NotNull + public HandlerList getHandlers() { + throw new IllegalStateException(); + } + } + + static { + Skript.registerSection(SecOpenInventory.class, "(show|open|create) %inventory/inventorytype%"); + } + + @Nullable + private Expression inventory; + + @Nullable + private Expression players; + + @Nullable + private Trigger trigger; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult, + SectionNode sectionNode, List triggerItems) { + + inventory = exprs[0]; + players = (Expression) exprs[1]; + if (inventory instanceof Literal) { + Literal literal = (Literal) inventory; + Object object = literal.getSingle(); + if (object instanceof InventoryType && !((InventoryType) object).isCreatable()) { + Skript.error("You can't open a '" + literal.toString() + "' inventory to players. It's not creatable."); + return false; + } + } + trigger = loadCode(sectionNode, "open inventory", InventorySectionEvent.class); + return true; + } + + @Override + protected TriggerItem walk(Event event) { + Inventory inventory = null; + Object object = this.inventory.getSingle(event); + if (object == null) + return super.walk(event, false); + if (object instanceof Inventory) { + inventory = (Inventory) object; + } else if (object instanceof InventoryType) { + try { + inventory = Bukkit.createInventory(null, (InventoryType) object); + } catch (Exception e) { + // Spigot forgot to label some InventoryType's as non creatable in some versions < 1.19.4 + // So this throws NullPointerException aswell ontop of the IllegalArgumentException. + // See https://hub.spigotmc.org/jira/browse/SPIGOT-7301 + Skript.error("You can't open a '" + Classes.toString((InventoryType) object) + "' inventory to players. It's not creatable."); + } + } else { + assert false; + } + if (inventory == null) + return super.walk(event, false); + InventorySectionEvent inventoryEvent = new InventorySectionEvent(inventory); + Object localVars = Variables.copyLocalVariables(event); + Variables.setLocalVariables(inventoryEvent, localVars); + TriggerItem.walk(trigger, inventoryEvent); + Variables.setLocalVariables(event, Variables.copyLocalVariables(inventoryEvent)); + Variables.removeLocals(inventoryEvent); + if (players != null) + for (Player player : players.getArray(event)) + player.openInventory(inventory); + return super.walk(event, false); + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return "section open " + inventory.toString(event, debug) + " to " + players.toString(event, debug); + } + +} From e81961ef2af233e94cd746733dc6b682e8cdea85 Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Sat, 18 Mar 2023 02:19:54 -0600 Subject: [PATCH 06/17] Add open inventory section --- .../java/ch/njol/skript/sections/SecOpenInventory.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java index 6db01ed1158..5057c0b3de2 100644 --- a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java +++ b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java @@ -51,10 +51,9 @@ "The section then allows to modify the event-inventory." }) @Examples({ - "show crafting table to player #unmodifiable, use open instead to allow for recipes to work", - "open a crafting table to the player", - "open a loom to the player", - "open the player's inventory for the player" + "new chest inventory:", + "\tset slot 1 of event-inventory to stone named \"example\"", + "open event-inventory to all players" }) @Since("INSERT VERSION") public class SecOpenInventory extends Section { @@ -78,7 +77,7 @@ public HandlerList getHandlers() { } static { - Skript.registerSection(SecOpenInventory.class, "(show|open|create) %inventory/inventorytype%"); + Skript.registerSection(SecOpenInventory.class, "[(show|open|create)] %inventory/inventorytype%"); } @Nullable From 0e3272456ccd3d8df83d3d17e8b6f3dec832de21 Mon Sep 17 00:00:00 2001 From: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> Date: Wed, 22 Mar 2023 16:12:00 -0600 Subject: [PATCH 07/17] Apply suggestions from code review Co-authored-by: Patrick Miller --- src/main/java/ch/njol/skript/effects/EffOpenInventory.java | 2 +- src/main/java/ch/njol/skript/sections/SecOpenInventory.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java index 669cab46420..709ea042ffc 100644 --- a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java +++ b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java @@ -127,7 +127,7 @@ private static String construct() { static { Skript.registerEffect(EffOpenInventory.class, "show %inventory/inventorytype% (to|for) %players%", - "open [a[n]] " + OpenableInventorySyntax.construct() + "[view|window|inventory] (to|for) %players%", + "open [a[n]] " + OpenableInventorySyntax.construct() + " [view|window|inventory] (to|for) %players%", "close [the] inventory [view] (of|for) %players%", "close %players%'[s] inventory [view]"); } diff --git a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java index 5057c0b3de2..b5313a5a78a 100644 --- a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java +++ b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java @@ -77,7 +77,7 @@ public HandlerList getHandlers() { } static { - Skript.registerSection(SecOpenInventory.class, "[(show|open|create)] %inventory/inventorytype%"); + Skript.registerSection(SecOpenInventory.class, "[show|open|create] %inventory/inventorytype%"); } @Nullable @@ -144,7 +144,7 @@ protected TriggerItem walk(Event event) { @Override public String toString(@Nullable Event event, boolean debug) { - return "section open " + inventory.toString(event, debug) + " to " + players.toString(event, debug); + return "open " + inventory.toString(event, debug) + " to " + players.toString(event, debug); } } From 25d8f7e77ff497c7b77f2b957ee905213448270c Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Fri, 30 Jun 2023 23:37:58 -0600 Subject: [PATCH 08/17] Apply changes --- src/main/java/ch/njol/skript/effects/EffOpenInventory.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java index 709ea042ffc..42ea1bf48ac 100644 --- a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java +++ b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java @@ -181,9 +181,12 @@ protected void execute(Event event) { if (object instanceof Inventory) { inventory = (Inventory) object; } else if (object instanceof InventoryType) { + InventoryType type = (InventoryType) object; + if (!type.isCreatable()) + return; try { - inventory = Bukkit.createInventory(null, (InventoryType) object); - } catch (Exception e) { + inventory = Bukkit.createInventory(null, type); + } catch (NullPointerException e) { // Spigot forgot to label some InventoryType's as non creatable in some versions < 1.19.4 // So this throws NullPointerException aswell ontop of the IllegalArgumentException. // See https://hub.spigotmc.org/jira/browse/SPIGOT-7301 From b27abf72eabf63e0b66cf77c747a22423f082224 Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Fri, 30 Jun 2023 23:45:55 -0600 Subject: [PATCH 09/17] Apply to both classes --- .../njol/skript/effects/EffOpenInventory.java | 31 ++++++++++++------- .../skript/sections/SecOpenInventory.java | 10 ++---- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java index 42ea1bf48ac..b420a6bbcff 100644 --- a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java +++ b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java @@ -171,6 +171,25 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye return true; } + /** + * Method because SecOpenInventory also uses this code block. + */ + @Nullable + public static Inventory createInventory(InventoryType type) { + Inventory inventory; + if (!type.isCreatable()) + return null; + try { + return Bukkit.createInventory(null, type); + } catch (NullPointerException e) { + // Spigot forgot to label some InventoryType's as non creatable in some versions < 1.19.4 + // So this throws NullPointerException aswell ontop of the IllegalArgumentException. + // See https://hub.spigotmc.org/jira/browse/SPIGOT-7301 + Skript.error("You can't open a '" + Classes.toString(type + "' inventory to players. It's not creatable."); + } + return null; + } + @Override protected void execute(Event event) { if (inventory != null) { // Show @@ -181,17 +200,7 @@ protected void execute(Event event) { if (object instanceof Inventory) { inventory = (Inventory) object; } else if (object instanceof InventoryType) { - InventoryType type = (InventoryType) object; - if (!type.isCreatable()) - return; - try { - inventory = Bukkit.createInventory(null, type); - } catch (NullPointerException e) { - // Spigot forgot to label some InventoryType's as non creatable in some versions < 1.19.4 - // So this throws NullPointerException aswell ontop of the IllegalArgumentException. - // See https://hub.spigotmc.org/jira/browse/SPIGOT-7301 - Skript.error("You can't open a '" + Classes.toString((InventoryType) object) + "' inventory to players. It's not creatable."); - } + inventory = createInventory((InventoryType) object); } else { assert false; } diff --git a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java index b5313a5a78a..15a53bf0940 100644 --- a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java +++ b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java @@ -35,6 +35,7 @@ import ch.njol.skript.doc.Examples; import ch.njol.skript.doc.Name; import ch.njol.skript.doc.Since; +import ch.njol.skript.effects.EffOpenInventory; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.Literal; import ch.njol.skript.lang.Section; @@ -117,14 +118,7 @@ protected TriggerItem walk(Event event) { if (object instanceof Inventory) { inventory = (Inventory) object; } else if (object instanceof InventoryType) { - try { - inventory = Bukkit.createInventory(null, (InventoryType) object); - } catch (Exception e) { - // Spigot forgot to label some InventoryType's as non creatable in some versions < 1.19.4 - // So this throws NullPointerException aswell ontop of the IllegalArgumentException. - // See https://hub.spigotmc.org/jira/browse/SPIGOT-7301 - Skript.error("You can't open a '" + Classes.toString((InventoryType) object) + "' inventory to players. It's not creatable."); - } + inventory = EffOpenInventory.createInventory((InventoryType) object); } else { assert false; } From 8eea6de268c7e03fbe1afb13b1b7fe9f8a5724fa Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Fri, 30 Jun 2023 23:47:39 -0600 Subject: [PATCH 10/17] Apply changes --- src/main/java/ch/njol/skript/effects/EffOpenInventory.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java index b420a6bbcff..da3784edc75 100644 --- a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java +++ b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java @@ -176,7 +176,6 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye */ @Nullable public static Inventory createInventory(InventoryType type) { - Inventory inventory; if (!type.isCreatable()) return null; try { @@ -185,7 +184,7 @@ public static Inventory createInventory(InventoryType type) { // Spigot forgot to label some InventoryType's as non creatable in some versions < 1.19.4 // So this throws NullPointerException aswell ontop of the IllegalArgumentException. // See https://hub.spigotmc.org/jira/browse/SPIGOT-7301 - Skript.error("You can't open a '" + Classes.toString(type + "' inventory to players. It's not creatable."); + Skript.error("You can't open a '" + Classes.toString(type) + "' inventory to players. It's not creatable."); } return null; } From 9b73d71cddc939917f6ce95ed7fd70b93c246bb1 Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Fri, 30 Jun 2023 23:48:41 -0600 Subject: [PATCH 11/17] Include IllegalArgumentException --- src/main/java/ch/njol/skript/effects/EffOpenInventory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java index da3784edc75..c0b3d2ae3aa 100644 --- a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java +++ b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java @@ -180,7 +180,7 @@ public static Inventory createInventory(InventoryType type) { return null; try { return Bukkit.createInventory(null, type); - } catch (NullPointerException e) { + } catch (NullPointerException | IllegalArgumentException e) { // Spigot forgot to label some InventoryType's as non creatable in some versions < 1.19.4 // So this throws NullPointerException aswell ontop of the IllegalArgumentException. // See https://hub.spigotmc.org/jira/browse/SPIGOT-7301 From f1816e39eb122fb15ce5b56a1a3913d94b39800d Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Fri, 30 Jun 2023 23:51:15 -0600 Subject: [PATCH 12/17] Remove un-needed imports --- src/main/java/ch/njol/skript/sections/SecOpenInventory.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java index 15a53bf0940..3af42b26dfd 100644 --- a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java +++ b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java @@ -20,14 +20,13 @@ import java.util.List; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; -import org.eclipse.jdt.annotation.Nullable; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import ch.njol.skript.Skript; import ch.njol.skript.config.SectionNode; @@ -42,7 +41,6 @@ import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.lang.Trigger; import ch.njol.skript.lang.TriggerItem; -import ch.njol.skript.registrations.Classes; import ch.njol.skript.variables.Variables; import ch.njol.util.Kleenean; From 75a8a302c92ab684ef31b902266c503f4feb25c1 Mon Sep 17 00:00:00 2001 From: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> Date: Sun, 31 Dec 2023 18:39:56 -0700 Subject: [PATCH 13/17] Update src/main/java/ch/njol/skript/sections/SecOpenInventory.java --- src/main/java/ch/njol/skript/sections/SecOpenInventory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java index 3af42b26dfd..96219b5ddcd 100644 --- a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java +++ b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java @@ -50,7 +50,7 @@ "The section then allows to modify the event-inventory." }) @Examples({ - "new chest inventory:", + "show chest inventory to player:", "\tset slot 1 of event-inventory to stone named \"example\"", "open event-inventory to all players" }) From 3d192685400b052f9c46c9de367972c67646da4a Mon Sep 17 00:00:00 2001 From: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> Date: Sun, 31 Dec 2023 18:40:18 -0700 Subject: [PATCH 14/17] Update src/main/java/ch/njol/skript/sections/SecOpenInventory.java --- src/main/java/ch/njol/skript/sections/SecOpenInventory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java index 96219b5ddcd..e66e2c439ec 100644 --- a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java +++ b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java @@ -76,7 +76,7 @@ public HandlerList getHandlers() { } static { - Skript.registerSection(SecOpenInventory.class, "[show|open|create] %inventory/inventorytype%"); + Skript.registerSection(SecOpenInventory.class, "[show|open|create] %inventory/inventorytype% [to %players%]"); } @Nullable From 87c440734f97f4ff759819e960023cefc80f1f54 Mon Sep 17 00:00:00 2001 From: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> Date: Thu, 3 Oct 2024 00:00:18 -0600 Subject: [PATCH 15/17] Apply suggestions from code review Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com> --- .../njol/skript/effects/EffOpenInventory.java | 6 ++--- .../skript/sections/SecOpenInventory.java | 23 ++----------------- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java index e0f64e32202..460f714e2d3 100644 --- a/src/main/java/ch/njol/skript/effects/EffOpenInventory.java +++ b/src/main/java/ch/njol/skript/effects/EffOpenInventory.java @@ -73,8 +73,7 @@ private static enum OpenableInventorySyntax { "Opening a stone cutter inventory requires PaperSpigot."), WORKBENCH("(crafting [table]|workbench)"); - @Nullable - private String methodError; + private @Nullable String methodError; @Nullable private Version version; @@ -174,8 +173,7 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye /** * Method because SecOpenInventory also uses this code block. */ - @Nullable - public static Inventory createInventory(InventoryType type) { + public static @Nullable Inventory createInventory(InventoryType type) { if (!type.isCreatable()) return null; try { diff --git a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java index e66e2c439ec..5c915f3e915 100644 --- a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java +++ b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java @@ -1,21 +1,3 @@ -/** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.sections; import java.util.List; @@ -52,7 +34,7 @@ @Examples({ "show chest inventory to player:", "\tset slot 1 of event-inventory to stone named \"example\"", - "open event-inventory to all players" + "\topen event-inventory to all players" }) @Since("INSERT VERSION") public class SecOpenInventory extends Section { @@ -69,8 +51,7 @@ public Inventory getInventory() { } @Override - @NotNull - public HandlerList getHandlers() { + public @NotNull HandlerList getHandlers() { throw new IllegalStateException(); } } From 8e1f81354bc1e930c92717e324af6d891e362440 Mon Sep 17 00:00:00 2001 From: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> Date: Thu, 3 Oct 2024 00:02:49 -0600 Subject: [PATCH 16/17] Update src/main/java/ch/njol/skript/sections/SecOpenInventory.java --- .../java/ch/njol/skript/sections/SecOpenInventory.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java index 5c915f3e915..a20164abe70 100644 --- a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java +++ b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java @@ -60,14 +60,11 @@ public Inventory getInventory() { Skript.registerSection(SecOpenInventory.class, "[show|open|create] %inventory/inventorytype% [to %players%]"); } - @Nullable - private Expression inventory; + @Nullable private Expression inventory; - @Nullable - private Expression players; + @Nullable private Expression players; - @Nullable - private Trigger trigger; + @Nullable private Trigger trigger; @Override @SuppressWarnings("unchecked") From 7326af179f186c49cef8ace165a95b03f594cffb Mon Sep 17 00:00:00 2001 From: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> Date: Thu, 3 Oct 2024 00:08:25 -0600 Subject: [PATCH 17/17] Update src/main/java/ch/njol/skript/sections/SecOpenInventory.java --- src/main/java/ch/njol/skript/sections/SecOpenInventory.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java index a20164abe70..e7e3751905c 100644 --- a/src/main/java/ch/njol/skript/sections/SecOpenInventory.java +++ b/src/main/java/ch/njol/skript/sections/SecOpenInventory.java @@ -60,11 +60,11 @@ public Inventory getInventory() { Skript.registerSection(SecOpenInventory.class, "[show|open|create] %inventory/inventorytype% [to %players%]"); } - @Nullable private Expression inventory; + private @Nullable Expression inventory; - @Nullable private Expression players; + private @Nullable Expression players; - @Nullable private Trigger trigger; + private @Nullable Trigger trigger; @Override @SuppressWarnings("unchecked")