From 3c0119f4351dae51b02936d37e02625769451d0c Mon Sep 17 00:00:00 2001 From: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Date: Tue, 2 Jul 2024 06:24:03 +1000 Subject: [PATCH] Adds tooltip syntax (#6712) * Adds effect, condition, and tests for tooltip-related syntax * Removed star imports * Apply suggestions from code review Co-authored-by: Patrick Miller Co-authored-by: Fusezion * Suggestions from Pickle and Lyric - Changed the parse marks/tags in the effect and patterns for the condition - Added some info to the condition's description * Apply suggestions from code review Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com> * Some more suggestions - Changed the condition to use setNegated and isNegated, and use Sovde's pattern changes - Changed the effect description and fixed to work with Sovde's pattern changes * Small fix * Apply suggestions from code review Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> * Update src/main/java/ch/njol/skript/conditions/CondTooltip.java Co-authored-by: Patrick Miller * Adds "reveal" as option in syntax --------- Co-authored-by: Patrick Miller Co-authored-by: Fusezion Co-authored-by: Moderocky Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com> Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> --- .../njol/skript/conditions/CondTooltip.java | 87 +++++++++++++++++ .../ch/njol/skript/effects/EffTooltip.java | 95 +++++++++++++++++++ .../tests/syntaxes/conditions/CondTooltip.sk | 15 +++ 3 files changed, 197 insertions(+) create mode 100644 src/main/java/ch/njol/skript/conditions/CondTooltip.java create mode 100644 src/main/java/ch/njol/skript/effects/EffTooltip.java create mode 100644 src/test/skript/tests/syntaxes/conditions/CondTooltip.sk diff --git a/src/main/java/ch/njol/skript/conditions/CondTooltip.java b/src/main/java/ch/njol/skript/conditions/CondTooltip.java new file mode 100644 index 00000000000..dc7c795ca76 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondTooltip.java @@ -0,0 +1,87 @@ +/** + * 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.conditions; + +import ch.njol.skript.Skript; +import ch.njol.skript.aliases.ItemType; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.RequiredPlugins; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Condition; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.meta.ItemMeta; +import org.eclipse.jdt.annotation.Nullable; + +@Name("Has Item Tooltips") +@Description({ + "Whether the entire or additional tooltip of an item is shown or hidden.", + "The 'entire tooltip' is what shows to the player when they hover an item (i.e. name, lore, etc.).", + "The 'additional tooltip' hides certain information from certain items (potions, maps, books, fireworks, and banners)." +}) +@Examples({ + "send true if entire tooltip of player's tool is shown", + "if additional tooltip of {_item} is hidden:" +}) +@RequiredPlugins("Spigot 1.20.5+") +@Since("INSERT VERSION") +public class CondTooltip extends Condition { + + static { + if (Skript.methodExists(ItemMeta.class, "isHideTooltip")) {// this method was added in the same version as the additional tooltip item flag + Skript.registerCondition(CondTooltip.class, + "[the] [entire|:additional] tool[ ]tip[s] of %itemtypes% (is|are) (:shown|hidden)", + "[the] [entire|:additional] tool[ ]tip[s] of %itemtypes% (isn't|is not|aren't|are not) (:shown|hidden)", + "%itemtypes%'[s] [entire|:additional] tool[ ]tip[s] (is|are) (:shown|hidden)", + "%itemtypes%'[s] [entire|:additional] tool[ ]tip[s] (isn't|is not|aren't|are not) (:shown|hidden)" + ); + } + } + + @SuppressWarnings("NotNullFieldNotInitialized") + private Expression items; + private boolean entire; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + items = (Expression) exprs[0]; + entire = !parseResult.hasTag("additional"); + setNegated(parseResult.hasTag("shown") ^ (matchedPattern == 1 || matchedPattern == 3)); + return true; + } + + @Override + public boolean check(Event event) { + if (entire) + return items.check(event, item -> item.getItemMeta().isHideTooltip(), isNegated()); + return items.check(event, item -> item.getItemMeta().hasItemFlag(ItemFlag.HIDE_ADDITIONAL_TOOLTIP), isNegated()); + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return "the " + (entire ? "entire" : "additional") + " tooltip of " + items.toString(event, debug) + " is " + (isNegated() ? "hidden" : "shown"); + } + +} diff --git a/src/main/java/ch/njol/skript/effects/EffTooltip.java b/src/main/java/ch/njol/skript/effects/EffTooltip.java new file mode 100644 index 00000000000..26272c4b716 --- /dev/null +++ b/src/main/java/ch/njol/skript/effects/EffTooltip.java @@ -0,0 +1,95 @@ +/** + * 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.effects; + +import ch.njol.skript.Skript; +import ch.njol.skript.aliases.ItemType; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.RequiredPlugins; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Effect; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.meta.ItemMeta; +import org.eclipse.jdt.annotation.Nullable; + +@Name("Item Tooltips") +@Description({ + "Show or hide the tooltip of an item.", + "If changing the 'entire' tooltip of an item, nothing will show up when a player hovers over it.", + "If changing the 'additional' tooltip, only specific parts (which change per item) will be hidden." +}) +@Examples({ + "hide the entire tooltip of player's tool", + "hide {_item}'s additional tool tip" +}) +@RequiredPlugins("Spigot 1.20.5+") +@Since("INSERT VERSION") +public class EffTooltip extends Effect { + + static { + if (Skript.methodExists(ItemMeta.class, "setHideTooltip", boolean.class)) { // this method was added in the same version as the additional tooltip item flag + Skript.registerEffect(EffTooltip.class, + "(show|reveal|:hide) %itemtypes%'[s] [entire|:additional] tool[ ]tip", + "(show|reveal|:hide) [the] [entire|:additional] tool[ ]tip of %itemtypes%" + ); + } + } + + @SuppressWarnings("NotNullFieldNotInitialized") + private Expression items; + private boolean hide, entire; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + items = (Expression) exprs[0]; + hide = parseResult.hasTag("hide"); + entire = !parseResult.hasTag("additional"); + return true; + } + + @Override + protected void execute(Event event) { + for (ItemType item : items.getArray(event)) { + ItemMeta meta = item.getItemMeta(); + if (entire) { + meta.setHideTooltip(hide); + } else { + if (hide) { + meta.addItemFlags(ItemFlag.HIDE_ADDITIONAL_TOOLTIP); + } else { + meta.removeItemFlags(ItemFlag.HIDE_ADDITIONAL_TOOLTIP); + } + } + item.setItemMeta(meta); + } + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return (hide ? "hide" : "show") + " the " + (entire ? "entire" : "additional") + " tooltip of " + items.toString(event, debug); + } + +} diff --git a/src/test/skript/tests/syntaxes/conditions/CondTooltip.sk b/src/test/skript/tests/syntaxes/conditions/CondTooltip.sk new file mode 100644 index 00000000000..117a2b5bbb1 --- /dev/null +++ b/src/test/skript/tests/syntaxes/conditions/CondTooltip.sk @@ -0,0 +1,15 @@ +test "tooltip" when running minecraft "1.20.5": + + set {_item} to a diamond + assert entire tooltip of {_item} is shown with "item unexpectedly doesn't have entire tooltip" + assert additional tooltip of {_item} is shown with "item unexpectedly doesn't have additional tooltip" + + hide entire tooltip of {_item} + assert entire tooltip of {_item} is hidden with "item unexpectedly has entire tooltip" + hide additional tooltip of {_item} + assert additional tooltip of {_item} is hidden with "item unexpectedly has additional tooltip" + + show entire tooltip of {_item} + assert entire tooltip of {_item} is shown with "item unexpectedly doesn't have entire tooltip" + show additional tooltip of {_item} + assert additional tooltip of {_item} is shown with "item unexpectedly doesn't have additional tooltip"