From 27217f5854b217dc3f468791d815f8620248c965 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 12 Nov 2018 00:32:35 +0100 Subject: [PATCH] some api and config for the pickaxe conversion and flower generator --- .../java/de/ellpeck/naturesaura/Helper.java | 34 +++++++++++++++++++ .../de/ellpeck/naturesaura/ModConfig.java | 26 ++++++++++++++ .../de/ellpeck/naturesaura/NaturesAura.java | 1 + .../naturesaura/api/NaturesAuraAPI.java | 16 +++++++++ .../tiles/TileEntityFlowerGenerator.java | 7 ++-- .../naturesaura/events/CommonEvents.java | 2 ++ .../items/tools/ItemPickaxeNA.java | 17 ++++++---- 7 files changed, 92 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/ellpeck/naturesaura/Helper.java b/src/main/java/de/ellpeck/naturesaura/Helper.java index 0d7224f9..904d5795 100644 --- a/src/main/java/de/ellpeck/naturesaura/Helper.java +++ b/src/main/java/de/ellpeck/naturesaura/Helper.java @@ -5,6 +5,9 @@ import de.ellpeck.naturesaura.api.NACapabilities; import de.ellpeck.naturesaura.api.aura.item.IAuraRecharge; import de.ellpeck.naturesaura.blocks.tiles.TileEntityImpl; import de.ellpeck.naturesaura.compat.Compat; +import net.minecraft.block.Block; +import net.minecraft.block.properties.IProperty; +import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.RenderHelper; @@ -16,6 +19,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; +import net.minecraft.util.ResourceLocation; import net.minecraft.util.SoundCategory; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; @@ -180,4 +184,34 @@ public final class Helper { } }; } + + public static IBlockState getStateFromString(String raw) { + String[] split = raw.split("\\["); + Block block = Block.REGISTRY.getObject(new ResourceLocation(split[0])); + if (block != null) { + IBlockState state = block.getDefaultState(); + if (split.length > 1) { + for (String part : split[1].replace("]", "").split(",")) { + String[] keyValue = part.split("="); + for (IProperty prop : state.getProperties().keySet()) { + IBlockState changed = findProperty(state, prop, keyValue[0], keyValue[1]); + if (changed != null) { + state = changed; + break; + } + } + } + } + return state; + } else + return null; + } + + private static > IBlockState findProperty(IBlockState state, IProperty prop, String key, String newValue) { + if (key.equals(prop.getName())) + for (T value : prop.getAllowedValues()) + if (prop.getName(value).equals(newValue)) + return state.withProperty(prop, value); + return null; + } } diff --git a/src/main/java/de/ellpeck/naturesaura/ModConfig.java b/src/main/java/de/ellpeck/naturesaura/ModConfig.java index c5a73d99..43270bc7 100644 --- a/src/main/java/de/ellpeck/naturesaura/ModConfig.java +++ b/src/main/java/de/ellpeck/naturesaura/ModConfig.java @@ -1,5 +1,6 @@ package de.ellpeck.naturesaura; +import de.ellpeck.naturesaura.api.NaturesAuraAPI; import net.minecraftforge.common.config.Config; import net.minecraftforge.common.config.Config.Comment; import net.minecraftforge.common.config.Config.RangeDouble; @@ -15,6 +16,11 @@ public final class ModConfig { @Comment("If using Dragon's Breath in a Brewing Stand should not cause a glass bottle to appear") public boolean removeDragonBreathContainerItem = true; + @Comment("Additional conversion recipes for the Botanist's Pickaxe right click function. Each entry needs to be formatted as modid:input_block[prop1=value1,...]->modid:output_block[prop1=value1,...] where block state properties are optional") + public String[] additionalBotanistPickaxeConversions = new String[0]; + + @Comment("Additional blocks that the Herbivorous Absorber can consume to generate Aura. Each entry needs to be formatted as modid:block[prop1=value1,...] where block state properties are optional") + public String[] additionalHerbivorousAbsorberFlowers = new String[0]; } public static class Client { @@ -26,4 +32,24 @@ public final class ModConfig { @Comment("If particle spawning should respect the particle setting in Minecraft's video settings screen") public boolean respectVanillaParticleSettings = true; } + + public static void initOrReload() { + try { + for (String s : general.additionalBotanistPickaxeConversions) { + String[] split = s.split("->"); + NaturesAuraAPI.BOTANIST_PICKAXE_CONVERSIONS.put( + Helper.getStateFromString(split[0]), + Helper.getStateFromString(split[1])); + } + } catch (Exception e) { + NaturesAura.LOGGER.warn("Error parsing additionalBotanistPickaxeConversions", e); + } + + try { + for (String s : general.additionalHerbivorousAbsorberFlowers) + NaturesAuraAPI.FLOWER_GENERATOR_BLOCKS.add(Helper.getStateFromString(s)); + } catch (Exception e) { + NaturesAura.LOGGER.warn("Error parsing additionalHerbivorousAbsorberFlowers", e); + } + } } diff --git a/src/main/java/de/ellpeck/naturesaura/NaturesAura.java b/src/main/java/de/ellpeck/naturesaura/NaturesAura.java index 81676e59..b3533b1e 100644 --- a/src/main/java/de/ellpeck/naturesaura/NaturesAura.java +++ b/src/main/java/de/ellpeck/naturesaura/NaturesAura.java @@ -76,6 +76,7 @@ public final class NaturesAura { @EventHandler public void init(FMLInitializationEvent event) { + ModConfig.initOrReload(); ModRecipes.init(); ModRegistry.init(event); OreDict.init(); diff --git a/src/main/java/de/ellpeck/naturesaura/api/NaturesAuraAPI.java b/src/main/java/de/ellpeck/naturesaura/api/NaturesAuraAPI.java index 8344bfb5..afbe36d6 100644 --- a/src/main/java/de/ellpeck/naturesaura/api/NaturesAuraAPI.java +++ b/src/main/java/de/ellpeck/naturesaura/api/NaturesAuraAPI.java @@ -5,13 +5,17 @@ import de.ellpeck.naturesaura.api.aura.container.IAuraContainer; import de.ellpeck.naturesaura.api.internal.StubHooks; import de.ellpeck.naturesaura.api.recipes.AltarRecipe; import de.ellpeck.naturesaura.api.recipes.TreeRitualRecipe; +import net.minecraft.block.BlockFlower; +import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import org.apache.commons.lang3.mutable.MutableInt; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.function.BiConsumer; @@ -35,6 +39,18 @@ public final class NaturesAuraAPI { * added to this list. */ public static final Map TREE_RITUAL_RECIPES = new HashMap<>(); + /** + * The list of all types of flowers that the flower generator can use for + * consumption. By default, all {@link BlockFlower} instances and all blocks + * specified in the config file are added + */ + public static final List FLOWER_GENERATOR_BLOCKS = new ArrayList<>(); + /** + * A map of all of the block states that the Botanist's Pickaxe can convert + * into their mossy variations. Contains mossy brick and mossy cobblestone + * by default, along with all blocks specified in the config file + */ + public static final Map BOTANIST_PICKAXE_CONVERSIONS = new HashMap<>(); /** * This method returns the active {@link IInternalHooks} instance which can diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFlowerGenerator.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFlowerGenerator.java index 063ecbb6..9d348523 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFlowerGenerator.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFlowerGenerator.java @@ -1,6 +1,7 @@ package de.ellpeck.naturesaura.blocks.tiles; import de.ellpeck.naturesaura.Helper; +import de.ellpeck.naturesaura.api.NaturesAuraAPI; import de.ellpeck.naturesaura.api.aura.AuraType; import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk; import de.ellpeck.naturesaura.packet.PacketHandler; @@ -25,12 +26,10 @@ import java.util.Map; public class TileEntityFlowerGenerator extends TileEntityImpl implements ITickable { - private static final List FLOWERS = new ArrayList<>(); - static { for (Block block : ForgeRegistries.BLOCKS) { if (block instanceof BlockFlower) { - FLOWERS.addAll(block.getBlockState().getValidStates()); + NaturesAuraAPI.FLOWER_GENERATOR_BLOCKS.addAll(block.getBlockState().getValidStates()); } } } @@ -46,7 +45,7 @@ public class TileEntityFlowerGenerator extends TileEntityImpl implements ITickab for (int z = -range; z <= range; z++) { BlockPos offset = this.pos.add(x, 0, z); IBlockState state = this.world.getBlockState(offset); - if (FLOWERS.contains(state)) { + if (NaturesAuraAPI.FLOWER_GENERATOR_BLOCKS.contains(state)) { possible.add(offset); } } diff --git a/src/main/java/de/ellpeck/naturesaura/events/CommonEvents.java b/src/main/java/de/ellpeck/naturesaura/events/CommonEvents.java index c4feb5fc..b5898395 100644 --- a/src/main/java/de/ellpeck/naturesaura/events/CommonEvents.java +++ b/src/main/java/de/ellpeck/naturesaura/events/CommonEvents.java @@ -1,5 +1,6 @@ package de.ellpeck.naturesaura.events; +import de.ellpeck.naturesaura.ModConfig; import de.ellpeck.naturesaura.NaturesAura; import de.ellpeck.naturesaura.api.NACapabilities; import de.ellpeck.naturesaura.api.aura.AuraType; @@ -58,6 +59,7 @@ public class CommonEvents { public void onConfigChanged(OnConfigChangedEvent event) { if (NaturesAura.MOD_ID.equals(event.getModID())) { ConfigManager.sync(NaturesAura.MOD_ID, Config.Type.INSTANCE); + ModConfig.initOrReload(); } } } diff --git a/src/main/java/de/ellpeck/naturesaura/items/tools/ItemPickaxeNA.java b/src/main/java/de/ellpeck/naturesaura/items/tools/ItemPickaxeNA.java index e9ad0852..3b335e49 100644 --- a/src/main/java/de/ellpeck/naturesaura/items/tools/ItemPickaxeNA.java +++ b/src/main/java/de/ellpeck/naturesaura/items/tools/ItemPickaxeNA.java @@ -1,7 +1,7 @@ package de.ellpeck.naturesaura.items.tools; -import com.google.common.collect.ImmutableMap; import de.ellpeck.naturesaura.Helper; +import de.ellpeck.naturesaura.api.NaturesAuraAPI; import de.ellpeck.naturesaura.items.ModItems; import de.ellpeck.naturesaura.reg.IModItem; import de.ellpeck.naturesaura.reg.IModelProvider; @@ -26,14 +26,17 @@ import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import javax.annotation.Nullable; -import java.util.Map; public class ItemPickaxeNA extends ItemPickaxe implements IModItem, IModelProvider { - private static final Map BOTANIST_CONVERSION = ImmutableMap.builder() - .put(Blocks.COBBLESTONE.getDefaultState(), Blocks.MOSSY_COBBLESTONE.getDefaultState()) - .put(Blocks.STONEBRICK.getDefaultState().withProperty(BlockStoneBrick.VARIANT, BlockStoneBrick.EnumType.DEFAULT), - Blocks.STONEBRICK.getDefaultState().withProperty(BlockStoneBrick.VARIANT, BlockStoneBrick.EnumType.MOSSY)).build(); + static { + NaturesAuraAPI.BOTANIST_PICKAXE_CONVERSIONS.put( + Blocks.COBBLESTONE.getDefaultState(), + Blocks.MOSSY_COBBLESTONE.getDefaultState()); + NaturesAuraAPI.BOTANIST_PICKAXE_CONVERSIONS.put( + Blocks.STONEBRICK.getDefaultState().withProperty(BlockStoneBrick.VARIANT, BlockStoneBrick.EnumType.DEFAULT), + Blocks.STONEBRICK.getDefaultState().withProperty(BlockStoneBrick.VARIANT, BlockStoneBrick.EnumType.MOSSY)); + } private final String baseName; @@ -72,7 +75,7 @@ public class ItemPickaxeNA extends ItemPickaxe implements IModItem, IModelProvid if (this == ModItems.INFUSED_PICKAXE) { ItemStack stack = player.getHeldItem(hand); IBlockState state = worldIn.getBlockState(pos); - IBlockState result = BOTANIST_CONVERSION.get(state); + IBlockState result = NaturesAuraAPI.BOTANIST_PICKAXE_CONVERSIONS.get(state); if (result != null) { if (!worldIn.isRemote) worldIn.setBlockState(pos, result);