use of com.viaversion.viaversion.api.protocol.packet.PacketWrapper in project ViaVersion by ViaVersion.
the class Protocol1_12To1_11_1 method registerPackets.
@Override
protected void registerPackets() {
metadataRewriter.register();
itemRewriter.register();
registerClientbound(ClientboundPackets1_9_3.SPAWN_ENTITY, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity id
map(Type.VAR_INT);
// 1 - UUID
map(Type.UUID);
// 2 - Type
map(Type.BYTE);
// Track Entity
handler(metadataRewriter.objectTrackerHandler());
}
});
registerClientbound(ClientboundPackets1_9_3.SPAWN_MOB, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// 1 - Entity UUID
map(Type.UUID);
// 2 - Entity Type
map(Type.VAR_INT);
// 3 - X
map(Type.DOUBLE);
// 4 - Y
map(Type.DOUBLE);
// 5 - Z
map(Type.DOUBLE);
// 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);
// 12 - Metadata
map(Types1_12.METADATA_LIST);
// Track mob and rewrite metadata
handler(metadataRewriter.trackerAndRewriterHandler(Types1_12.METADATA_LIST));
}
});
registerClientbound(ClientboundPackets1_9_3.CHAT_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
if (!Via.getConfig().is1_12NBTArrayFix())
return;
try {
JsonElement obj = Protocol1_9To1_8.FIX_JSON.transform(null, wrapper.passthrough(Type.COMPONENT).toString());
TranslateRewriter.toClient(obj, wrapper.user());
ChatItemRewriter.toClient(obj, wrapper.user());
wrapper.set(Type.COMPONENT, 0, obj);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
registerClientbound(ClientboundPackets1_9_3.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);
Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
Chunk chunk = wrapper.passthrough(type);
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
int block = section.getBlockWithoutData(x, y, z);
// Is this a bed?
if (block == 26) {
// NBT -> { color:14, x:132, y:64, z:222, id:"minecraft:bed" } (Debug output)
CompoundTag tag = new CompoundTag();
// Set color to red (Default in previous versions)
tag.put("color", new IntTag(14));
tag.put("x", new IntTag(x + (chunk.getX() << 4)));
tag.put("y", new IntTag(y + (i << 4)));
tag.put("z", new IntTag(z + (chunk.getZ() << 4)));
tag.put("id", new StringTag("minecraft:bed"));
// Add a fake block entity
chunk.getBlockEntities().add(tag);
}
}
}
}
}
}
});
}
});
metadataRewriter.registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
metadataRewriter.registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_12.METADATA_LIST);
registerClientbound(ClientboundPackets1_9_3.JOIN_GAME, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT);
map(Type.UNSIGNED_BYTE);
map(Type.INT);
handler(wrapper -> {
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
});
}
});
registerClientbound(ClientboundPackets1_9_3.RESPAWN, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT);
handler(wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId);
});
}
});
new SoundRewriter(this, this::getNewSoundId).registerSound(ClientboundPackets1_9_3.SOUND);
// New packet at 0x01
cancelServerbound(ServerboundPackets1_12.PREPARE_CRAFTING_GRID);
// Client Settings (max length changed)
registerServerbound(ServerboundPackets1_12.CLIENT_SETTINGS, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Locale
map(Type.STRING);
// 1 - view distance
map(Type.BYTE);
// 2 - chat mode
map(Type.VAR_INT);
// 3 - chat colors
map(Type.BOOLEAN);
// 4 - chat flags
map(Type.UNSIGNED_BYTE);
// 5 - main hand
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// As part of the fix for MC-111054, the max length of
// the locale was raised to 16 (from 7), and the client
// now makes sure that resource packs have names in that
// length. However, for older servers, it is still 7,
// and thus the server will reject it (and the client
// won't know that the pack's invalid).
// The fix is to just silently lower the length. The
// server doesn't actually use the locale anywhere, so
// this is fine.
String locale = wrapper.get(Type.STRING, 0);
if (locale.length() > 7) {
wrapper.set(Type.STRING, 0, locale.substring(0, 7));
}
}
});
}
});
// New packet at 0x17
cancelServerbound(ServerboundPackets1_12.RECIPE_BOOK_DATA);
// New packet 0x19
cancelServerbound(ServerboundPackets1_12.ADVANCEMENT_TAB);
}
use of com.viaversion.viaversion.api.protocol.packet.PacketWrapper in project ViaVersion by ViaVersion.
the class InventoryPackets method registerPackets.
@Override
public void registerPackets() {
registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
// Plugin message Packet -> Trading
protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Channel
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) {
// Passthrough Window ID
wrapper.passthrough(Type.INT);
int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
// Input Item
handleItemToClient(wrapper.passthrough(Type.ITEM));
// Output Item
handleItemToClient(wrapper.passthrough(Type.ITEM));
// Has second item
boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
if (secondItem) {
// Second Item
handleItemToClient(wrapper.passthrough(Type.ITEM));
}
// Trade disabled
wrapper.passthrough(Type.BOOLEAN);
// Number of tools uses
wrapper.passthrough(Type.INT);
// Maximum number of trade uses
wrapper.passthrough(Type.INT);
}
}
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_12.CLICK_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Window ID
map(Type.UNSIGNED_BYTE);
// 1 - Slot
map(Type.SHORT);
// 2 - Button
map(Type.BYTE);
// 3 - Action number
map(Type.SHORT);
// 4 - Mode
map(Type.VAR_INT);
// 5 - Clicked Item
map(Type.ITEM);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.ITEM, 0);
if (!Via.getConfig().is1_12QuickMoveActionFix()) {
handleItemToServer(item);
return;
}
byte button = wrapper.get(Type.BYTE, 0);
int mode = wrapper.get(Type.VAR_INT, 0);
// QUICK_MOVE PATCH (Shift + (click/double click))
if (mode == 1 && button == 0 && item == null) {
short windowId = wrapper.get(Type.UNSIGNED_BYTE, 0);
short slotId = wrapper.get(Type.SHORT, 0);
short actionId = wrapper.get(Type.SHORT, 1);
InventoryQuickMoveProvider provider = Via.getManager().getProviders().get(InventoryQuickMoveProvider.class);
boolean succeed = provider.registerQuickMoveAction(windowId, slotId, actionId, wrapper.user());
if (succeed) {
wrapper.cancel();
}
// otherwise just pass through so the server sends the PacketPlayOutTransaction packet.
} else {
handleItemToServer(item);
}
}
});
}
});
// Creative Inventory Action
registerCreativeInvAction(ServerboundPackets1_12.CREATIVE_INVENTORY_ACTION, Type.ITEM);
}
use of com.viaversion.viaversion.api.protocol.packet.PacketWrapper in project ViaVersion by ViaVersion.
the class CommonBoss method getPacket.
private PacketWrapper getPacket(UpdateAction action, UserConnection connection) {
try {
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, null, connection);
wrapper.write(Type.UUID, uuid);
wrapper.write(Type.VAR_INT, action.getId());
switch(action) {
case ADD:
Protocol1_9To1_8.FIX_JSON.write(wrapper, title);
wrapper.write(Type.FLOAT, health);
wrapper.write(Type.VAR_INT, color.getId());
wrapper.write(Type.VAR_INT, style.getId());
wrapper.write(Type.BYTE, (byte) flagToBytes());
break;
case REMOVE:
break;
case UPDATE_HEALTH:
wrapper.write(Type.FLOAT, health);
break;
case UPDATE_TITLE:
Protocol1_9To1_8.FIX_JSON.write(wrapper, title);
break;
case UPDATE_STYLE:
wrapper.write(Type.VAR_INT, color.getId());
wrapper.write(Type.VAR_INT, style.getId());
break;
case UPDATE_FLAGS:
wrapper.write(Type.BYTE, (byte) flagToBytes());
break;
}
return wrapper;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of com.viaversion.viaversion.api.protocol.packet.PacketWrapper in project ViaVersion by ViaVersion.
the class CommonBoss method sendPacket.
private void sendPacket(UpdateAction action) {
for (UserConnection conn : new ArrayList<>(connections.values())) {
PacketWrapper wrapper = getPacket(action, conn);
sendPacketConnection(conn, wrapper);
}
}
use of com.viaversion.viaversion.api.protocol.packet.PacketWrapper in project ViaVersion by ViaVersion.
the class InventoryPackets method registerPackets.
@Override
public void registerPackets() {
registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_ITEM);
registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_ITEM_ARRAY);
registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_ITEM);
registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
// Channel
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String channel = wrapper.get(Type.STRING, 0);
if (channel.equals("minecraft:trader_list") || channel.equals("trader_list")) {
// Passthrough Window ID
wrapper.passthrough(Type.INT);
int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
// Input Item
handleItemToClient(wrapper.passthrough(Type.FLAT_ITEM));
// Output Item
handleItemToClient(wrapper.passthrough(Type.FLAT_ITEM));
// Has second item
boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
if (secondItem) {
// Second Item
handleItemToClient(wrapper.passthrough(Type.FLAT_ITEM));
}
// Trade disabled
wrapper.passthrough(Type.BOOLEAN);
// Number of tools uses
wrapper.passthrough(Type.INT);
// Maximum number of trade uses
wrapper.passthrough(Type.INT);
}
}
}
});
}
});
registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_ITEM);
RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol);
protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
int size = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < size; i++) {
// First id, then type
String id = wrapper.passthrough(Type.STRING);
String type = wrapper.passthrough(Type.STRING).replace("minecraft:", "");
recipeRewriter.handle(wrapper, type);
}
});
}
});
registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_ITEM);
registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_ITEM);
registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_ITEM, Type.FLOAT);
}
Aggregations