use of com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_16 method handleItemToServer.
@Override
public Item handleItemToServer(Item item) {
if (item == null)
return null;
int identifier = item.identifier();
super.handleItemToServer(item);
CompoundTag tag = item.tag();
if (identifier == 771 && tag != null) {
Tag ownerTag = tag.get("SkullOwner");
if (ownerTag instanceof CompoundTag) {
CompoundTag ownerCompundTag = (CompoundTag) ownerTag;
Tag idTag = ownerCompundTag.get("Id");
if (idTag instanceof StringTag) {
UUID ownerUuid = UUID.fromString((String) idTag.getValue());
ownerCompundTag.put("Id", new IntArrayTag(UUIDIntArrayType.uuidToIntArray(ownerUuid)));
}
}
}
InventoryPackets.oldToNewAttributes(item);
enchantmentRewriter.handleToServer(item);
return item;
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_16 method registerPackets.
@Override
protected void registerPackets() {
BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
RecipeRewriter1_14 recipeRewriter = new RecipeRewriter1_14(protocol);
// Remove new smithing type, only in this handler
protocol.registerClientbound(ClientboundPackets1_16.DECLARE_RECIPES, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
int size = wrapper.passthrough(Type.VAR_INT);
int newSize = size;
for (int i = 0; i < size; i++) {
String originalType = wrapper.read(Type.STRING);
String type = originalType.replace("minecraft:", "");
if (type.equals("smithing")) {
newSize--;
wrapper.read(Type.STRING);
wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
wrapper.read(Type.FLAT_VAR_INT_ITEM);
continue;
}
wrapper.write(Type.STRING, originalType);
// Recipe Identifier
String id = wrapper.passthrough(Type.STRING);
recipeRewriter.handle(wrapper, type);
}
wrapper.set(Type.VAR_INT, 0, newSize);
});
}
});
registerSetCooldown(ClientboundPackets1_16.COOLDOWN);
registerWindowItems(ClientboundPackets1_16.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
registerSetSlot(ClientboundPackets1_16.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
registerTradeList(ClientboundPackets1_16.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
registerAdvancements(ClientboundPackets1_16.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_16.ACKNOWLEDGE_PLAYER_DIGGING);
blockRewriter.registerBlockAction(ClientboundPackets1_16.BLOCK_ACTION);
blockRewriter.registerBlockChange(ClientboundPackets1_16.BLOCK_CHANGE);
blockRewriter.registerMultiBlockChange(ClientboundPackets1_16.MULTI_BLOCK_CHANGE);
protocol.registerClientbound(ClientboundPackets1_16.ENTITY_EQUIPMENT, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
int entityId = wrapper.passthrough(Type.VAR_INT);
List<EquipmentData> equipmentData = new ArrayList<>();
byte slot;
do {
slot = wrapper.read(Type.BYTE);
Item item = handleItemToClient(wrapper.read(Type.FLAT_VAR_INT_ITEM));
int rawSlot = slot & 0x7F;
equipmentData.add(new EquipmentData(rawSlot, item));
} while ((slot & 0xFFFFFF80) != 0);
// Send first data in the current packet
EquipmentData firstData = equipmentData.get(0);
wrapper.write(Type.VAR_INT, firstData.slot);
wrapper.write(Type.FLAT_VAR_INT_ITEM, firstData.item);
// If there are more items, send new packets for them
for (int i = 1; i < equipmentData.size(); i++) {
PacketWrapper equipmentPacket = wrapper.create(ClientboundPackets1_15.ENTITY_EQUIPMENT);
EquipmentData data = equipmentData.get(i);
equipmentPacket.write(Type.VAR_INT, entityId);
equipmentPacket.write(Type.VAR_INT, data.slot);
equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, data.item);
equipmentPacket.send(Protocol1_15_2To1_16.class);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_16.UPDATE_LIGHT, new PacketRemapper() {
@Override
public void registerMap() {
// x
map(Type.VAR_INT);
// y
map(Type.VAR_INT);
map(Type.BOOLEAN, Type.NOTHING);
}
});
protocol.registerClientbound(ClientboundPackets1_16.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
Chunk chunk = wrapper.read(new Chunk1_16Type());
wrapper.write(new Chunk1_15Type(), chunk);
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
for (int j = 0; j < section.getPaletteSize(); j++) {
int old = section.getPaletteEntry(j);
section.setPaletteEntry(j, protocol.getMappingData().getNewBlockStateId(old));
}
}
CompoundTag heightMaps = chunk.getHeightMap();
for (Tag heightMapTag : heightMaps.values()) {
LongArrayTag heightMap = (LongArrayTag) heightMapTag;
int[] heightMapData = new int[256];
CompactArrayUtil.iterateCompactArrayWithPadding(9, heightMapData.length, heightMap.getValue(), (i, v) -> heightMapData[i] = v);
heightMap.setValue(CompactArrayUtil.createCompactArray(9, heightMapData.length, i -> heightMapData[i]));
}
if (chunk.isBiomeData()) {
for (int i = 0; i < 1024; i++) {
int biome = chunk.getBiomeData()[i];
switch(biome) {
// new nether biomes
case 170:
case 171:
case 172:
case 173:
chunk.getBiomeData()[i] = 8;
break;
}
}
}
if (chunk.getBlockEntities() == null)
return;
for (CompoundTag blockEntity : chunk.getBlockEntities()) {
handleBlockEntity(blockEntity);
}
});
}
});
blockRewriter.registerEffect(ClientboundPackets1_16.EFFECT, 1010, 2001);
registerSpawnParticle(ClientboundPackets1_16.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
protocol.registerClientbound(ClientboundPackets1_16.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);
if (property >= 4 && property <= 6) {
// Enchantment id
short enchantmentId = wrapper.get(Type.SHORT, 1);
if (enchantmentId > 11) {
// soul_speed
wrapper.set(Type.SHORT, 1, --enchantmentId);
} else if (enchantmentId == 11) {
wrapper.set(Type.SHORT, 1, (short) 9);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_16.MAP_DATA, new PacketRemapper() {
@Override
public void registerMap() {
// Map ID
map(Type.VAR_INT);
// Scale
map(Type.BYTE);
// Tracking Position
map(Type.BOOLEAN);
// Locked
map(Type.BOOLEAN);
handler(MapColorRewriter.getRewriteHandler(MapColorRewrites::getMappedColor));
}
});
protocol.registerClientbound(ClientboundPackets1_16.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
Position position = wrapper.passthrough(Type.POSITION1_14);
short action = wrapper.passthrough(Type.UNSIGNED_BYTE);
CompoundTag tag = wrapper.passthrough(Type.NBT);
handleBlockEntity(tag);
});
}
});
registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
protocol.registerServerbound(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
}
});
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag in project ViaBackwards by ViaVersion.
the class Protocol1_17To1_17_1 method registerPackets.
@Override
protected void registerPackets() {
registerClientbound(ClientboundPackets1_17_1.REMOVE_ENTITIES, null, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
int[] entityIds = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
wrapper.cancel();
for (int entityId : entityIds) {
// Send individual remove packets
PacketWrapper newPacket = wrapper.create(ClientboundPackets1_17.REMOVE_ENTITY);
newPacket.write(Type.VAR_INT, entityId);
newPacket.send(Protocol1_17To1_17_1.class);
}
});
}
});
registerClientbound(ClientboundPackets1_17_1.CLOSE_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
});
}
});
registerClientbound(ClientboundPackets1_17_1.SET_SLOT, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
int stateId = wrapper.read(Type.VAR_INT);
wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId);
});
}
});
registerClientbound(ClientboundPackets1_17_1.WINDOW_ITEMS, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
int stateId = wrapper.read(Type.VAR_INT);
wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId);
// Length is encoded as a var int in 1.17.1
wrapper.write(Type.FLAT_VAR_INT_ITEM_ARRAY, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT));
// Carried item - should work without adding it to the array above
wrapper.read(Type.FLAT_VAR_INT_ITEM);
});
}
});
registerServerbound(ServerboundPackets1_17.CLOSE_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
});
}
});
registerServerbound(ServerboundPackets1_17.CLICK_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
int stateId = wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
wrapper.write(Type.VAR_INT, stateId == Integer.MAX_VALUE ? 0 : stateId);
});
}
});
registerServerbound(ServerboundPackets1_17.EDIT_BOOK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM);
boolean signing = wrapper.read(Type.BOOLEAN);
// Slot comes first
wrapper.passthrough(Type.VAR_INT);
CompoundTag tag = item.tag();
ListTag pagesTag;
StringTag titleTag = null;
// Sanity checks
if (tag == null || (pagesTag = tag.get("pages")) == null || (signing && (titleTag = tag.get("title")) == null)) {
// Pages length
wrapper.write(Type.VAR_INT, 0);
// Optional title
wrapper.write(Type.BOOLEAN, false);
return;
}
// Write pages - limit them first
if (pagesTag.size() > MAX_PAGES) {
pagesTag = new ListTag(pagesTag.getValue().subList(0, MAX_PAGES));
}
wrapper.write(Type.VAR_INT, pagesTag.size());
for (Tag pageTag : pagesTag) {
String page = ((StringTag) pageTag).getValue();
// Limit page length
if (page.length() > MAX_PAGE_LENGTH) {
page = page.substring(0, MAX_PAGE_LENGTH);
}
wrapper.write(Type.STRING, page);
}
// Write optional title
wrapper.write(Type.BOOLEAN, signing);
if (signing) {
if (titleTag == null) {
titleTag = tag.get("title");
}
// Limit title length
String title = titleTag.getValue();
if (title.length() > MAX_TITLE_LENGTH) {
title = title.substring(0, MAX_TITLE_LENGTH);
}
wrapper.write(Type.STRING, title);
}
});
}
});
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag in project ViaBackwards by ViaVersion.
the class BackwardsBlockEntityProvider method transform.
/**
* Transform blocks to block entities!
*
* @param user The user
* @param position The position of the block entity
* @param id The block entity id
*/
public CompoundTag transform(UserConnection user, Position position, String id) throws Exception {
CompoundTag tag = new CompoundTag();
tag.put("id", new StringTag(id));
tag.put("x", new IntTag(Math.toIntExact(position.getX())));
tag.put("y", new IntTag(Math.toIntExact(position.getY())));
tag.put("z", new IntTag(Math.toIntExact(position.getZ())));
return this.transform(user, position, tag);
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_11 method registerPackets.
@Override
protected void registerPackets() {
protocol.registerClientbound(ClientboundPackets1_9_3.SET_SLOT, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// 1 - Slot ID
map(Type.SHORT);
// 2 - Slot Value
map(Type.ITEM);
handler(itemToClientHandler(Type.ITEM));
// Handle Llama
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
if (isLlama(wrapper.user())) {
Optional<ChestedHorseStorage> horse = getChestedHorse(wrapper.user());
if (!horse.isPresent())
return;
ChestedHorseStorage storage = horse.get();
int currentSlot = wrapper.get(Type.SHORT, 0);
wrapper.set(Type.SHORT, 0, ((Integer) (currentSlot = getNewSlotId(storage, currentSlot))).shortValue());
wrapper.set(Type.ITEM, 0, getNewItem(storage, currentSlot, wrapper.get(Type.ITEM, 0)));
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_9_3.WINDOW_ITEMS, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// 1 - Window Values
map(Type.ITEM_ARRAY);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.ITEM_ARRAY, 0);
for (int i = 0; i < stacks.length; i++) stacks[i] = handleItemToClient(stacks[i]);
if (isLlama(wrapper.user())) {
Optional<ChestedHorseStorage> horse = getChestedHorse(wrapper.user());
if (!horse.isPresent())
return;
ChestedHorseStorage storage = horse.get();
stacks = Arrays.copyOf(stacks, !storage.isChested() ? 38 : 53);
for (int i = stacks.length - 1; i >= 0; i--) {
stacks[getNewSlotId(storage, i)] = stacks[i];
stacks[i] = getNewItem(storage, i, stacks[i]);
}
wrapper.set(Type.ITEM_ARRAY, 0, stacks);
}
}
});
}
});
registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
// Plugin message Packet -> Trading
protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Channel
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) {
// Passthrough Window ID
wrapper.passthrough(Type.INT);
int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
// Input Item
wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
// Output Item
wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
// Has second item
boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
if (secondItem) {
// Second Item
wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
}
// Trade disabled
wrapper.passthrough(Type.BOOLEAN);
// Number of tools uses
wrapper.passthrough(Type.INT);
// Maximum number of trade uses
wrapper.passthrough(Type.INT);
}
}
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_9_3.CLICK_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// 1 - Slot
map(Type.SHORT);
// 2 - Button
map(Type.BYTE);
// 3 - Action number
map(Type.SHORT);
// 4 - Mode
map(Type.VAR_INT);
// 5 - Clicked Item
map(Type.ITEM);
handler(itemToServerHandler(Type.ITEM));
// Llama slot
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
if (isLlama(wrapper.user())) {
Optional<ChestedHorseStorage> horse = getChestedHorse(wrapper.user());
if (!horse.isPresent())
return;
ChestedHorseStorage storage = horse.get();
int clickSlot = wrapper.get(Type.SHORT, 0);
int correctSlot = getOldSlotId(storage, clickSlot);
wrapper.set(Type.SHORT, 0, ((Integer) correctSlot).shortValue());
}
}
});
}
});
registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
protocol.registerClientbound(ClientboundPackets1_9_3.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
// Use the 1.10 Chunk type since nothing changed.
Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
Chunk chunk = wrapper.passthrough(type);
handleChunk(chunk);
// only patch it for signs for now
for (CompoundTag tag : chunk.getBlockEntities()) {
Tag idTag = tag.get("id");
if (!(idTag instanceof StringTag))
continue;
String id = (String) idTag.getValue();
if (id.equals("minecraft:sign")) {
((StringTag) idTag).setValue("Sign");
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_9_3.BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Block Position
map(Type.POSITION);
// 1 - Block
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int idx = wrapper.get(Type.VAR_INT, 0);
wrapper.set(Type.VAR_INT, 0, handleBlockID(idx));
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_9_3.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 {
for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
record.setBlockId(handleBlockID(record.getBlockId()));
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_9_3.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Position
map(Type.POSITION);
// 1 - Action
map(Type.UNSIGNED_BYTE);
// 2 - NBT
map(Type.NBT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Remove on shulkerbox decleration
if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 10) {
wrapper.cancel();
}
// Handler Spawners
if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 1) {
CompoundTag tag = wrapper.get(Type.NBT, 0);
EntityIdRewriter.toClientSpawner(tag, true);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_9_3.OPEN_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// 1 - Window Type
map(Type.STRING);
// 2 - Title
map(Type.COMPONENT);
// 3 - Slots
map(Type.UNSIGNED_BYTE);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = -1;
// Passthrough Entity ID
if (wrapper.get(Type.STRING, 0).equals("EntityHorse")) {
entityId = wrapper.passthrough(Type.INT);
}
// Track Inventory
String inventory = wrapper.get(Type.STRING, 0);
WindowTracker windowTracker = wrapper.user().get(WindowTracker.class);
windowTracker.setInventory(inventory);
windowTracker.setEntityId(entityId);
// Change llama slotcount to the donkey one
if (isLlama(wrapper.user())) {
wrapper.set(Type.UNSIGNED_BYTE, 1, (short) 17);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_9_3.CLOSE_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
// Inventory tracking
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
WindowTracker windowTracker = wrapper.user().get(WindowTracker.class);
windowTracker.setInventory(null);
windowTracker.setEntityId(-1);
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_9_3.CLOSE_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
// Inventory tracking
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
WindowTracker windowTracker = wrapper.user().get(WindowTracker.class);
windowTracker.setInventory(null);
windowTracker.setEntityId(-1);
}
});
}
});
protocol.getEntityRewriter().filter().handler((event, meta) -> {
if (// Is Item
meta.metaType().type().equals(Type.ITEM))
meta.setValue(handleItemToClient((Item) meta.getValue()));
});
}
Aggregations