Skip to content

Commit

Permalink
Fix asteroid generation crash
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Jul 24, 2020
1 parent d410a0f commit 60ddf8a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ api_version = 0.15.1+build.380-1.16.1

# Mod
mod_name = astromine
mod_version = 1.3.3
mod_version = 1.3.4
mod_group = com.github.chainmailstudios
version_meta = fabric-1.16.1

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* MIT License
*
*
* Copyright (c) 2020 Chainmail Studios
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -23,36 +23,42 @@
*/
package com.github.chainmailstudios.astromine.client.registry;

import com.github.chainmailstudios.astromine.common.registry.base.MultiRegistry;
import com.github.chainmailstudios.astromine.common.utilities.data.Range;
import net.minecraft.block.Block;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.*;

public class AsteroidOreRegistry extends MultiRegistry<Integer, Block> {
public class AsteroidOreRegistry {
public static final AsteroidOreRegistry INSTANCE = new AsteroidOreRegistry();

public final Map<Block, Integer> minimumDiameters = new HashMap<>();
public final Map<Block, Integer> maximumDiameters = new HashMap();
private final Map<Block, @Nullable Range<Integer>> diameters = new HashMap<>();

private AsteroidOreRegistry() {
// Locked.
}

public void register(Range<Integer> range, int minimumDiameter, int maximumDiameter, Block block) {
public void register(Range<Integer> range, Block block) {
if (range.getMinimum() > range.getMaximum()) {
range = Range.of(range.getMaximum(), range.getMinimum());
} else if (range.getMinimum().equals(range.getMaximum())) {
range = null;
}

for (int chance = range.getMinimum(); chance < range.getMaximum(); ++chance) {
this.register(chance, block);
if (range == null) {
diameters.remove(block);
} else {
diameters.put(block, range);
}
}

public Set<Block> keySet() {
return diameters.keySet();
}

public int getDiameter(Random random, Block block) {
return (int) MathHelper.clamp(maximumDiameters.get(block) * random.nextFloat(), minimumDiameters.get(block), maximumDiameters.get(block));
Range<Integer> range = diameters.get(block);
if (range == null) return 0;
return (int) ((range.getMaximum() - range.getMinimum()) * Objects.requireNonNull(random, "random").nextFloat() + range.getMinimum());
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* MIT License
*
*
* Copyright (c) 2020 Chainmail Studios
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -55,7 +55,7 @@ public AsteroidOreFeature(Codec<DefaultFeatureConfig> codec) {
public boolean generate(ServerWorldAccess world, StructureAccessor accessor, ChunkGenerator generator, Random random, BlockPos featurePosition, DefaultFeatureConfig config) {
featurePosition = new BlockPos(featurePosition.getX(), random.nextInt(256), featurePosition.getZ());

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

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

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

for (Position streamPosition : vein.stream().collect(Collectors.toSet())) {
BlockPos orePosition = streamPosition.toBlockPos();
for (Position streamPosition : vein.stream().collect(Collectors.toSet())) {
BlockPos orePosition = streamPosition.toBlockPos();

if (world.getBlockState(orePosition).getBlock() == AstromineBlocks.ASTEROID_STONE) {
world.setBlockState(orePosition, ore.getDefaultState(), 0b0110100);
if (world.getBlockState(orePosition).getBlock() == AstromineBlocks.ASTEROID_STONE) {
world.setBlockState(orePosition, ore.getDefaultState(), 0b0110100);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* MIT License
*
*
* Copyright (c) 2020 Chainmail Studios
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -36,22 +36,22 @@

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

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

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

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

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

0 comments on commit 60ddf8a

Please sign in to comment.