Add more sanity checks for shulker boxes

Also properly implements reading byte tags from Java Edition.
This commit is contained in:
Camotoy 2021-09-18 10:35:49 -04:00
parent 4b05b74a9a
commit 58e00b2645
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
5 changed files with 21 additions and 23 deletions

View file

@ -81,7 +81,7 @@ public class FireworkEntity extends Entity {
NbtMapBuilder fireworksBuilder = NbtMap.builder(); NbtMapBuilder fireworksBuilder = NbtMap.builder();
if (fireworks.get("Flight") != null) { if (fireworks.get("Flight") != null) {
fireworksBuilder.putByte("Flight", MathUtils.convertByte(fireworks.get("Flight").getValue())); fireworksBuilder.putByte("Flight", MathUtils.getNbtByte(fireworks.get("Flight").getValue()));
} }
List<NbtMap> explosions = new ArrayList<>(); List<NbtMap> explosions = new ArrayList<>();
@ -91,7 +91,7 @@ public class FireworkEntity extends Entity {
NbtMapBuilder effectBuilder = NbtMap.builder(); NbtMapBuilder effectBuilder = NbtMap.builder();
if (effectData.get("Type") != null) { if (effectData.get("Type") != null) {
effectBuilder.putByte("FireworkType", MathUtils.convertByte(effectData.get("Type").getValue())); effectBuilder.putByte("FireworkType", MathUtils.getNbtByte(effectData.get("Type").getValue()));
} }
if (effectData.get("Colors") != null) { if (effectData.get("Colors") != null) {
@ -119,11 +119,11 @@ public class FireworkEntity extends Entity {
} }
if (effectData.get("Trail") != null) { if (effectData.get("Trail") != null) {
effectBuilder.putByte("FireworkTrail", MathUtils.convertByte(effectData.get("Trail").getValue())); effectBuilder.putByte("FireworkTrail", MathUtils.getNbtByte(effectData.get("Trail").getValue()));
} }
if (effectData.get("Flicker") != null) { if (effectData.get("Flicker") != null) {
effectBuilder.putByte("FireworkFlicker", MathUtils.convertByte(effectData.get("Flicker").getValue())); effectBuilder.putByte("FireworkFlicker", MathUtils.getNbtByte(effectData.get("Flicker").getValue()));
} }
explosions.add(effectBuilder.build()); explosions.add(effectBuilder.build());

View file

@ -42,7 +42,7 @@ public abstract class FireworkBaseTranslator extends NbtItemStackTranslator {
CompoundTag newExplosionData = new CompoundTag(newName); CompoundTag newExplosionData = new CompoundTag(newName);
if (explosion.get("Type") != null) { if (explosion.get("Type") != null) {
newExplosionData.put(new ByteTag("FireworkType", MathUtils.convertByte(explosion.get("Type").getValue()))); newExplosionData.put(new ByteTag("FireworkType", MathUtils.getNbtByte(explosion.get("Type").getValue())));
} }
if (explosion.get("Colors") != null) { if (explosion.get("Colors") != null) {
@ -70,11 +70,11 @@ public abstract class FireworkBaseTranslator extends NbtItemStackTranslator {
} }
if (explosion.get("Trail") != null) { if (explosion.get("Trail") != null) {
newExplosionData.put(new ByteTag("FireworkTrail", MathUtils.convertByte(explosion.get("Trail").getValue()))); newExplosionData.put(new ByteTag("FireworkTrail", MathUtils.getNbtByte(explosion.get("Trail").getValue())));
} }
if (explosion.get("Flicker") != null) { if (explosion.get("Flicker") != null) {
newExplosionData.put(new ByteTag("FireworkFlicker", MathUtils.convertByte(explosion.get("Flicker").getValue()))); newExplosionData.put(new ByteTag("FireworkFlicker", MathUtils.getNbtByte(explosion.get("Flicker").getValue())));
} }
return newExplosionData; return newExplosionData;
@ -84,7 +84,7 @@ public abstract class FireworkBaseTranslator extends NbtItemStackTranslator {
CompoundTag newExplosionData = new CompoundTag(newName); CompoundTag newExplosionData = new CompoundTag(newName);
if (explosion.get("FireworkType") != null) { if (explosion.get("FireworkType") != null) {
newExplosionData.put(new ByteTag("Type", MathUtils.convertByte(explosion.get("FireworkType").getValue()))); newExplosionData.put(new ByteTag("Type", MathUtils.getNbtByte(explosion.get("FireworkType").getValue())));
} }
if (explosion.get("FireworkColor") != null) { if (explosion.get("FireworkColor") != null) {
@ -112,11 +112,11 @@ public abstract class FireworkBaseTranslator extends NbtItemStackTranslator {
} }
if (explosion.get("FireworkTrail") != null) { if (explosion.get("FireworkTrail") != null) {
newExplosionData.put(new ByteTag("Trail", MathUtils.convertByte(explosion.get("FireworkTrail").getValue()))); newExplosionData.put(new ByteTag("Trail", MathUtils.getNbtByte(explosion.get("FireworkTrail").getValue())));
} }
if (explosion.get("FireworkFlicker") != null) { if (explosion.get("FireworkFlicker") != null) {
newExplosionData.put(new ByteTag("Flicker", MathUtils.convertByte(explosion.get("FireworkFlicker").getValue()))); newExplosionData.put(new ByteTag("Flicker", MathUtils.getNbtByte(explosion.get("FireworkFlicker").getValue())));
} }
return newExplosionData; return newExplosionData;

View file

@ -45,7 +45,7 @@ public class FireworkRocketTranslator extends FireworkBaseTranslator {
} }
if (fireworks.get("Flight") != null) { if (fireworks.get("Flight") != null) {
fireworks.put(new ByteTag("Flight", MathUtils.convertByte(fireworks.get("Flight").getValue()))); fireworks.put(new ByteTag("Flight", MathUtils.getNbtByte(fireworks.get("Flight").getValue())));
} }
ListTag explosions = fireworks.get("Explosions"); ListTag explosions = fireworks.get("Explosions");
@ -69,7 +69,7 @@ public class FireworkRocketTranslator extends FireworkBaseTranslator {
} }
if (fireworks.contains("Flight")) { if (fireworks.contains("Flight")) {
fireworks.put(new ByteTag("Flight", MathUtils.convertByte(fireworks.get("Flight").getValue()))); fireworks.put(new ByteTag("Flight", MathUtils.getNbtByte(fireworks.get("Flight").getValue())));
} }
ListTag explosions = fireworks.get("Explosions"); ListTag explosions = fireworks.get("Explosions");

View file

@ -25,11 +25,13 @@
package org.geysermc.connector.network.translators.item.translators.nbt; package org.geysermc.connector.network.translators.item.translators.nbt;
import com.github.steveice10.mc.protocol.data.game.Identifier;
import com.github.steveice10.opennbt.tag.builtin.*; import com.github.steveice10.opennbt.tag.builtin.*;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.ItemRemapper; import org.geysermc.connector.network.translators.ItemRemapper;
import org.geysermc.connector.network.translators.item.*; import org.geysermc.connector.network.translators.item.*;
import org.geysermc.connector.registry.type.ItemMapping; import org.geysermc.connector.registry.type.ItemMapping;
import org.geysermc.connector.utils.MathUtils;
@ItemRemapper @ItemRemapper
public class ShulkerBoxItemTranslator extends NbtItemStackTranslator { public class ShulkerBoxItemTranslator extends NbtItemStackTranslator {
@ -44,14 +46,14 @@ public class ShulkerBoxItemTranslator extends NbtItemStackTranslator {
for (Tag item : (ListTag) blockEntityTag.get("Items")) { for (Tag item : (ListTag) blockEntityTag.get("Items")) {
CompoundTag itemData = (CompoundTag) item; // Information about the item CompoundTag itemData = (CompoundTag) item; // Information about the item
CompoundTag boxItemTag = new CompoundTag(""); // Final item tag to add to the list CompoundTag boxItemTag = new CompoundTag(""); // Final item tag to add to the list
boxItemTag.put(new ByteTag("Slot", ((ByteTag) itemData.get("Slot")).getValue())); boxItemTag.put(new ByteTag("Slot", (byte) (MathUtils.getNbtByte(itemData.get("Slot").getValue()) & 255)));
boxItemTag.put(new ByteTag("WasPickedUp", (byte) 0)); // ??? boxItemTag.put(new ByteTag("WasPickedUp", (byte) 0)); // ???
ItemMapping boxMapping = session.getItemMappings().getMapping(((StringTag) itemData.get("id")).getValue()); ItemMapping boxMapping = session.getItemMappings().getMapping(Identifier.formalize(((StringTag) itemData.get("id")).getValue()));
boxItemTag.put(new StringTag("Name", boxMapping.getBedrockIdentifier())); boxItemTag.put(new StringTag("Name", boxMapping.getBedrockIdentifier()));
boxItemTag.put(new ShortTag("Damage", (short) boxMapping.getBedrockData())); boxItemTag.put(new ShortTag("Damage", (short) boxMapping.getBedrockData()));
boxItemTag.put(new ByteTag("Count", ((ByteTag) itemData.get("Count")).getValue())); boxItemTag.put(new ByteTag("Count", MathUtils.getNbtByte(itemData.get("Count").getValue())));
// Only the display name is what we have interest in, so just translate that if relevant // Only the display name is what we have interest in, so just translate that if relevant
CompoundTag displayTag = itemData.get("tag"); CompoundTag displayTag = itemData.get("tag");
if (displayTag == null && boxMapping.hasTranslation()) { if (displayTag == null && boxMapping.hasTranslation()) {

View file

@ -85,18 +85,14 @@ public class MathUtils {
} }
/** /**
* Converts the given object from an int or byte to byte. * Ensures the resulting object is a byte. Java Edition does not care whether a byte is encoded as an integer or not;
* This is used for NBT data that might be either an int * it converts it into a byte anyway.
* or byte and bedrock only takes it as an byte
* *
* @param value The value to convert * @param value The value to convert
* @return The converted byte * @return The converted byte
*/ */
public static Byte convertByte(Object value) { public static byte getNbtByte(Object value) {
if (value instanceof Integer) { return ((Number) value).byteValue();
return ((Integer) value).byteValue();
}
return (Byte) value;
} }
/** /**