Skip to content

Commit

Permalink
meta docs and enchantment switches
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 24, 2021
1 parent ad543f0 commit 3d2e62a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
Expand Up @@ -19,6 +19,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.*;
import org.bukkit.event.*;
import org.bukkit.event.inventory.InventoryType;
Expand Down Expand Up @@ -51,7 +52,7 @@ public abstract class BukkitScriptEvent extends ScriptEvent {
// You can use any valid item material type like "stick", or the name of an item script, or "item" as a catch-all, or "potion" for any potion item.
// Items can also be used with an "item_flagged" secondary prefix, so for an event that has "with:<item>", you can also do "with:item_flagged:<flag name>".
// For item matchers that aren't switches, this works similarly, like "on player consumes item_flagged:myflag:" (note that this is not a switch).
// You can also use "vanilla_tagged:<vanilla_tag_name>".
// You can also use "vanilla_tagged:<vanilla_tag_name>" to check vanilla tags, or "item_enchanted:<enchantment>" to check enchantments.
// You can also use "raw_exact:<item>" to do an exact raw item data comparison (almost always a bad idea to use).
//
// "<entity>", "<projectile>", "<vehicle>", etc. are examples of where an EntityTag will be expected.
Expand Down Expand Up @@ -927,6 +928,18 @@ public static boolean tryItem(ItemTag item, String comparedto) {
}
return true;
}
if (comparedto.startsWith("item_enchanted:")) {
String enchMatcher = comparedto.substring("item_enchanted:".length());
if (!item.getItemMeta().hasEnchants()) {
return false;
}
for (Enchantment enchant : item.getItemMeta().getEnchants().keySet()) {
if (runGenericCheck(enchMatcher, enchant.getKey().getKey())) {
return true;
}
}
return false;
}
if (comparedto.startsWith("raw_exact:")) {
ItemTag compareItem = ItemTag.valueOf(rawComparedTo.substring("raw_exact:".length()), CoreUtilities.errorButNoDebugContext);
return compareItem != null && compareItem.matchesRawExact(item);
Expand Down
Expand Up @@ -35,6 +35,8 @@ public class ItemEnchantedScriptEvent extends BukkitScriptEvent implements Liste
//
// @Cancellable true
//
// @Switch enchant:<name> to only process the event if any of the enchantments being added match the given name.
//
// @Triggers when an item is enchanted.
//
// @Context
Expand Down Expand Up @@ -82,15 +84,24 @@ public boolean couldMatch(ScriptPath path) {
@Override
public boolean matches(ScriptPath path) {
String itemTest = path.eventArgLowerAt(0);

if (!itemTest.equals("item") && !tryItem(item, itemTest)) {
return false;
}

if (!runInCheck(path, location)) {
return false;
}

if (path.switches.containsKey("enchant")) {
boolean any = false;
for (Enchantment enchant : event.getEnchantsToAdd().keySet()) {
any = runGenericSwitchCheck(path, "enchant", enchant.getKey().getKey());
if (any) {
break;
}
}
if (!any) {
return false;
}
}
return super.matches(path);
}

Expand Down
Expand Up @@ -14,6 +14,7 @@
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;

Expand Down Expand Up @@ -48,7 +49,11 @@ private ItemEnchantments(ItemTag _item) {
}

public static String getName(Enchantment enchantment) {
return enchantment.getKey().getKey();
NamespacedKey key = enchantment.getKey();
if (key.getNamespace().equals("minecraft")) {
return key.getKey();
}
return key.toString();
}

ItemTag item;
Expand Down
Expand Up @@ -31,6 +31,7 @@ public class EnchantmentScriptContainer extends ScriptContainer {
// @description
// Enchantment script containers allow you to register custom item enchantments.
// For the most part, they work similarly to vanilla enchantments, albeit with some limitations.
// These can be attached to enchanted books and used in anvils, and can be generated by the enchanting table (requires discoverable: true and treasure_only: false).
//
// In current implementation, custom enchantments do not appear in lore on their own, and will need fake lore added in their place. This may be fixed in the future.
//
Expand Down Expand Up @@ -60,7 +61,7 @@ public class EnchantmentScriptContainer extends ScriptContainer {
// # | Most enchantment scripts should have this key!
// rarity: common
//
// # The rarity level of this enchantment. Can be any of: ARMOR, ARMOR_FEET, ARMOR_LEGS, ARMOR_CHEST, ARMOR_HEAD, WEAPON, DIGGER, FISHING_ROD, TRIDENT, BREAKABLE, BOW, WEARABLE, CROSSBOW, VANISHABLE
// # The category/type of this enchantment. Can be any of: ARMOR, ARMOR_FEET, ARMOR_LEGS, ARMOR_CHEST, ARMOR_HEAD, WEAPON, DIGGER, FISHING_ROD, TRIDENT, BREAKABLE, BOW, WEARABLE, CROSSBOW, VANISHABLE
// # If unspecified, will use WEAPON.
// # | Most enchantment scripts should have this key!
// category: weapon
Expand Down Expand Up @@ -90,7 +91,7 @@ public class EnchantmentScriptContainer extends ScriptContainer {
// # | Most enchantment scripts should have this key!
// max_cost: <context.level.mul[1]>
//
// # Whether this enchantment is only considered to be treasure. (TODO: What difference does this make?)
// # Whether this enchantment is only considered to be treasure. Treasure enchantments do not show in the enchanting table.
// # If unspecified, will be false.
// # | Most enchantment scripts can exclude this key.
// treasure_only: false
Expand All @@ -105,7 +106,7 @@ public class EnchantmentScriptContainer extends ScriptContainer {
// # | Most enchantment scripts can exclude this key.
// is_tradable: true
//
// # Whether this enchantment is only considered to be discoverable. (TODO: What difference does this make?)
// # Whether this enchantment is only considered to be discoverable. If true, this will spawn from vanilla sources like the enchanting table. If false, it can only be given directly by script.
// # If unspecified, will be true.
// # | Most enchantment scripts can exclude this key.
// is_discoverable: true
Expand Down

0 comments on commit 3d2e62a

Please sign in to comment.