From 9f0a790b3d0008fbeb367bc447f6b9a8609a9933 Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Sat, 17 Aug 2024 08:37:46 +1000 Subject: [PATCH] SPIGOT-7868, #1463: Fix default and max power in FireworkMeta By: Doc --- .../inventory/CraftMetaFirework.java | 37 ++++++++++--------- .../craftbukkit/inventory/ItemMetaTest.java | 2 +- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java index 33c15a83d5..1d55be72b6 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Objects; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.core.component.DataComponents; import net.minecraft.world.item.component.FireworkExplosion; @@ -43,7 +44,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { static final ItemMetaKey EXPLOSIONS = new ItemMetaKey("firework-effects"); private List effects; - private int power; + private Integer power; CraftMetaFirework(CraftMetaItem meta) { super(meta); @@ -78,6 +79,18 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { }); } + CraftMetaFirework(Map map) { + super(map); + + Integer power = SerializableMeta.getObject(Integer.class, map, FLIGHT.BUKKIT, true); + if (power != null) { + this.power = power; + } + + Iterable effects = SerializableMeta.getObject(Iterable.class, map, EXPLOSIONS.BUKKIT, true); + safelyAddEffects(effects); + } + static FireworkEffect getEffect(FireworkExplosion explosion) { FireworkEffect.Builder effect = FireworkEffect.builder() .flicker(explosion.hasTwinkle()) @@ -144,18 +157,6 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { } } - CraftMetaFirework(Map map) { - super(map); - - Integer power = SerializableMeta.getObject(Integer.class, map, FLIGHT.BUKKIT, true); - if (power != null) { - this.power = power; - } - - Iterable effects = SerializableMeta.getObject(Iterable.class, map, EXPLOSIONS.BUKKIT, true); - safelyAddEffects(effects); - } - @Override public boolean hasEffects() { return !(effects == null || effects.isEmpty()); @@ -191,7 +192,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { } } - itemTag.put(FIREWORKS, new Fireworks(power, effects)); + itemTag.put(FIREWORKS, new Fireworks(this.getPower(), effects)); } static IntList addColors(List colors) { @@ -218,7 +219,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { } boolean hasPower() { - return power != 0; + return this.power != null; } @Override @@ -229,7 +230,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { if (meta instanceof CraftMetaFirework that) { - return (hasPower() ? that.hasPower() && this.power == that.power : !that.hasPower()) + return (Objects.equals(this.power, that.power)) && (hasEffects() ? that.hasEffects() && this.effects.equals(that.effects) : !that.hasEffects()); } @@ -339,13 +340,13 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { @Override public int getPower() { - return hasPower() ? this.power : 0; + return hasPower() ? this.power : 1; } @Override public void setPower(int power) { Preconditions.checkArgument(power >= 0, "power cannot be less than zero: %s", power); - Preconditions.checkArgument(power < 0x80, "power cannot be more than 127: %s", power); + Preconditions.checkArgument(power <= 255, "power cannot be more than 255: %s", power); this.power = power; } } diff --git a/paper-server/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java b/paper-server/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java index 022b726af5..91f59278f1 100644 --- a/paper-server/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java +++ b/paper-server/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java @@ -66,7 +66,7 @@ import org.junit.jupiter.api.Test; public class ItemMetaTest extends AbstractTestingBase { - static final int MAX_FIREWORK_POWER = 127; // Please update ItemStackFireworkTest if/when this gets changed. + static final int MAX_FIREWORK_POWER = 255; // Please update ItemStackFireworkTest if/when this gets changed. @Test public void testPowerLimitExact() {