9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-20 15:29:35 +00:00
Files
LeavesMC/patches/server/0030-Use-thread-unsafe-random-for-mob-spawning.patch
2022-08-17 10:31:39 +08:00

56 lines
3.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: violetc <58360096+s-yh-china@users.noreply.github.com>
Date: Mon, 15 Aug 2022 08:23:51 +0800
Subject: [PATCH] Use thread unsafe random for mob spawning
This patch is Powered by Pufferfish(https://github.com/pufferfish-gg/Pufferfish)
diff --git a/src/main/java/net/minecraft/world/level/NaturalSpawner.java b/src/main/java/net/minecraft/world/level/NaturalSpawner.java
index e31a2eea9a62ab2c0bed1a97dab6bae231b8cd8b..cc851e1a9c5b23269c53206a7f1e14f311292e8c 100644
--- a/src/main/java/net/minecraft/world/level/NaturalSpawner.java
+++ b/src/main/java/net/minecraft/world/level/NaturalSpawner.java
@@ -416,12 +416,21 @@ public final class NaturalSpawner {
private static BlockPos getRandomPosWithin(Level world, LevelChunk chunk) {
ChunkPos chunkcoordintpair = chunk.getPos();
- int i = chunkcoordintpair.getMinBlockX() + world.random.nextInt(16);
- int j = chunkcoordintpair.getMinBlockZ() + world.random.nextInt(16);
- int k = chunk.getHeight(Heightmap.Types.WORLD_SURFACE, i, j) + 1;
- int l = Mth.randomBetweenInclusive(world.random, world.getMinBuildHeight(), k);
-
- return new BlockPos(i, l, j);
+ // Leaves start - use thread unsafe random
+ if (top.leavesmc.leaves.LeavesConfig.useMoreThreadUnsafeRandom) {
+ int i = chunkcoordintpair.getMinBlockX() + world.getThreadUnsafeRandom().nextInt(16);
+ int j = chunkcoordintpair.getMinBlockZ() + world.getThreadUnsafeRandom().nextInt(16);
+ int k = chunk.getHeight(Heightmap.Types.WORLD_SURFACE, i, j) + 1;
+ int l = Mth.randomBetweenInclusive(world.getThreadUnsafeRandom(), world.getMinBuildHeight(), k);
+ return new BlockPos(i, l, j);
+ } else {
+ int i = chunkcoordintpair.getMinBlockX() + world.random.nextInt(16);
+ int j = chunkcoordintpair.getMinBlockZ() + world.random.nextInt(16);
+ int k = chunk.getHeight(Heightmap.Types.WORLD_SURFACE, i, j) + 1;
+ int l = Mth.randomBetweenInclusive(world.random, world.getMinBuildHeight(), k);
+ return new BlockPos(i, l, j);
+ }
+ // Leaves end - use thread unsafe random
}
public static boolean isValidEmptySpawnBlock(BlockGetter blockView, BlockPos pos, BlockState state, FluidState fluidState, EntityType<?> entityType) {
diff --git a/src/main/java/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java
index 2da10eaf3dc4fce1edf65fe4068f5c9e30df3b17..f7f2c18112fc711edda300753ce2f465fe75ba7f 100644
--- a/src/main/java/top/leavesmc/leaves/LeavesConfig.java
+++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java
@@ -278,6 +278,11 @@ public final class LeavesConfig {
entityTargetFindingOptimization = getBoolean("settings.performance.entity-target-find-optimization", entityTargetFindingOptimization);
}
+ public static boolean useMoreThreadUnsafeRandom = true;
+ private static void useMoreThreadUnsafeRandom() {
+ useMoreThreadUnsafeRandom = getBoolean("settings.performance.use-more-thread-unsafe-random", useMoreThreadUnsafeRandom);
+ }
+
public static final class WorldConfig {
public final String worldName;