use of com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_18 method registerPackets.
@Override
protected void registerPackets() {
new RecipeRewriter1_16(protocol).registerDefaultHandler(ClientboundPackets1_18.DECLARE_RECIPES);
registerSetCooldown(ClientboundPackets1_18.COOLDOWN);
registerWindowItems1_17_1(ClientboundPackets1_18.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT, Type.FLAT_VAR_INT_ITEM);
registerSetSlot1_17_1(ClientboundPackets1_18.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
registerEntityEquipmentArray(ClientboundPackets1_18.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
registerTradeList(ClientboundPackets1_18.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
registerAdvancements(ClientboundPackets1_18.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
registerClickWindow1_17_1(ServerboundPackets1_17.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_18.EFFECT, new PacketRemapper() {
@Override
public void registerMap() {
// Effect id
map(Type.INT);
// Location
map(Type.POSITION1_14);
// Data
map(Type.INT);
handler(wrapper -> {
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().getNewItemId(data));
}
});
}
});
registerCreativeInvAction(ServerboundPackets1_17.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_18.SPAWN_PARTICLE, new PacketRemapper() {
@Override
public void registerMap() {
// Particle id
map(Type.INT);
// Override limiter
map(Type.BOOLEAN);
// X
map(Type.DOUBLE);
// Y
map(Type.DOUBLE);
// Z
map(Type.DOUBLE);
// Offset X
map(Type.FLOAT);
// Offset Y
map(Type.FLOAT);
// Offset Z
map(Type.FLOAT);
// Max speed
map(Type.FLOAT);
// Particle Count
map(Type.INT);
handler(wrapper -> {
int id = wrapper.get(Type.INT, 0);
if (id == 3) {
// Block marker
int blockState = wrapper.read(Type.VAR_INT);
if (blockState == 7786) {
// Light block
wrapper.set(Type.INT, 0, 3);
} else {
// Else assume barrier block
wrapper.set(Type.INT, 0, 2);
}
return;
}
ParticleMappings mappings = protocol.getMappingData().getParticleMappings();
if (mappings.isBlockParticle(id)) {
int data = wrapper.passthrough(Type.VAR_INT);
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
} else if (mappings.isItemParticle(id)) {
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
int newId = protocol.getMappingData().getNewParticleId(id);
if (newId != id) {
wrapper.set(Type.INT, 0, newId);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_18.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14);
handler(wrapper -> {
final int id = wrapper.read(Type.VAR_INT);
final CompoundTag tag = wrapper.read(Type.NBT);
final int mappedId = BlockEntityIds.mappedId(id);
if (mappedId == -1) {
wrapper.cancel();
return;
}
final String identifier = protocol.getMappingData().blockEntities().get(id);
if (identifier == null) {
wrapper.cancel();
return;
}
// The 1.18 server doesn't include the id and positions (x, y, z) in the NBT anymore
// If those were the only fields on the block entity (e.g.: skull, bed), we'll receive a null NBT
// We initialize one and add the missing fields, so it can be handled correctly down the line
final CompoundTag newTag = tag == null ? new CompoundTag() : tag;
final Position pos = wrapper.get(Type.POSITION1_14, 0);
// The protocol converters downstream rely on this field, let's add it back
newTag.put("id", new StringTag("minecraft:" + identifier));
// Weird glitches happen with the 1.17 client and below if these fields are missing
// Some examples are block entity models becoming invisible (e.g.: signs, banners)
newTag.put("x", new IntTag(pos.x()));
newTag.put("y", new IntTag(pos.y()));
newTag.put("z", new IntTag(pos.z()));
handleSpawner(id, newTag);
wrapper.write(Type.UNSIGNED_BYTE, (short) mappedId);
wrapper.write(Type.NBT, newTag);
});
}
});
protocol.registerClientbound(ClientboundPackets1_18.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user());
final Chunk1_18Type chunkType = new Chunk1_18Type(tracker.currentWorldSectionHeight(), MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().size()), MathUtil.ceilLog2(tracker.biomesSent()));
final Chunk oldChunk = wrapper.read(chunkType);
final ChunkSection[] sections = oldChunk.getSections();
final BitSet mask = new BitSet(oldChunk.getSections().length);
final int[] biomeData = new int[sections.length * ChunkSection.BIOME_SIZE];
int biomeIndex = 0;
for (int j = 0; j < sections.length; j++) {
final ChunkSection section = sections[j];
// Write biome palette into biome array
final DataPalette biomePalette = section.palette(PaletteType.BIOMES);
for (int i = 0; i < ChunkSection.BIOME_SIZE; i++) {
biomeData[biomeIndex++] = biomePalette.idAt(i);
}
// Rewrite to empty section
if (section.getNonAirBlocksCount() == 0) {
sections[j] = null;
} else {
mask.set(j);
}
}
final List<CompoundTag> blockEntityTags = new ArrayList<>(oldChunk.blockEntities().size());
for (final BlockEntity blockEntity : oldChunk.blockEntities()) {
final String id = protocol.getMappingData().blockEntities().get(blockEntity.typeId());
if (id == null) {
// Shrug
continue;
}
final CompoundTag tag;
if (blockEntity.tag() != null) {
tag = blockEntity.tag();
handleSpawner(blockEntity.typeId(), tag);
} else {
tag = new CompoundTag();
}
blockEntityTags.add(tag);
tag.put("x", new IntTag((oldChunk.getX() << 4) + blockEntity.sectionX()));
tag.put("y", new IntTag(blockEntity.y()));
tag.put("z", new IntTag((oldChunk.getZ() << 4) + blockEntity.sectionZ()));
tag.put("id", new StringTag("minecraft:" + id));
}
final Chunk chunk = new BaseChunk(oldChunk.getX(), oldChunk.getZ(), true, false, mask, oldChunk.getSections(), biomeData, oldChunk.getHeightMap(), blockEntityTags);
wrapper.write(new Chunk1_17Type(tracker.currentWorldSectionHeight()), chunk);
// Create and send light packet first
final PacketWrapper lightPacket = wrapper.create(ClientboundPackets1_17_1.UPDATE_LIGHT);
lightPacket.write(Type.VAR_INT, chunk.getX());
lightPacket.write(Type.VAR_INT, chunk.getZ());
// Trust edges
lightPacket.write(Type.BOOLEAN, wrapper.read(Type.BOOLEAN));
// Sky light mask
lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
// Block light mask
lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
// Empty sky light mask
lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
// Empty block light mask
lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
final int skyLightLength = wrapper.read(Type.VAR_INT);
lightPacket.write(Type.VAR_INT, skyLightLength);
for (int i = 0; i < skyLightLength; i++) {
lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
}
final int blockLightLength = wrapper.read(Type.VAR_INT);
lightPacket.write(Type.VAR_INT, blockLightLength);
for (int i = 0; i < blockLightLength; i++) {
lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
}
lightPacket.send(Protocol1_17_1To1_18.class);
});
}
});
protocol.cancelClientbound(ClientboundPackets1_18.SET_SIMULATION_DISTANCE);
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag in project ViaBackwards by ViaVersion.
the class EntityPackets1_18 method registerPackets.
@Override
protected void registerPackets() {
registerMetadataRewriter(ClientboundPackets1_18.ENTITY_METADATA, Types1_18.METADATA_LIST, Types1_17.METADATA_LIST);
protocol.registerClientbound(ClientboundPackets1_18.JOIN_GAME, new PacketRemapper() {
@Override
public void registerMap() {
// Entity ID
map(Type.INT);
// Hardcore
map(Type.BOOLEAN);
// Gamemode
map(Type.UNSIGNED_BYTE);
// Previous Gamemode
map(Type.BYTE);
// Worlds
map(Type.STRING_ARRAY);
// Dimension registry
map(Type.NBT);
// Current dimension data
map(Type.NBT);
// World
map(Type.STRING);
// Seed
map(Type.LONG);
// Max players
map(Type.VAR_INT);
// Chunk radius
map(Type.VAR_INT);
// Read simulation distance
read(Type.VAR_INT);
handler(worldDataTrackerHandler(1));
handler(wrapper -> {
final CompoundTag registry = wrapper.get(Type.NBT, 0);
final CompoundTag biomeRegistry = registry.get("minecraft:worldgen/biome");
final ListTag biomes = biomeRegistry.get("value");
for (final Tag biome : biomes.getValue()) {
final CompoundTag biomeCompound = ((CompoundTag) biome).get("element");
final StringTag category = biomeCompound.get("category");
if (category.getValue().equals("mountain")) {
category.setValue("extreme_hills");
}
// The client just needs something
biomeCompound.put("depth", new FloatTag(0.125F));
biomeCompound.put("scale", new FloatTag(0.05F));
}
// Track amount of biomes sent
tracker(wrapper.user()).setBiomesSent(biomes.size());
});
}
});
protocol.registerClientbound(ClientboundPackets1_18.RESPAWN, new PacketRemapper() {
@Override
public void registerMap() {
// Dimension data
map(Type.NBT);
// World
map(Type.STRING);
handler(worldDataTrackerHandler(0));
}
});
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag in project ViaBackwards by ViaVersion.
the class Protocol1_18To1_18_2 method removeTagPrefix.
private void removeTagPrefix(CompoundTag tag) {
final Tag infiniburnTag = tag.get("infiniburn");
if (infiniburnTag instanceof StringTag) {
final StringTag infiniburn = (StringTag) infiniburnTag;
infiniburn.setValue(infiniburn.getValue().substring(1));
}
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_16_2 method handleBlockEntity.
private void handleBlockEntity(CompoundTag tag) {
StringTag idTag = tag.get("id");
if (idTag == null)
return;
if (idTag.getValue().equals("minecraft:skull")) {
// Workaround an old client bug: MC-68487
Tag skullOwnerTag = tag.get("SkullOwner");
if (!(skullOwnerTag instanceof CompoundTag))
return;
CompoundTag skullOwnerCompoundTag = (CompoundTag) skullOwnerTag;
if (!skullOwnerCompoundTag.contains("Id"))
return;
CompoundTag properties = skullOwnerCompoundTag.get("Properties");
if (properties == null)
return;
ListTag textures = properties.get("textures");
if (textures == null)
return;
CompoundTag first = textures.size() > 0 ? textures.get(0) : null;
if (first == null)
return;
// Make the client cache the skinprofile over this uuid
int hashCode = first.get("Value").getValue().hashCode();
// TODO split texture in 4 for a lower collision chance
int[] uuidIntArray = { hashCode, 0, 0, 0 };
skullOwnerCompoundTag.put("Id", new IntArrayTag(uuidIntArray));
}
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_14 method handleItemToClient.
@Override
public Item handleItemToClient(Item item) {
if (item == null)
return null;
super.handleItemToClient(item);
// Lore now uses JSON
CompoundTag tag = item.tag();
CompoundTag display;
if (tag != null && (display = tag.get("display")) != null) {
ListTag lore = display.get("Lore");
if (lore != null) {
saveListTag(display, lore, "Lore");
for (Tag loreEntry : lore) {
if (!(loreEntry instanceof StringTag))
continue;
StringTag loreEntryTag = (StringTag) loreEntry;
String value = loreEntryTag.getValue();
if (value != null && !value.isEmpty()) {
loreEntryTag.setValue(ChatRewriter.jsonToLegacyText(value));
}
}
}
}
enchantmentRewriter.handleToClient(item);
return item;
}
Aggregations