diff --git a/src/main/java/io/ix0rai/rainglow/Rainglow.java b/src/main/java/io/ix0rai/rainglow/Rainglow.java index 98094ce..e5b5e53 100644 --- a/src/main/java/io/ix0rai/rainglow/Rainglow.java +++ b/src/main/java/io/ix0rai/rainglow/Rainglow.java @@ -18,8 +18,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; - public class Rainglow implements ModInitializer { public static final String MOD_ID = "rainglow"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); @@ -28,8 +26,6 @@ public class Rainglow implements ModInitializer { public static final RainglowConfig CONFIG = RainglowConfig.create(ENVIRONMENT, "", MOD_ID, RainglowConfig.class); public static final Gson GSON = new Gson(); - private static final List COLOURS = new ArrayList<>(); - public static final String CUSTOM_NBT_KEY = "Colour"; @Override @@ -52,38 +48,14 @@ public static Identifier id(String id) { return new Identifier(MOD_ID, id); } - public static void setMode(RainglowMode mode) { - if (mode == null) { - mode = RainglowMode.get("rainbow"); - LOGGER.warn("attempted to load missing mode, resetting to rainbow"); - } - - COLOURS.clear(); - - List colours = mode.getColours(); - if (colours.isEmpty()) { - LOGGER.info("No colours were present in the internal collection, adding blue so that the game doesn't crash"); - colours.add(RainglowColour.BLUE); - } - - colours.forEach(Rainglow::addColour); - CONFIG.setInitialized(); - } - - private static void addColour(RainglowColour colour) { - COLOURS.add(colour); - - if (COLOURS.size() >= 100) { - throw new RuntimeException("Too many colours registered! Only up to 99 are allowed"); - } - } - public static String generateRandomColourId(RandomGenerator random) { - return COLOURS.get(random.nextInt(COLOURS.size())).getId(); + var colours = CONFIG.getMode().getColours(); + return colours.get(random.nextInt(colours.size())).getId(); } public static boolean colourUnloaded(RainglowEntity entityType, String colour) { - return !COLOURS.contains(RainglowColour.get(colour)) && !colour.equals(entityType.getDefaultColour().getId()); + var colours = CONFIG.getMode().getColours(); + return !colours.contains(RainglowColour.get(colour)) && !colour.equals(entityType.getDefaultColour().getId()); } public static String translatableTextKey(String key) { diff --git a/src/main/java/io/ix0rai/rainglow/client/RainglowClient.java b/src/main/java/io/ix0rai/rainglow/client/RainglowClient.java index 1907cd2..c84c628 100644 --- a/src/main/java/io/ix0rai/rainglow/client/RainglowClient.java +++ b/src/main/java/io/ix0rai/rainglow/client/RainglowClient.java @@ -47,9 +47,6 @@ public void onInitializeClient() { } Rainglow.CONFIG.toggles.setOverride(toggles.build()); - // lock the config from reloading on resource reload - Rainglow.CONFIG.setEditLocked(true); - // log Rainglow.LOGGER.info("received config from server: set mode to " + payload.currentMode() + " and custom colours to " + payload.customMode()); }); @@ -69,10 +66,6 @@ public void onInitializeClient() { } } - // now that we have modes, we can load the config - if (!Rainglow.CONFIG.isInitialized()) { - Rainglow.setMode(Rainglow.CONFIG.getMode()); - } // log if (!newModeIds.isEmpty()) { @@ -83,13 +76,8 @@ public void onInitializeClient() { ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> client.execute(() -> { - if (Rainglow.CONFIG.isEditLocked(client)) { - // unlock config - Rainglow.CONFIG.setEditLocked(false); - - // reset values to those configured in file - Rainglow.CONFIG.values().forEach(TrackedValue::removeOverride); - } + // reset values to those configured in file + Rainglow.CONFIG.values().forEach(TrackedValue::removeOverride); }) ); diff --git a/src/main/java/io/ix0rai/rainglow/config/CustomModeScreen.java b/src/main/java/io/ix0rai/rainglow/config/CustomModeScreen.java index ca8600d..aa74a05 100644 --- a/src/main/java/io/ix0rai/rainglow/config/CustomModeScreen.java +++ b/src/main/java/io/ix0rai/rainglow/config/CustomModeScreen.java @@ -2,7 +2,6 @@ import io.ix0rai.rainglow.Rainglow; import io.ix0rai.rainglow.data.RainglowColour; -import io.ix0rai.rainglow.data.RainglowMode; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.option.GameOptionsScreen; @@ -73,11 +72,6 @@ private void save() { } Rainglow.CONFIG.save(); - - // refresh colours of custom mode - if (Rainglow.CONFIG.getMode().getId().equals("custom")) { - Rainglow.setMode(RainglowMode.get("custom")); - } } @Override diff --git a/src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java b/src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java index 08948eb..e5c45ae 100644 --- a/src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java +++ b/src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java @@ -7,10 +7,10 @@ import folk.sisby.kaleido.lib.quiltconfig.api.values.TrackedValue; import folk.sisby.kaleido.lib.quiltconfig.api.values.ValueList; import folk.sisby.kaleido.lib.quiltconfig.api.values.ValueMap; +import io.ix0rai.rainglow.Rainglow; import io.ix0rai.rainglow.data.RainglowColour; import io.ix0rai.rainglow.data.RainglowEntity; import io.ix0rai.rainglow.data.RainglowMode; -import net.minecraft.client.MinecraftClient; import java.util.HashMap; import java.util.List; @@ -29,11 +29,16 @@ public class RainglowConfig extends ReflectiveConfig { @Comment("The custom colours to use when the mode is set to custom.") public final TrackedValue> customColours = this.list("", RainglowMode.getDefaultCustom().stream().map(RainglowColour::getId).toArray(String[]::new)); - private transient boolean editLocked = false; - private transient boolean initialized = false; - public RainglowMode getMode() { - return RainglowMode.get(this.mode.value()); + var mode = RainglowMode.get(this.mode.value()); + + if (mode == null) { + Rainglow.LOGGER.warn("unknown mode {}, defaulting to rainbow", this.mode.value()); + this.mode.setValue("rainbow"); + return getMode(); + } + + return mode; } public List getCustom() { @@ -62,35 +67,10 @@ public boolean isEntityEnabled(RainglowEntity entity) { return this.toggles.value().get(entity.getId()); } - public void setEntityEnabled(RainglowEntity entity, boolean enabled) { - this.toggles.value().put(entity.getId(), enabled); - } - public int getRarity(RainglowEntity entity) { return this.rarities.value().get(entity.getId()); } - public void setRarity(RainglowEntity entity, int rarity) { - this.rarities.value().put(entity.getId(), rarity); - } - - public boolean isEditLocked(MinecraftClient client) { - // client can only be locked inside a multiplayer server - return !client.isInSingleplayer() && (client.getCurrentServerEntry() != null && this.editLocked); - } - - public void setEditLocked(boolean editLocked) { - this.editLocked = editLocked; - } - - public boolean isInitialized() { - return this.initialized; - } - - public void setInitialized() { - this.initialized = true; - } - /** * creates a map of default values for each {@link RainglowEntity} */ diff --git a/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java b/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java index 5c01b5b..7991b9b 100644 --- a/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java +++ b/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java @@ -156,7 +156,6 @@ private void save() { } Rainglow.CONFIG.mode.setValue(this.mode.getId()); - Rainglow.setMode(RainglowMode.get(this.mode.getId())); } private Tooltip createColourListLabel(RainglowMode mode) { @@ -199,7 +198,8 @@ public void closeScreen(boolean saved) { this.isConfirming = true; this.clearAndInit(); } else { - if (Rainglow.CONFIG.isEditLocked(MinecraftClient.getInstance())) { + // overrides will exist when connected to a server syncing its values + if (Rainglow.CONFIG.mode.isBeingOverridden()) { sendConfigLockedToast(); } diff --git a/src/main/java/io/ix0rai/rainglow/data/RainglowMode.java b/src/main/java/io/ix0rai/rainglow/data/RainglowMode.java index 97a23e2..4c94a53 100644 --- a/src/main/java/io/ix0rai/rainglow/data/RainglowMode.java +++ b/src/main/java/io/ix0rai/rainglow/data/RainglowMode.java @@ -9,7 +9,6 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.Iterator; import java.util.List; import java.util.SortedMap; import java.util.TreeMap; @@ -33,7 +32,7 @@ public RainglowMode(JsonMode mode, boolean existsLocally) { public RainglowMode(String id, List colourIds, Text text, boolean existsLocally) { if (!id.matches("^[a-z0-9_]+$")) { - throw new IllegalArgumentException("loaded rainglow mode with id " + id + " which contains invalid characters"); + Rainglow.LOGGER.error("loaded rainglow mode with id {} which contains invalid characters! (only lowercase letters, numbers, and underscores are allowed)", id); } this.id = id; @@ -47,6 +46,10 @@ public RainglowMode(String id, List colourIds, Text text, boolean exists this.colours.add(RainglowColour.get(colour)); } + if (this.colours.isEmpty() && !id.equals("all_colours") && !id.equals("custom")) { + Rainglow.LOGGER.error("cannot load mode with id {}: no colours found!", id); + } + this.text = text; this.existsLocally = existsLocally; @@ -63,23 +66,6 @@ public List getColours() { }; } - public RainglowMode cycle() { - // cycle to next in list, wrapping around to 0 if the next ordinal is larger than the map's size - Collection values = MODES.values(); - Iterator iterator = values.iterator(); - - // look for matching key and return next mode - while (iterator.hasNext()) { - RainglowMode mode = iterator.next(); - if (mode.id.equals(this.id) && iterator.hasNext()) { - return iterator.next(); - } - } - - // otherwise return first mode - return values.iterator().next(); - } - @Override public String toString() { return this.getId(); diff --git a/src/main/java/io/ix0rai/rainglow/data/RainglowResourceReloader.java b/src/main/java/io/ix0rai/rainglow/data/RainglowResourceReloader.java index 5c91c4c..fd4ee44 100644 --- a/src/main/java/io/ix0rai/rainglow/data/RainglowResourceReloader.java +++ b/src/main/java/io/ix0rai/rainglow/data/RainglowResourceReloader.java @@ -1,13 +1,10 @@ package io.ix0rai.rainglow.data; import io.ix0rai.rainglow.Rainglow; -import net.fabricmc.api.EnvType; import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; -import net.fabricmc.loader.api.FabricLoader; import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; import net.minecraft.util.Identifier; -import net.minecraft.client.MinecraftClient; import java.io.IOException; import java.io.InputStream; @@ -45,10 +42,5 @@ default void reload(ResourceManager manager) { } this.log(); - - // load config - if (!Rainglow.CONFIG.isInitialized() || (FabricLoader.getInstance().getEnvironmentType().equals(EnvType.CLIENT) && !Rainglow.CONFIG.isEditLocked(MinecraftClient.getInstance()))) { - Rainglow.setMode(Rainglow.CONFIG.getMode()); - } } }