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

Merge pull request #1601 from KennyTV/abstraction

Cleanup some block rewrite methods
Dieser Commit ist enthalten in:
Myles 2020-01-06 14:56:17 +00:00 committet von GitHub
Commit 86af7f054a
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
7 geänderte Dateien mit 233 neuen und 311 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,131 @@
package us.myles.ViaVersion.api.rewriters;
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.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 IdRewriteFunction blockStateRewriter;
private final IdRewriteFunction blockRewriter;
private final Type<Position> positionType;
public BlockRewriter(Protocol protocol, Type<Position> positionType, IdRewriteFunction blockStateRewriter, IdRewriteFunction 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, IdRewriteFunction 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) {
registerSpawnParticle(coordType, oldPacketId, newPacketId, blockId, fallingDustId, itemId, null, itemRewriteFunction, itemType);
}
public void registerSpawnParticle(Type<?> coordType, int oldPacketId, int newPacketId, int blockId, int fallingDustId, int itemId,
IdRewriteFunction particleRewriteFunction, 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(wrapper -> {
int id = wrapper.get(Type.INT, 0);
if (id == -1) return;
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));
}
if (particleRewriteFunction != null) {
int newId = particleRewriteFunction.rewrite(id);
if (newId != id) {
wrapper.set(Type.INT, 0, newId);
}
}
});
}
});
}
}

Datei anzeigen

@ -0,0 +1,7 @@
package us.myles.ViaVersion.api.rewriters;
@FunctionalInterface
public interface IdRewriteFunction {
int rewrite(int id);
}

Datei anzeigen

@ -85,7 +85,7 @@ public class ItemRewriter {
}); });
} }
public void registerSetCooldown(int oldPacketId, int newPacketId, ItemIdRewriteFunction itemIDRewriteFunction) { public void registerSetCooldown(int oldPacketId, int newPacketId, IdRewriteFunction itemIDRewriteFunction) {
protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
@ -120,10 +120,4 @@ public class ItemRewriter {
void rewrite(Item item); void rewrite(Item item);
} }
@FunctionalInterface
public interface ItemIdRewriteFunction {
int rewrite(int itemId);
}
} }

Datei anzeigen

@ -1,12 +1,12 @@
package us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets; package us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets;
import us.myles.ViaVersion.api.PacketWrapper; 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.Chunk;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper; 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.api.type.Type;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; 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 class WorldPackets {
public static void register(Protocol protocol) { public static void register(Protocol protocol) {
BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION, Protocol1_13_1To1_13::getNewBlockStateId, Protocol1_13_1To1_13::getNewBlockId);
//Chunk //Chunk
protocol.registerOutgoing(State.PLAY, 0x22, 0x22, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x22, 0x22, new PacketRemapper() {
@Override @Override
@ -37,81 +39,17 @@ public class WorldPackets {
} }
}); });
// Block Action // Block action
protocol.registerOutgoing(State.PLAY, 0x0A, 0x0A, new PacketRemapper() { blockRewriter.registerBlockAction(0x0A, 0x0A);
@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 Change // Block Change
protocol.registerOutgoing(State.PLAY, 0xB, 0xB, new PacketRemapper() { blockRewriter.registerBlockChange(0xB, 0xB);
@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));
}
});
}
});
// Multi Block Change // Multi Block Change
protocol.registerOutgoing(State.PLAY, 0xF, 0xF, new PacketRemapper() { blockRewriter.registerMultiBlockChange(0xF, 0xF);
@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));
}
}
});
}
});
// Effect packet // Effect packet
protocol.registerOutgoing(State.PLAY, 0x23, 0x23, new PacketRemapper() { blockRewriter.registerEffect(0x23, 0x23, 1010, 2001, InventoryPackets::getNewItemId);
@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));
}
}
});
}
});
//join game //join game
protocol.registerOutgoing(State.PLAY, 0x25, 0x25, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x25, 0x25, new PacketRemapper() {
@ -150,33 +88,6 @@ public class WorldPackets {
}); });
//spawn particle //spawn particle
protocol.registerOutgoing(State.PLAY, 0x24, 0x24, new PacketRemapper() { blockRewriter.registerSpawnParticle(Type.FLOAT, 0x24, 0x24, 3, 20, 27, InventoryPackets::toClient, Type.FLAT_ITEM);
@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));
}
}
});
}
});
} }
} }

Datei anzeigen

@ -5,7 +5,6 @@ import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.entities.Entity1_14Types; import us.myles.ViaVersion.api.entities.Entity1_14Types;
import us.myles.ViaVersion.api.minecraft.BlockChangeRecord;
import us.myles.ViaVersion.api.minecraft.BlockFace; import us.myles.ViaVersion.api.minecraft.BlockFace;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
@ -14,6 +13,7 @@ import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.remapper.ValueCreator; import us.myles.ViaVersion.api.remapper.ValueCreator;
import us.myles.ViaVersion.api.rewriters.BlockRewriter;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type;
@ -39,6 +39,7 @@ public class WorldPackets {
} }
public static void register(final Protocol protocol) { public static void register(final Protocol protocol) {
BlockRewriter blockRewriter = new BlockRewriter(protocol, null, Protocol1_14To1_13_2::getNewBlockStateId, Protocol1_14To1_13_2::getNewBlockId);
// Block Break Animation // Block Break Animation
protocol.registerOutgoing(State.PLAY, 0x08, 0x08, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x08, 0x08, new PacketRemapper() {
@ -107,24 +108,7 @@ public class WorldPackets {
}); });
// Multi Block Change // Multi Block Change
protocol.registerOutgoing(State.PLAY, 0x0F, 0x0F, new PacketRemapper() { blockRewriter.registerMultiBlockChange(0x0F, 0x0F);
@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_14To1_13_2.getNewBlockStateId(id));
}
}
});
}
});
// Explosion // Explosion
protocol.registerOutgoing(State.PLAY, 0x1E, 0x1C, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x1E, 0x1C, new PacketRemapper() {
@ -302,39 +286,9 @@ public class WorldPackets {
} }
}); });
// Spawn Particle // Spawn particle
protocol.registerOutgoing(State.PLAY, 0x24, 0x23, new PacketRemapper() { blockRewriter.registerSpawnParticle(Type.FLOAT, 0x24, 0x23, 3, 20, 27,
@Override MetadataRewriter1_14To1_13_2::getNewParticleId, InventoryPackets::toClient, Type.FLAT_VAR_INT_ITEM);
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_14To1_13_2.getNewBlockStateId(data));
} else if (id == 27) {
InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
int newId = MetadataRewriter1_14To1_13_2.getNewParticleId(id);
if (newId != id) {
wrapper.set(Type.INT, 0, newId);
}
}
});
}
});
// Join Game // Join Game
protocol.registerOutgoing(State.PLAY, 0x25, 0x25, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x25, 0x25, new PacketRemapper() {

Datei anzeigen

@ -14,78 +14,78 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
public class PlayerPackets { public class PlayerPackets {
public static void register(Protocol protocol) { public static void register(Protocol protocol) {
// Respawn // Respawn
protocol.registerOutgoing(State.PLAY, 0x3A, 0x3B, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x3A, 0x3B, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.INT); map(Type.INT);
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0); int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId); clientWorld.setEnvironment(dimensionId);
} }
}); });
create(new ValueCreator() { create(new ValueCreator() {
@Override @Override
public void write(PacketWrapper wrapper) throws Exception { public void write(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.LONG, 0L); // Level Seed wrapper.write(Type.LONG, 0L); // Level Seed
} }
}); });
} }
}); });
// Join Game // Join Game
protocol.registerOutgoing(State.PLAY, 0x25, 0x26, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x25, 0x26, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.INT); // 0 - Entity ID map(Type.INT); // 0 - Entity ID
map(Type.UNSIGNED_BYTE); // 1 - Gamemode map(Type.UNSIGNED_BYTE); // 1 - Gamemode
map(Type.INT); // 2 - Dimension map(Type.INT); // 2 - Dimension
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
Entity1_15Types.EntityType entType = Entity1_15Types.EntityType.PLAYER; Entity1_15Types.EntityType entType = Entity1_15Types.EntityType.PLAYER;
EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class); EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class);
tracker.addEntity(wrapper.get(Type.INT, 0), entType); tracker.addEntity(wrapper.get(Type.INT, 0), entType);
} }
}); });
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
// Store the player // Store the player
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class); ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1); int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId); clientChunks.setEnvironment(dimensionId);
// Register Type ID // Register Type ID
EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class); EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class);
int entityId = wrapper.get(Type.INT, 0); int entityId = wrapper.get(Type.INT, 0);
tracker.addEntity(entityId, Entity1_15Types.EntityType.PLAYER); tracker.addEntity(entityId, Entity1_15Types.EntityType.PLAYER);
} }
}); });
create(new ValueCreator() { create(new ValueCreator() {
@Override @Override
public void write(PacketWrapper wrapper) throws Exception { public void write(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.LONG, 0L); // Level Seed wrapper.write(Type.LONG, 0L); // Level Seed
} }
}); });
map(Type.UNSIGNED_BYTE); // 3 - Max Players map(Type.UNSIGNED_BYTE); // 3 - Max Players
map(Type.STRING); // 4 - Level Type map(Type.STRING); // 4 - Level Type
map(Type.VAR_INT); // 5 - View Distance map(Type.VAR_INT); // 5 - View Distance
map(Type.BOOLEAN); // 6 - Reduce Debug Info map(Type.BOOLEAN); // 6 - Reduce Debug Info
create(new ValueCreator() { create(new ValueCreator() {
@Override @Override
public void write(PacketWrapper wrapper) throws Exception { public void write(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.BOOLEAN, !Via.getConfig().is1_15InstantRespawn()); // Show Death Screen 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; package us.myles.ViaVersion.protocols.protocol1_15to1_14_4.packets;
import us.myles.ViaVersion.api.PacketWrapper; 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.Chunk;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper; 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.api.type.Type;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type; 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 class WorldPackets {
public static void register(Protocol protocol) { public static void register(Protocol protocol) {
// Acknowledge player digging BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_15To1_14_4::getNewBlockStateId, Protocol1_15To1_14_4::getNewBlockId);
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));
}
});
}
});
// Block Action // Block action
protocol.registerOutgoing(State.PLAY, 0x0A, 0x0B, new PacketRemapper() { blockRewriter.registerBlockAction(0x0A, 0x0B);
@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 Change // Block Change
protocol.registerOutgoing(State.PLAY, 0x0B, 0x0C, new PacketRemapper() { blockRewriter.registerBlockChange(0x0B, 0x0C);
@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));
}
});
}
});
// Multi Block Change // Multi Block Change
protocol.registerOutgoing(State.PLAY, 0x0F, 0x10, new PacketRemapper() { blockRewriter.registerMultiBlockChange(0x0F, 0x10);
@Override
public void registerMap() { // Acknowledge player digging
map(Type.INT); // 0 - Chunk X blockRewriter.registerAcknowledgePlayerDigging(0x5C, 0x08);
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));
}
}
});
}
});
// Chunk Data // Chunk Data
protocol.registerOutgoing(State.PLAY, 0x21, 0x22, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x21, 0x22, new PacketRemapper() {
@ -135,26 +79,7 @@ public class WorldPackets {
}); });
// Effect // Effect
protocol.registerOutgoing(State.PLAY, 0x22, 0x23, new PacketRemapper() { blockRewriter.registerEffect(0x22, 0x23, 1010, 2001, InventoryPackets::getNewItemId);
@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));
}
}
});
}
});
// Spawn Particle // Spawn Particle
protocol.registerOutgoing(State.PLAY, 0x23, 0x24, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x23, 0x24, new PacketRemapper() {