Search in sources :

Example 16 with NamespacedKey

use of org.bukkit.NamespacedKey in project Denizen-For-Bukkit by DenizenScript.

the class ItemKnowledgeBookRecipes method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("knowledge_book_recipes")) {
        KnowledgeBookMeta meta = (KnowledgeBookMeta) item.getItemMeta();
        List<NamespacedKey> recipes = new ArrayList<>();
        ListTag newRecipes = mechanism.valueAsType(ListTag.class);
        for (String str : newRecipes) {
            recipes.add(Utilities.parseNamespacedKey(str));
        }
        meta.setRecipes(recipes);
        item.setItemMeta(meta);
    }
}
Also used : KnowledgeBookMeta(org.bukkit.inventory.meta.KnowledgeBookMeta) NamespacedKey(org.bukkit.NamespacedKey) ArrayList(java.util.ArrayList) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 17 with NamespacedKey

use of org.bukkit.NamespacedKey in project Denizen-For-Bukkit by DenizenScript.

the class EnchantmentHelperImpl method registerFakeEnchantment.

@Override
public org.bukkit.enchantments.Enchantment registerFakeEnchantment(EnchantmentScriptContainer.EnchantmentReference script) {
    try {
        EquipmentSlot[] slots = new EquipmentSlot[script.script.slots.size()];
        for (int i = 0; i < slots.length; i++) {
            slots[i] = EquipmentSlot.valueOf(script.script.slots.get(i).toUpperCase());
        }
        net.minecraft.world.item.enchantment.Enchantment nmsEnchant = new net.minecraft.world.item.enchantment.Enchantment(net.minecraft.world.item.enchantment.Enchantment.Rarity.valueOf(script.script.rarity), EnchantmentCategory.valueOf(script.script.category), slots) {

            @Override
            public int getMinLevel() {
                return script.script.minLevel;
            }

            @Override
            public int getMaxLevel() {
                return script.script.maxLevel;
            }

            @Override
            public int getMinCost(int level) {
                return script.script.getMinCost(level);
            }

            @Override
            public int getMaxCost(int level) {
                return script.script.getMaxCost(level);
            }

            @Override
            public int getDamageProtection(int level, DamageSource src) {
                return script.script.getDamageProtection(level, src.msgId, src.getEntity() == null ? null : src.getEntity().getBukkitEntity());
            }

            @Override
            public float getDamageBonus(int level, MobType type) {
                String typeName = "UNDEFINED";
                if (type == MobType.ARTHROPOD) {
                    typeName = "ARTHROPOD";
                } else if (type == MobType.ILLAGER) {
                    typeName = "ILLAGER";
                } else if (type == MobType.UNDEAD) {
                    typeName = "UNDEAD";
                } else if (type == MobType.WATER) {
                    typeName = "WATER";
                }
                return script.script.getDamageBonus(level, typeName);
            }

            @Override
            protected boolean checkCompatibility(net.minecraft.world.item.enchantment.Enchantment nmsEnchantment) {
                ResourceLocation nmsKey = Registry.ENCHANTMENT.getKey(nmsEnchantment);
                NamespacedKey bukkitKey = CraftNamespacedKey.fromMinecraft(nmsKey);
                org.bukkit.enchantments.Enchantment bukkitEnchant = CraftEnchantment.getByKey(bukkitKey);
                return script.script.isCompatible(bukkitEnchant);
            }

            @Override
            protected String getOrCreateDescriptionId() {
                return script.script.descriptionId;
            }

            @Override
            public String getDescriptionId() {
                return script.script.descriptionId;
            }

            @Override
            public Component getFullname(int level) {
                return Handler.componentToNMS(script.script.getFullName(level));
            }

            @Override
            public boolean canEnchant(net.minecraft.world.item.ItemStack var0) {
                return super.canEnchant(var0) && script.script.canEnchant(CraftItemStack.asBukkitCopy(var0));
            }

            @Override
            public void doPostAttack(LivingEntity attacker, Entity victim, int level) {
                script.script.doPostAttack(attacker.getBukkitEntity(), victim.getBukkitEntity(), level);
            }

            @Override
            public void doPostHurt(LivingEntity victim, Entity attacker, int level) {
                script.script.doPostHurt(victim.getBukkitEntity(), attacker.getBukkitEntity(), level);
            }

            @Override
            public boolean isTreasureOnly() {
                return script.script.isTreasureOnly;
            }

            @Override
            public boolean isCurse() {
                return script.script.isCurse;
            }

            @Override
            public boolean isTradeable() {
                return script.script.isTradable;
            }

            @Override
            public boolean isDiscoverable() {
                return script.script.isDiscoverable;
            }
        };
        String enchName = script.script.id.toUpperCase();
        Registry.register(Registry.ENCHANTMENT, "denizen:" + script.script.id, nmsEnchant);
        CraftEnchantment ench = new CraftEnchantment(nmsEnchant) {

            @Override
            public String getName() {
                return enchName;
            }
        };
        ENCHANTMENTS_BY_KEY.put(ench.getKey(), ench);
        ENCHANTMENTS_BY_NAME.put(enchName, ench);
        return ench;
    } catch (Throwable ex) {
        Debug.echoError("Failed to register enchantment " + script.script.id);
        Debug.echoError(ex);
        return null;
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) Entity(net.minecraft.world.entity.Entity) DamageSource(net.minecraft.world.damagesource.DamageSource) Enchantment(org.bukkit.enchantments.Enchantment) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) MobType(net.minecraft.world.entity.MobType) LivingEntity(net.minecraft.world.entity.LivingEntity) NamespacedKey(org.bukkit.NamespacedKey) CraftNamespacedKey(org.bukkit.craftbukkit.v1_17_R1.util.CraftNamespacedKey) ResourceLocation(net.minecraft.resources.ResourceLocation) Enchantment(org.bukkit.enchantments.Enchantment) CraftEnchantment(org.bukkit.craftbukkit.v1_17_R1.enchantments.CraftEnchantment) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) CraftEnchantment(org.bukkit.craftbukkit.v1_17_R1.enchantments.CraftEnchantment)

Example 18 with NamespacedKey

use of org.bukkit.NamespacedKey in project Denizen-For-Bukkit by DenizenScript.

the class EnchantmentTag method valueOf.

@Fetchable("enchantment")
public static EnchantmentTag valueOf(String string, TagContext context) {
    if (string == null) {
        return null;
    }
    string = CoreUtilities.toLowerCase(string);
    if (string.startsWith("enchantment@")) {
        string = string.substring("enchantment@".length());
    }
    Enchantment ench;
    NamespacedKey key = Utilities.parseNamespacedKey(string);
    ench = Enchantment.getByKey(key);
    if (ench == null) {
        ench = Enchantment.getByName(string.toUpperCase());
    }
    if (ench == null) {
        ench = Enchantment.getByKey(new NamespacedKey("denizen", Utilities.cleanseNamespaceID(string)));
    }
    if (ench == null && ScriptRegistry.containsScript(string, EnchantmentScriptContainer.class)) {
        ench = ScriptRegistry.getScriptContainerAs(string, EnchantmentScriptContainer.class).enchantment;
    }
    if (ench == null) {
        if (context == null || context.debug) {
            Debug.echoError("Unknown enchantment '" + string + "'");
        }
        return null;
    }
    return new EnchantmentTag(ench);
}
Also used : NamespacedKey(org.bukkit.NamespacedKey) EnchantmentScriptContainer(com.denizenscript.denizen.scripts.containers.core.EnchantmentScriptContainer) Enchantment(org.bukkit.enchantments.Enchantment) Fetchable(com.denizenscript.denizencore.objects.Fetchable)

Example 19 with NamespacedKey

use of org.bukkit.NamespacedKey in project Denizen-For-Bukkit by DenizenScript.

the class AdvancementCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag id = scriptEntry.getElement("id");
    ElementTag parent = scriptEntry.getElement("parent");
    ElementTag delete = scriptEntry.getElement("delete");
    ListTag grant = scriptEntry.getObjectTag("grant");
    ListTag revoke = scriptEntry.getObjectTag("revoke");
    ItemTag icon = scriptEntry.getObjectTag("icon");
    ElementTag title = scriptEntry.getElement("title");
    ElementTag description = scriptEntry.getElement("description");
    ElementTag background = scriptEntry.getElement("background");
    ElementTag frame = scriptEntry.getElement("frame");
    ElementTag toast = scriptEntry.getElement("toast");
    ElementTag announce = scriptEntry.getElement("announce");
    ElementTag hidden = scriptEntry.getElement("hidden");
    ElementTag x = scriptEntry.getElement("x");
    ElementTag y = scriptEntry.getElement("y");
    ElementTag progressLength = scriptEntry.getElement("progress_length");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, name, id, parent, delete, grant, revoke, icon, title, description, background, progressLength, frame, toast, announce, hidden, x, y);
    }
    final AdvancementHelper advancementHelper = NMSHandler.getAdvancementHelper();
    NamespacedKey key = new NamespacedKey(Denizen.getInstance(), id.asString());
    if (delete == null && grant == null && revoke == null) {
        NamespacedKey parentKey = null;
        NamespacedKey backgroundKey = null;
        if (parent != null) {
            List<String> split = CoreUtilities.split(parent.asString(), ':', 2);
            if (split.size() == 1) {
                parentKey = new NamespacedKey(Denizen.getInstance(), split.get(0));
            } else {
                parentKey = new NamespacedKey(CoreUtilities.toLowerCase(split.get(0)), CoreUtilities.toLowerCase(split.get(1)));
            }
        } else if (background != null) {
            List<String> backgroundSplit = CoreUtilities.split(background.asString(), ':', 2);
            if (backgroundSplit.size() == 1) {
                backgroundKey = NamespacedKey.minecraft(backgroundSplit.get(0));
            } else {
                backgroundKey = new NamespacedKey(CoreUtilities.toLowerCase(backgroundSplit.get(0)), CoreUtilities.toLowerCase(backgroundSplit.get(1)));
            }
        }
        final Advancement advancement = new Advancement(false, key, parentKey, icon.getItemStack(), title.asString(), description.asString(), backgroundKey, Advancement.Frame.valueOf(frame.asString().toUpperCase()), toast.asBoolean(), announce.asBoolean(), hidden.asBoolean(), x.asFloat(), y.asFloat(), progressLength == null ? 1 : progressLength.asInt());
        advancementHelper.register(advancement);
        customRegistered.put(key, advancement);
        return;
    }
    Advancement advancement = customRegistered.get(key);
    if (advancement == null) {
        Debug.echoError("Invalid advancement key '" + key + "': not registered. Are you sure you registered it using the 'advancement' command previously?" + " Note that the 'advancement' command is not for vanilla advancements.");
        return;
    }
    if (delete != null) {
        advancementHelper.unregister(advancement);
        customRegistered.remove(key);
    } else if (grant != null) {
        for (PlayerTag target : grant.filter(PlayerTag.class, scriptEntry)) {
            Player player = target.getPlayerEntity();
            if (player != null) {
                if (progressLength == null) {
                    advancementHelper.grant(advancement, player);
                } else {
                    advancementHelper.grantPartial(advancement, player, progressLength.asInt());
                }
            }
        }
    } else /*if (revoke != null)*/
    {
        for (PlayerTag target : revoke.filter(PlayerTag.class, scriptEntry)) {
            Player player = target.getPlayerEntity();
            if (player != null) {
                advancementHelper.revoke(advancement, player);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) AdvancementHelper(com.denizenscript.denizen.nms.interfaces.AdvancementHelper) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) NamespacedKey(org.bukkit.NamespacedKey) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Advancement(com.denizenscript.denizen.nms.util.Advancement)

Example 20 with NamespacedKey

use of org.bukkit.NamespacedKey in project Denizen-For-Bukkit by DenizenScript.

the class LocationFlagSearchHelper method getFlaggedLocations.

public static void getFlaggedLocations(Chunk chunk, String flagName, Consumer<Location> handleLocation) {
    int subKeyIndex = flagName.indexOf('.');
    String fullPath = null;
    if (subKeyIndex != -1) {
        fullPath = flagName;
        flagName = flagName.substring(0, subKeyIndex);
    }
    PersistentDataContainer container = chunk.getPersistentDataContainer();
    Location ref = new Location(chunk.getWorld(), 0, 0, 0);
    for (NamespacedKey key : container.getKeys()) {
        if (key.getNamespace().equals("denizen") && key.getKey().startsWith("flag_tracker_") && key.getKey().endsWith(flagName)) {
            List<String> split = CoreUtilities.split(key.getKey(), '_', 6);
            if (split.size() == 6 && split.get(5).equals(flagName)) {
                ref.setX(Integer.parseInt(split.get(2)));
                ref.setY(Integer.parseInt(split.get(3)));
                ref.setZ(Integer.parseInt(split.get(4)));
                if (fullPath == null || new LocationTag(ref).getFlagTracker().hasFlag(fullPath)) {
                    handleLocation.accept(ref);
                }
            }
        }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) NamespacedKey(org.bukkit.NamespacedKey) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer) Location(org.bukkit.Location)

Aggregations

NamespacedKey (org.bukkit.NamespacedKey)24 Enchantment (org.bukkit.enchantments.Enchantment)4 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)3 ListTag (com.denizenscript.denizencore.objects.core.ListTag)3 ItemStack (org.bukkit.inventory.ItemStack)3 Test (org.junit.jupiter.api.Test)3 AdvancementHelper (com.denizenscript.denizen.nms.interfaces.AdvancementHelper)2 Advancement (com.denizenscript.denizen.nms.util.Advancement)2 ItemTag (com.denizenscript.denizen.objects.ItemTag)2 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)2 List (java.util.List)2 ResourceLocation (net.minecraft.resources.ResourceLocation)2 DamageSource (net.minecraft.world.damagesource.DamageSource)2 Entity (net.minecraft.world.entity.Entity)2 EquipmentSlot (net.minecraft.world.entity.EquipmentSlot)2 LivingEntity (net.minecraft.world.entity.LivingEntity)2 MobType (net.minecraft.world.entity.MobType)2 Location (org.bukkit.Location)2 KeyedBossBar (org.bukkit.boss.KeyedBossBar)2 Player (org.bukkit.entity.Player)2