Flatten some PacketHandlers in rewriters

This commit is contained in:
Nassim Jahnke 2024-10-27 23:02:47 +01:00
parent 3caaed00dc
commit d60a37f7e6
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
6 changed files with 159 additions and 236 deletions

View File

@ -20,7 +20,6 @@ package com.viaversion.viaversion.protocols.template;
import com.viaversion.viaversion.api.minecraft.RegistryEntry;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_5;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.version.Types1_21;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundConfigurationPackets1_21;
@ -51,23 +50,22 @@ final class EntityPacketRewriter1_99 extends EntityRewriter<ClientboundPacket1_2
handleRegistryData1_20_5(wrapper.user(), registryKey, entries); // Caches dimensions to access data like height later and tracks the amount of biomes sent for chunk data
});
protocol.registerClientbound(ClientboundPackets1_21.LOGIN, new PacketHandlers() {
@Override
public void register() {
map(Types.INT); // Entity id
map(Types.BOOLEAN); // Hardcore
map(Types.STRING_ARRAY); // World List
map(Types.VAR_INT); // Max players
map(Types.VAR_INT); // View distance
map(Types.VAR_INT); // Simulation distance
map(Types.BOOLEAN); // Reduced debug info
map(Types.BOOLEAN); // Show death screen
map(Types.BOOLEAN); // Limited crafting
map(Types.VAR_INT); // Dimension id
map(Types.STRING); // World
handler(worldDataTrackerHandlerByKey1_20_5(3)); // Tracks world height and name for chunk data and entity (un)tracking
handler(playerTrackerHandler());
}
protocol.registerClientbound(ClientboundPackets1_21.LOGIN, wrapper -> {
final int entityId = wrapper.passthrough(Types.INT); // Entity id
wrapper.passthrough(Types.BOOLEAN); // Hardcore
wrapper.passthrough(Types.STRING_ARRAY); // World List
wrapper.passthrough(Types.VAR_INT); // Max players
wrapper.passthrough(Types.VAR_INT); // View distance
wrapper.passthrough(Types.VAR_INT); // Simulation distance
wrapper.passthrough(Types.BOOLEAN); // Reduced debug info
wrapper.passthrough(Types.BOOLEAN); // Show death screen
wrapper.passthrough(Types.BOOLEAN); // Limited crafting
final int dimensionId = wrapper.passthrough(Types.VAR_INT);
final String world = wrapper.passthrough(Types.STRING);
trackWorldDataByKey1_20_5(wrapper.user(), dimensionId, world);
trackPlayer(wrapper.user(), entityId);
});
protocol.registerClientbound(ClientboundPackets1_21.RESPAWN, wrapper -> {

View File

@ -35,7 +35,6 @@ import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.util.MathUtil;
@ -68,86 +67,64 @@ public class BlockRewriter<C extends ClientboundPacketType> {
}
public void registerBlockEvent(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(positionType); // Location
map(Types.UNSIGNED_BYTE); // Action id
map(Types.UNSIGNED_BYTE); // Action param
map(Types.VAR_INT); // Block id - /!\ NOT BLOCK STATE
handler(wrapper -> {
if (protocol.getMappingData().getBlockMappings() == null) {
return;
}
int id = wrapper.get(Types.VAR_INT, 0);
int mappedId = protocol.getMappingData().getNewBlockId(id);
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(positionType); // Location
wrapper.passthrough(Types.UNSIGNED_BYTE); // Action id
wrapper.passthrough(Types.UNSIGNED_BYTE); // Action param
final int blockId = wrapper.passthrough(Types.VAR_INT);
final int mappedId = protocol.getMappingData().getNewBlockId(blockId);
if (mappedId == -1) {
// Block (action) has been removed
wrapper.cancel();
return;
}
if (blockId != mappedId) {
wrapper.set(Types.VAR_INT, 0, mappedId);
});
}
});
}
public void registerBlockUpdate(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(positionType);
map(Types.VAR_INT);
handler(wrapper -> wrapper.set(Types.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(wrapper.get(Types.VAR_INT, 0))));
}
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(positionType);
final int blockId = wrapper.read(Types.VAR_INT);
wrapper.write(Types.VAR_INT, protocol.getMappingData().getNewBlockStateId(blockId));
});
}
public void registerChunkBlocksUpdate(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.INT); // 0 - Chunk X
map(Types.INT); // 1 - Chunk Z
handler(wrapper -> {
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.INT); // Chunk X
wrapper.passthrough(Types.INT); // Chunk Z
for (BlockChangeRecord record : wrapper.passthrough(Types.BLOCK_CHANGE_ARRAY)) {
record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId()));
}
});
}
});
}
public void registerSectionBlocksUpdate(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.LONG); // Chunk position
map(Types.BOOLEAN); // Suppress light updates
handler(wrapper -> {
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.LONG); // Chunk position
wrapper.passthrough(Types.BOOLEAN); // Suppress light updates
for (BlockChangeRecord record : wrapper.passthrough(Types.VAR_LONG_BLOCK_CHANGE_ARRAY)) {
record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId()));
}
});
}
});
}
public void registerSectionBlocksUpdate1_20(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.LONG); // Chunk position
handler(wrapper -> {
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.LONG); // Chunk position
for (BlockChangeRecord record : wrapper.passthrough(Types.VAR_LONG_BLOCK_CHANGE_ARRAY)) {
record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId()));
}
});
}
});
}
public void registerBlockBreakAck(C packetType) {
// Same exact handler

View File

@ -33,7 +33,6 @@ import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.util.ComponentUtil;
@ -66,20 +65,14 @@ public class ComponentRewriter<C extends ClientboundPacketType> implements com.v
}
public void registerBossEvent(final C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.UUID);
map(Types.VAR_INT);
handler(wrapper -> {
final int action = wrapper.get(Types.VAR_INT, 0);
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.UUID);
final int action = wrapper.passthrough(Types.VAR_INT);
if (action == 0 || action == 3) {
passthroughAndProcess(wrapper);
}
});
}
});
}
/**
* Handles sub 1.17 combat event components.
@ -112,24 +105,18 @@ public class ComponentRewriter<C extends ClientboundPacketType> implements com.v
}
public void registerLegacyOpenWindow(final C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.UNSIGNED_BYTE); // Id
map(Types.STRING); // Window Type
handler(wrapper -> processText(wrapper.user(), wrapper.passthrough(Types.COMPONENT)));
}
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.UNSIGNED_BYTE); // Id
wrapper.passthrough(Types.STRING); // Window Type
processText(wrapper.user(), wrapper.passthrough(Types.COMPONENT));
});
}
public void registerOpenScreen(final C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.VAR_INT); // Id
map(Types.VAR_INT); // Window Type
handler(wrapper -> passthroughAndProcess(wrapper));
}
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.VAR_INT); // Id
wrapper.passthrough(Types.VAR_INT); // Window Type
passthroughAndProcess(wrapper);
});
}
@ -222,23 +209,17 @@ public class ComponentRewriter<C extends ClientboundPacketType> implements com.v
}
public void registerPlayerCombatKill(final C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.VAR_INT); // Player ID
map(Types.INT); // Killer ID
handler(wrapper -> processText(wrapper.user(), wrapper.passthrough(Types.COMPONENT)));
}
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.VAR_INT); // Player ID
wrapper.passthrough(Types.INT); // Killer ID
processText(wrapper.user(), wrapper.passthrough(Types.COMPONENT));
});
}
public void registerPlayerCombatKill1_20(final C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.VAR_INT); // Player ID
handler(wrapper -> passthroughAndProcess(wrapper));
}
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.VAR_INT); // Player ID
passthroughAndProcess(wrapper);
});
}

View File

@ -38,6 +38,7 @@ import com.viaversion.viaversion.api.minecraft.entitydata.EntityData;
import com.viaversion.viaversion.api.minecraft.entitydata.EntityDataType;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.rewriter.RewriterBase;
@ -260,14 +261,11 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
}
public void registerTracker(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.VAR_INT); // 0 - Entity ID
map(Types.UUID); // 1 - Entity UUID
map(Types.VAR_INT); // 2 - Entity Type
handler(trackerHandler());
}
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.VAR_INT); // Entity ID
wrapper.passthrough(Types.UUID); // Entity UUID
wrapper.passthrough(Types.VAR_INT); // Entity Type
trackerHandler().handle(wrapper);
});
}
@ -275,15 +273,15 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.VAR_INT); // 0 - Entity id
map(Types.UUID); // 1 - Entity UUID
map(Types.VAR_INT); // 2 - Entity Type
map(Types.DOUBLE); // 3 - X
map(Types.DOUBLE); // 4 - Y
map(Types.DOUBLE); // 5 - Z
map(Types.BYTE); // 6 - Pitch
map(Types.BYTE); // 7 - Yaw
map(Types.INT); // 8 - Data
map(Types.VAR_INT); // Entity id
map(Types.UUID); // Entity UUID
map(Types.VAR_INT); // Entity Type
map(Types.DOUBLE); // X
map(Types.DOUBLE); // Y
map(Types.DOUBLE); // Z
map(Types.BYTE); // Pitch
map(Types.BYTE); // Yaw
map(Types.INT); // Data
handler(trackerHandler());
handler(wrapper -> {
int entityId = wrapper.get(Types.VAR_INT, 0);
@ -297,31 +295,22 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
}
public void registerTrackerWithData1_19(C packetType, EntityType fallingBlockType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.VAR_INT); // Entity id
map(Types.UUID); // Entity UUID
map(Types.VAR_INT); // Entity type
map(Types.DOUBLE); // X
map(Types.DOUBLE); // Y
map(Types.DOUBLE); // Z
map(Types.BYTE); // Pitch
map(Types.BYTE); // Yaw
map(Types.BYTE); // Head yaw
map(Types.VAR_INT); // Data
handler(trackerHandler());
handler(wrapper -> {
if (protocol.getMappingData() == null) {
return;
}
protocol.registerClientbound(packetType, wrapper -> {
final int entityId = wrapper.passthrough(Types.VAR_INT);
wrapper.passthrough(Types.UUID); // Entity UUID
final int entityTypeId = wrapper.passthrough(Types.VAR_INT);
wrapper.passthrough(Types.DOUBLE); // X
wrapper.passthrough(Types.DOUBLE); // Y
wrapper.passthrough(Types.DOUBLE); // Z
wrapper.passthrough(Types.BYTE); // Pitch
wrapper.passthrough(Types.BYTE); // Yaw
wrapper.passthrough(Types.BYTE); // Head yaw
final int data = wrapper.passthrough(Types.VAR_INT);
int entityId = wrapper.get(Types.VAR_INT, 0);
EntityType entityType = tracker(wrapper.user()).entityType(entityId);
if (entityType == fallingBlockType) {
wrapper.set(Types.VAR_INT, 2, protocol.getMappingData().getNewBlockStateId(wrapper.get(Types.VAR_INT, 2)));
}
});
final EntityType entityType = trackAndRewrite(wrapper, entityTypeId, entityId);
if (protocol.getMappingData() != null && entityType == fallingBlockType) {
final int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(data);
wrapper.set(Types.VAR_INT, 2, mappedBlockStateId);
}
});
}
@ -388,12 +377,13 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
}
public PacketHandler playerTrackerHandler() {
return wrapper -> {
final EntityTracker tracker = tracker(wrapper.user());
final int entityId = wrapper.get(Types.INT, 0);
return wrapper -> trackPlayer(wrapper.user(), wrapper.get(Types.INT, 0));
}
public void trackPlayer(final UserConnection connection, final int entityId) {
final EntityTracker tracker = tracker(connection);
tracker.setClientEntityId(entityId);
tracker.addEntity(entityId, tracker.playerType());
};
}
/**
@ -533,6 +523,17 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
}
}
public EntityType trackAndRewrite(final PacketWrapper wrapper, final int typeId, final int entityId) {
final int mappedTypeId = newEntityId(typeId);
if (mappedTypeId != typeId) {
wrapper.set(Types.VAR_INT, 1, mappedTypeId);
}
final EntityType entityType = typeFromId(trackMappedType ? mappedTypeId : typeId);
tracker(wrapper.user()).addEntity(entityId, entityType);
return entityType;
}
// ---------------------------------------------------------------------------
// Sub 1.14.1 methods
@ -546,16 +547,7 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
return wrapper -> {
int entityId = wrapper.get(Types.VAR_INT, 0);
int type = wrapper.get(Types.VAR_INT, 1);
int newType = newEntityId(type);
if (newType != type) {
wrapper.set(Types.VAR_INT, 1, newType);
}
EntityType entType = typeFromId(trackMappedType ? newType : type);
// Register Type ID
tracker(wrapper.user()).addEntity(entityId, entType);
trackAndRewrite(wrapper, type, entityId);
if (dataType != null) {
handleEntityData(entityId, wrapper.get(dataType, 0), wrapper.user());
}

View File

@ -145,13 +145,10 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
}
public void registerSetSlot(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.UNSIGNED_BYTE); // Container id
map(Types.SHORT); // Slot id
handler(wrapper -> passthroughClientboundItem(wrapper));
}
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.UNSIGNED_BYTE); // Container id
wrapper.passthrough(Types.SHORT); // Slot id
passthroughClientboundItem(wrapper);
});
}
@ -174,24 +171,18 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
// Sub 1.16
public void registerSetEquippedItem(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.VAR_INT); // Entity ID
map(Types.VAR_INT); // Slot ID
handler(wrapper -> passthroughClientboundItem(wrapper));
}
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.VAR_INT); // Entity ID
wrapper.passthrough(Types.VAR_INT); // Slot ID
passthroughClientboundItem(wrapper);
});
}
// 1.16+
public void registerSetEquipment(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.VAR_INT); // 0 - Entity ID
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.VAR_INT); // Entity ID
handler(wrapper -> {
byte slot;
do {
slot = wrapper.passthrough(Types.BYTE);
@ -200,30 +191,22 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
} while (slot < 0);
});
}
});
}
public void registerSetCreativeModeSlot(S packetType) {
protocol.registerServerbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.SHORT); // 0 - Slot
handler(wrapper -> passthroughServerboundItem(wrapper));
}
protocol.registerServerbound(packetType, wrapper -> {
wrapper.passthrough(Types.SHORT); // Slot
passthroughServerboundItem(wrapper);
});
}
public void registerContainerClick(S packetType) {
protocol.registerServerbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.UNSIGNED_BYTE); // 0 - Container ID
map(Types.SHORT); // 1 - Slot
map(Types.BYTE); // 2 - Button
map(Types.SHORT); // 3 - Action number
map(Types.VAR_INT); // 4 - Mode
handler(wrapper -> passthroughServerboundItem(wrapper));
}
protocol.registerServerbound(packetType, wrapper -> {
wrapper.passthrough(Types.UNSIGNED_BYTE); // Container ID
wrapper.passthrough(Types.SHORT); // Slot
wrapper.passthrough(Types.BYTE); // Button
wrapper.passthrough(Types.SHORT); // Action number
wrapper.passthrough(Types.VAR_INT); // Mode
passthroughServerboundItem(wrapper);
});
}
@ -460,11 +443,9 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
// Pre 1.21 for enchantments
public void registerContainerSetData(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.UNSIGNED_BYTE); // Container id
handler(wrapper -> {
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.UNSIGNED_BYTE); // Container id
Mappings mappings = protocol.getMappingData().getEnchantmentMappings();
if (mappings == null) {
return;
@ -477,8 +458,6 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
}
});
}
});
}
// Not the very best place for this, but has to stay here until *everything* is abstracted
public void registerLevelParticles(C packetType, Type<?> coordType) {

View File

@ -22,7 +22,6 @@ import com.viaversion.viaversion.api.minecraft.SoundEvent;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Types;
public class SoundRewriter<C extends ClientboundPacketType> {
@ -40,12 +39,9 @@ public class SoundRewriter<C extends ClientboundPacketType> {
}
public void registerSound(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
map(Types.VAR_INT); // Sound id
handler(getSoundHandler());
}
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.VAR_INT); // Sound id
getSoundHandler().handle(wrapper);
});
}