Use instance of ThreadLocalRandom for particle offsets

Random instances are synchronized meaning this was a potential deadlock situation.
This commit is contained in:
Camotoy 2021-08-26 21:43:53 -04:00
parent 26a778fd77
commit 3c18eb44aa
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
2 changed files with 6 additions and 6 deletions

View file

@ -43,7 +43,6 @@ import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
public class QueryPacketHandler { public class QueryPacketHandler {
@ -265,7 +264,7 @@ public class QueryPacketHandler {
public void regenerateToken() { public void regenerateToken() {
byte[] token = new byte[16]; byte[] token = new byte[16];
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
token[i] = (byte) new Random().nextInt(255); token[i] = (byte) ThreadLocalRandom.current().nextInt(255);
} }
this.token = token; this.token = token;

View file

@ -43,11 +43,11 @@ import org.geysermc.connector.registry.type.ParticleMapping;
import org.geysermc.connector.utils.DimensionUtils; import org.geysermc.connector.utils.DimensionUtils;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function; import java.util.function.Function;
@Translator(packet = ServerSpawnParticlePacket.class) @Translator(packet = ServerSpawnParticlePacket.class)
public class JavaSpawnParticleTranslator extends PacketTranslator<ServerSpawnParticlePacket> { public class JavaSpawnParticleTranslator extends PacketTranslator<ServerSpawnParticlePacket> {
private final Random random = new Random();
@Override @Override
public void translate(ServerSpawnParticlePacket packet, GeyserSession session) { public void translate(ServerSpawnParticlePacket packet, GeyserSession session) {
@ -58,10 +58,11 @@ public class JavaSpawnParticleTranslator extends PacketTranslator<ServerSpawnPar
Vector3f position = Vector3f.from(packet.getX(), packet.getY(), packet.getZ()); Vector3f position = Vector3f.from(packet.getX(), packet.getY(), packet.getZ());
session.sendUpstreamPacket(particleCreateFunction.apply(position)); session.sendUpstreamPacket(particleCreateFunction.apply(position));
} else { } else {
Random random = ThreadLocalRandom.current();
for (int i = 0; i < packet.getAmount(); i++) { for (int i = 0; i < packet.getAmount(); i++) {
double offsetX = this.random.nextGaussian() * (double) packet.getOffsetX(); double offsetX = random.nextGaussian() * (double) packet.getOffsetX();
double offsetY = this.random.nextGaussian() * (double) packet.getOffsetY(); double offsetY = random.nextGaussian() * (double) packet.getOffsetY();
double offsetZ = this.random.nextGaussian() * (double) packet.getOffsetZ(); double offsetZ = random.nextGaussian() * (double) packet.getOffsetZ();
Vector3f position = Vector3f.from(packet.getX() + offsetX, packet.getY() + offsetY, packet.getZ() + offsetZ); Vector3f position = Vector3f.from(packet.getX() + offsetX, packet.getY() + offsetY, packet.getZ() + offsetZ);
session.sendUpstreamPacket(particleCreateFunction.apply(position)); session.sendUpstreamPacket(particleCreateFunction.apply(position));