Search in sources :

Example 1 with IReplacementEntry

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);
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) IReplacementEntry(com.mcmoddev.orespawn.api.os3.IReplacementEntry) ResourceLocation(net.minecraft.util.ResourceLocation) IReplacementEntry(com.mcmoddev.orespawn.api.os3.IReplacementEntry) ReplacementEntry(com.mcmoddev.orespawn.impl.os3.ReplacementEntry)

Example 2 with IReplacementEntry

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());
    }
}
Also used : JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) StateUtil(com.mcmoddev.orespawn.util.StateUtil) JsonParser(com.google.gson.JsonParser) GsonBuilder(com.google.gson.GsonBuilder) IReplacementEntry(com.mcmoddev.orespawn.api.os3.IReplacementEntry) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) ItemStack(net.minecraft.item.ItemStack) Block(net.minecraft.block.Block) Charset(java.nio.charset.Charset) OreDictionary(net.minecraftforge.oredict.OreDictionary) Gson(com.google.gson.Gson) RegistryBuilder(net.minecraftforge.registries.RegistryBuilder) Map(java.util.Map) IForgeRegistryModifiable(net.minecraftforge.registries.IForgeRegistryModifiable) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) OreSpawn(com.mcmoddev.orespawn.OreSpawn) BufferedWriter(java.io.BufferedWriter) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) ReplacementEntry(com.mcmoddev.orespawn.impl.os3.ReplacementEntry) Collectors(java.util.stream.Collectors) IBlockState(net.minecraft.block.state.IBlockState) CrashReport(net.minecraft.crash.CrashReport) JsonArray(com.google.gson.JsonArray) List(java.util.List) TreeMap(java.util.TreeMap) Paths(java.nio.file.Paths) ForgeRegistries(net.minecraftforge.fml.common.registry.ForgeRegistries) ResourceLocation(net.minecraft.util.ResourceLocation) Collections(java.util.Collections) JsonArray(com.google.gson.JsonArray) Path(java.nio.file.Path) IReplacementEntry(com.mcmoddev.orespawn.api.os3.IReplacementEntry) GsonBuilder(com.google.gson.GsonBuilder) CrashReport(net.minecraft.crash.CrashReport) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 3 with IReplacementEntry

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);
    });
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) CrashReport(net.minecraft.crash.CrashReport) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) IReplacementEntry(com.mcmoddev.orespawn.api.os3.IReplacementEntry) ReplacementEntry(com.mcmoddev.orespawn.impl.os3.ReplacementEntry) LinkedList(java.util.LinkedList) JsonArray(com.google.gson.JsonArray) IReplacementEntry(com.mcmoddev.orespawn.api.os3.IReplacementEntry) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.util.ResourceLocation) Block(net.minecraft.block.Block) JsonParser(com.google.gson.JsonParser)

Aggregations

IReplacementEntry (com.mcmoddev.orespawn.api.os3.IReplacementEntry)3 ReplacementEntry (com.mcmoddev.orespawn.impl.os3.ReplacementEntry)3 IBlockState (net.minecraft.block.state.IBlockState)3 ResourceLocation (net.minecraft.util.ResourceLocation)3 JsonArray (com.google.gson.JsonArray)2 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 JsonParser (com.google.gson.JsonParser)2 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 Block (net.minecraft.block.Block)2 CrashReport (net.minecraft.crash.CrashReport)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 OreSpawn (com.mcmoddev.orespawn.OreSpawn)1 StateUtil (com.mcmoddev.orespawn.util.StateUtil)1 BufferedWriter (java.io.BufferedWriter)1 Charset (java.nio.charset.Charset)1 Files (java.nio.file.Files)1