Search in sources :

Example 6 with Mechanism

use of com.denizenscript.denizencore.objects.Mechanism in project Denizen-For-Bukkit by DenizenScript.

the class ItemScriptContainer method getItemFrom.

public ItemTag getItemFrom(TagContext context) {
    if (isProcessing) {
        Debug.echoError("Item script contains (or chains to) a reference to itself. Cannot process.");
        return null;
    }
    if (context == null) {
        context = new BukkitTagContext(null, null, new ScriptTag(this));
    } else {
        context = new BukkitTagContext((BukkitTagContext) context);
        context.script = new ScriptTag(this);
    }
    // Try to use this script to make an item.
    ItemTag stack;
    isProcessing = true;
    try {
        if (!contains("material", String.class)) {
            Debug.echoError("Item script '" + getName() + "' does not contain a material. Script cannot function.");
            return null;
        }
        // Check validity of material
        String material = TagManager.tag(getString("material"), context);
        if (material.startsWith("m@")) {
            material = material.substring(2);
        }
        stack = ItemTag.valueOf(material, this);
        // Make sure we're working with a valid base ItemStack
        if (stack == null) {
            Debug.echoError("Item script '" + getName() + "' contains an invalid or incorrect material '" + material + "' (did you spell the material name wrong?). Script cannot function.");
            return null;
        }
        // Handle listed mechanisms
        if (contains("mechanisms", Map.class)) {
            YamlConfiguration mechs = getConfigurationSection("mechanisms");
            for (StringHolder key : mechs.getKeys(false)) {
                ObjectTag obj = CoreUtilities.objectToTagForm(mechs.get(key.low), context, true, true);
                stack.safeAdjust(new Mechanism(key.low, obj, context));
            }
        }
        // Set Display Name
        if (contains("display name", String.class)) {
            String displayName = TagManager.tag(getString("display name"), context);
            NMSHandler.getItemHelper().setDisplayName(stack, displayName);
        }
        // Set if the object is bound to the player
        if (contains("bound", String.class)) {
            Deprecations.boundWarning.warn(context);
        }
        // Set Lore
        if (contains("lore", List.class)) {
            List<String> lore = NMSHandler.getItemHelper().getLore(stack);
            if (lore == null) {
                lore = new ArrayList<>();
            }
            for (String line : getStringList("lore")) {
                line = TagManager.tag(line, context);
                lore.add(line);
            }
            CoreUtilities.fixNewLinesToListSeparation(lore);
            NMSHandler.getItemHelper().setLore(stack, lore);
        }
        // Set Durability
        if (contains("durability", String.class)) {
            short durability = Short.valueOf(getString("durability"));
            stack.setDurability(durability);
        }
        // Set Enchantments
        if (contains("enchantments", List.class)) {
            for (String enchantment : getStringList("enchantments")) {
                enchantment = TagManager.tag(enchantment, context);
                try {
                    // Build enchantment context
                    int level = 1;
                    int colon = enchantment.lastIndexOf(':');
                    if (colon == -1) {
                        Debug.echoError("Item script '" + getName() + "' has enchantment '" + enchantment + "' without a level.");
                    } else {
                        level = Integer.valueOf(enchantment.substring(colon + 1).replace(" ", ""));
                        enchantment = enchantment.substring(0, colon).replace(" ", "");
                    }
                    // Add enchantment
                    EnchantmentTag ench = EnchantmentTag.valueOf(enchantment, context);
                    if (ench == null) {
                        Debug.echoError("Item script '" + getName() + "' specifies enchantment '" + enchantment + "' which is invalid.");
                        continue;
                    }
                    if (stack.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                        EnchantmentStorageMeta meta = (EnchantmentStorageMeta) stack.getItemMeta();
                        meta.addStoredEnchant(ench.enchantment, level, true);
                        stack.setItemMeta(meta);
                    } else {
                        stack.getItemStack().addUnsafeEnchantment(ench.enchantment, level);
                        stack.resetCache();
                    }
                } catch (Exception ex) {
                    Debug.echoError("While constructing item script '" + getName() + "', encountered error while applying enchantment '" + enchantment + "':");
                    Debug.echoError(ex);
                }
            }
        }
        // Set Color
        if (contains("color", String.class)) {
            Deprecations.itemScriptColor.warn(context);
            String color = TagManager.tag(getString("color"), context);
            LeatherColorer.colorArmor(stack, color);
        }
        // Set Book
        if (contains("book", String.class)) {
            BookScriptContainer book = ScriptRegistry.getScriptContainer(TagManager.tag(getString("book"), context).replace("s@", ""));
            stack = book.writeBookTo(stack, context);
        }
        if (contains("flags", Map.class)) {
            YamlConfiguration flagSection = getConfigurationSection("flags");
            AbstractFlagTracker tracker = stack.getFlagTracker();
            for (StringHolder key : flagSection.getKeys(false)) {
                tracker.setFlag(key.str, CoreUtilities.objectToTagForm(flagSection.get(key.str), context, true, true), null);
            }
            stack.reapplyTracker(tracker);
        }
        stack.setItemScript(this);
    } catch (Exception e) {
        Debug.echoError("Woah! An exception has been called with this item script!");
        Debug.echoError(e);
        stack = null;
    } finally {
        isProcessing = false;
    }
    return stack;
}
Also used : YamlConfiguration(com.denizenscript.denizencore.utilities.YamlConfiguration) Mechanism(com.denizenscript.denizencore.objects.Mechanism) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) AbstractFlagTracker(com.denizenscript.denizencore.flags.AbstractFlagTracker) EnchantmentTag(com.denizenscript.denizen.objects.EnchantmentTag) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Example 7 with Mechanism

use of com.denizenscript.denizencore.objects.Mechanism in project Denizen-For-Bukkit by DenizenScript.

the class EntityScriptContainer method getEntityFrom.

public EntityTag getEntityFrom(PlayerTag player, NPCTag npc) {
    EntityTag entity;
    try {
        TagContext context = new BukkitTagContext(player, npc, new ScriptTag(this));
        if (contains("entity_type", String.class)) {
            String entityType = TagManager.tag((getString("entity_type", "")), context);
            entity = EntityTag.valueOf(entityType, context);
        } else {
            throw new Exception("Missing entity_type argument!");
        }
        if (contains("flags", Map.class)) {
            YamlConfiguration flagSection = getConfigurationSection("flags");
            MapTagFlagTracker tracker = new MapTagFlagTracker();
            for (StringHolder key : flagSection.getKeys(false)) {
                tracker.setFlag(key.str, CoreUtilities.objectToTagForm(flagSection.get(key.str), context, true, true), null);
            }
            entity.safeAdjust(new Mechanism("flag_map", tracker.map, context));
        }
        if (contains("mechanisms", Map.class)) {
            YamlConfiguration mechSection = getConfigurationSection("mechanisms");
            Set<StringHolder> strings = mechSection.getKeys(false);
            for (StringHolder string : strings) {
                ObjectTag obj = CoreUtilities.objectToTagForm(mechSection.get(string.low), context, true, true);
                entity.safeAdjust(new Mechanism(string.low, obj, context));
            }
        }
        boolean any = false;
        Set<StringHolder> strings = getContents().getKeys(false);
        for (StringHolder string : strings) {
            if (!nonMechanismKeys.contains(string.low)) {
                any = true;
                ObjectTag obj = CoreUtilities.objectToTagForm(getContents().get(string.low), context, true, true);
                entity.safeAdjust(new Mechanism(string.low, obj, context));
            }
        }
        if (any) {
            Deprecations.entityMechanismsFormat.warn(this);
        }
        if (entity == null || entity.isUnique()) {
            return null;
        }
        entity.setEntityScript(getName());
    } catch (Exception e) {
        Debug.echoError("Woah! An exception has been called with this entity script!");
        Debug.echoError(e);
        entity = null;
    }
    return entity;
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) TagContext(com.denizenscript.denizencore.tags.TagContext) MapTagFlagTracker(com.denizenscript.denizencore.flags.MapTagFlagTracker) YamlConfiguration(com.denizenscript.denizencore.utilities.YamlConfiguration) Mechanism(com.denizenscript.denizencore.objects.Mechanism) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) EntityTag(com.denizenscript.denizen.objects.EntityTag)

Example 8 with Mechanism

use of com.denizenscript.denizencore.objects.Mechanism in project Denizen-For-Bukkit by DenizenScript.

the class CuboidBlockSet method pasteEntities.

public void pasteEntities(LocationTag relative) {
    if (entities == null) {
        return;
    }
    for (MapTag data : entities.filter(MapTag.class, CoreUtilities.noDebugContext)) {
        try {
            LocationTag offset = data.getObject("offset").asType(LocationTag.class, CoreUtilities.noDebugContext);
            int rotation = data.getObject("rotation").asElement().asInt();
            EntityTag entity = data.getObject("entity").asType(EntityTag.class, CoreUtilities.noDebugContext);
            if (entity == null || offset == null) {
                continue;
            }
            entity = entity.duplicate();
            offset = offset.clone();
            if (rotation != 0) {
                ArrayList<Mechanism> mechs = new ArrayList<>(entity.getWaitingMechanisms().size());
                for (Mechanism mech : entity.getWaitingMechanisms()) {
                    if (mech.getName().equals("rotation")) {
                        String rotationName = mech.getValue().asString();
                        BlockFace face = BlockFace.valueOf(rotationName.toUpperCase());
                        for (int i = 0; i < rotation; i += 90) {
                            face = rotateFaceOne(face);
                        }
                        // Compensate for hanging locations being very stupid
                        offset.add(face.getDirection().multiply(0.1));
                        mechs.add(new Mechanism("rotation", new ElementTag(face.name()), CoreUtilities.noDebugContext));
                    } else {
                        mechs.add(new Mechanism(mech.getName(), mech.value, CoreUtilities.noDebugContext));
                    }
                }
                entity.mechanisms = mechs;
            } else {
                for (Mechanism mechanism : entity.mechanisms) {
                    mechanism.context = CoreUtilities.noDebugContext;
                }
            }
            Location spawnLoc = relative.clone().add(offset);
            spawnLoc.setYaw(offset.getYaw() - rotation);
            spawnLoc.setPitch(offset.getPitch());
            entity.spawnAt(spawnLoc);
        } catch (Exception ex) {
            Debug.echoError(ex);
        }
    }
}
Also used : BlockFace(org.bukkit.block.BlockFace) ArrayList(java.util.ArrayList) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) Mechanism(com.denizenscript.denizencore.objects.Mechanism) Location(org.bukkit.Location)

Example 9 with Mechanism

use of com.denizenscript.denizencore.objects.Mechanism in project Denizen-For-Bukkit by DenizenScript.

the class DenizenEntityType method spawnNewEntity.

public Entity spawnNewEntity(Location location, ArrayList<Mechanism> mechanisms, String scriptName) {
    try {
        if (name.equals("DROPPED_ITEM")) {
            ItemStack itemStack = new ItemStack(Material.STONE);
            for (Mechanism mechanism : mechanisms) {
                if (mechanism.matches("item") && mechanism.requireObject(ItemTag.class)) {
                    itemStack = mechanism.valueAsType(ItemTag.class).getItemStack();
                    break;
                }
            }
            return location.getWorld().dropItem(location, itemStack);
        } else if (!isCustom()) {
            return SpawnEntityHelper.spawn(location, bukkitEntityType, mechanisms, scriptName);
        } else {
            CustomEntityHelper customEntityHelper = NMSHandler.getCustomEntityHelper();
            switch(customEntityType) {
                case FAKE_ARROW:
                    return customEntityHelper.spawnFakeArrow(location);
                case FAKE_PLAYER:
                    if (Settings.packetInterception()) {
                        String name = null;
                        String skin = null;
                        for (Mechanism mechanism : new ArrayList<>(mechanisms)) {
                            if (mechanism.matches("name")) {
                                name = mechanism.getValue().asString();
                                mechanisms.remove(mechanism);
                            } else if (mechanism.matches("skin")) {
                                skin = mechanism.getValue().asString();
                                mechanisms.remove(mechanism);
                            }
                            if (name != null && skin != null) {
                                break;
                            }
                        }
                        NetworkInterceptHelper.enable();
                        return customEntityHelper.spawnFakePlayer(location, name, skin);
                    }
                    break;
                case ITEM_PROJECTILE:
                    Deprecations.itemProjectile.warn();
                    ItemStack itemStack = new ItemStack(Material.STONE);
                    for (Mechanism mechanism : mechanisms) {
                        if (mechanism.matches("item") && mechanism.requireObject(ItemTag.class)) {
                            itemStack = mechanism.valueAsType(ItemTag.class).getItemStack();
                        }
                    }
                    return customEntityHelper.spawnItemProjectile(location, itemStack);
            }
        }
    } catch (Exception e) {
        Debug.echoError(e);
    }
    return null;
}
Also used : CustomEntityHelper(com.denizenscript.denizen.nms.interfaces.CustomEntityHelper) ItemStack(org.bukkit.inventory.ItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag) Mechanism(com.denizenscript.denizencore.objects.Mechanism)

Aggregations

Mechanism (com.denizenscript.denizencore.objects.Mechanism)9 EntityTag (com.denizenscript.denizen.objects.EntityTag)5 ItemTag (com.denizenscript.denizen.objects.ItemTag)5 ItemStack (org.bukkit.inventory.ItemStack)4 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)3 FakeEntity (com.denizenscript.denizen.utilities.entity.FakeEntity)3 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)3 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)3 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)2 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)2 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)2 YamlConfiguration (com.denizenscript.denizencore.utilities.YamlConfiguration)2 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)2 ServerEntity (net.minecraft.server.level.ServerEntity)2 ServerPlayer (net.minecraft.server.level.ServerPlayer)2 ServerGamePacketListenerImpl (net.minecraft.server.network.ServerGamePacketListenerImpl)2 CraftWorld (org.bukkit.craftbukkit.v1_18_R1.CraftWorld)2 CustomEntityHelper (com.denizenscript.denizen.nms.interfaces.CustomEntityHelper)1 CraftFakePlayerImpl (com.denizenscript.denizen.nms.v1_16.impl.entities.CraftFakePlayerImpl)1 EntityItemProjectileImpl (com.denizenscript.denizen.nms.v1_16.impl.entities.EntityItemProjectileImpl)1