Skip to content

Commit

Permalink
config screen work
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed May 8, 2024
1 parent 4b0861e commit c9d8e57
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/ix0rai/rainglow/Rainglow.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static boolean colourUnloaded(String colour) {
}

public static String translatableTextKey(String key) {
if (key.split("\\.").length != 2) throw new IllegalArgumentException("key must be in format \"category.key\"");
if (key.split("\\.").length < 2) throw new IllegalArgumentException("key must be in format \"category.key\": " + key);
return MOD_ID + "." + key;
}

Expand Down
43 changes: 37 additions & 6 deletions src/main/java/io/ix0rai/rainglow/config/DeferredSaveOption.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.ix0rai.rainglow.config;

import com.mojang.serialization.Codec;
import io.ix0rai.rainglow.Rainglow;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.GameOptions;
import net.minecraft.client.option.Option;
import net.minecraft.text.CommonTexts;
import net.minecraft.text.Text;

import java.util.Objects;
Expand All @@ -12,7 +15,11 @@ public class DeferredSaveOption<T> extends Option<T> {
private T deferredValue;

public DeferredSaveOption(String key, TooltipSupplier<T> tooltipSupplier, OptionTextGetter<T> textGetter, Option.ValueSet<T> values, T defaultValue, Consumer<T> updateCallback) {
super(key, tooltipSupplier, textGetter, values, defaultValue, updateCallback);
this(key, tooltipSupplier, textGetter, values, values.codec(), defaultValue, updateCallback);
}

public DeferredSaveOption(String key, TooltipSupplier<T> tooltipSupplier, OptionTextGetter<T> textGetter, Option.ValueSet<T> values, Codec<T> codec, T defaultValue, Consumer<T> updateCallback) {
super(key, tooltipSupplier, textGetter, values, codec, defaultValue, updateCallback);
this.deferredValue = this.value;
}

Expand All @@ -27,24 +34,48 @@ public void set(T value) {
} else {
if (!Objects.equals(this.value, object)) {
this.deferredValue = object;
this.updateCallback.accept(this.deferredValue);
// note: callback is called on save
}
}
}

public static DeferredSaveOption<Boolean> createBoolean(boolean defaultValue, String key) {
public static Option<Boolean> createDeferredBoolean(String key, boolean defaultValue, Consumer<Boolean> updateCallback) {
return new DeferredSaveOption<>(
"rainglow.config." + key,
Option.constantTooltip(Text.translatable("rainglow.config.tooltip." + key)),
(text, value) -> GameOptions.getGenericValueText(text, Text.translatable("ramel.config.value." + key, value)),
(text, value) -> value ? CommonTexts.YES : CommonTexts.NO,
Option.BOOLEAN_VALUES,
defaultValue,
value -> {
}
updateCallback
);
}

public static Option<Integer> createDeferredRangedInt(String key, int defaultValue, int min, int max, Consumer<Integer> updateCallback) {
return new DeferredSaveOption<>(
Rainglow.translatableTextKey("config." + key),
Option.constantTooltip(Rainglow.translatableText("tooltip." + key)),
(text, value) -> Rainglow.translatableText("value." + key, value),
new Option.IntRangeValueSet(min, max),
Codec.intRange(min, max),
defaultValue,
updateCallback
);
}

public static Option<Double> createDeferredRangedDouble(String key, double defaultValue, double min, double max, Consumer<Double> updateCallback) {
return new DeferredSaveOption<>(
"rainglow.config." + key,
Option.constantTooltip(Text.translatable("rainglow.tooltip." + key)),
(text, value) -> GameOptions.getGenericValueText(text, Text.translatable("rainglow.value." + key, value)),
new Option.IntRangeValueSet((int) (min * 10), (int) (max * 10)).withModifier(i -> (double) i / 10.0, double_ -> (int) (double_ * 10.0)),
Codec.doubleRange(min, max),
defaultValue,
updateCallback
);
}

public void save() {
this.value = this.deferredValue;
this.updateCallback.accept(this.value);
}
}
33 changes: 21 additions & 12 deletions src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;

import javax.swing.text.html.parser.Entity;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
Expand All @@ -20,16 +19,16 @@ public class RainglowConfig {
public static final String CUSTOM_KEY = "custom";
public static final String SERVER_SYNC_KEY = "enable_server_sync";

public static final String RARITY_KEY = "rarity";
public static final Function<RainglowEntity, String> TO_CONFIG_KEY = entity -> "enable_" + entity.getId();
public static final Function<RainglowEntity, String> RARITY_CONFIG_KEY = entity -> entity.getId() + "_rarity";

private RainglowMode mode;
private List<RainglowColour> custom;
private int rarity;
private boolean enableServerSync;
private boolean editLocked = false;
private boolean isInitialised = false;
private final Map<RainglowEntity, Boolean> entityToggles = new EnumMap<>(RainglowEntity.class);
private final Map<RainglowEntity, Integer> entityRarities = new EnumMap<>(RainglowEntity.class);

public RainglowConfig() {
// we cannot load the config here because it would be loaded before modes, since it's statically initialised
Expand Down Expand Up @@ -80,9 +79,14 @@ public void reloadFromFile() {
}

// parse rarity
int rarity = 100;
if (config.containsKey(RARITY_KEY)) {
rarity = ConfigIo.parseTomlInt(config.get(RARITY_KEY));
for (RainglowEntity entity : RainglowEntity.values()) {
String configKey = RARITY_CONFIG_KEY.apply(entity);

if (config.containsKey(configKey)) {
entityRarities.put(entity, ConfigIo.parseTomlInt(config.get(configKey)));
} else {
entityRarities.put(entity, 100);
}
}

// reset colours if parsing failed
Expand All @@ -94,7 +98,6 @@ public void reloadFromFile() {
this.mode = rainglowMode;
this.custom = customColours;
this.enableServerSync = serverSync;
this.rarity = rarity;
this.save(false);

this.isInitialised = true;
Expand All @@ -108,8 +111,8 @@ public List<RainglowColour> getCustom() {
return this.custom;
}

public int getRarity() {
return this.rarity;
public int getRarity(RainglowEntity entity) {
return this.entityRarities.get(entity);
}

public boolean isServerSyncEnabled() {
Expand All @@ -135,8 +138,8 @@ public void setCustom(List<RainglowColour> custom) {
Rainglow.refreshColours();
}

public void setRarity(int rarity) {
this.rarity = rarity;
public void setRarity(RainglowEntity entity, int rarity) {
this.entityRarities.put(entity, rarity);
}

public void setEditLocked(boolean editLocked) {
Expand All @@ -160,7 +163,7 @@ public void save(boolean log) {
ConfigIo.writeString(MODE_KEY, this.mode.getId());
this.saveCustom();
ConfigIo.writeBoolean(SERVER_SYNC_KEY, this.enableServerSync);
ConfigIo.writeInt(RARITY_KEY, this.rarity);
writeEntityRarities();
}

// entity toggles cannot be locked by the server
Expand All @@ -179,4 +182,10 @@ private void writeEntityToggles() {
ConfigIo.writeBoolean(TO_CONFIG_KEY.apply(entry.getKey()), entry.getValue());
}
}

private void writeEntityRarities() {
for (Map.Entry<RainglowEntity, Integer> entry : entityRarities.entrySet()) {
ConfigIo.writeInt(RARITY_CONFIG_KEY.apply(entry.getKey()), entry.getValue());
}
}
}
68 changes: 62 additions & 6 deletions src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,45 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.option.SimpleOptionsScreen;
import net.minecraft.client.gui.widget.button.ButtonWidget;
import net.minecraft.client.gui.widget.layout.LinearLayoutWidget;
import net.minecraft.client.gui.widget.text.TextWidget;
import net.minecraft.client.option.Option;
import net.minecraft.client.toast.SystemToast;
import net.minecraft.client.toast.Toast;
import net.minecraft.text.CommonTexts;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Language;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class RainglowConfigScreen extends SimpleOptionsScreen {
// private final SpruceOption modeOption;
// private final SpruceOption customOption;
// private final SpruceOption[] entityToggles = new SpruceOption[RainglowEntity.values().length];
// private final SpruceOption resetOption;
// private final SpruceOption saveOption;

//private final SpruceOption colourRarityOption;
private RainglowMode mode;
//private RainglowMode mode;
// colours to apply is saved in a variable so that it can be removed from the screen when cycling modes
// private SpruceLabelWidget coloursToApplyLabel;

public RainglowConfigScreen(@Nullable Screen parent) {
super(parent, MinecraftClient.getInstance().options, Rainglow.translatableText("config.title"),
new Option[]{
DeferredSaveOption.ofBoolean("gaming", false)
createEntityToggles().get(0),
createColourRaritySliders().get(0),
createEntityToggles().get(1),
createColourRaritySliders().get(1),
createEntityToggles().get(2),
createColourRaritySliders().get(2),
}
);

this.mode = Rainglow.CONFIG.getMode();


// // mode option cycles through available modes
// // it also updates the label to show which colours will be applied
Expand Down Expand Up @@ -103,6 +109,56 @@ public RainglowConfigScreen(@Nullable Screen parent) {
// );
}

private static List<Option<Boolean>> createEntityToggles() {
List<Option<Boolean>> toggles = new ArrayList<>();

for (RainglowEntity entity : RainglowEntity.values()) {
toggles.add(DeferredSaveOption.createDeferredBoolean(
"enable_" + entity.getId(),
Rainglow.CONFIG.isEntityEnabled(entity),
enabled -> Rainglow.CONFIG.setEntityEnabled(entity, enabled)
));
}

return toggles;
}

private static List<Option<Integer>> createColourRaritySliders() {
List<Option<Integer>> sliders = new ArrayList<>();

for (RainglowEntity entity : RainglowEntity.values()) {
sliders.add(DeferredSaveOption.createDeferredRangedInt(
entity.getId() + "_rarity",
Rainglow.CONFIG.getRarity(entity),
0,
100,
rarity -> Rainglow.CONFIG.setRarity(entity, rarity)
));
}

return sliders;
}

private void save() {
for (Option<?> option : this.options) {
if (option instanceof DeferredSaveOption) {
((DeferredSaveOption<?>) option).save();
}
}
}

@Override
protected void method_31387() {
LinearLayoutWidget linearLayout = this.field_49503.addToFooter(LinearLayoutWidget.createHorizontal().setSpacing(8));
linearLayout.add(ButtonWidget.builder(CommonTexts.DONE, button -> this.closeScreen()).build());
linearLayout.add(ButtonWidget.builder(CommonTexts.YES, button -> {
this.save();
this.closeScreen();
}).build());
this.field_49503.visitWidgets(this::addDrawableSelectableElement);
this.repositionElements();
}

private TextWidget createColourListLabel(String translationKey, RainglowMode mode, int x, int y) {
// creates a label and appends all the colours that will be applied in the given mode
StringBuilder text = new StringBuilder(Language.getInstance().get(translationKey));
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.ix0rai.rainglow.data.RainglowColour;
import io.ix0rai.rainglow.data.GlowSquidEntityData;
import io.ix0rai.rainglow.data.GlowSquidVariantProvider;
import io.ix0rai.rainglow.data.RainglowEntity;
import io.ix0rai.rainglow.data.SlimeEntityData;
import io.ix0rai.rainglow.data.SlimeVariantProvider;
import net.minecraft.entity.EntityData;
Expand All @@ -16,7 +17,6 @@
import net.minecraft.entity.mob.SlimeEntity;
import net.minecraft.entity.passive.AllayEntity;
import net.minecraft.entity.passive.GlowSquidEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.random.RandomGenerator;
import net.minecraft.world.LocalDifficulty;
import net.minecraft.world.ServerWorldAccess;
Expand Down Expand Up @@ -53,7 +53,21 @@ public void initialize(ServerWorldAccess world, LocalDifficulty difficulty, Spaw
}

@Unique
private static RainglowColour getColourOrDefault(RandomGenerator random, RainglowColour defaultColour, String randomColour) {
return random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? defaultColour : RainglowColour.get(randomColour);
private RainglowColour getColourOrDefault(RandomGenerator random, RainglowColour defaultColour, String randomColour) {
return random.nextInt(100) >= Rainglow.CONFIG.getRarity(this.getCurrentEntity()) ? defaultColour : RainglowColour.get(randomColour);
}

@Unique
@SuppressWarnings("all")
private RainglowEntity getCurrentEntity() {
if ((Object) this instanceof GlowSquidEntity) {
return RainglowEntity.GLOW_SQUID;
} else if ((Object) this instanceof AllayEntity) {
return RainglowEntity.ALLAY;
} else if ((Object) this instanceof SlimeEntity) {
return RainglowEntity.SLIME;
} else {
throw new RuntimeException("unsupported entity");
}
}
}
8 changes: 6 additions & 2 deletions src/main/resources/assets/rainglow/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
"rainglow.config.enable_allay": "Rainbow allays",
"rainglow.config.server_locked_title": "Config is locked by server",
"rainglow.config.server_locked_description": "You can't change the mode while the config is locked!",
"rainglow.config.rarity": "Colour Rarity",
"rainglow.tooltip.rarity": "Rarity determines what percent chance mobs have to spawn with a colour",
"rainglow.value.slime_rarity": "Slime rarity: %s",
"rainglow.tooltip.slime_rarity": "Rarity determines what percent chance mobs have to spawn with a colour.",
"rainglow.value.allay_rarity": "Allay rarity: %s",
"rainglow.tooltip.allay_rarity": "Rarity determines what percent chance mobs have to spawn with a colour.",
"rainglow.value.glow_squid_rarity": "Glow squid rarity: %s",
"rainglow.tooltip.glow_squid_rarity": "Rarity determines what percent chance mobs have to spawn with a colour.",
"rainglow.tooltip.mode": "Rainglow Mode dictates which colours are currently available for squids",
"rainglow.config.cannot_be_loaded_outside_world": "Rainglow config cannot be edited through the GUI before loading a world!",
"rainglow.mode.rainbow": "Rainbow",
Expand Down

0 comments on commit c9d8e57

Please sign in to comment.