use of com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_11 method handleItemToClient.
@Override
public Item handleItemToClient(Item item) {
if (item == null)
return null;
super.handleItemToClient(item);
CompoundTag tag = item.tag();
if (tag == null)
return item;
// Rewrite spawn eggs (id checks are done in the method itself)
EntityIdRewriter.toClientItem(item, true);
if (tag.get("ench") instanceof ListTag) {
enchantmentRewriter.rewriteEnchantmentsToClient(tag, false);
}
if (tag.get("StoredEnchantments") instanceof ListTag) {
enchantmentRewriter.rewriteEnchantmentsToClient(tag, true);
}
return item;
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_11 method handleItemToServer.
@Override
public Item handleItemToServer(Item item) {
if (item == null)
return null;
super.handleItemToServer(item);
CompoundTag tag = item.tag();
if (tag == null)
return item;
// Rewrite spawn eggs (id checks are done in the method itself)
EntityIdRewriter.toServerItem(item, true);
if (tag.contains(nbtTagName + "|ench")) {
enchantmentRewriter.rewriteEnchantmentsToServer(tag, false);
}
if (tag.contains(nbtTagName + "|StoredEnchantments")) {
enchantmentRewriter.rewriteEnchantmentsToServer(tag, true);
}
return item;
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_13 method handleItemToServer.
@Override
public Item handleItemToServer(Item item) {
if (item == null)
return null;
CompoundTag tag = item.tag();
// Save original id
int originalId = (item.identifier() << 16 | item.data() & 0xFFFF);
int rawId = (item.identifier() << 4 | item.data() & 0xF);
// NBT Additions
if (isDamageable(item.identifier())) {
if (tag == null)
item.setTag(tag = new CompoundTag());
tag.put("Damage", new IntTag(item.data()));
}
if (item.identifier() == 358) {
// map
if (tag == null)
item.setTag(tag = new CompoundTag());
tag.put("map", new IntTag(item.data()));
}
// NBT Changes
if (tag != null) {
// Shield and banner
invertShieldAndBannerId(item, tag);
// Display Name now uses JSON
Tag display = tag.get("display");
if (display instanceof CompoundTag) {
CompoundTag displayTag = (CompoundTag) display;
StringTag name = displayTag.get("Name");
if (name != null) {
StringTag via = displayTag.remove(extraNbtTag + "|Name");
name.setValue(via != null ? via.getValue() : ChatRewriter.legacyTextToJsonString(name.getValue()));
}
}
// ench is now Enchantments and now uses identifiers
rewriteEnchantmentsToServer(tag, false);
rewriteEnchantmentsToServer(tag, true);
rewriteCanPlaceToServer(tag, "CanPlaceOn");
rewriteCanPlaceToServer(tag, "CanDestroy");
// Handle SpawnEggs
if (item.identifier() == 383) {
CompoundTag entityTag = tag.get("EntityTag");
StringTag identifier;
if (entityTag != null && (identifier = entityTag.get("id")) != null) {
rawId = SpawnEggRewriter.getSpawnEggId(identifier.getValue());
if (rawId == -1) {
// Bat fallback
rawId = 25100288;
} else {
entityTag.remove("id");
if (entityTag.isEmpty()) {
tag.remove("EntityTag");
}
}
} else {
// Fallback to bat
rawId = 25100288;
}
}
if (tag.isEmpty()) {
item.setTag(tag = null);
}
}
// Handle custom mappings
int identifier = item.identifier();
item.setIdentifier(rawId);
super.handleItemToServer(item);
// Mapped with original data, we can return here
if (item.identifier() != rawId && item.identifier() != -1)
return item;
// Set to legacy id again
item.setIdentifier(identifier);
int newId = -1;
if (!protocol.getMappingData().getItemMappings().inverse().containsKey(rawId)) {
if (!isDamageable(item.identifier()) && item.identifier() != 358) {
// Map
if (tag == null)
item.setTag(tag = new CompoundTag());
// Data will be lost, saving original id
tag.put(extraNbtTag, new IntTag(originalId));
}
if (item.identifier() == 229) {
// purple shulker box
// directly set the new id -> base/colorless shulker box
newId = 362;
} else if (item.identifier() == 31 && item.data() == 0) {
// Shrub was removed
// Dead Bush
rawId = 32 << 4;
} else if (protocol.getMappingData().getItemMappings().inverse().containsKey(rawId & ~0xF)) {
// Remove data
rawId &= ~0xF;
} else {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
ViaBackwards.getPlatform().getLogger().warning("Failed to get 1.13 item for " + item.identifier());
}
// Stone
rawId = 16;
}
}
if (newId == -1) {
newId = protocol.getMappingData().getItemMappings().inverse().get(rawId);
}
item.setIdentifier(newId);
item.setData((short) 0);
return item;
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_13 method registerPackets.
@Override
protected void registerPackets() {
protocol.registerClientbound(ClientboundPackets1_13.COOLDOWN, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int itemId = wrapper.read(Type.VAR_INT);
int oldId = protocol.getMappingData().getItemMappings().get(itemId);
if (oldId != -1) {
Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId);
if (eggEntityId.isPresent()) {
itemId = 383 << 16;
} else {
itemId = (oldId >> 4) << 16 | oldId & 0xF;
}
}
wrapper.write(Type.VAR_INT, itemId);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ACTION, new PacketRemapper() {
@Override
public void registerMap() {
// Location
map(Type.POSITION);
// Action Id
map(Type.UNSIGNED_BYTE);
// Action param
map(Type.UNSIGNED_BYTE);
// Block Id - /!\ NOT BLOCK STATE ID
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int blockId = wrapper.get(Type.VAR_INT, 0);
if (blockId == 73)
blockId = 25;
else if (blockId == 99)
blockId = 33;
else if (blockId == 92)
blockId = 29;
else if (blockId == 142)
blockId = 54;
else if (blockId == 305)
blockId = 146;
else if (blockId == 249)
blockId = 130;
else if (blockId == 257)
blockId = 138;
else if (blockId == 140)
blockId = 52;
else if (blockId == 472)
blockId = 209;
else if (blockId >= 483 && blockId <= 498)
blockId = blockId - 483 + 219;
wrapper.set(Type.VAR_INT, 0, blockId);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Position
map(Type.POSITION);
// 1 - Action
map(Type.UNSIGNED_BYTE);
// 2 - NBT Data
map(Type.NBT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
BackwardsBlockEntityProvider provider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class);
// TODO conduit handling
if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 5) {
wrapper.cancel();
}
wrapper.set(Type.NBT, 0, provider.transform(wrapper.user(), wrapper.get(Type.POSITION, 0), wrapper.get(Type.NBT, 0)));
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.UNLOAD_CHUNK, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int chunkMinX = wrapper.passthrough(Type.INT) << 4;
int chunkMinZ = wrapper.passthrough(Type.INT) << 4;
int chunkMaxX = chunkMinX + 15;
int chunkMaxZ = chunkMinZ + 15;
BackwardsBlockStorage blockStorage = wrapper.user().get(BackwardsBlockStorage.class);
blockStorage.getBlocks().entrySet().removeIf(entry -> {
Position position = entry.getKey();
return position.getX() >= chunkMinX && position.getZ() >= chunkMinZ && position.getX() <= chunkMaxX && position.getZ() <= chunkMaxZ;
});
}
});
}
});
// Block Change
protocol.registerClientbound(ClientboundPackets1_13.BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Position
map(Type.POSITION);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int blockState = wrapper.read(Type.VAR_INT);
Position position = wrapper.get(Type.POSITION, 0);
// Store blocks
BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class);
storage.checkAndStore(position, blockState);
wrapper.write(Type.VAR_INT, protocol.getMappingData().getNewBlockStateId(blockState));
// Flower pot special treatment
flowerPotSpecialTreatment(wrapper.user(), blockState, position);
}
});
}
});
// Multi Block Change
protocol.registerClientbound(ClientboundPackets1_13.MULTI_BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Chunk X
map(Type.INT);
// 1 - Chunk Z
map(Type.INT);
map(Type.BLOCK_CHANGE_RECORD_ARRAY);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class);
for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
int chunkX = wrapper.get(Type.INT, 0);
int chunkZ = wrapper.get(Type.INT, 1);
int block = record.getBlockId();
Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
// Store if needed
storage.checkAndStore(position, block);
// Flower pot special treatment
flowerPotSpecialTreatment(wrapper.user(), block, position);
// Change to old id
record.setBlockId(protocol.getMappingData().getNewBlockStateId(block));
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.WINDOW_ITEMS, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE);
map(Type.FLAT_ITEM_ARRAY, Type.ITEM_ARRAY);
handler(itemArrayHandler(Type.ITEM_ARRAY));
}
});
protocol.registerClientbound(ClientboundPackets1_13.SET_SLOT, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE);
map(Type.SHORT);
map(Type.FLAT_ITEM, Type.ITEM);
handler(itemToClientHandler(Type.ITEM));
}
});
protocol.registerClientbound(ClientboundPackets1_13.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
Chunk1_9_3_4Type type_old = new Chunk1_9_3_4Type(clientWorld);
Chunk1_13Type type = new Chunk1_13Type(clientWorld);
Chunk chunk = wrapper.read(type);
// Handle Block Entities before block rewrite
BackwardsBlockEntityProvider provider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class);
BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class);
for (CompoundTag tag : chunk.getBlockEntities()) {
Tag idTag = tag.get("id");
if (idTag == null)
continue;
String id = (String) idTag.getValue();
// Ignore if we don't handle it
if (!provider.isHandled(id))
continue;
int sectionIndex = ((NumberTag) tag.get("y")).asInt() >> 4;
if (sectionIndex < 0 || sectionIndex > 15) {
// 1.17 chunks
continue;
}
ChunkSection section = chunk.getSections()[sectionIndex];
int x = ((NumberTag) tag.get("x")).asInt();
int y = ((NumberTag) tag.get("y")).asInt();
int z = ((NumberTag) tag.get("z")).asInt();
Position position = new Position(x, (short) y, z);
int block = section.getFlatBlock(x & 0xF, y & 0xF, z & 0xF);
storage.checkAndStore(position, block);
provider.transform(wrapper.user(), position, tag);
}
// Rewrite new blocks to old blocks
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null) {
continue;
}
// Flower pots require a special treatment, they are no longer block entities :(
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
int block = section.getFlatBlock(x, y, z);
// Check if the block is a flower
if (FlowerPotHandler.isFlowah(block)) {
Position pos = new Position((x + (chunk.getX() << 4)), (short) (y + (i << 4)), (z + (chunk.getZ() << 4)));
// Store block
storage.checkAndStore(pos, block);
CompoundTag nbt = provider.transform(wrapper.user(), pos, "minecraft:flower_pot");
chunk.getBlockEntities().add(nbt);
}
}
}
}
for (int p = 0; p < section.getPaletteSize(); p++) {
int old = section.getPaletteEntry(p);
if (old != 0) {
int oldId = protocol.getMappingData().getNewBlockStateId(old);
section.setPaletteEntry(p, oldId);
}
}
}
if (chunk.isBiomeData()) {
for (int i = 0; i < 256; i++) {
int biome = chunk.getBiomeData()[i];
int newId = -1;
switch(biome) {
// end biomes
case 40:
case 41:
case 42:
case 43:
newId = 9;
break;
// deep ocean biomes
case 47:
case 48:
case 49:
newId = 24;
break;
case // deep frozen... let's just pick the frozen variant
50:
newId = 10;
break;
// the other new ocean biomes
case 44:
case 45:
case 46:
newId = 0;
break;
}
if (newId != -1) {
chunk.getBiomeData()[i] = newId;
}
}
}
wrapper.write(type_old, chunk);
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.EFFECT, new PacketRemapper() {
@Override
public void registerMap() {
// Effect Id
map(Type.INT);
// Location
map(Type.POSITION);
// Data
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.INT, 0);
int data = wrapper.get(Type.INT, 1);
if (id == 1010) {
// Play record
wrapper.set(Type.INT, 1, protocol.getMappingData().getItemMappings().get(data) >> 4);
} else if (id == 2001) {
// Block break + block break sound
data = protocol.getMappingData().getNewBlockStateId(data);
int blockId = data >> 4;
int blockData = data & 0xF;
wrapper.set(Type.INT, 1, (blockId & 0xFFF) | (blockData << 12));
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.MAP_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.BYTE);
map(Type.BOOLEAN);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int iconCount = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < iconCount; i++) {
int type = wrapper.read(Type.VAR_INT);
byte x = wrapper.read(Type.BYTE);
byte z = wrapper.read(Type.BYTE);
byte direction = wrapper.read(Type.BYTE);
if (wrapper.read(Type.BOOLEAN)) {
wrapper.read(Type.COMPONENT);
}
if (type > 9) {
wrapper.set(Type.VAR_INT, 1, wrapper.get(Type.VAR_INT, 1) - 1);
continue;
}
wrapper.write(Type.BYTE, (byte) ((type << 4) | (direction & 0x0F)));
wrapper.write(Type.BYTE, x);
wrapper.write(Type.BYTE, z);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.ENTITY_EQUIPMENT, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.VAR_INT);
map(Type.FLAT_ITEM, Type.ITEM);
handler(itemToClientHandler(Type.ITEM));
}
});
protocol.registerClientbound(ClientboundPackets1_13.WINDOW_PROPERTY, new PacketRemapper() {
@Override
public void registerMap() {
// Window Id
map(Type.UNSIGNED_BYTE);
// Property
map(Type.SHORT);
// Value
map(Type.SHORT);
handler(wrapper -> {
short property = wrapper.get(Type.SHORT, 0);
// Enchantment table
if (property >= 4 && property <= 6) {
short oldId = wrapper.get(Type.SHORT, 1);
wrapper.set(Type.SHORT, 1, (short) protocol.getMappingData().getEnchantmentMappings().getNewId(oldId));
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_12_1.CREATIVE_INVENTORY_ACTION, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.SHORT);
map(Type.ITEM, Type.FLAT_ITEM);
handler(itemToServerHandler(Type.FLAT_ITEM));
}
});
protocol.registerServerbound(ServerboundPackets1_12_1.CLICK_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE);
map(Type.SHORT);
map(Type.BYTE);
map(Type.SHORT);
map(Type.VAR_INT);
map(Type.ITEM, Type.FLAT_ITEM);
handler(itemToServerHandler(Type.FLAT_ITEM));
}
});
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_13 method flowerPotSpecialTreatment.
// TODO find a less hacky way to do this (https://bugs.mojang.com/browse/MC-74231)
private static void flowerPotSpecialTreatment(UserConnection user, int blockState, Position position) throws Exception {
if (FlowerPotHandler.isFlowah(blockState)) {
BackwardsBlockEntityProvider beProvider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class);
CompoundTag nbt = beProvider.transform(user, position, "minecraft:flower_pot");
// Remove the flowerpot
PacketWrapper blockUpdateRemove = PacketWrapper.create(0x0B, null, user);
blockUpdateRemove.write(Type.POSITION, position);
blockUpdateRemove.write(Type.VAR_INT, 0);
blockUpdateRemove.scheduleSend(Protocol1_12_2To1_13.class);
// Create the flowerpot
PacketWrapper blockCreate = PacketWrapper.create(0x0B, null, user);
blockCreate.write(Type.POSITION, position);
blockCreate.write(Type.VAR_INT, Protocol1_12_2To1_13.MAPPINGS.getNewBlockStateId(blockState));
blockCreate.scheduleSend(Protocol1_12_2To1_13.class);
// Send a block entity update
PacketWrapper wrapper = PacketWrapper.create(0x09, null, user);
wrapper.write(Type.POSITION, position);
wrapper.write(Type.UNSIGNED_BYTE, (short) 5);
wrapper.write(Type.NBT, nbt);
wrapper.scheduleSend(Protocol1_12_2To1_13.class);
}
}
Aggregations