Skip to content

Commit

Permalink
fix some config stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed May 21, 2024
1 parent 3422473 commit e6e3513
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
39 changes: 24 additions & 15 deletions src/main/java/io/ix0rai/rainglow/client/RainglowClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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.RainglowMode;
Expand All @@ -26,25 +27,33 @@ public class RainglowClient implements ClientModInitializer {
public void onInitializeClient() {
ClientPlayNetworking.registerGlobalReceiver(RainglowNetworking.ConfigSyncPayload.PACKET_ID, (payload, context) -> {
MinecraftClient client = context.client();
client.execute(() -> {
// custom must be set before mode so that if the server sends a custom mode it is set correctly
// otherwise the client's custom would be used
ValueList<String> list = ValueList.create("", payload.customMode().stream().map(RainglowColour::getId).toArray(String[]::new));
Rainglow.CONFIG.customColours.setOverride(list);
Rainglow.CONFIG.mode.setOverride(payload.currentMode());
if (!client.isIntegratedServerRunning()) {
client.execute(() -> {
// custom must be set before mode so that if the server sends a custom mode it is set correctly
// otherwise the client's custom would be used
ValueList<String> customColours = ValueList.create("", payload.customMode().stream().map(RainglowColour::getId).toArray(String[]::new));
Rainglow.CONFIG.customColours.setOverride(customColours);
Rainglow.CONFIG.mode.setOverride(payload.currentMode());

// todo override toggles
var rarities = ValueMap.builder(0);
for (var entry : payload.rarities().entrySet()) {
rarities.put(entry.getKey().getId(), entry.getValue());
}
Rainglow.CONFIG.rarities.setOverride(rarities.build());

for (var entry : payload.enabledMobs().entrySet()) {
Rainglow.CONFIG.setEntityEnabled(entry.getKey(), entry.getValue());
}
var toggles = ValueMap.builder(true);
for (var entry : payload.enabledMobs().entrySet()) {
toggles.put(entry.getKey().getId(), entry.getValue());
}
Rainglow.CONFIG.toggles.setOverride(toggles.build());

// lock the config from reloading on resource reload
Rainglow.CONFIG.setEditLocked(true);
// 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());
});
// log
Rainglow.LOGGER.info("received config from server: set mode to " + payload.currentMode() + " and custom colours to " + payload.customMode());
});
}
});

ClientPlayNetworking.registerGlobalReceiver(RainglowNetworking.ModeSyncPayload.PACKET_ID, (payload, context) -> {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public Map<RainglowEntity, Boolean> getToggles() {
return map;
}

public Map<RainglowEntity, Integer> getRarities() {
Map<RainglowEntity, Integer> map = new HashMap<>();
for (RainglowEntity entity : RainglowEntity.values()) {
map.put(entity, this.getRarity(entity));
}

return map;
}

public boolean isEntityEnabled(RainglowEntity entity) {
return this.toggles.value().get(entity.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private DeferredSaveOption<Boolean> createEntityToggle(RainglowEntity entity) {
"config.enable_" + e.getId(),
"tooltip.entity_toggle",
Rainglow.CONFIG.toggles.getRealValue().get(e.getId()),
enabled -> Rainglow.CONFIG.setEntityEnabled(e, enabled),
enabled -> Rainglow.CONFIG.toggles.getRealValue().put(e.getId(), enabled),
enabled -> this.saveButton.active = true
));
}
Expand All @@ -122,7 +122,7 @@ private DeferredSaveOption<Integer> createColourRaritySlider(RainglowEntity enti
Rainglow.CONFIG.rarities.getRealValue().get(e.getId()),
0,
100,
rarity -> Rainglow.CONFIG.setRarity(e, rarity),
rarity -> Rainglow.CONFIG.rarities.getRealValue().put(e.getId(), rarity),
rarity -> this.saveButton.active = true
));
}
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ public class RainglowNetworking {
public static void syncConfig(ServerPlayerEntity player) {
// note: client does not need to know if server sync is enabled or not
// they already know that it is enabled because they are receiving this packet
ServerPlayNetworking.send(player, new ConfigSyncPayload(Rainglow.CONFIG.mode.value(), Rainglow.CONFIG.getCustom(), Rainglow.CONFIG.getToggles()));
ServerPlayNetworking.send(player, new ConfigSyncPayload(Rainglow.CONFIG.mode.value(), Rainglow.CONFIG.getCustom(), Rainglow.CONFIG.getToggles(), Rainglow.CONFIG.getRarities()));
}

public record ConfigSyncPayload(String currentMode, List<RainglowColour> customMode, Map<RainglowEntity, Boolean> enabledMobs) implements CustomPayload {
public record ConfigSyncPayload(String currentMode, List<RainglowColour> customMode, Map<RainglowEntity, Boolean> enabledMobs, Map<RainglowEntity, Integer> rarities) implements CustomPayload {
public static final CustomPayload.Id<ConfigSyncPayload> PACKET_ID = new CustomPayload.Id<>(Rainglow.id("config_sync"));
public static final PacketCodec<RegistryByteBuf, ConfigSyncPayload> PACKET_CODEC = PacketCodec.create(ConfigSyncPayload::write, ConfigSyncPayload::read);

public void write(RegistryByteBuf buf) {
buf.writeString(this.currentMode);
buf.writeCollection(this.customMode, RainglowColour::write);
buf.writeMap(this.enabledMobs, RainglowEntity::write, PacketByteBuf::writeBoolean);
buf.writeMap(this.rarities, RainglowEntity::write, PacketByteBuf::writeVarInt);
}

public static ConfigSyncPayload read(RegistryByteBuf buf) {
return new ConfigSyncPayload(
buf.readString(),
buf.readList(RainglowColour::read),
buf.readMap(RainglowEntity::read, PacketByteBuf::readBoolean)
buf.readMap(RainglowEntity::read, PacketByteBuf::readBoolean),
buf.readMap(RainglowEntity::read, PacketByteBuf::readVarInt)
);
}

Expand Down

0 comments on commit e6e3513

Please sign in to comment.