Skip to content

Commit

Permalink
playeffect: legacy names handling (#2629)
Browse files Browse the repository at this point in the history
* `playeffect`: legacy names handling

* Remove particle wrapper + renames

* Unrelated small meta fixes
  • Loading branch information
tal5 committed Jun 25, 2024
1 parent 83ff9d0 commit f6c3bac
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public class EntityTargetsScriptEvent extends BukkitScriptEvent implements Liste
//
// @Cancellable true
//
// @Triggers when an entity targets a new entity - usually a hostile mob preparing to attack something.
// @Triggers when an entity targets a new entity (usually a hostile mob preparing to attack something), or un-targets one.
//
// @Context
// <context.entity> returns the targeting entity.
// <context.reason> returns the reason the entity changed targets. Refer to <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityTargetEvent.TargetReason.html>.
// <context.target> returns the targeted entity.
// <context.target> returns the targeted entity, if any.
//
// @Determine
// EntityTag to make the entity target a different entity instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public static JavaPlugin getJavaPlugin() {
public static FishingHelper fishingHelper;
public static ItemHelper itemHelper;
public static PacketHelper packetHelper;
public static ParticleHelper particleHelper;
public static PlayerHelper playerHelper;
public static WorldHelper worldHelper;
public static EnchantmentHelper enchantmentHelper;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
package com.denizenscript.denizen.scripts.commands.world;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.objects.properties.bukkit.BukkitColorExtensions;
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
import com.denizenscript.denizen.utilities.LegacyNamingHelper;
import com.denizenscript.denizen.utilities.LegacyParticleNaming;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ColorTag;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.interfaces.Particle;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import org.bukkit.Effect;
import org.bukkit.Particle;
import org.bukkit.Vibration;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class PlayEffectCommand extends AbstractCommand {

public static final LegacyNamingHelper<Particle> PARTICLE_NAMING = new LegacyNamingHelper<>(Particle.class);
public static final List<Particle> VISIBLE_PARTICLES = new ArrayList<>(Arrays.asList(Particle.values()));

static {
VISIBLE_PARTICLES.removeAll(List.of(Particle.valueOf("SUSPENDED"), Particle.valueOf("SUSPENDED_DEPTH"), Particle.valueOf("WATER_BUBBLE")));
}

public PlayEffectCommand() {
setName("playeffect");
setSyntax("playeffect [effect:<name>] [at:<location>|...] (data:<#.#>) (special_data:<data>) (visibility:<#.#>) (quantity:<#>) (offset:<#.#>,<#.#>,<#.#>) (targets:<player>|...) (velocity:<vector>)");
Expand All @@ -39,7 +50,7 @@ public PlayEffectCommand() {
// @name Particle Effects
// @group Useful Lists
// @description
// All of the effects listed here can be used by <@link command PlayEffect> to display visual effects or play sounds
// All the effects listed here can be used by <@link command PlayEffect> to display visual effects or play sounds
//
// Effects:
// - Everything on <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Particle.html>
Expand Down Expand Up @@ -98,7 +109,7 @@ public PlayEffectCommand() {

@Override
public void addCustomTabCompletions(TabCompletionsBuilder tab) {
tab.addWithPrefix("effect:", NMSHandler.particleHelper.particles.keySet());
tab.addWithPrefix("effect:", Particle.values());
tab.addWithPrefix("effect:", Effect.values());
}

Expand All @@ -117,19 +128,26 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
else if (!scriptEntry.hasObject("effect") &&
!scriptEntry.hasObject("particleeffect") &&
!scriptEntry.hasObject("iconcrack")) {
if (NMSHandler.particleHelper.hasParticle(arg.getValue())) {
scriptEntry.addObject("particleeffect", NMSHandler.particleHelper.getParticle(arg.getValue()));
String particleName = CoreUtilities.toUpperCase(arg.getValue());
Particle particle = PARTICLE_NAMING.fromName(particleName, scriptEntry.getContext());
if (particle != null) {
scriptEntry.addObject("particleeffect", particle);
continue;
}
particle = LegacyParticleNaming.legacyParticleNames.get(particleName);
if (particle != null) {
BukkitImplDeprecations.oldSpigotNames.warn(scriptEntry);
scriptEntry.addObject("particleeffect", particle);
continue;
}
else if (arg.matches("barrier") && NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
scriptEntry.addObject("particleeffect", NMSHandler.particleHelper.getParticle("block_marker"));
if (arg.matches("barrier") && NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
scriptEntry.addObject("particleeffect", Particle.BLOCK_MARKER);
scriptEntry.addObject("special_data", new ElementTag("barrier"));
continue;
}
else if (arg.matches("random")) {
// Get another effect if "RANDOM" is used
List<Particle> visible = NMSHandler.particleHelper.getVisibleParticles();
scriptEntry.addObject("particleeffect", visible.get(CoreUtilities.getRandom().nextInt(visible.size())));
scriptEntry.addObject("particleeffect", VISIBLE_PARTICLES.get(CoreUtilities.getRandom().nextInt(VISIBLE_PARTICLES.size())));
continue;
}
else if (arg.startsWith("iconcrack_")) {
Expand Down Expand Up @@ -227,7 +245,7 @@ public void execute(ScriptEntry scriptEntry) {
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),
Debug.report(scriptEntry, getName(), (effect != null ? db("effect", effect.name()) : particleEffect != null ? db("special effect", particleEffect.name()) : 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) {
Expand Down Expand Up @@ -268,23 +286,23 @@ else if (particleEffect != null) {
}
}
}
Class clazz = particleEffect.neededData();
Class<?> clazz = particleEffect.getDataType() == Void.class ? null : particleEffect.getDataType();
Object dataObject = null;
if (clazz != null) {
if (special_data == null) {
Debug.echoError("Missing required special data for particle: " + particleEffect.getName());
Debug.echoError("Missing required special data for particle: " + particleEffect.name());
return;
}
else if (clazz == org.bukkit.Particle.DustOptions.class) {
else if (clazz == 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());
Debug.echoError("DustOptions special_data must have 2 list entries for particle: " + particleEffect.name());
return;
}
else {
float size = Float.parseFloat(dataList.get(0));
ColorTag color = ColorTag.valueOf(dataList.get(1), scriptEntry.context);
dataObject = new org.bukkit.Particle.DustOptions(BukkitColorExtensions.getColor(color), size);
dataObject = new Particle.DustOptions(BukkitColorExtensions.getColor(color), size);
}
}
else if (clazz == BlockData.class) {
Expand All @@ -295,23 +313,23 @@ else if (clazz == ItemStack.class) {
ItemTag itemType = ItemTag.valueOf(special_data.asString(), scriptEntry.getContext());
dataObject = itemType.getItemStack();
}
else if (clazz == org.bukkit.Particle.DustTransition.class) {
else if (clazz == 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());
Debug.echoError("DustTransition special_data must have 3 list entries for particle: " + particleEffect.name());
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(BukkitColorExtensions.getColor(fromColor), BukkitColorExtensions.getColor(toColor), size);
dataObject = new Particle.DustTransition(BukkitColorExtensions.getColor(fromColor), BukkitColorExtensions.getColor(toColor), size);
}
}
else if (clazz == Vibration.class) {
ListTag dataList = ListTag.valueOf(special_data.asString(), scriptEntry.getContext());
if (dataList.size() != 3) {
Debug.echoError("Vibration special_data must have 3 list entries for particle: " + particleEffect.getName());
Debug.echoError("Vibration special_data must have 3 list entries for particle: " + particleEffect.name());
return;
}
else {
Expand All @@ -329,27 +347,26 @@ else if (clazz == Vibration.class) {
}
}
else {
Debug.echoError("Unknown particle data type: " + clazz.getCanonicalName() + " for particle: " + particleEffect.getName());
Debug.echoError("Unknown particle data type: " + clazz.getCanonicalName() + " for particle: " + particleEffect.name());
return;
}
}
else if (special_data != null) {
Debug.echoError("Particles of type '" + particleEffect.getName() + "' cannot take special_data as input.");
Debug.echoError("Particles of type '" + particleEffect.name() + "' 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);
player.spawnParticle(particleEffect, location, quantityInt, offset.getX(), offset.getY(), offset.getZ(), 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);
player.spawnParticle(particleEffect, singleLocation, 0, velocity.getX(), velocity.getY(), velocity.getZ(), 1, dataObject);
}
}
}
Expand All @@ -374,9 +391,9 @@ else if (special_data != null) {
}
if (iconcrack != null) {
ItemStack itemStack = iconcrack.getItemStack();
Particle particle = NMSHandler.particleHelper.getParticle("ITEM_CRACK");
Particle particle = Particle.valueOf("ITEM_CRACK");
for (Player player : players) {
particle.playFor(player, location, quantity.asInt(), offset.toVector(), data.asFloat(), itemStack);
player.spawnParticle(particle, location, quantity.asInt(), offset.getX(), offset.getY(), offset.getZ(), data.asFloat(), itemStack);
}
}
}
Expand Down
Loading

0 comments on commit f6c3bac

Please sign in to comment.