From 1a92f6974c97135f539b94c60fcd87f43e3d6038 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Thu, 28 May 2020 16:38:23 +0100 Subject: [PATCH] Add byte conversion to allow int NBT values for Fireworks (#681) Firework NBT data might be either an int or byte and bedrock only takes it as a byte. Co-authored-by: Arktisfox <65837019+Arktisfox@users.noreply.github.com> --- .../connector/entity/FireworkEntity.java | 9 ++++---- .../translators/nbt/FireworkTranslator.java | 22 +++++++++++++------ .../geysermc/connector/utils/MathUtils.java | 15 +++++++++++++ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/entity/FireworkEntity.java b/connector/src/main/java/org/geysermc/connector/entity/FireworkEntity.java index cb050e860..1db4f757a 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/FireworkEntity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/FireworkEntity.java @@ -37,6 +37,7 @@ import com.nukkitx.protocol.bedrock.packet.SetEntityMotionPacket; import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.utils.FireworkColor; +import org.geysermc.connector.utils.MathUtils; import java.util.ArrayList; import java.util.List; @@ -63,7 +64,7 @@ public class FireworkEntity extends Entity { CompoundTagBuilder fireworksBuilder = CompoundTagBuilder.builder(); if (fireworks.get("Flight") != null) { - fireworksBuilder.byteTag("Flight", (Byte) fireworks.get("Flight").getValue()); + fireworksBuilder.byteTag("Flight", MathUtils.convertByte(fireworks.get("Flight").getValue())); } List explosions = new ArrayList<>(); @@ -73,7 +74,7 @@ public class FireworkEntity extends Entity { CompoundTagBuilder effectBuilder = CompoundTagBuilder.builder(); if (effectData.get("Type") != null) { - effectBuilder.byteTag("FireworkType", (Byte) effectData.get("Type").getValue()); + effectBuilder.byteTag("FireworkType", MathUtils.convertByte(effectData.get("Type").getValue())); } if (effectData.get("Colors") != null) { @@ -101,11 +102,11 @@ public class FireworkEntity extends Entity { } if (effectData.get("Trail") != null) { - effectBuilder.byteTag("FireworkTrail", (Byte) effectData.get("Trail").getValue()); + effectBuilder.byteTag("FireworkTrail", MathUtils.convertByte(effectData.get("Trail").getValue())); } if (effectData.get("Flicker") != null) { - effectBuilder.byteTag("FireworkFlicker", (Byte) effectData.get("Flicker").getValue()); + effectBuilder.byteTag("FireworkFlicker", MathUtils.convertByte(effectData.get("Flicker").getValue())); } explosions.add(effectBuilder.buildRootTag()); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/FireworkTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/FireworkTranslator.java index 268fe3932..5b5182a5b 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/FireworkTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/FireworkTranslator.java @@ -27,9 +27,10 @@ package org.geysermc.connector.network.translators.item.translators.nbt; import com.github.steveice10.opennbt.tag.builtin.*; import org.geysermc.connector.network.translators.ItemRemapper; -import org.geysermc.connector.network.translators.item.NbtItemStackTranslator; import org.geysermc.connector.network.translators.item.ItemEntry; +import org.geysermc.connector.network.translators.item.NbtItemStackTranslator; import org.geysermc.connector.utils.FireworkColor; +import org.geysermc.connector.utils.MathUtils; @ItemRemapper public class FireworkTranslator extends NbtItemStackTranslator { @@ -41,6 +42,10 @@ public class FireworkTranslator extends NbtItemStackTranslator { } CompoundTag fireworks = itemTag.get("Fireworks"); + if (fireworks.get("Flight") != null) { + fireworks.put(new ByteTag("Flight", MathUtils.convertByte(fireworks.get("Flight").getValue()))); + } + ListTag explosions = fireworks.get("Explosions"); if (explosions == null) { return; @@ -51,7 +56,7 @@ public class FireworkTranslator extends NbtItemStackTranslator { CompoundTag newEffectData = new CompoundTag(""); if (effectData.get("Type") != null) { - newEffectData.put(new ByteTag("FireworkType", (Byte) effectData.get("Type").getValue())); + newEffectData.put(new ByteTag("FireworkType", MathUtils.convertByte(effectData.get("Type").getValue()))); } if (effectData.get("Colors") != null) { @@ -79,11 +84,11 @@ public class FireworkTranslator extends NbtItemStackTranslator { } if (effectData.get("Trail") != null) { - newEffectData.put(new ByteTag("FireworkTrail", (Byte) effectData.get("Trail").getValue())); + newEffectData.put(new ByteTag("FireworkTrail", MathUtils.convertByte(effectData.get("Trail").getValue()))); } if (effectData.get("Flicker") != null) { - newEffectData.put(new ByteTag("FireworkFlicker", (Byte) effectData.get("Flicker").getValue())); + newEffectData.put(new ByteTag("FireworkFlicker", MathUtils.convertByte(effectData.get("Flicker").getValue()))); } explosions.remove(effect); @@ -94,6 +99,9 @@ public class FireworkTranslator extends NbtItemStackTranslator { @Override public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) { CompoundTag fireworks = itemTag.get("Fireworks"); + if (fireworks.get("Flight") != null) { + fireworks.put(new ByteTag("Flight", MathUtils.convertByte(fireworks.get("Flight").getValue()))); + } ListTag explosions = fireworks.get("Explosions"); for (Tag effect : explosions.getValue()) { @@ -102,7 +110,7 @@ public class FireworkTranslator extends NbtItemStackTranslator { CompoundTag newEffectData = new CompoundTag(""); if (effectData.get("FireworkType") != null) { - newEffectData.put(new ByteTag("Type", (Byte) effectData.get("FireworkType").getValue())); + newEffectData.put(new ByteTag("Type", MathUtils.convertByte(effectData.get("FireworkType").getValue()))); } if (effectData.get("FireworkColor") != null) { @@ -130,11 +138,11 @@ public class FireworkTranslator extends NbtItemStackTranslator { } if (effectData.get("FireworkTrail") != null) { - newEffectData.put(new ByteTag("Trail", (Byte) effectData.get("FireworkTrail").getValue())); + newEffectData.put(new ByteTag("Trail", MathUtils.convertByte(effectData.get("FireworkTrail").getValue()))); } if (effectData.get("FireworkFlicker") != null) { - newEffectData.put(new ByteTag("Flicker", (Byte) effectData.get("FireworkFlicker").getValue())); + newEffectData.put(new ByteTag("Flicker", MathUtils.convertByte(effectData.get("FireworkFlicker").getValue()))); } explosions.remove(effect); diff --git a/connector/src/main/java/org/geysermc/connector/utils/MathUtils.java b/connector/src/main/java/org/geysermc/connector/utils/MathUtils.java index 4e8497c6d..f66e869db 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/MathUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/MathUtils.java @@ -37,4 +37,19 @@ public class MathUtils { int truncated = (int) floatNumber; return floatNumber > truncated ? truncated + 1 : truncated; } + + /** + * Converts the given object from an int or byte to byte. + * This is used for NBT data that might be either an int + * or byte and bedrock only takes it as an byte + * + * @param value The value to convert + * @return The converted byte + */ + public static Byte convertByte(Object value) { + if (value instanceof Integer) { + return ((Integer) value).byteValue(); + } + return (Byte) value; + } }