Skip to content

Commit

Permalink
Remove Uses of Level's Random. Might need testing
Browse files Browse the repository at this point in the history
  • Loading branch information
iron431 committed Oct 26, 2023
1 parent cffe5ba commit 4d4f33b
Show file tree
Hide file tree
Showing 44 changed files with 109 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,9 @@ public void onCast(Level level, int spellLevel, LivingEntity entity, MagicData p

protected void playSound(Optional<SoundEvent> sound, Entity entity, boolean playDefaultSound) {
if (sound.isPresent()) {
entity.playSound(sound.get(), 2.0f, .9f + entity.level.random.nextFloat() * .2f);
entity.playSound(sound.get(), 2.0f, .9f + Utils.random.nextFloat() * .2f);
} else if (playDefaultSound) {
entity.playSound(defaultCastSound(), 2.0f, .9f + entity.level.random.nextFloat() * .2f);
entity.playSound(defaultCastSound(), 2.0f, .9f + Utils.random.nextFloat() * .2f);
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/io/redspace/ironsspellbooks/api/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.ServerLevelAccessor;
import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.level.levelgen.ThreadSafeLegacyRandomSource;
import net.minecraft.world.phys.*;
import net.minecraftforge.entity.PartEntity;
import org.jetbrains.annotations.NotNull;
Expand All @@ -64,6 +65,7 @@

public class Utils {

public static final RandomSource random = RandomSource.createThreadSafe();
public static String getStackTraceAsString() {
var trace = Arrays.stream(Thread.currentThread().getStackTrace());
StringBuffer sb = new StringBuffer();
Expand Down Expand Up @@ -369,10 +371,10 @@ public static void throwTarget(LivingEntity attacker, LivingEntity target, float
if (!(d2 <= 0.0D)) {
double d3 = target.getX() - attacker.getX();
double d4 = target.getZ() - attacker.getZ();
float f = (float) (attacker.level.random.nextInt(21) - 10);
double d5 = d2 * (double) (attacker.level.random.nextFloat() * 0.5F + 0.2F);
float f = (float) (Utils.random.nextInt(21) - 10);
double d5 = d2 * (double) (Utils.random.nextFloat() * 0.5F + 0.2F);
Vec3 vec3 = (new Vec3(d3, 0.0D, d4)).normalize().scale(d5).yRot(f);
double d6 = d2 * (double) attacker.level.random.nextFloat() * 0.5D;
double d6 = d2 * (double) Utils.random.nextFloat() * 0.5D;
target.push(vec3.x, d6, vec3.z);
target.hurtMarked = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.redspace.ironsspellbooks.block;

import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.capabilities.magic.MagicManager;
import io.redspace.ironsspellbooks.damage.DamageSources;
import io.redspace.ironsspellbooks.util.ParticleHelper;
Expand Down Expand Up @@ -54,7 +55,7 @@ public static void attemptCookEntity(BlockState blockState, Level level, BlockPo
if (entity instanceof LivingEntity livingEntity && livingEntity.getBoundingBox().intersects(cauldron.getInteractionShape(blockState, level, pos).bounds().move(pos))) {
if (livingEntity.hurt(DamageSources.CAULDRON, 2)) {
MagicManager.spawnParticles(level, ParticleHelper.BLOOD, entity.getX(), entity.getY() + entity.getBbHeight() / 2, entity.getZ(), 20, .05, .05, .05, .1, false);
if (level.random.nextDouble() <= .5 && !isCauldronFull(blockState)) {
if (Utils.random.nextDouble() <= .5 && !isCauldronFull(blockState)) {
execution.execute();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void serverTick(Level level, BlockPos pos, BlockState blockState,
cauldronTile.cooktimes[i] = 0;
}
}
var random = level.getRandom();
var random = Utils.random;
if (AlchemistCauldronBlock.isBoiling(blockState)) {
float waterLevel = Mth.lerp(AlchemistCauldronBlock.getLevel(blockState) / (float) AlchemistCauldronBlock.MAX_LEVELS, .25f, .9f);
MagicManager.spawnParticles(level, ParticleTypes.BUBBLE_POP, pos.getX() + Mth.randomBetween(random, .2f, .8f), pos.getY() + waterLevel, pos.getZ() + Mth.randomBetween(random, .2f, .8f), 1, 0, 0, 0, 0, false);
Expand Down Expand Up @@ -122,7 +122,7 @@ public void meltComponent(ItemStack itemStack) {
boolean shouldMelt = false;
boolean success = true;
if (itemStack.is(ItemRegistry.SCROLL.get()) && !isFull(resultItems)) {
if (level.random.nextFloat() < ServerConfigs.SCROLL_RECYCLE_CHANCE.get()) {
if (Utils.random.nextFloat() < ServerConfigs.SCROLL_RECYCLE_CHANCE.get()) {
ItemStack result = new ItemStack(getInkFromScroll(itemStack));
appendItem(resultItems, result);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.redspace.ironsspellbooks.block.arcane_anvil;

import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.gui.arcane_anvil.ArcaneAnvilMenu;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -54,7 +55,7 @@ protected void falling(FallingBlockEntity pFallingEntity) {

public void onLand(Level pLevel, BlockPos pPos, BlockState pState, BlockState pReplaceableState, FallingBlockEntity pFallingBlock) {
if (!pFallingBlock.isSilent()) {
pLevel.playSound(null,pPos, SoundEvents.ANVIL_LAND, SoundSource.BLOCKS, .3F, pLevel.random.nextFloat() * 0.1F + 0.9F);
pLevel.playSound(null,pPos, SoundEvents.ANVIL_LAND, SoundSource.BLOCKS, .3F, Utils.random.nextFloat() * 0.1F + 0.9F);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.redspace.ironsspellbooks.entity.mobs;

import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.capabilities.magic.MagicManager;
import io.redspace.ironsspellbooks.entity.mobs.goals.GenericFollowOwnerGoal;
import io.redspace.ironsspellbooks.registries.EntityRegistry;
Expand Down Expand Up @@ -27,13 +28,13 @@
public class SummonedHorse extends AbstractHorse implements MagicSummon {
public SummonedHorse(EntityType<? extends AbstractHorse> pEntityType, Level pLevel) {
super(pEntityType, pLevel);
//randomizeAttributes(level.random);
//randomizeAttributes(Utils.random);

}

public SummonedHorse(Level pLevel) {
this(EntityRegistry.SPECTRAL_STEED.get(), pLevel);
//randomizeAttributes(level.random);
//randomizeAttributes(Utils.random);

}

Expand Down Expand Up @@ -116,7 +117,7 @@ protected SoundEvent getAngrySound() {
public void spawnParticles() {

if (level.isClientSide) {
if (level.getRandom().nextFloat() < .25f) {
if (Utils.random.nextFloat() < .25f) {
float radius = .75f;
Vec3 vec = new Vec3(
random.nextFloat() * 2 * radius - radius,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected boolean isSunBurnTick() {

@Override
public SpawnGroupData finalizeSpawn(ServerLevelAccessor pLevel, DifficultyInstance pDifficulty, MobSpawnType pReason, @Nullable SpawnGroupData pSpawnData, @Nullable CompoundTag pDataTag) {
RandomSource randomsource = pLevel.getRandom();
RandomSource randomsource = Utils.random;
this.populateDefaultEquipmentSlots(randomsource, pDifficulty);
if (randomsource.nextDouble() < .3)
this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.IRON_SWORD));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected void defineSynchedData() {

@Override
public SpawnGroupData finalizeSpawn(ServerLevelAccessor pLevel, DifficultyInstance pDifficulty, MobSpawnType pReason, @Nullable SpawnGroupData pSpawnData, @Nullable CompoundTag pDataTag) {
RandomSource randomsource = pLevel.getRandom();
RandomSource randomsource = Utils.random;
this.populateDefaultEquipmentSlots(randomsource, pDifficulty);
if (randomsource.nextDouble() < .25)
this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.IRON_SWORD));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ protected void customServerAiStep() {
finishDrinkingPotion();
} else if (drinkTime % 4 == 0)
if (!this.isSilent())
this.level.playSound(null, this.getX(), this.getY(), this.getZ(), SoundEvents.GENERIC_DRINK, this.getSoundSource(), 1.0F, this.level.random.nextFloat() * 0.1F + 0.9F);
this.level.playSound(null, this.getX(), this.getY(), this.getZ(), SoundEvents.GENERIC_DRINK, this.getSoundSource(), 1.0F, Utils.random.nextFloat() * 0.1F + 0.9F);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected SoundEvent getStepSound() {

@Override
public SpawnGroupData finalizeSpawn(ServerLevelAccessor pLevel, DifficultyInstance pDifficulty, MobSpawnType pReason, @Nullable SpawnGroupData pSpawnData, @Nullable CompoundTag pDataTag) {
RandomSource randomsource = pLevel.getRandom();
RandomSource randomsource = Utils.random;
this.populateDefaultEquipmentSlots(randomsource, pDifficulty);
return pSpawnData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.redspace.ironsspellbooks.api.magic.MagicData;
import io.redspace.ironsspellbooks.api.registry.SpellRegistry;
import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.entity.mobs.abstract_spell_casting_mob.AbstractSpellCastingMob;
import io.redspace.ironsspellbooks.api.spells.AbstractSpell;
import net.minecraft.commands.arguments.EntityAnchorArgument;
Expand Down Expand Up @@ -90,8 +91,8 @@ public WizardAttackGoal setSpellQuality(float minSpellQuality, float maxSpellQua

public WizardAttackGoal setSingleUseSpell(AbstractSpell abstractSpell, int minDelay, int maxDelay, int minLevel, int maxLevel) {
this.singleUseSpell = abstractSpell;
this.singleUseDelay = mob.level.random.nextIntBetweenInclusive(minDelay, maxDelay);
this.singleUseLevel = mob.level.random.nextIntBetweenInclusive(minLevel, maxLevel);
this.singleUseDelay = Utils.random.nextIntBetweenInclusive(minDelay, maxDelay);
this.singleUseLevel = Utils.random.nextIntBetweenInclusive(minLevel, maxLevel);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.redspace.ironsspellbooks.entity.mobs.keeper;

import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.entity.mobs.AnimatedAttacker;
import io.redspace.ironsspellbooks.entity.mobs.abstract_spell_casting_mob.AbstractSpellCastingMob;
import io.redspace.ironsspellbooks.entity.mobs.goals.AttackAnimationData;
Expand Down Expand Up @@ -147,7 +148,7 @@ protected SoundEvent getDeathSound() {

@Override
public SpawnGroupData finalizeSpawn(ServerLevelAccessor pLevel, DifficultyInstance pDifficulty, MobSpawnType pReason, @Nullable SpawnGroupData pSpawnData, @Nullable CompoundTag pDataTag) {
RandomSource randomsource = pLevel.getRandom();
RandomSource randomsource = Utils.random;
this.populateDefaultEquipmentSlots(randomsource, pDifficulty);
return pSpawnData;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.redspace.ironsspellbooks.entity.mobs.necromancer;

import io.redspace.ironsspellbooks.api.registry.SpellRegistry;
import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.entity.mobs.abstract_spell_casting_mob.AbstractSpellCastingMob;
import io.redspace.ironsspellbooks.entity.mobs.goals.WizardAttackGoal;
import io.redspace.ironsspellbooks.entity.mobs.goals.WizardRecoverGoal;
Expand Down Expand Up @@ -80,7 +81,7 @@ protected SoundEvent getStepSound() {

@Override
public SpawnGroupData finalizeSpawn(ServerLevelAccessor pLevel, DifficultyInstance pDifficulty, MobSpawnType pReason, @Nullable SpawnGroupData pSpawnData, @Nullable CompoundTag pDataTag) {
RandomSource randomsource = pLevel.getRandom();
RandomSource randomsource = Utils.random;
this.populateDefaultEquipmentSlots(randomsource, pDifficulty);
return super.finalizeSpawn(pLevel, pDifficulty, pReason, pSpawnData, pDataTag);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.redspace.ironsspellbooks.entity.mobs.wizards.archevoker;

import io.redspace.ironsspellbooks.api.registry.SpellRegistry;
import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.entity.mobs.abstract_spell_casting_mob.AbstractSpellCastingMob;
import io.redspace.ironsspellbooks.entity.mobs.goals.*;
import io.redspace.ironsspellbooks.api.registry.AttributeRegistry;
Expand Down Expand Up @@ -61,7 +62,7 @@ protected void registerGoals() {

@Override
public SpawnGroupData finalizeSpawn(ServerLevelAccessor pLevel, DifficultyInstance pDifficulty, MobSpawnType pReason, @Nullable SpawnGroupData pSpawnData, @Nullable CompoundTag pDataTag) {
RandomSource randomsource = pLevel.getRandom();
RandomSource randomsource = Utils.random;
this.populateDefaultEquipmentSlots(randomsource, pDifficulty);
return super.finalizeSpawn(pLevel, pDifficulty, pReason, pSpawnData, pDataTag);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.redspace.ironsspellbooks.entity.mobs.wizards.cryomancer;

import io.redspace.ironsspellbooks.api.registry.SpellRegistry;
import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.entity.mobs.abstract_spell_casting_mob.AbstractSpellCastingMob;
import io.redspace.ironsspellbooks.entity.mobs.goals.PatrolNearLocationGoal;
import io.redspace.ironsspellbooks.entity.mobs.goals.SpellBarrageGoal;
Expand Down Expand Up @@ -56,7 +57,7 @@ protected void registerGoals() {

@Override
public SpawnGroupData finalizeSpawn(ServerLevelAccessor pLevel, DifficultyInstance pDifficulty, MobSpawnType pReason, @Nullable SpawnGroupData pSpawnData, @Nullable CompoundTag pDataTag) {
RandomSource randomsource = pLevel.getRandom();
RandomSource randomsource = Utils.random;
this.populateDefaultEquipmentSlots(randomsource, pDifficulty);
return super.finalizeSpawn(pLevel, pDifficulty, pReason, pSpawnData, pDataTag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.redspace.ironsspellbooks.IronsSpellbooks;
import io.redspace.ironsspellbooks.api.registry.SpellRegistry;
import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.entity.mobs.SupportMob;
import io.redspace.ironsspellbooks.entity.mobs.abstract_spell_casting_mob.AbstractSpellCastingMob;
import io.redspace.ironsspellbooks.entity.mobs.abstract_spell_casting_mob.NeutralWizard;
Expand Down Expand Up @@ -98,7 +99,7 @@ protected void registerGoals() {

@Override
public SpawnGroupData finalizeSpawn(ServerLevelAccessor pLevel, DifficultyInstance pDifficulty, MobSpawnType pReason, @Nullable SpawnGroupData pSpawnData, @Nullable CompoundTag pDataTag) {
RandomSource randomsource = pLevel.getRandom();
RandomSource randomsource = Utils.random;
this.populateDefaultEquipmentSlots(randomsource, pDifficulty);
this.setHome(this.blockPosition());
IronsSpellbooks.LOGGER.debug("Priest new home: {}", this.getHome());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.redspace.ironsspellbooks.entity.mobs.wizards.pyromancer;

import io.redspace.ironsspellbooks.api.registry.SpellRegistry;
import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.entity.mobs.abstract_spell_casting_mob.AbstractSpellCastingMob;
import io.redspace.ironsspellbooks.entity.mobs.goals.PatrolNearLocationGoal;
import io.redspace.ironsspellbooks.entity.mobs.goals.WizardAttackGoal;
Expand Down Expand Up @@ -57,7 +58,7 @@ protected void registerGoals() {

@Override
public SpawnGroupData finalizeSpawn(ServerLevelAccessor pLevel, DifficultyInstance pDifficulty, MobSpawnType pReason, @Nullable SpawnGroupData pSpawnData, @Nullable CompoundTag pDataTag) {
RandomSource randomsource = pLevel.getRandom();
RandomSource randomsource = Utils.random;
this.populateDefaultEquipmentSlots(randomsource, pDifficulty);
return super.finalizeSpawn(pLevel, pDifficulty, pReason, pSpawnData, pDataTag);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.redspace.ironsspellbooks.entity.spells;

import io.redspace.ironsspellbooks.api.magic.MagicData;
import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.entity.mobs.AntiMagicSusceptible;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -103,7 +104,7 @@ protected void onHit(HitResult hitresult) {
}

protected void doImpactSound(SoundEvent sound) {
level.playSound(null, getX(), getY(), getZ(), sound, SoundSource.NEUTRAL, 2, .9f + level.random.nextFloat() * .2f);
level.playSound(null, getX(), getY(), getZ(), sound, SoundSource.NEUTRAL, 2, .9f + Utils.random.nextFloat() * .2f);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.redspace.ironsspellbooks.api.events.SpellHealEvent;
import io.redspace.ironsspellbooks.api.registry.SpellRegistry;
import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.capabilities.magic.MagicManager;
import io.redspace.ironsspellbooks.damage.DamageSources;
import io.redspace.ironsspellbooks.entity.spells.AbstractMagicProjectile;
Expand Down Expand Up @@ -118,9 +119,9 @@ public void trailParticles() {

for (int i = 0; i < 2; i++) {
double speed = .05;
double dx = level.random.nextDouble() * 2 * speed - speed;
double dy = level.random.nextDouble() * 2 * speed - speed;
double dz = level.random.nextDouble() * 2 * speed - speed;
double dx = Utils.random.nextDouble() * 2 * speed - speed;
double dy = Utils.random.nextDouble() * 2 * speed - speed;
double dz = Utils.random.nextDouble() * 2 * speed - speed;
level.addParticle(ParticleHelper.BLOOD, this.getX() + dx, this.getY() + dy, this.getZ() + dz, dx, dy, dz);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.redspace.ironsspellbooks.api.events.SpellHealEvent;
import io.redspace.ironsspellbooks.api.magic.MagicData;
import io.redspace.ironsspellbooks.api.registry.SpellRegistry;
import io.redspace.ironsspellbooks.api.util.Utils;
import io.redspace.ironsspellbooks.capabilities.magic.MagicManager;
import io.redspace.ironsspellbooks.damage.DamageSources;
import io.redspace.ironsspellbooks.entity.spells.AbstractShieldEntity;
Expand Down Expand Up @@ -45,7 +46,7 @@ public class BloodSlashProjectile extends Projectile implements AntiMagicSuscept

public BloodSlashProjectile(EntityType<? extends BloodSlashProjectile> entityType, Level level) {
super(entityType, level);
animationSeed = level.random.nextInt(9999);
animationSeed = Utils.random.nextInt(9999);

float initialRadius = 2;
maxRadius = 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public float getSpeed() {

@Override
protected void doImpactSound(SoundEvent sound) {
level.playSound(null, getX(), getY(), getZ(), sound, SoundSource.NEUTRAL, .8f, 1.2f + level.random.nextFloat() * .3f);
level.playSound(null, getX(), getY(), getZ(), sound, SoundSource.NEUTRAL, .8f, 1.2f + Utils.random.nextFloat() * .3f);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void tick() {
if(doFire){
float range = 15 * Mth.DEG_TO_RAD;
for (int i = 0; i < 3; i++) {
Vec3 cast = getOwner().getLookAngle().normalize().xRot(level.random.nextFloat() * range * 2 - range).yRot(level.random.nextFloat() * range * 2 - range);
Vec3 cast = getOwner().getLookAngle().normalize().xRot(Utils.random.nextFloat() * range * 2 - range).yRot(Utils.random.nextFloat() * range * 2 - range);
HitResult hitResult = level.clip(new ClipContext(getOwner().getEyePosition(), getOwner().getEyePosition().add(cast.scale(10)), ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this));
if (hitResult.getType() == HitResult.Type.BLOCK) {
HitResult shieldResult = Utils.raycastForEntityOfClass(level, this, getOwner().getEyePosition(), hitResult.getLocation(), false, AbstractShieldEntity.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Optional<SoundEvent> getImpactSound() {

@Override
protected void doImpactSound(SoundEvent sound) {
level.playSound(null, getX(), getY(), getZ(), sound, SoundSource.NEUTRAL, 2, 1.2f + level.random.nextFloat() * .2f);
level.playSound(null, getX(), getY(), getZ(), sound, SoundSource.NEUTRAL, 2, 1.2f + Utils.random.nextFloat() * .2f);

}

Expand Down
Loading

0 comments on commit 4d4f33b

Please sign in to comment.