Skip to content

Commit

Permalink
Remove Platform Random, use Level Random wherever applicable (#7685)
Browse files Browse the repository at this point in the history
Closes #7684.

---------

Co-authored-by: Sebastian Hartte <shartte@users.noreply.github.com>
(cherry picked from commit 0d8998b)
  • Loading branch information
shartte committed Apr 1, 2024
1 parent 0ace311 commit e8db2b1
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 77 deletions.
11 changes: 6 additions & 5 deletions src/main/java/appeng/block/networking/CableBusBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.jetbrains.annotations.Nullable;

import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.Particle;
Expand Down Expand Up @@ -82,7 +83,6 @@
import appeng.integration.abstraction.IAEFacade;
import appeng.parts.ICableBusContainer;
import appeng.parts.NullCableBusContainer;
import appeng.util.Platform;

public class CableBusBlock extends AEBaseEntityBlock<CableBusBlockEntity> implements IAEFacade, SimpleWaterloggedBlock {

Expand Down Expand Up @@ -335,7 +335,7 @@ public boolean addHitEffects(BlockState state, Level level, HitResult target,

// Half the particle rate. Since we're spawning concentrated on a specific spot,
// our particle effect otherwise looks too strong
if (Platform.getRandom().nextBoolean()) {
if (level.getRandom().nextBoolean()) {
return true;
}

Expand All @@ -359,8 +359,9 @@ public boolean addHitEffects(BlockState state, Level level, HitResult target,
CableBusRenderState renderState = cb.getRenderState();

// Spawn a particle for one of the particle textures
TextureAtlasSprite texture = Platform.pickRandom(cableBusModel.getParticleTextures(renderState));
if (texture != null) {
var textures = cableBusModel.getParticleTextures(renderState);
if (!textures.isEmpty()) {
var texture = Util.getRandom(textures, level.getRandom());
double x = target.getLocation().x;
double y = target.getLocation().y;
double z = target.getLocation().z;
Expand Down Expand Up @@ -398,7 +399,7 @@ public boolean addDestroyEffects(BlockState state, Level level, BlockPos pos,
for (int l = 0; l < 4; ++l) {
// Randomly select one of the textures if the cable bus has more than just one
// possibility here
final TextureAtlasSprite texture = Platform.pickRandom(textures);
var texture = Util.getRandom(textures, level.getRandom());

final double x = pos.getX() + (j + 0.5D) / 4.0D;
final double y = pos.getY() + (k + 0.5D) / 4.0D;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.google.common.collect.Iterators;

import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -246,7 +247,7 @@ public void breakCluster() {
}

for (var entry : inv.list) {
var position = places.get(Platform.getRandomInt() % places.size());
var position = Util.getRandom(places, level.getRandom());
var stacks = new ArrayList<ItemStack>();
entry.getKey().addDrops(entry.getLongValue(), stacks, this.level, position);
Platform.spawnDrops(this.level, position, stacks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private void doWork(int ticksSinceLastCall) {
} else if (this.getInternalCurrentPower() > POWER_THRESHOLD
&& ChargerRecipes.findRecipe(level, myItem) != null) {
this.working = true;
if (Platform.getRandomFloat() > 0.8f) {
if (level != null && level.getRandom().nextFloat() > 0.8f) {
this.extractAEPower(this.getInternalMaxPower(), Actionable.MODULATE, PowerMultiplier.CONFIG);

Item charged = Objects.requireNonNull(ChargerRecipes.findRecipe(level, myItem)).result;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/appeng/core/AppEngBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@
import java.util.Collection;
import java.util.Collections;

import appeng.core.definitions.AEItems;
import com.mojang.brigadier.CommandDispatcher;

import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
Expand Down Expand Up @@ -62,6 +61,7 @@
import appeng.api.stacks.AEKeyType;
import appeng.api.stacks.AEKeyTypes;
import appeng.api.stacks.AEKeyTypesInternal;
import appeng.core.definitions.AEItems;
import appeng.core.sync.BasePacket;
import appeng.core.sync.network.NetworkHandler;
import appeng.hooks.SkyStoneBreakSpeed;
Expand Down Expand Up @@ -294,7 +294,7 @@ public MinecraftServer getCurrentServer() {
protected final CableRenderMode getCableRenderModeForPlayer(@Nullable Player player) {
if (player != null) {
if (AEItems.NETWORK_TOOL.isSameAs(player.getItemInHand(InteractionHand.MAIN_HAND))
|| AEItems.NETWORK_TOOL.isSameAs(player.getItemInHand(InteractionHand.OFF_HAND))) {
|| AEItems.NETWORK_TOOL.isSameAs(player.getItemInHand(InteractionHand.OFF_HAND))) {
return CableRenderMode.CABLE_VIEW;
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/appeng/core/AppEngClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,10 @@ public void spawnEffect(EffectType effect, Level level, double posX, double posY
}

private void spawnVibrant(Level level, double x, double y, double z) {
if (AppEngClient.instance().shouldAddParticles(Platform.getRandom())) {
final double d0 = (Platform.getRandomFloat() - 0.5F) * 0.26D;
final double d1 = (Platform.getRandomFloat() - 0.5F) * 0.26D;
final double d2 = (Platform.getRandomFloat() - 0.5F) * 0.26D;
if (AppEngClient.instance().shouldAddParticles(level.getRandom())) {
final double d0 = (level.getRandom().nextFloat() - 0.5F) * 0.26D;
final double d1 = (level.getRandom().nextFloat() - 0.5F) * 0.26D;
final double d2 = (level.getRandom().nextFloat() - 0.5F) * 0.26D;

Minecraft.getInstance().particleEngine.createParticle(ParticleTypes.VIBRANT, x + d0, y + d1, z + d2, 0.0D,
0.0D,
Expand All @@ -357,9 +357,10 @@ private void spawnVibrant(Level level, double x, double y, double z) {
}

private void spawnEnergy(Level level, double posX, double posY, double posZ) {
final float x = (float) (Platform.getRandomInt() % 100 * 0.01 - 0.5) * 0.7f;
final float y = (float) (Platform.getRandomInt() % 100 * 0.01 - 0.5) * 0.7f;
final float z = (float) (Platform.getRandomInt() % 100 * 0.01 - 0.5) * 0.7f;
var random = level.getRandom();
final float x = (float) (Math.abs(random.nextInt()) % 100 * 0.01 - 0.5) * 0.7f;
final float y = (float) (Math.abs(random.nextInt()) % 100 * 0.01 - 0.5) * 0.7f;
final float z = (float) (Math.abs(random.nextInt()) % 100 * 0.01 - 0.5) * 0.7f;

Minecraft.getInstance().particleEngine.createParticle(EnergyParticleData.FOR_BLOCK, posX + x, posY + y,
posZ + z,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.minecraft.sounds.SoundSource;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
Expand All @@ -44,7 +45,6 @@
import appeng.core.AELog;
import appeng.core.AppEngClient;
import appeng.core.sync.BasePacket;
import appeng.util.Platform;

/**
* Plays the block breaking or fluid pickup sound and a transition particle effect into the supplied direction. Used
Expand Down Expand Up @@ -99,21 +99,21 @@ public BlockTransitionEffectPacket(FriendlyByteBuf stream) {
@Override
@OnlyIn(Dist.CLIENT)
public void clientPacketData(Player player) {
spawnParticles();
spawnParticles(player.level());

playBreakOrPickupSound();
}

@OnlyIn(Dist.CLIENT)
private void spawnParticles() {
private void spawnParticles(Level level) {

EnergyParticleData data = new EnergyParticleData(false, direction);
for (int zz = 0; zz < 32; zz++) {
if (AppEngClient.instance().shouldAddParticles(Platform.getRandom())) {
if (AppEngClient.instance().shouldAddParticles(level.getRandom())) {
// Distribute the spawn point across the entire block's area
double x = pos.getX() + Platform.getRandomFloat();
double y = pos.getY() + Platform.getRandomFloat();
double z = pos.getZ() + Platform.getRandomFloat();
double x = pos.getX() + level.getRandom().nextFloat();
double y = pos.getY() + level.getRandom().nextFloat();
double z = pos.getZ() + level.getRandom().nextFloat();
double speedX = 0.1f * this.direction.getStepX();
double speedY = 0.1f * this.direction.getStepY();
double speedZ = 0.1f * this.direction.getStepZ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import appeng.client.render.effects.EnergyParticleData;
import appeng.core.AppEngClient;
import appeng.core.sync.BasePacket;
import appeng.util.Platform;

/**
* Plays a transition particle effect into the supplied direction. Used primarily by annihilation planes.
Expand Down Expand Up @@ -71,11 +70,11 @@ public ItemTransitionEffectPacket(FriendlyByteBuf stream) {
public void clientPacketData(Player player) {
EnergyParticleData data = new EnergyParticleData(true, this.d);
for (int zz = 0; zz < 8; zz++) {
if (AppEngClient.instance().shouldAddParticles(Platform.getRandom())) {
if (AppEngClient.instance().shouldAddParticles(player.getRandom())) {
// Distribute the spawn point around the item's position
double x = this.x + Platform.getRandomFloat() * 0.5 - 0.25;
double y = this.y + Platform.getRandomFloat() * 0.5 - 0.25;
double z = this.z + Platform.getRandomFloat() * 0.5 - 0.25;
double x = this.x + player.getRandom().nextFloat() * 0.5 - 0.25;
double y = this.y + player.getRandom().nextFloat() * 0.5 - 0.25;
double z = this.z + player.getRandom().nextFloat() * 0.5 - 0.25;
double speedX = 0.1f * this.d.getStepX();
double speedY = 0.1f * this.d.getStepY();
double speedZ = 0.1f * this.d.getStepZ();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/appeng/debug/MeteoritePlacerItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) {
}

// See MeteoriteStructure for original code
float coreRadius = Platform.getRandomFloat() * 6.0f + 2;
boolean pureCrater = Platform.getRandomFloat() > 0.5f;
float coreRadius = level.getRandom().nextFloat() * 6.0f + 2;
boolean pureCrater = level.getRandom().nextFloat() > 0.5f;
CraterType craterType = CraterType.values()[tag.getByte(MODE_TAG)];

MeteoriteSpawner spawner = new MeteoriteSpawner();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package appeng.decorative.solid;

import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -34,7 +35,7 @@ public void randomTick(BlockState state, ServerLevel level, BlockPos pos, Random
}

// Try to grow cluster
Direction direction = DIRECTIONS[randomSource.nextInt(DIRECTIONS.length)];
Direction direction = Util.getRandom(DIRECTIONS, randomSource);
BlockPos targetPos = pos.relative(direction);
BlockState targetState = level.getBlockState(targetPos);
Block newCluster = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import appeng.blockentity.misc.ChargerBlockEntity;
import appeng.blockentity.networking.CableBusBlockEntity;
import appeng.blockentity.networking.CrystalResonanceGeneratorBlockEntity;
import appeng.core.AppEng;
import appeng.helpers.patternprovider.PatternProviderLogicHost;
import appeng.integration.modules.igtooltip.blocks.ChargerDataProvider;
import appeng.integration.modules.igtooltip.blocks.CraftingMonitorDataProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import appeng.core.AppEng;
import appeng.core.sync.packets.LightningPacket;
import appeng.items.tools.powered.powersink.AEBasePoweredItem;
import appeng.util.Platform;

public class ChargedStaffItem extends AEBasePoweredItem {

Expand All @@ -43,10 +42,12 @@ public boolean hurtEnemy(ItemStack item, LivingEntity target, LivingEntity hitte
if (!target.level().isClientSide()) {
for (int x = 0; x < 2; x++) {
final AABB entityBoundingBox = target.getBoundingBox();
final float dx = (float) (Platform.getRandomFloat() * target.getBbWidth() + entityBoundingBox.minX);
final float dy = (float) (Platform.getRandomFloat() * target.getBbHeight()
final float dx = (float) (target.level().getRandom().nextFloat() * target.getBbWidth()
+ entityBoundingBox.minX);
final float dy = (float) (target.level().getRandom().nextFloat() * target.getBbHeight()
+ entityBoundingBox.minY);
final float dz = (float) (Platform.getRandomFloat() * target.getBbWidth() + entityBoundingBox.minZ);
final float dz = (float) (target.level().getRandom().nextFloat() * target.getBbWidth()
+ entityBoundingBox.minZ);
AppEng.instance().sendToAllNearExcept(null, dx, dy, dz, 32.0, target.level(),
new LightningPacket(dx, dy, dz));
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/appeng/parts/automation/ExportBusPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import appeng.items.parts.PartModels;
import appeng.menu.implementations.IOBusMenu;
import appeng.parts.PartModel;
import appeng.util.Platform;
import appeng.util.prioritylist.DefaultPriorityList;

/**
Expand Down Expand Up @@ -222,7 +221,7 @@ public ImmutableSet<ICraftingLink> getRequestedJobs() {

protected int getStartingSlot(SchedulingMode schedulingMode, int x) {
if (schedulingMode == SchedulingMode.RANDOM) {
return Platform.getRandom().nextInt(this.availableSlots());
return getLevel().getRandom().nextInt(this.availableSlots());
}

if (schedulingMode == SchedulingMode.ROUNDROBIN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -265,7 +264,7 @@ protected float calculateEnergyUsage(ServerLevel level, BlockPos pos, List<ItemS
if (enchantments.containsKey(Enchantments.UNBREAKING)) {
// Give plane only a (100 / (level + 1))% chance to use energy.
// This is similar to vanilla Unbreaking behaviour for tools.
int randomNumber = ThreadLocalRandom.current().nextInt(enchantments.get(Enchantments.UNBREAKING) + 1);
int randomNumber = level.getRandom().nextInt(enchantments.get(Enchantments.UNBREAKING) + 1);
useEnergy = randomNumber == 0;
}
var levelSum = enchantments.values().stream().reduce(0, Integer::sum) - efficiencyLevel;
Expand Down
35 changes: 0 additions & 35 deletions src/main/java/appeng/util/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
package appeng.util;

import java.text.DecimalFormat;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Iterables;
import com.mojang.authlib.GameProfile;

import org.jetbrains.annotations.Nullable;
Expand All @@ -43,7 +41,6 @@
import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.Containers;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.CraftingContainer;
Expand Down Expand Up @@ -85,11 +82,6 @@ public class Platform {
@VisibleForTesting
public static ThreadGroup serverThreadGroup = SidedThreadGroups.SERVER;

/*
* random source, use it for item drop locations...
*/
private static final RandomSource RANDOM_GENERATOR = RandomSource.create();

private static final P2PHelper P2P_HELPER = new P2PHelper();

public static final Direction[] DIRECTIONS_WITH_NULL = new Direction[] { Direction.DOWN, Direction.UP,
Expand Down Expand Up @@ -140,14 +132,6 @@ public static P2PHelper p2p() {
return P2P_HELPER;
}

public static RandomSource getRandom() {
return RANDOM_GENERATOR;
}

public static float getRandomFloat() {
return RANDOM_GENERATOR.nextFloat();
}

/**
* This displays the value for encoded longs ( double *100 )
*
Expand Down Expand Up @@ -262,10 +246,6 @@ public static void assertServerThread() {
}
}

public static int getRandomInt() {
return Math.abs(RANDOM_GENERATOR.nextInt());
}

public static String formatModName(String modId) {
return "" + ChatFormatting.BLUE + ChatFormatting.ITALIC + getModName(modId);
}
Expand Down Expand Up @@ -319,21 +299,6 @@ public static Player getFakePlayer(ServerLevel level, @Nullable UUID playerUuid)
return FakePlayer.get(level, new GameProfile(playerUuid, "[AE2]"));
}

/**
* Returns a random element from the given collection.
*
* @return null if the collection is empty
*/
@Nullable
public static <T> T pickRandom(Collection<T> outs) {
if (outs.isEmpty()) {
return null;
}

int index = RANDOM_GENERATOR.nextInt(outs.size());
return Iterables.get(outs, index, null);
}

public static Direction rotateAround(Direction forward, Direction axis) {
if (forward.getAxis() == axis.getAxis()) {
return forward;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/appeng/worldgen/meteorite/MeteoritePlacer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import java.util.stream.Stream;

import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -240,7 +241,7 @@ private void placeMeteoriteSkyStone() {
// Add a bud on top if it's not a regular certus block (index 0), and not the center.
// (70% chance)
if (certusIndex != 0 && (dx != 0 || dz != 0) && random.nextFloat() <= 0.7) {
var bud = quartzBuds.get(random.nextInt(quartzBuds.size()));
var bud = Util.getRandom(quartzBuds, random);
var budState = bud.setValue(AmethystClusterBlock.FACING, Direction.UP);
this.putter.put(level, pos.offset(0, 1, 0), budState);
}
Expand Down

0 comments on commit e8db2b1

Please sign in to comment.