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 4 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
86 changes: 86 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,86 @@
/**
* 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:"
})
@Since("INSERT VERSION")
@RequiredPlugins("MC 1.20.5+")
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
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 of %itemtypes% (is|are) (:shown|hidden)",
"[the] (:entire|additional) tool[ ]tip of %itemtypes% (isn't|is not|aren't|are not) (:shown|hidden)",
"%itemtypes%'[s] (:entire|additional) tool[ ]tip (is|are) (:shown|hidden)",
"%itemtypes%'[s] (:entire|additional) tool[ ]tip (isn't|is not|aren't|are not) (:shown|hidden)");
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
}

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

@SuppressWarnings("unchecked")
@Override
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
items = (Expression<ItemType>) exprs[0];
show = parseResult.hasTag("shown");
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
entire = parseResult.hasTag("entire");
not = (matchedPattern == 1 || matchedPattern == 3);
return true;
}

@Override
public boolean check(Event event) {
if (entire)
return items.check(event, item -> item.getItemMeta().isHideTooltip(), (show ^ not));
return items.check(event, item -> item.getItemMeta().hasItemFlag(ItemFlag.HIDE_ADDITIONAL_TOOLTIP), (show ^ not));
}

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

}
92 changes: 92 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,92 @@
/**
* 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."
})
@Examples({
"hide the entire tooltip of player's tool",
"hide {_item}'s additional tool tip"
})
@Since("INSERT VERSION")
@RequiredPlugins("MC 1.20.5+")
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
public class EffTooltip extends Effect {

static {
if (Skript.methodExists(ItemMeta.class, "isHideTooltip")) // this method was added in the same version as the additional tooltip item flag
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
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;

@SuppressWarnings("unchecked")
@Override
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
items = (Expression<ItemType>) exprs[0];
show = parseResult.hasTag("show");
entire = parseResult.hasTag("entire");
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 diamond
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
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}
hide additional tooltip of {_item}
assert entire tooltip of {_item} is hidden with "item unexpectedly has entire tooltip"
assert entire tooltip of {_item} is hidden with "item unexpectedly has additional tooltip"
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved

show entire tooltip of {_item}
show additional tooltip of {_item}
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"
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved