From 35355df9b42d8e9ba8feb1695aa80e5d1c65f7be Mon Sep 17 00:00:00 2001 From: Alex 'mcmonkey' Goodwin Date: Sat, 30 Nov 2019 13:48:08 -0800 Subject: [PATCH] add beacon primary+secondary effect tag/mech pair --- .../player/PlayerCraftsItemScriptEvent.java | 3 - .../denizen/objects/LocationTag.java | 62 ++++++++++++++ .../entity/EntityPotionEffects.java | 55 ++---------- .../objects/properties/item/ItemPotion.java | 85 ++++++++++--------- 4 files changed, 115 insertions(+), 90 deletions(-) diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerCraftsItemScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerCraftsItemScriptEvent.java index 75f33158dd..101a4a3528 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerCraftsItemScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerCraftsItemScriptEvent.java @@ -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); diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/LocationTag.java b/plugin/src/main/java/com/denizenscript/denizen/objects/LocationTag.java index b2537f7eba..c31d2384bd 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/LocationTag.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/LocationTag.java @@ -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; @@ -2762,6 +2764,36 @@ else if (attribute.startsWith("vertical", 2)) { return new ElementTag(((Beacon) object.getBlockStateForTag(attribute)).getTier()); }); + // <--[tag] + // @attribute + // @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 + // @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 // @returns LocationTag @@ -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 + // + // --> + 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 + // + // --> + 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 diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/entity/EntityPotionEffects.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/entity/EntityPotionEffects.java index ca3d003348..5e73c65c8e 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/entity/EntityPotionEffects.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/entity/EntityPotionEffects.java @@ -1,8 +1,6 @@ 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; @@ -10,14 +8,12 @@ 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 { @@ -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) { @@ -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)); } @@ -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 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); } } } diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemPotion.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemPotion.java index b265d8476a..cbb3a83545 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemPotion.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemPotion.java @@ -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; @@ -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() { @@ -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(); } @@ -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); }