use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection 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.api.minecraft.chunks.ChunkSection in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_14 method registerPackets.
@Override
protected void registerPackets() {
protocol.registerServerbound(ServerboundPackets1_13.EDIT_BOOK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
}
});
protocol.registerClientbound(ClientboundPackets1_14.OPEN_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int windowId = wrapper.read(Type.VAR_INT);
wrapper.write(Type.UNSIGNED_BYTE, (short) windowId);
int type = wrapper.read(Type.VAR_INT);
String stringType = null;
String containerTitle = null;
int slotSize = 0;
if (type < 6) {
if (type == 2)
containerTitle = "Barrel";
stringType = "minecraft:container";
slotSize = (type + 1) * 9;
} else {
switch(type) {
case 11:
stringType = "minecraft:crafting_table";
break;
// blast furnace
case 9:
// smoker
case 20:
// furnace
case 13:
case // grindstone
14:
if (type == 9)
containerTitle = "Blast Furnace";
else if (type == 20)
containerTitle = "Smoker";
else if (type == 14)
containerTitle = "Grindstone";
stringType = "minecraft:furnace";
slotSize = 3;
break;
case 6:
stringType = "minecraft:dropper";
slotSize = 9;
break;
case 12:
stringType = "minecraft:enchanting_table";
break;
case 10:
stringType = "minecraft:brewing_stand";
slotSize = 5;
break;
case 18:
stringType = "minecraft:villager";
break;
case 8:
stringType = "minecraft:beacon";
slotSize = 1;
break;
// cartography_table
case 21:
case 7:
if (type == 21)
containerTitle = "Cartography Table";
stringType = "minecraft:anvil";
break;
case 15:
stringType = "minecraft:hopper";
slotSize = 5;
break;
case 19:
stringType = "minecraft:shulker_box";
slotSize = 27;
break;
}
}
if (stringType == null) {
ViaBackwards.getPlatform().getLogger().warning("Can't open inventory for 1.13 player! Type: " + type);
wrapper.cancel();
return;
}
wrapper.write(Type.STRING, stringType);
JsonElement title = wrapper.read(Type.COMPONENT);
if (containerTitle != null) {
// Don't rewrite renamed, only translatable titles
JsonObject object;
if (title.isJsonObject() && (object = title.getAsJsonObject()).has("translate")) {
// Don't rewrite other 9x3 translatable containers
if (type != 2 || object.getAsJsonPrimitive("translate").getAsString().equals("container.barrel")) {
title = ChatRewriter.legacyTextToJson(containerTitle);
}
}
}
wrapper.write(Type.COMPONENT, title);
wrapper.write(Type.UNSIGNED_BYTE, (short) slotSize);
}
});
}
});
// Horse window -> Open Window
protocol.registerClientbound(ClientboundPackets1_14.OPEN_HORSE_WINDOW, ClientboundPackets1_13.OPEN_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Window id
wrapper.passthrough(Type.UNSIGNED_BYTE);
// Type
wrapper.write(Type.STRING, "EntityHorse");
JsonObject object = new JsonObject();
object.addProperty("translate", "minecraft.horse");
// Title
wrapper.write(Type.COMPONENT, object);
// Number of slots
wrapper.write(Type.UNSIGNED_BYTE, wrapper.read(Type.VAR_INT).shortValue());
// Entity id
wrapper.passthrough(Type.INT);
}
});
}
});
BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION);
registerSetCooldown(ClientboundPackets1_14.COOLDOWN);
registerWindowItems(ClientboundPackets1_14.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
registerSetSlot(ClientboundPackets1_14.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
registerAdvancements(ClientboundPackets1_14.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
// Trade List -> Plugin Message
protocol.registerClientbound(ClientboundPackets1_14.TRADE_LIST, ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.STRING, "minecraft:trader_list");
int windowId = wrapper.read(Type.VAR_INT);
wrapper.write(Type.INT, windowId);
int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
// Input Item
Item input = wrapper.read(Type.FLAT_VAR_INT_ITEM);
input = handleItemToClient(input);
wrapper.write(Type.FLAT_VAR_INT_ITEM, input);
// Output Item
Item output = wrapper.read(Type.FLAT_VAR_INT_ITEM);
output = handleItemToClient(output);
wrapper.write(Type.FLAT_VAR_INT_ITEM, output);
// Has second item
boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
if (secondItem) {
// Second Item
Item second = wrapper.read(Type.FLAT_VAR_INT_ITEM);
second = handleItemToClient(second);
wrapper.write(Type.FLAT_VAR_INT_ITEM, second);
}
// Trade disabled
wrapper.passthrough(Type.BOOLEAN);
// Number of tools uses
wrapper.passthrough(Type.INT);
// Maximum number of trade uses
wrapper.passthrough(Type.INT);
wrapper.read(Type.INT);
wrapper.read(Type.INT);
wrapper.read(Type.FLOAT);
}
wrapper.read(Type.VAR_INT);
wrapper.read(Type.VAR_INT);
wrapper.read(Type.BOOLEAN);
}
});
}
});
// Open Book -> Plugin Message
protocol.registerClientbound(ClientboundPackets1_14.OPEN_BOOK, ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.STRING, "minecraft:book_open");
wrapper.passthrough(Type.VAR_INT);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_14.ENTITY_EQUIPMENT, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// 1 - Slot ID
map(Type.VAR_INT);
// 2 - Item
map(Type.FLAT_VAR_INT_ITEM);
handler(itemToClientHandler(Type.FLAT_VAR_INT_ITEM));
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
EntityType entityType = wrapper.user().getEntityTracker(Protocol1_13_2To1_14.class).entityType(entityId);
if (entityType == null)
return;
if (entityType.isOrHasParent(Entity1_14Types.ABSTRACT_HORSE)) {
wrapper.setId(0x3F);
wrapper.resetReader();
wrapper.passthrough(Type.VAR_INT);
wrapper.read(Type.VAR_INT);
Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM);
int armorType = item == null || item.identifier() == 0 ? 0 : item.identifier() - 726;
if (armorType < 0 || armorType > 3) {
ViaBackwards.getPlatform().getLogger().warning("Received invalid horse armor: " + item);
wrapper.cancel();
return;
}
List<Metadata> metadataList = new ArrayList<>();
metadataList.add(new Metadata(16, Types1_13_2.META_TYPES.varIntType, armorType));
wrapper.write(Types1_13.METADATA_LIST, metadataList);
}
}
});
}
});
RecipeRewriter recipeHandler = new RecipeRewriter1_13_2(protocol);
protocol.registerClientbound(ClientboundPackets1_14.DECLARE_RECIPES, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
private final Set<String> removedTypes = ImmutableSet.of("crafting_special_suspiciousstew", "blasting", "smoking", "campfire_cooking", "stonecutting");
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int size = wrapper.passthrough(Type.VAR_INT);
int deleted = 0;
for (int i = 0; i < size; i++) {
String type = wrapper.read(Type.STRING);
// Recipe Identifier
String id = wrapper.read(Type.STRING);
type = type.replace("minecraft:", "");
if (removedTypes.contains(type)) {
switch(type) {
case "blasting":
case "smoking":
case "campfire_cooking":
// Group
wrapper.read(Type.STRING);
// Ingredients
wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
wrapper.read(Type.FLAT_VAR_INT_ITEM);
// EXP
wrapper.read(Type.FLOAT);
// Cooking time
wrapper.read(Type.VAR_INT);
break;
case "stonecutting":
// Group?
wrapper.read(Type.STRING);
// Ingredients
wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
// Result
wrapper.read(Type.FLAT_VAR_INT_ITEM);
break;
}
deleted++;
continue;
}
wrapper.write(Type.STRING, id);
wrapper.write(Type.STRING, type);
// Handle the rest of the types
recipeHandler.handle(wrapper, type);
}
wrapper.set(Type.VAR_INT, 0, size - deleted);
}
});
}
});
registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_14.BLOCK_BREAK_ANIMATION, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.POSITION1_14, Type.POSITION);
map(Type.BYTE);
}
});
protocol.registerClientbound(ClientboundPackets1_14.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14, Type.POSITION);
}
});
protocol.registerClientbound(ClientboundPackets1_14.BLOCK_ACTION, new PacketRemapper() {
@Override
public void registerMap() {
// Location
map(Type.POSITION1_14, Type.POSITION);
// Action id
map(Type.UNSIGNED_BYTE);
// Action param
map(Type.UNSIGNED_BYTE);
// Block id - /!\ NOT BLOCK STATE
map(Type.VAR_INT);
handler(wrapper -> {
int mappedId = protocol.getMappingData().getNewBlockId(wrapper.get(Type.VAR_INT, 0));
if (mappedId == -1) {
wrapper.cancel();
return;
}
wrapper.set(Type.VAR_INT, 0, mappedId);
});
}
});
protocol.registerClientbound(ClientboundPackets1_14.BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14, Type.POSITION);
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.VAR_INT, 0);
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(id));
}
});
}
});
blockRewriter.registerMultiBlockChange(ClientboundPackets1_14.MULTI_BLOCK_CHANGE);
protocol.registerClientbound(ClientboundPackets1_14.EXPLOSION, new PacketRemapper() {
@Override
public void registerMap() {
// X
map(Type.FLOAT);
// Y
map(Type.FLOAT);
// Z
map(Type.FLOAT);
// Radius
map(Type.FLOAT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
for (int i = 0; i < 3; i++) {
float coord = wrapper.get(Type.FLOAT, i);
if (coord < 0f) {
coord = (float) Math.floor(coord);
wrapper.set(Type.FLOAT, i, coord);
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_14.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);
Chunk chunk = wrapper.read(new Chunk1_14Type());
wrapper.write(new Chunk1_13Type(clientWorld), chunk);
ChunkLightStorage.ChunkLight chunkLight = wrapper.user().get(ChunkLightStorage.class).getStoredLight(chunk.getX(), chunk.getZ());
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
ChunkSectionLight sectionLight = new ChunkSectionLightImpl();
section.setLight(sectionLight);
if (chunkLight == null) {
sectionLight.setBlockLight(ChunkLightStorage.FULL_LIGHT);
if (clientWorld.getEnvironment() == Environment.NORMAL) {
sectionLight.setSkyLight(ChunkLightStorage.FULL_LIGHT);
}
} else {
byte[] blockLight = chunkLight.getBlockLight()[i];
sectionLight.setBlockLight(blockLight != null ? blockLight : ChunkLightStorage.FULL_LIGHT);
if (clientWorld.getEnvironment() == Environment.NORMAL) {
byte[] skyLight = chunkLight.getSkyLight()[i];
sectionLight.setSkyLight(skyLight != null ? skyLight : ChunkLightStorage.FULL_LIGHT);
}
}
if (Via.getConfig().isNonFullBlockLightFix() && section.getNonAirBlocksCount() != 0 && sectionLight.hasBlockLight()) {
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
int id = section.getFlatBlock(x, y, z);
if (Protocol1_14To1_13_2.MAPPINGS.getNonFullBlocks().contains(id)) {
sectionLight.getBlockLightNibbleArray().set(x, y, z, 0);
}
}
}
}
}
for (int j = 0; j < section.getPaletteSize(); j++) {
int old = section.getPaletteEntry(j);
int newId = protocol.getMappingData().getNewBlockStateId(old);
section.setPaletteEntry(j, newId);
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_14.UNLOAD_CHUNK, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int x = wrapper.passthrough(Type.INT);
int z = wrapper.passthrough(Type.INT);
wrapper.user().get(ChunkLightStorage.class).unloadChunk(x, z);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_14.EFFECT, new PacketRemapper() {
@Override
public void registerMap() {
// Effect Id
map(Type.INT);
// Location
map(Type.POSITION1_14, 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().getNewItemId(data));
} else if (id == 2001) {
// Block break + block break sound
wrapper.set(Type.INT, 1, protocol.getMappingData().getNewBlockStateId(data));
}
}
});
}
});
registerSpawnParticle(ClientboundPackets1_14.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
protocol.registerClientbound(ClientboundPackets1_14.MAP_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.BYTE);
map(Type.BOOLEAN);
// Locked
map(Type.BOOLEAN, Type.NOTHING);
}
});
protocol.registerClientbound(ClientboundPackets1_14.SPAWN_POSITION, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14, Type.POSITION);
}
});
}
use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection 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.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.
the class ChunkSectionType1_13 method read.
@Override
public ChunkSection read(ByteBuf buffer) throws Exception {
// Reaad bits per block
int bitsPerBlock = buffer.readUnsignedByte();
int originalBitsPerBlock = bitsPerBlock;
if (bitsPerBlock == 0 || bitsPerBlock > 8) {
bitsPerBlock = GLOBAL_PALETTE;
}
// Read palette
ChunkSection chunkSection;
if (bitsPerBlock != GLOBAL_PALETTE) {
int paletteLength = Type.VAR_INT.readPrimitive(buffer);
chunkSection = new ChunkSectionImpl(true, paletteLength);
for (int i = 0; i < paletteLength; i++) {
chunkSection.addPaletteEntry(Type.VAR_INT.readPrimitive(buffer));
}
} else {
chunkSection = new ChunkSectionImpl(true);
}
// Read blocks
long[] blockData = new long[Type.VAR_INT.readPrimitive(buffer)];
if (blockData.length > 0) {
int expectedLength = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0);
if (blockData.length != expectedLength) {
throw new IllegalStateException("Block data length (" + blockData.length + ") does not match expected length (" + expectedLength + ")! bitsPerBlock=" + bitsPerBlock + ", originalBitsPerBlock=" + originalBitsPerBlock);
}
for (int i = 0; i < blockData.length; i++) {
blockData[i] = buffer.readLong();
}
CompactArrayUtil.iterateCompactArray(bitsPerBlock, ChunkSection.SIZE, blockData, bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock : chunkSection::setPaletteIndex);
}
return chunkSection;
}
use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.
the class ChunkSectionType1_16 method read.
@Override
public ChunkSection read(ByteBuf buffer) throws Exception {
// Reaad bits per block
int bitsPerBlock = buffer.readUnsignedByte();
int originalBitsPerBlock = bitsPerBlock;
if (bitsPerBlock == 0 || bitsPerBlock > 8) {
bitsPerBlock = GLOBAL_PALETTE;
}
// Read palette
ChunkSection chunkSection;
if (bitsPerBlock != GLOBAL_PALETTE) {
int paletteLength = Type.VAR_INT.readPrimitive(buffer);
chunkSection = new ChunkSectionImpl(false, paletteLength);
for (int i = 0; i < paletteLength; i++) {
chunkSection.addPaletteEntry(Type.VAR_INT.readPrimitive(buffer));
}
} else {
chunkSection = new ChunkSectionImpl(false);
}
// Read blocks
long[] blockData = new long[Type.VAR_INT.readPrimitive(buffer)];
if (blockData.length > 0) {
char valuesPerLong = (char) (64 / bitsPerBlock);
int expectedLength = (ChunkSection.SIZE + valuesPerLong - 1) / valuesPerLong;
if (blockData.length != expectedLength) {
throw new IllegalStateException("Block data length (" + blockData.length + ") does not match expected length (" + expectedLength + ")! bitsPerBlock=" + bitsPerBlock + ", originalBitsPerBlock=" + originalBitsPerBlock);
}
for (int i = 0; i < blockData.length; i++) {
blockData[i] = buffer.readLong();
}
CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerBlock, ChunkSection.SIZE, blockData, bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock : chunkSection::setPaletteIndex);
}
return chunkSection;
}
Aggregations