Skip to content

Commit

Permalink
remove tons of useless garbage
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed May 24, 2024
1 parent c13409c commit dd63b3c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 111 deletions.
36 changes: 4 additions & 32 deletions src/main/java/io/ix0rai/rainglow/Rainglow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<RainglowColour> COLOURS = new ArrayList<>();

public static final String CUSTOM_NBT_KEY = "Colour";

@Override
Expand All @@ -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<RainglowColour> 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) {
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/io/ix0rai/rainglow/client/RainglowClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
Expand All @@ -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()) {
Expand All @@ -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);
})
);

Expand Down
6 changes: 0 additions & 6 deletions src/main/java/io/ix0rai/rainglow/config/CustomModeScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
40 changes: 10 additions & 30 deletions src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ValueList<String>> 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<RainglowColour> getCustom() {
Expand Down Expand Up @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}

Expand Down
24 changes: 5 additions & 19 deletions src/main/java/io/ix0rai/rainglow/data/RainglowMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +32,7 @@ public RainglowMode(JsonMode mode, boolean existsLocally) {

public RainglowMode(String id, List<String> 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;
Expand All @@ -47,6 +46,10 @@ public RainglowMode(String id, List<String> 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;

Expand All @@ -63,23 +66,6 @@ public List<RainglowColour> 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<RainglowMode> values = MODES.values();
Iterator<RainglowMode> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit dd63b3c

Please sign in to comment.