use of com.mcmoddev.orespawn.api.os3.IReplacementEntry in project OreSpawn by MinecraftModDevelopmentMods.
the class ReplacementsRegistry method addBlock.
public void addBlock(String name, IBlockState state) {
ResourceLocation regName = new ResourceLocation(name);
if (registry.containsKey(regName)) {
IReplacementEntry old = registry.getValue(regName);
IReplacementEntry newRE;
List<IBlockState> oldList = old.getEntries();
oldList.add(state);
newRE = new ReplacementEntry(name, oldList);
registry.remove(regName);
newRE.setRegistryName(regName);
registry.register(newRE);
return;
}
IReplacementEntry r = new ReplacementEntry(name, state);
registry.register(r);
}
use of com.mcmoddev.orespawn.api.os3.IReplacementEntry in project OreSpawn by MinecraftModDevelopmentMods.
the class ReplacementsRegistry method saveFile.
public void saveFile(String modName) {
JsonObject outs = new JsonObject();
registry.getEntries().stream().filter(ent -> ent.getKey().getResourceDomain().equals(modName)).forEach(ent -> {
JsonArray entry = new JsonArray();
IReplacementEntry workVal = ent.getValue();
workVal.getEntries().stream().forEach(bs -> {
JsonObject block = new JsonObject();
block.addProperty(Constants.ConfigNames.BLOCK, bs.getBlock().getRegistryName().toString());
if (!bs.toString().matches("\\[normal\\]")) {
block.addProperty(Constants.ConfigNames.STATE, bs.toString().replaceAll("[\\[\\]]", ""));
}
entry.add(block);
});
outs.add(ent.getKey().toString(), entry);
});
Path p = Paths.get("config", "orespawn3", "sysconfig", String.format("replacements-%s.json", modName));
try (BufferedWriter w = Files.newBufferedWriter(p)) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String ov = gson.toJson(outs);
w.write(ov);
} catch (IOException e) {
CrashReport report = CrashReport.makeCrashReport(e, String.format("Failed writing replacements file %s", p.toAbsolutePath().toString()));
report.getCategory().addCrashSection("OreSpawn Version", Constants.VERSION);
OreSpawn.LOGGER.info(report.getCompleteReport());
}
}
use of com.mcmoddev.orespawn.api.os3.IReplacementEntry in project OreSpawn by MinecraftModDevelopmentMods.
the class ReplacementsRegistry method loadFile.
@SuppressWarnings("deprecation")
public void loadFile(Path file) {
JsonParser parser = new JsonParser();
JsonObject elements;
String rawJson;
try {
rawJson = FileUtils.readFileToString(file.toFile(), Charset.defaultCharset());
} catch (IOException e) {
CrashReport report = CrashReport.makeCrashReport(e, "Failed reading config " + file.getFileName());
report.getCategory().addCrashSection(ORE_SPAWN_VERSION, Constants.VERSION);
OreSpawn.LOGGER.info(report.getCompleteReport());
return;
}
elements = parser.parse(rawJson).getAsJsonObject();
elements.entrySet().stream().forEach(elem -> {
String entName = elem.getKey();
JsonArray entries = elem.getValue().getAsJsonArray();
List<IBlockState> blocks = new LinkedList<>();
for (JsonElement e : entries) {
JsonObject asObj = e.getAsJsonObject();
String blockName = asObj.get(Constants.ConfigNames.NAME).getAsString().toLowerCase();
// is this an OreDictionary entry ?
if (blockName.startsWith("ore:")) {
// yes, it is
String oreDictName = blockName.split(":")[1];
OreDictionary.getOres(oreDictName).forEach(iS -> {
if (iS.getMetadata() != 0) {
blocks.add(Block.getBlockFromItem(iS.getItem()).getStateFromMeta(iS.getMetadata()));
} else {
blocks.add(Block.getBlockFromItem(iS.getItem()).getDefaultState());
}
});
} else {
String state = null;
ResourceLocation blockRL = new ResourceLocation(blockName);
Block theBlock = ForgeRegistries.BLOCKS.getValue(blockRL);
if (asObj.has(Constants.ConfigNames.METADATA)) {
// has metadata
int meta = asObj.get(Constants.ConfigNames.METADATA).getAsInt();
blocks.add(theBlock.getStateFromMeta(meta));
} else if (asObj.has(Constants.ConfigNames.STATE)) {
// has a state
state = asObj.get(Constants.ConfigNames.STATE).getAsString();
blocks.add(StateUtil.deserializeState(theBlock, state));
} else {
// use the default state
blocks.add(theBlock.getDefaultState());
}
}
}
IReplacementEntry replacer = new ReplacementEntry("orespawn:" + entName, blocks);
registry.register(replacer);
});
}
Aggregations