use of com.viaversion.viaversion.api.minecraft.metadata.MetaType in project ViaVersion by ViaVersion.
the class ModernMetaType method read.
@Override
public Metadata read(final ByteBuf buffer) throws Exception {
final short index = buffer.readUnsignedByte();
// End of metadata
if (index == END)
return null;
final MetaType type = this.getType(Type.VAR_INT.readPrimitive(buffer));
return new Metadata(index, type, type.type().read(buffer));
}
use of com.viaversion.viaversion.api.minecraft.metadata.MetaType in project ViaVersion by ViaVersion.
the class ModernMetaType method write.
@Override
public void write(final ByteBuf buffer, final Metadata object) throws Exception {
if (object == null) {
buffer.writeByte(END);
} else {
buffer.writeByte(object.id());
final MetaType type = object.metaType();
Type.VAR_INT.writePrimitive(buffer, type.typeId());
type.type().write(buffer, object.getValue());
}
}
use of com.viaversion.viaversion.api.minecraft.metadata.MetaType in project ViaVersion by ViaVersion.
the class AbstractMetaTypes method add.
protected MetaType add(final int typeId, final Type<?> type) {
final MetaType metaType = MetaType.create(typeId, type);
values[typeId] = metaType;
return metaType;
}
use of com.viaversion.viaversion.api.minecraft.metadata.MetaType in project ViaVersion by ViaVersion.
the class MetadataRewriter1_9To1_8 method handleMetadata.
@Override
protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
MetaIndex metaIndex = MetaIndex.searchIndex(type, metadata.id());
if (metaIndex == null) {
throw new Exception("Could not find valid metadata");
}
if (metaIndex.getNewType() == null) {
metadatas.remove(metadata);
return;
}
metadata.setId(metaIndex.getNewIndex());
metadata.setMetaTypeUnsafe(metaIndex.getNewType());
Object value = metadata.getValue();
switch(metaIndex.getNewType()) {
case Byte:
// convert from int, byte
if (metaIndex.getOldType() == MetaType1_8.Byte) {
metadata.setValue(value);
}
if (metaIndex.getOldType() == MetaType1_8.Int) {
metadata.setValue(((Integer) value).byteValue());
}
// After writing the last one
if (metaIndex == MetaIndex.ENTITY_STATUS && type == Entity1_10Types.EntityType.PLAYER) {
Byte val = 0;
if ((((Byte) value) & 0x10) == 0x10) {
// Player eating/aiming/drinking
val = 1;
}
int newIndex = MetaIndex.PLAYER_HAND.getNewIndex();
MetaType metaType = MetaIndex.PLAYER_HAND.getNewType();
metadatas.add(new Metadata(newIndex, metaType, val));
}
break;
case OptUUID:
String owner = (String) value;
UUID toWrite = null;
if (!owner.isEmpty()) {
try {
toWrite = UUID.fromString(owner);
} catch (Exception ignored) {
}
}
metadata.setValue(toWrite);
break;
case VarInt:
// convert from int, short, byte
if (metaIndex.getOldType() == MetaType1_8.Byte) {
metadata.setValue(((Byte) value).intValue());
}
if (metaIndex.getOldType() == MetaType1_8.Short) {
metadata.setValue(((Short) value).intValue());
}
if (metaIndex.getOldType() == MetaType1_8.Int) {
metadata.setValue(value);
}
break;
case Float:
metadata.setValue(value);
break;
case String:
metadata.setValue(value);
break;
case Boolean:
if (metaIndex == MetaIndex.AGEABLE_AGE)
metadata.setValue((Byte) value < 0);
else
metadata.setValue((Byte) value != 0);
break;
case Slot:
metadata.setValue(value);
ItemRewriter.toClient((Item) metadata.getValue());
break;
case Position:
Vector vector = (Vector) value;
metadata.setValue(vector);
break;
case Vector3F:
EulerAngle angle = (EulerAngle) value;
metadata.setValue(angle);
break;
case Chat:
value = Protocol1_9To1_8.fixJson(value.toString());
metadata.setValue(value);
break;
case BlockID:
// Convert from int, short, byte
metadata.setValue(((Number) value).intValue());
break;
default:
metadatas.remove(metadata);
throw new Exception("Unhandled MetaDataType: " + metaIndex.getNewType());
}
}
use of com.viaversion.viaversion.api.minecraft.metadata.MetaType in project ViaBackwards by ViaVersion.
the class EntityPackets1_14 method registerRewrites.
@Override
protected void registerRewrites() {
mapTypes(Entity1_14Types.values(), Entity1_13Types.EntityType.class);
mapEntityTypeWithData(Entity1_14Types.CAT, Entity1_14Types.OCELOT).jsonName();
mapEntityTypeWithData(Entity1_14Types.TRADER_LLAMA, Entity1_14Types.LLAMA).jsonName();
mapEntityTypeWithData(Entity1_14Types.FOX, Entity1_14Types.WOLF).jsonName();
mapEntityTypeWithData(Entity1_14Types.PANDA, Entity1_14Types.POLAR_BEAR).jsonName();
mapEntityTypeWithData(Entity1_14Types.PILLAGER, Entity1_14Types.VILLAGER).jsonName();
mapEntityTypeWithData(Entity1_14Types.WANDERING_TRADER, Entity1_14Types.VILLAGER).jsonName();
mapEntityTypeWithData(Entity1_14Types.RAVAGER, Entity1_14Types.COW).jsonName();
filter().handler((event, meta) -> {
int typeId = meta.metaType().typeId();
if (typeId <= 15) {
meta.setMetaType(Types1_13_2.META_TYPES.byId(typeId));
}
MetaType type = meta.metaType();
if (type == Types1_13_2.META_TYPES.itemType) {
Item item = (Item) meta.getValue();
meta.setValue(protocol.getItemRewriter().handleItemToClient(item));
} else if (type == Types1_13_2.META_TYPES.blockStateType) {
int blockstate = meta.value();
meta.setValue(protocol.getMappingData().getNewBlockStateId(blockstate));
}
});
filter().type(Entity1_14Types.PILLAGER).cancel(15);
filter().type(Entity1_14Types.FOX).cancel(15);
filter().type(Entity1_14Types.FOX).cancel(16);
filter().type(Entity1_14Types.FOX).cancel(17);
filter().type(Entity1_14Types.FOX).cancel(18);
filter().type(Entity1_14Types.PANDA).cancel(15);
filter().type(Entity1_14Types.PANDA).cancel(16);
filter().type(Entity1_14Types.PANDA).cancel(17);
filter().type(Entity1_14Types.PANDA).cancel(18);
filter().type(Entity1_14Types.PANDA).cancel(19);
filter().type(Entity1_14Types.PANDA).cancel(20);
filter().type(Entity1_14Types.CAT).cancel(18);
filter().type(Entity1_14Types.CAT).cancel(19);
filter().type(Entity1_14Types.CAT).cancel(20);
filter().handler((event, meta) -> {
EntityType type = event.entityType();
if (type == null)
return;
if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE) || type == Entity1_14Types.RAVAGER || type == Entity1_14Types.WITCH) {
int index = event.index();
if (index == 14) {
event.cancel();
} else if (index > 14) {
event.setIndex(index - 1);
}
}
});
filter().type(Entity1_14Types.AREA_EFFECT_CLOUD).index(10).handler((event, meta) -> {
rewriteParticle((Particle) meta.getValue());
});
filter().type(Entity1_14Types.FIREWORK_ROCKET).index(8).handler((event, meta) -> {
meta.setMetaType(Types1_13_2.META_TYPES.varIntType);
Integer value = (Integer) meta.getValue();
if (value == null) {
meta.setValue(0);
}
});
filter().filterFamily(Entity1_14Types.ABSTRACT_ARROW).removeIndex(9);
// Head shake timer
filter().type(Entity1_14Types.VILLAGER).cancel(15);
MetaHandler villagerDataHandler = (event, meta) -> {
VillagerData villagerData = (VillagerData) meta.getValue();
meta.setTypeAndValue(Types1_13_2.META_TYPES.varIntType, villagerDataToProfession(villagerData));
if (meta.id() == 16) {
// decreased by 2 again in one of the following handlers
event.setIndex(15);
}
};
filter().type(Entity1_14Types.ZOMBIE_VILLAGER).index(18).handler(villagerDataHandler);
filter().type(Entity1_14Types.VILLAGER).index(16).handler(villagerDataHandler);
// Holding arms up - from bitfield into own boolean
filter().filterFamily(Entity1_14Types.ABSTRACT_SKELETON).index(13).handler((event, meta) -> {
byte value = (byte) meta.getValue();
if ((value & 4) != 0) {
event.createExtraMeta(new Metadata(14, Types1_13_2.META_TYPES.booleanType, true));
}
});
filter().filterFamily(Entity1_14Types.ZOMBIE).index(13).handler((event, meta) -> {
byte value = (byte) meta.getValue();
if ((value & 4) != 0) {
event.createExtraMeta(new Metadata(16, Types1_13_2.META_TYPES.booleanType, true));
}
});
filter().filterFamily(Entity1_14Types.ZOMBIE).addIndex(16);
// Remove bed location
filter().filterFamily(Entity1_14Types.LIVINGENTITY).handler((event, meta) -> {
int index = event.index();
if (index == 12) {
Position position = (Position) meta.getValue();
if (position != null) {
// Use bed
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_13.USE_BED, null, event.user());
wrapper.write(Type.VAR_INT, event.entityId());
wrapper.write(Type.POSITION, position);
try {
wrapper.scheduleSend(Protocol1_13_2To1_14.class);
} catch (Exception ex) {
ex.printStackTrace();
}
}
event.cancel();
} else if (index > 12) {
event.setIndex(index - 1);
}
});
// Pose
filter().removeIndex(6);
filter().type(Entity1_14Types.OCELOT).index(13).handler((event, meta) -> {
event.setIndex(15);
meta.setTypeAndValue(Types1_13_2.META_TYPES.varIntType, 0);
});
filter().type(Entity1_14Types.CAT).handler((event, meta) -> {
if (event.index() == 15) {
meta.setValue(1);
} else if (event.index() == 13) {
meta.setValue((byte) ((byte) meta.getValue() & 0x4));
}
});
filter().handler((event, meta) -> {
if (meta.metaType().typeId() > 15) {
throw new IllegalArgumentException("Unhandled metadata: " + meta);
}
});
}
Aggregations