3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-28 17:10:13 +01:00

Common methods for block rewriting

Dieser Commit ist enthalten in:
KennyTV 2019-12-11 12:03:30 +01:00
Ursprung cd9566fbc8
Commit 1c1ec10a0a
4 geänderte Dateien mit 217 neuen und 252 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,129 @@
package us.myles.ViaVersion.api.rewriters;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.minecraft.BlockChangeRecord;
import us.myles.ViaVersion.api.minecraft.Position;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
// If any of these methods become outdated, just create a new rewriter overriding the methods
public class BlockRewriter {
private final Protocol protocol;
private final BlockIdRewriteFunction blockStateRewriter;
private final BlockIdRewriteFunction blockRewriter;
private final Type<Position> positionType;
public BlockRewriter(Protocol protocol, Type<Position> positionType, BlockIdRewriteFunction blockStateRewriter, BlockIdRewriteFunction blockRewriter) {
this.protocol = protocol;
this.positionType = positionType;
this.blockStateRewriter = blockStateRewriter;
this.blockRewriter = blockRewriter;
}
public void registerBlockAction(int oldPacketId, int newPacketId) {
protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override
public void registerMap() {
map(positionType); // Location
map(Type.UNSIGNED_BYTE); // Action id
map(Type.UNSIGNED_BYTE); // Action param
map(Type.VAR_INT); // Block id - /!\ NOT BLOCK STATE
handler(wrapper -> wrapper.set(Type.VAR_INT, 0, blockRewriter.rewrite(wrapper.get(Type.VAR_INT, 0))));
}
});
}
public void registerBlockChange(int oldPacketId, int newPacketId) {
protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override
public void registerMap() {
map(positionType);
map(Type.VAR_INT);
handler(wrapper -> wrapper.set(Type.VAR_INT, 0, blockStateRewriter.rewrite(wrapper.get(Type.VAR_INT, 0))));
}
});
}
public void registerMultiBlockChange(int oldPacketId, int newPacketId) {
protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Chunk X
map(Type.INT); // 1 - Chunk Z
map(Type.BLOCK_CHANGE_RECORD_ARRAY); // 2 - Records
handler(wrapper -> {
for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
record.setBlockId(blockStateRewriter.rewrite(record.getBlockId()));
}
});
}
});
}
public void registerAcknowledgePlayerDigging(int oldPacketId, int newPacketId) {
// Same exact handler
registerBlockChange(oldPacketId, newPacketId);
}
public void registerEffect(int oldPacketId, int newPacketId, int playRecordId, int blockBreakId, ItemRewriter.ItemIdRewriteFunction itemIdRewriteFunction) {
protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // Effect Id
map(positionType); // Location
map(Type.INT); // Data
handler(wrapper -> {
int id = wrapper.get(Type.INT, 0);
int data = wrapper.get(Type.INT, 1);
if (id == playRecordId) { // Play record
wrapper.set(Type.INT, 1, itemIdRewriteFunction.rewrite(data));
} else if (id == blockBreakId) { // Block break + block break sound
wrapper.set(Type.INT, 1, blockStateRewriter.rewrite(data));
}
});
}
});
}
public void registerSpawnParticle(Type<?> coordType, int oldPacketId, int newPacketId, int blockId, int fallingDustId, int itemId,
ItemRewriter.RewriteFunction itemRewriteFunction, Type<Item> itemType) {
protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Particle ID
map(Type.BOOLEAN); // 1 - Long Distance
map(coordType); // 2 - X
map(coordType); // 3 - Y
map(coordType); // 4 - Z
map(Type.FLOAT); // 5 - Offset X
map(Type.FLOAT); // 6 - Offset Y
map(Type.FLOAT); // 7 - Offset Z
map(Type.FLOAT); // 8 - Particle Data
map(Type.INT); // 9 - Particle Count
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.INT, 0);
if (id == blockId || id == fallingDustId) {
int data = wrapper.passthrough(Type.VAR_INT);
wrapper.set(Type.VAR_INT, 0, blockStateRewriter.rewrite(data));
} else if (id == itemId) {
// Has to be like this, until we make *everything* object oriented inside of each protocol :(
itemRewriteFunction.rewrite(wrapper.passthrough(itemType));
}
}
});
}
});
}
@FunctionalInterface
public interface BlockIdRewriteFunction {
int rewrite(int itemId);
}
}

Datei anzeigen

@ -1,12 +1,12 @@
package us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.minecraft.BlockChangeRecord;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.BlockRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13;
@ -16,6 +16,8 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
public class WorldPackets {
public static void register(Protocol protocol) {
BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION, Protocol1_13_1To1_13::getNewBlockStateId, Protocol1_13_1To1_13::getNewBlockId);
//Chunk
protocol.registerOutgoing(State.PLAY, 0x22, 0x22, new PacketRemapper() {
@Override
@ -37,81 +39,17 @@ public class WorldPackets {
}
});
// Block Action
protocol.registerOutgoing(State.PLAY, 0x0A, 0x0A, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION); // Location
map(Type.UNSIGNED_BYTE); // Action id
map(Type.UNSIGNED_BYTE); // Action param
map(Type.VAR_INT); // Block id - /!\ NOT BLOCK STATE
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(Type.VAR_INT, 0, Protocol1_13_1To1_13.getNewBlockId(wrapper.get(Type.VAR_INT, 0)));
}
});
}
});
// Block action
blockRewriter.registerBlockAction(0x0A, 0x0A);
// Block Change
protocol.registerOutgoing(State.PLAY, 0xB, 0xB, new PacketRemapper() {
@Override
public void registerMap() {
map(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, Protocol1_13_1To1_13.getNewBlockStateId(id));
}
});
}
});
blockRewriter.registerBlockChange(0xB, 0xB);
// Multi Block Change
protocol.registerOutgoing(State.PLAY, 0xF, 0xF, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Chunk X
map(Type.INT); // 1 - Chunk Z
map(Type.BLOCK_CHANGE_RECORD_ARRAY); // 2 - Records
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Convert ids
for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
int id = record.getBlockId();
record.setBlockId(Protocol1_13_1To1_13.getNewBlockStateId(id));
}
}
});
}
});
blockRewriter.registerMultiBlockChange(0xF, 0xF);
// Effect packet
protocol.registerOutgoing(State.PLAY, 0x23, 0x23, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // Effect Id
map(Type.POSITION); // Location
map(Type.INT); // Data
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, InventoryPackets.getNewItemId(data));
} else if (id == 2001) { // Block break + block break sound
wrapper.set(Type.INT, 1, Protocol1_13_1To1_13.getNewBlockStateId(data));
}
}
});
}
});
blockRewriter.registerEffect(0x23, 0x23, 1010, 2001, InventoryPackets::getNewItemId);
//join game
protocol.registerOutgoing(State.PLAY, 0x25, 0x25, new PacketRemapper() {
@ -150,33 +88,6 @@ public class WorldPackets {
});
//spawn particle
protocol.registerOutgoing(State.PLAY, 0x24, 0x24, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Particle ID
map(Type.BOOLEAN); // 1 - Long Distance
map(Type.FLOAT); // 2 - X
map(Type.FLOAT); // 3 - Y
map(Type.FLOAT); // 4 - Z
map(Type.FLOAT); // 5 - Offset X
map(Type.FLOAT); // 6 - Offset Y
map(Type.FLOAT); // 7 - Offset Z
map(Type.FLOAT); // 8 - Particle Data
map(Type.INT); // 9 - Particle Count
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.INT, 0);
if (id == 3 || id == 20) {
int data = wrapper.passthrough(Type.VAR_INT);
wrapper.set(Type.VAR_INT, 0, Protocol1_13_1To1_13.getNewBlockStateId(data));
} else if (id == 27) {
InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_ITEM));
}
}
});
}
});
blockRewriter.registerSpawnParticle(Type.FLOAT, 0x24, 0x24, 3, 20, 27, InventoryPackets::toClient, Type.FLAT_ITEM);
}
}

Datei anzeigen

@ -14,78 +14,78 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
public class PlayerPackets {
public static void register(Protocol protocol) {
// Respawn
protocol.registerOutgoing(State.PLAY, 0x3A, 0x3B, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId);
}
});
create(new ValueCreator() {
@Override
public void write(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.LONG, 0L); // Level Seed
}
});
}
});
public static void register(Protocol protocol) {
// Respawn
protocol.registerOutgoing(State.PLAY, 0x3A, 0x3B, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId);
}
});
create(new ValueCreator() {
@Override
public void write(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.LONG, 0L); // Level Seed
}
});
}
});
// Join Game
protocol.registerOutgoing(State.PLAY, 0x25, 0x26, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Entity ID
map(Type.UNSIGNED_BYTE); // 1 - Gamemode
map(Type.INT); // 2 - Dimension
// Join Game
protocol.registerOutgoing(State.PLAY, 0x25, 0x26, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Entity ID
map(Type.UNSIGNED_BYTE); // 1 - Gamemode
map(Type.INT); // 2 - Dimension
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Entity1_15Types.EntityType entType = Entity1_15Types.EntityType.PLAYER;
EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class);
tracker.addEntity(wrapper.get(Type.INT, 0), entType);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Store the player
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Entity1_15Types.EntityType entType = Entity1_15Types.EntityType.PLAYER;
EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class);
tracker.addEntity(wrapper.get(Type.INT, 0), entType);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Store the player
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
// Register Type ID
EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class);
int entityId = wrapper.get(Type.INT, 0);
tracker.addEntity(entityId, Entity1_15Types.EntityType.PLAYER);
}
});
create(new ValueCreator() {
@Override
public void write(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.LONG, 0L); // Level Seed
}
});
// Register Type ID
EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class);
int entityId = wrapper.get(Type.INT, 0);
tracker.addEntity(entityId, Entity1_15Types.EntityType.PLAYER);
}
});
create(new ValueCreator() {
@Override
public void write(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.LONG, 0L); // Level Seed
}
});
map(Type.UNSIGNED_BYTE); // 3 - Max Players
map(Type.STRING); // 4 - Level Type
map(Type.VAR_INT); // 5 - View Distance
map(Type.BOOLEAN); // 6 - Reduce Debug Info
map(Type.UNSIGNED_BYTE); // 3 - Max Players
map(Type.STRING); // 4 - Level Type
map(Type.VAR_INT); // 5 - View Distance
map(Type.BOOLEAN); // 6 - Reduce Debug Info
create(new ValueCreator() {
@Override
public void write(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.BOOLEAN, !Via.getConfig().is1_15InstantRespawn()); // Show Death Screen
}
});
}
});
}
create(new ValueCreator() {
@Override
public void write(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.BOOLEAN, !Via.getConfig().is1_15InstantRespawn()); // Show Death Screen
}
});
}
});
}
}

Datei anzeigen

@ -1,12 +1,12 @@
package us.myles.ViaVersion.protocols.protocol1_15to1_14_4.packets;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.minecraft.BlockChangeRecord;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.BlockRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type;
@ -17,75 +17,19 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
public class WorldPackets {
public static void register(Protocol protocol) {
// Acknowledge player digging
protocol.registerOutgoing(State.PLAY, 0x5C, 0x08, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14);
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int blockState = wrapper.get(Type.VAR_INT, 0);
wrapper.set(Type.VAR_INT, 0, Protocol1_15To1_14_4.getNewBlockStateId(blockState));
}
});
}
});
BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_15To1_14_4::getNewBlockStateId, Protocol1_15To1_14_4::getNewBlockId);
// Block Action
protocol.registerOutgoing(State.PLAY, 0x0A, 0x0B, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14); // Location
map(Type.UNSIGNED_BYTE); // Action id
map(Type.UNSIGNED_BYTE); // Action param
map(Type.VAR_INT); // Block id - /!\ NOT BLOCK STATE
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(Type.VAR_INT, 0, Protocol1_15To1_14_4.getNewBlockId(wrapper.get(Type.VAR_INT, 0)));
}
});
}
});
// Block action
blockRewriter.registerBlockAction(0x0A, 0x0B);
// Block Change
protocol.registerOutgoing(State.PLAY, 0x0B, 0x0C, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14);
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, Protocol1_15To1_14_4.getNewBlockStateId(id));
}
});
}
});
blockRewriter.registerBlockChange(0x0B, 0x0C);
// Multi Block Change
protocol.registerOutgoing(State.PLAY, 0x0F, 0x10, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Chunk X
map(Type.INT); // 1 - Chunk Z
map(Type.BLOCK_CHANGE_RECORD_ARRAY); // 2 - Records
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Convert ids
for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
int id = record.getBlockId();
record.setBlockId(Protocol1_15To1_14_4.getNewBlockStateId(id));
}
}
});
}
});
blockRewriter.registerMultiBlockChange(0x0F, 0x10);
// Acknowledge player digging
blockRewriter.registerAcknowledgePlayerDigging(0x5C, 0x08);
// Chunk Data
protocol.registerOutgoing(State.PLAY, 0x21, 0x22, new PacketRemapper() {
@ -135,26 +79,7 @@ public class WorldPackets {
});
// Effect
protocol.registerOutgoing(State.PLAY, 0x22, 0x23, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // Effect Id
map(Type.POSITION1_14); // Location
map(Type.INT); // Data
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, InventoryPackets.getNewItemId(data));
} else if (id == 2001) { // Block break + block break sound
wrapper.set(Type.INT, 1, data = Protocol1_15To1_14_4.getNewBlockStateId(data));
}
}
});
}
});
blockRewriter.registerEffect(0x22, 0x23, 1010, 2001, InventoryPackets::getNewItemId);
// Spawn Particle
protocol.registerOutgoing(State.PLAY, 0x23, 0x24, new PacketRemapper() {