|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package org.geysermc.connector.network.translators.java.world; |
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; |
|
import com.github.steveice10.mc.protocol.data.game.world.particle.*; |
|
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerSpawnParticlePacket; |
|
import com.nukkitx.math.vector.Vector3f; |
|
import com.nukkitx.protocol.bedrock.BedrockPacket; |
|
import com.nukkitx.protocol.bedrock.data.LevelEventType; |
|
import com.nukkitx.protocol.bedrock.data.inventory.ItemData; |
|
import com.nukkitx.protocol.bedrock.packet.LevelEventPacket; |
|
import com.nukkitx.protocol.bedrock.packet.SpawnParticleEffectPacket; |
|
import org.geysermc.connector.network.session.GeyserSession; |
|
import org.geysermc.connector.network.translators.PacketTranslator; |
|
import org.geysermc.connector.network.translators.Translator; |
|
import org.geysermc.connector.network.translators.item.ItemTranslator; |
|
import org.geysermc.connector.registry.Registries; |
|
import org.geysermc.connector.registry.type.ParticleMapping; |
|
import org.geysermc.connector.utils.DimensionUtils; |
|
|
|
import java.util.Random; |
|
import java.util.concurrent.ThreadLocalRandom; |
|
import java.util.function.Function; |
|
|
|
@Translator(packet = ServerSpawnParticlePacket.class) |
|
public class JavaSpawnParticleTranslator extends PacketTranslator<ServerSpawnParticlePacket> { |
|
|
|
@Override |
|
public void translate(GeyserSession session, ServerSpawnParticlePacket packet) { |
|
Function<Vector3f, BedrockPacket> particleCreateFunction = createParticle(session, packet.getParticle()); |
|
if (particleCreateFunction != null) { |
|
if (packet.getAmount() == 0) { |
|
|
|
Vector3f position = Vector3f.from(packet.getX(), packet.getY(), packet.getZ()); |
|
session.sendUpstreamPacket(particleCreateFunction.apply(position)); |
|
} else { |
|
Random random = ThreadLocalRandom.current(); |
|
for (int i = 0; i < packet.getAmount(); i++) { |
|
double offsetX = random.nextGaussian() * (double) packet.getOffsetX(); |
|
double offsetY = random.nextGaussian() * (double) packet.getOffsetY(); |
|
double offsetZ = random.nextGaussian() * (double) packet.getOffsetZ(); |
|
Vector3f position = Vector3f.from(packet.getX() + offsetX, packet.getY() + offsetY, packet.getZ() + offsetZ); |
|
|
|
session.sendUpstreamPacket(particleCreateFunction.apply(position)); |
|
} |
|
} |
|
} else { |
|
|
|
session.getConnector().getLogger().debug("Unhandled particle packet: " + packet); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Function<Vector3f, BedrockPacket> createParticle(GeyserSession session, Particle particle) { |
|
switch (particle.getType()) { |
|
case BLOCK: { |
|
int blockState = session.getBlockMappings().getBedrockBlockId(((BlockParticleData) particle.getData()).getBlockState()); |
|
return (position) -> { |
|
LevelEventPacket packet = new LevelEventPacket(); |
|
packet.setType(LevelEventType.PARTICLE_CRACK_BLOCK); |
|
packet.setPosition(position); |
|
packet.setData(blockState); |
|
return packet; |
|
}; |
|
} |
|
case FALLING_DUST: { |
|
int blockState = session.getBlockMappings().getBedrockBlockId(((FallingDustParticleData) particle.getData()).getBlockState()); |
|
return (position) -> { |
|
LevelEventPacket packet = new LevelEventPacket(); |
|
|
|
|
|
packet.setType(LevelEventType.PARTICLE_FALLING_DUST); |
|
packet.setData(blockState); |
|
packet.setPosition(position); |
|
return packet; |
|
}; |
|
} |
|
case ITEM: { |
|
ItemStack javaItem = ((ItemParticleData) particle.getData()).getItemStack(); |
|
ItemData bedrockItem = ItemTranslator.translateToBedrock(session, javaItem); |
|
int data = bedrockItem.getId() << 16 | bedrockItem.getDamage(); |
|
return (position) -> { |
|
LevelEventPacket packet = new LevelEventPacket(); |
|
packet.setType(LevelEventType.PARTICLE_ITEM_BREAK); |
|
packet.setData(data); |
|
packet.setPosition(position); |
|
return packet; |
|
}; |
|
} |
|
case DUST: |
|
case DUST_COLOR_TRANSITION: { |
|
DustParticleData data = (DustParticleData) particle.getData(); |
|
int r = (int) (data.getRed() * 255); |
|
int g = (int) (data.getGreen() * 255); |
|
int b = (int) (data.getBlue() * 255); |
|
int rgbData = ((0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); |
|
return (position) -> { |
|
LevelEventPacket packet = new LevelEventPacket(); |
|
packet.setType(LevelEventType.PARTICLE_FALLING_DUST); |
|
packet.setData(rgbData); |
|
packet.setPosition(position); |
|
return packet; |
|
}; |
|
} |
|
default: { |
|
ParticleMapping particleMapping = Registries.PARTICLES.get(particle.getType()); |
|
if (particleMapping == null) { |
|
return null; |
|
} |
|
|
|
if (particleMapping.getLevelEventType() != null) { |
|
return (position) -> { |
|
LevelEventPacket packet = new LevelEventPacket(); |
|
packet.setType(particleMapping.getLevelEventType()); |
|
packet.setPosition(position); |
|
return packet; |
|
}; |
|
} else if (particleMapping.getIdentifier() != null) { |
|
int dimensionId = DimensionUtils.javaToBedrock(session.getDimension()); |
|
return (position) -> { |
|
SpawnParticleEffectPacket stringPacket = new SpawnParticleEffectPacket(); |
|
stringPacket.setIdentifier(particleMapping.getIdentifier()); |
|
stringPacket.setDimensionId(dimensionId); |
|
stringPacket.setPosition(position); |
|
return stringPacket; |
|
}; |
|
} else { |
|
return null; |
|
} |
|
} |
|
} |
|
} |
|
} |