Added weighted randomness to color list.
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package dev.micle.firefly_bush_backport.config;
|
||||
|
||||
import dev.micle.firefly_bush_backport.FireflyBushBackport;
|
||||
import net.minecraft.util.random.SimpleWeightedRandomList;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraftforge.common.ForgeConfigSpec;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
@ -10,7 +11,6 @@ import net.minecraftforge.fml.event.config.ModConfigEvent;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Mod.EventBusSubscriber(modid = FireflyBushBackport.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
@ -70,7 +70,8 @@ public final class Config {
|
||||
public static ForgeConfigSpec.DoubleValue particleExtraFriction;
|
||||
public static ForgeConfigSpec.DoubleValue particleExtraScale;
|
||||
public static ForgeConfigSpec.DoubleValue particleExtraBrightness;
|
||||
public static ForgeConfigSpec.ConfigValue<List<? extends String>> particleExtraColors;
|
||||
private static ForgeConfigSpec.ConfigValue<List<? extends String>> particleExtraColors;
|
||||
public static SimpleWeightedRandomList<Color> particleExtraColorsWeightedList;
|
||||
|
||||
Client(ForgeConfigSpec.Builder builder) {
|
||||
builder.comment("Settings for the firefly bush.").push("bush");
|
||||
@ -108,15 +109,23 @@ public final class Config {
|
||||
particleExtraBrightness = builder
|
||||
.defineInRange("particleExtraBrightness", 255.0, 0, 255);
|
||||
particleExtraColors = builder
|
||||
.defineList("particleExtraColors", List.of("#ffffff"), Client::isValidColorEntry);
|
||||
.comment("List of color hex values and their weight. Format: '<color_hex>,<weight>'")
|
||||
.defineList("particleExtraColors", List.of("#ffffff,1"), Client::isValidColorEntry);
|
||||
builder.pop();
|
||||
builder.pop();
|
||||
}
|
||||
|
||||
private static void onConfigReload() {}
|
||||
private static void onConfigReload() {
|
||||
SimpleWeightedRandomList.Builder<Color> particleExtraColorsWeightedListBuilder = SimpleWeightedRandomList.builder();
|
||||
for (String entry : particleExtraColors.get()) {
|
||||
String[] splitEntry = entry.split(",");
|
||||
particleExtraColorsWeightedListBuilder.add(Color.decode(splitEntry[0]), Integer.parseInt(splitEntry[1]));
|
||||
}
|
||||
particleExtraColorsWeightedList = particleExtraColorsWeightedListBuilder.build();
|
||||
}
|
||||
|
||||
private static boolean isValidColorEntry(Object entry) {
|
||||
return entry instanceof String && ((String) entry).matches("#(\\w{6})$");
|
||||
return entry instanceof String && ((String) entry).matches("#\\w{6},\\d+$");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class FireflyParticle extends TextureSheetParticle {
|
||||
@ -103,9 +103,10 @@ public class FireflyParticle extends TextureSheetParticle {
|
||||
fireflyParticle.pickSprite(this.sprite);
|
||||
fireflyParticle.setAlpha(0.0F);
|
||||
|
||||
List<? extends String> possibleColors = Config.Client.particleExtraColors.get();
|
||||
Color color = Color.decode(possibleColors.get(clientLevel.random.nextIntBetweenInclusive(0, possibleColors.size() - 1)));
|
||||
fireflyParticle.setColor(color.getRed() / 255F, color.getGreen() / 255F, color.getBlue() / 255F);
|
||||
Optional<Color> optionalColor = Config.Client.particleExtraColorsWeightedList.getRandomValue(clientLevel.random);
|
||||
optionalColor.ifPresent(color ->
|
||||
fireflyParticle.setColor(color.getRed() / 255F, color.getGreen() / 255F, color.getBlue() / 255F)
|
||||
);
|
||||
|
||||
return fireflyParticle;
|
||||
}
|
||||
|
Reference in New Issue
Block a user