Skip to content

Commit

Permalink
Removed Unsafe Random
Browse files Browse the repository at this point in the history
 - Changed from Java's `Random` to `Random.createThreadSafe()`
  • Loading branch information
chronosacaria committed Apr 21, 2024
1 parent 560e99e commit 283843a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/main/java/chronosacaria/mcdar/api/AOEHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import net.minecraft.world.World;

import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Predicate;

Expand Down Expand Up @@ -138,14 +137,14 @@ public static void knockbackNearbyEnemies(PlayerEntity user, LivingEntity nearby
for (
zRatio = user.getZ() - nearbyEntity.getZ();
xRatio * xRatio + zRatio < 1.0E-4D;
zRatio = (Math.random() - Math.random()) * 0.01D) {
xRatio = (Math.random() - Math.random()) * 0.01D;
zRatio = (CleanlinessHelper.RANDOM.nextDouble() - CleanlinessHelper.RANDOM.nextDouble()) * 0.01D) {
xRatio = (CleanlinessHelper.RANDOM.nextDouble() - CleanlinessHelper.RANDOM.nextDouble()) * 0.01D;
}
nearbyEntity.takeKnockback(0.4F * knockbackMultiplier, xRatio, zRatio);
}

public static void satchelOfElementsEffects(PlayerEntity user) {
int effectInt = (new Random()).nextInt(3);
int effectInt = (CleanlinessHelper.RANDOM.nextInt(3));

if (effectInt == 0){ // BURNING
for (LivingEntity nearbyEntity : getEntitiesByPredicate(user, 5,
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/chronosacaria/mcdar/api/CleanlinessHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.random.Random;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Locale;
import java.util.Random;

public class CleanlinessHelper {

static final Random RANDOM = new Random();
@SuppressWarnings("deprecation")
public static final net.minecraft.util.math.random.Random RANDOM = Random.createThreadSafe();

public static void playCenteredSound(LivingEntity center, SoundEvent soundEvent, float volume, float pitch) {
playCenteredSound(center, soundEvent, SoundCategory.PLAYERS, volume, pitch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import net.minecraft.world.World;

import java.util.List;
import java.util.Random;

public class EnchantedGrassItem extends ArtifactSummoningItem{
public EnchantedGrassItem() {
Expand All @@ -33,7 +32,7 @@ public ActionResult useOnBlock (ItemUsageContext itemUsageContext){

if (itemUsageContextPlayer != null) {

int effectInt = (new Random()).nextInt(3);
int effectInt = CleanlinessHelper.RANDOM.nextInt(3);
SheepEntity sheep = SummoningHelper.SHEEP.get(effectInt).create(serverWorld);

if (SummoningHelper.mcdar$summonSummonableEntity(sheep, itemUsageContextPlayer, itemUsageContext.getBlockPos())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundEvents;

import java.util.Random;

public class ArtifactEffects {
private static final float EXPLOSION_RADIUS = 3.0F;

Expand Down Expand Up @@ -55,7 +53,7 @@ public static void enchantersTomeEffects(PlayerEntity user) {
(nearbyEntity) -> AbilityHelper.isPetOf(nearbyEntity, user))){
StatusEffect[] statuses = {StatusEffects.HASTE, StatusEffects.STRENGTH, StatusEffects.SPEED};
StatusEffectInstance statusEffectInstance =
new StatusEffectInstance(statuses[new Random().nextInt(statuses.length)], 100, 2);
new StatusEffectInstance(statuses[CleanlinessHelper.RANDOM.nextInt(statuses.length)], 100, 2);
nearbyEntity.addStatusEffect(statusEffectInstance);
}
}
Expand Down

0 comments on commit 283843a

Please sign in to comment.