Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Platform Random, use Level Random wherever applicable #7685

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/main/java/appeng/block/networking/CableBusBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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 +334,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,7 +358,8 @@ 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));
var textures = cableBusModel.getParticleTextures(renderState);
var texture = textures.get(level.getRandom().nextInt(textures.size()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably keep a util-method in Platform, but make it take the random source.
What do you think?

Copy link
Member Author

@62832 62832 Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might work, though I figured it was probably worth just stripping that away altogether since the only place that method was used is in this class.

if (texture != null) {
double x = target.getLocation().x;
double y = target.getLocation().y;
Expand Down Expand Up @@ -398,7 +398,8 @@ 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);
final TextureAtlasSprite texture = textures
.get(level.getRandom().nextInt(textures.size()));

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 @@ -246,7 +246,7 @@ public void breakCluster() {
}

for (var entry : inv.list) {
var position = places.get(Platform.getRandomInt() % places.size());
var position = places.get(Math.abs(level.getRandom().nextInt()) % places.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably also a place for randomly chosing an element (?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd be right. That method could probably be brought back, then.

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 @@ -209,7 +209,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
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 @@ -344,10 +344,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 @@ -356,9 +356,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 @@ -12,6 +12,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 @@ -25,7 +26,6 @@
import appeng.core.AELog;
import appeng.core.AppEngClient;
import appeng.core.network.ClientboundPacket;
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 @@ -69,21 +69,21 @@ public static BlockTransitionEffectPacket decode(FriendlyByteBuf data) {
@Override
@OnlyIn(Dist.CLIENT)
public void handleOnClient(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 @@ -11,7 +11,6 @@
import appeng.client.render.effects.EnergyParticleData;
import appeng.core.AppEngClient;
import appeng.core.network.ClientboundPacket;
import appeng.util.Platform;

/**
* Plays a transition particle effect into the supplied direction. Used primarily by annihilation planes.
Expand Down Expand Up @@ -42,11 +41,11 @@ public void write(FriendlyByteBuf data) {
public void handleOnClient(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.level().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.level().getRandom().nextFloat() * 0.5 - 0.25;
double y = this.y + player.level().getRandom().nextFloat() * 0.5 - 0.25;
double z = this.z + player.level().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 @@ -99,8 +99,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
Expand Up @@ -27,7 +27,6 @@
import appeng.core.AppEng;
import appeng.core.network.clientbound.LightningPacket;
import appeng.items.tools.powered.powersink.AEBasePoweredItem;
import appeng.util.Platform;

public class ChargedStaffItem extends AEBasePoweredItem {

Expand All @@ -42,10 +41,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
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