Search in sources :

Example 1 with ReplacementEntry

use of com.mcmoddev.orespawn.impl.os3.ReplacementEntry 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 ReplacementEntry

use of com.mcmoddev.orespawn.impl.os3.ReplacementEntry 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)2 ReplacementEntry (com.mcmoddev.orespawn.impl.os3.ReplacementEntry)2 IBlockState (net.minecraft.block.state.IBlockState)2 ResourceLocation (net.minecraft.util.ResourceLocation)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 Block (net.minecraft.block.Block)1 CrashReport (net.minecraft.crash.CrashReport)1