use of com.viaversion.viaversion.api.minecraft.item.Item 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.Item 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.Item in project ViaVersion by ViaVersion.
the class PlayerPackets method register.
public static void register(Protocol protocol) {
protocol.registerClientbound(ClientboundPackets1_13.OPEN_SIGN_EDITOR, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION, Type.POSITION1_14);
}
});
protocol.registerServerbound(ServerboundPackets1_14.QUERY_BLOCK_NBT, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.POSITION1_14, Type.POSITION);
}
});
protocol.registerServerbound(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
protocol.getItemRewriter().handleItemToServer(item);
if (item == null)
return;
CompoundTag tag = item.tag();
if (tag == null)
return;
Tag pages = tag.get("pages");
// Fix for https://github.com/ViaVersion/ViaVersion/issues/2660
if (pages == null) {
tag.put("pages", new ListTag(Collections.singletonList(new StringTag())));
}
// Client limit when editing a book was upped from 50 to 100 in 1.14, but some anti-exploit plugins ban with a size higher than the old client limit
if (Via.getConfig().isTruncate1_14Books() && pages instanceof ListTag) {
ListTag listTag = (ListTag) pages;
if (listTag.size() > 50) {
listTag.setValue(listTag.getValue().subList(0, 50));
}
}
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_14.PLAYER_DIGGING, new PacketRemapper() {
@Override
public void registerMap() {
// Action
map(Type.VAR_INT);
// Position
map(Type.POSITION1_14, Type.POSITION);
}
});
protocol.registerServerbound(ServerboundPackets1_14.RECIPE_BOOK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
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) {
wrapper.passthrough(Type.STRING);
} else if (type == 1) {
// Crafting Recipe Book Open
wrapper.passthrough(Type.BOOLEAN);
// Crafting Recipe Filter Active
wrapper.passthrough(Type.BOOLEAN);
// Smelting Recipe Book Open
wrapper.passthrough(Type.BOOLEAN);
// Smelting Recipe Filter Active
wrapper.passthrough(Type.BOOLEAN);
// Unknown new booleans
wrapper.read(Type.BOOLEAN);
wrapper.read(Type.BOOLEAN);
wrapper.read(Type.BOOLEAN);
wrapper.read(Type.BOOLEAN);
}
}
});
}
});
protocol.registerServerbound(ServerboundPackets1_14.UPDATE_COMMAND_BLOCK, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14, Type.POSITION);
}
});
protocol.registerServerbound(ServerboundPackets1_14.UPDATE_STRUCTURE_BLOCK, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14, Type.POSITION);
}
});
protocol.registerServerbound(ServerboundPackets1_14.UPDATE_SIGN, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14, Type.POSITION);
}
});
protocol.registerServerbound(ServerboundPackets1_14.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int hand = wrapper.read(Type.VAR_INT);
Position position = wrapper.read(Type.POSITION1_14);
int face = wrapper.read(Type.VAR_INT);
float x = wrapper.read(Type.FLOAT);
float y = wrapper.read(Type.FLOAT);
float z = wrapper.read(Type.FLOAT);
// new unknown boolean
wrapper.read(Type.BOOLEAN);
wrapper.write(Type.POSITION, position);
wrapper.write(Type.VAR_INT, face);
wrapper.write(Type.VAR_INT, hand);
wrapper.write(Type.FLOAT, x);
wrapper.write(Type.FLOAT, y);
wrapper.write(Type.FLOAT, z);
}
});
}
});
}
use of com.viaversion.viaversion.api.minecraft.item.Item 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.Item in project ViaVersion by ViaVersion.
the class EntityRewriter method rewriteParticle.
protected void rewriteParticle(Particle particle) {
ParticleMappings mappings = protocol.getMappingData().getParticleMappings();
int id = particle.getId();
if (mappings.isBlockParticle(id)) {
Particle.ParticleData data = particle.getArguments().get(0);
data.setValue(protocol.getMappingData().getNewBlockStateId(data.get()));
} else if (mappings.isItemParticle(id) && protocol.getItemRewriter() != null) {
Particle.ParticleData data = particle.getArguments().get(0);
Item item = data.get();
protocol.getItemRewriter().handleItemToClient(item);
}
particle.setId(protocol.getMappingData().getNewParticleId(id));
}
Aggregations