use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaVersion by ViaVersion.
the class MetadataRewriter1_11To1_10 method handleMetadata.
@Override
protected void handleMetadata(int entityId, com.viaversion.viaversion.api.minecraft.entities.EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) {
if (metadata.getValue() instanceof DataItem) {
// Apply rewrite
EntityIdRewriter.toClientItem((Item) metadata.getValue());
}
if (type == null)
return;
if (type.is(EntityType.ELDER_GUARDIAN) || type.is(EntityType.GUARDIAN)) {
// Guardians
int oldid = metadata.id();
if (oldid == 12) {
boolean val = (((byte) metadata.getValue()) & 0x02) == 0x02;
metadata.setTypeAndValue(MetaType1_9.Boolean, val);
}
}
if (type.isOrHasParent(EntityType.ABSTRACT_SKELETON)) {
// Skeletons
int oldid = metadata.id();
if (oldid == 12) {
metadatas.remove(metadata);
}
if (oldid == 13) {
metadata.setId(12);
}
}
if (type.isOrHasParent(EntityType.ZOMBIE)) {
// Zombie | Zombie Villager | Husk
if (type.is(EntityType.ZOMBIE, EntityType.HUSK) && metadata.id() == 14) {
metadatas.remove(metadata);
} else {
if (metadata.id() == 15) {
metadata.setId(14);
} else {
if (metadata.id() == 14) {
metadata.setId(15);
}
}
}
}
if (type.isOrHasParent(EntityType.ABSTRACT_HORSE)) {
// Horses
// Remap metadata id
int oldid = metadata.id();
if (oldid == 14) {
// Type
metadatas.remove(metadata);
}
if (oldid == 16) {
// Owner
metadata.setId(14);
}
if (oldid == 17) {
// Armor
metadata.setId(16);
}
// Process per type
if (type.is(EntityType.HORSE)) {
// Normal Horse
} else {
// Remove 15, 16
if (metadata.id() == 15 || metadata.id() == 16) {
metadatas.remove(metadata);
}
}
if (type.is(EntityType.DONKEY, EntityType.MULE)) {
// Chested Horse
if (metadata.id() == 13) {
if ((((byte) metadata.getValue()) & 0x08) == 0x08) {
metadatas.add(new Metadata(15, MetaType1_9.Boolean, true));
} else {
metadatas.add(new Metadata(15, MetaType1_9.Boolean, false));
}
}
}
}
if (type.is(EntityType.ARMOR_STAND) && Via.getConfig().isHologramPatch()) {
Metadata flags = metaByIndex(11, metadatas);
Metadata customName = metaByIndex(2, metadatas);
Metadata customNameVisible = metaByIndex(3, metadatas);
if (metadata.id() == 0 && flags != null && customName != null && customNameVisible != null) {
byte data = (byte) metadata.getValue();
// Check invisible | Check small | Check if custom name is empty | Check if custom name visible is true
if ((data & 0x20) == 0x20 && ((byte) flags.getValue() & 0x01) == 0x01 && !((String) customName.getValue()).isEmpty() && (boolean) customNameVisible.getValue()) {
EntityTracker1_11 tracker = tracker(connection);
if (tracker.addHologram(entityId)) {
try {
// Send movement
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9_3.ENTITY_POSITION, null, connection);
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.send(Protocol1_11To1_10.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaVersion by ViaVersion.
the class Protocol1_17_1To1_17 method registerPackets.
@Override
protected void registerPackets() {
registerClientbound(ClientboundPackets1_17.REMOVE_ENTITY, ClientboundPackets1_17_1.REMOVE_ENTITIES, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
// Aaaaand back to an array again!
int entityId = wrapper.read(Type.VAR_INT);
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { entityId });
});
}
});
registerClientbound(ClientboundPackets1_17.SET_SLOT, new PacketRemapper() {
@Override
public void registerMap() {
// Container id
map(Type.UNSIGNED_BYTE);
// Add arbitrary state id
create(Type.VAR_INT, 0);
}
});
registerClientbound(ClientboundPackets1_17.WINDOW_ITEMS, new PacketRemapper() {
@Override
public void registerMap() {
// Container id
map(Type.UNSIGNED_BYTE);
// Add arbitrary state id
create(Type.VAR_INT, 0);
handler(wrapper -> {
// Length encoded as var int now
wrapper.write(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY));
// Carried item - should work like this
wrapper.write(Type.FLAT_VAR_INT_ITEM, null);
});
}
});
registerServerbound(ServerboundPackets1_17.CLICK_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
// Container id
map(Type.UNSIGNED_BYTE);
// Remove state id
read(Type.VAR_INT);
}
});
registerServerbound(ServerboundPackets1_17.EDIT_BOOK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
CompoundTag tag = new CompoundTag();
// Magic value for writable books
Item item = new DataItem(942, (byte) 1, (short) 0, tag);
// Write the item, edit the tag down the line
wrapper.write(Type.FLAT_VAR_INT_ITEM, item);
int slot = wrapper.read(Type.VAR_INT);
// Save pages to tag
int pages = wrapper.read(Type.VAR_INT);
ListTag pagesTag = new ListTag(StringTag.class);
for (int i = 0; i < pages; i++) {
String page = wrapper.read(PAGE_STRING_TYPE);
pagesTag.add(new StringTag(page));
}
// Legacy servers don't like an empty pages list
if (pagesTag.size() == 0) {
pagesTag.add(new StringTag(""));
}
tag.put("pages", pagesTag);
if (wrapper.read(Type.BOOLEAN)) {
// Save the title to tag
String title = wrapper.read(TITLE_STRING_TYPE);
tag.put("title", new StringTag(title));
// Even if unused, legacy servers check for the author tag
tag.put("author", new StringTag(wrapper.user().getProtocolInfo().getUsername()));
// Write signing
wrapper.write(Type.BOOLEAN, true);
} else {
wrapper.write(Type.BOOLEAN, false);
}
// Write the slot
wrapper.write(Type.VAR_INT, slot);
});
}
});
}
use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaFabric by ViaVersion.
the class VRHandItemProvider method fromNative.
private Item fromNative(ItemStack original) {
Identifier iid = Registry.ITEM.getId(original.getItem());
int id = RemappingUtil.swordId(iid.toString());
return new DataItem(id, (byte) original.getCount(), (short) original.getDamage(), null);
}
use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaFabric by ViaVersion.
the class VRHandItemProvider method fromNative.
private Item fromNative(ItemStack original) {
Identifier iid = Registry.ITEM.getId(original.getItem());
int id = RemappingUtil.swordId(iid.toString());
return new DataItem(id, (byte) original.getCount(), (short) original.getDamage(), null);
}
use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaVersion by ViaVersion.
the class ItemTypeTest method testNormalItemRead.
@Test
public void testNormalItemRead() throws Exception {
// Test item read
Assertions.assertEquals(new DataItem((int) Short.MAX_VALUE, (byte) -128, (short) 257, null), Type.ITEM.read(Unpooled.wrappedBuffer(new byte[] { 127, -1, -128, 1, 1, 0 })));
Assertions.assertEquals(new DataItem(420, (byte) 53, (short) 0, null), Type.FLAT_ITEM.read(Unpooled.wrappedBuffer(new byte[] { 1, (byte) 164, 53, 0 })));
Assertions.assertEquals(new DataItem(268435456, (byte) 127, (short) 0, null), Type.FLAT_VAR_INT_ITEM.read(Unpooled.wrappedBuffer(new byte[] { 1, -128, -128, -128, -128, 1, 127, 0 })));
}
Aggregations