-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config module rewrite
- Loading branch information
Showing
30 changed files
with
845 additions
and
608 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
specter-config/src/client/java/dev/spiritstudios/specter/api/ConfigScreenManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package dev.spiritstudios.specter.api; | ||
|
||
import dev.spiritstudios.specter.api.config.Config; | ||
import dev.spiritstudios.specter.impl.config.gui.widget.*; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; | ||
import net.minecraft.client.gui.widget.ClickableWidget; | ||
import net.minecraft.util.Identifier; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import java.util.Map; | ||
import java.util.function.BiFunction; | ||
|
||
@SuppressWarnings("unchecked") | ||
public final class ConfigScreenManager { | ||
private static final Map<Class<?>, BiFunction<Config.Value<?>, Identifier, ? extends ClickableWidget>> widgetFactories = new Object2ObjectOpenHashMap<>(); | ||
|
||
public static void registerWidgetFactory(Class<?> clazz, BiFunction<Config.Value<?>, Identifier, ? extends ClickableWidget> factory) { | ||
widgetFactories.put(clazz, factory); | ||
} | ||
|
||
@ApiStatus.Internal | ||
public static <T> BiFunction<Config.Value<?>, Identifier, ? extends ClickableWidget> getWidgetFactory(Config.Value<T> value) { | ||
// We are using a switch instead of just adding to our map for 2 reasons: | ||
// 1. It's (usually) faster than a map lookup, as most of the time the value will be one of these types | ||
// 2. It lets us handle the lowercased names of primitive types, which are different Class<> instances because reasons | ||
return switch (value.defaultValue()) { | ||
case Boolean ignored -> BOOLEAN_WIDGET_FACTORY; | ||
case Integer ignored -> INTEGER_WIDGET_FACTORY; | ||
case Double ignored -> DOUBLE_WIDGET_FACTORY; | ||
case Float ignored -> FLOAT_WIDGET_FACTORY; | ||
case String ignored -> STRING_WIDGET_FACTORY; | ||
case Enum<?> ignored -> ENUM_WIDGET_FACTORY; | ||
default -> widgetFactories.get(value.defaultValue().getClass()); | ||
}; | ||
} | ||
|
||
private static final BiFunction<Config.Value<?>, Identifier, ? extends ClickableWidget> BOOLEAN_WIDGET_FACTORY = (configValue, id) -> new BooleanButtonWidget((Config.Value<Boolean>) configValue, id); | ||
private static final BiFunction<Config.Value<?>, Identifier, ? extends ClickableWidget> INTEGER_WIDGET_FACTORY = (configValue, id) -> new IntegerSliderWidget((Config.Value<Integer>) configValue, id); | ||
private static final BiFunction<Config.Value<?>, Identifier, ? extends ClickableWidget> DOUBLE_WIDGET_FACTORY = (configValue, id) -> new DoubleSliderWidget((Config.Value<Double>) configValue, id); | ||
private static final BiFunction<Config.Value<?>, Identifier, ? extends ClickableWidget> FLOAT_WIDGET_FACTORY = (configValue, id) -> new FloatSliderWidget((Config.Value<Float>) configValue, id); | ||
private static final BiFunction<Config.Value<?>, Identifier, ? extends ClickableWidget> STRING_WIDGET_FACTORY = (configValue, id) -> new TextBoxWidget((Config.Value<String>) configValue, id); | ||
private static final BiFunction<Config.Value<?>, Identifier, ? extends ClickableWidget> ENUM_WIDGET_FACTORY = (configValue, id) -> new EnumButtonWidget((Config.Value<Enum<?>>) configValue, id); | ||
|
||
private ConfigScreenManager() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 4 additions & 28 deletions
32
...ter-config/src/client/java/dev/spiritstudios/specter/impl/config/SpecterConfigClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,19 @@ | ||
package dev.spiritstudios.specter.impl.config; | ||
|
||
import dev.spiritstudios.specter.api.config.Config; | ||
import dev.spiritstudios.specter.api.config.ConfigManager; | ||
import dev.spiritstudios.specter.api.config.annotations.Sync; | ||
import dev.spiritstudios.specter.api.core.util.ReflectionHelper; | ||
import dev.spiritstudios.specter.api.core.SpecterGlobals; | ||
import dev.spiritstudios.specter.impl.config.network.ConfigSyncS2CPayload; | ||
import net.fabricmc.api.ClientModInitializer; | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; | ||
import net.minecraft.util.Identifier; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
public class SpecterConfigClient implements ClientModInitializer { | ||
@Override | ||
public void onInitializeClient() { | ||
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> ConfigManager.reloadConfigs()); | ||
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> ConfigManager.reload()); | ||
|
||
ClientPlayNetworking.registerGlobalReceiver(ConfigSyncS2CPayload.ID, (payload, context) -> { | ||
Map<Identifier, String> configs = payload.configs(); | ||
|
||
context.client().execute(() -> configs.forEach((id, data) -> { | ||
Config config = ConfigManager.getConfigById(id); | ||
if (config == null) return; | ||
|
||
config.save(); | ||
Config serverConfig = ConfigManager.GSON.fromJson(data, config.getClass()); | ||
|
||
Arrays.stream(serverConfig.getClass().getDeclaredFields()) | ||
.filter(field -> field.isAnnotationPresent(Sync.class)) | ||
.forEach(field -> ReflectionHelper.setFieldValue( | ||
config, | ||
field, | ||
ReflectionHelper.getFieldValue(serverConfig, field) | ||
)); | ||
|
||
ConfigManager.getConfigs().put(id, config); | ||
})); | ||
SpecterGlobals.debug("Received config sync packet"); | ||
SpecterGlobals.debug("Payload: %s".formatted(payload)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 40 additions & 119 deletions
159
specter-config/src/client/java/dev/spiritstudios/specter/impl/config/gui/ConfigScreen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.