Make FireworkEffect record

This commit is contained in:
themode 2021-12-19 20:10:27 +01:00 committed by TheMode
parent 2e07af2b80
commit 81aa3dde27
5 changed files with 38 additions and 156 deletions

View File

@ -42,7 +42,7 @@ public abstract class ItemMetaBuilder implements TagWritable {
@Contract("_ -> this")
public @NotNull ItemMetaBuilder unbreakable(boolean unbreakable) {
this.unbreakable = unbreakable;
mutateNbt(compound -> compound.setByte("Unbreakable", (byte) (unbreakable ? 1 : 0)));
mutateNbt(compound -> compound.set("Unbreakable", NBT.Boolean(unbreakable)));
return this;
}

View File

@ -2,39 +2,21 @@ package net.minestom.server.item.firework;
import net.minestom.server.color.Color;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.collections.ImmutableIntArray;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTIntArray;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Map;
public class FireworkEffect {
private final boolean flicker;
private final boolean trail;
private final FireworkEffectType type;
private final List<Color> color;
private final List<Color> fadeColor;
//FIXME: fix javadoc
/**
* Initializes a new firework effect.
*
* @param flicker {@code true} if this explosion has the Twinkle effect (glowstone dust), otherwise {@code false}.
* @param trail {@code true} if this explosion hsa the Trail effect (diamond), otherwise {@code false}.
* @param type The shape of this firework's explosion.
* @param color The primary colors of this firework effect.
* @param fadeColor The secondary colors of this firework effect.
*/
public FireworkEffect(boolean flicker, boolean trail, FireworkEffectType type, List<Color> color, List<Color> fadeColor) {
this.flicker = flicker;
this.trail = trail;
this.type = type;
this.color = color;
this.fadeColor = fadeColor;
public record FireworkEffect(boolean flicker, boolean trail,
@NotNull FireworkEffectType type,
@NotNull List<Color> colors,
@NotNull List<Color> fadeColors) {
public FireworkEffect {
colors = List.copyOf(colors);
fadeColors = List.copyOf(fadeColors);
}
/**
@ -43,94 +25,23 @@ public class FireworkEffect {
* @param compound The NBT connection, which should be a fireworks effect.
* @return A new created firework effect.
*/
public static FireworkEffect fromCompound(@NotNull NBTCompound compound) {
public static @NotNull FireworkEffect fromCompound(@NotNull NBTCompound compound) {
List<Color> primaryColor = new ArrayList<>();
List<Color> secondaryColor = new ArrayList<>();
if (compound.containsKey("Colors")) {
ImmutableIntArray color = compound.getIntArray("Colors");
for (int j : color) {
primaryColor.add(new Color(j));
}
if (compound.get("Colors") instanceof NBTIntArray colors) {
for (int rgb : colors) primaryColor.add(new Color(rgb));
}
if (compound.get("FadeColors") instanceof NBTIntArray fadeColors) {
for (int rgb : fadeColors) secondaryColor.add(new Color(rgb));
}
if (compound.containsKey("FadeColors")) {
ImmutableIntArray fadeColor = compound.getIntArray("FadeColors");
for (int j : fadeColor) {
secondaryColor.add(new Color(j));
}
}
boolean flicker = compound.containsKey("Flicker") && compound.getBoolean("Flicker");
boolean trail = compound.containsKey("Trail") && compound.getBoolean("Trail");
FireworkEffectType type = compound.containsKey("Type") ?
FireworkEffectType.byId(compound.getAsByte("Type")) : FireworkEffectType.SMALL_BALL;
boolean flicker = compound.containsKey("Flicker") && compound.getAsByte("Flicker") == 1;
boolean trail = compound.containsKey("Trail") && compound.getAsByte("Trail") == 1;
FireworkEffectType type = compound.containsKey("Type") ? FireworkEffectType.byId(compound.getAsByte("Type")) : FireworkEffectType.SMALL_BALL;
return new FireworkEffect(
flicker,
trail,
type,
primaryColor,
secondaryColor);
}
/**
* Whether the firework has a flicker effect.
*
* @return {@code 1} if this explosion has the flicker effect, otherwise {@code 0}.
*/
public byte getFlicker() {
return (byte) (this.flicker ? 1 : 0);
}
/**
* Whether the firework has a trail effect.
*
* @return {@code 1} if this explosion has the trail effect, otherwise {@code 0};
*/
public byte getTrail() {
return (byte) (this.trail ? 1 : 0);
}
/**
* Retrieves type of the firework effect.
*
* @return The firework type as a byte.
*/
public byte getType() {
return this.type.getType();
}
/**
* Retrieves array of integer values corresponding to the primary colors of this firework's explosion.
* <p>
* If custom color codes are used, the game renders is as "Custom" in the tooltip, but the proper color is used
* in the explosion.
*
* @return An array of integer values corresponding to the primary colors of this firework's explosion.
*/
public int[] getColors() {
int[] primary = new int[color.size()];
for (int i = 0; i < color.size(); i++) {
primary[i] = color.get(i).asRGB();
}
return primary;
}
/**
* Retrieves array of integer values corresponding to the fading colors of this firework's explosion.
* <p>
* Same handling of custom colors as Colors.
*
* @return An array of integer values corresponding to the fading colors of this firework's explosion.
*/
public int[] getFadeColors() {
int[] secondary = new int[fadeColor.size()];
for (int i = 0; i < fadeColor.size(); i++) {
secondary[i] = fadeColor.get(i).asRGB();
}
return secondary;
return new FireworkEffect(flicker, trail, type, primaryColor, secondaryColor);
}
/**
@ -138,37 +49,12 @@ public class FireworkEffect {
*
* @return The firework effect as a nbt compound.
*/
public NBTCompound asCompound() {
return NBT.Compound(explosionCompound -> {
explosionCompound.setByte("Flicker", this.getFlicker());
explosionCompound.setByte("Trail", this.getTrail());
explosionCompound.setByte("Type", this.getType());
explosionCompound.setIntArray("Colors", this.getColors());
explosionCompound.setIntArray("FadeColors", this.getFadeColors());
});
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FireworkEffect that = (FireworkEffect) o;
return flicker == that.flicker &&
trail == that.trail &&
type == that.type &&
Objects.equals(color, that.color) &&
Objects.equals(fadeColor, that.fadeColor);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hash(flicker, trail, type, color, fadeColor);
public @NotNull NBTCompound asCompound() {
return NBT.Compound(Map.of(
"Flicker", NBT.Boolean(flicker),
"Trail", NBT.Boolean(trail),
"Type", NBT.Byte(type.getType()),
"Colors", NBT.IntArray(colors.stream().mapToInt(Color::asRGB).toArray()),
"FadeColors", NBT.IntArray(fadeColors.stream().mapToInt(Color::asRGB).toArray())));
}
}

View File

@ -7,13 +7,11 @@ import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap;
* An enumeration that representing all available firework types.
*/
public enum FireworkEffectType {
SMALL_BALL((byte) 0),
LARGE_BALL((byte) 1),
STAR_SHAPED((byte) 2),
CREEPER_SHAPED((byte) 3),
BURST((byte) 4),
;
BURST((byte) 4);
private static final Byte2ObjectMap<FireworkEffectType> BY_ID = new Byte2ObjectOpenHashMap<>();
@ -47,6 +45,5 @@ public enum FireworkEffectType {
public byte getType() {
return type;
}
}

View File

@ -15,6 +15,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTType;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider<PotionMeta.Builder> {
@ -62,14 +63,13 @@ public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider<Pot
NBTList<NBTCompound> potionList = NBT.List(
NBTType.TAG_Compound,
customPotionEffects.stream()
.map(customPotionEffect -> NBT.Compound(potionCompound -> {
potionCompound.setByte("Id", customPotionEffect.getId());
potionCompound.setByte("Amplifier", customPotionEffect.getAmplifier());
potionCompound.setInt("Duration", customPotionEffect.getDuration());
potionCompound.setByte("Ambient", (byte) (customPotionEffect.isAmbient() ? 1 : 0));
potionCompound.setByte("ShowParticles", (byte) (customPotionEffect.showParticles() ? 1 : 0));
potionCompound.setByte("ShowIcon", (byte) (customPotionEffect.showIcon() ? 1 : 0));
}))
.map(customPotionEffect -> NBT.Compound(Map.of(
"Id", NBT.Byte(customPotionEffect.getId()),
"Amplifier", NBT.Byte(customPotionEffect.getAmplifier()),
"Duration", NBT.Int(customPotionEffect.getDuration()),
"Ambient", NBT.Boolean(customPotionEffect.isAmbient()),
"ShowParticles", NBT.Boolean(customPotionEffect.showParticles()),
"ShowIcon", NBT.Boolean(customPotionEffect.showIcon()))))
.toList()
);
mutateNbt(compound -> compound.set("CustomPotionEffects", potionList));

View File

@ -56,8 +56,7 @@ public class WritableBookMeta extends ItemMeta implements ItemMetaBuilder.Provid
public Builder title(@Nullable String title) {
this.title = title;
handleNullable(title, "title",
() -> new NBTString(Objects.requireNonNull(title)));
handleNullable(title, "title", () -> NBT.String(Objects.requireNonNull(title)));
return this;
}