Skip to content

Commit 6b93e7b

Browse files
committed
fix ore gen crash
1 parent 2033b24 commit 6b93e7b

6 files changed

Lines changed: 59 additions & 9 deletions

File tree

changeLog/更新日志.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
---
44

5+
## 1.5.2
6+
7+
* 修复了矿物生成的注册在下界崩溃的问题
8+
9+
---
10+
511
## 1.5.1
612

713
* `SmeltingRecipeRegisterEvent`能为配方指定热值需求了

src/main/java/moddedmite/rustedironcore/api/event/events/OreGenerationRegisterEvent.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import moddedmite.rustedironcore.api.world.Dimension;
55
import net.minecraft.WorldGenMinable;
66

7+
import java.util.Random;
8+
import java.util.function.ToIntFunction;
9+
710
public record OreGenerationRegisterEvent(OreGenerationHandler handler) {
811

912
public void register(Dimension dimension, WorldGenMinable ore, int frequency) {
10-
register(dimension, ore, frequency, false);
13+
this.register(dimension, ore, frequency, false);
1114
}
1215

1316
public void register(Dimension dimension, WorldGenMinable ore, int frequency, boolean increasesWithDepth) {
@@ -21,4 +24,7 @@ public void unregister(Dimension dimension, int mineId) {
2124
this.handler.unregisterOre(dimension, mineId);
2225
}
2326

27+
public void setDimensionOreHeight(Dimension dimension, ToIntFunction<Random> height) {
28+
this.handler.setDimensionOreHeight(dimension, height);
29+
}
2430
}

src/main/java/moddedmite/rustedironcore/api/event/handler/OreGenerationHandler.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
package moddedmite.rustedironcore.api.event.handler;
22

33
import moddedmite.rustedironcore.api.event.EventHandler;
4+
import moddedmite.rustedironcore.api.event.Handlers;
45
import moddedmite.rustedironcore.api.event.events.OreGenerationRegisterEvent;
56
import moddedmite.rustedironcore.api.world.Dimension;
67
import net.minecraft.BiomeDecorator;
78
import net.minecraft.World;
89
import net.minecraft.WorldGenMinable;
910
import org.jetbrains.annotations.ApiStatus;
11+
import org.jetbrains.annotations.Nullable;
1012

1113
import java.util.*;
14+
import java.util.function.ToIntFunction;
1215

1316
public class OreGenerationHandler extends EventHandler<OreGenerationRegisterEvent> {
1417
private final Map<Dimension, List<Setting>> ORE_MAP = new HashMap<>();
1518

19+
public static final ToIntFunction<Random> NETHER_ORE_HEIGHT = r -> r.nextInt(108) + 10;
20+
private final Map<Dimension, ToIntFunction<Random>> ORE_HEIGHT_MAP = new HashMap<>();
21+
22+
public OreGenerationHandler() {
23+
this.ORE_HEIGHT_MAP.put(Dimension.NETHER, NETHER_ORE_HEIGHT);
24+
}
25+
1626
@ApiStatus.Internal
1727
public void onOresGeneration(Context context) {
1828
World world = context.world;
@@ -36,11 +46,15 @@ public void unregisterOre(Dimension dimension, int blockId) {
3646
}
3747
}
3848

39-
public static Context context(BiomeDecorator biomeDecorator, World world) {
40-
return new Context(biomeDecorator, world);
49+
public void setDimensionOreHeight(Dimension dimension, ToIntFunction<Random> height) {
50+
this.ORE_HEIGHT_MAP.put(dimension, height);
51+
}
52+
53+
public static Context context(World world, @Nullable BiomeDecorator biomeDecorator, Random random, int blockX, int blockZ) {
54+
return new Context(world, biomeDecorator, random, blockX, blockZ);
4155
}
4256

43-
public record Context(BiomeDecorator biomeDecorator, World world) {
57+
public record Context(World world, @Nullable BiomeDecorator biomeDecorator, Random random, int blockX, int blockZ) {
4458
}
4559

4660
private static Setting setting(WorldGenMinable ore, int frequency, boolean increasesWithDepth) {
@@ -49,7 +63,28 @@ private static Setting setting(WorldGenMinable ore, int frequency, boolean incre
4963

5064
private record Setting(WorldGenMinable ore, int frequency, boolean increasesWithDepth) {
5165
private void generate(Context context) {
52-
context.biomeDecorator.genMinable(frequency, ore, increasesWithDepth);
66+
BiomeDecorator decorator = context.biomeDecorator;
67+
if (decorator != null) {
68+
decorator.genMinable(frequency, ore, increasesWithDepth);
69+
return;
70+
}
71+
World world = context.world;
72+
Dimension dimension = Dimension.fromWorld(world);
73+
ToIntFunction<Random> oreHeight;
74+
if (dimension != null && Handlers.OreGeneration.ORE_HEIGHT_MAP.containsKey(dimension)) {
75+
oreHeight = Handlers.OreGeneration.ORE_HEIGHT_MAP.get(dimension);
76+
} else {
77+
oreHeight = NETHER_ORE_HEIGHT;
78+
}
79+
Random random = context.random;
80+
int blockX = context.blockX;
81+
int blockZ = context.blockZ;
82+
for (int i = 0; i < frequency; i++) {
83+
int x = blockX + random.nextInt(16);
84+
int y = oreHeight.applyAsInt(random);
85+
int z = blockZ + random.nextInt(16);
86+
ore.generate(world, random, x, y, z);
87+
}
5388
}
5489
}
5590
}

src/main/java/moddedmite/rustedironcore/api/world/Dimension.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public static Dimension fromId(int id) {
3636
return ID_TO_DIMENSION.get(id);
3737
}
3838

39+
@Nullable
40+
public static Dimension fromWorld(World world) {
41+
return fromId(world.getDimensionId());
42+
}
43+
3944
public boolean isOf(World world) {
4045
return world.getDimensionId() == this.id;
4146
}

src/main/java/moddedmite/rustedironcore/mixin/chunk/ChunkProviderHellMixin.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ private void onDecoration(IChunkProvider par1IChunkProvider, int par2, int par3,
5454
));
5555
Handlers.OreGeneration.onOresGeneration(
5656
OreGenerationHandler.context(
57-
BiomeGenBase.hell.theBiomeDecorator,
58-
this.worldObj
57+
this.worldObj, null, this.hellRNG, blockX, blockZ
5958
)
6059
);
6160
}

src/main/java/moddedmite/rustedironcore/mixin/world/BiomeDecoratorMixin.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public class BiomeDecoratorMixin {
3636
private void onOresGeneration(CallbackInfo ci) {
3737
Handlers.OreGeneration.onOresGeneration(
3838
OreGenerationHandler.context(
39-
(BiomeDecorator) (Object) this,
40-
this.currentWorld
39+
this.currentWorld, (BiomeDecorator) (Object) this, this.randomGenerator, this.chunk_X, this.chunk_Z
4140
)
4241
);
4342
}

0 commit comments

Comments
 (0)