Skip to content

Commit 6886a65

Browse files
committed
Add json converter and relicense to lgpl2.1
1 parent 13c8f4b commit 6886a65

File tree

8 files changed

+659
-80
lines changed

8 files changed

+659
-80
lines changed

LICENSE

Lines changed: 494 additions & 21 deletions
Large diffs are not rendered by default.

build.gradle

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ archivesBaseName = "orespawn"
3131
sourceCompatibility = targetCompatibility = "1.8"
3232

3333
class Secrets {
34-
def data = null
34+
def data = null
3535

36-
def getProperty(String key) {
37-
return data ? data[key] : ""
38-
}
36+
def getProperty(String key) {
37+
return data ? data[key] : ""
38+
}
3939
}
4040

4141
import groovy.json.JsonSlurper
@@ -49,9 +49,9 @@ if (System.getenv().SECRET_FILE) {
4949

5050
project.ext.secret = new Secrets()
5151
if (secretFile.exists()) {
52-
secretFile.withReader {
53-
project.ext.secret.data = new JsonSlurper().parse it
54-
}
52+
secretFile.withReader {
53+
project.ext.secret.data = new JsonSlurper().parse it
54+
}
5555
}
5656

5757
minecraft {
@@ -145,6 +145,9 @@ curseforge {
145145
releaseType = "alpha"
146146
addGameVersion("1.10.2")
147147
def displayVersion = getVersion("VERSION", mod_file)
148+
if (System.getenv().BUILD_NUMBER) {
149+
displayVersion += "." + System.getenv().BUILD_NUMBER
150+
}
148151
mainArtifact(jar) {
149152
displayName = "OreSpawn $displayVersion"
150153
}

src/main/java/mmd/orespawn/OreSpawn.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class OreSpawn {
2626
public void onPreInit(FMLPreInitializationEvent event) {
2727
MinecraftForge.ORE_GEN_BUS.register(EventHandler.INSTANCE);
2828
OreSpawnReader.INSTANCE.readSpawnEntries();
29+
OreSpawnReader.INSTANCE.convertOldSpawnEntries();
2930

3031
FMLInterModComms.sendFunctionMessage("orespawn", "api", "mmd.orespawn.VanillaOreSpawn");
3132
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package mmd.orespawn.api;
22

3+
import java.util.Map;
4+
35
public interface OreSpawnAPI {
46
int DIMENSION_WILDCARD = "DIMENSION_WILDCARD".hashCode();
57

68
SpawnLogic createSpawnLogic();
79

810
SpawnLogic getSpawnLogic(String id);
11+
12+
Map<String, SpawnLogic> getAllSpawnLogic();
913
}

src/main/java/mmd/orespawn/impl/DimensionLogicImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class DimensionLogicImpl implements DimensionLogic {
1515
private final List<SpawnEntry> logic = new ArrayList<>();
1616
private final SpawnLogic parent;
1717

18-
DimensionLogicImpl(SpawnLogic parent) {
18+
public DimensionLogicImpl(SpawnLogic parent) {
1919
this.parent = parent;
2020
}
2121

@@ -34,5 +34,4 @@ public Collection<SpawnEntry> getEntries() {
3434
public SpawnLogic end() {
3535
return this.parent;
3636
}
37-
3837
}

src/main/java/mmd/orespawn/impl/OreSpawnImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mmd.orespawn.impl;
22

3+
import com.google.common.collect.ImmutableMap;
34
import mmd.orespawn.OreSpawn;
45
import mmd.orespawn.api.DimensionLogic;
56
import mmd.orespawn.api.OreSpawnAPI;
@@ -25,6 +26,11 @@ public SpawnLogic getSpawnLogic(String id) {
2526
return this.spawnLogic.get(id);
2627
}
2728

29+
@Override
30+
public Map<String, SpawnLogic> getAllSpawnLogic() {
31+
return ImmutableMap.copyOf(this.spawnLogic);
32+
}
33+
2834
public void registerSpawnLogic(String id, SpawnLogic spawnLogic) {
2935
this.spawnLogic.put(id, spawnLogic);
3036

src/main/java/mmd/orespawn/json/OreSpawnReader.java

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package mmd.orespawn.json;
22

3-
import com.google.gson.JsonArray;
4-
import com.google.gson.JsonElement;
5-
import com.google.gson.JsonObject;
6-
import com.google.gson.JsonParser;
3+
import com.google.gson.*;
74
import mmd.orespawn.OreSpawn;
85
import mmd.orespawn.api.DimensionLogic;
96
import mmd.orespawn.api.OreSpawnAPI;
@@ -123,4 +120,106 @@ public void readSpawnEntries() {
123120
}
124121
});
125122
}
123+
124+
@Deprecated //planned for removal in 2.1.0
125+
public void convertOldSpawnEntries() {
126+
File directory = new File(".", "config" + File.separator + "orespawn");
127+
JsonParser parser = new JsonParser();
128+
129+
if (!directory.exists() || !directory.isDirectory()) {
130+
return;
131+
}
132+
133+
File[] files = directory.listFiles();
134+
135+
if (files == null) {
136+
return;
137+
}
138+
139+
Arrays.stream(files).filter(file -> file.getName().endsWith(".json")).forEach(file -> {
140+
try {
141+
OreSpawn.LOGGER.info("Converting JSON " + file.getName());
142+
143+
JsonElement element = parser.parse(FileUtils.readFileToString(file));
144+
JsonObject object = element.getAsJsonObject();
145+
JsonArray dimensions = object.get("dimensions").getAsJsonArray();
146+
147+
SpawnLogic spawnLogic = OreSpawn.API.createSpawnLogic();
148+
149+
for (JsonElement dimensionsEntry : dimensions) {
150+
JsonObject dimension = dimensionsEntry.getAsJsonObject();
151+
152+
int newDimension = OreSpawnAPI.DIMENSION_WILDCARD;
153+
154+
JsonPrimitive id = dimension.get("dimension").getAsJsonPrimitive();
155+
if (id.isNumber()) {
156+
newDimension = id.getAsInt();
157+
}
158+
159+
DimensionLogic dimensionLogic = spawnLogic.getDimension(newDimension);
160+
161+
JsonArray ores = dimension.get("ores").getAsJsonArray();
162+
163+
for (JsonElement oreEntry : ores) {
164+
JsonObject ore = oreEntry.getAsJsonObject();
165+
166+
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(ore.get("blockID").getAsString()));
167+
168+
if (block == null) {
169+
continue;
170+
}
171+
172+
IBlockState state = block.getDefaultState();
173+
174+
if (ore.has("blockMeta")) {
175+
state = block.getStateFromMeta(ore.get("blockMeta").getAsInt());
176+
}
177+
178+
int size = ore.get("size").getAsInt();
179+
int variation = ore.get("variation").getAsInt();
180+
int frequency = ore.get("frequency").getAsInt();
181+
int minHeight = ore.get("minHeight").getAsInt();
182+
int maxHeight = ore.get("maxHeight").getAsInt();
183+
List<Biome> biomes = new ArrayList<>();
184+
185+
if (ore.has("biomes")) {
186+
JsonArray biomesArray = ore.get("biomes").getAsJsonArray();
187+
188+
for (JsonElement biomeEntry : biomesArray) {
189+
String biome = biomeEntry.getAsString();
190+
191+
try {
192+
int biomeID = Integer.parseInt(biome);
193+
Biome result = Biome.getBiome(biomeID);
194+
195+
if (result != null) {
196+
biomes.add(result);
197+
}
198+
} catch (NumberFormatException e) {
199+
for (Biome result : ForgeRegistries.BIOMES) {
200+
if (result.getBiomeName().equals(biome)) {
201+
biomes.add(result);
202+
break;
203+
}
204+
}
205+
}
206+
}
207+
}
208+
209+
dimensionLogic.addOre(state, size, variation, frequency, minHeight, maxHeight, biomes.toArray(new Biome[biomes.size()]));
210+
}
211+
212+
}
213+
214+
OreSpawn.API.registerSpawnLogic(file.getName().substring(0, file.getName().lastIndexOf(".")), spawnLogic);
215+
file.delete();
216+
} catch (Exception e) {
217+
CrashReport report = CrashReport.makeCrashReport(e, "Failed reading config " + file.getName());
218+
report.getCategory().addCrashSection("OreSpawn Version", OreSpawn.VERSION);
219+
OreSpawn.LOGGER.info(report.getCompleteReport());
220+
}
221+
});
222+
223+
directory.delete();
224+
}
126225
}

src/main/java/mmd/orespawn/json/OreSpawnWriter.java

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
import com.google.gson.*;
44
import mmd.orespawn.OreSpawn;
55
import mmd.orespawn.api.DimensionLogic;
6-
import mmd.orespawn.api.SpawnEntry;
76
import mmd.orespawn.api.OreSpawnAPI;
7+
import mmd.orespawn.api.SpawnEntry;
88
import mmd.orespawn.api.SpawnLogic;
99
import net.minecraft.block.state.IBlockState;
1010
import net.minecraft.world.biome.Biome;
11-
import net.minecraftforge.fml.common.Loader;
12-
import net.minecraftforge.fml.common.ModContainer;
1311
import org.apache.commons.io.Charsets;
1412
import org.apache.commons.io.FileUtils;
1513
import org.apache.commons.lang3.StringEscapeUtils;
@@ -25,68 +23,64 @@ public enum OreSpawnWriter {
2523
public void writeSpawnEntries() {
2624
Gson gson = new GsonBuilder().setPrettyPrinting().create();
2725

28-
Loader.instance().getActiveModList().stream().map(ModContainer::getModId).forEach(id -> {
29-
SpawnLogic logic = OreSpawn.API.getSpawnLogic(id);
26+
for (Map.Entry<String, SpawnLogic> entry : OreSpawn.API.getAllSpawnLogic().entrySet()) {
27+
Map<Integer, DimensionLogic> dimensions = entry.getValue().getAllDimensions();
3028

31-
if (logic != null) {
32-
Map<Integer, DimensionLogic> dimensions = logic.getAllDimensions();
29+
JsonArray array = new JsonArray();
3330

34-
JsonArray array = new JsonArray();
31+
for (Map.Entry<Integer, DimensionLogic> dimension : dimensions.entrySet()) {
32+
JsonObject object = new JsonObject();
3533

36-
for (Map.Entry<Integer, DimensionLogic> dimension : dimensions.entrySet()) {
37-
JsonObject object = new JsonObject();
38-
39-
if (dimension.getKey() != OreSpawnAPI.DIMENSION_WILDCARD) {
40-
object.addProperty("dimension", dimension.getKey());
41-
}
42-
43-
JsonArray ores = new JsonArray();
44-
Collection<SpawnEntry> entries = dimension.getValue().getEntries();
34+
if (dimension.getKey() != OreSpawnAPI.DIMENSION_WILDCARD) {
35+
object.addProperty("dimension", dimension.getKey());
36+
}
4537

46-
for (SpawnEntry entry : entries) {
47-
JsonObject ore = new JsonObject();
38+
JsonArray ores = new JsonArray();
39+
Collection<SpawnEntry> entries = dimension.getValue().getEntries();
4840

49-
ore.addProperty("block", entry.getState().getBlock().getRegistryName().toString());
41+
for (SpawnEntry spawnEntry : entries) {
42+
JsonObject ore = new JsonObject();
5043

51-
if (entry.getState() != entry.getState().getBlock().getDefaultState()) {
52-
ore.addProperty("state", this.serializeBlockState(entry.getState()));
53-
}
44+
ore.addProperty("block", spawnEntry.getState().getBlock().getRegistryName().toString());
5445

55-
ore.addProperty("size", entry.getSize());
56-
ore.addProperty("variation", entry.getVariation());
57-
ore.addProperty("frequency", entry.getFrequency());
58-
ore.addProperty("min_height", entry.getMinHeight());
59-
ore.addProperty("max_height", entry.getMaxHeight());
46+
if (spawnEntry.getState() != spawnEntry.getState().getBlock().getDefaultState()) {
47+
ore.addProperty("state", this.serializeBlockState(spawnEntry.getState()));
48+
}
6049

61-
Biome[] biomeArray = entry.getBiomes();
50+
ore.addProperty("size", spawnEntry.getSize());
51+
ore.addProperty("variation", spawnEntry.getVariation());
52+
ore.addProperty("frequency", spawnEntry.getFrequency());
53+
ore.addProperty("min_height", spawnEntry.getMinHeight());
54+
ore.addProperty("max_height", spawnEntry.getMaxHeight());
6255

63-
if (biomeArray != null && biomeArray.length != 0) {
64-
JsonArray biomes = new JsonArray();
56+
Biome[] biomeArray = spawnEntry.getBiomes();
6557

66-
for (Biome biome : biomeArray) {
67-
biomes.add(new JsonPrimitive(biome.getRegistryName().toString()));
68-
}
58+
if (biomeArray != null && biomeArray.length != 0) {
59+
JsonArray biomes = new JsonArray();
6960

70-
ore.add("biomes", biomes);
61+
for (Biome biome : biomeArray) {
62+
biomes.add(new JsonPrimitive(biome.getRegistryName().toString()));
7163
}
7264

73-
ores.add(ore);
65+
ore.add("biomes", biomes);
7466
}
7567

76-
object.add("ores", ores);
77-
78-
array.add(object);
68+
ores.add(ore);
7969
}
8070

81-
String json = gson.toJson(array);
71+
object.add("ores", ores);
8272

83-
try {
84-
FileUtils.writeStringToFile(new File("." + File.separator + "orespawn", id + ".json"), StringEscapeUtils.unescapeJson(json), Charsets.UTF_8);
85-
} catch (IOException e) {
86-
e.printStackTrace();
87-
}
73+
array.add(object);
8874
}
89-
});
75+
76+
String json = gson.toJson(array);
77+
78+
try {
79+
FileUtils.writeStringToFile(new File("." + File.separator + "orespawn", entry.getKey() + ".json"), StringEscapeUtils.unescapeJson(json), Charsets.UTF_8);
80+
} catch (IOException e) {
81+
e.printStackTrace();
82+
}
83+
}
9084
}
9185

9286
private String serializeBlockState(IBlockState state) {

0 commit comments

Comments
 (0)