Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tooltip syntax #6712

Merged
merged 16 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondTooltip.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
* 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<ItemType> items;
private boolean entire;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
items = (Expression<ItemType>) 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 (entire ? "entire" : "additional") + " tooltip of " + items.toString(event, debug) + " is " + (isNegated() ? "hidden" : "shown");
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
}

}
95 changes: 95 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffTooltip.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
* 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."
APickledWalrus marked this conversation as resolved.
Show resolved Hide resolved
})
@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|hide) %itemtypes%'[s] [entire|:additional] tool[ ]tip",
"(:show|hide) [the] [entire|:additional] tool[ ]tip of %itemtypes%"
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<ItemType> items;
private boolean show, entire;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
items = (Expression<ItemType>) exprs[0];
show = parseResult.hasTag("show");
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(!show);
} else {
if (show) {
meta.removeItemFlags(ItemFlag.HIDE_ADDITIONAL_TOOLTIP);
} else {
meta.addItemFlags(ItemFlag.HIDE_ADDITIONAL_TOOLTIP);
}
}
item.setItemMeta(meta);
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return (show ? "show" : "hide") + " the " + (entire ? "entire" : "additional") + " tooltip of " + items.toString(event, debug);
}

}
15 changes: 15 additions & 0 deletions src/test/skript/tests/syntaxes/conditions/CondTooltip.sk
Original file line number Diff line number Diff line change
@@ -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"