Skip to content

Commit

Permalink
add beacon primary+secondary effect tag/mech pair
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 30, 2019
1 parent f82a56a commit 35355df
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 90 deletions.
Expand Up @@ -117,9 +117,6 @@ public void onCraftItem(CraftItemEvent event) {
return;
}
Recipe eRecipe = event.getRecipe();
if (eRecipe == null || eRecipe.getResult() == null) {
return;
}
this.event = event;
result = new ItemTag(eRecipe.getResult());
this.player = EntityTag.getPlayerFrom(humanEntity);
Expand Down
Expand Up @@ -46,6 +46,8 @@
import org.bukkit.material.Attachable;
import org.bukkit.material.Door;
import org.bukkit.material.MaterialData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.BlockIterator;
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;
Expand Down Expand Up @@ -2762,6 +2764,36 @@ else if (attribute.startsWith("vertical", 2)) {
return new ElementTag(((Beacon) object.getBlockStateForTag(attribute)).getTier());
});

// <--[tag]
// @attribute <LocationTag.beacon_primary_effect>
// @returns ElementTag
// @mechanism LocationTag.beacon_primary_effect
// @description
// Returns the primary effect of the beacon. The return is simply a potion effect type name.
// -->
registerTag("beacon_primary_effect", (attribute, object) -> {
PotionEffect effect = ((Beacon) object.getBlockStateForTag(attribute)).getPrimaryEffect();
if (effect == null) {
return null;
}
return new ElementTag(effect.getType().getName());
});

// <--[tag]
// @attribute <LocationTag.beacon_secondary_effect>
// @returns ElementTag
// @mechanism LocationTag.beacon_secondary_effect
// @description
// Returns the secondary effect of the beacon. The return is simply a potion effect type name.
// -->
registerTag("beacon_secondary_effect", (attribute, object) -> {
PotionEffect effect = ((Beacon) object.getBlockStateForTag(attribute)).getSecondaryEffect();
if (effect == null) {
return null;
}
return new ElementTag(effect.getType().getName());
});

// <--[tag]
// @attribute <LocationTag.attached_to>
// @returns LocationTag
Expand Down Expand Up @@ -3305,6 +3337,36 @@ else if (getBlock().getType() == Material.FLOWER_POT) {
}
}

// <--[mechanism]
// @object LocationTag
// @name beacon_primary_effect
// @input ElementTag
// @description
// Sets the primary effect of a beacon, with input as just an effect type name.
// @tags
// <LocationTag.beacon_primary_effect>
// -->
if (mechanism.matches("beacon_primary_effect")) {
Beacon beacon = (Beacon) getBlockState();
beacon.setPrimaryEffect(PotionEffectType.getByName(mechanism.getValue().asString().toUpperCase()));
beacon.update();
}

// <--[mechanism]
// @object LocationTag
// @name beacon_secondary_effect
// @input ElementTag
// @description
// Sets the secondary effect of a beacon, with input as just an effect type name.
// @tags
// <LocationTag.beacon_secondary_effect>
// -->
if (mechanism.matches("beacon_secondary_effect")) {
Beacon beacon = (Beacon) getBlockState();
beacon.setSecondaryEffect(PotionEffectType.getByName(mechanism.getValue().asString().toUpperCase()));
beacon.update();
}

// <--[mechanism]
// @object LocationTag
// @name activate
Expand Down
@@ -1,23 +1,19 @@
package com.denizenscript.denizen.objects.properties.entity;

import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.properties.item.ItemPotion;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.entity.Arrow;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class EntityPotionEffects implements Property {

Expand Down Expand Up @@ -85,12 +81,6 @@ public String getPropertyId() {
return "potion_effects";
}

public static String stringify(PotionEffect effect) {
return effect.getType().getName() + "," + effect.getAmplifier() + "," + effect.getDuration()
+ "," + effect.isAmbient() + "," + effect.hasParticles()
+ (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13) ? "," + effect.hasIcon() : "");
}

public ObjectTag getObjectAttribute(Attribute attribute) {

if (attribute == null) {
Expand All @@ -110,7 +100,7 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute.startsWith("list_effects")) {
ListTag effects = new ListTag();
for (PotionEffect effect : getEffectsList()) {
effects.add(stringify(effect));
effects.add(ItemPotion.stringifyEffect(effect));
}
return effects.getObjectAttribute(attribute.fulfill(1));
}
Expand Down Expand Up @@ -159,42 +149,13 @@ public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("potion_effects")) {
ListTag effects = ListTag.valueOf(mechanism.getValue().asString());
for (String effect : effects) {
List<String> split = CoreUtilities.split(effect, ',');
if (split.size() < 3) {
// Maybe error message?
continue;
}
PotionEffectType effectType = PotionEffectType.getByName(split.get(0));
if (effectType == null) {
Debug.echoError("Cannot apply potion effect '" + split.get(0) + "': unknown effect type.");
continue;
}
try {
PotionEffect actualEffect;
if (split.size() >= 6) {
actualEffect = new PotionEffect(effectType, Integer.valueOf(split.get(2)),
Integer.valueOf(split.get(1)), split.get(3).equalsIgnoreCase("true"), split.get(4).equalsIgnoreCase("true"),
split.get(5).equalsIgnoreCase("true"));
}
else if (split.size() >= 5) {
actualEffect = new PotionEffect(effectType, Integer.valueOf(split.get(2)),
Integer.valueOf(split.get(1)), split.get(3).equalsIgnoreCase("true"), split.get(4).equalsIgnoreCase("true"));
}
else {
actualEffect = new PotionEffect(effectType, Integer.valueOf(split.get(2)),
Integer.valueOf(split.get(1)));
}
if (entity.isLivingEntity()) {
entity.getLivingEntity().addPotionEffect(actualEffect);
}
else if (entity.getBukkitEntity() instanceof Arrow) {
((Arrow) entity.getBukkitEntity()).addCustomEffect(actualEffect, true);
}
for (String effectStr : effects) {
PotionEffect effect = ItemPotion.parseEffect(effectStr);
if (entity.isLivingEntity()) {
entity.getLivingEntity().addPotionEffect(effect);
}
catch (NumberFormatException ex) {
Debug.echoError("Cannot apply potion effect '" + effect + "': invalid amplifier or duration number.");
continue;
else if (entity.getBukkitEntity() instanceof Arrow) {
((Arrow) entity.getBukkitEntity()).addCustomEffect(effect, true);
}
}
}
Expand Down
Expand Up @@ -3,7 +3,6 @@
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.nms.interfaces.ItemHelper;
import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
Expand Down Expand Up @@ -56,6 +55,49 @@ private ItemPotion(ItemTag item) {

ItemTag item;

public static String stringifyEffect(PotionEffect effect) {
StringBuilder sb = new StringBuilder();
sb.append(effect.getType().getName()).append(",")
.append(effect.getAmplifier()).append(",")
.append(effect.getDuration()).append(",")
.append(effect.isAmbient()).append(",")
.append(effect.hasParticles());
if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_12) && effect.getColor() != null) {
sb.append(",").append(new ColorTag(effect.getColor()).identify().replace(",", "&comma"));
}
return sb.toString();
}

public static PotionEffect parseEffect(String str) {
String[] d2 = str.split(",");
PotionEffectType type = PotionEffectType.getByName(d2[0].toUpperCase());
// NOTE: amplifier and duration are swapped around in the input format
// as compared to the PotionEffect constructor!
int duration = new ElementTag(d2[2]).asInt();
int amplifier = new ElementTag(d2[1]).asInt();
boolean ambient = new ElementTag(d2[3]).asBoolean();
boolean particles = new ElementTag(d2[4]).asBoolean();
Color color = null;
boolean icon = false;
if (d2.length > 5) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13)) {
ElementTag check = new ElementTag(d2[5]);
if (check.isBoolean()) {
icon = check.asBoolean();
}
else {
Debug.echoError("Custom effects with the color option are not supported as of Minecraft version 1.13.");
}
}
else {
String check = d2[5].replace("&comma", ",");
if (ColorTag.matches(check)) {
color = ColorTag.valueOf(check).getColor();
}
}
}
return NMSHandler.getItemHelper().getPotionEffect(type, duration, amplifier, ambient, particles, color, icon);
}

@Override
public String getPropertyString() {
Expand All @@ -73,16 +115,7 @@ public String getPropertyString() {
+ (meta.hasColor() ? "," + new ColorTag(meta.getColor()).identify().replace(",", "&comma") : "")
);
for (PotionEffect pot : meta.getCustomEffects()) {
StringBuilder sb = new StringBuilder();
sb.append(pot.getType().getName()).append(",")
.append(pot.getAmplifier()).append(",")
.append(pot.getDuration()).append(",")
.append(pot.isAmbient()).append(",")
.append(pot.hasParticles());
if (pot.getColor() != null) {
sb.append(",").append(new ColorTag(pot.getColor()).identify().replace(",", "&comma"));
}
effects.add(sb.toString());
effects.add(stringifyEffect(pot));
}
return effects.identify();
}
Expand Down Expand Up @@ -339,36 +372,8 @@ public void adjust(Mechanism mechanism) {
meta.setColor(ColorTag.valueOf(d1[3].replace("&comma", ",")).getColor());
}
meta.clearCustomEffects();
ItemHelper itemHelper = NMSHandler.getItemHelper();
for (int i = 1; i < data.size(); i++) {
String[] d2 = data.get(i).split(",");
PotionEffectType type = PotionEffectType.getByName(d2[0].toUpperCase());
// NOTE: amplifier and duration are swapped around in the input format
// as compared to the PotionEffect constructor!
int duration = new ElementTag(d2[2]).asInt();
int amplifier = new ElementTag(d2[1]).asInt();
boolean ambient = new ElementTag(d2[3]).asBoolean();
boolean particles = new ElementTag(d2[4]).asBoolean();
Color color = null;
boolean icon = false;
if (d2.length > 5) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13)) {
ElementTag check = new ElementTag(d2[5]);
if (check.isBoolean()) {
icon = check.asBoolean();
}
else {
Debug.echoError("Custom effects with the color option are not supported as of Minecraft version 1.13.");
}
}
else {
String check = d2[5].replace("&comma", ",");
if (ColorTag.matches(check)) {
color = ColorTag.valueOf(check).getColor();
}
}
}
meta.addCustomEffect(itemHelper.getPotionEffect(type, duration, amplifier, ambient, particles, color, icon), false);
meta.addCustomEffect(parseEffect(data.get(i)), false);
}
item.getItemStack().setItemMeta(meta);
}
Expand Down

0 comments on commit 35355df

Please sign in to comment.