Skip to content

Commit 5bb7469

Browse files
committed
fix: change random to threadlocalrandom where possible
1 parent 77c6197 commit 5bb7469

File tree

8 files changed

+32
-13
lines changed

8 files changed

+32
-13
lines changed

contributors.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,4 @@ jumpyrocks=#0f1115,#d32f2f,#f6c16b,#ffeb3b,#ffffff,#7c3aed,#2aefff
300300
lightlike=#006600,#ff33ff
301301
1v1c3nes=#6c7073,#296cf2
302302
thefayewitch=#a117ad,#3076d1,#c44388,#000000,#ffffff,#c2a804
303+
zhichaoxi=#A83E3E,#ECB212,#55BCC4,#AD248D

src/main/java/vazkii/psi/common/entity/EntitySpellGrenade.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import vazkii.psi.api.spell.SpellContext;
2727

2828
import java.util.Optional;
29+
import java.util.concurrent.ThreadLocalRandom;
2930

3031
public class EntitySpellGrenade extends EntitySpellProjectile {
3132
boolean sound = false;
@@ -76,9 +77,9 @@ public void doExplosion() {
7677
double d1 = getCommandSenderWorld().random.nextGaussian() * m;
7778
double d2 = getCommandSenderWorld().random.nextGaussian() * m;
7879

79-
double x = getX() + 0.75 * getCommandSenderWorld().random.nextFloat() - 0.375;
80-
double y = getY() + 0.5 * getCommandSenderWorld().random.nextFloat();
81-
double z = getZ() + 0.75 * getCommandSenderWorld().random.nextFloat() - 0.375;
80+
double x = getX() + 0.75 * ThreadLocalRandom.current().nextFloat() - 0.375;
81+
double y = getY() + 0.5 * ThreadLocalRandom.current().nextFloat();
82+
double z = getZ() + 0.75 * ThreadLocalRandom.current().nextFloat() - 0.375;
8283
getCommandSenderWorld().addParticle(ParticleTypes.EXPLOSION, x, y, z, d0, d1, d2);
8384
}
8485
}

src/main/java/vazkii/psi/common/item/ItemCAD.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import vazkii.psi.common.spell.trick.block.PieceTrickBreakBlock;
7373

7474
import java.util.*;
75+
import java.util.concurrent.ThreadLocalRandom;
7576
import java.util.function.Consumer;
7677
import java.util.function.Predicate;
7778
import java.util.regex.Matcher;
@@ -481,13 +482,13 @@ public boolean craft(ItemStack cad, Player player, PieceCraftingTrick craftingTr
481482
ItemStack outCopy = recipe.get().value().getResultItem(RegistryAccess.EMPTY).copy();
482483
int count = stack.getCount() * outCopy.getCount();
483484
while(count > 64) {
484-
int dropCount = world.getRandom().nextInt(32) + 32;
485+
int dropCount = ThreadLocalRandom.current().nextInt(32) + 32;
485486
ItemEntity drop = new ItemEntity(world, item.getX(), item.getY(), item.getZ(),
486487
new ItemStack(outCopy.getItem(), dropCount));
487488
Vec3 motion = item.getDeltaMovement();
488-
drop.setDeltaMovement(motion.x() + (world.getRandom().nextFloat() - 0.5D) / 5,
489-
motion.y() + (world.getRandom().nextFloat()) / 10,
490-
motion.z() + (world.getRandom().nextFloat() - 0.5D) / 5);
489+
drop.setDeltaMovement(motion.x() + (ThreadLocalRandom.current().nextFloat() - 0.5D) / 5,
490+
motion.y() + (ThreadLocalRandom.current().nextFloat()) / 10,
491+
motion.z() + (ThreadLocalRandom.current().nextFloat() - 0.5D) / 5);
491492
world.addFreshEntity(drop);
492493
count -= dropCount;
493494
}

src/main/java/vazkii/psi/common/network/message/MessageVisualEffect.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import vazkii.psi.common.Psi;
2222

23+
import java.util.concurrent.ThreadLocalRandom;
24+
2325
public record MessageVisualEffect(int color, double x, double y, double z, double width, double height, double offset,
2426
int effectType) implements CustomPacketPayload {
2527

@@ -82,9 +84,9 @@ public void handle(IPayloadContext ctx) {
8284
double d2 = world.random.nextGaussian() * m;
8385

8486
world.addParticle(ParticleTypes.EXPLOSION,
85-
x + world.random.nextFloat() * width * 2.0F - width - d0 * d3,
86-
y + world.random.nextFloat() * height - d1 * d3,
87-
z + world.random.nextFloat() * width * 2.0F - width - d2 * d3, d0, d1, d2);
87+
x + ThreadLocalRandom.current().nextFloat() * width * 2.0F - width - d0 * d3,
88+
y + ThreadLocalRandom.current().nextFloat() * height - d1 * d3,
89+
z + ThreadLocalRandom.current().nextFloat() * width * 2.0F - width - d2 * d3, d0, d1, d2);
8890
}
8991
}
9092
}

src/main/java/vazkii/psi/common/spell/operator/entity/PieceOperatorRandomEntity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import vazkii.psi.api.spell.piece.PieceOperator;
1919
import vazkii.psi.api.spell.wrapper.EntityListWrapper;
2020

21+
import java.util.concurrent.ThreadLocalRandom;
22+
2123
public class PieceOperatorRandomEntity extends PieceOperator {
2224

2325
SpellParam<EntityListWrapper> list;
@@ -38,7 +40,7 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
3840
throw new SpellRuntimeException(SpellRuntimeException.NULL_TARGET);
3941
}
4042

41-
return listVal.get(context.caster.getCommandSenderWorld().random.nextInt(listVal.size()));
43+
return listVal.get(ThreadLocalRandom.current().nextInt(listVal.size()));
4244
}
4345

4446
@Override

src/main/java/vazkii/psi/common/spell/operator/number/PieceOperatorRandom.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import vazkii.psi.api.spell.param.ParamNumber;
1616
import vazkii.psi.api.spell.piece.PieceOperator;
1717

18+
import java.util.concurrent.ThreadLocalRandom;
19+
1820
public class PieceOperatorRandom extends PieceOperator {
1921

2022
SpellParam<Number> max;
@@ -39,7 +41,7 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
3941
throw new SpellRuntimeException(SpellRuntimeException.NEGATIVE_NUMBER);
4042
}
4143

42-
return (double) (context.caster.getCommandSenderWorld().random.nextInt(maxVal - minVal) + minVal);
44+
return (double) (ThreadLocalRandom.current().nextInt(maxVal - minVal) + minVal);
4345
}
4446

4547
@Override

src/main/java/vazkii/psi/common/spell/trick/PieceTrickTorrent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import vazkii.psi.api.spell.param.ParamVector;
3333
import vazkii.psi.api.spell.piece.PieceTrick;
3434

35+
import java.util.concurrent.ThreadLocalRandom;
36+
3537
public class PieceTrickTorrent extends PieceTrick {
3638

3739
SpellParam<Vector3> position;
@@ -54,7 +56,7 @@ public static boolean placeWater(@Nullable Player playerIn, Level worldIn, Block
5456
int i = pos.getX();
5557
int j = pos.getY();
5658
int k = pos.getZ();
57-
worldIn.playSound(playerIn, pos, SoundEvents.FIRE_EXTINGUISH, SoundSource.BLOCKS, 0.5F, 2.6F + (worldIn.random.nextFloat() - worldIn.random.nextFloat()) * 0.8F);
59+
worldIn.playSound(playerIn, pos, SoundEvents.FIRE_EXTINGUISH, SoundSource.BLOCKS, 0.5F, 2.6F + (ThreadLocalRandom.current().nextFloat() - ThreadLocalRandom.current().nextFloat()) * 0.8F);
5860

5961
for(int l = 0; l < 8; ++l) {
6062
worldIn.addParticle(ParticleTypes.LARGE_SMOKE, (double) i + Math.random(), (double) j + Math.random(), (double) k + Math.random(), 0.0D, 0.0D, 0.0D);

web/changelog.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
1.21-108
2+
* Fixed random operations not being thread-safe (Kamefrede)
3+
* Fixed Spell Import message from old versions not having the correct text (Kamefrede)
4+
* Fixed CAD Data being wrongly shared between all CADs (Kamefrede)
5+
* Fixed Vector3 conversion to BlockPos truncating instead of rounding (Kamefrede)
6+
* Fixed Psi Overload death message not having the correct text (Kamefrede)
7+
* Fixed crash when moving CAD in inventory while loopcast is active (Kamefrede)
8+
* Fixed crash when a player removes a CAD from an assembbler with another player looking at it (Kamefrede)
19
1.21-107
210
* [API] Make Total PSI and PSI Regeneration configurable attributes (lightsing)
311
* Fixed Trick: Break Block not playing particles correctly (zhichaoxi2006)

0 commit comments

Comments
 (0)