Cleanup entity tracking/rewrites and item rewrites (#155)

* Cleanup entity tracking and rewrites

* Fix

* Item rewriter cleanup
This commit is contained in:
Nassim 2019-10-12 11:08:18 +02:00 committed by Myles
parent 1d12eb0b95
commit 8e032f6499
26 changed files with 417 additions and 1349 deletions

View File

@ -40,6 +40,7 @@ public class EntityData {
} }
public interface MetaCreator { public interface MetaCreator {
void handle(MetaStorage storage); void handle(MetaStorage storage);
} }
} }

View File

@ -41,15 +41,14 @@ public abstract class BlockItemRewriter<T extends BackwardsProtocol> extends Rew
} }
protected Item handleItemToClient(Item i) { protected Item handleItemToClient(Item i) {
if (i == null) if (i == null) return null;
return null;
if (!replacementData.containsKey(i.getIdentifier()))
return i;
BlockItemSettings data = replacementData.get(i.getIdentifier()); BlockItemSettings data = replacementData.get(i.getIdentifier());
if (data == null) return i;
Item original = ItemUtil.copyItem(i); Item original = ItemUtil.copyItem(i);
if (data.hasRepItem()) { if (data.hasRepItem()) {
i = ItemUtil.copyItem(data.getRepItem()); ItemUtil.copyItem(i, data.getRepItem());
if (i.getTag() == null) if (i.getTag() == null)
i.setTag(new CompoundTag("")); i.setTag(new CompoundTag(""));
@ -58,9 +57,11 @@ public abstract class BlockItemRewriter<T extends BackwardsProtocol> extends Rew
i.getTag().put(createViaNBT(original)); i.getTag().put(createViaNBT(original));
// Keep original data // Keep original data
if (original.getTag() != null) if (original.getTag() != null) {
for (Tag ai : original.getTag()) for (Tag ai : original.getTag()) {
i.getTag().put(ai); i.getTag().put(ai);
}
}
// Handle colors // Handle colors
if (i.getTag().contains("display")) { if (i.getTag().contains("display")) {

View File

@ -33,6 +33,7 @@ import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -64,15 +65,11 @@ public abstract class EntityRewriter<T extends BackwardsProtocol> extends Rewrit
} }
protected Optional<EntityData> getEntityData(EntityType type) { protected Optional<EntityData> getEntityData(EntityType type) {
if (!entityTypes.containsKey(type)) return Optional.ofNullable(entityTypes.get(type));
return Optional.empty();
return Optional.of(entityTypes.get(type));
} }
protected Optional<EntityData> getObjectData(ObjectType type) { protected Optional<EntityData> getObjectData(ObjectType type) {
if (!objectTypes.containsKey(type)) return Optional.ofNullable(objectTypes.get(type));
return Optional.empty();
return Optional.of(objectTypes.get(type));
} }
protected EntityData regEntType(EntityType oldEnt, EntityType replacement) { protected EntityData regEntType(EntityType oldEnt, EntityType replacement) {
@ -256,6 +253,89 @@ public abstract class EntityRewriter<T extends BackwardsProtocol> extends Rewrit
registerEntityDestroy(packetId, packetId); registerEntityDestroy(packetId, packetId);
} }
protected PacketHandler getObjectTrackerHandler() {
return new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(wrapper.user(), wrapper.get(Type.VAR_INT, 0), getObjectTypeFromId(wrapper.get(Type.BYTE, 0)));
}
};
}
protected PacketHandler getTrackerHandler(Type intType, int typeIndex) {
return new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Number id = (Number) wrapper.get(intType, typeIndex);
addTrackedEntity(wrapper.user(), wrapper.get(Type.VAR_INT, 0), getTypeFromId(id.intValue()));
}
};
}
protected PacketHandler getTrackerHandler() {
return getTrackerHandler(Type.VAR_INT, 1);
}
protected PacketHandler getTrackerHandler(EntityType entityType, Type intType) {
return new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(wrapper.user(), (int) wrapper.get(intType, 0), entityType);
}
};
}
protected PacketHandler getMobSpawnRewriter(Type<List<Metadata>> metaType) {
return new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
EntityType type = getEntityType(wrapper.user(), entityId);
MetaStorage storage = new MetaStorage(wrapper.get(metaType, 0));
handleMeta(wrapper.user(), entityId, storage);
Optional<EntityData> optEntDat = getEntityData(type);
if (optEntDat.isPresent()) {
EntityData data = optEntDat.get();
int replacementId = getOldEntityId(data.getReplacementId());
wrapper.set(Type.VAR_INT, 1, replacementId);
if (data.hasBaseMeta()) {
data.getDefaultMeta().handle(storage);
}
}
// Rewrite Metadata
wrapper.set(metaType, 0, storage.getMetaDataList());
}
};
}
protected PacketHandler getTrackerAndMetaHandler(Type<List<Metadata>> metaType, EntityType entityType) {
return new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(wrapper.user(), wrapper.get(Type.VAR_INT, 0), entityType);
List<Metadata> metaDataList = handleMeta(wrapper.user(), wrapper.get(Type.VAR_INT, 0),
new MetaStorage(wrapper.get(metaType, 0))).getMetaDataList();
wrapper.set(metaType, 0, metaDataList);
}
};
}
protected PacketHandler getDimensionHandler(int index) {
return new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, index);
clientWorld.setEnvironment(dimensionId);
}
};
}
protected EntityTracker.ProtocolEntityTracker getEntityTracker(UserConnection user) { protected EntityTracker.ProtocolEntityTracker getEntityTracker(UserConnection user) {
return user.get(EntityTracker.class).get(getProtocol()); return user.get(EntityTracker.class).get(getProtocol());
} }
@ -283,4 +363,19 @@ public abstract class EntityRewriter<T extends BackwardsProtocol> extends Rewrit
protected void setDisplayNameJson(boolean displayNameJson) { protected void setDisplayNameJson(boolean displayNameJson) {
isDisplayNameJson = displayNameJson; isDisplayNameJson = displayNameJson;
} }
protected abstract EntityType getTypeFromId(int typeId);
protected EntityType getObjectTypeFromId(int typeId) {
return getTypeFromId(typeId);
}
// Only needs to be overriden when getMobSpawnTracker is used
protected Optional<Integer> getOptOldEntityId(int newId) {
return Optional.of(newId);
}
protected int getOldEntityId(int newId) {
return newId;
}
} }

View File

@ -28,6 +28,7 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
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.ItemRewriter;
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_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type;
@ -41,7 +42,7 @@ import java.util.Optional;
public class BlockItemPackets1_11 extends BlockItemRewriter<Protocol1_10To1_11> { public class BlockItemPackets1_11 extends BlockItemRewriter<Protocol1_10To1_11> {
@Override @Override
protected void registerPackets(Protocol1_10To1_11 protocol) { protected void registerPackets(Protocol1_10To1_11 protocol) {
/* Item packets */ ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer);
// Set slot packet // Set slot packet
protocol.registerOutgoing(State.PLAY, 0x16, 0x16, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x16, 0x16, new PacketRemapper() {
@ -51,13 +52,7 @@ public class BlockItemPackets1_11 extends BlockItemRewriter<Protocol1_10To1_11>
map(Type.SHORT); // 1 - Slot ID map(Type.SHORT); // 1 - Slot ID
map(Type.ITEM); // 2 - Slot Value map(Type.ITEM); // 2 - Slot Value
handler(new PacketHandler() { handler(itemRewriter.itemToClientHandler(Type.ITEM));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
wrapper.set(Type.ITEM, 0, handleItemToClient(stack));
}
});
// Handle Llama // Handle Llama
handler(new PacketHandler() { handler(new PacketHandler() {
@ -110,22 +105,7 @@ public class BlockItemPackets1_11 extends BlockItemRewriter<Protocol1_10To1_11>
}); });
// Entity Equipment Packet // Entity Equipment Packet
protocol.registerOutgoing(State.PLAY, 0x3C, 0x3C, new PacketRemapper() { itemRewriter.registerEntityEquipment(Type.ITEM, 0x3C, 0x3C);
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Type.VAR_INT); // 1 - Slot ID
map(Type.ITEM); // 2 - Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
wrapper.set(Type.ITEM, 0, handleItemToClient(stack));
}
});
}
});
// Plugin message Packet -> Trading // Plugin message Packet -> Trading
protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() {
@ -169,13 +149,7 @@ public class BlockItemPackets1_11 extends BlockItemRewriter<Protocol1_10To1_11>
map(Type.VAR_INT); // 4 - Mode map(Type.VAR_INT); // 4 - Mode
map(Type.ITEM); // 5 - Clicked Item map(Type.ITEM); // 5 - Clicked Item
handler(new PacketHandler() { handler(itemRewriter.itemToServerHandler(Type.ITEM));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
handleItemToServer(item);
}
});
// Llama slot // Llama slot
handler(new PacketHandler() { handler(new PacketHandler() {
@ -198,25 +172,9 @@ public class BlockItemPackets1_11 extends BlockItemRewriter<Protocol1_10To1_11>
); );
// Creative Inventory Action // Creative Inventory Action
protocol.registerIncoming(State.PLAY, 0x18, 0x18, new PacketRemapper() { itemRewriter.registerCreativeInvAction(Type.ITEM, 0x18, 0x18);
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(Type.ITEM); // 1 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
handleItemToServer(item);
}
});
}
}
);
/* Block packets */ /* Block packets */
// Chunk packet // Chunk packet
protocol.registerOutgoing(State.PLAY, 0x20, 0x20, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x20, 0x20, new PacketRemapper() {
@Override @Override

View File

@ -30,7 +30,6 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.api.type.types.version.Types1_9;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import java.util.Optional; import java.util.Optional;
@ -53,16 +52,7 @@ public class EntityPackets1_11 extends EntityRewriter<Protocol1_10To1_11> {
map(Type.INT); // 8 - data map(Type.INT); // 8 - data
// Track Entity // Track Entity
handler(new PacketHandler() { handler(getObjectTrackerHandler());
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_11Types.getTypeFromId(wrapper.get(Type.BYTE, 0), true)
);
}
});
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
@ -131,16 +121,7 @@ public class EntityPackets1_11 extends EntityRewriter<Protocol1_10To1_11> {
map(Types1_9.METADATA_LIST); // 12 - Metadata map(Types1_9.METADATA_LIST); // 12 - Metadata
// Track entity // Track entity
handler(new PacketHandler() { handler(getTrackerHandler(Type.UNSIGNED_BYTE, 0));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_11Types.getTypeFromId(wrapper.get(Type.UNSIGNED_BYTE, 0), false)
);
}
});
// Rewrite entity type / metadata // Rewrite entity type / metadata
handler(new PacketHandler() { handler(new PacketHandler() {
@ -186,25 +167,8 @@ public class EntityPackets1_11 extends EntityRewriter<Protocol1_10To1_11> {
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(getTrackerHandler(Entity1_11Types.EntityType.PLAYER, Type.INT));
@Override handler(getDimensionHandler(1));
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.INT, 0),
Entity1_11Types.EntityType.PLAYER
);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientWorld.setEnvironment(dimensionId);
}
});
} }
}); });
@ -213,14 +177,7 @@ public class EntityPackets1_11 extends EntityRewriter<Protocol1_10To1_11> {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.INT); // 0 - Dimension ID map(Type.INT); // 0 - Dimension ID
handler(new PacketHandler() { handler(getDimensionHandler(0));
@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);
}
});
} }
}); });
@ -237,33 +194,7 @@ public class EntityPackets1_11 extends EntityRewriter<Protocol1_10To1_11> {
map(Type.BYTE); // 6 - Pitch map(Type.BYTE); // 6 - Pitch
map(Types1_9.METADATA_LIST); // 7 - Metadata list map(Types1_9.METADATA_LIST); // 7 - Metadata list
// Track Entity handler(getTrackerAndMetaHandler(Types1_9.METADATA_LIST, Entity1_11Types.EntityType.PLAYER));
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_11Types.EntityType.PLAYER
);
}
});
// Rewrite Metadata
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(
Types1_9.METADATA_LIST,
0,
handleMeta(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
new MetaStorage(wrapper.get(Types1_9.METADATA_LIST, 0))
).getMetaDataList()
);
}
});
} }
}); });
@ -290,7 +221,6 @@ public class EntityPackets1_11 extends EntityRewriter<Protocol1_10To1_11> {
wrapper.setId(0x1E); // Change Game State wrapper.setId(0x1E); // Change Game State
wrapper.write(Type.UNSIGNED_BYTE, (short) 10); // Play Elder Guardian animation wrapper.write(Type.UNSIGNED_BYTE, (short) 10); // Play Elder Guardian animation
wrapper.write(Type.FLOAT, 0F); wrapper.write(Type.FLOAT, 0F);
} }
} }
}); });
@ -508,4 +438,13 @@ public class EntityPackets1_11 extends EntityRewriter<Protocol1_10To1_11> {
return new Metadata(14, MetaType1_9.VarInt, type); return new Metadata(14, MetaType1_9.VarInt, type);
} }
@Override
protected EntityType getTypeFromId(int typeId) {
return Entity1_11Types.getTypeFromId(typeId, false);
}
@Override
protected EntityType getObjectTypeFromId(int typeId) {
return Entity1_11Types.getTypeFromId(typeId, true);
}
} }

View File

@ -22,12 +22,12 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
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.ItemRewriter;
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_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.DoubleTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.ListTag; import us.myles.viaversion.libs.opennbt.tag.builtin.ListTag;
import java.util.Collections; import java.util.Collections;
@ -35,7 +35,6 @@ import java.util.Collections;
public class BlockItemPackets1_12 extends BlockItemRewriter<Protocol1_11_1To1_12> { public class BlockItemPackets1_12 extends BlockItemRewriter<Protocol1_11_1To1_12> {
@Override @Override
protected void registerPackets(Protocol1_11_1To1_12 protocol) { protected void registerPackets(Protocol1_11_1To1_12 protocol) {
/* Item packets */
protocol.registerOutgoing(State.PLAY, 0x24, 0x24, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x24, 0x24, new PacketRemapper() {
@Override @Override
@ -74,59 +73,16 @@ public class BlockItemPackets1_12 extends BlockItemRewriter<Protocol1_11_1To1_12
} }
}); });
// Set slot packet ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer);
protocol.registerOutgoing(State.PLAY, 0x16, 0x16, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot ID
map(Type.ITEM); // 2 - Slot Value
handler(new PacketHandler() { // Set slot packet
@Override itemRewriter.registerSetSlot(Type.ITEM, 0x16, 0x16);
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
wrapper.set(Type.ITEM, 0, handleItemToClient(stack));
}
});
}
});
// Window items packet // Window items packet
protocol.registerOutgoing(State.PLAY, 0x14, 0x14, new PacketRemapper() { itemRewriter.registerWindowItems(Type.ITEM_ARRAY, 0x14, 0x14);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.ITEM_ARRAY); // 1 - Window Values
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.ITEM_ARRAY, 0);
for (int i = 0; i < stacks.length; i++)
stacks[i] = handleItemToClient(stacks[i]);
}
});
}
});
// Entity Equipment Packet // Entity Equipment Packet
protocol.registerOutgoing(State.PLAY, 0x3E, 0x3C, new PacketRemapper() { itemRewriter.registerEntityEquipment(Type.ITEM, 0x3E, 0x3C);
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Type.VAR_INT); // 1 - Slot ID
map(Type.ITEM); // 2 - Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
wrapper.set(Type.ITEM, 0, handleItemToClient(stack));
}
});
}
});
// Plugin message Packet -> Trading // Plugin message Packet -> Trading
protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() {
@ -200,81 +156,63 @@ public class BlockItemPackets1_12 extends BlockItemRewriter<Protocol1_11_1To1_12
); );
// Creative Inventory Action // Creative Inventory Action
protocol.registerIncoming(State.PLAY, 0x1B, 0x18, new PacketRemapper() { itemRewriter.registerCreativeInvAction(Type.ITEM, 0x1B, 0x18);
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(Type.ITEM); // 1 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
handleItemToServer(item);
}
});
}
}
);
/* Block packets */ /* Block packets */
// Chunk packet // Chunk packet
protocol.registerOutgoing(State.PLAY, 0x20, 0x20, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x20, 0x20, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override @Override
public void registerMap() { public void handle(PacketWrapper wrapper) throws Exception {
handler(new PacketHandler() { ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld); // Use the 1.9.4 Chunk type since nothing changed. Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld); // Use the 1.9.4 Chunk type since nothing changed.
Chunk chunk = wrapper.passthrough(type); Chunk chunk = wrapper.passthrough(type);
handleChunk(chunk); handleChunk(chunk);
}
});
} }
} });
); }
});
// Block Change Packet // Block Change Packet
protocol.registerOutgoing(State.PLAY, 0x0B, 0x0B, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x0B, 0x0B, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.POSITION); // 0 - Block Position map(Type.POSITION); // 0 - Block Position
map(Type.VAR_INT); // 1 - Block map(Type.VAR_INT); // 1 - Block
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
int idx = wrapper.get(Type.VAR_INT, 0); int idx = wrapper.get(Type.VAR_INT, 0);
wrapper.set(Type.VAR_INT, 0, handleBlockID(idx)); wrapper.set(Type.VAR_INT, 0, handleBlockID(idx));
}
});
} }
} });
); }
});
// Multi Block Change Packet // Multi Block Change Packet
protocol.registerOutgoing(State.PLAY, 0x10, 0x10, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x10, 0x10, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.INT); // 0 - Chunk X map(Type.INT); // 0 - Chunk X
map(Type.INT); // 1 - Chunk Z map(Type.INT); // 1 - Chunk Z
map(Type.BLOCK_CHANGE_RECORD_ARRAY); map(Type.BLOCK_CHANGE_RECORD_ARRAY);
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
record.setBlockId(handleBlockID(record.getBlockId())); record.setBlockId(handleBlockID(record.getBlockId()));
} }
}
});
} }
} });
); }
});
// Update Block Entity // Update Block Entity
protocol.registerOutgoing(State.PLAY, 0x09, 0x09, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x09, 0x09, new PacketRemapper() {

View File

@ -25,33 +25,17 @@ public class ChangedPacketIds1_12 extends Rewriter<Protocol1_11_1To1_12> {
p.registerOutgoing(State.PLAY, 0x26, 0x25); // Entity Relative Move p.registerOutgoing(State.PLAY, 0x26, 0x25); // Entity Relative Move
p.registerOutgoing(State.PLAY, 0x27, 0x26); // Entity Look and Relative Move p.registerOutgoing(State.PLAY, 0x27, 0x26); // Entity Look and Relative Move
p.registerOutgoing(State.PLAY, 0x28, 0x27); // Entity Look p.registerOutgoing(State.PLAY, 0x28, 0x27); // Entity Look
p.registerOutgoing(State.PLAY, 0x30, -1, new PacketRemapper() {
@Override p.cancelOutgoing(State.PLAY, 0x30); // Unlock Recipes
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.cancel();
}
});
}
}); // Unlock Recipes TODO does ViaVersion cancel the packet if the new id is -1?
// 0x31 -> 0x30 Destroy Entities handled in EntityPackets1_12.java // 0x31 -> 0x30 Destroy Entities handled in EntityPackets1_12.java
p.registerOutgoing(State.PLAY, 0x32, 0x31); // Remove Entity Effect p.registerOutgoing(State.PLAY, 0x32, 0x31); // Remove Entity Effect
p.registerOutgoing(State.PLAY, 0x33, 0x32); // Resource Pack Send p.registerOutgoing(State.PLAY, 0x33, 0x32); // Resource Pack Send
// 0x34 -> 0x33 Respawn handled in EntityPackets1_12.java // 0x34 -> 0x33 Respawn handled in EntityPackets1_12.java
p.registerOutgoing(State.PLAY, 0x35, 0x34); // Entity Head Look p.registerOutgoing(State.PLAY, 0x35, 0x34); // Entity Head Look
p.registerOutgoing(State.PLAY, 0x36, -1, new PacketRemapper() {
@Override p.cancelOutgoing(State.PLAY, 0x36); // Advancement Progress
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.cancel();
}
});
}
}); // Advancement Progress
p.registerOutgoing(State.PLAY, 0x37, 0x35); // World Border p.registerOutgoing(State.PLAY, 0x37, 0x35); // World Border
p.registerOutgoing(State.PLAY, 0x38, 0x36); //Camera p.registerOutgoing(State.PLAY, 0x38, 0x36); //Camera
p.registerOutgoing(State.PLAY, 0x39, 0x37); // Held Item Change (ClientBound) p.registerOutgoing(State.PLAY, 0x39, 0x37); // Held Item Change (ClientBound)
@ -73,17 +57,9 @@ public class ChangedPacketIds1_12 extends Rewriter<Protocol1_11_1To1_12> {
p.registerOutgoing(State.PLAY, 0x49, 0x47); // Player List Header And Footer p.registerOutgoing(State.PLAY, 0x49, 0x47); // Player List Header And Footer
p.registerOutgoing(State.PLAY, 0x4A, 0x48); // Collect Item p.registerOutgoing(State.PLAY, 0x4A, 0x48); // Collect Item
p.registerOutgoing(State.PLAY, 0x4B, 0x49); // Entity Teleport p.registerOutgoing(State.PLAY, 0x4B, 0x49); // Entity Teleport
p.registerOutgoing(State.PLAY, 0x4C, -1, new PacketRemapper() {
@Override p.cancelOutgoing(State.PLAY, 0x4C); // Advancements
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.cancel();
}
});
}
}); // Advancements
p.registerOutgoing(State.PLAY, 0x4D, 0x4A); // Entity Properties p.registerOutgoing(State.PLAY, 0x4D, 0x4A); // Entity Properties
p.registerOutgoing(State.PLAY, 0x4E, 0x4B); // Entity Effect p.registerOutgoing(State.PLAY, 0x4E, 0x4B); // Entity Effect

View File

@ -12,7 +12,6 @@ package nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.packets;
import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.ViaBackwards;
import nl.matsv.viabackwards.api.entities.storage.EntityData; import nl.matsv.viabackwards.api.entities.storage.EntityData;
import nl.matsv.viabackwards.api.entities.storage.MetaStorage;
import nl.matsv.viabackwards.api.exceptions.RemovedValueException; import nl.matsv.viabackwards.api.exceptions.RemovedValueException;
import nl.matsv.viabackwards.api.rewriters.EntityRewriter; import nl.matsv.viabackwards.api.rewriters.EntityRewriter;
import nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12; import nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12;
@ -30,12 +29,12 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_12; import us.myles.ViaVersion.api.type.types.version.Types1_12;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import java.util.Optional; import java.util.Optional;
public class EntityPackets1_12 extends EntityRewriter<Protocol1_11_1To1_12> { public class EntityPackets1_12 extends EntityRewriter<Protocol1_11_1To1_12> {
@Override @Override
protected void registerPackets(Protocol1_11_1To1_12 protocol) { protected void registerPackets(Protocol1_11_1To1_12 protocol) {
// Spawn Object // Spawn Object
@ -53,16 +52,7 @@ public class EntityPackets1_12 extends EntityRewriter<Protocol1_11_1To1_12> {
map(Type.INT); // 8 - data map(Type.INT); // 8 - data
// Track Entity // Track Entity
handler(new PacketHandler() { handler(getObjectTrackerHandler());
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_12Types.getTypeFromId(wrapper.get(Type.BYTE, 0), true)
);
}
});
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
@ -131,48 +121,10 @@ public class EntityPackets1_12 extends EntityRewriter<Protocol1_11_1To1_12> {
map(Types1_12.METADATA_LIST); // 12 - Metadata map(Types1_12.METADATA_LIST); // 12 - Metadata
// Track entity // Track entity
handler(new PacketHandler() { handler(getTrackerHandler());
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_12Types.getTypeFromId(wrapper.get(Type.VAR_INT, 1), false)
);
}
});
// Rewrite entity type / metadata // Rewrite entity type / metadata
handler(new PacketHandler() { handler(getMobSpawnRewriter(Types1_12.METADATA_LIST));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
EntityType type = getEntityType(wrapper.user(), entityId);
MetaStorage storage = new MetaStorage(wrapper.get(Types1_12.METADATA_LIST, 0));
handleMeta(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
storage
);
Optional<EntityData> optEntDat = getEntityData(type);
if (optEntDat.isPresent()) {
EntityData data = optEntDat.get();
wrapper.set(Type.VAR_INT, 1, data.getReplacementId());
if (data.hasBaseMeta())
data.getDefaultMeta().handle(storage);
}
// Rewrite Metadata
wrapper.set(
Types1_12.METADATA_LIST,
0,
storage.getMetaDataList()
);
}
});
} }
}); });
@ -192,33 +144,7 @@ public class EntityPackets1_12 extends EntityRewriter<Protocol1_11_1To1_12> {
map(Type.BYTE); // 6 - Pitch map(Type.BYTE); // 6 - Pitch
map(Types1_12.METADATA_LIST); // 7 - Metadata list map(Types1_12.METADATA_LIST); // 7 - Metadata list
// Track Entity handler(getTrackerAndMetaHandler(Types1_12.METADATA_LIST, Entity1_12Types.EntityType.PLAYER));
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_12Types.EntityType.PLAYER
);
}
});
// Rewrite Metadata
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(
Types1_12.METADATA_LIST,
0,
handleMeta(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
new MetaStorage(wrapper.get(Types1_12.METADATA_LIST, 0))
).getMetaDataList()
);
}
});
} }
}); });
@ -230,16 +156,9 @@ public class EntityPackets1_12 extends EntityRewriter<Protocol1_11_1To1_12> {
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(getTrackerHandler(Entity1_12Types.EntityType.PLAYER, Type.INT));
@Override
public void handle(PacketWrapper wrapper) throws Exception { handler(getDimensionHandler(1));
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.INT, 0),
Entity1_12Types.EntityType.PLAYER
);
}
});
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
@ -249,15 +168,6 @@ public class EntityPackets1_12 extends EntityRewriter<Protocol1_11_1To1_12> {
} }
}); });
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientWorld.setEnvironment(dimensionId);
}
});
// Send fake inventory achievement // Send fake inventory achievement
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
@ -279,14 +189,7 @@ public class EntityPackets1_12 extends EntityRewriter<Protocol1_11_1To1_12> {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.INT); // 0 - Dimension ID map(Type.INT); // 0 - Dimension ID
handler(new PacketHandler() { handler(getDimensionHandler(0));
@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);
}
});
} }
}); });
@ -390,4 +293,14 @@ public class EntityPackets1_12 extends EntityRewriter<Protocol1_11_1To1_12> {
throw RemovedValueException.EX; throw RemovedValueException.EX;
}); });
} }
@Override
protected EntityType getTypeFromId(int typeId) {
return Entity1_12Types.getTypeFromId(typeId, false);
}
@Override
protected EntityType getObjectTypeFromId(final int typeId) {
return Entity1_12Types.getTypeFromId(typeId, true);
}
} }

View File

@ -12,7 +12,6 @@ package nl.matsv.viabackwards.protocol.protocol1_11to1_11_1.packets;
import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.ViaBackwards;
import nl.matsv.viabackwards.api.entities.storage.EntityData; import nl.matsv.viabackwards.api.entities.storage.EntityData;
import nl.matsv.viabackwards.api.entities.storage.MetaStorage;
import nl.matsv.viabackwards.api.rewriters.EntityRewriter; import nl.matsv.viabackwards.api.rewriters.EntityRewriter;
import nl.matsv.viabackwards.protocol.protocol1_11to1_11_1.Protocol1_11To1_11_1; import nl.matsv.viabackwards.protocol.protocol1_11to1_11_1.Protocol1_11To1_11_1;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
@ -24,7 +23,6 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.api.type.types.version.Types1_9;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import java.util.Optional; import java.util.Optional;
@ -47,16 +45,7 @@ public class EntityPackets1_11_1 extends EntityRewriter<Protocol1_11To1_11_1> {
map(Type.INT); // 8 - data map(Type.INT); // 8 - data
// Track Entity // Track Entity
handler(new PacketHandler() { handler(getObjectTrackerHandler());
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_11Types.getTypeFromId(wrapper.get(Type.BYTE, 0), true)
);
}
});
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
@ -106,48 +95,10 @@ public class EntityPackets1_11_1 extends EntityRewriter<Protocol1_11To1_11_1> {
map(Types1_9.METADATA_LIST); // 12 - Metadata map(Types1_9.METADATA_LIST); // 12 - Metadata
// Track entity // Track entity
handler(new PacketHandler() { handler(getTrackerHandler());
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_11Types.getTypeFromId(wrapper.get(Type.VAR_INT, 1), false)
);
}
});
// Rewrite entity type / metadata // Rewrite entity type / metadata
handler(new PacketHandler() { handler(getMobSpawnRewriter(Types1_9.METADATA_LIST));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
EntityType type = getEntityType(wrapper.user(), entityId);
MetaStorage storage = new MetaStorage(wrapper.get(Types1_9.METADATA_LIST, 0));
handleMeta(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
storage
);
Optional<EntityData> optEntDat = getEntityData(type);
if (optEntDat.isPresent()) {
EntityData data = optEntDat.get();
wrapper.set(Type.VAR_INT, 1, data.getReplacementId());
if (data.hasBaseMeta())
data.getDefaultMeta().handle(storage);
}
// Rewrite Metadata
wrapper.set(
Types1_9.METADATA_LIST,
0,
storage.getMetaDataList()
);
}
});
} }
}); });
@ -162,25 +113,8 @@ public class EntityPackets1_11_1 extends EntityRewriter<Protocol1_11To1_11_1> {
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(getTrackerHandler(Entity1_11Types.EntityType.PLAYER, Type.INT));
@Override handler(getDimensionHandler(1));
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.INT, 0),
Entity1_11Types.EntityType.PLAYER
);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientWorld.setEnvironment(dimensionId);
}
});
} }
}); });
@ -189,14 +123,7 @@ public class EntityPackets1_11_1 extends EntityRewriter<Protocol1_11To1_11_1> {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.INT); // 0 - Dimension ID map(Type.INT); // 0 - Dimension ID
handler(new PacketHandler() { handler(getDimensionHandler(0));
@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);
}
});
} }
}); });
@ -213,33 +140,7 @@ public class EntityPackets1_11_1 extends EntityRewriter<Protocol1_11To1_11_1> {
map(Type.BYTE); // 6 - Pitch map(Type.BYTE); // 6 - Pitch
map(Types1_9.METADATA_LIST); // 7 - Metadata list map(Types1_9.METADATA_LIST); // 7 - Metadata list
// Track Entity handler(getTrackerAndMetaHandler(Types1_9.METADATA_LIST, Entity1_11Types.EntityType.PLAYER));
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_11Types.EntityType.PLAYER
);
}
});
// Rewrite Metadata
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(
Types1_9.METADATA_LIST,
0,
handleMeta(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
new MetaStorage(wrapper.get(Types1_9.METADATA_LIST, 0))
).getMetaDataList()
);
}
});
} }
}); });
@ -258,4 +159,14 @@ public class EntityPackets1_11_1 extends EntityRewriter<Protocol1_11To1_11_1> {
// Handle non-existing pig metadata (index 14 - boost time) // Handle non-existing pig metadata (index 14 - boost time)
registerMetaHandler().filter(Entity1_11Types.EntityType.PIG, 14).removed(); registerMetaHandler().filter(Entity1_11Types.EntityType.PIG, 14).removed();
} }
@Override
protected EntityType getTypeFromId(int typeId) {
return Entity1_11Types.getTypeFromId(typeId, false);
}
@Override
protected EntityType getObjectTypeFromId(final int typeId) {
return Entity1_11Types.getTypeFromId(typeId, true);
}
} }

View File

@ -17,6 +17,7 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
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.ItemRewriter;
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;
@ -24,59 +25,16 @@ public class ItemPackets1_11_1 extends BlockItemRewriter<Protocol1_11To1_11_1> {
@Override @Override
protected void registerPackets(Protocol1_11To1_11_1 protocol) { protected void registerPackets(Protocol1_11To1_11_1 protocol) {
// Set slot packet ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer);
protocol.registerOutgoing(State.PLAY, 0x16, 0x16, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot ID
map(Type.ITEM); // 2 - Slot Value
handler(new PacketHandler() { // Set slot packet
@Override itemRewriter.registerSetSlot(Type.ITEM, 0x16, 0x16);
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
wrapper.set(Type.ITEM, 0, handleItemToClient(stack));
}
});
}
});
// Window items packet // Window items packet
protocol.registerOutgoing(State.PLAY, 0x14, 0x14, new PacketRemapper() { itemRewriter.registerWindowItems(Type.ITEM_ARRAY, 0x14, 0x14);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.ITEM_ARRAY); // 1 - Window Values
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.ITEM_ARRAY, 0);
for (int i = 0; i < stacks.length; i++)
stacks[i] = handleItemToClient(stacks[i]);
}
});
}
});
// Entity Equipment Packet // Entity Equipment Packet
protocol.registerOutgoing(State.PLAY, 0x3C, 0x3C, new PacketRemapper() { itemRewriter.registerEntityEquipment(Type.ITEM, 0x3C, 0x3C);
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Type.VAR_INT); // 1 - Slot ID
map(Type.ITEM); // 2 - Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
wrapper.set(Type.ITEM, 0, handleItemToClient(stack));
}
});
}
});
// Plugin message Packet -> Trading // Plugin message Packet -> Trading
protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() {
@ -110,44 +68,10 @@ public class ItemPackets1_11_1 extends BlockItemRewriter<Protocol1_11To1_11_1> {
}); });
// Click window packet // Click window packet
protocol.registerIncoming(State.PLAY, 0x07, 0x07, new PacketRemapper() { itemRewriter.registerClickWindow(Type.ITEM, 0x07, 0x07);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot
map(Type.BYTE); // 2 - Button
map(Type.SHORT); // 3 - Action number
map(Type.VAR_INT); // 4 - Mode
map(Type.ITEM); // 5 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
handleItemToServer(item);
}
});
}
}
);
// Creative Inventory Action // Creative Inventory Action
protocol.registerIncoming(State.PLAY, 0x18, 0x18, new PacketRemapper() { itemRewriter.registerCreativeInvAction(Type.ITEM, 0x18, 0x18);
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(Type.ITEM); // 1 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
handleItemToServer(item);
}
});
}
}
);
// Handle item metadata // Handle item metadata
protocol.getEntityPackets().registerMetaHandler().handle(e -> { protocol.getEntityPackets().registerMetaHandler().handle(e -> {

View File

@ -8,20 +8,20 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
public class EntityTypeMapping { public class EntityTypeMapping {
private static Map<Integer, Integer> entityTypes = new HashMap<>(); private static final Map<Integer, Integer> TYPES = new HashMap<>();
static { static {
try { try {
Field field = EntityTypeRewriter.class.getDeclaredField("entityTypes"); Field field = EntityTypeRewriter.class.getDeclaredField("entityTypes");
field.setAccessible(true); field.setAccessible(true);
Map<Integer, Integer> entityTypes = (Map<Integer, Integer>) field.get(null); Map<Integer, Integer> entityTypes = (Map<Integer, Integer>) field.get(null);
entityTypes.forEach((type1_12, type1_13) -> EntityTypeMapping.entityTypes.put(type1_13, type1_12)); entityTypes.forEach((type1_12, type1_13) -> EntityTypeMapping.TYPES.put(type1_13, type1_12));
} catch (NoSuchFieldException | IllegalAccessException ex) { } catch (NoSuchFieldException | IllegalAccessException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
public static Optional<Integer> getOldId(int type1_13) { public static Optional<Integer> getOldId(int type1_13) {
return Optional.ofNullable(entityTypes.get(type1_13)); return Optional.ofNullable(TYPES.get(type1_13));
} }
} }

View File

@ -27,6 +27,7 @@ import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.item.Item;
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.ItemRewriter;
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.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
@ -205,21 +206,16 @@ public class BlockItemPackets1_13 extends BlockItemRewriter<Protocol1_12_2To1_13
} }
}); });
ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer);
// Windows Items // Windows Items
protocol.out(State.PLAY, 0x15, 0x14, new PacketRemapper() { protocol.out(State.PLAY, 0x15, 0x14, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.UNSIGNED_BYTE); map(Type.UNSIGNED_BYTE);
map(Type.FLAT_ITEM_ARRAY, Type.ITEM_ARRAY); map(Type.FLAT_ITEM_ARRAY, Type.ITEM_ARRAY);
handler(new PacketHandler() {
@Override handler(itemRewriter.itemArrayHandler(Type.ITEM_ARRAY));
public void handle(PacketWrapper wrapper) throws Exception {
Item[] items = wrapper.get(Type.ITEM_ARRAY, 0);
for (int i = 0; i < items.length; i++)
items[i] = handleItemToClient(items[i]);
wrapper.set(Type.ITEM_ARRAY, 0, items);
}
});
} }
}); });
@ -230,14 +226,8 @@ public class BlockItemPackets1_13 extends BlockItemRewriter<Protocol1_12_2To1_13
map(Type.BYTE); map(Type.BYTE);
map(Type.SHORT); map(Type.SHORT);
map(Type.FLAT_ITEM, Type.ITEM); map(Type.FLAT_ITEM, Type.ITEM);
handler(new PacketHandler() {
@Override handler(itemRewriter.itemToClientHandler(Type.ITEM));
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
item = handleItemToClient(item);
wrapper.set(Type.ITEM, 0, item);
}
});
} }
}); });
@ -425,14 +415,8 @@ public class BlockItemPackets1_13 extends BlockItemRewriter<Protocol1_12_2To1_13
map(Type.VAR_INT); map(Type.VAR_INT);
map(Type.VAR_INT); map(Type.VAR_INT);
map(Type.FLAT_ITEM, Type.ITEM); map(Type.FLAT_ITEM, Type.ITEM);
handler(new PacketHandler() {
@Override handler(itemRewriter.itemToClientHandler(Type.ITEM));
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
item = handleItemToClient(item);
wrapper.set(Type.ITEM, 0, item);
}
});
} }
}); });
@ -443,14 +427,8 @@ public class BlockItemPackets1_13 extends BlockItemRewriter<Protocol1_12_2To1_13
public void registerMap() { public void registerMap() {
map(Type.SHORT); map(Type.SHORT);
map(Type.ITEM, Type.FLAT_ITEM); map(Type.ITEM, Type.FLAT_ITEM);
handler(new PacketHandler() {
@Override handler(itemRewriter.itemToServerHandler(Type.FLAT_ITEM));
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.FLAT_ITEM, 0);
item = handleItemToServer(item);
wrapper.set(Type.FLAT_ITEM, 0, item);
}
});
} }
}); });
@ -464,14 +442,8 @@ public class BlockItemPackets1_13 extends BlockItemRewriter<Protocol1_12_2To1_13
map(Type.SHORT); map(Type.SHORT);
map(Type.VAR_INT); map(Type.VAR_INT);
map(Type.ITEM, Type.FLAT_ITEM); map(Type.ITEM, Type.FLAT_ITEM);
handler(new PacketHandler() {
@Override handler(itemRewriter.itemToServerHandler(Type.FLAT_ITEM));
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.FLAT_ITEM, 0);
item = handleItemToServer(item);
wrapper.set(Type.FLAT_ITEM, 0, item);
}
});
} }
}); });
} }
@ -712,11 +684,7 @@ public class BlockItemPackets1_13 extends BlockItemRewriter<Protocol1_12_2To1_13
if (((CompoundTag) tag.get("display")).get("Name") instanceof StringTag) { if (((CompoundTag) tag.get("display")).get("Name") instanceof StringTag) {
StringTag name = display.get("Name"); StringTag name = display.get("Name");
StringTag via = display.get(NBT_TAG_NAME + "|Name"); StringTag via = display.get(NBT_TAG_NAME + "|Name");
name.setValue( name.setValue(via != null ? via.getValue() : ChatRewriter.jsonTextToLegacy(name.getValue()));
via != null ? via.getValue() : ChatRewriter.jsonTextToLegacy(
name.getValue()
)
);
display.remove(NBT_TAG_NAME + "|Name"); display.remove(NBT_TAG_NAME + "|Name");
} }
} }

View File

@ -1,8 +1,6 @@
package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets; package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets;
import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.ViaBackwards;
import nl.matsv.viabackwards.api.entities.storage.EntityData;
import nl.matsv.viabackwards.api.entities.storage.MetaStorage;
import nl.matsv.viabackwards.api.exceptions.RemovedValueException; import nl.matsv.viabackwards.api.exceptions.RemovedValueException;
import nl.matsv.viabackwards.api.rewriters.EntityRewriter; import nl.matsv.viabackwards.api.rewriters.EntityRewriter;
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13;
@ -10,7 +8,6 @@ import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.EntityTypeMappin
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.PaintingMapping; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.PaintingMapping;
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.ParticleMapping; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.ParticleMapping;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.entities.Entity1_12Types;
import us.myles.ViaVersion.api.entities.Entity1_13Types; import us.myles.ViaVersion.api.entities.Entity1_13Types;
import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.entities.EntityType;
import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.item.Item;
@ -24,7 +21,6 @@ import us.myles.ViaVersion.api.type.types.version.Types1_13;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.Particle; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.Particle;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import java.util.Optional; import java.util.Optional;
@ -46,22 +42,9 @@ public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
map(Type.BYTE); map(Type.BYTE);
map(Type.BYTE); map(Type.BYTE);
map(Type.INT); map(Type.INT);
handler(new PacketHandler() {
@Override handler(getObjectTrackerHandler());
public void handle(PacketWrapper wrapper) throws Exception {
byte type = wrapper.get(Type.BYTE, 0);
EntityType entityType = Entity1_13Types.getTypeFromId(type, true);
if (entityType == null) {
ViaBackwards.getPlatform().getLogger().warning("Could not find 1.13 entity type " + type);
return;
}
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
entityType
);
}
});
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
@ -125,11 +108,8 @@ public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
int type = wrapper.get(Type.VAR_INT, 1); int type = wrapper.get(Type.VAR_INT, 1);
EntityType entityType = Entity1_13Types.getTypeFromId(type, false); EntityType entityType = Entity1_13Types.getTypeFromId(type, false);
addTrackedEntity( addTrackedEntity(wrapper.user(), wrapper.get(Type.VAR_INT, 0), entityType);
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
entityType
);
Optional<Integer> oldId = EntityTypeMapping.getOldId(type); Optional<Integer> oldId = EntityTypeMapping.getOldId(type);
if (!oldId.isPresent()) { if (!oldId.isPresent()) {
if (!hasData(entityType)) if (!hasData(entityType))
@ -140,38 +120,8 @@ public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
} }
}); });
// Handle entity type & metadata // Rewrite entity type / metadata
handler(new PacketHandler() { handler(getMobSpawnRewriter(Types1_12.METADATA_LIST));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
EntityType type = getEntityType(wrapper.user(), entityId);
MetaStorage storage = new MetaStorage(wrapper.get(Types1_12.METADATA_LIST, 0));
handleMeta(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
storage
);
Optional<EntityData> optEntDat = getEntityData(type);
if (optEntDat.isPresent()) {
EntityData data = optEntDat.get();
Optional<Integer> replacementId = EntityTypeMapping.getOldId(data.getReplacementId());
wrapper.set(Type.VAR_INT, 1, replacementId.orElse(Entity1_12Types.EntityType.ZOMBIE.getId()));
if (data.hasBaseMeta())
data.getDefaultMeta().handle(storage);
}
// Rewrite Metadata
wrapper.set(
Types1_12.METADATA_LIST,
0,
storage.getMetaDataList()
);
}
});
} }
}); });
@ -188,52 +138,18 @@ public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
map(Type.BYTE); map(Type.BYTE);
map(Types1_13.METADATA_LIST, Types1_12.METADATA_LIST); map(Types1_13.METADATA_LIST, Types1_12.METADATA_LIST);
// Track Entity handler(getTrackerAndMetaHandler(Types1_12.METADATA_LIST, Entity1_13Types.EntityType.PLAYER));
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_13Types.EntityType.PLAYER
);
}
});
// Rewrite Metadata
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(
Types1_12.METADATA_LIST,
0,
handleMeta(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
new MetaStorage(wrapper.get(Types1_12.METADATA_LIST, 0))
).getMetaDataList()
);
}
});
} }
}); });
//Spawn Painting // Spawn Painting
protocol.out(State.PLAY, 0x04, 0x04, new PacketRemapper() { protocol.out(State.PLAY, 0x04, 0x04, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.VAR_INT); map(Type.VAR_INT);
map(Type.UUID); map(Type.UUID);
handler(new PacketHandler() {
@Override handler(getTrackerHandler(Entity1_13Types.EntityType.PAINTING, Type.VAR_INT));
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_13Types.EntityType.PAINTING
);
}
});
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
@ -253,45 +169,18 @@ public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
map(Type.UNSIGNED_BYTE); // 1 - Gamemode map(Type.UNSIGNED_BYTE); // 1 - Gamemode
map(Type.INT); // 2 - Dimension map(Type.INT); // 2 - Dimension
// Track Entity handler(getTrackerHandler(Entity1_13Types.EntityType.PLAYER, Type.INT));
handler(new PacketHandler() { handler(getDimensionHandler(1));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.INT, 0),
Entity1_12Types.EntityType.PLAYER
);
}
});
// Save dimension
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
}
});
} }
}); });
// Respawn Packet (save dimension id) // Respawn Packet (save dimension id)
protocol.registerOutgoing(State.PLAY, 0x38, 0x35, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x38, 0x35, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.INT); // 0 - Dimension ID map(Type.INT); // 0 - Dimension ID
handler(new PacketHandler() { handler(getDimensionHandler(0));
@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);
}
});
} }
}); });
@ -427,4 +316,19 @@ public class EntityPackets1_13 extends EntityRewriter<Protocol1_12_2To1_13> {
// TODO REWRITE BLOCKS IN MINECART // TODO REWRITE BLOCKS IN MINECART
} }
@Override
protected EntityType getTypeFromId(int typeId) {
return Entity1_13Types.getTypeFromId(typeId, false);
}
@Override
protected EntityType getObjectTypeFromId(final int typeId) {
return Entity1_13Types.getTypeFromId(typeId, true);
}
@Override
protected int getOldEntityId(final int newId) {
return EntityTypeMapping.getOldId(newId).orElse(newId);
}
} }

View File

@ -20,17 +20,7 @@ import us.myles.ViaVersion.packets.State;
public class Protocol1_12To1_12_1 extends BackwardsProtocol { public class Protocol1_12To1_12_1 extends BackwardsProtocol {
@Override @Override
protected void registerPackets() { protected void registerPackets() {
registerOutgoing(State.PLAY, 0x2b, -1, new PacketRemapper() { cancelOutgoing(State.PLAY, 0x2B); //TODO
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.cancel();
}
});
}
}); // TODO
registerOutgoing(State.PLAY, 0x2c, 0x2b); registerOutgoing(State.PLAY, 0x2c, 0x2b);
registerOutgoing(State.PLAY, 0x2d, 0x2c); registerOutgoing(State.PLAY, 0x2d, 0x2c);
registerOutgoing(State.PLAY, 0x2e, 0x2d); registerOutgoing(State.PLAY, 0x2e, 0x2d);
@ -68,17 +58,7 @@ public class Protocol1_12To1_12_1 extends BackwardsProtocol {
registerOutgoing(State.PLAY, 0x4e, 0x4d); registerOutgoing(State.PLAY, 0x4e, 0x4d);
registerOutgoing(State.PLAY, 0x4f, 0x4e); registerOutgoing(State.PLAY, 0x4f, 0x4e);
registerIncoming(State.PLAY, -1, 0x1, new PacketRemapper() { cancelIncoming(State.PLAY, 0x01); //TODO
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.cancel();
}
});
}
}); // TODO
registerIncoming(State.PLAY, 0x1, 0x2); registerIncoming(State.PLAY, 0x1, 0x2);
registerIncoming(State.PLAY, 0x2, 0x3); registerIncoming(State.PLAY, 0x2, 0x3);
registerIncoming(State.PLAY, 0x3, 0x4); registerIncoming(State.PLAY, 0x3, 0x4);

View File

@ -93,30 +93,11 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol {
registerOutgoing(State.PLAY, 0x55, 0x4F); // c registerOutgoing(State.PLAY, 0x55, 0x4F); // c
registerOutgoing(State.PLAY, 0x56, 0x50); // c registerOutgoing(State.PLAY, 0x56, 0x50); // c
//Update View Position // Update View Position
registerOutgoing(State.PLAY, 0x40, -1, new PacketRemapper() { cancelOutgoing(State.PLAY, 0x40);
@Override
public void registerMap() { // Update View Distance
handler(new PacketHandler() { cancelOutgoing(State.PLAY, 0x41);
@Override
public void handle(PacketWrapper packetWrapper) throws Exception {
packetWrapper.cancel();
}
});
}
});
//Update View Distance
registerOutgoing(State.PLAY, 0x41, -1, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper packetWrapper) throws Exception {
packetWrapper.cancel();
}
});
}
});
registerOutgoing(State.PLAY, 0x57, 0x51, new PacketRemapper() { // c registerOutgoing(State.PLAY, 0x57, 0x51, new PacketRemapper() { // c
@Override @Override

View File

@ -20,6 +20,7 @@ import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_13_2; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_13_2;
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.ItemRewriter;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_13; import us.myles.ViaVersion.api.type.types.version.Types1_13;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
@ -139,44 +140,13 @@ public class BlockItemPackets1_14 extends BlockItemRewriter<Protocol1_13_2To1_14
} }
}); });
// Window items packet ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer);
protocol.registerOutgoing(State.PLAY, 0x14, 0x15, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.FLAT_VAR_INT_ITEM_ARRAY); // 1 - Window Values
handler(new PacketHandler() { // Window items packet
@Override itemRewriter.registerWindowItems(Type.FLAT_VAR_INT_ITEM_ARRAY, 0x14, 0x15);
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.FLAT_VAR_INT_ITEM_ARRAY, 0);
for (int i = 0; i < stacks.length; i++) {
stacks[i] = handleItemToClient(stacks[i]);
}
wrapper.set(Type.FLAT_VAR_INT_ITEM_ARRAY, 0, stacks);
}
});
}
});
// Set slot packet // Set slot packet
protocol.registerOutgoing(State.PLAY, 0x16, 0x17, new PacketRemapper() { itemRewriter.registerSetSlot(Type.FLAT_VAR_INT_ITEM, 0x16, 0x17);
@Override
public void registerMap() {
map(Type.BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot ID
map(Type.FLAT_VAR_INT_ITEM); // 2 - Slot Value
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.FLAT_VAR_INT_ITEM, 0);
item = handleItemToClient(item);
wrapper.set(Type.FLAT_VAR_INT_ITEM, 0, item);
}
});
}
});
// Trade list // Trade list
protocol.out(State.PLAY, 0x27, 0x19, new PacketRemapper() { protocol.out(State.PLAY, 0x27, 0x19, new PacketRemapper() {
@ -249,13 +219,7 @@ public class BlockItemPackets1_14 extends BlockItemRewriter<Protocol1_13_2To1_14
map(Type.VAR_INT); // 1 - Slot ID map(Type.VAR_INT); // 1 - Slot ID
map(Type.FLAT_VAR_INT_ITEM); // 2 - Item map(Type.FLAT_VAR_INT_ITEM); // 2 - Item
handler(new PacketHandler() { handler(itemRewriter.itemToClientHandler(Type.FLAT_VAR_INT_ITEM));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = handleItemToClient(wrapper.get(Type.FLAT_VAR_INT_ITEM, 0));
wrapper.set(Type.FLAT_VAR_INT_ITEM, 0, item);
}
});
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
@ -374,43 +338,10 @@ public class BlockItemPackets1_14 extends BlockItemRewriter<Protocol1_13_2To1_14
*/ */
// Click window packet // Click window packet
protocol.registerIncoming(State.PLAY, 0x09, 0x08, new PacketRemapper() { itemRewriter.registerClickWindow(Type.FLAT_VAR_INT_ITEM, 0x09, 0x08);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot
map(Type.BYTE); // 2 - Button
map(Type.SHORT); // 3 - Action number
map(Type.VAR_INT); // 4 - Mode
map(Type.FLAT_VAR_INT_ITEM); // 5 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = handleItemToServer(wrapper.get(Type.FLAT_VAR_INT_ITEM, 0));
wrapper.set(Type.FLAT_VAR_INT_ITEM, 0, item);
}
});
}
});
// Creative Inventory Action // Creative Inventory Action
protocol.registerIncoming(State.PLAY, 0x26, 0x24, new PacketRemapper() { itemRewriter.registerCreativeInvAction(Type.FLAT_VAR_INT_ITEM, 0x26, 0x24);
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(Type.FLAT_VAR_INT_ITEM); // 1 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.FLAT_VAR_INT_ITEM, 0);
item = handleItemToServer(item);
wrapper.set(Type.FLAT_VAR_INT_ITEM, 0, item);
}
});
}
});
// Block break animation // Block break animation
protocol.registerOutgoing(State.PLAY, 0x08, 0x08, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x08, 0x08, new PacketRemapper() {
@ -643,24 +574,6 @@ public class BlockItemPackets1_14 extends BlockItemRewriter<Protocol1_13_2To1_14
} }
}); });
//respawn
protocol.registerOutgoing(State.PLAY, 0x3A, 0x38, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Dimension ID
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);
wrapper.write(Type.UNSIGNED_BYTE, (short) 0); // todo - do we need to store it from difficulty packet?
wrapper.user().get(ChunkLightStorage.class).clear();
}
});
}
});
// Spawn position // Spawn position
protocol.registerOutgoing(State.PLAY, 0x4D, 0x49, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x4D, 0x49, new PacketRemapper() {
@Override @Override

View File

@ -3,12 +3,12 @@ package nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.packets;
import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.ViaBackwards;
import nl.matsv.viabackwards.api.entities.meta.MetaHandler; import nl.matsv.viabackwards.api.entities.meta.MetaHandler;
import nl.matsv.viabackwards.api.entities.storage.EntityData; import nl.matsv.viabackwards.api.entities.storage.EntityData;
import nl.matsv.viabackwards.api.entities.storage.MetaStorage;
import nl.matsv.viabackwards.api.exceptions.RemovedValueException; import nl.matsv.viabackwards.api.exceptions.RemovedValueException;
import nl.matsv.viabackwards.api.rewriters.EntityRewriter; import nl.matsv.viabackwards.api.rewriters.EntityRewriter;
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets.BlockItemPackets1_13; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets.BlockItemPackets1_13;
import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14;
import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.EntityTypeMapping; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.EntityTypeMapping;
import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.entities.Entity1_13Types; import us.myles.ViaVersion.api.entities.Entity1_13Types;
import us.myles.ViaVersion.api.entities.Entity1_14Types; import us.myles.ViaVersion.api.entities.Entity1_14Types;
@ -22,7 +22,6 @@ import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_13_2;
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.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_13;
import us.myles.ViaVersion.api.type.types.version.Types1_13_2; import us.myles.ViaVersion.api.type.types.version.Types1_13_2;
import us.myles.ViaVersion.api.type.types.version.Types1_14; import us.myles.ViaVersion.api.type.types.version.Types1_14;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
@ -32,6 +31,7 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import java.util.Optional; import java.util.Optional;
public class EntityPackets1_14 extends EntityRewriter<Protocol1_13_2To1_14> { public class EntityPackets1_14 extends EntityRewriter<Protocol1_13_2To1_14> {
@Override @Override
protected void registerPackets(Protocol1_13_2To1_14 protocol) { protected void registerPackets(Protocol1_13_2To1_14 protocol) {
// Spawn Object // Spawn Object
@ -48,22 +48,8 @@ public class EntityPackets1_14 extends EntityRewriter<Protocol1_13_2To1_14> {
map(Type.BYTE); // 7 - Yaw map(Type.BYTE); // 7 - Yaw
map(Type.INT); // 8 - Data map(Type.INT); // 8 - Data
handler(new PacketHandler() { handler(getObjectTrackerHandler());
@Override
public void handle(PacketWrapper wrapper) throws Exception {
byte type = wrapper.get(Type.BYTE, 0);
Entity1_14Types.EntityType entityType = Entity1_14Types.getTypeFromId(type);
if (entityType == null) {
ViaBackwards.getPlatform().getLogger().warning("Could not find 1.14 entity type " + type);
return;
}
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
entityType
);
}
});
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
@ -171,37 +157,7 @@ public class EntityPackets1_14 extends EntityRewriter<Protocol1_13_2To1_14> {
}); });
// Handle entity type & metadata // Handle entity type & metadata
handler(new PacketHandler() { handler(getMobSpawnRewriter(Types1_13_2.METADATA_LIST));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
EntityType type = getEntityType(wrapper.user(), entityId);
MetaStorage storage = new MetaStorage(wrapper.get(Types1_13_2.METADATA_LIST, 0));
handleMeta(
wrapper.user(),
entityId,
storage
);
Optional<EntityData> optEntDat = getEntityData(type);
if (optEntDat.isPresent()) {
EntityData data = optEntDat.get();
Optional<Integer> replacementId = EntityTypeMapping.getOldId(data.getReplacementId());
wrapper.set(Type.VAR_INT, 1, replacementId.orElse(Entity1_13Types.EntityType.ZOMBIE.getId()));
if (data.hasBaseMeta())
data.getDefaultMeta().handle(storage);
}
// Rewrite Metadata
wrapper.set(
Types1_13_2.METADATA_LIST,
0,
storage.getMetaDataList()
);
}
});
} }
}); });
@ -227,23 +183,7 @@ public class EntityPackets1_14 extends EntityRewriter<Protocol1_13_2To1_14> {
map(Type.BYTE); // 6 - Pitch map(Type.BYTE); // 6 - Pitch
map(Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST); // 7 - Metadata map(Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST); // 7 - Metadata
handler(new PacketHandler() { handler(getTrackerAndMetaHandler(Types1_13_2.METADATA_LIST, Entity1_14Types.EntityType.PLAYER));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
Entity1_14Types.EntityType entType = Entity1_14Types.EntityType.PLAYER;
// Register Type ID
addTrackedEntity(wrapper.user(), entityId, entType);
wrapper.set(Types1_13_2.METADATA_LIST, 0,
handleMeta(
wrapper.user(),
entityId,
new MetaStorage(wrapper.get(Types1_13_2.METADATA_LIST, 0))
).getMetaDataList()
);
}
});
} }
}); });
@ -251,9 +191,9 @@ public class EntityPackets1_14 extends EntityRewriter<Protocol1_13_2To1_14> {
registerEntityDestroy(0x37, 0x35); registerEntityDestroy(0x37, 0x35);
// Entity Metadata packet // Entity Metadata packet
registerMetadataRewriter(0x43, 0x3F, Types1_14.METADATA_LIST, Types1_13.METADATA_LIST); registerMetadataRewriter(0x43, 0x3F, Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST);
//join game // Join game
protocol.registerOutgoing(State.PLAY, 0x25, 0x25, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x25, 0x25, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
@ -261,24 +201,36 @@ public class EntityPackets1_14 extends EntityRewriter<Protocol1_13_2To1_14> {
map(Type.UNSIGNED_BYTE); // 1 - Gamemode map(Type.UNSIGNED_BYTE); // 1 - Gamemode
map(Type.INT); // 2 - Dimension map(Type.INT); // 2 - Dimension
handler(getTrackerHandler(Entity1_14Types.EntityType.PLAYER, Type.INT));
handler(getDimensionHandler(1));
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
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
int entityId = wrapper.get(Type.INT, 0);
// Register Type ID
addTrackedEntity(wrapper.user(), entityId, Entity1_14Types.EntityType.PLAYER);
wrapper.write(Type.UNSIGNED_BYTE, (short) 0); wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
wrapper.passthrough(Type.UNSIGNED_BYTE); // Max Players wrapper.passthrough(Type.UNSIGNED_BYTE); // Max Players
wrapper.passthrough(Type.STRING); // Level Type wrapper.passthrough(Type.STRING); // Level Type
wrapper.read(Type.VAR_INT); //Read View Distance wrapper.read(Type.VAR_INT); // Read View Distance
}
});
}
});
// Respawn
protocol.registerOutgoing(State.PLAY, 0x3A, 0x38, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Dimension ID
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);
wrapper.write(Type.UNSIGNED_BYTE, (short) 0); // todo - do we need to store it from difficulty packet?
wrapper.user().get(ChunkLightStorage.class).clear();
} }
}); });
} }
@ -521,4 +473,14 @@ public class EntityPackets1_14 extends EntityRewriter<Protocol1_13_2To1_14> {
} }
return id; return id;
} }
@Override
protected EntityType getTypeFromId(int typeId) {
return Entity1_14Types.getTypeFromId(typeId);
}
@Override
protected int getOldEntityId(final int newId) {
return EntityTypeMapping.getOldId(newId).orElse(newId);
}
} }

View File

@ -5,8 +5,8 @@ import nl.matsv.viabackwards.api.entities.storage.MetaStorage;
import nl.matsv.viabackwards.api.rewriters.EntityRewriter; import nl.matsv.viabackwards.api.rewriters.EntityRewriter;
import nl.matsv.viabackwards.protocol.protocol1_13to1_13_1.Protocol1_13To1_13_1; import nl.matsv.viabackwards.protocol.protocol1_13to1_13_1.Protocol1_13To1_13_1;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.entities.Entity1_12Types;
import us.myles.ViaVersion.api.entities.Entity1_13Types; import us.myles.ViaVersion.api.entities.Entity1_13Types;
import us.myles.ViaVersion.api.entities.EntityType;
import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_13; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_13;
@ -15,7 +15,6 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_13; import us.myles.ViaVersion.api.type.types.version.Types1_13;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
public class EntityPackets1_13_1 extends EntityRewriter<Protocol1_13To1_13_1> { public class EntityPackets1_13_1 extends EntityRewriter<Protocol1_13To1_13_1> {
@ -54,11 +53,7 @@ public class EntityPackets1_13_1 extends EntityRewriter<Protocol1_13To1_13_1> {
} }
// Track Entity // Track Entity
addTrackedEntity( addTrackedEntity(wrapper.user(), entityId, entType);
wrapper.user(),
entityId,
entType
);
} }
}); });
} }
@ -89,39 +84,19 @@ public class EntityPackets1_13_1 extends EntityRewriter<Protocol1_13To1_13_1> {
map(Types1_13.METADATA_LIST); // 12 - Metadata map(Types1_13.METADATA_LIST); // 12 - Metadata
// Track Entity // Track Entity
handler(new PacketHandler() { handler(getTrackerHandler());
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int type = wrapper.get(Type.VAR_INT, 1);
Entity1_13Types.EntityType entityType = Entity1_13Types.getTypeFromId(type, false);
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
entityType
);
}
});
// Rewrite Metadata // Rewrite Metadata
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
MetaStorage storage = new MetaStorage(wrapper.get(Types1_13.METADATA_LIST, 0)); MetaStorage storage = new MetaStorage(wrapper.get(Types1_13.METADATA_LIST, 0));
handleMeta( handleMeta(wrapper.user(), wrapper.get(Type.VAR_INT, 0), storage);
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
storage
);
// Don't handle new ids / base meta since it's not used for this version // Don't handle new ids / base meta since it's not used for this version
// Rewrite Metadata // Rewrite Metadata
wrapper.set( wrapper.set(Types1_13.METADATA_LIST, 0, storage.getMetaDataList());
Types1_13.METADATA_LIST,
0,
storage.getMetaDataList()
);
} }
}); });
} }
@ -140,37 +115,11 @@ public class EntityPackets1_13_1 extends EntityRewriter<Protocol1_13To1_13_1> {
map(Type.BYTE); // 6 - Pitch map(Type.BYTE); // 6 - Pitch
map(Types1_13.METADATA_LIST); // 7 - Metadata map(Types1_13.METADATA_LIST); // 7 - Metadata
// Track Entity handler(getTrackerAndMetaHandler(Types1_13.METADATA_LIST, Entity1_13Types.EntityType.PLAYER));
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_13Types.EntityType.PLAYER
);
}
});
// Rewrite Metadata
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(
Types1_13.METADATA_LIST,
0,
handleMeta(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
new MetaStorage(wrapper.get(Types1_13.METADATA_LIST, 0))
).getMetaDataList()
);
}
});
} }
}); });
//Spawn Painting // Spawn Painting
registerExtraTracker(0x04, Entity1_13Types.EntityType.PAINTING); registerExtraTracker(0x04, Entity1_13Types.EntityType.PAINTING);
// Join Game // Join Game
@ -181,27 +130,8 @@ public class EntityPackets1_13_1 extends EntityRewriter<Protocol1_13To1_13_1> {
map(Type.UNSIGNED_BYTE); // 1 - Gamemode map(Type.UNSIGNED_BYTE); // 1 - Gamemode
map(Type.INT); // 2 - Dimension map(Type.INT); // 2 - Dimension
// Track Entity handler(getTrackerHandler(Entity1_13Types.EntityType.PLAYER, Type.INT));
handler(new PacketHandler() { handler(getDimensionHandler(1));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.INT, 0),
Entity1_12Types.EntityType.PLAYER
);
}
});
// Save dimension
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
}
});
} }
}); });
@ -212,14 +142,7 @@ public class EntityPackets1_13_1 extends EntityRewriter<Protocol1_13To1_13_1> {
map(Type.INT); // 0 - Dimension ID\ map(Type.INT); // 0 - Dimension ID\
// Save dimension // Save dimension
handler(new PacketHandler() { handler(getDimensionHandler(0));
@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);
}
});
} }
}); });
@ -232,7 +155,6 @@ public class EntityPackets1_13_1 extends EntityRewriter<Protocol1_13To1_13_1> {
@Override @Override
protected void registerRewrites() { protected void registerRewrites() {
// Rewrite items & blocks // Rewrite items & blocks
registerMetaHandler().handle(e -> { registerMetaHandler().handle(e -> {
Metadata meta = e.getData(); Metadata meta = e.getData();
@ -273,4 +195,14 @@ public class EntityPackets1_13_1 extends EntityRewriter<Protocol1_13To1_13_1> {
return meta; return meta;
}); });
} }
@Override
protected EntityType getTypeFromId(int typeId) {
return Entity1_13Types.getTypeFromId(typeId, false);
}
@Override
protected EntityType getObjectTypeFromId(final int typeId) {
return Entity1_13Types.getTypeFromId(typeId, true);
}
} }

View File

@ -5,54 +5,22 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
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.ItemRewriter;
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;
public class InventoryPackets1_13_1 { public class InventoryPackets1_13_1 {
public static void register(Protocol protocol) { public static void register(Protocol protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets1_13_1::toClient, InventoryPackets1_13_1::toServer);
/*
Outgoing packets
*/
// Window items packet // Window items packet
protocol.registerOutgoing(State.PLAY, 0x15, 0x15, new PacketRemapper() { itemRewriter.registerWindowItems(Type.FLAT_ITEM_ARRAY, 0x15, 0x15);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.FLAT_ITEM_ARRAY); // 1 - Window Values
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.FLAT_ITEM_ARRAY, 0);
for (Item stack : stacks)
toClient(stack);
}
});
}
});
// Set slot packet // Set slot packet
protocol.registerOutgoing(State.PLAY, 0x17, 0x17, new PacketRemapper() { itemRewriter.registerSetSlot(Type.FLAT_ITEM, 0x17, 0x17);
@Override
public void registerMap() {
map(Type.BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot ID
map(Type.FLAT_ITEM); // 2 - Slot Value
handler(new PacketHandler() { // Plugin Message
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.FLAT_ITEM, 0);
toClient(stack);
}
});
}
});
//Plugin Message
protocol.registerOutgoing(State.PLAY, 0x19, 0x19, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x19, 0x19, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
@ -90,67 +58,14 @@ public class InventoryPackets1_13_1 {
}); });
// Entity Equipment Packet // Entity Equipment Packet
protocol.registerOutgoing(State.PLAY, 0x42, 0x42, new PacketRemapper() { itemRewriter.registerEntityEquipment(Type.FLAT_ITEM, 0x42, 0x42);
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Type.VAR_INT); // 1 - Slot ID
map(Type.FLAT_ITEM); // 2 - Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.FLAT_ITEM, 0);
toClient(stack);
}
});
}
});
/*
Incoming packets
*/
// Click window packet // Click window packet
protocol.registerIncoming(State.PLAY, 0x08, 0x08, new PacketRemapper() { itemRewriter.registerClickWindow(Type.FLAT_ITEM, 0x08, 0x08);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot
map(Type.BYTE); // 2 - Button
map(Type.SHORT); // 3 - Action number
map(Type.VAR_INT); // 4 - Mode
map(Type.FLAT_ITEM); // 5 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.FLAT_ITEM, 0);
toServer(item);
}
});
}
}
);
// Creative Inventory Action // Creative Inventory Action
protocol.registerIncoming(State.PLAY, 0x24, 0x24, new PacketRemapper() { itemRewriter.registerCreativeInvAction(Type.FLAT_ITEM, 0x24, 0x24);
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(Type.FLAT_ITEM); // 1 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.FLAT_ITEM, 0);
toServer(item);
}
});
}
}
);
} }
public static void toClient(Item item) { public static void toClient(Item item) {
@ -158,7 +73,7 @@ public class InventoryPackets1_13_1 {
item.setIdentifier(getOldItemId(item.getIdentifier())); item.setIdentifier(getOldItemId(item.getIdentifier()));
} }
//1.13.1 Item Id // 1.13.1 Item Id
public static int getNewItemId(int itemId) { public static int getNewItemId(int itemId) {
if (itemId >= 443) { if (itemId >= 443) {
return itemId + 5; return itemId + 5;
@ -171,7 +86,7 @@ public class InventoryPackets1_13_1 {
item.setIdentifier(getNewItemId(item.getIdentifier())); item.setIdentifier(getNewItemId(item.getIdentifier()));
} }
//1.13 Item Id // 1.13 Item Id
public static int getOldItemId(int newId) { public static int getOldItemId(int newId) {
if (newId >= 448) { if (newId >= 448) {
return newId - 5; return newId - 5;

View File

@ -13,17 +13,7 @@ public class Protocol1_14_3To1_14_4 extends BackwardsProtocol {
@Override @Override
protected void registerPackets() { protected void registerPackets() {
// Acknowledge Player Digging - added in pre4 // Acknowledge Player Digging - added in pre4
registerOutgoing(State.PLAY, 0x5c, -1, new PacketRemapper() { cancelOutgoing(State.PLAY, 0x5C);
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.cancel();
}
});
}
});
// Trade list // Trade list
registerOutgoing(State.PLAY, 0x27, 0x27, new PacketRemapper() { registerOutgoing(State.PLAY, 0x27, 0x27, new PacketRemapper() {

View File

@ -2,14 +2,14 @@ package nl.matsv.viabackwards.protocol.protocol1_14to1_14_1;
import nl.matsv.viabackwards.api.BackwardsProtocol; import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.protocol.protocol1_14to1_14_1.packets.EntityPackets; import nl.matsv.viabackwards.protocol.protocol1_14to1_14_1.packets.EntityPackets1_14_1;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
public class Protocol1_14To1_14_1 extends BackwardsProtocol { public class Protocol1_14To1_14_1 extends BackwardsProtocol {
@Override @Override
protected void registerPackets() { protected void registerPackets() {
new EntityPackets().register(this); new EntityPackets1_14_1().register(this);
} }
@Override @Override

View File

@ -5,13 +5,14 @@ import nl.matsv.viabackwards.api.rewriters.EntityRewriter;
import nl.matsv.viabackwards.protocol.protocol1_14to1_14_1.Protocol1_14To1_14_1; import nl.matsv.viabackwards.protocol.protocol1_14to1_14_1.Protocol1_14To1_14_1;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.entities.Entity1_14Types; import us.myles.ViaVersion.api.entities.Entity1_14Types;
import us.myles.ViaVersion.api.entities.EntityType;
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.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_14; import us.myles.ViaVersion.api.type.types.version.Types1_14;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
public class EntityPackets extends EntityRewriter<Protocol1_14To1_14_1> { public class EntityPackets1_14_1 extends EntityRewriter<Protocol1_14To1_14_1> {
@Override @Override
protected void registerPackets(Protocol1_14To1_14_1 protocol) { protected void registerPackets(Protocol1_14To1_14_1 protocol) {
@ -30,13 +31,7 @@ public class EntityPackets extends EntityRewriter<Protocol1_14To1_14_1> {
map(Type.UUID); // 1 - UUID map(Type.UUID); // 1 - UUID
map(Type.VAR_INT); // 2 - Type map(Type.VAR_INT); // 2 - Type
handler(new PacketHandler() { handler(getTrackerHandler());
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int type = wrapper.get(Type.VAR_INT, 1);
addTrackedEntity(wrapper.user(), wrapper.get(Type.VAR_INT, 0), Entity1_14Types.getTypeFromId(type));
}
});
} }
}); });
@ -75,21 +70,7 @@ public class EntityPackets extends EntityRewriter<Protocol1_14To1_14_1> {
}); });
// Entity Metadata // Entity Metadata
protocol.registerOutgoing(State.PLAY, 0x43, 0x43, new PacketRemapper() { registerMetadataRewriter(0x43, 0x43, Types1_14.METADATA_LIST);
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Types1_14.METADATA_LIST); // 1 - Metadata list
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
wrapper.set(Types1_14.METADATA_LIST, 0,
handleMeta(wrapper.user(), entityId, new MetaStorage(wrapper.get(Types1_14.METADATA_LIST, 0))).getMetaDataList());
}
});
}
});
} }
@Override @Override
@ -98,4 +79,9 @@ public class EntityPackets extends EntityRewriter<Protocol1_14To1_14_1> {
registerMetaHandler().filter(Entity1_14Types.EntityType.VILLAGER, 16).handleIndexChange(15); registerMetaHandler().filter(Entity1_14Types.EntityType.VILLAGER, 16).handleIndexChange(15);
registerMetaHandler().filter(Entity1_14Types.EntityType.WANDERING_TRADER, 15).removed(); registerMetaHandler().filter(Entity1_14Types.EntityType.WANDERING_TRADER, 15).removed();
} }
@Override
protected EntityType getTypeFromId(int typeId) {
return Entity1_14Types.getTypeFromId(typeId);
}
} }

View File

@ -20,6 +20,7 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
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.ItemRewriter;
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_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type;
@ -28,61 +29,16 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
public class BlockItemPackets1_10 extends BlockItemRewriter<Protocol1_9_4To1_10> { public class BlockItemPackets1_10 extends BlockItemRewriter<Protocol1_9_4To1_10> {
protected void registerPackets(Protocol1_9_4To1_10 protocol) { protected void registerPackets(Protocol1_9_4To1_10 protocol) {
/* Item packets */ ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer);
// Set slot packet // Set slot packet
protocol.registerOutgoing(State.PLAY, 0x16, 0x16, new PacketRemapper() { itemRewriter.registerSetSlot(Type.ITEM, 0x16, 0x16);
@Override
public void registerMap() {
map(Type.BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot ID
map(Type.ITEM); // 2 - Slot Value
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
wrapper.set(Type.ITEM, 0, handleItemToClient(stack));
}
});
}
});
// Window items packet // Window items packet
protocol.registerOutgoing(State.PLAY, 0x14, 0x14, new PacketRemapper() { itemRewriter.registerWindowItems(Type.ITEM_ARRAY, 0x14, 0x14);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.ITEM_ARRAY); // 1 - Window Values
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.ITEM_ARRAY, 0);
for (int i = 0; i < stacks.length; i++)
stacks[i] = handleItemToClient(stacks[i]);
}
});
}
});
// Entity Equipment Packet // Entity Equipment Packet
protocol.registerOutgoing(State.PLAY, 0x3C, 0x3C, new PacketRemapper() { itemRewriter.registerEntityEquipment(Type.ITEM, 0x3C, 0x3C);
@Override
public void registerMap() {
map(Type.VAR_INT); // 0 - Entity ID
map(Type.VAR_INT); // 1 - Slot ID
map(Type.ITEM); // 2 - Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
wrapper.set(Type.ITEM, 0, handleItemToClient(stack));
}
});
}
});
// Plugin message Packet -> Trading // Plugin message Packet -> Trading
protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x18, 0x18, new PacketRemapper() {
@ -116,47 +72,13 @@ public class BlockItemPackets1_10 extends BlockItemRewriter<Protocol1_9_4To1_10>
}); });
// Click window packet // Click window packet
protocol.registerIncoming(State.PLAY, 0x07, 0x07, new PacketRemapper() { itemRewriter.registerClickWindow(Type.ITEM, 0x07, 0x07);
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.SHORT); // 1 - Slot
map(Type.BYTE); // 2 - Button
map(Type.SHORT); // 3 - Action number
map(Type.VAR_INT); // 4 - Mode
map(Type.ITEM); // 5 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
handleItemToServer(item);
}
});
}
}
);
// Creative Inventory Action // Creative Inventory Action
protocol.registerIncoming(State.PLAY, 0x18, 0x18, new PacketRemapper() { itemRewriter.registerCreativeInvAction(Type.ITEM, 0x18, 0x18);
@Override
public void registerMap() {
map(Type.SHORT); // 0 - Slot
map(Type.ITEM); // 1 - Clicked Item
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
handleItemToServer(item);
}
});
}
}
);
/* Block packets */ /* Block packets */
// Chunk packet // Chunk packet
protocol.registerOutgoing(State.PLAY, 0x20, 0x20, new PacketRemapper() { protocol.registerOutgoing(State.PLAY, 0x20, 0x20, new PacketRemapper() {
@Override @Override

View File

@ -30,12 +30,9 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.api.type.types.version.Types1_9;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import java.util.Optional; import java.util.Optional;
import static us.myles.ViaVersion.api.entities.Entity1_11Types.getTypeFromId;
public class EntityPackets1_10 extends EntityRewriter<Protocol1_9_4To1_10> { public class EntityPackets1_10 extends EntityRewriter<Protocol1_9_4To1_10> {
@Override @Override
@ -56,16 +53,7 @@ public class EntityPackets1_10 extends EntityRewriter<Protocol1_9_4To1_10> {
map(Type.INT); // 8 - data map(Type.INT); // 8 - data
// Track Entity // Track Entity
handler(new PacketHandler() { handler(getObjectTrackerHandler());
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
getTypeFromId(wrapper.get(Type.BYTE, 0), true)
);
}
});
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
@ -134,16 +122,7 @@ public class EntityPackets1_10 extends EntityRewriter<Protocol1_9_4To1_10> {
map(Types1_9.METADATA_LIST); // 12 - Metadata map(Types1_9.METADATA_LIST); // 12 - Metadata
// Track entity // Track entity
handler(new PacketHandler() { handler(getTrackerHandler(Type.UNSIGNED_BYTE, 0));
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_10Types.getTypeFromId(wrapper.get(Type.UNSIGNED_BYTE, 0), false)
);
}
});
// Rewrite entity type / metadata // Rewrite entity type / metadata
handler(new PacketHandler() { handler(new PacketHandler() {
@ -190,25 +169,8 @@ public class EntityPackets1_10 extends EntityRewriter<Protocol1_9_4To1_10> {
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(getTrackerHandler(Entity1_10Types.EntityType.PLAYER, Type.INT));
@Override handler(getDimensionHandler(1));
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.INT, 0),
Entity1_10Types.EntityType.PLAYER
);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientWorld.setEnvironment(dimensionId);
}
});
} }
}); });
@ -217,14 +179,7 @@ public class EntityPackets1_10 extends EntityRewriter<Protocol1_9_4To1_10> {
@Override @Override
public void registerMap() { public void registerMap() {
map(Type.INT); // 0 - Dimension ID map(Type.INT); // 0 - Dimension ID
handler(new PacketHandler() { handler(getDimensionHandler(0));
@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);
}
});
} }
}); });
@ -241,33 +196,7 @@ public class EntityPackets1_10 extends EntityRewriter<Protocol1_9_4To1_10> {
map(Type.BYTE); // 6 - Pitch map(Type.BYTE); // 6 - Pitch
map(Types1_9.METADATA_LIST); // 7 - Metadata list map(Types1_9.METADATA_LIST); // 7 - Metadata list
// Track Entity handler(getTrackerAndMetaHandler(Types1_9.METADATA_LIST, Entity1_11Types.EntityType.PLAYER));
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
addTrackedEntity(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
Entity1_10Types.EntityType.PLAYER
);
}
});
// Rewrite Metadata
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(
Types1_9.METADATA_LIST,
0,
handleMeta(
wrapper.user(),
wrapper.get(Type.VAR_INT, 0),
new MetaStorage(wrapper.get(Types1_9.METADATA_LIST, 0))
).getMetaDataList()
);
}
});
} }
}); });
@ -326,4 +255,14 @@ public class EntityPackets1_10 extends EntityRewriter<Protocol1_9_4To1_10> {
return data; return data;
}); });
} }
@Override
protected EntityType getTypeFromId(int typeId) {
return Entity1_10Types.getTypeFromId(typeId, false);
}
@Override
protected EntityType getObjectTypeFromId(int typeId) {
return Entity1_10Types.getTypeFromId(typeId, true);
}
} }

View File

@ -14,8 +14,28 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
public class ItemUtil { public class ItemUtil {
/**
* Sets all the data of the item into the original item.
*
* @param original item to be modified
* @param item item to replicate the data from
* @return modified original item
*/
public static void copyItem(Item original, Item item) {
original.setIdentifier(item.getIdentifier());
original.setAmount(item.getAmount());
original.setData(item.getData());
original.setTag(item.getTag() != null ? item.getTag().clone() : null);
}
/**
* Creates a new item object with the same data as the given one.
*
* @param item item to be copied
* @return a new copy of the item
*/
public static Item copyItem(Item item) { public static Item copyItem(Item item) {
if (item == null) return null; if (item == null) return null;
return new Item(item.getId(), item.getAmount(), item.getData(), item.getTag() != null ? item.getTag().clone() : null); return new Item(item.getIdentifier(), item.getAmount(), item.getData(), item.getTag() != null ? item.getTag().clone() : null);
} }
} }

View File

@ -61,7 +61,7 @@
<dependency> <dependency>
<groupId>us.myles</groupId> <groupId>us.myles</groupId>
<artifactId>viaversion</artifactId> <artifactId>viaversion</artifactId>
<version>2.1.4-19w38b</version> <version>2.1.4-19w39a</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>