Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented rarity config #21

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 6 additions & 6 deletions src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ 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(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)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you abstract this into a method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing! I'll get that done real fast!

} 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)));
}
}
}
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