Skip to content

Commit

Permalink
Add byte conversion to allow int NBT values for Fireworks (GeyserMC#681)
Browse files Browse the repository at this point in the history
Firework NBT data might be either an int or byte and bedrock only takes it as a byte.

Co-authored-by: Arktisfox <[email protected]>
  • Loading branch information
rtm516 and Arktisfox authored May 28, 2020
1 parent 14fcd77 commit 1a92f69
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;
Expand All @@ -63,7 +64,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s

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<com.nukkitx.nbt.tag.CompoundTag> explosions = new ArrayList<>();
Expand All @@ -73,7 +74,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
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) {
Expand Down Expand Up @@ -101,11 +102,11 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@

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 {
Expand All @@ -41,6 +42,10 @@ public void translateToBedrock(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");
if (explosions == null) {
return;
Expand All @@ -51,7 +56,7 @@ public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
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) {
Expand Down Expand Up @@ -79,11 +84,11 @@ public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
}

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);
Expand All @@ -94,6 +99,9 @@ public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
@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()) {
Expand All @@ -102,7 +110,7 @@ public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
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) {
Expand Down Expand Up @@ -130,11 +138,11 @@ public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,19 @@ public static int ceil(float floatNumber) {
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;
}
}

0 comments on commit 1a92f69

Please sign in to comment.