From 283843af79cbe4a862cae078722dc1e95b48f197 Mon Sep 17 00:00:00 2001 From: chronosacaria Date: Sat, 20 Apr 2024 20:21:09 -0400 Subject: [PATCH] Removed Unsafe Random - Changed from Java's `Random` to `Random.createThreadSafe()` --- src/main/java/chronosacaria/mcdar/api/AOEHelper.java | 7 +++---- .../java/chronosacaria/mcdar/api/CleanlinessHelper.java | 5 +++-- .../chronosacaria/mcdar/artifacts/EnchantedGrassItem.java | 3 +-- .../java/chronosacaria/mcdar/effects/ArtifactEffects.java | 4 +--- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/chronosacaria/mcdar/api/AOEHelper.java b/src/main/java/chronosacaria/mcdar/api/AOEHelper.java index ca3704e..668ea93 100644 --- a/src/main/java/chronosacaria/mcdar/api/AOEHelper.java +++ b/src/main/java/chronosacaria/mcdar/api/AOEHelper.java @@ -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; @@ -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, diff --git a/src/main/java/chronosacaria/mcdar/api/CleanlinessHelper.java b/src/main/java/chronosacaria/mcdar/api/CleanlinessHelper.java index 0efec02..c536fc4 100644 --- a/src/main/java/chronosacaria/mcdar/api/CleanlinessHelper.java +++ b/src/main/java/chronosacaria/mcdar/api/CleanlinessHelper.java @@ -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); diff --git a/src/main/java/chronosacaria/mcdar/artifacts/EnchantedGrassItem.java b/src/main/java/chronosacaria/mcdar/artifacts/EnchantedGrassItem.java index 7b98d0e..058d6de 100644 --- a/src/main/java/chronosacaria/mcdar/artifacts/EnchantedGrassItem.java +++ b/src/main/java/chronosacaria/mcdar/artifacts/EnchantedGrassItem.java @@ -16,7 +16,6 @@ import net.minecraft.world.World; import java.util.List; -import java.util.Random; public class EnchantedGrassItem extends ArtifactSummoningItem{ public EnchantedGrassItem() { @@ -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())) { diff --git a/src/main/java/chronosacaria/mcdar/effects/ArtifactEffects.java b/src/main/java/chronosacaria/mcdar/effects/ArtifactEffects.java index 87ec7fb..783c88b 100644 --- a/src/main/java/chronosacaria/mcdar/effects/ArtifactEffects.java +++ b/src/main/java/chronosacaria/mcdar/effects/ArtifactEffects.java @@ -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; @@ -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); } }