use of com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8 in project ViaVersion by ViaVersion.
the class InventoryPackets method register.
public static void register(Protocol protocol) {
protocol.registerClientbound(ClientboundPackets1_8.WINDOW_PROPERTY, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// 1 - Property Key
map(Type.SHORT);
// 2 - Property Value
map(Type.SHORT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
final short windowId = wrapper.get(Type.UNSIGNED_BYTE, 0);
final short property = wrapper.get(Type.SHORT, 0);
short value = wrapper.get(Type.SHORT, 1);
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
if (inventoryTracker.getInventory() != null) {
if (inventoryTracker.getInventory().equalsIgnoreCase("minecraft:enchanting_table")) {
if (property > 3 && property < 7) {
// Send 2 properties, splitting it into enchantID & level
final short level = (short) (value >> 8);
final short enchantID = (short) (value & 0xFF);
wrapper.create(wrapper.getId(), new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.UNSIGNED_BYTE, windowId);
wrapper.write(Type.SHORT, property);
wrapper.write(Type.SHORT, enchantID);
}
}).scheduleSend(Protocol1_9To1_8.class);
wrapper.set(Type.SHORT, 0, (short) (property + 3));
wrapper.set(Type.SHORT, 1, level);
}
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.OPEN_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// 1 - Window Type
map(Type.STRING);
// 2 - Window Title
map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
// 3 - Slot Count
map(Type.UNSIGNED_BYTE);
// There is a horse parameter after this, we don't handle it and let it passthrough
// Inventory tracking
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String inventory = wrapper.get(Type.STRING, 0);
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
inventoryTracker.setInventory(inventory);
}
});
// Brewing patch
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String inventory = wrapper.get(Type.STRING, 0);
if (inventory.equals("minecraft:brewing_stand")) {
wrapper.set(Type.UNSIGNED_BYTE, 1, (short) (wrapper.get(Type.UNSIGNED_BYTE, 1) + 1));
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.SET_SLOT, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// 1 - Slot ID
map(Type.SHORT);
// 2 - Slot Value
map(Type.ITEM);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() && Via.getConfig().isShieldBlocking();
// Check if it is the inventory of the player
if (showShieldWhenSwordInHand) {
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
short slotID = wrapper.get(Type.SHORT, 0);
byte windowId = wrapper.get(Type.UNSIGNED_BYTE, 0).byteValue();
// Store item in slot
inventoryTracker.setItemId(windowId, slotID, stack == null ? 0 : stack.identifier());
// Sync shield item in offhand with main hand
entityTracker.syncShieldWithSword();
}
ItemRewriter.toClient(stack);
}
});
// Brewing patch
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
short slotID = wrapper.get(Type.SHORT, 0);
if (inventoryTracker.getInventory() != null) {
if (inventoryTracker.getInventory().equals("minecraft:brewing_stand")) {
if (slotID >= 4) {
wrapper.set(Type.SHORT, 0, (short) (slotID + 1));
}
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.WINDOW_ITEMS, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// 1 - Window Values
map(Type.ITEM_ARRAY);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item[] stacks = wrapper.get(Type.ITEM_ARRAY, 0);
Short windowId = wrapper.get(Type.UNSIGNED_BYTE, 0);
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() && Via.getConfig().isShieldBlocking();
for (short i = 0; i < stacks.length; i++) {
Item stack = stacks[i];
// Store items in slots
if (showShieldWhenSwordInHand) {
inventoryTracker.setItemId(windowId, i, stack == null ? 0 : stack.identifier());
}
ItemRewriter.toClient(stack);
}
// Sync shield item in offhand with main hand
if (showShieldWhenSwordInHand) {
entityTracker.syncShieldWithSword();
}
}
});
// Brewing Patch
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
if (inventoryTracker.getInventory() != null) {
if (inventoryTracker.getInventory().equals("minecraft:brewing_stand")) {
Item[] oldStack = wrapper.get(Type.ITEM_ARRAY, 0);
Item[] newStack = new Item[oldStack.length + 1];
for (int i = 0; i < newStack.length; i++) {
if (i > 4) {
newStack[i] = oldStack[i - 1];
} else {
if (i != 4) {
// Leave index 3 blank
newStack[i] = oldStack[i];
}
}
}
wrapper.set(Type.ITEM_ARRAY, 0, newStack);
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.CLOSE_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// Inventory tracking
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
inventoryTracker.setInventory(null);
inventoryTracker.resetInventory(wrapper.get(Type.UNSIGNED_BYTE, 0));
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.MAP_DATA, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Map ID
map(Type.VAR_INT);
// 1 - Map Scale
map(Type.BYTE);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) {
// 2 - Show marker
wrapper.write(Type.BOOLEAN, true);
}
});
// Everything else is passed through
}
});
/* Incoming Packets */
protocol.registerServerbound(ServerboundPackets1_9.CREATIVE_INVENTORY_ACTION, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Slot ID
map(Type.SHORT);
// 1 - Item
map(Type.ITEM);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() && Via.getConfig().isShieldBlocking();
if (showShieldWhenSwordInHand) {
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
short slotID = wrapper.get(Type.SHORT, 0);
// Update item in slot
inventoryTracker.setItemId((short) 0, slotID, stack == null ? 0 : stack.identifier());
// Sync shield item in offhand with main hand
entityTracker.syncShieldWithSword();
}
ItemRewriter.toServer(stack);
}
});
// Elytra throw patch
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
final short slot = wrapper.get(Type.SHORT, 0);
boolean throwItem = (slot == 45);
if (throwItem) {
// Send a packet wiping the slot
wrapper.create(ClientboundPackets1_9.SET_SLOT, new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
wrapper.write(Type.SHORT, slot);
wrapper.write(Type.ITEM, null);
}
}).send(Protocol1_9To1_8.class);
// Finally reset to simulate throwing item
// Set slot to -999
wrapper.set(Type.SHORT, 0, (short) -999);
}
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_9.CLICK_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// 1 - Slot ID
map(Type.SHORT);
// 2 - Button
map(Type.BYTE);
// 3 - Action
map(Type.SHORT);
// 4 - Mode
map(Type.VAR_INT, Type.BYTE);
// 5 - Clicked Item
map(Type.ITEM);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item stack = wrapper.get(Type.ITEM, 0);
if (Via.getConfig().isShowShieldWhenSwordInHand()) {
Short windowId = wrapper.get(Type.UNSIGNED_BYTE, 0);
byte mode = wrapper.get(Type.BYTE, 1);
short hoverSlot = wrapper.get(Type.SHORT, 0);
byte button = wrapper.get(Type.BYTE, 0);
// Move items in inventory to track the sword location
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
inventoryTracker.handleWindowClick(wrapper.user(), windowId, mode, hoverSlot, button);
}
ItemRewriter.toServer(stack);
}
});
// Brewing patch and elytra throw patch
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
final short windowID = wrapper.get(Type.UNSIGNED_BYTE, 0);
final short slot = wrapper.get(Type.SHORT, 0);
boolean throwItem = (slot == 45 && windowID == 0);
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
if (inventoryTracker.getInventory() != null) {
if (inventoryTracker.getInventory().equals("minecraft:brewing_stand")) {
if (slot == 4) {
throwItem = true;
}
if (slot > 4) {
wrapper.set(Type.SHORT, 0, (short) (slot - 1));
}
}
}
if (throwItem) {
// Send a packet wiping the slot
wrapper.create(ClientboundPackets1_9.SET_SLOT, new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.UNSIGNED_BYTE, (short) windowID);
wrapper.write(Type.SHORT, slot);
wrapper.write(Type.ITEM, null);
}
}).scheduleSend(Protocol1_9To1_8.class);
// Finally reset to simulate throwing item
// Set button to 0
wrapper.set(Type.BYTE, 0, (byte) 0);
// Set mode to 0
wrapper.set(Type.BYTE, 1, (byte) 0);
// Set slot to -999
wrapper.set(Type.SHORT, 0, (short) -999);
}
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_9.CLOSE_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// Inventory tracking
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
inventoryTracker.setInventory(null);
inventoryTracker.resetInventory(wrapper.get(Type.UNSIGNED_BYTE, 0));
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_9.HELD_ITEM_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Slot id
map(Type.SHORT);
// Blocking patch
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() && Via.getConfig().isShieldBlocking();
EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
if (entityTracker.isBlocking()) {
entityTracker.setBlocking(false);
if (!showShieldWhenSwordInHand) {
entityTracker.setSecondHand(null);
}
}
if (showShieldWhenSwordInHand) {
// Update current held item slot index
entityTracker.setHeldItemSlot(wrapper.get(Type.SHORT, 0));
// Sync shield item in offhand with main hand
entityTracker.syncShieldWithSword();
}
}
});
}
});
}
use of com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8 in project ViaVersion by ViaVersion.
the class SpawnPackets method register.
public static void register(Protocol1_9To1_8 protocol) {
protocol.registerClientbound(ClientboundPackets1_8.SPAWN_ENTITY, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
// 1 - UUID
wrapper.write(Type.UUID, tracker.getEntityUUID(entityID));
}
});
// 2 - Type
map(Type.BYTE);
// Parse this info
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityID = wrapper.get(Type.VAR_INT, 0);
int typeID = wrapper.get(Type.BYTE, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
tracker.addEntity(entityID, Entity1_10Types.getTypeFromId(typeID, true));
tracker.sendMetadataBuffer(entityID);
}
});
// 3 - X - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 4 - Y - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 5 - Z - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 6 - Pitch
map(Type.BYTE);
// 7 - Yaw
map(Type.BYTE);
// 8 - Data
map(Type.INT);
// Create last 3 shorts
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Data (1st Integer)
int data = wrapper.get(Type.INT, 0);
short vX = 0, vY = 0, vZ = 0;
if (data > 0) {
vX = wrapper.read(Type.SHORT);
vY = wrapper.read(Type.SHORT);
vZ = wrapper.read(Type.SHORT);
}
wrapper.write(Type.SHORT, vX);
wrapper.write(Type.SHORT, vY);
wrapper.write(Type.SHORT, vZ);
}
});
// Handle potions
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
final int entityID = wrapper.get(Type.VAR_INT, 0);
// Data
final int data = wrapper.get(Type.INT, 0);
int typeID = wrapper.get(Type.BYTE, 0);
if (Entity1_10Types.getTypeFromId(typeID, true) == Entity1_10Types.EntityType.SPLASH_POTION) {
// Convert this to meta data, woo!
PacketWrapper metaPacket = wrapper.create(ClientboundPackets1_9.ENTITY_METADATA, new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.VAR_INT, entityID);
List<Metadata> meta = new ArrayList<>();
// Potion
Item item = new DataItem(373, (byte) 1, (short) data, null);
// Rewrite so that it gets the right nbt
ItemRewriter.toClient(item);
// TEMP FIX FOR POTIONS UNTIL WE FIGURE OUT HOW TO TRANSFORM SENT PACKETS
Metadata potion = new Metadata(5, MetaType1_9.Slot, item);
meta.add(potion);
wrapper.write(Types1_9.METADATA_LIST, meta);
}
});
// Fix packet order
wrapper.send(Protocol1_9To1_8.class);
metaPacket.send(Protocol1_9To1_8.class);
wrapper.cancel();
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.SPAWN_EXPERIENCE_ORB, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// Parse this info
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
tracker.addEntity(entityID, Entity1_10Types.EntityType.EXPERIENCE_ORB);
tracker.sendMetadataBuffer(entityID);
}
});
// 1 - X - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 2 - Y - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 3 - Z - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 4 - Amount to spawn
map(Type.SHORT);
}
});
protocol.registerClientbound(ClientboundPackets1_8.SPAWN_GLOBAL_ENTITY, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// 1 - Type
map(Type.BYTE);
// Parse this info
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Currently only lightning uses this
int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
tracker.addEntity(entityID, Entity1_10Types.EntityType.LIGHTNING);
tracker.sendMetadataBuffer(entityID);
}
});
// 2 - X - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 3 - Y - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 4 - Z - Needs to be divide by 32
map(Type.INT, toNewDouble);
}
});
protocol.registerClientbound(ClientboundPackets1_8.SPAWN_MOB, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
// 1 - UUID
wrapper.write(Type.UUID, tracker.getEntityUUID(entityID));
}
});
// 2 - Type
map(Type.UNSIGNED_BYTE);
// Parse this info
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityID = wrapper.get(Type.VAR_INT, 0);
int typeID = wrapper.get(Type.UNSIGNED_BYTE, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
tracker.addEntity(entityID, Entity1_10Types.getTypeFromId(typeID, false));
tracker.sendMetadataBuffer(entityID);
}
});
// 3 - X - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 4 - Y - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 5 - Z - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 6 - Yaw
map(Type.BYTE);
// 7 - Pitch
map(Type.BYTE);
// 8 - Head Pitch
map(Type.BYTE);
// 9 - Velocity X
map(Type.SHORT);
// 10 - Velocity Y
map(Type.SHORT);
// 11 - Velocity Z
map(Type.SHORT);
map(Types1_8.METADATA_LIST, Types1_9.METADATA_LIST);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
int entityId = wrapper.get(Type.VAR_INT, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
if (tracker.hasEntity(entityId)) {
protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user());
} else {
Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId);
metadataList.clear();
}
}
});
// Handler for meta data
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
tracker.handleMetadata(entityID, metadataList);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.SPAWN_PAINTING, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// Parse this info
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
tracker.addEntity(entityID, Entity1_10Types.EntityType.PAINTING);
tracker.sendMetadataBuffer(entityID);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
// 1 - UUID
wrapper.write(Type.UUID, tracker.getEntityUUID(entityID));
}
});
// 2 - Title
map(Type.STRING);
// 3 - Position
map(Type.POSITION);
// 4 - Direction
map(Type.BYTE);
}
});
protocol.registerClientbound(ClientboundPackets1_8.SPAWN_PLAYER, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// 1 - Player UUID
map(Type.UUID);
// Parse this info
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
tracker.addEntity(entityID, Entity1_10Types.EntityType.PLAYER);
tracker.sendMetadataBuffer(entityID);
}
});
// 2 - X - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 3 - Y - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 4 - Z - Needs to be divide by 32
map(Type.INT, toNewDouble);
// 5 - Yaw
map(Type.BYTE);
// 6 - Pitch
map(Type.BYTE);
handler(new // Handle discontinued player hand item
PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
short item = wrapper.read(Type.SHORT);
if (item != 0) {
PacketWrapper packet = PacketWrapper.create(ClientboundPackets1_9.ENTITY_EQUIPMENT, null, wrapper.user());
packet.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0));
packet.write(Type.VAR_INT, 0);
packet.write(Type.ITEM, new DataItem(item, (byte) 1, (short) 0, null));
try {
packet.send(Protocol1_9To1_8.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
map(Types1_8.METADATA_LIST, Types1_9.METADATA_LIST);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
int entityId = wrapper.get(Type.VAR_INT, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
if (tracker.hasEntity(entityId)) {
protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user());
} else {
Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId);
metadataList.clear();
}
}
});
// Handler for meta data
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
tracker.handleMetadata(entityID, metadataList);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.DESTROY_ENTITIES, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entities to destroy
map(Type.VAR_INT_ARRAY_PRIMITIVE);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int[] entities = wrapper.get(Type.VAR_INT_ARRAY_PRIMITIVE, 0);
EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
for (int entity : entities) {
// EntityTracker
tracker.removeEntity(entity);
}
}
});
}
});
}
use of com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8 in project ViaVersion by ViaVersion.
the class WorldPackets method register.
public static void register(Protocol protocol) {
protocol.registerClientbound(ClientboundPackets1_8.UPDATE_SIGN, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Sign Position
map(Type.POSITION);
// 1 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
// 2 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
// 3 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
// 4 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
}
});
protocol.registerClientbound(ClientboundPackets1_8.EFFECT, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Effect ID
map(Type.INT);
// 1 - Position
map(Type.POSITION);
// 2 - Data
map(Type.INT);
// 3 - Disable relative volume
map(Type.BOOLEAN);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.INT, 0);
id = Effect.getNewId(id);
wrapper.set(Type.INT, 0, id);
}
});
// Rewrite potion effect as it changed to use a dynamic registry
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.INT, 0);
if (id == 2002) {
int data = wrapper.get(Type.INT, 1);
int newData = ItemRewriter.getNewEffectID(data);
wrapper.set(Type.INT, 1, newData);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.NAMED_SOUND, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Sound Name
map(Type.STRING);
// 1 - Sound Category ID
// Everything else get's written through
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String name = wrapper.get(Type.STRING, 0);
SoundEffect effect = SoundEffect.getByName(name);
int catid = 0;
String newname = name;
if (effect != null) {
catid = effect.getCategory().getId();
newname = effect.getNewName();
}
wrapper.set(Type.STRING, 0, newname);
// Write Category ID
wrapper.write(Type.VAR_INT, catid);
if (effect != null && effect.isBreaksound()) {
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
// Position X
int x = wrapper.passthrough(Type.INT);
// Position Y
int y = wrapper.passthrough(Type.INT);
// Position Z
int z = wrapper.passthrough(Type.INT);
if (tracker.interactedBlockRecently((int) Math.floor(x / 8.0), (int) Math.floor(y / 8.0), (int) Math.floor(z / 8.0))) {
wrapper.cancel();
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
ClientChunks clientChunks = wrapper.user().get(ClientChunks.class);
Chunk chunk = wrapper.read(new Chunk1_8Type(clientWorld));
long chunkHash = ClientChunks.toLong(chunk.getX(), chunk.getZ());
// Check if the chunk should be handled as an unload packet
if (chunk.isFullChunk() && chunk.getBitmask() == 0) {
wrapper.setPacketType(ClientboundPackets1_9.UNLOAD_CHUNK);
wrapper.write(Type.INT, chunk.getX());
wrapper.write(Type.INT, chunk.getZ());
// Remove commandBlocks on chunk unload
CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
provider.unloadChunk(wrapper.user(), chunk.getX(), chunk.getZ());
clientChunks.getLoadedChunks().remove(chunkHash);
// Unload the empty chunks
if (Via.getConfig().isChunkBorderFix()) {
for (BlockFace face : BlockFace.HORIZONTAL) {
int chunkX = chunk.getX() + face.modX();
int chunkZ = chunk.getZ() + face.modZ();
if (!clientChunks.getLoadedChunks().contains(ClientChunks.toLong(chunkX, chunkZ))) {
PacketWrapper unloadChunk = wrapper.create(ClientboundPackets1_9.UNLOAD_CHUNK);
unloadChunk.write(Type.INT, chunkX);
unloadChunk.write(Type.INT, chunkZ);
unloadChunk.send(Protocol1_9To1_8.class);
}
}
}
} else {
Type<Chunk> chunkType = new Chunk1_9_1_2Type(clientWorld);
wrapper.write(chunkType, chunk);
clientChunks.getLoadedChunks().add(chunkHash);
// Send empty chunks surrounding the loaded chunk to force 1.9+ clients to render the new chunk
if (Via.getConfig().isChunkBorderFix()) {
for (BlockFace face : BlockFace.HORIZONTAL) {
int chunkX = chunk.getX() + face.modX();
int chunkZ = chunk.getZ() + face.modZ();
if (!clientChunks.getLoadedChunks().contains(ClientChunks.toLong(chunkX, chunkZ))) {
PacketWrapper emptyChunk = wrapper.create(ClientboundPackets1_9.CHUNK_DATA);
Chunk c = new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], new int[256], new ArrayList<>());
emptyChunk.write(chunkType, c);
emptyChunk.send(Protocol1_9To1_8.class);
}
}
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.MAP_BULK_CHUNK, null, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
// Cancel the packet from being sent
wrapper.cancel();
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
ClientChunks clientChunks = wrapper.user().get(ClientChunks.class);
Chunk[] chunks = wrapper.read(new ChunkBulk1_8Type(clientWorld));
Type<Chunk> chunkType = new Chunk1_9_1_2Type(clientWorld);
// Split into multiple chunk packets
for (Chunk chunk : chunks) {
PacketWrapper chunkData = wrapper.create(ClientboundPackets1_9.CHUNK_DATA);
chunkData.write(chunkType, chunk);
chunkData.send(Protocol1_9To1_8.class);
clientChunks.getLoadedChunks().add(ClientChunks.toLong(chunk.getX(), chunk.getZ()));
// Send empty chunks surrounding the loaded chunk to force 1.9+ clients to render the new chunk
if (Via.getConfig().isChunkBorderFix()) {
for (BlockFace face : BlockFace.HORIZONTAL) {
int chunkX = chunk.getX() + face.modX();
int chunkZ = chunk.getZ() + face.modZ();
if (!clientChunks.getLoadedChunks().contains(ClientChunks.toLong(chunkX, chunkZ))) {
PacketWrapper emptyChunk = wrapper.create(ClientboundPackets1_9.CHUNK_DATA);
Chunk c = new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], new int[256], new ArrayList<>());
emptyChunk.write(chunkType, c);
emptyChunk.send(Protocol1_9To1_8.class);
}
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Block Position
map(Type.POSITION);
// 1 - Action
map(Type.UNSIGNED_BYTE);
// 2 - NBT (Might not be present)
map(Type.NBT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int action = wrapper.get(Type.UNSIGNED_BYTE, 0);
if (action == 1) {
// Update Spawner
CompoundTag tag = wrapper.get(Type.NBT, 0);
if (tag != null) {
if (tag.contains("EntityId")) {
String entity = (String) tag.get("EntityId").getValue();
CompoundTag spawn = new CompoundTag();
spawn.put("id", new StringTag(entity));
tag.put("SpawnData", spawn);
} else {
// EntityID does not exist
CompoundTag spawn = new CompoundTag();
// Make spawners show up as empty when no EntityId is given.
spawn.put("id", new StringTag("AreaEffectCloud"));
tag.put("SpawnData", spawn);
}
}
}
if (action == 2) {
// Update Command Block
CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
provider.addOrUpdateBlock(wrapper.user(), wrapper.get(Type.POSITION, 0), wrapper.get(Type.NBT, 0));
// To prevent window issues don't send updates
wrapper.cancel();
}
}
});
}
});
/* Incoming Packets */
protocol.registerServerbound(ServerboundPackets1_9.UPDATE_SIGN, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Sign Position
map(Type.POSITION);
// 1 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
// 2 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
// 3 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
// 4 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
}
});
protocol.registerServerbound(ServerboundPackets1_9.PLAYER_DIGGING, new PacketRemapper() {
@Override
public void registerMap() {
// Action
map(Type.VAR_INT);
// Position
map(Type.POSITION);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int status = wrapper.get(Type.VAR_INT, 0);
if (status == 6)
wrapper.cancel();
}
});
// Blocking
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int status = wrapper.get(Type.VAR_INT, 0);
if (status == 5 || status == 4 || status == 3) {
EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
if (entityTracker.isBlocking()) {
entityTracker.setBlocking(false);
if (!Via.getConfig().isShowShieldWhenSwordInHand()) {
entityTracker.setSecondHand(null);
}
}
}
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_9.USE_ITEM, null, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int hand = wrapper.read(Type.VAR_INT);
// Wipe the input buffer
wrapper.clearInputBuffer();
// First set this packet ID to Block placement
wrapper.setId(0x08);
wrapper.write(Type.POSITION, new Position(-1, (short) -1, -1));
wrapper.write(Type.UNSIGNED_BYTE, (short) 255);
// Write item in hand
Item item = Protocol1_9To1_8.getHandItem(wrapper.user());
// Blocking patch
if (Via.getConfig().isShieldBlocking()) {
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
// Check if the shield is already there or if we have to give it here
boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand();
// Method to identify the sword in hand
boolean isSword = showShieldWhenSwordInHand ? tracker.hasSwordInHand() : item != null && Protocol1_9To1_8.isSword(item.identifier());
if (isSword) {
if (hand == 0) {
if (!tracker.isBlocking()) {
tracker.setBlocking(true);
// Check if the shield is already in the offhand
if (!showShieldWhenSwordInHand && tracker.getItemInSecondHand() == null) {
// Set shield in offhand when interacting with main hand
Item shield = new DataItem(442, (byte) 1, (short) 0, null);
tracker.setSecondHand(shield);
}
}
}
// Use the main hand to trigger the blocking
boolean blockUsingMainHand = Via.getConfig().isNoDelayShieldBlocking() && !showShieldWhenSwordInHand;
if (blockUsingMainHand && hand == 1 || !blockUsingMainHand && hand == 0) {
wrapper.cancel();
}
} else {
if (!showShieldWhenSwordInHand) {
// Remove the shield from the offhand
tracker.setSecondHand(null);
}
tracker.setBlocking(false);
}
}
wrapper.write(Type.ITEM, item);
wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_9.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Position
map(Type.POSITION);
// 1 - Block Face
map(Type.VAR_INT, Type.UNSIGNED_BYTE);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// 2 - Hand
final int hand = wrapper.read(Type.VAR_INT);
if (hand != 0)
wrapper.cancel();
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = Protocol1_9To1_8.getHandItem(wrapper.user());
// 3 - Item
wrapper.write(Type.ITEM, item);
}
});
// 4 - X
map(Type.UNSIGNED_BYTE);
// 5 - Y
map(Type.UNSIGNED_BYTE);
// 6 - Z
map(Type.UNSIGNED_BYTE);
// Register block place to fix sounds
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int face = wrapper.get(Type.UNSIGNED_BYTE, 0);
if (face == 255)
return;
Position p = wrapper.get(Type.POSITION, 0);
int x = p.x();
int y = p.y();
int z = p.z();
switch(face) {
case 0:
y--;
break;
case 1:
y++;
break;
case 2:
z--;
break;
case 3:
z++;
break;
case 4:
x--;
break;
case 5:
x++;
break;
}
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
tracker.addBlockInteraction(new Position(x, y, z));
}
});
// Handle CommandBlocks
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
Position pos = wrapper.get(Type.POSITION, 0);
Optional<CompoundTag> tag = provider.get(wrapper.user(), pos);
// Send the Update Block Entity packet if present
if (tag.isPresent()) {
PacketWrapper updateBlockEntity = PacketWrapper.create(ClientboundPackets1_9.BLOCK_ENTITY_DATA, null, wrapper.user());
updateBlockEntity.write(Type.POSITION, pos);
updateBlockEntity.write(Type.UNSIGNED_BYTE, (short) 2);
updateBlockEntity.write(Type.NBT, tag.get());
updateBlockEntity.scheduleSend(Protocol1_9To1_8.class);
}
}
});
}
});
}
use of com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8 in project ViaVersion by ViaVersion.
the class EntityTracker1_9 method handleMetadata.
public void handleMetadata(int entityId, List<Metadata> metadataList) {
com.viaversion.viaversion.api.minecraft.entities.EntityType type = entityType(entityId);
if (type == null) {
return;
}
for (Metadata metadata : new ArrayList<>(metadataList)) {
// Fix: wither (crash fix)
if (type == EntityType.WITHER) {
if (metadata.id() == 10) {
metadataList.remove(metadata);
// metadataList.add(new Metadata(10, NewType.Byte.getTypeID(), Type.BYTE, 0));
}
}
// Fix: enderdragon (crash fix)
if (type == EntityType.ENDER_DRAGON) {
if (metadata.id() == 11) {
metadataList.remove(metadata);
// metadataList.add(new Metadata(11, NewType.Byte.getTypeID(), Type.VAR_INT, 0));
}
}
if (type == EntityType.SKELETON) {
if ((getMetaByIndex(metadataList, 12)) == null) {
metadataList.add(new Metadata(12, MetaType1_9.Boolean, true));
}
}
// ECHOPET Patch
if (type == EntityType.HORSE) {
// Wrong metadata value from EchoPet, patch since it's discontinued. (https://github.com/DSH105/EchoPet/blob/06947a8b08ce40be9a518c2982af494b3b99d140/modules/API/src/main/java/com/dsh105/echopet/compat/api/entity/HorseArmour.java#L22)
if (metadata.id() == 16 && (int) metadata.getValue() == Integer.MIN_VALUE)
metadata.setValue(0);
}
if (type == EntityType.PLAYER) {
if (metadata.id() == 0) {
// Byte
byte data = (byte) metadata.getValue();
if (entityId != getProvidedEntityId() && Via.getConfig().isShieldBlocking()) {
if ((data & 0x10) == 0x10) {
if (validBlocking.contains(entityId)) {
Item shield = new DataItem(442, (byte) 1, (short) 0, null);
setSecondHand(entityId, shield);
} else {
setSecondHand(entityId, null);
}
} else {
setSecondHand(entityId, null);
}
}
}
if (metadata.id() == 12 && Via.getConfig().isLeftHandedHandling()) {
// Player model
metadataList.add(new Metadata(// Main hand
13, MetaType1_9.Byte, (byte) (((((byte) metadata.getValue()) & 0x80) != 0) ? 0 : 1)));
}
}
if (type == EntityType.ARMOR_STAND && Via.getConfig().isHologramPatch()) {
if (metadata.id() == 0 && getMetaByIndex(metadataList, 10) != null) {
// Only happens if the armorstand is small
Metadata meta = getMetaByIndex(metadataList, 10);
byte data = (byte) metadata.getValue();
// Check invisible | Check small | Check if custom name is empty | Check if custom name visible is true
Metadata displayName;
Metadata displayNameVisible;
if ((data & 0x20) == 0x20 && ((byte) meta.getValue() & 0x01) == 0x01 && (displayName = getMetaByIndex(metadataList, 2)) != null && !((String) displayName.getValue()).isEmpty() && (displayNameVisible = getMetaByIndex(metadataList, 3)) != null && (boolean) displayNameVisible.getValue()) {
if (!knownHolograms.contains(entityId)) {
knownHolograms.add(entityId);
try {
// Send movement
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.ENTITY_POSITION, null, user());
wrapper.write(Type.VAR_INT, entityId);
wrapper.write(Type.SHORT, (short) 0);
wrapper.write(Type.SHORT, (short) (128D * (Via.getConfig().getHologramYOffset() * 32D)));
wrapper.write(Type.SHORT, (short) 0);
wrapper.write(Type.BOOLEAN, true);
wrapper.scheduleSend(Protocol1_9To1_8.class);
} catch (Exception ignored) {
}
}
}
}
}
// Boss bar
if (Via.getConfig().isBossbarPatch()) {
if (type == EntityType.ENDER_DRAGON || type == EntityType.WITHER) {
if (metadata.id() == 2) {
BossBar bar = bossBarMap.get(entityId);
String title = (String) metadata.getValue();
title = title.isEmpty() ? (type == EntityType.ENDER_DRAGON ? DRAGON_TRANSLATABLE : WITHER_TRANSLATABLE) : title;
if (bar == null) {
bar = Via.getAPI().legacyAPI().createLegacyBossBar(title, BossColor.PINK, BossStyle.SOLID);
bossBarMap.put(entityId, bar);
bar.addConnection(user());
bar.show();
// Send to provider
Via.getManager().getProviders().get(BossBarProvider.class).handleAdd(user(), bar.getId());
} else {
bar.setTitle(title);
}
} else if (metadata.id() == 6 && !Via.getConfig().isBossbarAntiflicker()) {
// If anti flicker is enabled, don't update health
BossBar bar = bossBarMap.get(entityId);
// Make health range between 0 and 1
float maxHealth = type == EntityType.ENDER_DRAGON ? 200.0f : 300.0f;
float health = Math.max(0.0f, Math.min(((float) metadata.getValue()) / maxHealth, 1.0f));
if (bar == null) {
String title = type == EntityType.ENDER_DRAGON ? DRAGON_TRANSLATABLE : WITHER_TRANSLATABLE;
bar = Via.getAPI().legacyAPI().createLegacyBossBar(title, health, BossColor.PINK, BossStyle.SOLID);
bossBarMap.put(entityId, bar);
bar.addConnection(user());
bar.show();
// Send to provider
Via.getManager().getProviders().get(BossBarProvider.class).handleAdd(user(), bar.getId());
} else {
bar.setHealth(health);
}
}
}
}
}
}
use of com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8 in project ViaVersion by ViaVersion.
the class CommandBlockProvider method sendPermission.
public void sendPermission(UserConnection user) throws Exception {
if (!isEnabled())
return;
// Entity status
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.ENTITY_STATUS, null, user);
EntityTracker1_9 tracker = user.getEntityTracker(Protocol1_9To1_8.class);
// Entity ID
wrapper.write(Type.INT, tracker.getProvidedEntityId());
// Hardcoded op permission level
wrapper.write(Type.BYTE, (byte) 26);
wrapper.scheduleSend(Protocol1_9To1_8.class);
user.get(CommandBlockStorage.class).setPermissions(true);
}
Aggregations