Search in sources :

Example 1 with Particle

use of com.denizenscript.denizen.nms.interfaces.Particle in project Denizen-For-Bukkit by DenizenScript.

the class PlayEffectCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("location");
    List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
    Effect effect = (Effect) scriptEntry.getObject("effect");
    Particle particleEffect = (Particle) scriptEntry.getObject("particleeffect");
    ItemTag iconcrack = scriptEntry.getObjectTag("iconcrack");
    ElementTag radius = scriptEntry.getElement("radius");
    ElementTag data = scriptEntry.getElement("data");
    ElementTag quantity = scriptEntry.getElement("quantity");
    ElementTag no_offset = scriptEntry.getElement("no_offset");
    boolean should_offset = no_offset == null || !no_offset.asBoolean();
    LocationTag offset = scriptEntry.getObjectTag("offset");
    ElementTag special_data = scriptEntry.getElement("special_data");
    LocationTag velocity = scriptEntry.getObjectTag("velocity");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (effect != null ? db("effect", effect.name()) : particleEffect != null ? db("special effect", particleEffect.getName()) : iconcrack), db("locations", locations), db("targets", targets), radius, data, quantity, offset, special_data, velocity, (should_offset ? db("note", "Location will be offset 1 block-height upward (see documentation)") : ""));
    }
    for (LocationTag location : locations) {
        if (should_offset) {
            // Slightly increase the location's Y so effects don't seem to come out of the ground
            location = new LocationTag(location.clone().add(0, 1, 0));
        }
        // Play the Bukkit effect the number of times specified
        if (effect != null) {
            for (int n = 0; n < quantity.asInt(); n++) {
                if (targets != null) {
                    for (PlayerTag player : targets) {
                        if (player.isValid() && player.isOnline()) {
                            // TODO: 1.13
                            player.getPlayerEntity().playEffect(location, effect, data.asInt());
                        }
                    }
                } else {
                    location.getWorld().playEffect(location, effect, data.asInt(), radius.asInt());
                }
            }
        } else // Play a ParticleEffect
        if (particleEffect != null) {
            List<Player> players = new ArrayList<>();
            if (targets == null) {
                float rad = radius.asFloat();
                for (Player player : location.getWorld().getPlayers()) {
                    if (player.getLocation().distanceSquared(location) < rad * rad) {
                        players.add(player);
                    }
                }
            } else {
                for (PlayerTag player : targets) {
                    if (player.isValid() && player.isOnline()) {
                        players.add(player.getPlayerEntity());
                    }
                }
            }
            Class clazz = particleEffect.neededData();
            Object dataObject = null;
            if (clazz != null) {
                if (special_data == null) {
                    Debug.echoError("Missing required special data for particle: " + particleEffect.getName());
                    return;
                } else if (clazz == org.bukkit.Particle.DustOptions.class) {
                    ListTag dataList = ListTag.valueOf(special_data.asString(), scriptEntry.getContext());
                    if (dataList.size() != 2) {
                        Debug.echoError("DustOptions special_data must have 2 list entries for particle: " + particleEffect.getName());
                        return;
                    } else {
                        float size = Float.parseFloat(dataList.get(0));
                        ColorTag color = ColorTag.valueOf(dataList.get(1), scriptEntry.context);
                        dataObject = new org.bukkit.Particle.DustOptions(color.getColor(), size);
                    }
                } else if (clazz == BlockData.class) {
                    MaterialTag blockMaterial = MaterialTag.valueOf(special_data.asString(), scriptEntry.getContext());
                    dataObject = blockMaterial.getModernData();
                } else if (clazz == ItemStack.class) {
                    ItemTag itemType = ItemTag.valueOf(special_data.asString(), scriptEntry.getContext());
                    dataObject = itemType.getItemStack();
                } else // Intentionally list last due to requiring 1.17+
                if (clazz == org.bukkit.Particle.DustTransition.class) {
                    ListTag dataList = ListTag.valueOf(special_data.asString(), scriptEntry.getContext());
                    if (dataList.size() != 3) {
                        Debug.echoError("DustTransition special_data must have 3 list entries for particle: " + particleEffect.getName());
                        return;
                    } else {
                        float size = Float.parseFloat(dataList.get(0));
                        ColorTag fromColor = ColorTag.valueOf(dataList.get(1), scriptEntry.context);
                        ColorTag toColor = ColorTag.valueOf(dataList.get(2), scriptEntry.context);
                        dataObject = new org.bukkit.Particle.DustTransition(fromColor.getColor(), toColor.getColor(), size);
                    }
                } else {
                    Debug.echoError("Unknown particle data type: " + clazz.getCanonicalName() + " for particle: " + particleEffect.getName());
                    return;
                }
            } else if (special_data != null) {
                Debug.echoError("Particles of type '" + particleEffect.getName() + "' cannot take special_data as input.");
                return;
            }
            Random random = CoreUtilities.getRandom();
            int quantityInt = quantity.asInt();
            for (Player player : players) {
                if (velocity == null) {
                    particleEffect.playFor(player, location, quantityInt, offset.toVector(), data.asDouble(), dataObject);
                } else {
                    Vector velocityVector = velocity.toVector();
                    for (int i = 0; i < quantityInt; i++) {
                        LocationTag singleLocation = location.clone().add((random.nextDouble() - 0.5) * offset.getX(), (random.nextDouble() - 0.5) * offset.getY(), (random.nextDouble() - 0.5) * offset.getZ());
                        particleEffect.playFor(player, singleLocation, 0, velocityVector, 1f, dataObject);
                    }
                }
            }
        } else // Play an iconcrack (item break) effect
        {
            List<Player> players = new ArrayList<>();
            if (targets == null) {
                float rad = radius.asFloat();
                for (Player player : location.getWorld().getPlayers()) {
                    if (player.getLocation().distanceSquared(location) < rad * rad) {
                        players.add(player);
                    }
                }
            } else {
                for (PlayerTag player : targets) {
                    if (player.isValid() && player.isOnline()) {
                        players.add(player.getPlayerEntity());
                    }
                }
            }
            if (iconcrack != null) {
                ItemStack itemStack = iconcrack.getItemStack();
                Particle particle = NMSHandler.getParticleHelper().getParticle("ITEM_CRACK");
                for (Player player : players) {
                    particle.playFor(player, location, quantity.asInt(), offset.toVector(), data.asFloat(), itemStack);
                }
            }
        }
    }
}
Also used : Particle(com.denizenscript.denizen.nms.interfaces.Particle) BlockData(org.bukkit.block.data.BlockData) Vector(org.bukkit.util.Vector) Player(org.bukkit.entity.Player) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Effect(org.bukkit.Effect) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

Particle (com.denizenscript.denizen.nms.interfaces.Particle)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 Effect (org.bukkit.Effect)1 BlockData (org.bukkit.block.data.BlockData)1 Player (org.bukkit.entity.Player)1 ItemStack (org.bukkit.inventory.ItemStack)1 Vector (org.bukkit.util.Vector)1