Skip to content

Commit 4bd88f2

Browse files
authored
Merge pull request #42 from dshadowwolf/v3
Logic Bombs
2 parents b308da6 + 15316ab commit 4bd88f2

File tree

6 files changed

+62
-32
lines changed

6 files changed

+62
-32
lines changed

src/main/java/com/mcmoddev/orespawn/OreSpawn.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
import com.mcmoddev.orespawn.json.OS3Reader;
99
import com.mcmoddev.orespawn.json.OS3Writer;
1010
import com.mcmoddev.orespawn.api.OreSpawnAPI;
11+
import com.mcmoddev.orespawn.api.SpawnEntry;
1112
import com.mcmoddev.orespawn.commands.AddOreCommand;
1213
import com.mcmoddev.orespawn.commands.ClearChunkCommand;
1314
import com.mcmoddev.orespawn.commands.DumpBiomesCommand;
1415
import com.mcmoddev.orespawn.data.Config;
1516
import com.mcmoddev.orespawn.api.SpawnLogic;
1617

1718
import java.nio.file.Paths;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.Map.Entry;
1823

1924
import com.google.common.base.Function;
2025
import com.google.common.base.Optional;
@@ -52,7 +57,8 @@ public class OreSpawn {
5257
public static final OS3Writer writer = new OS3Writer();
5358
public static final EventHandlers eventHandlers = new EventHandlers();
5459
public static final FeatureRegistry FEATURES = new FeatureRegistry();
55-
public static String OS1ConfigPath;
60+
private String OS1ConfigPath;
61+
public static final Map<Integer, List<SpawnEntry>> spawns = new HashMap<>();
5662

5763
@EventHandler
5864
public void preInit(FMLPreInitializationEvent ev) {
@@ -66,7 +72,7 @@ public void preInit(FMLPreInitializationEvent ev) {
6672
MinecraftForge.ORE_GEN_BUS.register(eventHandlers);
6773
}
6874

69-
OS1ConfigPath = Paths.get(ev.getSuggestedConfigurationFile().toPath().getParent().toString(),"orespawn").toString();
75+
this.OS1ConfigPath = Paths.get(ev.getSuggestedConfigurationFile().toPath().getParent().toString(),"orespawn").toString();
7076
FMLInterModComms.sendFunctionMessage("orespawn", "api", "com.mcmoddev.orespawn.data.VanillaOrespawn");
7177
}
7278

@@ -75,6 +81,7 @@ public void init(FMLInitializationEvent ev) {
7581
OS1Reader.loadEntries(Paths.get(OS1ConfigPath));
7682
OS2Reader.loadEntries();
7783
OS3Reader.loadEntries();
84+
API.registerSpawns();
7885
}
7986

8087
@EventHandler

src/main/java/com/mcmoddev/orespawn/api/OreSpawnAPI.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Map;
44

55
import com.mcmoddev.orespawn.api.SpawnLogic;
6+
import com.mcmoddev.orespawn.worldgen.OreSpawnWorldGen;
67

78
public interface OreSpawnAPI {
89
int DIMENSION_WILDCARD = "DIMENSION_WILDCARD".hashCode();
@@ -14,4 +15,8 @@ public interface OreSpawnAPI {
1415
Map<String, SpawnLogic> getAllSpawnLogic();
1516

1617
void registerSpawnLogic(String name, SpawnLogic logic);
18+
19+
OreSpawnWorldGen getWorldGenerator();
20+
21+
void registerSpawns();
1722
}

src/main/java/com/mcmoddev/orespawn/data/ReplacementsRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private ReplacementsRegistry() {
1919

2020
public static IBlockState getDimensionDefault(int dimension) {
2121
String[] names = { "minecraft:netherrack", "minecraft:stone", "minecraft:end_stone" };
22-
if( dimension < 0 || dimension > 1 ) {
22+
if( dimension < -1 || dimension > 1 ) {
2323
return ForgeRegistries.BLOCKS.getValue(new ResourceLocation(names[1])).getDefaultState();
2424
}
2525
return ForgeRegistries.BLOCKS.getValue(new ResourceLocation(names[dimension+1])).getDefaultState();

src/main/java/com/mcmoddev/orespawn/impl/OreSpawnImpl.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mcmoddev.orespawn.impl;
22

33
import com.google.common.collect.ImmutableMap;
4+
import com.mcmoddev.orespawn.api.DimensionLogic;
45
import com.mcmoddev.orespawn.api.OreSpawnAPI;
56
import com.mcmoddev.orespawn.api.SpawnLogic;
67

@@ -9,6 +10,7 @@
910
import net.minecraftforge.fml.common.registry.GameRegistry;
1011

1112
import java.util.*;
13+
import java.util.Map.Entry;
1214

1315
public class OreSpawnImpl implements OreSpawnAPI {
1416
private final Map<String, SpawnLogic> spawnLogic = new HashMap<>();
@@ -31,17 +33,31 @@ public Map<String, SpawnLogic> getAllSpawnLogic() {
3133

3234
public void registerSpawnLogic(String id, SpawnLogic spawnLogic) {
3335
this.spawnLogic.put(id, spawnLogic);
34-
35-
Random random = new Random();
36-
37-
worldGenerator = new OreSpawnWorldGen(spawnLogic.getAllDimensions(), random.nextLong());
38-
//if (!Config.getBoolean(Constants.RETROGEN_KEY)) {
39-
GameRegistry.registerWorldGenerator(worldGenerator, 100);
40-
//}
41-
42-
OreSpawn.LOGGER.info("Registered spawn logic for mod " + id);
4336
}
4437

38+
public void registerSpawns() {
39+
// build a proper tracking of data for the spawner
40+
for( Entry<String, SpawnLogic> ent : spawnLogic.entrySet() ) {
41+
for( Entry<Integer, DimensionLogic> dL : ent.getValue().getAllDimensions().entrySet()) {
42+
if( OreSpawn.spawns.containsKey(dL.getKey()) ) {
43+
OreSpawn.spawns.get(dL.getKey()).addAll(dL.getValue().getEntries());
44+
} else {
45+
OreSpawn.spawns.put(dL.getKey(), new ArrayList<>());
46+
OreSpawn.spawns.get(dL.getKey()).addAll(dL.getValue().getEntries());
47+
}
48+
}
49+
OreSpawn.LOGGER.info("Registered spawn logic for mod " + ent.getKey());
50+
}
51+
52+
Random random = new Random();
53+
54+
worldGenerator = new OreSpawnWorldGen(OreSpawn.spawns, random.nextLong());
55+
//if (!Config.getBoolean(Constants.RETROGEN_KEY)) {
56+
GameRegistry.registerWorldGenerator(worldGenerator, 100);
57+
//}
58+
59+
}
60+
4561
public OreSpawnWorldGen getWorldGenerator() {
4662
return worldGenerator;
4763
}

src/main/java/com/mcmoddev/orespawn/impl/features/DefaultFeatureGenerator.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
105105

106106
public static void spawnOre( BlockPos blockPos, IBlockState oreBlock, int quantity, World world, Random prng, IBlockState replaceBlock) {
107107
int count = quantity;
108-
// OreSpawn.LOGGER.fatal("Spawning block of "+oreBlock+" at "+blockPos+" with quantity "+quantity);
109108
if(quantity <= 8){
110109
int[] scrambledLUT = new int[offsetIndexRef_small.length];
111110
System.arraycopy(offsetIndexRef_small, 0, scrambledLUT, 0, scrambledLUT.length);
@@ -182,7 +181,6 @@ private static boolean canReplace(IBlockState target, IBlockState toReplace) {
182181
}
183182

184183
private static void spawn(IBlockState b, World w, BlockPos coord, int dimension, boolean cacheOverflow, IBlockState replaceBlock){
185-
//OreSpawn.LOGGER.fatal("!!!! Trying to spawn block at "+coord+" of type "+b.getBlock());
186184
IBlockState b2r = replaceBlock;
187185
if(b2r == null) {
188186
b2r = ReplacementsRegistry.getDimensionDefault(w.provider.getDimension());

src/main/java/com/mcmoddev/orespawn/worldgen/OreSpawnWorldGen.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import java.util.ArrayList;
44
import java.util.Collections;
5+
import java.util.HashMap;
56
import java.util.List;
67
import java.util.Map;
8+
import java.util.Map.Entry;
79
import java.util.Random;
810
import java.util.stream.Collectors;
911

@@ -12,8 +14,12 @@
1214
import com.mcmoddev.orespawn.api.IFeature;
1315
import com.mcmoddev.orespawn.api.OreSpawnAPI;
1416
import com.mcmoddev.orespawn.api.SpawnEntry;
17+
import com.mcmoddev.orespawn.api.SpawnLogic;
18+
import com.mcmoddev.orespawn.data.ReplacementsRegistry;
19+
import com.mcmoddev.orespawn.impl.SpawnLogicImpl;
1520

1621
import net.minecraft.block.Block;
22+
import net.minecraft.block.state.IBlockState;
1723
import net.minecraft.init.Blocks;
1824
import net.minecraft.item.ItemBlock;
1925
import net.minecraft.util.math.BlockPos;
@@ -26,12 +32,12 @@
2632

2733
public class OreSpawnWorldGen implements IWorldGenerator {
2834

29-
private final Map<Integer, DimensionLogic> dimensions;
35+
private final Map<Integer, List<SpawnEntry>> dimensions;
3036
public static final List<Block> SPAWN_BLOCKS = new ArrayList<>();
3137

3238
@SuppressWarnings("unused") private final long nextL;
3339

34-
public OreSpawnWorldGen(Map<Integer, DimensionLogic> allDimensions, long nextLong) {
40+
public OreSpawnWorldGen(Map<Integer, List<SpawnEntry>> allDimensions, long nextLong) {
3541
this.dimensions = Collections.unmodifiableMap(allDimensions);
3642
this.nextL = nextLong;
3743
if (SPAWN_BLOCKS.isEmpty()) {
@@ -41,41 +47,39 @@ public OreSpawnWorldGen(Map<Integer, DimensionLogic> allDimensions, long nextLon
4147
SPAWN_BLOCKS.addAll(OreDictionary.getOres("stone").stream().filter(stack -> stack.getItem() instanceof ItemBlock).map(stack -> ((ItemBlock) stack.getItem()).getBlock()).collect(Collectors.toList()));
4248
}
4349
}
44-
50+
4551
@Override
4652
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator,
4753
IChunkProvider chunkProvider) {
48-
49-
int thisDim = world.provider.getDimension();
50-
DimensionLogic dimensionLogic = this.dimensions.get(thisDim);
51-
List<SpawnEntry> entries = new ArrayList<>();
5254

53-
if( dimensionLogic == null ) {
55+
int thisDim = world.provider.getDimension();
56+
List<SpawnEntry> entries = this.dimensions.get(thisDim);
57+
if( entries == null ) {
5458
// no logic for this dimension, if this is nether or end, just exit
5559
if( thisDim == -1 || thisDim == 1 ) {
5660
return;
5761
}
5862

59-
dimensionLogic = this.dimensions.get(OreSpawnAPI.DIMENSION_WILDCARD);
60-
if( dimensionLogic == null ) {
61-
OreSpawn.LOGGER.fatal("no logic for dimension "+thisDim+" or for all dimensions");
63+
entries = this.dimensions.get(OreSpawnAPI.DIMENSION_WILDCARD);
64+
if( entries == null ) {
65+
OreSpawn.LOGGER.fatal("no spawn entries for dimension "+thisDim+" or for all dimensions");
6266
return;
6367
}
64-
entries.addAll(dimensionLogic.getEntries());
6568
} else if( thisDim != -1 && thisDim != 1 ) {
66-
dimensionLogic = this.dimensions.get(OreSpawnAPI.DIMENSION_WILDCARD);
67-
if( dimensionLogic != null ) {
68-
entries.addAll(dimensionLogic.getEntries());
69+
if( this.dimensions.get(OreSpawnAPI.DIMENSION_WILDCARD) != null ) {
70+
entries.addAll(this.dimensions.get(OreSpawnAPI.DIMENSION_WILDCARD));
6971
}
7072
}
7173

7274
for( SpawnEntry sE : entries ) {
7375
Biome biome = world.getBiomeProvider().getBiome(new BlockPos(chunkX*16, 64,chunkZ*16));
74-
// OreSpawn.LOGGER.fatal("Trying to generate in biome "+biome+" for spawn entry with block of type "+sE.getState());
7576
if( sE.getBiomes().contains(biome) || sE.getBiomes().equals(Collections.<Biome>emptyList()) || sE.getBiomes().isEmpty() ) {
7677
IFeature currentFeatureGen = sE.getFeatureGen();
77-
// what follows is a stop-gap
78-
currentFeatureGen.generate(random, chunkX, chunkZ, world, chunkGenerator, chunkProvider, sE.getParameters(), sE.getState(), sE.getReplacement());
78+
IBlockState replacement = sE.getReplacement();
79+
if( replacement == null ) {
80+
replacement = ReplacementsRegistry.getDimensionDefault(thisDim);
81+
}
82+
currentFeatureGen.generate(random, chunkX, chunkZ, world, chunkGenerator, chunkProvider, sE.getParameters(), sE.getState(), replacement);
7983
}
8084
}
8185
}

0 commit comments

Comments
 (0)