Skip to content

Commit

Permalink
Update potion effect properties to modern format (#2325)
Browse files Browse the repository at this point in the history
* Start on effect_mao

* Finish update

* Fix sub-tags

* Small fixes
  • Loading branch information
tal5 committed Feb 21, 2022
1 parent 35b7f9b commit 1fd18ae
Show file tree
Hide file tree
Showing 6 changed files with 446 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ public void registerStonecuttingRecipe(String keyName, String group, ItemStack r

public abstract ItemStack setNbtData(ItemStack itemStack, CompoundTag compoundTag);

public abstract PotionEffect getPotionEffect(PotionEffectType type, int duration, int amplifier, boolean ambient, boolean particles, boolean icon);

public void registerSmithingRecipe(String keyName, ItemStack result, ItemStack[] baseItem, boolean baseExact, ItemStack[] upgradeItem, boolean upgradeExact) {
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.entity.Arrow;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
Expand All @@ -34,10 +36,6 @@ public static EntityPotionEffects getFrom(ObjectTag object) {
}
}

public static final String[] handledTags = new String[] {
"list_effects", "has_effect"
};

public static final String[] handledMechs = new String[] {
"potion_effects"
};
Expand All @@ -52,33 +50,46 @@ public Collection<PotionEffect> getEffectsList() {
if (entity.isLivingEntity()) {
return entity.getLivingEntity().getActivePotionEffects();
}
else if (entity.getBukkitEntity() instanceof Arrow) {
return ((Arrow) entity.getBukkitEntity()).getCustomEffects();
else if (isArrow()) {
return getArrow().getCustomEffects();
}
return new ArrayList<>();
}

public String getPropertyString() {
Collection<PotionEffect> effects = getEffectsList();
if (effects.isEmpty()) {
return null;
public ListTag getEffectsListTag() {
ListTag result = new ListTag();
for (PotionEffect effect : getEffectsList()) {
result.add(ItemPotion.stringifyEffect(effect));
}
ListTag returnable = new ListTag();
for (PotionEffect effect : effects) {
returnable.add(ItemPotion.stringifyEffect(effect));
return result;
}

public ListTag getEffectsMapTag() {
ListTag result = new ListTag();
for (PotionEffect effect : getEffectsList()) {
result.addObject(ItemPotion.effectToMap(effect));
}
return returnable.identify();
return result;
}

public boolean isArrow() {
return entity.getBukkitEntity() instanceof Arrow;
}

public Arrow getArrow() {
return (Arrow) entity.getBukkitEntity();
}

public String getPropertyString() {
ListTag effects = getEffectsMapTag();
return effects.isEmpty() ? null : effects.identify();
}

public String getPropertyId() {
return "potion_effects";
}

public ObjectTag getObjectAttribute(Attribute attribute) {

if (attribute == null) {
return null;
}
public static void registerTags() {

// <--[tag]
// @attribute <EntityTag.list_effects>
Expand All @@ -91,13 +102,21 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
// IS_AMBIENT, HAS_PARTICLES, and HAS_ICON are booleans.
// The effect type will be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>.
// -->
if (attribute.startsWith("list_effects")) {
ListTag effects = new ListTag();
for (PotionEffect effect : getEffectsList()) {
effects.add(ItemPotion.stringifyEffect(effect));
}
return effects.getObjectAttribute(attribute.fulfill(1));
}
PropertyParser.<EntityPotionEffects, ListTag>registerTag(ListTag.class, "list_effects", (attribute, object) -> {
return object.getEffectsListTag();
});

// <--[tag]
// @attribute <EntityTag.effects_data>
// @returns ListTag(MapTag)
// @group attribute
// @mechanism EntityTag.potion_effects
// @description
// Returns the active potion effects on the entity, in the MapTag format of the mechanism.
// -->
PropertyParser.<EntityPotionEffects, ListTag>registerTag(ListTag.class, "effects_data", (attribute, object) -> {
return object.getEffectsMapTag();
});

// <--[tag]
// @attribute <EntityTag.has_effect[<effect>]>
Expand All @@ -109,23 +128,26 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
// If no effect is specified, returns whether the entity has any effect.
// The effect type must be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>.
// -->
if (attribute.startsWith("has_effect")) {
PropertyParser.<EntityPotionEffects, ElementTag>registerTag(ElementTag.class, "has_effect", (attribute, object) -> {
boolean returnElement = false;
if (attribute.hasParam()) {
PotionEffectType effectType = PotionEffectType.getByName(attribute.getParam());
for (org.bukkit.potion.PotionEffect effect : getEffectsList()) {
if (effect.getType().equals(effectType)) {
returnElement = true;
}
if (effectType == null) {
attribute.echoError("Invalid effect type specified: " + attribute.getParam());
return null;
}
if (object.entity.isLivingEntity()) {
returnElement = object.entity.getLivingEntity().hasPotionEffect(effectType);
}
else if (object.isArrow()) {
returnElement = object.getArrow().hasCustomEffect(effectType);
}
}
else if (!getEffectsList().isEmpty()) {
else if (!object.getEffectsList().isEmpty()) {
returnElement = true;
}
return new ElementTag(returnElement).getObjectAttribute(attribute.fulfill(1));
}

return null;
return new ElementTag(returnElement);
});
}

public void adjust(Mechanism mechanism) {
Expand All @@ -136,28 +158,39 @@ public void adjust(Mechanism mechanism) {
// @input ListTag
// @description
// Set the entity's active potion effects.
// Each item in the list is formatted as: TYPE,AMPLIFIER,DURATION,IS_AMBIENT,HAS_PARTICLES,HAS_ICON
// Each item in the list can be any of the following:
// 1: Comma-separated potion effect data in the format: TYPE,AMPLIFIER,DURATION,IS_AMBIENT,HAS_PARTICLES,HAS_ICON
// Note that AMPLIFIER is a number representing the level, and DURATION is a number representing the time, in ticks, it will last for.
// IS_AMBIENT, HAS_PARTICLES, and HAS_ICON are booleans.
// For example: SPEED,0,120,false,true,true would give the entity a swiftness potion for 120 ticks.
// 2: A MapTag with "type", "amplifier", "duration", "ambient", "particles", and "icon" keys.
// For example: [type=SPEED;amplifier=0;duration=120t;ambient=false;particles=true;icon=true]
// The effect type must be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>.
// @tags
// <EntityTag.effects_data>
// <EntityTag.list_effects>
// <EntityTag.has_effect[<effect>]>
// -->
if (mechanism.matches("potion_effects")) {
ListTag effects = ListTag.valueOf(mechanism.getValue().asString(), mechanism.context);
for (String effectStr : effects) {
PotionEffect effect = ItemPotion.parseEffect(effectStr, mechanism.context);
for (ObjectTag effectObj : CoreUtilities.objectToList(mechanism.value, mechanism.context)) {
PotionEffect effect;
if (effectObj.canBeType(MapTag.class)) {
MapTag effectMap = effectObj.asType(MapTag.class, mechanism.context);
effect = ItemPotion.parseEffect(effectMap, mechanism.context);
}
else {
String effectStr = effectObj.toString();
effect = ItemPotion.parseEffect(effectStr, mechanism.context);
}
if (effect == null) {
mechanism.echoError("Invalid potion effect '" + effectStr + "'");
mechanism.echoError("Invalid potion effect '" + effectObj + "'");
continue;
}
if (entity.isLivingEntity()) {
entity.getLivingEntity().addPotionEffect(effect);
}
else if (entity.getBukkitEntity() instanceof Arrow) {
((Arrow) entity.getBukkitEntity()).addCustomEffect(effect, true);
else if (isArrow()) {
getArrow().addCustomEffect(effect, true);
}
}
}
Expand Down
Loading

0 comments on commit 1fd18ae

Please sign in to comment.