use of com.github.dirtpowered.dirtmv.data.user.ProtocolStorage 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) });
}
});
}
use of com.github.dirtpowered.dirtmv.data.user.ProtocolStorage in project DirtMultiversion by DirtPowered.
the class InventoryPackets method registerTranslators.
@Override
public void registerTranslators() {
// set slot
addTranslator(0x2F, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
short windowId = data.read(Type.UNSIGNED_BYTE, 0);
short slot = data.read(Type.SHORT, 1);
int windowType = 0;
if (storage.hasObject(WindowTypeTracker.class)) {
WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
windowType = windowTracker.getWindowType(windowId);
}
ItemStack originalItem = data.read(Type.V1_3R_ITEM, 2);
if (originalItem == null)
return new PacketData(0x2F, data.getObjects());
ItemStack itemStack = itemRemapper.replaceItem(originalItem);
if (windowType == 4) {
if (slot >= 1) {
slot++;
}
}
// quick-bar cache
if (storage.hasObject(QuickBarTracker.class)) {
QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
quickBarTracker.setItem(slot, itemStack);
}
return PacketUtil.createPacket(0x2F, new TypeHolder[] { data.read(0), set(Type.SHORT, slot), set(Type.V1_8R_ITEM, itemStack) });
}
});
// window items
addTranslator(0x30, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
short windowId = data.read(Type.UNSIGNED_BYTE, 0);
ItemStack[] itemArray = data.read(Type.V1_3R_ITEM_ARRAY, 1);
int windowType = 0;
if (storage.hasObject(WindowTypeTracker.class)) {
WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
windowType = windowTracker.getWindowType(windowId);
}
if (windowType == 4) {
ItemStack[] copy = itemArray;
itemArray = new ItemStack[copy.length + 1];
itemArray[0] = copy[0];
int slot = 0;
while (slot < copy.length - 1) {
itemArray[slot + 2] = copy[slot + 1];
slot++;
}
// lapis
itemArray[1] = new ItemStack(351, 3, 4, null);
}
for (int i = 0; i < itemArray.length; i++) {
ItemStack originalItem = itemArray[i];
ItemStack item = originalItem;
if (originalItem != null) {
item = itemRemapper.replaceItem(originalItem);
}
itemArray[i] = item;
// quick-bar cache
if (storage.hasObject(QuickBarTracker.class)) {
QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
quickBarTracker.setItem(i, item);
}
}
return PacketUtil.createPacket(0x30, new TypeHolder[] { data.read(0), set(Type.V1_8R_ITEM_ARRAY, itemArray) });
}
});
// open window
addTranslator(0x2D, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
short windowId = data.read(Type.UNSIGNED_BYTE, 0);
short type = data.read(Type.UNSIGNED_BYTE, 1);
String title = data.read(Type.V1_7_STRING, 2);
short slots = data.read(Type.UNSIGNED_BYTE, 3);
// non storage inventories have always 0 slots
slots = InventoryUtils.isNonStorageInventory(type) ? 0 : slots;
// cache window type (for enchanting tables)
if (storage.hasObject(WindowTypeTracker.class)) {
WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
windowTracker.setWindowTypeFor(windowId, type);
}
return PacketUtil.createPacket(0x2D, new TypeHolder[] { data.read(0), set(Type.V1_7_STRING, InventoryUtils.getNamedTypeFromId(type)), set(Type.V1_7_STRING, InventoryUtils.addTranslateComponent(type == 6 ? "entity.Villager.name" : title)), set(Type.UNSIGNED_BYTE, slots) });
}
});
// window click
addTranslator(0x0E, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
byte windowId = data.read(Type.BYTE, 0);
short slot = data.read(Type.SHORT, 1);
int windowType = 0;
if (storage.hasObject(WindowTypeTracker.class)) {
WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
windowType = windowTracker.getWindowType(windowId);
}
if (windowType == 4) {
if (slot > 1) {
slot--;
}
}
if (storage.hasObject(QuickBarTracker.class)) {
QuickBarTracker cache = storage.get(QuickBarTracker.class);
boolean shiftClick = data.read(Type.BYTE, 4) == 1;
updateLocalInventory(cache, slot, shiftClick);
}
return PacketUtil.createPacket(0x0E, new TypeHolder[] { data.read(0), set(Type.SHORT, slot), data.read(2), data.read(3), data.read(4), set(Type.V1_3R_ITEM, data.read(Type.V1_8R_ITEM, 5)) });
}
});
// creative item get
addTranslator(0x10, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack item = data.read(Type.V1_8R_ITEM, 1);
boolean notNull = item != null;
if (notNull && !creativeTab.exists(item.getItemId())) {
// replace all unknown items to stone
item.setItemId(1);
item.setData(0);
}
return PacketUtil.createPacket(0x10, new TypeHolder[] { data.read(0), set(Type.V1_3R_ITEM, item) });
}
});
// change held slot
addTranslator(0x09, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
if (storage.hasObject(QuickBarTracker.class)) {
QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
quickBarTracker.setCurrentHotBarSlot(data.read(Type.SHORT, 0));
}
return data;
}
});
// change held slot (server-side)
addTranslator(0x09, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ProtocolStorage storage = session.getStorage();
if (storage.hasObject(QuickBarTracker.class)) {
QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
quickBarTracker.setCurrentHotBarSlot(data.read(Type.BYTE, 0));
}
return data;
}
});
// window property
addTranslator(0x31, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int property = data.read(Type.SHORT, 1);
switch(property) {
case 0:
property = 2;
PacketData windowProperty = PacketUtil.createPacket(0x31, new TypeHolder[] { data.read(0), set(Type.SHORT, (short) 3), set(Type.SHORT, (short) 200) });
session.sendPacket(windowProperty, PacketDirection.TO_CLIENT, getFrom());
break;
case 1:
property = 0;
break;
case 2:
property = 1;
break;
}
return PacketUtil.createPacket(0x31, new TypeHolder[] { data.read(0), set(Type.SHORT, (short) property), data.read(2) });
}
});
}
use of com.github.dirtpowered.dirtmv.data.user.ProtocolStorage in project DirtMultiversion by DirtPowered.
the class MovementTranslator method updateBoundingBox.
public static void updateBoundingBox(ServerSession session, Location position) {
ProtocolStorage storage = session.getStorage();
if (!storage.hasObject(BoundingBox.class)) {
BoundingBox b = new BoundingBox(position.getX(), position.getY(), position.getZ(), PLAYER_MODEL_X, PLAYER_MODEL_Y, PLAYER_MODEL_Z);
storage.set(BoundingBox.class, b);
}
BoundingBox boundingBox = storage.get(BoundingBox.class);
boundingBox.setMiddleX(position.getX());
boundingBox.setMiddleY(position.getY() + 0.9D);
boundingBox.setMiddleZ(position.getZ());
}
use of com.github.dirtpowered.dirtmv.data.user.ProtocolStorage in project DirtMultiversion by DirtPowered.
the class ProtocolRelease47To5 method onConnect.
@Override
public void onConnect(ServerSession session) {
ProtocolStorage storage = session.getStorage();
storage.set(OnGroundTracker.class, new OnGroundTracker());
storage.set(WindowTypeTracker.class, new WindowTypeTracker());
storage.set(QuickBarTracker.class, new QuickBarTracker());
// additional block storage (for nether portal rotation fix)
storage.set(PortalFrameCache.class, new PortalFrameCache());
if (session.getMain().getConfiguration().enableViaVersion()) {
// it's not needed for 1.8->1.7, but ViaVersion will need that to fix issues
// with eating, entering portals, effects (potions)
storage.set(PlayerMovementTracker.class, new PlayerMovementTracker());
}
// check if 1.6 entity tracker exists - if not, create one
if (!storage.hasObject(EntityTracker.class)) {
storage.set(V1_7EntityTracker.class, new V1_7EntityTracker());
}
// fixes block hardness inconsistencies
if (session.getMain().getConfiguration().getServerVersion() == MinecraftVersion.B1_7_3) {
storage.set(BlockMiningTimeFixer.class, new BlockMiningTimeFixer(session));
}
}
use of com.github.dirtpowered.dirtmv.data.user.ProtocolStorage in project DirtMultiversion by DirtPowered.
the class V1_3ToV1_8ChunkTranslator method getChunkData.
public byte[] getChunkData() {
ExtendedBlockStorage[] columnStorage = chunkStorage.getColumnStorage();
List<ExtendedBlockStorage> blockStorages = new ArrayList<>();
boolean skyLight = chunkStorage.isSkylight();
byte[] biomes = chunkStorage.getBiomeData();
int columnBits = 0;
for (int i = 0; i < columnStorage.length; ++i) {
ExtendedBlockStorage extendedblockstorage = columnStorage[i];
boolean f = extendedblockstorage != null && oldChunk != columnStorage[i].isEmpty();
if (extendedblockstorage != null && (this.bitmapValue & 1 << i) != 0 && (!this.groundUp || f)) {
columnBits |= 1 << i;
blockStorages.add(extendedblockstorage);
}
}
byte[] data = new byte[ChunkUtils.calculateDataSize(Integer.bitCount(columnBits), skyLight)];
int totalSize = 0;
for (int i = 0; i < blockStorages.size(); i++) {
ExtendedBlockStorage blockStorage = blockStorages.get(i);
byte[] blockArray = blockStorage.getBlockLSBArray();
for (int j = 0; j < blockArray.length; ++j) {
int x = j & 15;
int y = (j >> 8);
int z = j >> 4 & 15;
int blockId = blockArray[j] & 255;
int blockData = blockStorage.getBlockMetadataArray().getNibble(x, y, z);
if (DataFixers.shouldCache(blockId)) {
ProtocolStorage storage = session.getStorage();
PortalFrameCache portalFrameCache = storage.get(PortalFrameCache.class);
y = +i * 16 & 255;
int xPos = x + (chunkStorage.getChunkX() << 4);
int zPos = z + (chunkStorage.getChunkZ() << 4);
portalFrameCache.setBlockAt(chunkStorage.getChunkX(), chunkStorage.getChunkZ(), xPos, y, zPos, blockId);
blockData = DataFixers.getCorrectedDataFor(portalFrameCache, xPos, y, zPos, blockId, blockData);
} else {
blockData = DataFixers.fixInvalidData(blockId, blockData);
}
char c = (char) (blockId << 4 | blockData);
data[totalSize++] = (byte) (c & 255);
data[totalSize++] = (byte) (c >> 8 & 255);
}
}
for (ExtendedBlockStorage blockStorage : blockStorages) {
totalSize = writeData(blockStorage.getBlockLightArray().getData(), data, totalSize);
}
if (skyLight) {
for (ExtendedBlockStorage blockStorage : blockStorages) {
totalSize = writeData(blockStorage.getSkylightArray().getData(), data, totalSize);
}
}
if (this.groundUp) {
totalSize = writeData(biomes, data, totalSize);
}
byte[] chunk = new byte[totalSize];
System.arraycopy(data, 0, chunk, 0, totalSize);
return chunk;
}
Aggregations