Skip to content

Commit

Permalink
1.20+ potions support (#2692)
Browse files Browse the repository at this point in the history
* Split base potion data handling for cleaner flow

* Custom name, null base type, modern effect type

* Back-Support legacy tags/mechs

* Metaify all changes

* Fixup some version checks

* Minor `applyBasePotionData` cleanup & renames

* More backsupport for now-null base potion type

* Final fixups

* `effect` key only on 1.20+ to match input

* Oops whitespace
  • Loading branch information
tal5 authored Jan 8, 2025
1 parent e9d9077 commit 9adc643
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 92 deletions.
2 changes: 1 addition & 1 deletion paper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.21.3-R0.1-SNAPSHOT</version>
<version>1.21.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class PlayerBeaconEffectScriptEvent extends BukkitScriptEvent implements
//
// @Context
// <context.location> returns the LocationTag of the beacon applying an effect.
// <context.effect_data> returns a MapTag of the potion effect (in the same format as <@link tag EntityTag.effects_data>).
// <context.effect_data> returns a MapTag of the potion effect in <@link language Potion Effect Format>.
// <context.effect_type> returns an ElementTag of the effect type.
// <context.is_primary> returns an ElementTag(Boolean) of whether the beacon effect is the primary effect.
//
Expand Down Expand Up @@ -78,7 +78,7 @@ public ObjectTag getContext(String name) {
return switch (name) {
case "location" -> new LocationTag(event.getBlock().getLocation());
case "effect" -> new ElementTag(ItemPotion.effectToLegacyString(event.getEffect(), null));
case "effect_data" -> ItemPotion.effectToMap(event.getEffect());
case "effect_data" -> ItemPotion.effectToMap(event.getEffect(), true);
case "effect_type" -> new ElementTag(event.getEffect().getType().getName());
case "is_primary" -> new ElementTag(event.isPrimary());
default -> super.getContext(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionBrewer;
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Consumer;
Expand Down Expand Up @@ -387,4 +388,9 @@ public String getClientBrand(Player player) {
public boolean canUseEquipmentSlot(LivingEntity entity, EquipmentSlot slot) {
return NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20) ? entity.canUseEquipmentSlot(slot) : super.canUseEquipmentSlot(entity, slot);
}

@Override
public boolean hasCustomName(PotionMeta meta) {
return meta.hasCustomPotionName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class EntityPotionEffectScriptEvent extends BukkitScriptEvent implements
// <context.cause> returns the cause of the effect change, based on <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityPotionEffectEvent.Cause.html>
// <context.action> returns the action of the effect changed, which can be 'added', 'changed', 'cleared', or 'removed'
// <context.override> returns whether the new potion effect will override the old.
// <context.new_effect_data> returns the new potion effect (in the same format as <@link tag EntityTag.effects_data>) (if any).
// <context.old_effect_data> returns the old potion effect (in the same format as <@link tag EntityTag.effects_data>) (if any).
// <context.new_effect_data> returns the new potion effect in <@link language Potion Effect Format>.
// <context.old_effect_data> returns the old potion effect in <@link language Potion Effect Format>.
// <context.effect_type> returns the name of the modified potion effect type.
//
// @Determine
Expand Down Expand Up @@ -118,8 +118,8 @@ public ObjectTag getContext(String name) {
case "override" -> new ElementTag(event.isOverride());
case "new_effect" -> event.getNewEffect() == null ? null : new ElementTag(ItemPotion.effectToLegacyString(event.getNewEffect(), null));
case "old_effect" -> event.getOldEffect() == null ? null : new ElementTag(ItemPotion.effectToLegacyString(event.getOldEffect(), null));
case "new_effect_data" -> event.getNewEffect() == null ? null : ItemPotion.effectToMap(event.getNewEffect());
case "old_effect_data" -> event.getOldEffect() == null ? null : ItemPotion.effectToMap(event.getOldEffect());
case "new_effect_data" -> event.getNewEffect() == null ? null : ItemPotion.effectToMap(event.getNewEffect(), true);
case "old_effect_data" -> event.getOldEffect() == null ? null : ItemPotion.effectToMap(event.getOldEffect(), true);
default -> super.getContext(name);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ public static void registerMainProperties() {
registerItemProperty(ItemMap.class, "map_id");
PropertyParser.registerProperty(ItemNBT.class, ItemTag.class);
registerItemProperty(ItemPatterns.class, "banner_patterns");
if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_19)) {
registerItemProperty(ItemPotion.class, "potion_contents");
}
registerItemProperty(ItemPotion.class, "potion_contents");
PropertyParser.registerProperty(ItemQuantity.class, ItemTag.class);
PropertyParser.registerProperty(ItemRawNBT.class, ItemTag.class);
registerItemProperty(ItemRepairCost.class, "repair_cost");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public ListTag getEffectsListTag(TagContext context) {
return result;
}

public ListTag getEffectsMapTag() {
public ListTag getEffectsMapTag(boolean includeDeprecated) {
ListTag result = new ListTag();
for (PotionEffect effect : getEffectsList()) {
result.addObject(ItemPotion.effectToMap(effect));
result.addObject(ItemPotion.effectToMap(effect, includeDeprecated));
}
return result;
}
Expand All @@ -83,7 +83,7 @@ public Arrow getArrow() {
}

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

Expand Down Expand Up @@ -114,10 +114,10 @@ public static void register() {
// @group attribute
// @mechanism EntityTag.potion_effects
// @description
// Returns the active potion effects on the entity, in the MapTag format of the mechanism.
// Returns the active potion effects on the entity, as a list of maps in <@link language Potion Effect Format>.
// -->
PropertyParser.registerTag(EntityPotionEffects.class, ListTag.class, "effects_data", (attribute, object) -> {
return object.getEffectsMapTag();
return object.getEffectsMapTag(true);
});

// <--[tag]
Expand Down Expand Up @@ -160,17 +160,9 @@ public void adjust(Mechanism mechanism) {
// @input ListTag
// @description
// Set the entity's active potion effects.
// Each item in the list must be a MapTag with keys:
// "type" - from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>
// "amplifier" - number to increase the level by (0 for default level 1)
// "duration" - DurationTag, how long it lasts
// "ambient", "particles", "icon" - booleans
//
// For example: [type=SPEED;amplifier=0;duration=120t;ambient=false;particles=true;icon=true]
// This example would be a level 1 swiftness potion that lasts 120 ticks.
// Each item in the list must be a MapTag in <@link language Potion Effect Format>.
// @tags
// <EntityTag.effects_data>
// <EntityTag.list_effects>
// <EntityTag.has_effect[<effect>]>
// -->
if (mechanism.matches("potion_effects")) {
Expand Down
Loading

0 comments on commit 9adc643

Please sign in to comment.