use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaVersion by ViaVersion.
the class FlatVarIntItemType method read.
@Override
public Item read(ByteBuf buffer) throws Exception {
boolean present = buffer.readBoolean();
if (!present) {
return null;
} else {
Item item = new DataItem();
item.setIdentifier(VAR_INT.readPrimitive(buffer));
item.setAmount(buffer.readByte());
item.setTag(NBT.read(buffer));
return item;
}
}
use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaVersion by ViaVersion.
the class MetadataRewriter1_14To1_13_2 method handleMetadata.
@Override
protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
metadata.setMetaType(Types1_14.META_TYPES.byId(metadata.metaType().typeId()));
EntityTracker1_14 tracker = tracker(connection);
if (metadata.metaType() == Types1_14.META_TYPES.itemType) {
protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == Types1_14.META_TYPES.blockStateType) {
// Convert to new block id
int data = (int) metadata.getValue();
metadata.setValue(protocol.getMappingData().getNewBlockStateId(data));
} else if (metadata.metaType() == Types1_14.META_TYPES.particleType) {
rewriteParticle((Particle) metadata.getValue());
}
if (type == null)
return;
// Metadata 6 added to abstract_entity
if (metadata.id() > 5) {
metadata.setId(metadata.id() + 1);
}
if (metadata.id() == 8 && type.isOrHasParent(Entity1_14Types.LIVINGENTITY)) {
final float v = ((Number) metadata.getValue()).floatValue();
if (Float.isNaN(v) && Via.getConfig().is1_14HealthNaNFix()) {
metadata.setValue(1F);
}
}
// Metadata 12 added to living_entity
if (metadata.id() > 11 && type.isOrHasParent(Entity1_14Types.LIVINGENTITY)) {
metadata.setId(metadata.id() + 1);
}
if (type.isOrHasParent(Entity1_14Types.ABSTRACT_INSENTIENT)) {
if (metadata.id() == 13) {
tracker.setInsentientData(entityId, (byte) ((((Number) metadata.getValue()).byteValue() & ~0x4) | // New attacking metadata
(tracker.getInsentientData(entityId) & 0x4)));
metadata.setValue(tracker.getInsentientData(entityId));
}
}
if (type.isOrHasParent(Entity1_14Types.PLAYER)) {
if (entityId != tracker.clientEntityId()) {
if (metadata.id() == 0) {
byte flags = ((Number) metadata.getValue()).byteValue();
// Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize
tracker.setEntityFlags(entityId, flags);
} else if (metadata.id() == 7) {
tracker.setRiptide(entityId, (((Number) metadata.getValue()).byteValue() & 0x4) != 0);
}
if (metadata.id() == 0 || metadata.id() == 7) {
metadatas.add(new Metadata(6, Types1_14.META_TYPES.poseType, recalculatePlayerPose(entityId, tracker)));
}
}
} else if (type.isOrHasParent(Entity1_14Types.ZOMBIE)) {
if (metadata.id() == 16) {
tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | // New attacking
((boolean) metadata.getValue() ? 0x4 : 0)));
// "Are hands held up"
metadatas.remove(metadata);
metadatas.add(new Metadata(13, Types1_14.META_TYPES.byteType, tracker.getInsentientData(entityId)));
} else if (metadata.id() > 16) {
metadata.setId(metadata.id() - 1);
}
}
if (type.isOrHasParent(Entity1_14Types.MINECART_ABSTRACT)) {
if (metadata.id() == 10) {
// New block format
int data = (int) metadata.getValue();
metadata.setValue(protocol.getMappingData().getNewBlockStateId(data));
}
} else if (type.is(Entity1_14Types.HORSE)) {
if (metadata.id() == 18) {
metadatas.remove(metadata);
int armorType = (int) metadata.getValue();
Item armorItem = null;
if (armorType == 1) {
// iron armor
armorItem = new DataItem(protocol.getMappingData().getNewItemId(727), (byte) 1, (short) 0, null);
} else if (armorType == 2) {
// gold armor
armorItem = new DataItem(protocol.getMappingData().getNewItemId(728), (byte) 1, (short) 0, null);
} else if (armorType == 3) {
// diamond armor
armorItem = new DataItem(protocol.getMappingData().getNewItemId(729), (byte) 1, (short) 0, null);
}
PacketWrapper equipmentPacket = PacketWrapper.create(ClientboundPackets1_14.ENTITY_EQUIPMENT, null, connection);
equipmentPacket.write(Type.VAR_INT, entityId);
equipmentPacket.write(Type.VAR_INT, 4);
equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, armorItem);
equipmentPacket.scheduleSend(Protocol1_14To1_13_2.class);
}
} else if (type.is(Entity1_14Types.VILLAGER)) {
if (metadata.id() == 15) {
// plains
metadata.setTypeAndValue(Types1_14.META_TYPES.villagerDatatType, new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0));
}
} else if (type.is(Entity1_14Types.ZOMBIE_VILLAGER)) {
if (metadata.id() == 18) {
// plains
metadata.setTypeAndValue(Types1_14.META_TYPES.villagerDatatType, new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0));
}
} else if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ARROW)) {
if (metadata.id() >= 9) {
// New piercing
metadata.setId(metadata.id() + 1);
}
} else if (type.is(Entity1_14Types.FIREWORK_ROCKET)) {
if (metadata.id() == 8) {
metadata.setMetaType(Types1_14.META_TYPES.optionalVarIntType);
if (metadata.getValue().equals(0)) {
// https://bugs.mojang.com/browse/MC-111480
metadata.setValue(null);
}
}
} else if (type.isOrHasParent(Entity1_14Types.ABSTRACT_SKELETON)) {
if (metadata.id() == 14) {
tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | // New attacking
((boolean) metadata.getValue() ? 0x4 : 0)));
// "Is swinging arms"
metadatas.remove(metadata);
metadatas.add(new Metadata(13, Types1_14.META_TYPES.byteType, tracker.getInsentientData(entityId)));
}
}
if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE)) {
if (metadata.id() == 14) {
tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | // New attacking
(((Number) metadata.getValue()).byteValue() != 0 ? 0x4 : 0)));
// "Has target (aggressive state)"
metadatas.remove(metadata);
metadatas.add(new Metadata(13, Types1_14.META_TYPES.byteType, tracker.getInsentientData(entityId)));
}
}
if (type.is(Entity1_14Types.WITCH) || type.is(Entity1_14Types.RAVAGER) || type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE)) {
if (metadata.id() >= 14) {
// 19w13 added a new boolean (raid participant - is celebrating) with id 14
metadata.setId(metadata.id() + 1);
}
}
}
use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaVersion by ViaVersion.
the class InventoryPackets method registerPackets.
@Override
public void registerPackets() {
registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_13.OPEN_WINDOW, null, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Short windowId = wrapper.read(Type.UNSIGNED_BYTE);
String type = wrapper.read(Type.STRING);
JsonElement title = wrapper.read(Type.COMPONENT);
COMPONENT_REWRITER.processText(title);
Short slots = wrapper.read(Type.UNSIGNED_BYTE);
if (type.equals("EntityHorse")) {
wrapper.setId(0x1F);
int entityId = wrapper.read(Type.INT);
wrapper.write(Type.UNSIGNED_BYTE, windowId);
wrapper.write(Type.VAR_INT, slots.intValue());
wrapper.write(Type.INT, entityId);
} else {
wrapper.setId(0x2E);
wrapper.write(Type.VAR_INT, windowId.intValue());
int typeId = -1;
switch(type) {
case "minecraft:crafting_table":
typeId = 11;
break;
case "minecraft:furnace":
typeId = 13;
break;
case "minecraft:dropper":
case "minecraft:dispenser":
typeId = 6;
break;
case "minecraft:enchanting_table":
typeId = 12;
break;
case "minecraft:brewing_stand":
typeId = 10;
break;
case "minecraft:villager":
typeId = 18;
break;
case "minecraft:beacon":
typeId = 8;
break;
case "minecraft:anvil":
typeId = 7;
break;
case "minecraft:hopper":
typeId = 15;
break;
case "minecraft:shulker_box":
typeId = 19;
break;
case "minecraft:container":
case "minecraft:chest":
default:
if (slots > 0 && slots <= 54) {
typeId = slots / 9 - 1;
}
break;
}
if (typeId == -1) {
Via.getPlatform().getLogger().warning("Can't open inventory for 1.14 player! Type: " + type + " Size: " + slots);
}
wrapper.write(Type.VAR_INT, typeId);
wrapper.write(Type.COMPONENT, title);
}
}
});
}
});
registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
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")) {
wrapper.setId(0x27);
wrapper.resetReader();
// Remove channel
wrapper.read(Type.STRING);
int windowId = wrapper.read(Type.INT);
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
tracker.setLatestTradeWindowId(windowId);
wrapper.write(Type.VAR_INT, windowId);
int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
// Input Item
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
// Output Item
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
// Has second item
boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
if (secondItem) {
// Second Item
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
// Trade disabled
wrapper.passthrough(Type.BOOLEAN);
// Number of tools uses
wrapper.passthrough(Type.INT);
// Maximum number of trade uses
wrapper.passthrough(Type.INT);
wrapper.write(Type.INT, 0);
wrapper.write(Type.INT, 0);
wrapper.write(Type.FLOAT, 0f);
}
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.BOOLEAN, false);
} else if (channel.equals("minecraft:book_open") || channel.equals("book_open")) {
int hand = wrapper.read(Type.VAR_INT);
wrapper.clearPacket();
wrapper.setId(0x2D);
wrapper.write(Type.VAR_INT, hand);
}
}
});
}
});
registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_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);
int deleted = 0;
for (int i = 0; i < size; i++) {
// Recipe Identifier
String id = wrapper.read(Type.STRING);
String type = wrapper.read(Type.STRING);
if (REMOVED_RECIPE_TYPES.contains(type)) {
deleted++;
continue;
}
wrapper.write(Type.STRING, type);
wrapper.write(Type.STRING, id);
recipeRewriter.handle(wrapper, type);
}
wrapper.set(Type.VAR_INT, 0, size - deleted);
});
}
});
registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
protocol.registerServerbound(ServerboundPackets1_14.SELECT_TRADE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Selecting trade now moves the items, we need to resync the inventory
PacketWrapper resyncPacket = wrapper.create(0x08);
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
// 0 - Window ID
resyncPacket.write(Type.UNSIGNED_BYTE, ((short) tracker.getLatestTradeWindowId()));
// 1 - Slot
resyncPacket.write(Type.SHORT, ((short) -999));
// 2 - Button - End left click
resyncPacket.write(Type.BYTE, (byte) 2);
// 3 - Action number
resyncPacket.write(Type.SHORT, ((short) ThreadLocalRandom.current().nextInt()));
// 4 - Mode - Drag
resyncPacket.write(Type.VAR_INT, 5);
CompoundTag tag = new CompoundTag();
// Tags with NaN are not equal
tag.put("force_resync", new DoubleTag(Double.NaN));
// 5 - Clicked Item
resyncPacket.write(Type.FLAT_VAR_INT_ITEM, new DataItem(1, (byte) 1, (short) 0, tag));
resyncPacket.scheduleSendToServer(Protocol1_14To1_13_2.class);
}
});
}
});
registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
}
use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaVersion by ViaVersion.
the class ParticleRewriter method iconcrackHandler.
// Rewrite IconCrack items to new format :)
private static ParticleDataHandler iconcrackHandler() {
return new ParticleDataHandler() {
@Override
public Particle handler(Particle particle, Integer[] data) {
Item item;
if (data.length == 1)
item = new DataItem(data[0], (byte) 1, (short) 0, null);
else if (data.length == 2)
item = new DataItem(data[0], (byte) 1, data[1].shortValue(), null);
else
return particle;
// Transform to new Item
Via.getManager().getProtocolManager().getProtocol(Protocol1_13To1_12_2.class).getItemRewriter().handleItemToClient(item);
// Item Slot The item that will be used.
particle.getArguments().add(new Particle.ParticleData(Type.FLAT_ITEM, item));
return particle;
}
};
}
use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaVersion by ViaVersion.
the class Protocol1_13To1_12_2 method registerPackets.
@Override
protected void registerPackets() {
entityRewriter.register();
itemRewriter.register();
EntityPackets.register(this);
WorldPackets.register(this);
registerClientbound(State.LOGIN, 0x0, 0x0, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> componentRewriter.processText(wrapper.passthrough(Type.COMPONENT)));
}
});
registerClientbound(State.STATUS, 0x00, 0x00, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String response = wrapper.get(Type.STRING, 0);
try {
JsonObject json = GsonUtil.getGson().fromJson(response, JsonObject.class);
if (json.has("favicon")) {
json.addProperty("favicon", json.get("favicon").getAsString().replace("\n", ""));
}
wrapper.set(Type.STRING, 0, GsonUtil.getGson().toJson(json));
} catch (JsonParseException e) {
e.printStackTrace();
}
}
});
}
});
// New packet 0x04 - Login Plugin Message
// Statistics
registerClientbound(ClientboundPackets1_12_1.STATISTICS, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int size = wrapper.read(Type.VAR_INT);
List<StatisticData> remappedStats = new ArrayList<>();
for (int i = 0; i < size; i++) {
String name = wrapper.read(Type.STRING);
String[] split = name.split("\\.");
int categoryId = 0;
int newId = -1;
int value = wrapper.read(Type.VAR_INT);
if (split.length == 2) {
// Custom types
categoryId = 8;
Integer newIdRaw = StatisticMappings.CUSTOM_STATS.get(name);
if (newIdRaw != null) {
newId = newIdRaw;
} else {
Via.getPlatform().getLogger().warning("Could not find 1.13 -> 1.12.2 statistic mapping for " + name);
}
} else if (split.length > 2) {
String category = split[1];
// TODO convert string ids (blocks, items, entities)
switch(category) {
case "mineBlock":
categoryId = 0;
break;
case "craftItem":
categoryId = 1;
break;
case "useItem":
categoryId = 2;
break;
case "breakItem":
categoryId = 3;
break;
case "pickup":
categoryId = 4;
break;
case "drop":
categoryId = 5;
break;
case "killEntity":
categoryId = 6;
break;
case "entityKilledBy":
categoryId = 7;
break;
}
}
if (newId != -1)
remappedStats.add(new StatisticData(categoryId, newId, value));
}
// size
wrapper.write(Type.VAR_INT, remappedStats.size());
for (StatisticData stat : remappedStats) {
// category id
wrapper.write(Type.VAR_INT, stat.getCategoryId());
// statistics id
wrapper.write(Type.VAR_INT, stat.getNewId());
// value
wrapper.write(Type.VAR_INT, stat.getValue());
}
}
});
}
});
componentRewriter.registerBossBar(ClientboundPackets1_12_1.BOSSBAR);
componentRewriter.registerComponentPacket(ClientboundPackets1_12_1.CHAT_MESSAGE);
registerClientbound(ClientboundPackets1_12_1.TAB_COMPLETE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.VAR_INT, wrapper.user().get(TabCompleteTracker.class).getTransactionId());
String input = wrapper.user().get(TabCompleteTracker.class).getInput();
// Start & End
int index;
int length;
// If no input or new word (then it's the start)
if (input.endsWith(" ") || input.isEmpty()) {
index = input.length();
length = 0;
} else {
// Otherwise find the last space (+1 as we include it)
int lastSpace = input.lastIndexOf(' ') + 1;
index = lastSpace;
length = input.length() - lastSpace;
}
// Write index + length
wrapper.write(Type.VAR_INT, index);
wrapper.write(Type.VAR_INT, length);
int count = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < count; i++) {
String suggestion = wrapper.read(Type.STRING);
// If we're at the start then handle removing slash
if (suggestion.startsWith("/") && index == 0) {
suggestion = suggestion.substring(1);
}
wrapper.write(Type.STRING, suggestion);
wrapper.write(Type.BOOLEAN, false);
}
}
});
}
});
registerClientbound(ClientboundPackets1_12_1.OPEN_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
// Id
map(Type.UNSIGNED_BYTE);
// Window type
map(Type.STRING);
// Title
handler(wrapper -> componentRewriter.processText(wrapper.passthrough(Type.COMPONENT)));
}
});
registerClientbound(ClientboundPackets1_12_1.COOLDOWN, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int item = wrapper.read(Type.VAR_INT);
int ticks = wrapper.read(Type.VAR_INT);
wrapper.cancel();
if (item == 383) {
// Spawn egg
for (int i = 0; i < 44; i++) {
Integer newItem = getMappingData().getItemMappings().get(item << 16 | i);
if (newItem != null) {
PacketWrapper packet = wrapper.create(ClientboundPackets1_13.COOLDOWN);
packet.write(Type.VAR_INT, newItem);
packet.write(Type.VAR_INT, ticks);
packet.send(Protocol1_13To1_12_2.class);
} else {
break;
}
}
} else {
for (int i = 0; i < 16; i++) {
int newItem = getMappingData().getItemMappings().get(item << 4 | i);
if (newItem != -1) {
PacketWrapper packet = wrapper.create(ClientboundPackets1_13.COOLDOWN);
packet.write(Type.VAR_INT, newItem);
packet.write(Type.VAR_INT, ticks);
packet.send(Protocol1_13To1_12_2.class);
} else {
break;
}
}
}
}
});
}
});
componentRewriter.registerComponentPacket(ClientboundPackets1_12_1.DISCONNECT);
registerClientbound(ClientboundPackets1_12_1.EFFECT, new PacketRemapper() {
@Override
public void registerMap() {
// Effect Id
map(Type.INT);
// Location
map(Type.POSITION);
// Data
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.INT, 0);
int data = wrapper.get(Type.INT, 1);
if (id == 1010) {
// Play record
wrapper.set(Type.INT, 1, getMappingData().getItemMappings().get(data << 4));
} else if (id == 2001) {
// Block break + block break sound
int blockId = data & 0xFFF;
int blockData = data >> 12;
wrapper.set(Type.INT, 1, WorldPackets.toNewId(blockId << 4 | blockData));
}
}
});
}
});
registerClientbound(ClientboundPackets1_12_1.JOIN_GAME, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.INT);
// 1 - Gamemode
map(Type.UNSIGNED_BYTE);
// 2 - Dimension
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Store the player
int entityId = wrapper.get(Type.INT, 0);
wrapper.user().getEntityTracker(Protocol1_13To1_12_2.class).addEntity(entityId, Entity1_13Types.EntityType.PLAYER);
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
}
});
handler(SEND_DECLARE_COMMANDS_AND_TAGS);
}
});
registerClientbound(ClientboundPackets1_12_1.CRAFT_RECIPE_RESPONSE, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.BYTE);
handler(wrapper -> wrapper.write(Type.STRING, "viaversion:legacy/" + wrapper.read(Type.VAR_INT)));
}
});
componentRewriter.registerCombatEvent(ClientboundPackets1_12_1.COMBAT_EVENT);
registerClientbound(ClientboundPackets1_12_1.MAP_DATA, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Map id
map(Type.VAR_INT);
// 1 - Scale
map(Type.BYTE);
// 2 - Tracking Position
map(Type.BOOLEAN);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int iconCount = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < iconCount; i++) {
byte directionAndType = wrapper.read(Type.BYTE);
int type = (directionAndType & 0xF0) >> 4;
wrapper.write(Type.VAR_INT, type);
// Icon X
wrapper.passthrough(Type.BYTE);
// Icon Z
wrapper.passthrough(Type.BYTE);
byte direction = (byte) (directionAndType & 0x0F);
wrapper.write(Type.BYTE, direction);
// Display Name
wrapper.write(Type.OPTIONAL_COMPONENT, null);
}
}
});
}
});
registerClientbound(ClientboundPackets1_12_1.UNLOCK_RECIPES, new PacketRemapper() {
@Override
public void registerMap() {
// action
map(Type.VAR_INT);
// crafting book open
map(Type.BOOLEAN);
// crafting filter active
map(Type.BOOLEAN);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// smelting book open
wrapper.write(Type.BOOLEAN, false);
// smelting filter active
wrapper.write(Type.BOOLEAN, false);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int action = wrapper.get(Type.VAR_INT, 0);
for (int i = 0; i < (action == 0 ? 2 : 1); i++) {
int[] ids = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
String[] stringIds = new String[ids.length];
for (int j = 0; j < ids.length; j++) {
stringIds[j] = "viaversion:legacy/" + ids[j];
}
wrapper.write(Type.STRING_ARRAY, stringIds);
}
if (action == 0) {
wrapper.create(ClientboundPackets1_13.DECLARE_RECIPES, new // Declare recipes
PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.VAR_INT, RecipeData.recipes.size());
for (Map.Entry<String, RecipeData.Recipe> entry : RecipeData.recipes.entrySet()) {
// Id
wrapper.write(Type.STRING, entry.getKey());
wrapper.write(Type.STRING, entry.getValue().getType());
switch(entry.getValue().getType()) {
case "crafting_shapeless":
{
wrapper.write(Type.STRING, entry.getValue().getGroup());
wrapper.write(Type.VAR_INT, entry.getValue().getIngredients().length);
for (Item[] ingredient : entry.getValue().getIngredients()) {
// Clone because array and item is mutable
Item[] clone = ingredient.clone();
for (int i = 0; i < clone.length; i++) {
if (clone[i] == null)
continue;
clone[i] = new DataItem(clone[i]);
}
wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
}
wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult()));
break;
}
case "crafting_shaped":
{
wrapper.write(Type.VAR_INT, entry.getValue().getWidth());
wrapper.write(Type.VAR_INT, entry.getValue().getHeight());
wrapper.write(Type.STRING, entry.getValue().getGroup());
for (Item[] ingredient : entry.getValue().getIngredients()) {
// Clone because array and item is mutable
Item[] clone = ingredient.clone();
for (int i = 0; i < clone.length; i++) {
if (clone[i] == null)
continue;
clone[i] = new DataItem(clone[i]);
}
wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
}
wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult()));
break;
}
case "smelting":
{
wrapper.write(Type.STRING, entry.getValue().getGroup());
// Clone because array and item is mutable
Item[] clone = entry.getValue().getIngredient().clone();
for (int i = 0; i < clone.length; i++) {
if (clone[i] == null)
continue;
clone[i] = new DataItem(clone[i]);
}
wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult()));
wrapper.write(Type.FLOAT, entry.getValue().getExperience());
wrapper.write(Type.VAR_INT, entry.getValue().getCookingTime());
break;
}
}
}
}
}).send(Protocol1_13To1_12_2.class);
}
}
});
}
});
registerClientbound(ClientboundPackets1_12_1.RESPAWN, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Dimension ID
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId);
if (Via.getConfig().isServersideBlockConnections()) {
ConnectionData.clearBlockStorage(wrapper.user());
}
}
});
handler(SEND_DECLARE_COMMANDS_AND_TAGS);
}
});
registerClientbound(ClientboundPackets1_12_1.SCOREBOARD_OBJECTIVE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Objective name
map(Type.STRING);
// 1 - Mode
map(Type.BYTE);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
byte mode = wrapper.get(Type.BYTE, 0);
// On create or update
if (mode == 0 || mode == 2) {
// Value
String value = wrapper.read(Type.STRING);
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(value));
String type = wrapper.read(Type.STRING);
// integer or hearts
wrapper.write(Type.VAR_INT, type.equals("integer") ? 0 : 1);
}
}
});
}
});
registerClientbound(ClientboundPackets1_12_1.TEAMS, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Team Name
map(Type.STRING);
// 1 - Mode
map(Type.BYTE);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
byte action = wrapper.get(Type.BYTE, 0);
if (action == 0 || action == 2) {
// Display Name
String displayName = wrapper.read(Type.STRING);
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(displayName));
// Prefix moved
String prefix = wrapper.read(Type.STRING);
// Suffix moved
String suffix = wrapper.read(Type.STRING);
// Flags
wrapper.passthrough(Type.BYTE);
// Name Tag Visibility
wrapper.passthrough(Type.STRING);
// Collision rule
wrapper.passthrough(Type.STRING);
// Handle new colors
int colour = wrapper.read(Type.BYTE).intValue();
if (colour == -1) {
// -1 changed to 21
colour = 21;
}
if (Via.getConfig().is1_13TeamColourFix()) {
char lastColorChar = getLastColorChar(prefix);
colour = ChatColorUtil.getColorOrdinal(lastColorChar);
suffix = ChatColorUtil.COLOR_CHAR + Character.toString(lastColorChar) + suffix;
}
wrapper.write(Type.VAR_INT, colour);
// Prefix
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(prefix));
// Suffix
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(suffix));
}
if (action == 0 || action == 3 || action == 4) {
// Entities
String[] names = wrapper.read(Type.STRING_ARRAY);
for (int i = 0; i < names.length; i++) {
names[i] = rewriteTeamMemberName(names[i]);
}
wrapper.write(Type.STRING_ARRAY, names);
}
}
});
}
});
registerClientbound(ClientboundPackets1_12_1.UPDATE_SCORE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Display Name
String displayName = wrapper.read(Type.STRING);
displayName = rewriteTeamMemberName(displayName);
wrapper.write(Type.STRING, displayName);
byte action = wrapper.read(Type.BYTE);
wrapper.write(Type.BYTE, action);
// Objective Name
wrapper.passthrough(Type.STRING);
if (action != 1) {
// Value
wrapper.passthrough(Type.VAR_INT);
}
}
});
}
});
componentRewriter.registerTitle(ClientboundPackets1_12_1.TITLE);
// New 0x4C - Stop Sound
new SoundRewriter(this).registerSound(ClientboundPackets1_12_1.SOUND);
registerClientbound(ClientboundPackets1_12_1.TAB_LIST, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
componentRewriter.processText(wrapper.passthrough(Type.COMPONENT));
componentRewriter.processText(wrapper.passthrough(Type.COMPONENT));
}
});
}
});
registerClientbound(ClientboundPackets1_12_1.ADVANCEMENTS, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Reset/clear
wrapper.passthrough(Type.BOOLEAN);
// Mapping size
int size = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < size; i++) {
// Identifier
wrapper.passthrough(Type.STRING);
// Parent
if (wrapper.passthrough(Type.BOOLEAN))
wrapper.passthrough(Type.STRING);
// Display data
if (wrapper.passthrough(Type.BOOLEAN)) {
// Title
componentRewriter.processText(wrapper.passthrough(Type.COMPONENT));
// Description
componentRewriter.processText(wrapper.passthrough(Type.COMPONENT));
Item icon = wrapper.read(Type.ITEM);
itemRewriter.handleItemToClient(icon);
// Translate item to flat item
wrapper.write(Type.FLAT_ITEM, icon);
// Frame type
wrapper.passthrough(Type.VAR_INT);
// Flags
int flags = wrapper.passthrough(Type.INT);
if ((flags & 1) != 0) {
// Background texture
wrapper.passthrough(Type.STRING);
}
// X
wrapper.passthrough(Type.FLOAT);
// Y
wrapper.passthrough(Type.FLOAT);
}
// Criteria
wrapper.passthrough(Type.STRING_ARRAY);
int arrayLength = wrapper.passthrough(Type.VAR_INT);
for (int array = 0; array < arrayLength; array++) {
// String array
wrapper.passthrough(Type.STRING_ARRAY);
}
}
}
});
}
});
// Incoming packets
// New packet 0x02 - Login Plugin Message
cancelServerbound(State.LOGIN, 0x02);
// New 0x01 - Query Block NBT
cancelServerbound(ServerboundPackets1_13.QUERY_BLOCK_NBT);
// Tab-Complete
registerServerbound(ServerboundPackets1_13.TAB_COMPLETE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Disable auto-complete if configured
if (Via.getConfig().isDisable1_13AutoComplete()) {
wrapper.cancel();
}
int tid = wrapper.read(Type.VAR_INT);
// Save transaction id
wrapper.user().get(TabCompleteTracker.class).setTransactionId(tid);
}
});
// Prepend /
map(Type.STRING, new ValueTransformer<String, String>(Type.STRING) {
@Override
public String transform(PacketWrapper wrapper, String inputValue) {
wrapper.user().get(TabCompleteTracker.class).setInput(inputValue);
return "/" + inputValue;
}
});
// Fake the end of the packet
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.BOOLEAN, false);
wrapper.write(Type.OPTIONAL_POSITION, null);
if (!wrapper.isCancelled() && Via.getConfig().get1_13TabCompleteDelay() > 0) {
TabCompleteTracker tracker = wrapper.user().get(TabCompleteTracker.class);
wrapper.cancel();
tracker.setTimeToSend(System.currentTimeMillis() + Via.getConfig().get1_13TabCompleteDelay() * 50L);
tracker.setLastTabComplete(wrapper.get(Type.STRING, 0));
}
}
});
}
});
// New 0x0A - Edit book -> Plugin Message
registerServerbound(ServerboundPackets1_13.EDIT_BOOK, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.read(Type.FLAT_ITEM);
boolean isSigning = wrapper.read(Type.BOOLEAN);
itemRewriter.handleItemToServer(item);
// Channel
wrapper.write(Type.STRING, isSigning ? "MC|BSign" : "MC|BEdit");
wrapper.write(Type.ITEM, item);
}
});
}
});
// New 0x0C - Query Entity NBT
cancelServerbound(ServerboundPackets1_13.ENTITY_NBT_REQUEST);
// New 0x15 - Pick Item -> Plugin Message
registerServerbound(ServerboundPackets1_13.PICK_ITEM, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Channel
wrapper.write(Type.STRING, "MC|PickItem");
}
});
}
});
registerServerbound(ServerboundPackets1_13.CRAFT_RECIPE_REQUEST, new PacketRemapper() {
@Override
public void registerMap() {
// Window id
map(Type.BYTE);
handler(wrapper -> {
String s = wrapper.read(Type.STRING);
Integer id;
if (s.length() < 19 || (id = Ints.tryParse(s.substring(18))) == null) {
wrapper.cancel();
return;
}
wrapper.write(Type.VAR_INT, id);
});
}
});
registerServerbound(ServerboundPackets1_13.RECIPE_BOOK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Type
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int type = wrapper.get(Type.VAR_INT, 0);
if (type == 0) {
String s = wrapper.read(Type.STRING);
Integer id;
// Custom recipes
if (s.length() < 19 || (id = Ints.tryParse(s.substring(18))) == null) {
wrapper.cancel();
return;
}
wrapper.write(Type.INT, id);
}
if (type == 1) {
// Crafting Recipe Book Open
wrapper.passthrough(Type.BOOLEAN);
// Crafting Recipe Filter Active
wrapper.passthrough(Type.BOOLEAN);
// Smelting Recipe Book Open | IGNORE NEW 1.13 FIELD
wrapper.read(Type.BOOLEAN);
// Smelting Recipe Filter Active | IGNORE NEW 1.13 FIELD
wrapper.read(Type.BOOLEAN);
}
}
});
}
});
// New 0x1C - Name Item -> Plugin Message
registerServerbound(ServerboundPackets1_13.RENAME_ITEM, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
// Channel
wrapper.write(Type.STRING, "MC|ItemName");
});
}
});
// New 0x1F - Select Trade -> Plugin Message
registerServerbound(ServerboundPackets1_13.SELECT_TRADE, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
// Channel
wrapper.write(Type.STRING, "MC|TrSel");
});
// Slot
map(Type.VAR_INT, Type.INT);
}
});
// New 0x20 - Set Beacon Effect -> Plugin Message
registerServerbound(ServerboundPackets1_13.SET_BEACON_EFFECT, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
// Channel
wrapper.write(Type.STRING, "MC|Beacon");
});
// Primary Effect
map(Type.VAR_INT, Type.INT);
// Secondary Effect
map(Type.VAR_INT, Type.INT);
}
});
// New 0x22 - Update Command Block -> Plugin Message
registerServerbound(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> wrapper.write(Type.STRING, "MC|AutoCmd"));
handler(POS_TO_3_INT);
// Command
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int mode = wrapper.read(Type.VAR_INT);
byte flags = wrapper.read(Type.BYTE);
String stringMode = mode == 0 ? "SEQUENCE" : mode == 1 ? "AUTO" : "REDSTONE";
// Track output
wrapper.write(Type.BOOLEAN, (flags & 0x1) != 0);
wrapper.write(Type.STRING, stringMode);
// Is conditional
wrapper.write(Type.BOOLEAN, (flags & 0x2) != 0);
// Automatic
wrapper.write(Type.BOOLEAN, (flags & 0x4) != 0);
}
});
}
});
// New 0x23 - Update Command Block Minecart -> Plugin Message
registerServerbound(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK_MINECART, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.STRING, "MC|AdvCmd");
// Type 1 for Entity
wrapper.write(Type.BYTE, (byte) 1);
}
});
// Entity Id
map(Type.VAR_INT, Type.INT);
}
});
// 0x1B -> 0x24 in InventoryPackets
// New 0x25 - Update Structure Block -> Message Channel
registerServerbound(ServerboundPackets1_13.UPDATE_STRUCTURE_BLOCK, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
// Channel
wrapper.write(Type.STRING, "MC|Struct");
});
handler(POS_TO_3_INT);
map(Type.VAR_INT, new // Action
ValueTransformer<Integer, Byte>(// Action
Type.BYTE) {
@Override
public Byte transform(PacketWrapper wrapper, Integer action) throws Exception {
return (byte) (action + 1);
}
});
// Action
map(Type.VAR_INT, new ValueTransformer<Integer, String>(Type.STRING) {
@Override
public String transform(PacketWrapper wrapper, Integer mode) throws Exception {
return mode == 0 ? "SAVE" : mode == 1 ? "LOAD" : mode == 2 ? "CORNER" : "DATA";
}
});
// Name
map(Type.STRING);
// Offset X
map(Type.BYTE, Type.INT);
// Offset Y
map(Type.BYTE, Type.INT);
// Offset Z
map(Type.BYTE, Type.INT);
// Size X
map(Type.BYTE, Type.INT);
// Size Y
map(Type.BYTE, Type.INT);
// Size Z
map(Type.BYTE, Type.INT);
map(Type.VAR_INT, new // Mirror
ValueTransformer<Integer, String>(// Mirror
Type.STRING) {
@Override
public String transform(PacketWrapper wrapper, Integer mirror) throws Exception {
return mirror == 0 ? "NONE" : mirror == 1 ? "LEFT_RIGHT" : "FRONT_BACK";
}
});
map(Type.VAR_INT, new // Rotation
ValueTransformer<Integer, String>(// Rotation
Type.STRING) {
@Override
public String transform(PacketWrapper wrapper, Integer rotation) throws Exception {
return rotation == 0 ? "NONE" : rotation == 1 ? "CLOCKWISE_90" : rotation == 2 ? "CLOCKWISE_180" : "COUNTERCLOCKWISE_90";
}
});
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
float integrity = wrapper.read(Type.FLOAT);
long seed = wrapper.read(Type.VAR_LONG);
byte flags = wrapper.read(Type.BYTE);
// Ignore Entities
wrapper.write(Type.BOOLEAN, (flags & 0x1) != 0);
// Show air
wrapper.write(Type.BOOLEAN, (flags & 0x2) != 0);
// Show bounding box
wrapper.write(Type.BOOLEAN, (flags & 0x4) != 0);
wrapper.write(Type.FLOAT, integrity);
wrapper.write(Type.VAR_LONG, seed);
}
});
}
});
}
Aggregations