the grand config refactor

This commit is contained in:
FalsePattern committed 2022-07-31 20:28:37 +02:00
1 parent c5fbf5dee5
commit eaec528f5d
32 files changed
+3038 -190

No files matched your search

@@ -0,0 +1,68 @@
/**
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
* The above copyright notice, this permission notice and the word "SNEED"
* shall be included in all copies or substantial portions of the Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.config.event;
import com.falsepattern.lib.StableAPI;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.val;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/**
* This event is pushed on the FMLCommonHandler event bus once all the config synchronizations have finished.
*
* This is a client-only event, and is never triggered serverside!
*/
@SideOnly(Side.CLIENT)
@StableAPI(since = "0.10.0")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class AllConfigSyncEvent extends Event {
public static boolean postStart() {
val event = new Start();
FMLCommonHandler.instance().bus().post(event);
return !event.isCanceled();
}
public static void postEnd() {
FMLCommonHandler.instance().bus().post(new End());
}
@SideOnly(Side.CLIENT)
@StableAPI(since = "0.10.0")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class Start extends AllConfigSyncEvent {
@Override
public boolean isCancelable() {
return true;
}
}
@SideOnly(Side.CLIENT)
@StableAPI(since = "0.10.0")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class End extends AllConfigSyncEvent {
}
}
@@ -0,0 +1,89 @@
/**
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
* The above copyright notice, this permission notice and the word "SNEED"
* shall be included in all copies or substantial portions of the Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.config.event;
import com.falsepattern.lib.StableAPI;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.val;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/**
* This event is pushed on the FMLCommonHandler event bus once each time a single config synchronization has finished.
*
* This is a client-only event, and is never triggered serverside!
*/
@SideOnly(Side.CLIENT)
@StableAPI(since = "0.10.0")
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class ConfigSyncEvent extends Event {
@StableAPI(since = "0.10.0")
public final Class<?> configClass;
public static boolean postStart(Class<?> configClass) {
val event = new Start(configClass);
FMLCommonHandler.instance().bus().post(event);
return !event.isCanceled();
}
public static void postEndSuccess(Class<?> configClass) {
FMLCommonHandler.instance().bus().post(new End(configClass, true, null));
}
public static void postEndFailure(Class<?> configClass, Throwable error) {
FMLCommonHandler.instance().bus().post(new End(configClass, false, error));
}
@SideOnly(Side.CLIENT)
@StableAPI(since = "0.10.0")
public static class Start extends ConfigSyncEvent {
private Start(Class<?> configClass) {
super(configClass);
}
@Override
public boolean isCancelable() {
return true;
}
}
@SideOnly(Side.CLIENT)
@StableAPI(since = "0.10.0")
public static class End extends ConfigSyncEvent {
@StableAPI(since = "0.10.0")
public final boolean successful;
/**
* null if successful == true.
*/
@StableAPI(since = "0.10.0")
public final Throwable error;
private End(Class<?> configClass, boolean successful, Throwable error) {
super(configClass);
this.successful = successful;
this.error = error;
}
}
}
@@ -0,0 +1,68 @@
/**
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
* The above copyright notice, this permission notice and the word "SNEED"
* shall be included in all copies or substantial portions of the Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.config.event;
import com.falsepattern.lib.StableAPI;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import net.minecraft.entity.player.EntityPlayerMP;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.Event;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* You can use this event to manually trigger a synchronization request. The two different flavors are used on the two
* different game sides. If you want to trigger this on the server, use {@link #postServer}, and if on the client, use
* {@link #postClient}
*
*/
@StableAPI(since = "0.10.0")
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class ConfigSyncRequestEvent extends Event {
@StableAPI(since = "0.10.0")
public static class Client extends ConfigSyncRequestEvent {
}
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@StableAPI(since = "0.10.0")
public static class Server extends ConfigSyncRequestEvent {
private final List<EntityPlayerMP> players;
public List<EntityPlayerMP> getPlayers() {
return Collections.unmodifiableList(players);
}
}
@StableAPI(since = "0.10.0")
public static void postClient() {
FMLCommonHandler.instance().bus().post(new Client());
}
@StableAPI(since = "0.10.0")
public static void postServer(List<EntityPlayerMP> players) {
FMLCommonHandler.instance().bus().post(new Server(new ArrayList<>(players)));
}
}
@@ -0,0 +1,204 @@
/**
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
* <p>
* The above copyright notice, this permission notice and the word "SNEED"
* shall be included in all copies or substantial portions of the Software.
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.config.event;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.config.Config;
import com.falsepattern.lib.internal.FalsePatternLib;
import com.falsepattern.lib.text.FormattedText;
import com.falsepattern.lib.toasts.GuiToast;
import com.falsepattern.lib.toasts.SimpleToast;
import com.falsepattern.lib.toasts.icon.ToastBG;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.val;
import net.minecraft.util.EnumChatFormatting;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.lang.reflect.Field;
@StableAPI(since = "0.10.0")
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public abstract class ConfigValidationFailureEvent extends Event {
@StableAPI(since = "0.10.0")
public final String reason;
@StableAPI(since = "0.10.0")
public final Class<?> configClass;
@StableAPI(since = "0.10.0")
public final String fieldName;
@StableAPI(since = "0.10.0")
public final boolean listElement;
@StableAPI(since = "0.10.0")
public final int listIndex;
public static void postNumericRangeOutOfBounds(Field field, int listIndex, String value, String min, String max) {
FMLCommonHandler.instance().bus().post(new NumericRangeOutOfBounds(field.getDeclaringClass(), field.getName(), listIndex, value, min, max));
}
public static void postSize(Field field, int requestedSize, boolean fixedSize, int maxSize, int defaultSize) {
FMLCommonHandler.instance().bus().post(new ListSizeOutOfBounds(field.getDeclaringClass(), field.getName(), requestedSize, fixedSize, maxSize, defaultSize));
}
public static void postStringSizeOutOfBounds(Field field, int listIndex, String text, int maxSize) {
FMLCommonHandler.instance().bus().post(new StringSizeOutOfBounds(field.getDeclaringClass(), field.getName(), listIndex, text, maxSize));
}
public static void fieldIsNull(Field field, int listIndex) {
FMLCommonHandler.instance().bus().post(new FieldIsNull(field.getDeclaringClass(), field.getName(), listIndex));
}
public static void postStringPatternMismatch(Field field, int listIndex, String text, String pattern) {
FMLCommonHandler.instance().bus().post(new StringPatternMismatch(field.getDeclaringClass(), field.getName(), listIndex, text, pattern));
}
@StableAPI(since = "0.10.0")
public static class NumericRangeOutOfBounds extends ConfigValidationFailureEvent {
@StableAPI(since = "0.10.0")
public final String value;
@StableAPI(since = "0.10.0")
public final String min;
@StableAPI(since = "0.10.0")
public final String max;
public NumericRangeOutOfBounds(Class<?> configClass, String fieldName, int listIndex, String value, String min, String max) {
super("Number range out of bounds", configClass, fieldName, listIndex >= 0, listIndex);
this.value = value;
this.min = min;
this.max = max;
}
@Override
protected void customText(StringBuilder b) {
b.append("\nValue: ").append(value)
.append("\nMin: ").append(min)
.append("\nMax: ").append(max);
}
}
@StableAPI(since = "0.10.0")
public static class ListSizeOutOfBounds extends ConfigValidationFailureEvent {
@StableAPI(since = "0.10.0")
public final int size;
@StableAPI(since = "0.10.0")
public final boolean fixedSize;
@StableAPI(since = "0.10.0")
public final int maxSize;
@StableAPI(since = "0.10.0")
public final int defaultSize;
private ListSizeOutOfBounds(Class<?> configClass, String fieldName, int size, boolean fixedSize, int maxSize, int defaultSize) {
super("Array size out of bounds", configClass, fieldName, false, -1);
this.size = size;
this.fixedSize = fixedSize;
this.maxSize = maxSize;
this.defaultSize = defaultSize;
}
@Override
protected void customText(StringBuilder b) {
b.append("\nSize: ").append(size);
if (fixedSize) {
b.append("\nRequired size: ").append(defaultSize);
}
b.append("\nMaximum size: ").append(maxSize);
}
}
@StableAPI(since = "0.10.0")
public static class StringSizeOutOfBounds extends ConfigValidationFailureEvent {
@StableAPI(since = "0.10.0")
public final String text;
@StableAPI(since = "0.10.0")
public final int maxSize;
private StringSizeOutOfBounds(Class<?> configClass, String fieldName, int listIndex, String text, int maxSize) {
super("String size out of bounds", configClass, fieldName, listIndex >= 0, listIndex);
this.text = text;
this.maxSize = maxSize;
}
@Override
protected void customText(StringBuilder b) {
b.append("\nText: ").append(text)
.append("\nSize: ").append(text.length())
.append("\nMax size:").append(maxSize);
}
}
@StableAPI(since = "0.10.0")
public static class FieldIsNull extends ConfigValidationFailureEvent {
private FieldIsNull(Class<?> configClass, String fieldName, int listIndex) {
super("Unexpected null", configClass, fieldName, listIndex >= 0, listIndex);
}
@Override
protected void customText(StringBuilder b) {
}
}
@StableAPI(since = "0.10.0")
public static class StringPatternMismatch extends ConfigValidationFailureEvent {
@StableAPI(since = "0.10.0")
public final String text;
@StableAPI(since = "0.10.0")
public final String pattern;
private StringPatternMismatch(Class<?> configClass, String fieldName, int listIndex, String text, String pattern) {
super("String pattern mismatch", configClass, fieldName, listIndex >= 0, listIndex);
this.text = text;
this.pattern = pattern;
}
@Override
protected void customText(StringBuilder b) {
b.append("\nText: ").append(text)
.append("\nPattern: ").append(pattern);
}
}
@SideOnly(Side.CLIENT)
public void toast() {
val ann = configClass.getAnnotation(Config.class);
val toast = new SimpleToast(ToastBG.TOAST_DARK, null,
FormattedText.parse(EnumChatFormatting.RED + "Config validation failed").toChatText().get(0),
FormattedText.parse(ann.modid() + ":" + ann.category()).toChatText().get(0),
false, 2000);
GuiToast.add(toast);
}
protected abstract void customText(StringBuilder b);
public void logWarn() {
val errorString = new StringBuilder("Error validating configuration field!");
errorString.append("\nReason: ").append(reason);
errorString.append("\nClass: ").append(configClass.getName())
.append("\nField: ").append(fieldName);
if (listElement) {
errorString.append("\nArray index: ").append(listIndex);
}
customText(errorString);
FalsePatternLib.getLog().error(errorString.toString());
}
}