Jump to content

[1.14.4] get config in IConditionSerializer


F43nd1r

Recommended Posts

I need to access my configuration inside my IConditionSerializer. My current solution is very ugly, as it relies on reflection. How would I do this correctly?

Current code:

@Mod.EventBusSubscriber(
        modid = "hadron",
        bus = Mod.EventBusSubscriber.Bus.MOD
)
public class HadronConfigConditionFactory implements IConditionSerializer {
    @Override
    public BooleanSupplier parse(JsonObject json) {
        String setting = JSONUtils.getString(json, "config", null);
        if (setting != null) {
            return new BooleanSupplier() {
                @Override
                public boolean getAsBoolean() {
                    try {
                        Field f = ForgeConfigSpec.class.getDeclaredField("childConfig");
                        f.setAccessible(true);
                        Config config = (Config) f.get(HadronConfigHolder.config);
                        return config.getOrElse(setting, true);
                    } catch(Exception e) {
                        return true;
                    }
                }
            };
        } else {
            return () -> true;
        }
    }

    @SubscribeEvent
    public static void setup(FMLCommonSetupEvent e) {
        CraftingHelper.register(new ResourceLocation("hadron", "config"), new HadronConfigConditionFactory());
    }
}
Link to comment
Share on other sites

1 hour ago, F43nd1r said:

How would I do this correctly?

Store your configuration when you create it in your setup method in a static field.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

ForgeConfigSpec implements UnmodifiableConfig, have you tried calling UnmodifiableConfig#getOrElse on your ForgeConfigSpec instance?

 

Note that if you've followed Forge's example and made your ForgeConfigSpec fields private, you'll need to create a public method in your config class that calls UnmodifiableConfig#get(String) on the appropriate ForgeConfigSpec instance.

Edited by Choonster

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

16 minutes ago, Choonster said:

ForgeConfigSpec implements UnmodifiableConfig, have you tried calling UnmodifiableConfig#getOrElse on your ForgeConfigSpec instance?

I've tried that. Problem is, that calls to

ForgeConfigSpec.config

, while the real values are in

ForgeConficSpec.childConfig

. Hence the dirty workaround to access childConfig.

Edited by F43nd1r
Link to comment
Share on other sites

3 minutes ago, LexManos said:

why have this a config condition? If they are changing the config then they can change the loot table

Here is an example using this condition: magma_bricks.json

{
    "result":{"item":"hadron:magma_bricks","count":4},
    "conditions":[{"config":"Building.MagmaBricks","type":"hadron:config"}],
    "ingredients":[
        {"item":"minecraft:stone_bricks"},
        {"item":"minecraft:stone_bricks"},
        {"item":"minecraft:magma_block"},
        {"item":"minecraft:magma_block"}],
    "type":"minecraft:crafting_shapeless"
}

I'm not aware of any solutions outside of conditions to disable multiple recipes based on settings. Aren't loot tables for item drops?

Link to comment
Share on other sites

Then they can change the recipes.

The issue is dependant recipes is advancements and other things.

You're just moving the config from one file to another.

I can see your usecasse, but it has downsides of just missing recipes.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Link to comment
Share on other sites

6 hours ago, LexManos said:

Then they can change the recipes.

The issue is dependant recipes is advancements and other things.

You're just moving the config from one file to another.

I can see your usecasse, but it has downsides of just missing recipes.

Lets assume a mod like quark, which is registering lots of blocks. IMO such a mod should provide a userfriendly way to enable/disable groups of these blocks. Overwriting all recipes of blocks in this group is not user friendly, because a group can have like 50 recipes (Do you disagree?). This also doesn't consider creative tabs.

My current solution registers all blocks (according to you "always register everything"), and then disables recipes of disabled blocks and removes them from creative tabs based on a configuration file. Would you say this approach is valid? Is there a better approach?

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I want to download Forge 1.20.6, but when I press download, it redirects me to Adfocus and does not provide any download. I have never encountered such a problem when I downloaded Forge 1.18.2 at least 12 months ago.
    • did you find any solutions? i have the same problem and also that mahito doesnt transform
    • Here's a simple example to solve this particular issue: public class ModArmorMaterials { public static final DeferredRegister<ArmorMaterial> ARMORS = DeferredRegister.create(Registries.ARMOR_MATERIAL, MyModName.MOD_ID); public static final RegistryObject<ArmorMaterial> COPPER = registerArmor("copper", Util.make(new EnumMap<>(ArmorItem.Type.class), enumMap -> { enumMap.put(ArmorItem.Type.BOOTS, 1); enumMap.put(ArmorItem.Type.LEGGINGS, 4); enumMap.put(ArmorItem.Type.CHESTPLATE, 5); enumMap.put(ArmorItem.Type.HELMET, 2); enumMap.put(ArmorItem.Type.BODY, 4); }), 12, SoundEvents.ARMOR_EQUIP_IRON, 0.0F, 0.0F, () -> Ingredient.of(Items.COPPER_INGOT)); private static RegistryObject<ArmorMaterial> registerArmor(String pGroup, EnumMap<ArmorItem.Type, Integer> pEnumMap, int pEnchantmentValue, Holder<SoundEvent> pEquipSound, float pToughness, float pKnockbackResistance, Supplier<Ingredient> pRepairIngredient) { List<ArmorMaterial.Layer> pLayerList = List.of(new ArmorMaterial.Layer(new ResourceLocation(pGroup))); return registerArmor(pGroup, pEnumMap, pEnchantmentValue, pEquipSound, pToughness, pKnockbackResistance, pRepairIngredient, pLayerList); } private static RegistryObject<ArmorMaterial> registerArmor( String pGroup, EnumMap<ArmorItem.Type, Integer> pEnumMap, int pEnchantmentValue, Holder<SoundEvent> pEquipSound, float pToughness, float pKnockbackResistance, Supplier<Ingredient> pRepairIngredient, List<ArmorMaterial.Layer> pLayerList) { return ARMORS.register(pGroup, () -> new ArmorMaterial(pEnumMap, pEnchantmentValue, pEquipSound, pRepairIngredient, pLayerList, pToughness, pKnockbackResistance)); } public static void register(IEventBus eventBus) { ARMORS.register(eventBus); } } Essentially, the TierSortingRegistry has been removed and so you now need to set up your own DeferredRegister. Please see the minecraftforge GitHub for more information; I found this solution through their issues (https://github.com/MinecraftForge/MinecraftForge/issues/9961)
    • How did I even leave that in there? I’ll try it in a bit, that very well could be it.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.