-
-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Enchantment Glint Syntaxes (#6638)
* Add enchantment glint override * Add Skript docs and fix annotations * Add expr item with glint * Add condition item has enchantment glint * Add version check * Add tests * Correction to syntax patterns * License says hi * Expression said bye * Fix setExpr * Finally * Fix tests * Bulk requested changes * Implementation overhaul * Implementation overhaul * Implementation overhaul * Fix test * Fix test * Fix test * Fix test * Fix test * Remove license header (#6684) * Requested changes and optimise imports * Requested changes, reworked condition, and optimized for latest code convention * Fix and test revamp * Reorganize test * Indentation! * Test… --------- Co-authored-by: Moderocky <admin@moderocky.com>
- Loading branch information
1 parent
b85b254
commit 192c134
Showing
4 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/java/ch/njol/skript/conditions/CondItemEnchantmentGlint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package ch.njol.skript.conditions; | ||
|
||
import org.bukkit.inventory.meta.ItemMeta; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.aliases.ItemType; | ||
import ch.njol.skript.conditions.base.PropertyCondition; | ||
import ch.njol.skript.doc.*; | ||
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 PropertyCondition<ItemType> { | ||
|
||
static { | ||
if (Skript.methodExists(ItemMeta.class, "getEnchantmentGlintOverride")) { | ||
register(CondItemEnchantmentGlint.class, PropertyType.HAVE, "enchantment glint overrid(den|e)", "itemtypes"); | ||
register(CondItemEnchantmentGlint.class, PropertyType.BE, "forced to [:not] glint", "itemtypes"); | ||
} | ||
} | ||
|
||
private int matchedPattern; | ||
|
||
@Override | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
this.matchedPattern = matchedPattern; | ||
return super.init(expressions, matchedPattern, isDelayed, parseResult); | ||
} | ||
|
||
@Override | ||
public boolean check(ItemType itemType) { | ||
ItemMeta meta = itemType.getItemMeta(); | ||
// enchantment glint override | ||
if (matchedPattern == 0) | ||
return meta.hasEnchantmentGlintOverride(); | ||
// forced to glint | ||
if (!meta.hasEnchantmentGlintOverride()) | ||
return false; | ||
return meta.getEnchantmentGlintOverride(); | ||
} | ||
|
||
@Override | ||
protected String getPropertyName() { | ||
if (matchedPattern == 0) | ||
return "enchantment glint overridden"; | ||
return "forced to " + (isNegated() ? "not " : "") + "glint"; | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/ch/njol/skript/effects/EffForceEnchantmentGlint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ch.njol.skript.effects; | ||
|
||
import org.bukkit.event.Event; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.aliases.ItemType; | ||
import ch.njol.skript.doc.*; | ||
import ch.njol.skript.lang.Effect; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.util.Kleenean; | ||
|
||
@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.methodExists(ItemMeta.class, "setEnchantmentGlintOverride", Boolean.class)) | ||
Skript.registerEffect(EffForceEnchantmentGlint.class, | ||
"(force|make) %itemtypes% [to] [start] glint[ing]", | ||
"(force|make) %itemtypes% [to] (not|stop) glint[ing]", | ||
"(clear|delete|reset) [the] enchantment glint override of %itemtypes%", | ||
"(clear|delete|reset) %itemtypes%'s enchantment glint override"); | ||
} | ||
|
||
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; | ||
if (pattern == 0) { | ||
// Pattern: forced to glint | ||
glint = true; | ||
} else if (pattern == 1) { | ||
// Pattern: forced to not glint | ||
glint = false; | ||
} else { | ||
// Pattern: Clear glint override | ||
glint = null; | ||
} | ||
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 " + itemTypes.toString(event, debug) + " to " + (pattern == 0 ? "start" : "stop") + " glinting"; | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/ch/njol/skript/expressions/ExprItemWithEnchantmentGlint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
import org.bukkit.event.Event; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.aliases.ItemType; | ||
import ch.njol.skript.doc.*; | ||
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; | ||
|
||
@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.methodExists(ItemMeta.class, "getEnchantmentGlintOverride")) | ||
Skript.registerExpression(ExprItemWithEnchantmentGlint.class, ItemType.class, ExpressionType.PROPERTY, "%itemtypes% with[:out] [enchant[ment]] glint"); | ||
} | ||
|
||
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, itemType -> { | ||
itemType = itemType.clone(); | ||
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"; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/test/skript/tests/syntaxes/expressions/ExprItemWithEnchantmentGlint.sk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
test "item enchantment glint" when running minecraft "1.20.5": | ||
# Glint | ||
set {_item} to diamond with enchantment glint | ||
assert {_item} has enchantment glint override with "Item expected to have enchantment glint override #1" | ||
assert {_item} is forced to glint with "Item expected to be forced to glint #1" | ||
delete {_item} | ||
|
||
set {_item} to diamond | ||
make {_item} glint | ||
assert {_item} has enchantment glint override with "Item expected to have enchantment glint override #2" | ||
assert {_item} is forced to glint with "Item expected to be forced to glint #2" | ||
delete {_item} | ||
|
||
set {_item} to diamond without enchantment glint | ||
assert {_item} has enchantment glint override with "Item expected to have enchantment glint override #3" | ||
assert {_item} is forced to not glint with "Item expected to be forced to not glint #1" | ||
delete {_item} | ||
|
||
# Not Glint | ||
set {_item} to diamond | ||
make {_item} not glint | ||
assert {_item} has enchantment glint override with "Item expected to have enchantment glint override #4" | ||
assert {_item} is forced to not glint with "Item expected to be forced to not glint #2" | ||
delete {_item} | ||
|
||
# Without Glint Enforcement | ||
set {_item} to diamond | ||
assert {_item} does not have enchantment glint override with "Item expected to not have enchantment glint override #1" | ||
|
||
set {_item} to {_item} with enchantment glint | ||
clear enchantment glint override of {_item} | ||
assert {_item} does not have enchantment glint override with "Item expected to not have enchantment glint override #2" | ||
|
||
test "item enchantment glint - edge cases" when running minecraft "1.20.5": | ||
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 any wool does not have enchantment glint override with "Condition 'does not have enchantment glint override' failed with plural itemtype aliases" | ||
assert diamond with enchantment glint = diamond with enchantment glint with "Item with enchantment glint comparison failed #1" | ||
assert diamond with enchantment glint != diamond without enchantment glint with "Item with enchantment glint comparison failed #2" | ||
assert diamond != diamond with enchantment glint with "Item with enchantment glint comparison failed #3" | ||
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" |