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 Enchantment Glint Syntaxes #6638

Open
wants to merge 31 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3d9a79d
Add enchantment glint override
NotSoDelayed May 3, 2024
1e4d32c
Add Skript docs and fix annotations
NotSoDelayed May 3, 2024
b0319a7
Add expr item with glint
NotSoDelayed May 4, 2024
c97d40c
Add condition item has enchantment glint
NotSoDelayed May 4, 2024
cb1cdc5
Add version check
NotSoDelayed May 4, 2024
cc60891
Add tests
NotSoDelayed May 4, 2024
49cce6f
Correction to syntax patterns
NotSoDelayed May 4, 2024
c0740c7
License says hi
NotSoDelayed May 4, 2024
d75af9d
Expression said bye
NotSoDelayed May 4, 2024
49f79d6
Fix setExpr
NotSoDelayed May 4, 2024
4405b6c
Finally
NotSoDelayed May 4, 2024
9540e42
Fix tests
NotSoDelayed May 4, 2024
7036781
Bulk requested changes
NotSoDelayed May 5, 2024
6ed6051
Merge branch 'dev/feature' into feature/expr-enchantment-glint
Moderocky May 8, 2024
ec0a561
Merge branch 'dev/feature' into feature/expr-enchantment-glint
NotSoDelayed May 10, 2024
1477e1f
Merge branch 'dev/feature' into feature/expr-enchantment-glint
Moderocky Jun 19, 2024
b0c2278
Merge branch 'dev/feature' into feature/expr-enchantment-glint
Moderocky Jun 26, 2024
48ef71d
Implementation overhaul
NotSoDelayed Jun 30, 2024
f65f7c7
Implementation overhaul
NotSoDelayed Jun 30, 2024
70c07d2
Implementation overhaul
NotSoDelayed Jun 30, 2024
2588bd9
Fix test
NotSoDelayed Jun 30, 2024
c8658fc
Fix test
NotSoDelayed Jul 1, 2024
36ef45a
Fix test
NotSoDelayed Jul 1, 2024
7795ea5
Fix test
NotSoDelayed Jul 1, 2024
3cc306a
Fix test
NotSoDelayed Jul 1, 2024
9d3d7f7
Merge branch 'dev/feature' into feature/expr-enchantment-glint
NotSoDelayed Jul 1, 2024
5091294
Remove license header (#6684)
NotSoDelayed Jul 19, 2024
e17c558
Merge branch 'dev/feature' into feature/expr-enchantment-glint
NotSoDelayed Jul 20, 2024
cad832e
Merge branch 'dev/feature' into feature/expr-enchantment-glint
Moderocky Aug 15, 2024
1c9d1f0
Requested changes and optimise imports
NotSoDelayed Aug 24, 2024
1c0408f
Merge remote-tracking branch 'origin/feature/expr-enchantment-glint' …
NotSoDelayed Aug 24, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ch.njol.skript.conditions;

import org.bukkit.event.Event;
import org.bukkit.inventory.meta.ItemMeta;
import org.eclipse.jdt.annotation.Nullable;

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;

@Name("Item Has Enchantment Glint Override")
@Description("Checks whether an item has the enchantment glint overridden, or is forced to glint or not.")
@Examples({
"if the player's tool has the enchantment glint override",
"\tsend \"Your tool has the enchantment glint override.\" to player",
"",
"if {_item} is forced to glint:",
"\tsend \"This item is forced to glint.\" to player",
"else if {_item} is forced to not glint:",
"\tsend \"This item is forced to not glint.\" to player",
"else:",
"\tsend \"This item does not have any glint override.\" to player"
})
@RequiredPlugins("Spigot 1.20.5+")
@Since("INSERT VERSION")
public class CondItemEnchantmentGlint extends Condition {

static {
if (Skript.isRunningMinecraft(1, 20, 5))
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
Skript.registerCondition(CondItemEnchantmentGlint.class,
"%itemtypes% (has|have) [the] enchantment glint overrid(den|e)",
"%itemtypes% (doesn't|does not|do not|don't) have [the] enchantment glint overrid(den|e)",
"%itemtypes% (is|are) forced (to [:not]|[:not] to) glint");
}

private Expression<ItemType> itemtypes;
private int pattern;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
itemtypes = (Expression<ItemType>) expressions[0];
pattern = matchedPattern;
// Pattern 'is forced to glint'
if (matchedPattern == 2) {
setNegated(parseResult.hasTag("not"));
// Pattern 'has enchantment glint override'
} else {
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
setNegated(matchedPattern == 1);
}
return true;
}

@Override
public boolean check(Event event) {
return itemtypes.check(event, itemType -> {
ItemMeta meta = itemType.getItemMeta();
// Pattern 'is forced to glint'
if (pattern == 2) {
if (!meta.hasEnchantmentGlintOverride())
return isNegated();
return meta.getEnchantmentGlintOverride();
// Pattern 'has enchantment glint override'
} else {
return meta.hasEnchantmentGlintOverride();
}
}, isNegated());
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return null;
}

}
77 changes: 77 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffForceEnchantmentGlint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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.meta.ItemMeta;
import org.jetbrains.annotations.Nullable;

@Name("Force Enchantment Glint")
@Description("Forces the items to glint or not, or removes its existing enchantment glint enforcement.")
@Examples({
"force {_items::*} to glint",
"force the player's tool to stop glinting"
})
@RequiredPlugins("Spigot 1.20.5+")
@Since("INSERT VERSION")
public class EffForceEnchantmentGlint extends Effect {

static {
if (Skript.isRunningMinecraft(1, 20, 5))
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
Skript.registerEffect(EffForceEnchantmentGlint.class,
"(force|make) %itemtypes% [to] [start] glint[ing]",
"(force|make) %itemtypes% [to] (not|stop) glint[ing]",
"(clear|delete) [the] enchantment glint override of %itemtypes%",
"(clear|delete) %itemtypes%'s enchantment glint override");
}

@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<ItemType> itemtypes;
private int pattern;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
itemtypes = (Expression<ItemType>) expressions[0];
pattern = matchedPattern;
return true;
}

@Override
protected void execute(Event event) {
for (ItemType itemType : itemtypes.getArray(event)) {
ItemMeta meta = itemType.getItemMeta();
Boolean glint;
// Pattern: forced to glint
if (pattern == 0) {
glint = true;
// Pattern: forced to not glint
} else if (pattern == 1) {
glint = false;
// Pattern: Clear glint override
} else {
glint = null;
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
}
meta.setEnchantmentGlintOverride(glint);
itemType.setItemMeta(meta);
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
// Pattern: Clear glint override
if (pattern > 1)
return "clear the enchantment glint override of " + itemtypes.toString(event, debug);
return "force the " + itemtypes.toString(event, debug) + " to " + (pattern == 0 ? "start" : "stop") + " glinting";
}

}
93 changes: 93 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprEnchantmentGlint.java
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think we should look into how the conditions can be modified so that we do not need to include a boolean expression

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.Changer.ChangeMode;
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.expressions.base.SimplePropertyExpression;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.Nullable;

@Name("Enchantment Glint")
@Description({
"Sets the 'enchantment_glint_override' on items.",
"If true, the item will glint, even without enchantments.",
"if false, the item will not glint, even with enchantments.",
"If cleared, the glint enforcement will be cleared."
})
@Examples({
"set the enchantment glint of player's tool to true",
"set the enchantment glint of {_items::*} to false",
"clear the enchantment glint of player's tool"
})
@RequiredPlugins("Spigot 1.20.5+")
@Since("INSERT VERSION")
public class ExprEnchantmentGlint extends SimplePropertyExpression<ItemType, Boolean> {

static {
if (Skript.isRunningMinecraft(1, 20, 5))
register(ExprEnchantmentGlint.class, Boolean.class, "enchantment glint", "itemtypes");
}

@Override
@Nullable
public Boolean convert(ItemType item) {
ItemMeta meta = item.getItemMeta();
if (!meta.hasEnchantmentGlintOverride())
return null;
// Spigot claims this does not return null, hence we return null ourselves
return meta.getEnchantmentGlintOverride();
}

@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
switch (mode) {
case SET:
case DELETE:
case RESET:
return CollectionUtils.array(Boolean.class);
default:
return null;
}
}

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
switch (mode) {
case SET:
if (!(delta[0] instanceof Boolean))
return;
for (ItemType itemType : getExpr().getArray(event)) {
ItemMeta meta = itemType.getItemMeta();
meta.setEnchantmentGlintOverride((Boolean) delta[0]);
itemType.setItemMeta(meta);
}
break;
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
case DELETE:
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
case RESET:
for (ItemType itemType : getExpr().getArray(event)) {
ItemMeta meta = itemType.getItemMeta();
meta.setEnchantmentGlintOverride(null);
itemType.setItemMeta(meta);
}
}
}

@Override
public Class<? extends Boolean> getReturnType() {
return Boolean.class;
}

@Override
protected String getPropertyName() {
return "enchantment glint";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ch.njol.skript.expressions;

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.expressions.base.PropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.Nullable;

@Name("Item with Enchantment Glint")
@Description("Get an item with or without enchantment glint.")
@Examples({
"set {_item with glint} to diamond with enchantment glint",
"set {_item without glint} to diamond without enchantment glint"
})
@RequiredPlugins("Spigot 1.20.5+")
@Since("INSERT VERSION")
public class ExprItemWithEnchantmentGlint extends PropertyExpression<ItemType, ItemType> {

static {
if (Skript.isRunningMinecraft(1, 20, 5))
Skript.registerExpression(ExprItemWithEnchantmentGlint.class, ItemType.class, ExpressionType.PROPERTY, "%itemtypes% with[:out] [enchant[ment]] glint");
}
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved

private boolean glint;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setExpr((Expression<ItemType>) expressions[0]);
glint = !parseResult.hasTag("out");
return true;
}

@Override
protected ItemType[] get(Event event, ItemType[] source) {
return get(source.clone(), itemType -> {
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
ItemMeta meta = itemType.getItemMeta();
meta.setEnchantmentGlintOverride(glint);
itemType.setItemMeta(meta);
return itemType;
});
}

@Override
public Class<? extends ItemType> getReturnType() {
return ItemType.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return getExpr().toString(event, debug) + (glint ? " with" : " without") + " enchantment glint";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
test "item with enchantment glint" when running minecraft "1.20.5":

Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts about adding some test using the parse section testing most if not all patterns?

# with enchantment glint
loop 2 times:
if loop-number = 1:
set {_item} to diamond with enchantment glint
else:
set {_item} to diamond
make {_item} glint
assert {_item} is forced to glint with "Item is expected to be forced to glint"
assert {_item} has enchantment glint override with "Item is expected to have enchantment glint override set ##1"
assert {_item} does not have enchantment glint override to fail with "Item is expected to have enchantment glint override set ##2"
delete {_item}

# without enchantment glint
loop 2 times:
if loop-number = 1:
set {_item} to diamond without enchantment glint
else:
set {_item} to diamond
make {_item} to not glint
assert {_item} is forced to not glint with "Item is expected to be forced to not glint"
assert {_item} has enchantment glint override with "Item is expected to have enchantment glint override set ##3"
assert {_item} does not have enchantment glint override to fail with "Item is expected to have enchantment glint override set ##4"

# clear enchantment glint override
clear enchantment glint override of {_item}
assert {_item} is forced to glint to fail with "Item is expected to not have enchantment override set ##1"
assert {_item} is forced to not glint to fail with "Item is expected to not have enchantment override set ##2"
assert {_item} has enchantment glint override to fail with "Item is expected to not have enchantment override set ##3"
assert {_item} does not have enchantment glint override with "Item is expected to have enchantment glint override set ##4"

# edge cases
assert {_null} is forced to glint to fail with "Condition 'item is forced to glint' expected to fail with non itemtypes ##1"
assert {_null} has enchantment glint override to fail with "Condition 'item has enchantment glint override' expected to fail with non itemtypes ##1"

assert diamond with enchantment glint and stone with enchantment glint is forced to glint with "Both items expected to be forced to glint ##1"
assert diamond without enchantment glint and stone without enchantment glint is forced to not glint with "Both items expected to be forced to glint ##2"
assert diamond with enchantment glint and stone without enchantment glint have enchantment glint override with "Both items expected to have enchantment glint override set"

# Skript's long term negation related issue
# assert {_null} is forced to not glint to fail with "Condition 'item is forced to glint' expected to fail with non itemtypes ##2"
# assert {_null} does not have enchantment glint override to fail with "Condition 'item has enchantment glint override' expected to fail with non itemtypes ##2"