Skip to content

Commit

Permalink
force load the config so we can actually access it in registration phase
Browse files Browse the repository at this point in the history
  • Loading branch information
DerToaster98 committed Jul 5, 2022
1 parent 67cc33d commit 321efd0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import de.dertoaster.crossbowverhaul.config.CrossbowverhaulConfigHolder;
import de.dertoaster.crossbowverhaul.config.COConfig;
import de.dertoaster.crossbowverhaul.init.ModEnchantments;
import de.dertoaster.crossbowverhaul.init.ModEntityTypes;
import de.dertoaster.crossbowverhaul.init.ModItemProperties;
Expand All @@ -17,6 +17,7 @@
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.loading.FMLPaths;

// The value here should match an entry in the META-INF/mods.toml file
@Mod(CrossbowverhaulMod.MODID)
Expand All @@ -31,7 +32,9 @@ public CrossbowverhaulMod() {
IEventBus modbus = FMLJavaModLoadingContext.get().getModEventBus();

//Register config
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, CrossbowverhaulConfigHolder.CONFIG_SPEC, "co-config.toml");
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, COConfig.CONFIG_SPEC, "co-config.toml");
COConfig.loadConfig(COConfig.CONFIG_SPEC,
FMLPaths.CONFIGDIR.get().resolve("co-config.toml").toString());

//Register items
ModItems.registerToEventBus(modbus);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package de.dertoaster.crossbowverhaul.config;

import java.io.File;

import org.apache.commons.lang3.tuple.Pair;

import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.electronwill.nightconfig.core.io.WritingMode;

import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.ForgeConfigSpec.ConfigValue;

public class CrossbowverhaulConfigHolder {
public class COConfig {

public static final CrossbowverhaulConfig CONFIG;
public static final ForgeConfigSpec CONFIG_SPEC;
Expand All @@ -14,6 +19,13 @@ public class CrossbowverhaulConfigHolder {
CONFIG = serverSpecPair.getLeft();
CONFIG_SPEC = serverSpecPair.getRight();
}

public static void loadConfig(ForgeConfigSpec config, String path) {
final CommentedFileConfig file = CommentedFileConfig.builder(new File(path)).sync().autosave()
.writingMode(WritingMode.REPLACE).build();
file.load();
config.setConfig(file);
}

public static class CrossbowverhaulConfig {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.dertoaster.crossbowverhaul.enchantment;

import de.dertoaster.crossbowverhaul.config.CrossbowverhaulConfigHolder;
import de.dertoaster.crossbowverhaul.config.COConfig;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.enchantment.MultiShotEnchantment;

Expand All @@ -12,7 +12,7 @@ public MultishotEnchantment(Rarity p_i50017_1_, EquipmentSlot... p_i50017_2_) {

@Override
public int getMaxLevel() {
return CrossbowverhaulConfigHolder.CONFIG.coModMultishot.get().intValue();
return COConfig.CONFIG.coModMultishot.get().intValue();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.dertoaster.crossbowverhaul.init;

import de.dertoaster.crossbowverhaul.CrossbowverhaulMod;
import de.dertoaster.crossbowverhaul.config.CrossbowverhaulConfigHolder;
import de.dertoaster.crossbowverhaul.config.COConfig;
import de.dertoaster.crossbowverhaul.item.ItemBolt;
import de.dertoaster.crossbowverhaul.item.ItemBoltExplosive;
import de.dertoaster.crossbowverhaul.item.ItemCrossbow;
Expand Down Expand Up @@ -35,9 +35,9 @@ public static void registerToEventBus(IEventBus eventbus) {
public static final RegistryObject<Item> ITEM_BOLT_EXPLOSIVE = ITEMS.register("bolt_explosive", () -> new ItemBoltExplosive(new Properties().tab(CreativeModeTab.TAB_COMBAT).stacksTo(64).fireResistant()));

//Netherite crossbow
public static final RegistryObject<Item> ITEM_CROSSBOW_NETHERITE = ITEMS.register("crossbow_netherite", () -> (new ItemCrossbowNetherite(new Item.Properties().stacksTo(1).tab(CreativeModeTab.TAB_COMBAT).durability(/*CrossbowverhaulConfigHolder.CONFIG.coNetheriteCrossbowDurability.get()*/1024).fireResistant())));
public static final RegistryObject<Item> ITEM_CROSSBOW_NETHERITE = ITEMS.register("crossbow_netherite", () -> (new ItemCrossbowNetherite(new Item.Properties().stacksTo(1).tab(CreativeModeTab.TAB_COMBAT).durability(COConfig.CONFIG.coNetheriteCrossbowDurability.get()).fireResistant())));

//now, override the crossbow
public static final RegistryObject<Item> ITEM_CROSSBOW = VANILLA_ITEMS.register("crossbow", () -> (new ItemCrossbow(new Item.Properties().stacksTo(1).tab(CreativeModeTab.TAB_COMBAT).durability(/*CrossbowverhaulConfigHolder.CONFIG.coCrossbowDurability.get()*/512))));
public static final RegistryObject<Item> ITEM_CROSSBOW = VANILLA_ITEMS.register("crossbow", () -> (new ItemCrossbow(new Item.Properties().stacksTo(1).tab(CreativeModeTab.TAB_COMBAT).durability(COConfig.CONFIG.coCrossbowDurability.get()))));

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;
import java.util.function.Predicate;

import de.dertoaster.crossbowverhaul.config.CrossbowverhaulConfigHolder;
import de.dertoaster.crossbowverhaul.config.COConfig;
import de.dertoaster.crossbowverhaul.init.ModItems;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
Expand All @@ -25,7 +25,7 @@ public class ItemCrossbow extends CrossbowItem implements Vanishable, IModifiedC
};

protected static final Predicate<ItemStack> PREDICATE_BOLTS_ONLY_NO_EXPLOSIVE = (itemstack) -> {
return itemstack.getItem() instanceof ItemBolt && (CrossbowverhaulConfigHolder.CONFIG.coAllowExplosiveBoltsOnNormalCrossbow.get().booleanValue() || !(itemstack.getItem() instanceof ItemBoltExplosive));
return itemstack.getItem() instanceof ItemBolt && (COConfig.CONFIG.coAllowExplosiveBoltsOnNormalCrossbow.get().booleanValue() || !(itemstack.getItem() instanceof ItemBoltExplosive));
};

//This only checks the items in off and main hand
Expand Down Expand Up @@ -140,17 +140,17 @@ public static ItemStack getFirstLoadedBolt(ItemStack crossbow) {
//Override charging duration
@Override
public int getMaxChargeTime() {
return CrossbowverhaulConfigHolder.CONFIG.coModCrossbowChargeTime.get().intValue();
return COConfig.CONFIG.coModCrossbowChargeTime.get().intValue();
}

@Override
public int getDefaultProjectileRange() {
return (int)(CrossbowverhaulConfigHolder.CONFIG.coModCrossbowProjectileRange.get().doubleValue() * super.getDefaultProjectileRange());
return (int)(COConfig.CONFIG.coModCrossbowProjectileRange.get().doubleValue() * super.getDefaultProjectileRange());
}

@Override
public float getProjectileSpeedModifier() {
float f = CrossbowverhaulConfigHolder.CONFIG.coModCrossbowProjectileSpeed.get().floatValue();
float f = COConfig.CONFIG.coModCrossbowProjectileSpeed.get().floatValue();
return f * IModifiedCrossbowMethod.super.getProjectileSpeedModifier();
}

Expand All @@ -164,12 +164,12 @@ protected boolean parentClassIsBookEnchantable(ItemStack stack, ItemStack book)

@Override
public boolean isEnchantable(ItemStack p_41456_) {
return this.parentClassIsEnchantable(p_41456_) && CrossbowverhaulConfigHolder.CONFIG.coEnchCrossbow.get().booleanValue();
return this.parentClassIsEnchantable(p_41456_) && COConfig.CONFIG.coEnchCrossbow.get().booleanValue();
}

@Override
public boolean isBookEnchantable(ItemStack stack, ItemStack book) {
return this.parentClassIsBookEnchantable(stack, book) && CrossbowverhaulConfigHolder.CONFIG.coEnchCrossbow.get().booleanValue();
return this.parentClassIsBookEnchantable(stack, book) && COConfig.CONFIG.coEnchCrossbow.get().booleanValue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.function.Predicate;

import de.dertoaster.crossbowverhaul.config.CrossbowverhaulConfigHolder;
import de.dertoaster.crossbowverhaul.config.COConfig;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;

Expand All @@ -24,12 +24,12 @@ public Predicate<ItemStack> getAllSupportedProjectiles() {

@Override
public boolean isEnchantable(ItemStack p_41456_) {
return this.parentClassIsEnchantable(p_41456_) && CrossbowverhaulConfigHolder.CONFIG.coEnchNetheriteCrossbow.get().booleanValue();
return this.parentClassIsEnchantable(p_41456_) && COConfig.CONFIG.coEnchNetheriteCrossbow.get().booleanValue();
}

@Override
public boolean isBookEnchantable(ItemStack stack, ItemStack book) {
return this.parentClassIsBookEnchantable(stack, book) && CrossbowverhaulConfigHolder.CONFIG.coEnchNetheriteCrossbow.get().booleanValue();
return this.parentClassIsBookEnchantable(stack, book) && COConfig.CONFIG.coEnchNetheriteCrossbow.get().booleanValue();
}

public ItemCrossbowNetherite(Properties properties) {
Expand All @@ -38,18 +38,18 @@ public ItemCrossbowNetherite(Properties properties) {

@Override
public int getDefaultProjectileRange() {
return (int)(CrossbowverhaulConfigHolder.CONFIG.coModNetheriteCrossbowProjectileRange.get().intValue() * (super.getDefaultProjectileRange() / CrossbowverhaulConfigHolder.CONFIG.coModCrossbowProjectileRange.get()));
return (int)(COConfig.CONFIG.coModNetheriteCrossbowProjectileRange.get().intValue() * (super.getDefaultProjectileRange() / COConfig.CONFIG.coModCrossbowProjectileRange.get()));
}

@Override
public float getProjectileSpeedModifier() {
return CrossbowverhaulConfigHolder.CONFIG.coModNetheriteCrossbowProjectileSpeed.get().floatValue() * (super.getProjectileSpeedModifier() / CrossbowverhaulConfigHolder.CONFIG.coModCrossbowProjectileSpeed.get().floatValue());
return COConfig.CONFIG.coModNetheriteCrossbowProjectileSpeed.get().floatValue() * (super.getProjectileSpeedModifier() / COConfig.CONFIG.coModCrossbowProjectileSpeed.get().floatValue());
}

// Override charging duration
@Override
public int getMaxChargeTime() {
return CrossbowverhaulConfigHolder.CONFIG.coModNetheriteCrossbowChargeTime.get().intValue();
return COConfig.CONFIG.coModNetheriteCrossbowChargeTime.get().intValue();
}

}

0 comments on commit 321efd0

Please sign in to comment.