From 0c2fb66ca0616e4f4519227d4a21adba6808bb25 Mon Sep 17 00:00:00 2001 From: HellionGames Date: Sat, 7 Oct 2023 18:44:29 -0400 Subject: [PATCH 1/2] Implemented rarity config Solves #17 In doing this, I also discovered bugs with the Slime coloring. Slimes seem to ignore rarity, but I don't believe it's a fault of the rarity code. They also sometimes change colour upon touching the ground for the first time after being spawned. I think there's something else happening that's overriding their colours. --- .../io/ix0rai/rainglow/config/ConfigIo.java | 8 ++++++++ .../rainglow/config/RainglowConfig.java | 19 +++++++++++++++++++ .../rainglow/config/RainglowConfigScreen.java | 10 ++++++++++ .../ix0rai/rainglow/mixin/MobEntityMixin.java | 12 ++++++------ .../resources/assets/rainglow/lang/en_us.json | 2 ++ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/ix0rai/rainglow/config/ConfigIo.java b/src/main/java/io/ix0rai/rainglow/config/ConfigIo.java index 5472fce..e3f33f2 100644 --- a/src/main/java/io/ix0rai/rainglow/config/ConfigIo.java +++ b/src/main/java/io/ix0rai/rainglow/config/ConfigIo.java @@ -23,6 +23,10 @@ public static boolean parseTomlBoolean(String value) { return value.equals("true"); } + public static int parseTomlInt(String value) { + return Integer.parseInt(value); + } + public static String parseTomlString(String string) { try { return string.split("\"")[1].split("\"")[0]; @@ -109,6 +113,10 @@ public static void writeBoolean(String key, boolean bool) { write(key, bool ? "true" : "false", "boolean"); } + public static void writeInt(String key, int value) { + write(key, Integer.toString(value), "int"); + } + public static void writeStringList(String key, List list) { // convert to toml-friendly format StringBuilder tomlCompatibleList = new StringBuilder("["); diff --git a/src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java b/src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java index ec4b404..3e379bb 100644 --- a/src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java +++ b/src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java @@ -18,10 +18,13 @@ public class RainglowConfig { public static final String MODE_KEY = "mode"; 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 TO_CONFIG_KEY = entity -> "enable_" + entity.getId(); private RainglowMode mode; private List custom; + private int rarity; private boolean enableServerSync; private boolean editLocked = false; private boolean isInitialised = false; @@ -75,6 +78,12 @@ public void reloadFromFile() { } } + // parse rarity + int rarity = 100; + if (config.containsKey(RARITY_KEY)) { + rarity = ConfigIo.parseTomlInt(config.get(RARITY_KEY)); + } + // reset colours if parsing failed if (customColours.isEmpty()) { customColours = RainglowMode.getDefaultCustom(); @@ -84,6 +93,7 @@ public void reloadFromFile() { this.mode = rainglowMode; this.custom = customColours; this.enableServerSync = serverSync; + this.rarity = rarity; this.save(false); this.isInitialised = true; @@ -97,6 +107,10 @@ public List getCustom() { return this.custom; } + public int getRarity() { + return this.rarity; + } + public boolean isServerSyncEnabled() { return this.enableServerSync; } @@ -120,6 +134,10 @@ public void setCustom(List custom) { Rainglow.refreshColours(); } + public void setRarity(int rarity) { + this.rarity = rarity; + } + public void setEditLocked(boolean editLocked) { this.editLocked = editLocked; } @@ -137,6 +155,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); } // entity toggles cannot be locked by the server diff --git a/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java b/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java index 3269471..4e21c23 100644 --- a/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java +++ b/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java @@ -3,6 +3,7 @@ import dev.lambdaurora.spruceui.Position; import dev.lambdaurora.spruceui.option.SpruceBooleanOption; import dev.lambdaurora.spruceui.option.SpruceCyclingOption; +import dev.lambdaurora.spruceui.option.SpruceIntegerInputOption; import dev.lambdaurora.spruceui.option.SpruceOption; import dev.lambdaurora.spruceui.option.SpruceSimpleActionOption; import dev.lambdaurora.spruceui.widget.SpruceLabelWidget; @@ -30,6 +31,8 @@ public class RainglowConfigScreen extends RainglowScreen { private final SpruceOption[] entityToggles = new SpruceOption[RainglowEntity.values().length]; private final SpruceOption resetOption; private final SpruceOption saveOption; + + private final SpruceOption colourRarityOption; 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; @@ -73,6 +76,12 @@ public RainglowConfigScreen(@Nullable Screen parent) { ); } + this.colourRarityOption = new SpruceIntegerInputOption(Rainglow.translatableTextKey("config.rarity"), + Rainglow.CONFIG::getRarity, + Rainglow.CONFIG::setRarity, + Rainglow.translatableText("tooltip.rarity") + ); + // resets the config to default values this.resetOption = SpruceSimpleActionOption.reset(btn -> { MinecraftClient client = MinecraftClient.getInstance(); @@ -117,6 +126,7 @@ protected void init() { } optionList.addOptionEntry(this.modeOption, this.customOption); + optionList.addSingleOptionEntry(this.colourRarityOption); this.addDrawableChild(optionList); // current colours label and colours to apply label diff --git a/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java index 7a1fad9..5698821 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java @@ -36,16 +36,16 @@ protected MobEntityMixin(EntityType entityType, World world public void initialize(ServerWorldAccess world, LocalDifficulty difficulty, SpawnReason spawnReason, EntityData entityData, NbtCompound entityNbt, CallbackInfoReturnable cir) { if ((Object) this instanceof GlowSquidEntity glowSquid) { String colour = Rainglow.generateRandomColourId(this.getRandom()); - ((GlowSquidVariantProvider) glowSquid).setVariant(RainglowColour.get(colour)); - cir.setReturnValue(new GlowSquidEntityData(RainglowColour.get(colour))); + ((GlowSquidVariantProvider) glowSquid).setVariant(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour)); + cir.setReturnValue(new GlowSquidEntityData(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour))); } else if ((Object) this instanceof AllayEntity allay) { String colour = Rainglow.generateRandomColourId(this.getRandom()); - ((AllayVariantProvider) allay).setVariant(RainglowColour.get(colour)); - cir.setReturnValue(new AllayEntityData(RainglowColour.get(colour))); + ((AllayVariantProvider) allay).setVariant(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour)); + cir.setReturnValue(new AllayEntityData(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour))); } else if ((Object) this instanceof SlimeEntity slime) { String colour = Rainglow.generateRandomColourId(this.getRandom()); - ((SlimeVariantProvider) slime).setVariant(RainglowColour.get(colour)); - cir.setReturnValue(new SlimeEntityData(RainglowColour.get(colour))); + ((SlimeVariantProvider) slime).setVariant(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.LIME : RainglowColour.get(colour)); + cir.setReturnValue(new SlimeEntityData(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.LIME : RainglowColour.get(colour))); } } } diff --git a/src/main/resources/assets/rainglow/lang/en_us.json b/src/main/resources/assets/rainglow/lang/en_us.json index e1d1f95..d209973 100644 --- a/src/main/resources/assets/rainglow/lang/en_us.json +++ b/src/main/resources/assets/rainglow/lang/en_us.json @@ -12,6 +12,8 @@ "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.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", From 14cb36df79a6968597f047827d08f54cef9d5d8d Mon Sep 17 00:00:00 2001 From: HellionGames Date: Sat, 7 Oct 2023 18:54:24 -0400 Subject: [PATCH 2/2] Abstracted Colour Rarity into method --- .../ix0rai/rainglow/mixin/MobEntityMixin.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java index 5698821..678c5f4 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java @@ -17,10 +17,12 @@ 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; import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @@ -36,16 +38,21 @@ protected MobEntityMixin(EntityType entityType, World world public void initialize(ServerWorldAccess world, LocalDifficulty difficulty, SpawnReason spawnReason, EntityData entityData, NbtCompound entityNbt, CallbackInfoReturnable cir) { if ((Object) this instanceof GlowSquidEntity glowSquid) { String colour = Rainglow.generateRandomColourId(this.getRandom()); - ((GlowSquidVariantProvider) glowSquid).setVariant(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour)); - cir.setReturnValue(new GlowSquidEntityData(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour))); + ((GlowSquidVariantProvider) glowSquid).setVariant(getColourOrDefault(this.random, RainglowColour.BLUE, colour)); + cir.setReturnValue(new GlowSquidEntityData(getColourOrDefault(this.random, RainglowColour.BLUE, colour))); } else if ((Object) this instanceof AllayEntity allay) { String colour = Rainglow.generateRandomColourId(this.getRandom()); - ((AllayVariantProvider) allay).setVariant(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour)); - cir.setReturnValue(new AllayEntityData(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour))); + ((AllayVariantProvider) allay).setVariant(getColourOrDefault(this.random, RainglowColour.BLUE, colour)); + cir.setReturnValue(new AllayEntityData(getColourOrDefault(this.random, RainglowColour.BLUE, colour))); } else if ((Object) this instanceof SlimeEntity slime) { String colour = Rainglow.generateRandomColourId(this.getRandom()); - ((SlimeVariantProvider) slime).setVariant(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.LIME : RainglowColour.get(colour)); - cir.setReturnValue(new SlimeEntityData(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.LIME : RainglowColour.get(colour))); + ((SlimeVariantProvider) slime).setVariant(getColourOrDefault(this.random, RainglowColour.LIME, colour)); + cir.setReturnValue(new SlimeEntityData(getColourOrDefault(this.random, RainglowColour.LIME, colour))); } } + + @Unique + private static RainglowColour getColourOrDefault(RandomGenerator random, RainglowColour defaultColour, String randomColour) { + return random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? defaultColour : RainglowColour.get(randomColour); + } }