Skip to content

Commit

Permalink
Breaking Changes: Big Refactoring
Browse files Browse the repository at this point in the history
Add MModdingPackets
Renaming Config#getQualifier
Add ClientOperations
  • Loading branch information
FirstMegaGame4 committed Jul 31, 2023
1 parent 3f4b4ab commit 3ff9643
Show file tree
Hide file tree
Showing 25 changed files with 133 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.mmodding.mmodding_lib;

import com.mmodding.mmodding_lib.library.config.*;
import com.mmodding.mmodding_lib.library.config.screen.ConfigScreen;
import com.mmodding.mmodding_lib.library.config.screen.ConfigScreenOptions;
import com.mmodding.mmodding_lib.library.config.client.screen.ConfigScreen;
import com.mmodding.mmodding_lib.library.config.client.screen.ConfigScreenOptions;
import net.minecraft.text.Text;

public class MModdingLibConfig implements Config {

@Override
public String getConfigName() {
public String getQualifier() {
return "mmodding_lib";
}

@Override
public String getFileName() {
public String getFilePath() {
return "mmodding/common";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mmodding.mmodding_lib.library.events.client.ClientConfigNetworkingEvents;
import com.mmodding.mmodding_lib.library.config.Config;
import com.mmodding.mmodding_lib.library.config.ConfigObject;
import net.minecraft.util.Identifier;
import com.mmodding.mmodding_lib.networking.MModdingPackets;
import org.jetbrains.annotations.ApiStatus;
import org.quiltmc.loader.api.minecraft.ClientOnly;
import org.quiltmc.qsl.networking.api.client.ClientPlayNetworking;
Expand All @@ -15,7 +15,7 @@ public class ClientPacketReceivers {

public static void register() {

ClientPlayNetworking.registerGlobalReceiver(new Identifier("configs-channel"), ((client, handler, buf, responseSender) -> {
ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.CONFIGS, ((client, handler, buf, responseSender) -> {
String configName = buf.readString();
String configContent = buf.readString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.google.common.collect.ImmutableList;
import com.mmodding.mmodding_lib.MModdingLib;
import com.mmodding.mmodding_lib.library.client.TemporaryConfig;
import com.mmodding.mmodding_lib.library.config.client.TemporaryConfig;
import com.mmodding.mmodding_lib.library.config.Config;
import com.mmodding.mmodding_lib.library.config.screen.ConfigScreen;
import com.mmodding.mmodding_lib.library.config.client.screen.ConfigScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.Selectable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mmodding.mmodding_lib.client;

import com.mmodding.mmodding_lib.MModdingLib;
import com.mmodding.mmodding_lib.library.config.screen.ConfigScreen;
import com.mmodding.mmodding_lib.library.config.client.screen.ConfigScreen;
import com.mmodding.mmodding_lib.library.utils.Colors;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.screen.Screen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mmodding.mmodding_lib.MModdingLib;
import com.mmodding.mmodding_lib.client.MModdingScreen;
import com.mmodding.mmodding_lib.library.config.screen.ConfigScreen;
import com.mmodding.mmodding_lib.library.config.client.screen.ConfigScreen;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;

Expand All @@ -21,7 +21,7 @@ public Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {
Map<String, ConfigScreenFactory<?>> configScreens = new HashMap<>();
MModdingLib.CONFIGS.forEach(((modId, config) -> {
if (config.getConfigOptions() != null) {
configScreens.put(modId, screen -> new ConfigScreen(config.getConfigName(), config, screen));
configScreens.put(modId, screen -> new ConfigScreen(config.getQualifier(), config, screen));
}
}));
return configScreens;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ default void onInitializeClient(ModContainer mod) {
this.getClientElementsInitializers().forEach(ClientElementsInitializer::registerClient);
if (this.getClientConfig() != null) {
this.getClientConfig().initializeConfig();
MModdingLibClient.CLIENT_CONFIGS.put(this.getClientConfig().getConfigName(), this.getClientConfig());
MModdingLibClient.CLIENT_CONFIGS.put(this.getClientConfig().getQualifier(), this.getClientConfig());
}
}

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

import com.mmodding.mmodding_lib.MModdingLib;
import com.mmodding.mmodding_lib.client.MModdingLibClient;
import com.mmodding.mmodding_lib.library.client.TemporaryConfig;
import com.mmodding.mmodding_lib.library.config.client.TemporaryConfig;
import com.mmodding.mmodding_lib.library.config.Config;
import com.mmodding.mmodding_lib.library.initializers.ElementsInitializer;
import net.fabricmc.api.EnvType;
Expand All @@ -27,9 +27,9 @@ default void onInitialize(ModContainer mod) {
this.getElementsInitializers().forEach(ElementsInitializer::register);
if (this.getConfig() != null) {
this.getConfig().initializeConfig();
MModdingLib.CONFIGS.put(this.getConfig().getConfigName(), this.getConfig());
MModdingLib.CONFIGS.put(this.getConfig().getQualifier(), this.getConfig());
if (MinecraftQuiltLoader.getEnvironmentType() == EnvType.CLIENT) {
MModdingLibClient.CLIENT_CONFIGS.put(this.getConfig().getConfigName(), TemporaryConfig.fromConfig(this.getConfig()));
MModdingLibClient.CLIENT_CONFIGS.put(this.getConfig().getQualifier(), TemporaryConfig.fromConfig(this.getConfig()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mmodding.mmodding_lib.library.screens.widgets;
package com.mmodding.mmodding_lib.library.client.screen.widgets;

import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.widget.TextFieldWidget;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mmodding.mmodding_lib.library.screens.widgets;
package com.mmodding.mmodding_lib.library.client.screen.widgets;

import net.minecraft.client.font.TextRenderer;
import net.minecraft.text.Text;
Expand Down
55 changes: 44 additions & 11 deletions src/main/java/com/mmodding/mmodding_lib/library/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.mmodding.mmodding_lib.library.config.screen.ConfigScreenOptions;
import com.mmodding.mmodding_lib.library.server.ServerOperations;
import com.mmodding.mmodding_lib.MModdingLibConfig;
import com.mmodding.mmodding_lib.library.config.client.screen.ConfigScreenOptions;
import com.mmodding.mmodding_lib.networking.server.ServerOperations;
import com.mmodding.mmodding_lib.library.utils.ConfigUtils;
import net.minecraft.server.network.ServerPlayerEntity;
import org.quiltmc.loader.api.QuiltLoader;
Expand All @@ -14,18 +15,50 @@

public interface Config {

String getConfigName();
/**
* The "identifier" of the config. Used to define the translations keys of the config.
* You can take example on {@link MModdingLibConfig#getQualifier()}.
* @return the qualifier of the config
*/
String getQualifier();

String getFileName();
/**
* The file path.
* You can take example on {@link MModdingLibConfig#getPath()}.
* @return the path of the file (from the config directory and without file extension)
*/
String getFilePath();

/**
* The default config object.
* You can take example on {@link MModdingLibConfig#defaultConfig()}.
* @return the config object in its default state
* @see ConfigObject.Builder
*/
ConfigObject defaultConfig();

/**
* Some options used in the config screen.
* You can take example on {@link MModdingLibConfig#getConfigOptions()}
* @return the config screen options
* @see ConfigScreenOptions
*/
ConfigScreenOptions getConfigOptions();

/**
* The config content directly read from the config json file
* @return the config content
* @see ConfigObject
*/
default ConfigObject getContent() {
return new ConfigObject(this.getReader().getAsJsonObject());
}

/**
* This method allows to send a config from a server to a client
* @param player the targeted player
* @see ServerOperations
*/
@DedicatedServerOnly
default void sendServerConfigToClient(ServerPlayerEntity player) {
ServerOperations.sendConfigToClient(this, player);
Expand All @@ -41,17 +74,17 @@ private JsonElement getReader() {

private String getPath() {
return QuiltLoader.getConfigDir().toString() + ConfigUtils.getSeparator() +
this.getFileName()
.replace("\\", ConfigUtils.getSeparator())
.replace("/", ConfigUtils.getSeparator())
+ ".json";
this.getFilePath()
.replace("\\", ConfigUtils.getSeparator())
.replace("/", ConfigUtils.getSeparator())
+ ".json";
}

default void saveConfig(ConfigObject configObject) {
try {
FileWriter configWriter = new FileWriter(this.getPath());
String json = new GsonBuilder().setPrettyPrinting().create()
.toJson(ConfigObject.Builder.fromConfigObject(configObject).getJsonObject());
.toJson(ConfigObject.Builder.fromConfigObject(configObject).getJsonObject());
configWriter.write(json);
configWriter.close();
} catch (IOException e) {
Expand All @@ -60,8 +93,8 @@ default void saveConfig(ConfigObject configObject) {
}

default void initializeConfig() {
if (this.getFileName().contains("\\") || this.getFileName().contains("/")) {
String path = this.getFileName().replace("\\", "/");
if (this.getFilePath().contains("\\") || this.getFilePath().contains("/")) {
String path = this.getFilePath().replace("\\", "/");
String[] strings = path.split("/");
int counter = 0;
StringBuilder temp = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ public static Builder fromConfigObject(ConfigObject configObject) {

public Builder addParameter(String parameter, Value<?> value) {
switch (value.getType()) {
case "string" -> this.jsonObject.addProperty(parameter, value.getValue());
case "number" -> this.jsonObject.addProperty(parameter, Integer.valueOf(value.getValue()));
case "boolean" -> {
this.jsonObject.addProperty(parameter, Boolean.valueOf(value.getValue()));
}
case STRING -> this.jsonObject.addProperty(parameter, value.getValue());
case NUMBER -> this.jsonObject.addProperty(parameter, Integer.valueOf(value.getValue()));
case BOOLEAN -> this.jsonObject.addProperty(parameter, Boolean.valueOf(value.getValue()));
}
return this;
}
Expand Down Expand Up @@ -132,7 +130,7 @@ public ConfigObject build() {
public static class Value <T> {

private final T value;
private final String type;
private final Type type;

public static Value<?> fromJsonElement(JsonElement element) {
if (element instanceof JsonPrimitive primitive) {
Expand All @@ -147,11 +145,11 @@ public static Value<?> fromJsonElement(JsonElement element) {
public Value(T value) {
this.value = value;
if (value instanceof String) {
this.type = "string";
this.type = Type.STRING;
} else if (value instanceof Number) {
this.type = "number";
this.type = Type.NUMBER;
} else if (value instanceof Boolean) {
this.type = "boolean";
this.type = Type.BOOLEAN;
} else {
throw new IllegalArgumentException("Invalid Parameter Type");
}
Expand All @@ -161,8 +159,14 @@ public String getValue() {
return String.valueOf(value);
}

public String getType() {
public Type getType() {
return this.type;
}

public enum Type {
STRING,
NUMBER,
BOOLEAN
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.mmodding.mmodding_lib.library.client;
package com.mmodding.mmodding_lib.library.config.client;

import com.mmodding.mmodding_lib.library.config.Config;
import com.mmodding.mmodding_lib.library.config.ConfigObject;
import com.mmodding.mmodding_lib.library.config.screen.ConfigScreenOptions;
import com.mmodding.mmodding_lib.library.config.client.screen.ConfigScreenOptions;

public abstract class TemporaryConfig implements Config {

Expand All @@ -22,13 +22,13 @@ public static TemporaryConfig fromConfig(Config config) {
return new TemporaryConfig() {

@Override
public String getConfigName() {
return config.getConfigName();
public String getQualifier() {
return config.getQualifier();
}

@Override
public String getFileName() {
return config.getFileName();
public String getFilePath() {
return config.getFilePath();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mmodding.mmodding_lib.library.config.screen;
package com.mmodding.mmodding_lib.library.config.client.screen;

import com.mmodding.mmodding_lib.library.config.ConfigObject;
import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -35,17 +35,17 @@ public void render(MatrixStack matrices, int index, int y, int x, int entryWidth
int color;
String stringValue;
switch (this.fieldValue.getType()) {
case "string" -> {
case STRING -> {
fieldType = Text.translatable("mmodding_lib.configs.string");
color = 4781378;
stringValue = this.fieldValue.getValue();
}
case "number" -> {
case NUMBER -> {
fieldType = Text.translatable("mmodding_lib.configs.integer");
color = 1641430;
stringValue = this.fieldValue.getValue();
}
case "boolean" -> {
case BOOLEAN -> {
fieldType = Text.translatable("mmodding_lib.configs.boolean");
color = 14027531;
stringValue = this.fieldValue.getValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.mmodding.mmodding_lib.library.config.screen;
package com.mmodding.mmodding_lib.library.config.client.screen;

import com.mmodding.mmodding_lib.library.config.Config;
import com.mmodding.mmodding_lib.library.config.ConfigObject;
import com.mmodding.mmodding_lib.library.config.screen.editing.BooleanEditScreen;
import com.mmodding.mmodding_lib.library.config.screen.editing.NumberEditScreen;
import com.mmodding.mmodding_lib.library.config.screen.editing.StringEditScreen;
import com.mmodding.mmodding_lib.library.config.client.screen.editing.BooleanEditScreen;
import com.mmodding.mmodding_lib.library.config.client.screen.editing.NumberEditScreen;
import com.mmodding.mmodding_lib.library.config.client.screen.editing.StringEditScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget;

Expand Down Expand Up @@ -60,9 +60,9 @@ public void removeParameter(ConfigElementsListEntry entry) {
public void modifyParameter(ConfigElementsListEntry entry) {
String stringValue = entry.getFieldValue().getValue();
switch (entry.getFieldValue().getType()) {
case "string" -> this.client.setScreen(new StringEditScreen(this.screen, this, entry, this.mutableConfig, entry.getFieldName(), new ConfigObject.Value<>(stringValue)));
case "number" -> this.client.setScreen(new NumberEditScreen(this.screen, this, entry, this.mutableConfig, entry.getFieldName(), new ConfigObject.Value<>(Integer.valueOf(stringValue))));
case "boolean" -> this.client.setScreen(new BooleanEditScreen(this.screen, this, entry, this.mutableConfig, entry.getFieldName(), new ConfigObject.Value<>(Boolean.valueOf(stringValue))));
case STRING -> this.client.setScreen(new StringEditScreen(this.screen, this, entry, this.mutableConfig, entry.getFieldName(), new ConfigObject.Value<>(stringValue)));
case NUMBER -> this.client.setScreen(new NumberEditScreen(this.screen, this, entry, this.mutableConfig, entry.getFieldName(), new ConfigObject.Value<>(Integer.valueOf(stringValue))));
case BOOLEAN -> this.client.setScreen(new BooleanEditScreen(this.screen, this, entry, this.mutableConfig, entry.getFieldName(), new ConfigObject.Value<>(Boolean.valueOf(stringValue))));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mmodding.mmodding_lib.library.config.screen;
package com.mmodding.mmodding_lib.library.config.client.screen;

import com.mmodding.mmodding_lib.library.config.Config;
import com.mojang.blaze3d.systems.RenderSystem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mmodding.mmodding_lib.library.config.screen;
package com.mmodding.mmodding_lib.library.config.client.screen;

import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.mmodding.mmodding_lib.library.config.screen.editing;
package com.mmodding.mmodding_lib.library.config.client.screen.editing;

import com.mmodding.mmodding_lib.library.config.ConfigObject;
import com.mmodding.mmodding_lib.library.config.screen.ConfigElementsListEntry;
import com.mmodding.mmodding_lib.library.config.screen.ConfigElementsListWidget;
import com.mmodding.mmodding_lib.library.config.screen.ConfigScreen;
import com.mmodding.mmodding_lib.library.config.client.screen.ConfigElementsListEntry;
import com.mmodding.mmodding_lib.library.config.client.screen.ConfigElementsListWidget;
import com.mmodding.mmodding_lib.library.config.client.screen.ConfigScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
Expand Down
Loading

0 comments on commit 3ff9643

Please sign in to comment.