Skip to content

Commit 60ddf8a

Browse files
committed
Fix asteroid generation crash
1 parent d410a0f commit 60ddf8a

File tree

4 files changed

+54
-46
lines changed

4 files changed

+54
-46
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ api_version = 0.15.1+build.380-1.16.1
1111

1212
# Mod
1313
mod_name = astromine
14-
mod_version = 1.3.3
14+
mod_version = 1.3.4
1515
mod_group = com.github.chainmailstudios
1616
version_meta = fabric-1.16.1
1717

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
22
* MIT License
3-
*
3+
*
44
* Copyright (c) 2020 Chainmail Studios
5-
*
5+
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
88
* in the Software without restriction, including without limitation the rights
99
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
* copies of the Software, and to permit persons to whom the Software is
1111
* furnished to do so, subject to the following conditions:
12-
*
12+
*
1313
* The above copyright notice and this permission notice shall be included in all
1414
* copies or substantial portions of the Software.
15-
*
15+
*
1616
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -23,36 +23,42 @@
2323
*/
2424
package com.github.chainmailstudios.astromine.client.registry;
2525

26-
import com.github.chainmailstudios.astromine.common.registry.base.MultiRegistry;
2726
import com.github.chainmailstudios.astromine.common.utilities.data.Range;
2827
import net.minecraft.block.Block;
29-
import net.minecraft.util.math.MathHelper;
28+
import org.jetbrains.annotations.Nullable;
3029

31-
import java.util.HashMap;
32-
import java.util.Map;
33-
import java.util.Random;
30+
import java.util.*;
3431

35-
public class AsteroidOreRegistry extends MultiRegistry<Integer, Block> {
32+
public class AsteroidOreRegistry {
3633
public static final AsteroidOreRegistry INSTANCE = new AsteroidOreRegistry();
3734

38-
public final Map<Block, Integer> minimumDiameters = new HashMap<>();
39-
public final Map<Block, Integer> maximumDiameters = new HashMap();
35+
private final Map<Block, @Nullable Range<Integer>> diameters = new HashMap<>();
4036

4137
private AsteroidOreRegistry() {
4238
// Locked.
4339
}
4440

45-
public void register(Range<Integer> range, int minimumDiameter, int maximumDiameter, Block block) {
41+
public void register(Range<Integer> range, Block block) {
4642
if (range.getMinimum() > range.getMaximum()) {
4743
range = Range.of(range.getMaximum(), range.getMinimum());
44+
} else if (range.getMinimum().equals(range.getMaximum())) {
45+
range = null;
4846
}
4947

50-
for (int chance = range.getMinimum(); chance < range.getMaximum(); ++chance) {
51-
this.register(chance, block);
48+
if (range == null) {
49+
diameters.remove(block);
50+
} else {
51+
diameters.put(block, range);
5252
}
5353
}
5454

55+
public Set<Block> keySet() {
56+
return diameters.keySet();
57+
}
58+
5559
public int getDiameter(Random random, Block block) {
56-
return (int) MathHelper.clamp(maximumDiameters.get(block) * random.nextFloat(), minimumDiameters.get(block), maximumDiameters.get(block));
60+
Range<Integer> range = diameters.get(block);
61+
if (range == null) return 0;
62+
return (int) ((range.getMaximum() - range.getMinimum()) * Objects.requireNonNull(random, "random").nextFloat() + range.getMinimum());
5763
}
5864
}

src/main/java/com/github/chainmailstudios/astromine/common/world/feature/AsteroidOreFeature.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
22
* MIT License
3-
*
3+
*
44
* Copyright (c) 2020 Chainmail Studios
5-
*
5+
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
88
* in the Software without restriction, including without limitation the rights
99
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
* copies of the Software, and to permit persons to whom the Software is
1111
* furnished to do so, subject to the following conditions:
12-
*
12+
*
1313
* The above copyright notice and this permission notice shall be included in all
1414
* copies or substantial portions of the Software.
15-
*
15+
*
1616
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -55,7 +55,7 @@ public AsteroidOreFeature(Codec<DefaultFeatureConfig> codec) {
5555
public boolean generate(ServerWorldAccess world, StructureAccessor accessor, ChunkGenerator generator, Random random, BlockPos featurePosition, DefaultFeatureConfig config) {
5656
featurePosition = new BlockPos(featurePosition.getX(), random.nextInt(256), featurePosition.getZ());
5757

58-
List<Block> ores = Lists.newArrayList(AsteroidOreRegistry.INSTANCE.get(random.nextInt(100)));
58+
List<Block> ores = Lists.newArrayList(AsteroidOreRegistry.INSTANCE.keySet());
5959

6060
if (ores.isEmpty()) {
6161
return true;
@@ -69,15 +69,17 @@ public boolean generate(ServerWorldAccess world, StructureAccessor accessor, Chu
6969
double ySize = AsteroidOreRegistry.INSTANCE.getDiameter(random, ore);
7070
double zSize = AsteroidOreRegistry.INSTANCE.getDiameter(random, ore);
7171

72-
Shape vein = Shapes.ellipsoid((float) xSize, (float) ySize, (float) zSize)
73-
.applyLayer(RotateLayer.of(Quaternion.of(random.nextDouble() * 360, random.nextDouble() * 360, random.nextDouble() * 360, true)))
74-
.applyLayer(TranslateLayer.of(Position.of(featurePosition)));
72+
if (xSize > 0 && ySize > 0 && zSize > 0) {
73+
Shape vein = Shapes.ellipsoid((float) xSize, (float) ySize, (float) zSize)
74+
.applyLayer(RotateLayer.of(Quaternion.of(random.nextDouble() * 360, random.nextDouble() * 360, random.nextDouble() * 360, true)))
75+
.applyLayer(TranslateLayer.of(Position.of(featurePosition)));
7576

76-
for (Position streamPosition : vein.stream().collect(Collectors.toSet())) {
77-
BlockPos orePosition = streamPosition.toBlockPos();
77+
for (Position streamPosition : vein.stream().collect(Collectors.toSet())) {
78+
BlockPos orePosition = streamPosition.toBlockPos();
7879

79-
if (world.getBlockState(orePosition).getBlock() == AstromineBlocks.ASTEROID_STONE) {
80-
world.setBlockState(orePosition, ore.getDefaultState(), 0b0110100);
80+
if (world.getBlockState(orePosition).getBlock() == AstromineBlocks.ASTEROID_STONE) {
81+
world.setBlockState(orePosition, ore.getDefaultState(), 0b0110100);
82+
}
8183
}
8284
}
8385

src/main/java/com/github/chainmailstudios/astromine/registry/AstromineOres.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
22
* MIT License
3-
*
3+
*
44
* Copyright (c) 2020 Chainmail Studios
5-
*
5+
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
88
* in the Software without restriction, including without limitation the rights
99
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
* copies of the Software, and to permit persons to whom the Software is
1111
* furnished to do so, subject to the following conditions:
12-
*
12+
*
1313
* The above copyright notice and this permission notice shall be included in all
1414
* copies or substantial portions of the Software.
15-
*
15+
*
1616
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -36,22 +36,22 @@
3636

3737
public class AstromineOres {
3838
public static void initialize() {
39-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidCoalOreMinimumRange, AstromineConfig.get().asteroidCoalOreMaximumRange), AstromineConfig.get().asteroidCoalOreMinimumSize, AstromineConfig.get().asteroidCoalOreMaximumSize, AstromineBlocks.ASTEROID_COAL_ORE);
40-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidIronOreMinimumRange, AstromineConfig.get().asteroidIronOreMaximumRange), AstromineConfig.get().asteroidIronOreMinimumSize, AstromineConfig.get().asteroidIronOreMaximumSize, AstromineBlocks.ASTEROID_IRON_ORE);
41-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidGoldOreMinimumRange, AstromineConfig.get().asteroidGoldOreMaximumRange), AstromineConfig.get().asteroidGoldOreMinimumSize, AstromineConfig.get().asteroidGoldOreMaximumSize, AstromineBlocks.ASTEROID_GOLD_ORE);
42-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidCopperOreMinimumRange, AstromineConfig.get().asteroidCopperOreMaximumRange), AstromineConfig.get().asteroidCopperOreMinimumSize, AstromineConfig.get().asteroidCopperOreMaximumSize, AstromineBlocks.ASTEROID_COPPER_ORE);
43-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidTinOreMinimumRange, AstromineConfig.get().asteroidTinOreMaximumRange), AstromineConfig.get().asteroidTinOreMinimumSize, AstromineConfig.get().asteroidTinOreMaximumSize, AstromineBlocks.ASTEROID_TIN_ORE);
39+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidCoalOreMinimumRange, AstromineConfig.get().asteroidCoalOreMaximumRange), AstromineBlocks.ASTEROID_COAL_ORE);
40+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidIronOreMinimumRange, AstromineConfig.get().asteroidIronOreMaximumRange), AstromineBlocks.ASTEROID_IRON_ORE);
41+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidGoldOreMinimumRange, AstromineConfig.get().asteroidGoldOreMaximumRange), AstromineBlocks.ASTEROID_GOLD_ORE);
42+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidCopperOreMinimumRange, AstromineConfig.get().asteroidCopperOreMaximumRange), AstromineBlocks.ASTEROID_COPPER_ORE);
43+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidTinOreMinimumRange, AstromineConfig.get().asteroidTinOreMaximumRange), AstromineBlocks.ASTEROID_TIN_ORE);
4444

45-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidRedstoneOreMinimumRange, AstromineConfig.get().asteroidRedstoneOreMaximumRange), AstromineConfig.get().asteroidRedstoneOreMinimumSize, AstromineConfig.get().asteroidRedstoneOreMaximumSize, AstromineBlocks.ASTEROID_REDSTONE_ORE);
46-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidLapisOreMinimumRange, AstromineConfig.get().asteroidLapisOreMaximumRange), AstromineConfig.get().asteroidLapisOreMinimumSize, AstromineConfig.get().asteroidLapisOreMaximumSize, AstromineBlocks.ASTEROID_LAPIS_ORE);
45+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidRedstoneOreMinimumRange, AstromineConfig.get().asteroidRedstoneOreMaximumRange), AstromineBlocks.ASTEROID_REDSTONE_ORE);
46+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidLapisOreMinimumRange, AstromineConfig.get().asteroidLapisOreMaximumRange), AstromineBlocks.ASTEROID_LAPIS_ORE);
4747

48-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidDiamondOreMinimumRange, AstromineConfig.get().asteroidDiamondOreMaximumRange), AstromineConfig.get().asteroidDiamondOreMinimumSize, AstromineConfig.get().asteroidDiamondOreMaximumSize, AstromineBlocks.ASTEROID_DIAMOND_ORE);
49-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidEmeraldOreMinimumRange, AstromineConfig.get().asteroidEmeraldOreMaximumRange), AstromineConfig.get().asteroidEmeraldOreMinimumSize, AstromineConfig.get().asteroidEmeraldOreMaximumSize, AstromineBlocks.ASTEROID_EMERALD_ORE);
48+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidDiamondOreMinimumRange, AstromineConfig.get().asteroidDiamondOreMaximumRange), AstromineBlocks.ASTEROID_DIAMOND_ORE);
49+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidEmeraldOreMinimumRange, AstromineConfig.get().asteroidEmeraldOreMaximumRange), AstromineBlocks.ASTEROID_EMERALD_ORE);
5050

51-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidMetiteOreMinimumRange, AstromineConfig.get().asteroidMetiteOreMaximumRange), AstromineConfig.get().asteroidMetiteOreMinimumSize, AstromineConfig.get().asteroidMetiteOreMaximumSize, AstromineBlocks.ASTEROID_METITE_ORE);
52-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidAsteriteOreMinimumRange, AstromineConfig.get().asteroidAsteriteOreMaximumRange), AstromineConfig.get().asteroidAsteriteOreMinimumSize, AstromineConfig.get().asteroidAsteriteOreMaximumSize, AstromineBlocks.ASTEROID_ASTERITE_ORE);
53-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidStellumOreMinimumRange, AstromineConfig.get().asteroidStellumOreMaximumRange), AstromineConfig.get().asteroidStellumOreMinimumSize, AstromineConfig.get().asteroidStellumOreMaximumSize, AstromineBlocks.ASTEROID_STELLUM_ORE);
54-
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidGalaxiumOreMinimumRange, AstromineConfig.get().asteroidGalaxiumOreMaximumRange), AstromineConfig.get().asteroidGalaxiumOreMinimumSize, AstromineConfig.get().asteroidGalaxiumOreMaximumSize, AstromineBlocks.ASTEROID_GALAXIUM_ORE);
51+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidMetiteOreMinimumRange, AstromineConfig.get().asteroidMetiteOreMaximumRange), AstromineBlocks.ASTEROID_METITE_ORE);
52+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidAsteriteOreMinimumRange, AstromineConfig.get().asteroidAsteriteOreMaximumRange), AstromineBlocks.ASTEROID_ASTERITE_ORE);
53+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidStellumOreMinimumRange, AstromineConfig.get().asteroidStellumOreMaximumRange), AstromineBlocks.ASTEROID_STELLUM_ORE);
54+
AsteroidOreRegistry.INSTANCE.register(Range.of(AstromineConfig.get().asteroidGalaxiumOreMinimumRange, AstromineConfig.get().asteroidGalaxiumOreMaximumRange), AstromineBlocks.ASTEROID_GALAXIUM_ORE);
5555

5656
for (Biome biome : Registry.BIOME) {
5757
if (biome.getCategory() != Biome.Category.NETHER && biome.getCategory() != Biome.Category.THEEND && biome.getCategory() != Biome.Category.NONE) {

0 commit comments

Comments
 (0)