Skip to content

Commit

Permalink
remove dumb existsLocally logic in server sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed May 28, 2024
1 parent 26fd63f commit 9190947
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 26 deletions.
8 changes: 3 additions & 5 deletions src/main/java/io/ix0rai/rainglow/client/RainglowClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ public void onInitializeClient() {
client.execute(() -> {
List<String> newModeIds = new ArrayList<>();

// add modes that do not exist on the client to the map
// add modes on the client
for (RainglowMode mode : payload.modes()) {
if (!mode.existsLocally()) {
newModeIds.add(mode.getId());
RainglowMode.addMode(mode);
}
newModeIds.add(mode.getId());
RainglowMode.addMode(mode);
}


Expand Down
20 changes: 4 additions & 16 deletions src/main/java/io/ix0rai/rainglow/data/RainglowMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ public class RainglowMode {
private final String id;
private final List<RainglowColour> colours = new ArrayList<>();
private final Text text;
private final boolean existsLocally;

public RainglowMode(JsonMode mode, boolean existsLocally) {
public RainglowMode(JsonMode mode) {
this(
mode.id,
mode.colourIds,
Rainglow.translatableText("mode." + mode.id).copy().setStyle(Style.EMPTY.withColor(TextColor.fromRgb(Integer.parseInt(mode.textColour, 16)))),
existsLocally
Rainglow.translatableText("mode." + mode.id).copy().setStyle(Style.EMPTY.withColor(TextColor.fromRgb(Integer.parseInt(mode.textColour, 16))))
);
}

public RainglowMode(String id, List<String> colourIds, Text text, boolean existsLocally) {
public RainglowMode(String id, List<String> colourIds, Text text) {
if (!id.matches("^[a-z0-9_]+$")) {
Rainglow.LOGGER.error("loaded rainglow mode with id {} which contains invalid characters! (only lowercase letters, numbers, and underscores are allowed)", id);
}
Expand All @@ -51,7 +49,6 @@ public RainglowMode(String id, List<String> colourIds, Text text, boolean exists
}

this.text = text;
this.existsLocally = existsLocally;

MODES.put(this.id, this);
}
Expand Down Expand Up @@ -79,10 +76,6 @@ public String getId() {
return this.id;
}

public boolean existsLocally() {
return this.existsLocally;
}

public static RainglowMode get(String id) {
return MODES.get(id);
}
Expand All @@ -99,11 +92,6 @@ public static List<RainglowColour> getDefaultCustom() {
return List.of(RainglowColour.BLUE, RainglowColour.WHITE, RainglowColour.PINK);
}

public static void clearUniversalModes() {
Collection<RainglowMode> modes = List.copyOf(MODES.values());
for (RainglowMode mode : modes) if (mode.existsLocally()) MODES.remove(mode.id);
}

public static void printLoadedModes() {
StringBuilder formatted = new StringBuilder();
for (RainglowMode mode : MODES.values())
Expand All @@ -130,7 +118,7 @@ public static RainglowMode read(PacketByteBuf buf) {
Text text = TextCodecs.UNLIMITED_TEXT_PACKET_CODEC.decode(buf);
List<String> colourIds = buf.readList(PacketByteBuf::readString);

return new RainglowMode(id, colourIds, text, RainglowMode.get(id) != null);
return new RainglowMode(id, colourIds, text);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ default void log() {

@Override
default void reload(ResourceManager manager) {
// remove existing modes to avoid adding duplicates
// this only clears modes that exist on both the server and the client
// otherwise we would have to re-request the mode data packet on every reload
RainglowMode.clearUniversalModes();
Rainglow.RAINGLOW_DATAPACKS.clear();

// load custom modes from rainglow/custom_modes in the datapack
Expand All @@ -36,9 +32,11 @@ default void reload(ResourceManager manager) {
try (InputStream stream = entry.getValue().open()) {
Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
RainglowMode.JsonMode result = Rainglow.GSON.fromJson(reader, RainglowMode.JsonMode.class);
RainglowMode.addMode(new RainglowMode(result, true));

// todo logic for handling duplicates
String name = entry.getValue().getSourceName();
RainglowMode.addMode(new RainglowMode(result));

if (this.getFabricId().equals(Rainglow.SERVER_MODE_DATA_ID) && !Rainglow.RAINGLOW_DATAPACKS.contains(name)) {
Rainglow.RAINGLOW_DATAPACKS.add(name);
}
Expand Down

0 comments on commit 9190947

Please sign in to comment.