Skip to content

Commit 71e84e1

Browse files
committed
Add /addore and /dumpbiomes
Remove legacy json converter Fix /help command
1 parent 47ca363 commit 71e84e1

File tree

7 files changed

+229
-130
lines changed

7 files changed

+229
-130
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import com.google.common.base.Optional;
55
import mmd.orespawn.api.OreSpawnAPI;
66
import mmd.orespawn.api.SpawnLogic;
7+
import mmd.orespawn.command.AddOreCommand;
78
import mmd.orespawn.command.ClearChunkCommand;
9+
import mmd.orespawn.command.DumpBiomesCommand;
810
import mmd.orespawn.impl.OreSpawnImpl;
911
import mmd.orespawn.json.OreSpawnReader;
1012
import mmd.orespawn.json.OreSpawnWriter;
@@ -42,13 +44,14 @@ public void onPreInit(FMLPreInitializationEvent event) {
4244

4345
@Mod.EventHandler
4446
public void onPostInit(FMLPostInitializationEvent event) {
45-
OreSpawnReader.INSTANCE.convertOldSpawnEntries();
4647
OreSpawnWriter.INSTANCE.writeSpawnEntries();
4748
}
4849

4950
@Mod.EventHandler
5051
public void onServerStarting(FMLServerStartingEvent event) {
5152
event.registerServerCommand(new ClearChunkCommand());
53+
event.registerServerCommand(new AddOreCommand());
54+
event.registerServerCommand(new DumpBiomesCommand());
5255
}
5356

5457
@Mod.EventHandler
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package mmd.orespawn.command;
2+
3+
import com.google.gson.*;
4+
import mmd.orespawn.api.OreSpawnAPI;
5+
import mmd.orespawn.util.StateUtil;
6+
import net.minecraft.block.state.IBlockState;
7+
import net.minecraft.command.CommandBase;
8+
import net.minecraft.command.CommandException;
9+
import net.minecraft.command.ICommand;
10+
import net.minecraft.command.ICommandSender;
11+
import net.minecraft.entity.player.EntityPlayer;
12+
import net.minecraft.item.ItemBlock;
13+
import net.minecraft.item.ItemStack;
14+
import net.minecraft.server.MinecraftServer;
15+
import net.minecraft.util.EnumHand;
16+
import net.minecraft.util.text.TextComponentString;
17+
import org.apache.commons.io.Charsets;
18+
import org.apache.commons.io.FileUtils;
19+
import org.apache.commons.lang3.StringEscapeUtils;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
24+
public class AddOreCommand extends CommandBase {
25+
@Override
26+
public String getCommandName() {
27+
return "addore";
28+
}
29+
30+
@Override
31+
public String getCommandUsage(ICommandSender sender) {
32+
return "/addore <file> <dimension|all>";
33+
}
34+
35+
@Override
36+
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
37+
if (!(sender instanceof EntityPlayer)) {
38+
throw new CommandException("Only players can use this command");
39+
}
40+
41+
EntityPlayer player = (EntityPlayer) sender;
42+
ItemStack stack = player.getHeldItem(EnumHand.MAIN_HAND);
43+
44+
if (stack == null) {
45+
throw new CommandException("You have no item in your main hand");
46+
} else if (!(stack.getItem() instanceof ItemBlock)) {
47+
throw new CommandException("The item in your main hand isn't a block");
48+
} else if (args.length != 2) {
49+
throw new CommandException(this.getCommandUsage(sender));
50+
}
51+
52+
File file = new File(".", "orespawn" + File.separator + args[0] + ".json");
53+
JsonParser parser = new JsonParser();
54+
IBlockState state = ((ItemBlock) stack.getItem()).getBlock().getStateFromMeta(stack.getItemDamage());
55+
56+
if (!file.exists()) {
57+
throw new CommandException("That file doesn't exist" + (args[0].endsWith(".json") ? " (don't add .json)" : ""));
58+
}
59+
60+
int dimension = OreSpawnAPI.DIMENSION_WILDCARD;
61+
62+
try {
63+
if (!args[1].equals("all")) {
64+
dimension = Integer.parseInt(args[1]);
65+
}
66+
} catch (NumberFormatException e) {
67+
throw new CommandException(args[1] + " isn't a valid dimension");
68+
}
69+
70+
JsonObject ore = new JsonObject();
71+
ore.addProperty("block", state.getBlock().getRegistryName().toString());
72+
ore.addProperty("state", StateUtil.serializeState(state));
73+
ore.addProperty("size", 25);
74+
ore.addProperty("variation", 12);
75+
ore.addProperty("frequency", 20);
76+
ore.addProperty("min_height", 0);
77+
ore.addProperty("max_height", 128);
78+
79+
try {
80+
JsonArray json = parser.parse(FileUtils.readFileToString(file)).getAsJsonArray();
81+
82+
for (JsonElement element : json) {
83+
JsonObject object = element.getAsJsonObject();
84+
85+
if (object.has("dimension") ? dimension == object.get("dimension").getAsInt() : dimension == OreSpawnAPI.DIMENSION_WILDCARD) {
86+
object.get("ores").getAsJsonArray().add(ore);
87+
this.saveFile(json, file);
88+
89+
return;
90+
}
91+
}
92+
93+
JsonObject object = new JsonObject();
94+
95+
if (dimension != OreSpawnAPI.DIMENSION_WILDCARD) {
96+
object.addProperty("dimension", dimension);
97+
}
98+
99+
JsonArray array = new JsonArray();
100+
array.add(ore);
101+
object.add("ores", array);
102+
103+
this.saveFile(json, file);
104+
} catch (IOException e) {
105+
throw new CommandException("Failed to read the json file");
106+
}
107+
108+
player.addChatComponentMessage(new TextComponentString("Added " + state.getBlock().getRegistryName().toString() + " to the json"));
109+
}
110+
111+
private void saveFile(JsonArray array, File file) throws CommandException {
112+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
113+
String json = gson.toJson(array);
114+
115+
try {
116+
FileUtils.writeStringToFile(file, StringEscapeUtils.unescapeJson(json), Charsets.UTF_8);
117+
} catch (IOException e) {
118+
throw new CommandException("Failed to save the updated json file");
119+
}
120+
}
121+
122+
@Override
123+
public int compareTo(ICommand command) {
124+
return this.getCommandName().compareTo(command.getCommandName());
125+
}
126+
}

src/main/java/mmd/orespawn/command/ClearChunkCommand.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.minecraft.block.Block;
55
import net.minecraft.command.CommandBase;
66
import net.minecraft.command.CommandException;
7+
import net.minecraft.command.ICommand;
78
import net.minecraft.command.ICommandSender;
89
import net.minecraft.entity.player.EntityPlayer;
910
import net.minecraft.server.MinecraftServer;
@@ -45,4 +46,9 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
4546
}
4647
}
4748
}
49+
50+
@Override
51+
public int compareTo(ICommand command) {
52+
return this.getCommandName().compareTo(command.getCommandName());
53+
}
4854
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package mmd.orespawn.command;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.JsonArray;
6+
import com.google.gson.JsonPrimitive;
7+
import net.minecraft.command.CommandBase;
8+
import net.minecraft.command.CommandException;
9+
import net.minecraft.command.ICommand;
10+
import net.minecraft.command.ICommandSender;
11+
import net.minecraft.server.MinecraftServer;
12+
import net.minecraft.util.text.TextComponentString;
13+
import net.minecraft.world.biome.Biome;
14+
import net.minecraftforge.fml.common.registry.ForgeRegistries;
15+
import org.apache.commons.io.Charsets;
16+
import org.apache.commons.io.FileUtils;
17+
import org.apache.commons.lang3.StringEscapeUtils;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
public class DumpBiomesCommand extends CommandBase {
23+
@Override
24+
public String getCommandName() {
25+
return "dumpbiomes";
26+
}
27+
28+
@Override
29+
public String getCommandUsage(ICommandSender sender) {
30+
return "/dumpbiomes";
31+
}
32+
33+
@Override
34+
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
35+
JsonArray array = new JsonArray();
36+
37+
for (Biome biome : ForgeRegistries.BIOMES) {
38+
array.add(new JsonPrimitive(biome.getRegistryName().toString()));
39+
}
40+
41+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
42+
String json = gson.toJson(array);
43+
44+
try {
45+
FileUtils.writeStringToFile(new File(".", "biome_dump.json"), StringEscapeUtils.unescapeJson(json), Charsets.UTF_8);
46+
} catch (IOException e) {
47+
throw new CommandException("Failed to save the json file");
48+
}
49+
50+
sender.addChatMessage(new TextComponentString("Done"));
51+
}
52+
53+
@Override
54+
public int compareTo(ICommand command) {
55+
return this.getCommandName().compareTo(command.getCommandName());
56+
}
57+
}

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

Lines changed: 3 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import mmd.orespawn.api.DimensionLogic;
66
import mmd.orespawn.api.OreSpawnAPI;
77
import mmd.orespawn.api.SpawnLogic;
8+
import mmd.orespawn.util.StateUtil;
89
import net.minecraft.block.Block;
910
import net.minecraft.block.state.IBlockState;
1011
import net.minecraft.crash.CrashReport;
@@ -68,23 +69,9 @@ public void readSpawnEntries() {
6869

6970
if (ore.has("state")) {
7071
String stateString = ore.get("state").getAsString();
71-
boolean foundState = false;
72+
state = StateUtil.deserializeState(block, stateString);
7273

73-
for (IBlockState validState : block.getBlockState().getValidStates()) {
74-
String string = validState.toString();
75-
string = string.substring(string.indexOf("[") + 1, string.length() - (string.endsWith("]") ? 1 : 0));
76-
if (string.equals(block.getRegistryName().toString())) {
77-
string = "";
78-
}
79-
80-
if (stateString.equals(string)) {
81-
state = validState;
82-
foundState = true;
83-
break;
84-
}
85-
}
86-
87-
if (!foundState) {
74+
if (state == null) {
8875
throw new RuntimeException("Invalid state " + stateString + " for block " + block.getRegistryName());
8976
}
9077
}
@@ -120,106 +107,4 @@ public void readSpawnEntries() {
120107
}
121108
});
122109
}
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-
}
225110
}

0 commit comments

Comments
 (0)