Skip to content

Commit

Permalink
Use NBT lib version 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SupremeMortal committed Jul 4, 2020
1 parent b919922 commit 9ab29a1
Show file tree
Hide file tree
Showing 35 changed files with 133 additions and 390 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import com.nukkitx.math.vector.Vector2f;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.nbt.NBTInputStream;
import com.nukkitx.nbt.NBTOutputStream;
import com.nukkitx.nbt.NbtUtils;
import com.nukkitx.nbt.stream.NBTInputStream;
import com.nukkitx.nbt.stream.NBTOutputStream;
import com.nukkitx.nbt.tag.Tag;
import com.nukkitx.network.VarInts;
import com.nukkitx.network.util.Preconditions;
import com.nukkitx.protocol.bedrock.data.GameRuleData;
Expand Down Expand Up @@ -496,17 +495,18 @@ public <T> void writeArrayShortLE(ByteBuf buffer, Collection<T> array, BiConsume
}
}

public Tag<?> readTag(ByteBuf buffer) {
@SuppressWarnings("unchecked")
public <T> T readTag(ByteBuf buffer) {
try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
return reader.readTag();
return (T) reader.readTag();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void writeTag(ByteBuf buffer, Tag<?> tag) {
public <T> void writeTag(ByteBuf buffer, T tag) {
try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
writer.write(tag);
writer.writeTag(tag);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -167,7 +167,7 @@ public static Type from(Object o) {
return EntityData.Type.FLOAT;
} else if (o instanceof String) {
return EntityData.Type.STRING;
} else if (o instanceof ItemData || o instanceof CompoundTag) {
} else if (o instanceof ItemData || o instanceof NbtMap) {
return EntityData.Type.NBT;
} else if (o instanceof Vector3i) {
return EntityData.Type.VECTOR3I;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.nbt.NbtMap;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -81,15 +81,15 @@ public EntityDataMap putString(EntityData key, String value) {
return this;
}

public CompoundTag getTag(EntityData key) {
return getTag(key, CompoundTag.EMPTY);
public NbtMap getTag(EntityData key) {
return getTag(key, NbtMap.EMPTY);
}

public CompoundTag getTag(EntityData key, CompoundTag defaultValue) {
public NbtMap getTag(EntityData key, NbtMap defaultValue) {
return getOrDefault(key, defaultValue);
}

public EntityDataMap putTag(EntityData key, CompoundTag value) {
public EntityDataMap putTag(EntityData key, NbtMap value) {
this.put(key, value);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nukkitx.protocol.bedrock.data.inventory;

import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.network.util.Preconditions;
import lombok.AccessLevel;
import lombok.Data;
Expand All @@ -21,7 +21,7 @@ public final class ItemData {
private final int id;
private final short damage;
private final int count;
private final CompoundTag tag;
private final NbtMap tag;
private final String[] canPlace;
private final String[] canBreak;
private final long blockingTicks;
Expand All @@ -32,31 +32,31 @@ public static ItemData of(int id, short damage, int count) {
return of(id, damage, count, null);
}

public static ItemData of(int id, short damage, int count, CompoundTag tag) {
public static ItemData of(int id, short damage, int count, NbtMap tag) {
return fromNet(1, id, damage, count, tag, EMPTY, EMPTY);
}

public static ItemData of(int id, short damage, int count, CompoundTag tag, String[] canPlace, String[] canBreak) {
public static ItemData of(int id, short damage, int count, NbtMap tag, String[] canPlace, String[] canBreak) {
return fromNet(1, id, damage, count, tag, canPlace, canBreak, 0);
}

public static ItemData of(int id, short damage, int count, CompoundTag tag, String[] canPlace, String[] canBreak, long blockingTicks) {
public static ItemData of(int id, short damage, int count, NbtMap tag, String[] canPlace, String[] canBreak, long blockingTicks) {
return fromNet(1, id, damage, count, tag, canPlace, canBreak, blockingTicks);
}

public static ItemData fromNet(int netId, int id, short damage, int count) {
return fromNet(netId, id, damage, count, null);
}

public static ItemData fromNet(int netId, int id, short damage, int count, CompoundTag tag) {
public static ItemData fromNet(int netId, int id, short damage, int count, NbtMap tag) {
return fromNet(netId, id, damage, count, tag, EMPTY, EMPTY);
}

public static ItemData fromNet(int netId, int id, short damage, int count, CompoundTag tag, String[] canPlace, String[] canBreak) {
public static ItemData fromNet(int netId, int id, short damage, int count, NbtMap tag, String[] canPlace, String[] canBreak) {
return fromNet(netId, id, damage, count, tag, canPlace, canBreak, 0);
}

public static ItemData fromNet(int netId, int id, short damage, int count, CompoundTag tag, String[] canPlace, String[] canBreak, long blockingTicks) {
public static ItemData fromNet(int netId, int id, short damage, int count, NbtMap tag, String[] canPlace, String[] canBreak, long blockingTicks) {
if (id == 0) {
return AIR;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nukkitx.protocol.bedrock.packet;

import com.nukkitx.nbt.tag.Tag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
Expand All @@ -10,7 +10,7 @@
@Data
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AvailableEntityIdentifiersPacket extends BedrockPacket {
private Tag<?> tag;
private NbtMap identifiers;

@Override
public boolean handle(BedrockPacketHandler handler) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nukkitx.protocol.bedrock.packet;

import com.nukkitx.nbt.tag.Tag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
Expand All @@ -10,7 +10,7 @@
@Data
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class BiomeDefinitionListPacket extends BedrockPacket {
private Tag<?> tag;
private NbtMap definitions;

@Override
public boolean handle(BedrockPacketHandler handler) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.nukkitx.protocol.bedrock.packet;

import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.nbt.tag.Tag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
Expand All @@ -12,7 +12,7 @@
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class BlockEntityDataPacket extends BedrockPacket {
private Vector3i blockPosition;
private Tag<?> data;
private NbtMap data;

@Override
public final boolean handle(BedrockPacketHandler handler) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nukkitx.protocol.bedrock.packet;

import com.nukkitx.nbt.tag.Tag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
Expand All @@ -11,7 +11,7 @@
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class LevelEventGenericPacket extends BedrockPacket {
private int eventId;
private Tag<?> tag;
private NbtMap tag;

@Override
public boolean handle(BedrockPacketHandler handler) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nukkitx.protocol.bedrock.packet;

import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
Expand All @@ -12,7 +12,7 @@
public class PositionTrackingDBServerBroadcastPacket extends BedrockPacket {
private Action action;
private int trackingId;
private CompoundTag tag;
private NbtMap tag;

@Override
public boolean handle(BedrockPacketHandler handler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import com.nukkitx.math.vector.Vector2f;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.nbt.tag.ListTag;
import com.nukkitx.nbt.NbtList;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.data.GamePublishSetting;
import com.nukkitx.protocol.bedrock.data.GameRuleData;
import com.nukkitx.protocol.bedrock.data.GameType;
import com.nukkitx.protocol.bedrock.data.PlayerPermission;
import com.nukkitx.protocol.bedrock.data.SpawnBiomeType;
import com.nukkitx.protocol.bedrock.data.*;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
Expand Down Expand Up @@ -83,7 +79,7 @@ public class StartGamePacket extends BedrockPacket {
private boolean movementServerAuthoritative;
private long currentTick;
private int enchantmentSeed;
private ListTag<CompoundTag> blockPalette;
private NbtList<NbtMap> blockPalette;
private List<ItemEntry> itemEntries = new ObjectArrayList<>();
private String multiplayerCorrelationId;
private boolean inventoriesServerAuthoritative;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nukkitx.protocol.bedrock.packet;

import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.data.structure.StructureTemplateResponseType;
Expand All @@ -13,7 +13,7 @@
public class StructureTemplateDataResponsePacket extends BedrockPacket {
private String name;
private boolean save;
private CompoundTag tag;
private NbtMap tag;
private StructureTemplateResponseType type;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nukkitx.protocol.bedrock.packet;

import com.nukkitx.nbt.tag.Tag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
Expand All @@ -10,7 +10,7 @@
@Data
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class UpdateBlockPropertiesPacket extends BedrockPacket {
private Tag<?> properties;
private NbtMap properties;

@Override
public boolean handle(BedrockPacketHandler handler) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nukkitx.protocol.bedrock.packet;

import com.nukkitx.nbt.tag.Tag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
Expand All @@ -14,7 +14,7 @@ public class UpdateEquipPacket extends BedrockPacket {
private short windowType;
private int size; // Couldn't find anything on this one. Looks like it isn't used?
private long uniqueEntityId;
private Tag<?> tag;
private NbtMap tag;

@Override
public final boolean handle(BedrockPacketHandler handler) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nukkitx.protocol.bedrock.packet;

import com.nukkitx.nbt.tag.Tag;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
Expand All @@ -18,7 +18,7 @@ public class UpdateTradePacket extends BedrockPacket {
private long traderUniqueEntityId;
private long playerUniqueEntityId;
private String displayName;
private Tag<?> offers;
private NbtMap offers;
private boolean newTradingUi;
private boolean recipeAddedOnUpdate;
private boolean usingEconomyTrade;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.nbt.NBTInputStream;
import com.nukkitx.nbt.NBTOutputStream;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtUtils;
import com.nukkitx.nbt.stream.NBTInputStream;
import com.nukkitx.nbt.stream.NBTOutputStream;
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.nbt.tag.Tag;
import com.nukkitx.network.VarInts;
import com.nukkitx.network.util.Preconditions;
import com.nukkitx.protocol.bedrock.BedrockPacketHelper;
Expand Down Expand Up @@ -727,12 +726,12 @@ public ItemData readItem(ByteBuf buffer) {
int count = aux & 0xff;
short nbtSize = buffer.readShortLE();

CompoundTag compoundTag = null;
NbtMap compoundTag = null;
if (nbtSize > 0) {
try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
Tag<?> tag = reader.readTag();
if (tag instanceof CompoundTag) {
compoundTag = (CompoundTag) tag;
Object tag = reader.readTag();
if (tag instanceof NbtMap) {
compoundTag = (NbtMap) tag;
}
} catch (IOException e) {
throw new IllegalStateException("Unable to load NBT data", e);
Expand Down Expand Up @@ -771,7 +770,7 @@ public void writeItem(ByteBuf buffer, ItemData item) {
if (item.getTag() != null) {
int afterSizeIndex = buffer.writerIndex();
try (NBTOutputStream stream = new NBTOutputStream(new LittleEndianByteBufOutputStream(buffer))) {
stream.write(item.getTag());
stream.writeTag(item.getTag());
} catch (IOException e) {
// This shouldn't happen (as this is backed by a Netty ByteBuf), but okay...
throw new IllegalStateException("Unable to save NBT data", e);
Expand Down Expand Up @@ -944,8 +943,8 @@ public void writeEntityData(ByteBuf buffer, EntityDataMap entityDataMap) {
break;
case NBT:
ItemData item;
if (object instanceof CompoundTag) {
item = ItemData.of(1, (short) 0, 1, (CompoundTag) object);
if (object instanceof NbtMap) {
item = ItemData.of(1, (short) 0, 1, (NbtMap) object);
} else {
item = (ItemData) object;
}
Expand Down
Loading

0 comments on commit 9ab29a1

Please sign in to comment.