use of com.github.dirtpowered.dirtmv.data.entity.SpawnableObject in project DirtMultiversion by DirtPowered.
the class V1_7RTo1_8RMetadataTransformer method transformMetadata.
@Override
public WatchableObject[] transformMetadata(SpawnableObject entityType, WatchableObject[] watchableObjects) {
List<WatchableObject> newMetaData = new ArrayList<>();
for (WatchableObject watchableObject : watchableObjects) {
MetadataType type = watchableObject.getType();
Object value = watchableObject.getValue();
int index = watchableObject.getIndex();
if (entityType.isLivingEntity()) {
if (type == MetadataType.BYTE && index == 11) {
// nametag visibility
newMetaData.add(new WatchableObject(MetadataType.BYTE, 3, value));
} else if (type == MetadataType.INT && index == 12) {
// ageable entities
int age = (int) value;
newMetaData.add(new WatchableObject(MetadataType.BYTE, 12, (byte) (age < 0 ? -1 : 0)));
} else if (entityType == EntityType.ENDER_MAN && type == MetadataType.BYTE && index == 16) {
// enderman carried item
newMetaData.add(new WatchableObject(MetadataType.SHORT, 16, ((Byte) value).shortValue()));
} else if (entityType == EntityType.HUMAN) {
if (type == MetadataType.BYTE && index == 16) {
// TODO: check for metadata changes
} else {
newMetaData.add(watchableObject);
}
} else if (type == MetadataType.STRING && index == 10) {
// custom display name
newMetaData.add(new WatchableObject(MetadataType.STRING, 2, value));
} else {
newMetaData.add(watchableObject);
}
} else {
if (entityType == ObjectType.ITEM_FRAME) {
if (type == MetadataType.BYTE && index == 3) {
newMetaData.add(new WatchableObject(MetadataType.BYTE, 9, value));
} else if (type == MetadataType.ITEM && index == 2) {
newMetaData.add(new WatchableObject(MetadataType.ITEM, 8, value));
}
} else {
newMetaData.add(watchableObject);
}
}
}
if (newMetaData.isEmpty()) {
List<WatchableObject> defaultMetadata = Arrays.asList(new WatchableObject(MetadataType.BYTE, 0, (byte) 0), new WatchableObject(MetadataType.SHORT, 1, 300), new WatchableObject(MetadataType.BYTE, 4, (byte) 0));
newMetaData.addAll(defaultMetadata);
}
return newMetaData.toArray(new WatchableObject[0]);
}
use of com.github.dirtpowered.dirtmv.data.entity.SpawnableObject in project DirtMultiversion by DirtPowered.
the class V1_5RTo1_6RMetadataTransformer method transformMetadata.
@Override
public WatchableObject[] transformMetadata(SpawnableObject entityType, WatchableObject[] watchableObjects) {
List<WatchableObject> newMetaData = new ArrayList<>();
for (WatchableObject watchableObject : watchableObjects) {
MetadataType type = watchableObject.getType();
Object value = watchableObject.getValue();
int index = watchableObject.getIndex();
if (entityType.isLivingEntity()) {
if (index == 8) {
// potion color
newMetaData.add(new WatchableObject(type, 7, value));
} else if (index == 9) {
// potion ambient
newMetaData.add(new WatchableObject(type, 8, value));
} else if (index == 10) {
// arrow count in entity
newMetaData.add(new WatchableObject(type, 9, value));
} else if (index == 5) {
// name tag
newMetaData.add(new WatchableObject(type, 10, value));
} else if (index == 6) {
// name tag visibility
newMetaData.add(new WatchableObject(type, 11, value));
} else if (entityType == EntityType.WITHER | entityType == EntityType.ENDER_DRAGON) {
if (index == 16) {
newMetaData.add(new WatchableObject(MetadataType.FLOAT, 6, ((Integer) value).floatValue()));
} else {
newMetaData.add(watchableObject);
}
} else if (entityType == EntityType.WOLF) {
if (index == 18) {
newMetaData.add(new WatchableObject(MetadataType.FLOAT, 18, ((Integer) value).floatValue()));
} else {
newMetaData.add(watchableObject);
}
} else if (entityType == EntityType.HUMAN) {
if (index == 17) {
// absorption amount
if (value instanceof Byte) {
newMetaData.add(new WatchableObject(MetadataType.FLOAT, 17, ((Byte) value).floatValue()));
} else if (value instanceof Integer) {
newMetaData.add(new WatchableObject(MetadataType.FLOAT, 17, ((Integer) value).floatValue()));
}
} else {
newMetaData.add(watchableObject);
}
} else {
newMetaData.add(watchableObject);
}
} else {
newMetaData.add(watchableObject);
}
}
if (newMetaData.isEmpty()) {
List<WatchableObject> defaultMetadata = Arrays.asList(new WatchableObject(MetadataType.BYTE, 0, 0), new WatchableObject(MetadataType.SHORT, 1, 300));
newMetaData.addAll(defaultMetadata);
}
return newMetaData.toArray(new WatchableObject[0]);
}
use of com.github.dirtpowered.dirtmv.data.entity.SpawnableObject in project DirtMultiversion by DirtPowered.
the class EntityPackets method registerTranslators.
@Override
public void registerTranslators() {
// spawn mob
addTranslator(0x0F, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
EntityType entityType = EntityType.fromEntityTypeId(data.read(Type.BYTE, 1));
if (storage.hasObject(V1_7EntityTracker.class)) {
V1_7EntityTracker tracker = storage.get(V1_7EntityTracker.class);
int entityId = data.read(Type.VAR_INT, 0);
tracker.addEntity(entityId, entityType);
}
WatchableObject[] oldMeta = data.read(Type.V1_7R_METADATA, 11);
WatchableObject[] newMeta = metadataTransformer.transformMetadata(entityType, oldMeta);
return PacketUtil.createPacket(0x0F, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7), data.read(8), data.read(9), data.read(10), set(Type.V1_8R_METADATA, newMeta) });
}
});
// entity metadata
addTranslator(0x1C, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
SpawnableObject entityObject = null;
int entityId = data.read(Type.INT, 0);
if (storage.hasObject(EntityTracker.class)) {
EntityTracker tracker = storage.get(EntityTracker.class);
entityObject = tracker.getEntityById(entityId);
} else if (storage.hasObject(V1_7EntityTracker.class)) {
V1_7EntityTracker tracker = storage.get(V1_7EntityTracker.class);
entityObject = tracker.getEntityById(entityId);
}
if (entityObject == null)
return cancel();
WatchableObject[] oldMeta = data.read(Type.V1_7R_METADATA, 1);
WatchableObject[] watchableObjects = metadataTransformer.transformMetadata(entityObject, oldMeta);
for (int i = 0; i < watchableObjects.length; i++) {
WatchableObject watchableObject = watchableObjects[i];
if (watchableObject.getType() == MetadataType.ITEM) {
ItemStack obj = (ItemStack) watchableObject.getValue();
if (obj != null) {
obj = InventoryPackets.itemRemapper.replaceItem(obj);
}
watchableObjects[i] = new WatchableObject(MetadataType.ITEM, watchableObject.getIndex(), obj);
}
}
return PacketUtil.createPacket(0x1C, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), set(Type.V1_8R_METADATA, watchableObjects) });
}
});
// entity velocity
addTranslator(0x12, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x12, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1), data.read(2), data.read(3) });
}
});
// entity destroy
addTranslator(0x13, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
int[] entities = data.read(Type.BYTE_INT_ARRAY, 0);
if (storage.hasObject(V1_7EntityTracker.class)) {
for (int entityId : entities) {
V1_7EntityTracker tracker = storage.get(V1_7EntityTracker.class);
tracker.removeEntity(entityId);
}
}
return PacketUtil.createPacket(0x13, new TypeHolder[] { set(Type.VAR_INT_ARRAY, entities) });
}
});
// spawn player
addTranslator(0x0C, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@SneakyThrows
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
UUID uniqueId = UUID.fromString(data.read(Type.V1_7_STRING, 1));
String username = data.read(Type.V1_7_STRING, 2);
GameProfile profile = GameProfileFetcher.getProfile(username).get();
if (storage.hasObject(V1_7EntityTracker.class)) {
V1_7EntityTracker tracker = storage.get(V1_7EntityTracker.class);
int entityId = data.read(Type.VAR_INT, 0);
tracker.addEntity(entityId, EntityType.HUMAN);
}
WatchableObject[] oldMeta = data.read(Type.V1_7R_METADATA, 10);
WatchableObject[] newMeta = metadataTransformer.transformMetadata(EntityType.HUMAN, oldMeta);
// skin layers
WatchableObject[] layers = new WatchableObject[newMeta.length + 1];
System.arraycopy(newMeta, 0, layers, 0, newMeta.length);
layers[newMeta.length] = new WatchableObject(MetadataType.BYTE, 10, (byte) 127);
newMeta = layers;
PacketData playerSpawn = PacketUtil.createPacket(0x0C, new TypeHolder[] { data.read(0), set(Type.UUID, profile.getId()), data.read(4), data.read(5), data.read(6), data.read(7), data.read(8), data.read(9), set(Type.V1_8R_METADATA, newMeta) });
// create fake profile
// GameProfile profile = new GameProfile(uniqueId, username);
TabListEntry tabAddListEntry = new TabListEntry(TabListAction.ADD_PLAYER, new PlayerListEntry[] { new PlayerListEntry(profile, new Property[0], 0, 0, null) });
PacketData tabEntry = PacketUtil.createPacket(0x38, new TypeHolder[] { set(Type.TAB_LIST_ENTRY, tabAddListEntry) });
// seems that client overwrites old tab packet after sending a new one
session.sendPacket(tabEntry, PacketDirection.TO_CLIENT, getFrom());
// send player spawn (right after tablist packet)
session.sendPacket(playerSpawn, PacketDirection.TO_CLIENT, getFrom());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
TabListEntry tabRemoveListEntry = new TabListEntry(TabListAction.REMOVE_PLAYER, new PlayerListEntry[] { new PlayerListEntry(profile, new Property[0], 0, 0, null) });
PacketData removeTab = PacketUtil.createPacket(0x38, new TypeHolder[] { set(Type.TAB_LIST_ENTRY, tabRemoveListEntry) });
session.sendPacket(removeTab, PacketDirection.TO_CLIENT, getFrom());
}
}, 40L);
return cancel();
}
});
// entity equipment
addTranslator(0x04, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack originalItem = data.read(Type.V1_3R_ITEM, 2);
if (originalItem != null) {
originalItem = InventoryPackets.itemRemapper.replaceItem(originalItem);
}
return PacketUtil.createPacket(0x04, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1), set(Type.V1_8R_ITEM, originalItem) });
}
});
// update tile entity
addTranslator(0x35, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 0);
short y = data.read(Type.SHORT, 1);
int z = data.read(Type.INT, 2);
return PacketUtil.createPacket(0x35, new TypeHolder[] { set(Type.LONG, ProtocolRelease47To5.toBlockPosition(x, y, z)), data.read(3), set(Type.COMPRESSED_COMPOUND_TAG, data.read(Type.COMPOUND_TAG, 4)) });
}
});
// spawn painting
addTranslator(0x10, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 2);
int y = data.read(Type.INT, 3);
int z = data.read(Type.INT, 4);
int direction = data.read(Type.INT, 5);
switch(direction) {
case 0:
z += 1;
break;
case 1:
x -= 1;
break;
case 2:
z -= 1;
break;
case 3:
x += 1;
break;
}
return PacketUtil.createPacket(0x10, new TypeHolder[] { data.read(0), data.read(1), set(Type.LONG, ProtocolRelease47To5.toBlockPosition(x, y, z)), set(Type.BYTE, data.read(Type.INT, 5).byteValue()) });
}
});
// entity effect
addTranslator(0x1D, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x1D, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1), data.read(2), set(Type.VAR_INT, data.read(Type.SHORT, 3).intValue()), set(Type.BOOLEAN, false) });
}
});
// remove entity effect
addTranslator(0x1E, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x1E, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1) });
}
});
// spawn object
addTranslator(0x0E, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
int x = data.read(Type.INT, 2);
int y = data.read(Type.INT, 3);
int z = data.read(Type.INT, 4);
byte type = data.read(Type.BYTE, 1);
byte yaw = data.read(Type.BYTE, 6);
Motion motionData = data.read(Type.MOTION, 7);
ObjectType objectType = ObjectType.fromObjectTypeId(type);
switch(objectType) {
case ITEM:
y -= 4;
break;
case TNT_PRIMED:
y -= 16;
break;
case FALLING_OBJECT:
int itemId = motionData.getThrowerId();
int itemData = motionData.getThrowerId() >> 16;
short mX = motionData.getMotionX();
short mY = motionData.getMotionY();
short mZ = motionData.getMotionZ();
y -= 16;
motionData = new Motion(itemId | itemData << 12, mX, mY, mZ);
break;
case ITEM_FRAME:
int rotation = motionData.getThrowerId();
if (rotation == 0) {
z += 32;
yaw = 0;
} else if (rotation == 1) {
x -= 32;
yaw = 64;
} else if (rotation == 2) {
z -= 32;
yaw = -128;
} else if (rotation == 3) {
x += 32;
yaw = -64;
}
break;
}
if (storage.hasObject(V1_7EntityTracker.class)) {
V1_7EntityTracker tracker = storage.get(V1_7EntityTracker.class);
int entityId = data.read(Type.VAR_INT, 0);
tracker.addEntity(entityId, objectType);
}
return PacketUtil.createPacket(0x0E, new TypeHolder[] { data.read(0), set(Type.BYTE, type), set(Type.INT, x), set(Type.INT, y), set(Type.INT, z), data.read(5), set(Type.BYTE, yaw), set(Type.MOTION, motionData) });
}
});
}
Aggregations