Skip to content

Commit

Permalink
Merge pull request #21 from Dariensg/master
Browse files Browse the repository at this point in the history
Implemented rarity config
  • Loading branch information
ix0rai authored Oct 7, 2023
2 parents e6c9dda + 14cb36d commit d670dd4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/main/java/io/ix0rai/rainglow/config/ConfigIo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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("[");
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/ix0rai/rainglow/config/RainglowConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<RainglowEntity, String> TO_CONFIG_KEY = entity -> "enable_" + entity.getId();

private RainglowMode mode;
private List<RainglowColour> custom;
private int rarity;
private boolean enableServerSync;
private boolean editLocked = false;
private boolean isInitialised = false;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -97,6 +107,10 @@ public List<RainglowColour> getCustom() {
return this.custom;
}

public int getRarity() {
return this.rarity;
}

public boolean isServerSyncEnabled() {
return this.enableServerSync;
}
Expand All @@ -120,6 +134,10 @@ public void setCustom(List<RainglowColour> custom) {
Rainglow.refreshColours();
}

public void setRarity(int rarity) {
this.rarity = rarity;
}

public void setEditLocked(boolean editLocked) {
this.editLocked = editLocked;
}
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,16 +38,21 @@ protected MobEntityMixin(EntityType<? extends MobEntity> entityType, World world
public void initialize(ServerWorldAccess world, LocalDifficulty difficulty, SpawnReason spawnReason, EntityData entityData, NbtCompound entityNbt, CallbackInfoReturnable<EntityData> 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(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(RainglowColour.get(colour));
cir.setReturnValue(new AllayEntityData(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(RainglowColour.get(colour));
cir.setReturnValue(new SlimeEntityData(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);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/assets/rainglow/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d670dd4

Please sign in to comment.