Skip to content

Commit

Permalink
simplify config
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed Feb 10, 2024
1 parent 3d146d3 commit 78bbac0
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 65 deletions.
4 changes: 2 additions & 2 deletions common/src/main/java/dev/rdh/createunlimited/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ public static String remapMethod(Class<?> clazz, String name, Class<?>... parame

public static Supplier<Multimap<Attribute, AttributeModifier>> singleRange() {
AttributeModifier am = new AttributeModifier(UUID.fromString("7f7dbdb2-0d0d-458a-aa40-ac7633691f66"), "Range modifier",
CUConfigs.server().singleExtendoGripRange.get(), AttributeModifier.Operation.ADDITION);
CUConfigs.server.singleExtendoGripRange.get(), AttributeModifier.Operation.ADDITION);
return Suppliers.memoize(() -> ImmutableMultimap.of(getReachAttribute(), am));
}

public static Supplier<Multimap<Attribute, AttributeModifier>> doubleRange() {
AttributeModifier am = new AttributeModifier(UUID.fromString("8f7dbdb2-0d0d-458a-aa40-ac7633691f66"), "Range modifier",
CUConfigs.server().doubleExtendoGripRange.get(), AttributeModifier.Operation.ADDITION);
CUConfigs.server.doubleExtendoGripRange.get(), AttributeModifier.Operation.ADDITION);
return Suppliers.memoize(() -> ImmutableMultimap.of(getReachAttribute(), am));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class CUConfigCommand {
// get config as CValue
CValue<?, ?> cValue;
try {
cValue = (CValue<?, ?>) field.get(CUConfigs.server());
cValue = (CValue<?, ?>) field.get(CUConfigs.server);
} catch (IllegalAccessException | ClassCastException e) {
CreateUnlimited.LOGGER.error("Failed to get config value for " + field.getName(), e);
continue;
Expand All @@ -106,15 +106,15 @@ public class CUConfigCommand {
if (value instanceof BooleanValue bValue)
setBoolean(category, name, bValue);

// set for enums
// set for enums
else if (value instanceof EnumValue<? extends Enum<?>> eValue)
setEnum(category, name, eValue);

// set for int
// set for int
else if (value instanceof IntValue iValue)
setInt(category, name, iValue);

// set for double
// set for double
else if (value instanceof DoubleValue dValue)
setDouble(category, name, dValue);

Expand Down
56 changes: 9 additions & 47 deletions common/src/main/java/dev/rdh/createunlimited/config/CUConfigs.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.rdh.createunlimited.config;

import com.simibubi.create.foundation.config.ConfigBase;
import com.simibubi.create.foundation.config.ui.BaseConfigScreen;

import dev.rdh.createunlimited.Util;
Expand All @@ -12,66 +11,29 @@

import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.config.ModConfig.Type;

import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.EnumMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Supplier;

import static net.minecraftforge.fml.config.ModConfig.Type.*;

public class CUConfigs {

@ApiStatus.Internal
public static final Map<Type, ConfigBase> CONFIGS = new EnumMap<>(Type.class);

public static CUServer server() {
return (CUServer) byType(SERVER);
}

public static @Nullable ConfigBase byType(Type type) {
return CONFIGS.get(type);
}

public static @Nullable ForgeConfigSpec getSpecByType(Type type) {
ConfigBase config = CONFIGS.get(type);
return config != null ? config.specification : null;
}

private static <T extends ConfigBase> void register(Supplier<T> factory, Type side) {
Pair<T, ForgeConfigSpec> specPair = new ForgeConfigSpec.Builder().configure(builder -> {
T config = factory.get();
config.registerAll(builder);
return config;
});

T config = specPair.getLeft();
config.specification = specPair.getRight();
CONFIGS.put(side, config);
}
public static final CUServer server = new CUServer();

public static void register() {
register(CUServer::new, SERVER);

for(Entry<Type, ConfigBase> pair : CONFIGS.entrySet())
Util.registerConfig(pair.getKey(), pair.getValue().specification);
ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
server.registerAll(builder);
Util.registerConfig(SERVER, server.specification = builder.build());
}

public static void onLoad(ModConfig modConfig) {
for (ConfigBase config : CONFIGS.values())
if (config.specification == modConfig.getSpec())
config.onLoad();
if (server.specification == modConfig.getSpec())
server.onLoad();
}

public static void onReload(ModConfig modConfig) {
for (ConfigBase config : CONFIGS.values())
if (config.specification == modConfig.getSpec())
config.onReload();
if(server.specification == modConfig.getSpec())
server.onReload();
}

public static BaseConfigScreen createConfigScreen(Screen parent) {
Expand All @@ -84,7 +46,7 @@ public static BaseConfigScreen createConfigScreen(Screen parent) {
private static void initBCS() {
if(done) return;
BaseConfigScreen.setDefaultActionFor(CreateUnlimited.ID, (base) ->
base.withSpecs(getSpecByType(CLIENT), getSpecByType(COMMON), getSpecByType(SERVER))
base.withSpecs(null, null, server.specification)
.withTitles("", "", "Settings")
);
done = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
public abstract class CarriageContraptionMixin {
@ModifyExpressionValue(method = "assemble", at = @At(value = "INVOKE", target = "Ljava/util/Map;size()I", ordinal = 0))
private int modifyMinBlocksOnTrain(int original) {
return !CUConfigs.server().trainAssemblyChecks.get() ? 2 : original;
return !CUConfigs.server.trainAssemblyChecks.get() ? 2 : original;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private void onAuthenticateServer(String serverHash, CallbackInfoReturnable<Comp
CreateUnlimited.LOGGER.info("Checking if Create Unlimited is installed on the server...");
try {
// this will fail if unlimited is not installed on the server
CUConfigs.server().allowAllCopycatBlocks.get();
CUConfigs.server.allowAllCopycatBlocks.get();
} catch (IllegalStateException e) {
Component c = Component.literal("Create Unlimited is not installed on the server!" +
"\nPlease either install it or remove it from your client.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public abstract class CopycatBlockMixin {
@ModifyExpressionValue(method = "getAcceptedBlockState", at = @At(value = "INVOKE", target = "Lcom/simibubi/create/content/decoration/copycat/CopycatBlock;isAcceptedRegardless(Lnet/minecraft/world/level/block/state/BlockState;)Z"))
private boolean isAcceptedRegardless(boolean original) {
return CUConfigs.server().allowAllCopycatBlocks.get() || original;
return CUConfigs.server.allowAllCopycatBlocks.get() || original;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
public abstract class StationBlockEntityMixin {
@ModifyExpressionValue(method = "assemble", at = @At(value = "INVOKE", target = "Lcom/simibubi/create/content/trains/bogey/AbstractBogeyBlock;allowsSingleBogeyCarriage()Z", ordinal = 0))
private boolean forceAllowSingleBogeyCarriage(boolean original) {
return !CUConfigs.server().trainAssemblyChecks.get() || original;
return !CUConfigs.server.trainAssemblyChecks.get() || original;
}

@ModifyExpressionValue(method = "assemble", at = @At(value = "CONSTANT", args = "intValue=3", ordinal = 0))
private int setMinBogeySpacing(int original) {
return CUConfigs.server().trainAssemblyChecks.get() ? original : 0;
return CUConfigs.server.trainAssemblyChecks.get() ? original : 0;
}

@Inject(method = "isValidBogeyOffset", at = @At("HEAD"), cancellable = true)
private void disableBogeyOffsetCheck(CallbackInfoReturnable<Boolean> cir) {
if(!CUConfigs.server().trainAssemblyChecks.get())
if(!CUConfigs.server.trainAssemblyChecks.get())
cir.setReturnValue(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public abstract class SuperGlueSectionHandlerMixin {
// client-side modification
@ModifyConstant(method = "tick", constant = @Constant(doubleValue = 24))
private double modifyMaxSuperGlueDistance(double original) {
return CUConfigs.server().maxGlueConnectionRange.get();
return CUConfigs.server.maxGlueConnectionRange.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public abstract class SuperGlueSelectionPacketMixin {
// server-side modification
@ModifyConstant(method = "lambda$handle$0", constant = @Constant(doubleValue = 25))
private double modifyMaxSuperGlueDistance(double original) {
return CUConfigs.server().maxGlueConnectionRange.get();
return CUConfigs.server.maxGlueConnectionRange.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
public abstract class TrackEdgeMixin {
@ModifyConstant(method = "canTravelTo", constant = @Constant(doubleValue = 0.875))
private double canTravelTo(double original) {
return CUConfigs.server().extendedDriving.get();
return CUConfigs.server.extendedDriving.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract class TrackPlacementMixin {
@Overwrite
public static PlacementInfo tryConnect(Level level, Player player, BlockPos pos2, BlockState state2,
ItemStack stack, boolean girder, boolean maximiseTurn) {
boolean enabled = CUConfigs.server().placementChecks.get().isEnabledFor(player);
boolean enabled = CUConfigs.server.placementChecks.get().isEnabledFor(player);

Vec3 lookVec = player.getLookAngle();
int lookAngle = (int) (22.5 + AngleHelper.deg(Mth.atan2(lookVec.z, lookVec.x)) % 360) / 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public abstract class TrainMixin {
@ModifyConstant(method = "tick", constant = @Constant(doubleValue = 4))
private double modifyMaxStress(double original) {
double a = CUConfigs.server().maxAllowedStress.get();
double a = CUConfigs.server.maxAllowedStress.get();
return a < 0 ? Double.MAX_VALUE : a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public abstract class TrainRelocatorMixin {
@ModifyExpressionValue(method = {"onClicked", "clientTick"}, at = @At(value = "CONSTANT", args = "doubleValue=24.0"))
private static double modifyMaxTrainRelocatingDistance(double original) {
return CUConfigs.server().maxTrainRelocationDistance.get();
return CUConfigs.server.maxTrainRelocationDistance.get();
}
}

0 comments on commit 78bbac0

Please sign in to comment.