use of com.viaversion.viaversion.api.type.types.Particle in project ViaVersion by ViaVersion.
the class Particle1_13_2Type method read.
@Override
public Particle read(ByteBuf buffer) throws Exception {
int type = Type.VAR_INT.readPrimitive(buffer);
Particle particle = new Particle(type);
switch(type) {
// Block / Falling Dust /
case 3:
case 20:
// Flat Block
particle.getArguments().add(new Particle.ParticleData(Type.VAR_INT, Type.VAR_INT.readPrimitive(buffer)));
break;
// Dust
case 11:
// Red 0 - 1
particle.getArguments().add(new Particle.ParticleData(Type.FLOAT, Type.FLOAT.readPrimitive(buffer)));
// Green 0 - 1
particle.getArguments().add(new Particle.ParticleData(Type.FLOAT, Type.FLOAT.readPrimitive(buffer)));
// Blue 0 - 1
particle.getArguments().add(new Particle.ParticleData(Type.FLOAT, Type.FLOAT.readPrimitive(buffer)));
// Scale 0.01 - 4
particle.getArguments().add(new Particle.ParticleData(Type.FLOAT, Type.FLOAT.readPrimitive(buffer)));
break;
// Item
case 27:
// Flat item
particle.getArguments().add(new Particle.ParticleData(Type.FLAT_VAR_INT_ITEM, Type.FLAT_VAR_INT_ITEM.read(buffer)));
break;
}
return particle;
}
use of com.viaversion.viaversion.api.type.types.Particle in project ViaVersion by ViaVersion.
the class MetadataRewriter1_13To1_12_2 method handleMetadata.
@Override
protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
// Handle new MetaTypes
if (metadata.metaType().typeId() > 4) {
metadata.setMetaType(Types1_13.META_TYPES.byId(metadata.metaType().typeId() + 1));
} else {
metadata.setMetaType(Types1_13.META_TYPES.byId(metadata.metaType().typeId()));
}
// Handle String -> Chat DisplayName
if (metadata.id() == 2) {
if (metadata.getValue() != null && !((String) metadata.getValue()).isEmpty()) {
metadata.setTypeAndValue(Types1_13.META_TYPES.optionalComponentType, ChatRewriter.legacyTextToJson((String) metadata.getValue()));
} else {
metadata.setTypeAndValue(Types1_13.META_TYPES.optionalComponentType, null);
}
}
// Remap held block to match new format for remapping to flat block
if (type == Entity1_13Types.EntityType.ENDERMAN && metadata.id() == 12) {
int stateId = (int) metadata.getValue();
int id = stateId & 4095;
int data = stateId >> 12 & 15;
metadata.setValue((id << 4) | (data & 0xF));
}
// 1.13 changed item to flat item (no data)
if (metadata.metaType() == Types1_13.META_TYPES.itemType) {
metadata.setMetaType(Types1_13.META_TYPES.itemType);
protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == Types1_13.META_TYPES.blockStateType) {
// Convert to new block id
metadata.setValue(WorldPackets.toNewId((int) metadata.getValue()));
}
// Skip type related changes when the type is null
if (type == null)
return;
// Handle new colors
if (type == Entity1_13Types.EntityType.WOLF && metadata.id() == 17) {
metadata.setValue(15 - (int) metadata.getValue());
}
// Handle new zombie meta (INDEX 15 - Boolean - Zombie is shaking while enabled)
if (type.isOrHasParent(Entity1_13Types.EntityType.ZOMBIE)) {
if (metadata.id() > 14)
metadata.setId(metadata.id() + 1);
}
// Handle Minecart inner block
if (type.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT) && metadata.id() == 9) {
// New block format
int oldId = (int) metadata.getValue();
int combined = (((oldId & 4095) << 4) | (oldId >> 12 & 15));
int newId = WorldPackets.toNewId(combined);
metadata.setValue(newId);
}
// Handle other changes
if (type == Entity1_13Types.EntityType.AREA_EFFECT_CLOUD) {
if (metadata.id() == 9) {
int particleId = (int) metadata.getValue();
Metadata parameter1Meta = metaByIndex(10, metadatas);
Metadata parameter2Meta = metaByIndex(11, metadatas);
int parameter1 = parameter1Meta != null ? (int) parameter1Meta.getValue() : 0;
int parameter2 = parameter2Meta != null ? (int) parameter2Meta.getValue() : 0;
Particle particle = ParticleRewriter.rewriteParticle(particleId, new Integer[] { parameter1, parameter2 });
if (particle != null && particle.getId() != -1) {
metadatas.add(new Metadata(9, Types1_13.META_TYPES.particleType, particle));
}
}
if (metadata.id() >= 9)
// Remove
metadatas.remove(metadata);
}
if (metadata.id() == 0) {
// Previously unused, now swimming
metadata.setValue((byte) ((byte) metadata.getValue() & ~0x10));
}
// TODO: Boat has changed
}
use of com.viaversion.viaversion.api.type.types.Particle in project ViaVersion by ViaVersion.
the class WorldPackets method register.
public static void register(Protocol protocol) {
// Outgoing packets
protocol.registerClientbound(ClientboundPackets1_12_1.SPAWN_PAINTING, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// 1 - Entity UUID
map(Type.UUID);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
PaintingProvider provider = Via.getManager().getProviders().get(PaintingProvider.class);
String motive = wrapper.read(Type.STRING);
Optional<Integer> id = provider.getIntByIdentifier(motive);
if (!id.isPresent() && (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug())) {
Via.getPlatform().getLogger().warning("Could not find painting motive: " + motive + " falling back to default (0)");
}
wrapper.write(Type.VAR_INT, id.orElse(0));
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Location
map(Type.POSITION);
// 1 - Action
map(Type.UNSIGNED_BYTE);
// 2 - NBT data
map(Type.NBT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Position position = wrapper.get(Type.POSITION, 0);
short action = wrapper.get(Type.UNSIGNED_BYTE, 0);
CompoundTag tag = wrapper.get(Type.NBT, 0);
BlockEntityProvider provider = Via.getManager().getProviders().get(BlockEntityProvider.class);
int newId = provider.transform(wrapper.user(), position, tag, true);
if (newId != -1) {
BlockStorage storage = wrapper.user().get(BlockStorage.class);
BlockStorage.ReplacementData replacementData = storage.get(position);
if (replacementData != null) {
replacementData.setReplacement(newId);
}
}
if (action == 5) {
// Set type of flower in flower pot
// Removed
wrapper.cancel();
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_ACTION, new PacketRemapper() {
@Override
public void registerMap() {
// Location
map(Type.POSITION);
// Action Id
map(Type.UNSIGNED_BYTE);
// Action param
map(Type.UNSIGNED_BYTE);
// Block Id - /!\ NOT BLOCK STATE ID
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Position pos = wrapper.get(Type.POSITION, 0);
short action = wrapper.get(Type.UNSIGNED_BYTE, 0);
short param = wrapper.get(Type.UNSIGNED_BYTE, 1);
int blockId = wrapper.get(Type.VAR_INT, 0);
if (blockId == 25)
blockId = 73;
else if (blockId == 33)
blockId = 99;
else if (blockId == 29)
blockId = 92;
else if (blockId == 54)
blockId = 142;
else if (blockId == 146)
blockId = 305;
else if (blockId == 130)
blockId = 249;
else if (blockId == 138)
blockId = 257;
else if (blockId == 52)
blockId = 140;
else if (blockId == 209)
blockId = 472;
else if (blockId >= 219 && blockId <= 234)
blockId = blockId - 219 + 483;
if (blockId == 73) {
// Note block
// block change
PacketWrapper blockChange = wrapper.create(0x0B);
blockChange.write(Type.POSITION, pos);
blockChange.write(Type.VAR_INT, 249 + (action * 24 * 2) + (param * 2));
blockChange.send(Protocol1_13To1_12_2.class);
}
wrapper.set(Type.VAR_INT, 0, blockId);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION);
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Position position = wrapper.get(Type.POSITION, 0);
int newId = toNewId(wrapper.get(Type.VAR_INT, 0));
UserConnection userConnection = wrapper.user();
if (Via.getConfig().isServersideBlockConnections()) {
ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), newId);
newId = ConnectionData.connect(userConnection, position, newId);
}
wrapper.set(Type.VAR_INT, 0, checkStorage(wrapper.user(), position, newId));
if (Via.getConfig().isServersideBlockConnections()) {
// Workaround for packet order issue
wrapper.send(Protocol1_13To1_12_2.class);
wrapper.cancel();
ConnectionData.update(userConnection, position);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.MULTI_BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Chunk X
map(Type.INT);
// 1 - Chunk Z
map(Type.INT);
// 2 - Records
map(Type.BLOCK_CHANGE_RECORD_ARRAY);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int chunkX = wrapper.get(Type.INT, 0);
int chunkZ = wrapper.get(Type.INT, 1);
UserConnection userConnection = wrapper.user();
BlockChangeRecord[] records = wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0);
// Convert ids
for (BlockChangeRecord record : records) {
int newBlock = toNewId(record.getBlockId());
Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
if (Via.getConfig().isServersideBlockConnections()) {
ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), newBlock);
}
record.setBlockId(checkStorage(wrapper.user(), position, newBlock));
}
if (Via.getConfig().isServersideBlockConnections()) {
for (BlockChangeRecord record : records) {
int blockState = record.getBlockId();
Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
ConnectionHandler handler = ConnectionData.getConnectionHandler(blockState);
if (handler != null) {
blockState = handler.connect(userConnection, position, blockState);
record.setBlockId(blockState);
}
}
// Workaround for packet order issue
wrapper.send(Protocol1_13To1_12_2.class);
wrapper.cancel();
for (BlockChangeRecord record : records) {
Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
ConnectionData.update(userConnection, position);
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.EXPLOSION, new PacketRemapper() {
@Override
public void registerMap() {
if (!Via.getConfig().isServersideBlockConnections())
return;
// X
map(Type.FLOAT);
// Y
map(Type.FLOAT);
// Z
map(Type.FLOAT);
// Radius
map(Type.FLOAT);
// Record Count
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
UserConnection userConnection = wrapper.user();
int x = (int) Math.floor(wrapper.get(Type.FLOAT, 0));
int y = (int) Math.floor(wrapper.get(Type.FLOAT, 1));
int z = (int) Math.floor(wrapper.get(Type.FLOAT, 2));
int recordCount = wrapper.get(Type.INT, 0);
Position[] records = new Position[recordCount];
for (int i = 0; i < recordCount; i++) {
Position position = new Position(x + wrapper.passthrough(Type.BYTE), (short) (y + wrapper.passthrough(Type.BYTE)), z + wrapper.passthrough(Type.BYTE));
records[i] = position;
// Set to air
ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), 0);
}
// Workaround for packet order issue
wrapper.send(Protocol1_13To1_12_2.class);
wrapper.cancel();
for (int i = 0; i < recordCount; i++) {
ConnectionData.update(userConnection, records[i]);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.UNLOAD_CHUNK, new PacketRemapper() {
@Override
public void registerMap() {
if (Via.getConfig().isServersideBlockConnections()) {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int x = wrapper.passthrough(Type.INT);
int z = wrapper.passthrough(Type.INT);
ConnectionData.blockConnectionProvider.unloadChunk(wrapper.user(), x, z);
}
});
}
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.NAMED_SOUND, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String sound = wrapper.get(Type.STRING, 0).replace("minecraft:", "");
String newSoundId = NamedSoundRewriter.getNewId(sound);
wrapper.set(Type.STRING, 0, newSoundId);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.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);
BlockStorage storage = wrapper.user().get(BlockStorage.class);
Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
Chunk1_13Type type1_13 = new Chunk1_13Type(clientWorld);
Chunk chunk = wrapper.read(type);
wrapper.write(type1_13, chunk);
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
for (int p = 0; p < section.getPaletteSize(); p++) {
int old = section.getPaletteEntry(p);
int newId = toNewId(old);
section.setPaletteEntry(p, newId);
}
boolean willSaveToStorage = false;
for (int p = 0; p < section.getPaletteSize(); p++) {
int newId = section.getPaletteEntry(p);
if (storage.isWelcome(newId)) {
willSaveToStorage = true;
break;
}
}
boolean willSaveConnection = false;
if (Via.getConfig().isServersideBlockConnections() && ConnectionData.needStoreBlocks()) {
for (int p = 0; p < section.getPaletteSize(); p++) {
int newId = section.getPaletteEntry(p);
if (ConnectionData.isWelcome(newId)) {
willSaveConnection = true;
break;
}
}
}
if (willSaveToStorage) {
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);
if (storage.isWelcome(block)) {
storage.store(new Position((x + (chunk.getX() << 4)), (short) (y + (i << 4)), (z + (chunk.getZ() << 4))), block);
}
}
}
}
}
if (willSaveConnection) {
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);
if (ConnectionData.isWelcome(block)) {
ConnectionData.blockConnectionProvider.storeBlock(wrapper.user(), x + (chunk.getX() << 4), y + (i << 4), z + (chunk.getZ() << 4), block);
}
}
}
}
}
}
// Rewrite biome id 255 to plains
if (chunk.isBiomeData()) {
int latestBiomeWarn = Integer.MIN_VALUE;
for (int i = 0; i < 256; i++) {
int biome = chunk.getBiomeData()[i];
if (!VALID_BIOMES.contains(biome)) {
if (// is it generated naturally? *shrug*
biome != 255 && latestBiomeWarn != biome) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Received invalid biome id " + biome);
}
latestBiomeWarn = biome;
}
// Plains
chunk.getBiomeData()[i] = 1;
}
}
}
// Rewrite BlockEntities to normal blocks
BlockEntityProvider provider = Via.getManager().getProviders().get(BlockEntityProvider.class);
final Iterator<CompoundTag> iterator = chunk.getBlockEntities().iterator();
while (iterator.hasNext()) {
CompoundTag tag = iterator.next();
int newId = provider.transform(wrapper.user(), null, tag, false);
if (newId != -1) {
int x = ((NumberTag) tag.get("x")).asInt();
int y = ((NumberTag) tag.get("y")).asInt();
int z = ((NumberTag) tag.get("z")).asInt();
Position position = new Position(x, (short) y, z);
// Store the replacement blocks for blockupdates
BlockStorage.ReplacementData replacementData = storage.get(position);
if (replacementData != null) {
replacementData.setReplacement(newId);
}
chunk.getSections()[y >> 4].setFlatBlock(x & 0xF, y & 0xF, z & 0xF, newId);
}
final Tag idTag = tag.get("id");
if (idTag instanceof StringTag) {
// No longer block entities
final String id = ((StringTag) idTag).getValue();
if (id.equals("minecraft:noteblock") || id.equals("minecraft:flower_pot")) {
iterator.remove();
}
}
}
if (Via.getConfig().isServersideBlockConnections()) {
ConnectionData.connectBlocks(wrapper.user(), chunk);
// Workaround for packet order issue
wrapper.send(Protocol1_13To1_12_2.class);
wrapper.cancel();
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
ConnectionData.updateChunkSectionNeighbours(wrapper.user(), chunk.getX(), chunk.getZ(), i);
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.SPAWN_PARTICLE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Particle ID
map(Type.INT);
// 1 - Long Distance
map(Type.BOOLEAN);
// 2 - X
map(Type.FLOAT);
// 3 - Y
map(Type.FLOAT);
// 4 - Z
map(Type.FLOAT);
// 5 - Offset X
map(Type.FLOAT);
// 6 - Offset Y
map(Type.FLOAT);
// 7 - Offset Z
map(Type.FLOAT);
// 8 - Particle Data
map(Type.FLOAT);
// 9 - Particle Count
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int particleId = wrapper.get(Type.INT, 0);
// Get the data (Arrays are overrated)
int dataCount = 0;
// Particles with 1 data [BlockCrack,BlockDust,FallingDust]
if (particleId == 37 || particleId == 38 || particleId == 46)
dataCount = 1;
else // Particles with 2 data [IconCrack]
if (particleId == 36)
dataCount = 2;
Integer[] data = new Integer[dataCount];
for (int i = 0; i < data.length; i++) data[i] = wrapper.read(Type.VAR_INT);
Particle particle = ParticleRewriter.rewriteParticle(particleId, data);
// Cancel if null or completely removed
if (particle == null || particle.getId() == -1) {
wrapper.cancel();
return;
}
// Handle reddust particle color
if (particle.getId() == 11) {
int count = wrapper.get(Type.INT, 1);
float speed = wrapper.get(Type.FLOAT, 6);
// Only handle for count = 0
if (count == 0) {
wrapper.set(Type.INT, 1, 1);
wrapper.set(Type.FLOAT, 6, 0f);
List<Particle.ParticleData> arguments = particle.getArguments();
for (int i = 0; i < 3; i++) {
// RGB values are represented by the X/Y/Z offset
float colorValue = wrapper.get(Type.FLOAT, i + 3) * speed;
if (colorValue == 0 && i == 0) {
// https://minecraft.gamepedia.com/User:Alphappy/reddust
colorValue = 1;
}
arguments.get(i).setValue(colorValue);
wrapper.set(Type.FLOAT, i + 3, 0f);
}
}
}
wrapper.set(Type.INT, 0, particle.getId());
for (Particle.ParticleData particleData : particle.getArguments()) wrapper.write(particleData.getType(), particleData.getValue());
}
});
}
});
}
use of com.viaversion.viaversion.api.type.types.Particle in project ViaVersion by ViaVersion.
the class ParticleRewriter method iconcrackHandler.
// Rewrite IconCrack items to new format :)
private static ParticleDataHandler iconcrackHandler() {
return new ParticleDataHandler() {
@Override
public Particle handler(Particle particle, Integer[] data) {
Item item;
if (data.length == 1)
item = new DataItem(data[0], (byte) 1, (short) 0, null);
else if (data.length == 2)
item = new DataItem(data[0], (byte) 1, data[1].shortValue(), null);
else
return particle;
// Transform to new Item
Via.getManager().getProtocolManager().getProtocol(Protocol1_13To1_12_2.class).getItemRewriter().handleItemToClient(item);
// Item Slot The item that will be used.
particle.getArguments().add(new Particle.ParticleData(Type.FLAT_ITEM, item));
return particle;
}
};
}
use of com.viaversion.viaversion.api.type.types.Particle in project ViaVersion by ViaVersion.
the class EntityRewriter method rewriteParticle.
protected void rewriteParticle(Particle particle) {
ParticleMappings mappings = protocol.getMappingData().getParticleMappings();
int id = particle.getId();
if (mappings.isBlockParticle(id)) {
Particle.ParticleData data = particle.getArguments().get(0);
data.setValue(protocol.getMappingData().getNewBlockStateId(data.get()));
} else if (mappings.isItemParticle(id) && protocol.getItemRewriter() != null) {
Particle.ParticleData data = particle.getArguments().get(0);
Item item = data.get();
protocol.getItemRewriter().handleItemToClient(item);
}
particle.setId(protocol.getMappingData().getNewParticleId(id));
}
Aggregations