Added support for CustomPotionEffect in PotionMeta

This commit is contained in:
themode 2020-10-16 16:32:57 +02:00
parent 512b30e9df
commit 0b6a063311
5 changed files with 109 additions and 8 deletions

View File

@ -17,8 +17,6 @@ public final class ChatParser {
* @return a {@link ColoredText} representing the text
*/
public static ColoredText toColoredText(String json) {
json = json.replace("\\\"", "\"");
StringBuilder builder = new StringBuilder();
try {
@ -30,7 +28,7 @@ public final class ChatParser {
} else if (element instanceof JsonArray) {
final JsonArray array = element.getAsJsonArray();
for (JsonElement e : array) {
JsonObject object = e.getAsJsonObject();
final JsonObject object = e.getAsJsonObject();
appendBuilder(builder, object);
}
}
@ -47,9 +45,9 @@ public final class ChatParser {
final boolean hasExtra = object.has("extra");
if (hasExtra) {
JsonArray extraArray = object.get("extra").getAsJsonArray();
final JsonArray extraArray = object.get("extra").getAsJsonArray();
for (JsonElement extraElement : extraArray) {
JsonObject extraObject = extraElement.getAsJsonObject();
final JsonObject extraObject = extraElement.getAsJsonObject();
builder.append(parseText(extraObject));
}
}
@ -90,7 +88,7 @@ public final class ChatParser {
builder.append("{").append(colorString).append("}");
} else {
// Color simple name
ChatColor color = ChatColor.fromName(colorString);
final ChatColor color = ChatColor.fromName(colorString);
builder.append(color);
}
}

View File

@ -57,7 +57,8 @@ public class ArgumentItemStack extends Argument<ItemStack> {
ItemStack itemStack = new ItemStack(material, (byte) 1);
final String sNBT = value.substring(nbtIndex);
final String sNBT = value.substring(nbtIndex).replace("\\\"", "\"");
final NBTCompound compound = (NBTCompound) new SNBTParser(new StringReader(sNBT)).parse();
NBTUtils.loadDataIntoItem(itemStack, compound);

View File

@ -118,7 +118,7 @@ public class ItemStack implements DataContainer {
if (!nbt.containsKey("id") || !nbt.containsKey("Count"))
throw new IllegalArgumentException("Invalid item NBT, must at least contain 'id' and 'Count' tags");
final Material material = Registries.getMaterial(nbt.getString("id"));
final byte count = nbt.getByte("Count");
final byte count = nbt.getAsByte("Count");
ItemStack s = new ItemStack(material, count);

View File

@ -1,12 +1,26 @@
package net.minestom.server.item.metadata;
import net.minestom.server.potion.CustomPotionEffect;
import net.minestom.server.potion.PotionType;
import net.minestom.server.registry.Registries;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Item meta for
* {@link net.minestom.server.item.Material#POTION},
* {@link net.minestom.server.item.Material#LINGERING_POTION},
* {@link net.minestom.server.item.Material#SPLASH_POTION},
* {@link net.minestom.server.item.Material#TIPPED_ARROW}.
*/
public class PotionMeta implements ItemMeta {
private PotionType potionType;
private List<CustomPotionEffect> customPotionEffects = new CopyOnWriteArrayList<>();
/**
* Gets the potion type.
@ -26,6 +40,15 @@ public class PotionMeta implements ItemMeta {
this.potionType = potionType;
}
/**
* Get a list of {@link CustomPotionEffect}.
*
* @return the custom potion effects
*/
public List<CustomPotionEffect> getCustomPotionEffects() {
return customPotionEffects;
}
@Override
public boolean hasNbt() {
return potionType != null;
@ -41,6 +64,20 @@ public class PotionMeta implements ItemMeta {
if (compound.containsKey("Potion")) {
this.potionType = Registries.getPotionType(compound.getString("Potion"));
}
if (compound.containsKey("CustomPotionEffects")) {
NBTList<NBTCompound> customEffectList = compound.getList("CustomPotionEffects");
for (NBTCompound potionCompound : customEffectList) {
final byte id = potionCompound.getAsByte("Id");
final byte amplifier = potionCompound.getAsByte("Amplifier");
final int duration = potionCompound.getAsInt("Duration");
final boolean ambient = potionCompound.getAsByte("Ambient") == 1;
final boolean showParticles = potionCompound.getAsByte("ShowParticles") == 1;
final boolean showIcon = potionCompound.getAsByte("ShowIcon") == 1;
this.customPotionEffects.add(
new CustomPotionEffect(id, amplifier, duration, ambient, showParticles, showIcon));
}
}
}
@Override
@ -48,6 +85,23 @@ public class PotionMeta implements ItemMeta {
if (potionType != null) {
compound.setString("Potion", potionType.getNamespaceID());
}
if (!customPotionEffects.isEmpty()) {
NBTList<NBTCompound> potionList = new NBTList<>(NBTTypes.TAG_Compound);
for (CustomPotionEffect customPotionEffect : customPotionEffects) {
NBTCompound potionCompound = new NBTCompound();
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));
potionList.add(potionCompound);
}
compound.set("CustomPotionEffects", potionList);
}
}
@Override

View File

@ -0,0 +1,48 @@
package net.minestom.server.potion;
/**
* Represents a custom effect in {@link net.minestom.server.item.metadata.PotionMeta}.
*/
public class CustomPotionEffect {
private final byte id;
private final byte amplifier;
private final int duration;
private final boolean ambient;
private final boolean showParticles;
private final boolean showIcon;
public CustomPotionEffect(byte id, byte amplifier, int duration,
boolean ambient, boolean showParticles, boolean showIcon) {
this.id = id;
this.amplifier = amplifier;
this.duration = duration;
this.ambient = ambient;
this.showParticles = showParticles;
this.showIcon = showIcon;
}
public byte getId() {
return id;
}
public byte getAmplifier() {
return amplifier;
}
public int getDuration() {
return duration;
}
public boolean isAmbient() {
return ambient;
}
public boolean showParticles() {
return showParticles;
}
public boolean showIcon() {
return showIcon;
}
}