use of com.viaversion.viaversion.api.minecraft.Position in project ViaVersion by ViaVersion.
the class Protocol1_9_1_2To1_9_3_4 method registerPackets.
@Override
protected void registerPackets() {
registerClientbound(ClientboundPackets1_9_3.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
// Position
map(Type.POSITION);
// Type
map(Type.UNSIGNED_BYTE);
// NBT
map(Type.NBT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 9) {
Position position = wrapper.get(Type.POSITION, 0);
CompoundTag tag = wrapper.get(Type.NBT, 0);
// Clear the packet
wrapper.clearPacket();
// Update sign packet
wrapper.setId(ClientboundPackets1_9.UPDATE_SIGN.ordinal());
// Position
wrapper.write(Type.POSITION, position);
for (int i = 1; i < 5; i++) {
// Should technically be written as COMPONENT, but left as String for simplification/to remove redundant wrapping for VR
// Sign line
wrapper.write(Type.STRING, (String) tag.get("Text" + i).getValue());
}
}
}
});
}
});
registerClientbound(ClientboundPackets1_9_3.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
Chunk1_9_3_4Type newType = new Chunk1_9_3_4Type(clientWorld);
// Get the old type to not write Block Entities
Chunk1_9_1_2Type oldType = new Chunk1_9_1_2Type(clientWorld);
Chunk chunk = wrapper.read(newType);
wrapper.write(oldType, chunk);
BlockEntity.handle(chunk.getBlockEntities(), wrapper.user());
}
});
}
});
registerClientbound(ClientboundPackets1_9_3.JOIN_GAME, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.INT);
// 1 - Gamemode
map(Type.UNSIGNED_BYTE);
// 2 - Dimension
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
}
});
}
});
registerClientbound(ClientboundPackets1_9_3.RESPAWN, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Dimension ID
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId);
}
});
}
});
}
use of com.viaversion.viaversion.api.minecraft.Position in project ViaVersion by ViaVersion.
the class BlockEntity method handle.
public static void handle(List<CompoundTag> tags, UserConnection connection) {
for (CompoundTag tag : tags) {
try {
if (!tag.contains("id"))
throw new Exception("NBT tag not handled because the id key is missing");
String id = (String) tag.get("id").getValue();
if (!types.containsKey(id))
throw new Exception("Not handled id: " + id);
int newId = types.get(id);
if (newId == -1)
continue;
int x = ((NumberTag) tag.get("x")).asInt();
int y = ((NumberTag) tag.get("y")).asInt();
int z = ((NumberTag) tag.get("z")).asInt();
Position pos = new Position(x, (short) y, z);
updateBlockEntity(pos, (short) newId, tag, connection);
} catch (Exception e) {
if (Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Block Entity: " + e.getMessage() + ": " + tag);
}
}
}
}
use of com.viaversion.viaversion.api.minecraft.Position in project ViaVersion by ViaVersion.
the class Protocol1_9_3To1_9_1_2 method registerPackets.
@Override
protected void registerPackets() {
// Sign update packet
registerClientbound(ClientboundPackets1_9.UPDATE_SIGN, null, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// read data
Position position = wrapper.read(Type.POSITION);
JsonElement[] lines = new JsonElement[4];
for (int i = 0; i < 4; i++) {
lines[i] = wrapper.read(Type.COMPONENT);
}
wrapper.clearInputBuffer();
// write data
// Update block entity
wrapper.setId(0x09);
// Block location
wrapper.write(Type.POSITION, position);
// Action type (9 update sign)
wrapper.write(Type.UNSIGNED_BYTE, (short) 9);
// Create nbt
CompoundTag tag = new CompoundTag();
tag.put("id", new StringTag("Sign"));
tag.put("x", new IntTag(position.x()));
tag.put("y", new IntTag(position.y()));
tag.put("z", new IntTag(position.z()));
for (int i = 0; i < lines.length; i++) {
tag.put("Text" + (i + 1), new StringTag(lines[i].toString()));
}
wrapper.write(Type.NBT, tag);
}
});
}
});
registerClientbound(ClientboundPackets1_9.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
Chunk chunk = wrapper.read(new Chunk1_9_1_2Type(clientWorld));
wrapper.write(new Chunk1_9_3_4Type(clientWorld), chunk);
List<CompoundTag> tags = chunk.getBlockEntities();
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
int block = section.getBlockWithoutData(x, y, z);
if (FakeTileEntity.isTileEntity(block)) {
tags.add(FakeTileEntity.createTileEntity(x + (chunk.getX() << 4), y + (i << 4), z + (chunk.getZ() << 4), block));
}
}
}
}
}
}
});
}
});
registerClientbound(ClientboundPackets1_9.JOIN_GAME, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.INT);
// 1 - Gamemode
map(Type.UNSIGNED_BYTE);
// 2 - Dimension
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientWorld.setEnvironment(dimensionId);
}
});
}
});
registerClientbound(ClientboundPackets1_9.RESPAWN, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Dimension ID
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId);
}
});
}
});
// Sound effect
registerClientbound(ClientboundPackets1_9.SOUND, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Sound name
map(Type.VAR_INT);
// 1 - Sound Category
map(Type.VAR_INT);
// 2 - x
map(Type.INT);
// 3 - y
map(Type.INT);
// 4 - z
map(Type.INT);
// 5 - Volume
map(Type.FLOAT);
// 6 - Pitch
map(ADJUST_PITCH);
}
});
}
use of com.viaversion.viaversion.api.minecraft.Position in project ViaVersion by ViaVersion.
the class ConnectionData method update.
public static void update(UserConnection user, Position position) {
for (BlockFace face : BlockFace.values()) {
Position pos = position.getRelative(face);
int blockState = blockConnectionProvider.getBlockData(user, pos.x(), pos.y(), pos.z());
ConnectionHandler handler = connectionHandlerMap.get(blockState);
if (handler == null)
continue;
int newBlockState = handler.connect(user, pos, blockState);
PacketWrapper blockUpdatePacket = PacketWrapper.create(ClientboundPackets1_13.BLOCK_CHANGE, null, user);
blockUpdatePacket.write(Type.POSITION, pos);
blockUpdatePacket.write(Type.VAR_INT, newBlockState);
try {
blockUpdatePacket.send(Protocol1_13To1_12_2.class);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
use of com.viaversion.viaversion.api.minecraft.Position in project ViaVersion by ViaVersion.
the class ConnectionData method connectBlocks.
public static void connectBlocks(UserConnection user, Chunk chunk) {
long xOff = chunk.getX() << 4;
long zOff = chunk.getZ() << 4;
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
boolean willConnect = false;
for (int p = 0; p < section.getPaletteSize(); p++) {
int id = section.getPaletteEntry(p);
if (ConnectionData.connects(id)) {
willConnect = true;
break;
}
}
if (!willConnect)
continue;
long yOff = i << 4;
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
int block = section.getFlatBlock(x, y, z);
ConnectionHandler handler = ConnectionData.getConnectionHandler(block);
if (handler != null) {
block = handler.connect(user, new Position((int) (xOff + x), (short) (yOff + y), (int) (zOff + z)), block);
section.setFlatBlock(x, y, z, block);
}
}
}
}
}
}
Aggregations