From b56848a8a4be2aa41655ede54322917a67712206 Mon Sep 17 00:00:00 2001 From: yurisuika Date: Sat, 16 Sep 2023 20:00:32 -0700 Subject: [PATCH] Move config to new class --- .../java/dev/yurisuika/blossom/Blossom.java | 217 +---------------- .../blossom/block/FruitingLeavesBlock.java | 2 +- .../blossom/client/option/BlossomConfig.java | 222 ++++++++++++++++++ .../blossom/entity/ai/goal/BlossomGoal.java | 1 + .../blossom/entity/ai/goal/FruitGoal.java | 1 + .../server/command/BlossomCommand.java | 22 +- 6 files changed, 238 insertions(+), 227 deletions(-) create mode 100644 src/main/java/dev/yurisuika/blossom/client/option/BlossomConfig.java diff --git a/src/main/java/dev/yurisuika/blossom/Blossom.java b/src/main/java/dev/yurisuika/blossom/Blossom.java index 1d344abd..85bf31db 100644 --- a/src/main/java/dev/yurisuika/blossom/Blossom.java +++ b/src/main/java/dev/yurisuika/blossom/Blossom.java @@ -1,7 +1,5 @@ package dev.yurisuika.blossom; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import dev.yurisuika.blossom.block.FloweringLeavesBlock; import dev.yurisuika.blossom.block.FruitingLeavesBlock; import dev.yurisuika.blossom.client.particle.BlossomParticle; @@ -38,228 +36,17 @@ import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; -import net.minecraftforge.fml.loading.FMLPaths; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegistryObject; -import java.io.File; -import java.io.FileWriter; -import java.nio.file.Files; -import java.util.Arrays; import java.util.function.Supplier; +import static dev.yurisuika.blossom.client.option.BlossomConfig.*; + @Mod("blossom") public class Blossom { - public static File file = new File(FMLPaths.CONFIGDIR.get().toFile(), "blossom.json"); - public static Gson gson = new GsonBuilder().enableComplexMapKeySerialization().setPrettyPrinting().create(); - public static Config config = new Config(); - - public static class Config { - - public Value value = new Value( - new Value.Blossoming(0.2F, 10.0D), - new Value.Fruiting(0.2F, 10.0D), - new Value.Harvesting(3, 0.5714286F) - ); - public Filter filter = new Filter( - new Filter.Temperature(-2.0F, 2.0F), - new Filter.Downfall(0.0F, 1.0F), - new Filter.Dimension(new String[]{"minecraft:overworld"}, new String[]{"minecraft:the_nether", "minecraft:the_end"}), - new Filter.Biome(new String[]{"minecraft:forest"}, new String[]{"minecraft:the_void"}) - ); - public Toggle toggle = new Toggle( - false, - false - ); - - } - - public static class Value { - - public Blossoming blossoming; - public Fruiting fruiting; - public Harvesting harvesting; - - public Value(Blossoming blossoming, Fruiting fruiting, Harvesting harvesting) { - this.blossoming = blossoming; - this.fruiting = fruiting; - this.harvesting = harvesting; - } - - public static class Blossoming { - - public float chance; - public double distance; - - public Blossoming(float chance, double distance) { - this.chance = chance; - this.distance = distance; - } - - } - - public static class Fruiting { - - public float chance; - public double distance; - - public Fruiting(float chance, double distance) { - this.chance = chance; - this.distance = distance; - } - - } - - public static class Harvesting { - - public int bonus; - public float chance; - - public Harvesting(int bonus, float chance) { - this.bonus = bonus; - this.chance = chance; - } - - } - - } - - public static class Filter { - - public Temperature temperature; - public Downfall downfall; - public Dimension dimension; - public Biome biome; - - public Filter(Temperature temperature, Downfall downfall, Dimension dimension, Biome biome) { - this.temperature = temperature; - this.downfall = downfall; - this.dimension = dimension; - this.biome = biome; - } - - public static class Temperature { - - public float min; - public float max; - - public Temperature(float min, float max) { - this.min = min; - this.max = max; - } - - } - - public static class Downfall { - - public float min; - public float max; - - public Downfall(float min, float max) { - this.min = min; - this.max = max; - } - - } - - public static class Dimension { - - public String[] whitelist; - public String[] blacklist; - - public Dimension(String[] whitelist, String[] blacklist) { - this.whitelist = whitelist; - this.blacklist = blacklist; - } - - } - - public static class Biome { - - public String[] whitelist; - public String[] blacklist; - - public Biome(String[] whitelist, String[] blacklist) { - this.whitelist = whitelist; - this.blacklist = blacklist; - } - - } - - } - - public static class Toggle { - - public boolean whitelist; - public boolean blacklist; - - public Toggle(boolean whitelist, boolean blacklist) { - this.whitelist = whitelist; - this.blacklist = blacklist; - } - - } - - public static void saveConfig() { - try { - FileWriter fileWriter = new FileWriter(file); - fileWriter.write(gson.toJson(getConfig())); - fileWriter.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public static void loadConfig() { - try { - if (file.exists()) { - config = gson.fromJson(Files.readString(file.toPath()), Config.class); - } else { - config = new Config(); - } - } catch (Exception e) { - e.printStackTrace(); - } - checkBounds(); - setConfig(config); - } - - public static void setConfig(Config config) { - Blossom.config = config; - } - - public static Config getConfig() { - return config; - } - - public static void checkBounds() { - config.value.blossoming.chance = Math.max(Math.min(config.value.blossoming.chance, 1.0F), 0.0F); - config.value.blossoming.distance = Math.max(config.value.blossoming.distance, 0.0D); - config.value.fruiting.chance = Math.max(Math.min(config.value.fruiting.chance, 1.0F), 0.0F); - config.value.fruiting.distance = Math.max(config.value.fruiting.distance, 0.0D); - config.value.harvesting.bonus = Math.max(config.value.harvesting.bonus, 0); - config.value.harvesting.chance = Math.max(Math.min(config.value.harvesting.chance, 1.0F), 0.0F); - - float temperatureMin = Math.max(Math.min(Math.min(config.filter.temperature.min, 2.0F), config.filter.temperature.max), -2.0F); - float temperatureMax = Math.max(Math.max(Math.min(config.filter.temperature.max, 2.0F), config.filter.temperature.min), -2.0F); - config.filter.temperature.min = temperatureMin; - config.filter.temperature.max = temperatureMax; - - float downfallMin = Math.max(Math.min(Math.min(config.filter.downfall.min, 1.0F), config.filter.downfall.max), 0.0F); - float downfallMax = Math.max(Math.max(Math.min(config.filter.downfall.max, 1.0F), config.filter.downfall.min), 0.0F); - config.filter.downfall.min = downfallMin; - config.filter.downfall.max = downfallMax; - - Arrays.sort(config.filter.dimension.whitelist); - Arrays.sort(config.filter.dimension.blacklist); - Arrays.sort(config.filter.biome.whitelist); - Arrays.sort(config.filter.biome.blacklist); - - saveConfig(); - } - public static final DeferredRegister BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, "blossom"); public static final DeferredRegister ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, "blossom"); public static final DeferredRegister> PARTICLES = DeferredRegister.create(ForgeRegistries.PARTICLE_TYPES, "blossom"); diff --git a/src/main/java/dev/yurisuika/blossom/block/FruitingLeavesBlock.java b/src/main/java/dev/yurisuika/blossom/block/FruitingLeavesBlock.java index 8bcae8d9..5b748ede 100644 --- a/src/main/java/dev/yurisuika/blossom/block/FruitingLeavesBlock.java +++ b/src/main/java/dev/yurisuika/blossom/block/FruitingLeavesBlock.java @@ -42,7 +42,7 @@ import java.util.OptionalInt; import java.util.concurrent.ThreadLocalRandom; -import static dev.yurisuika.blossom.Blossom.*; +import static dev.yurisuika.blossom.client.option.BlossomConfig.*; public class FruitingLeavesBlock extends LeavesBlock implements Fertilizable { diff --git a/src/main/java/dev/yurisuika/blossom/client/option/BlossomConfig.java b/src/main/java/dev/yurisuika/blossom/client/option/BlossomConfig.java new file mode 100644 index 00000000..c7c46e9f --- /dev/null +++ b/src/main/java/dev/yurisuika/blossom/client/option/BlossomConfig.java @@ -0,0 +1,222 @@ +package dev.yurisuika.blossom.client.option; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import net.minecraftforge.fml.loading.FMLPaths; + +import java.io.File; +import java.io.FileWriter; +import java.nio.file.Files; +import java.util.Arrays; + +public class BlossomConfig { + + public static File file = new File(FMLPaths.CONFIGDIR.get().toFile(), "blossom.json"); + public static Gson gson = new GsonBuilder().enableComplexMapKeySerialization().setPrettyPrinting().create(); + public static Config config = new Config(); + + public static class Config { + + public Value value = new Value( + new Value.Blossoming(0.2F, 10.0D), + new Value.Fruiting(0.2F, 10.0D), + new Value.Harvesting(3, 0.5714286F) + ); + public Filter filter = new Filter( + new Filter.Temperature(-2.0F, 2.0F), + new Filter.Downfall(0.0F, 1.0F), + new Filter.Dimension(new String[]{"minecraft:overworld"}, new String[]{"minecraft:the_nether", "minecraft:the_end"}), + new Filter.Biome(new String[]{"minecraft:forest"}, new String[]{"minecraft:the_void"}) + ); + public Toggle toggle = new Toggle( + false, + false + ); + + public static class Value { + + public Blossoming blossoming; + public Fruiting fruiting; + public Harvesting harvesting; + + public Value(Blossoming blossoming, Fruiting fruiting, Harvesting harvesting) { + this.blossoming = blossoming; + this.fruiting = fruiting; + this.harvesting = harvesting; + } + + public static class Blossoming { + + public float chance; + public double distance; + + public Blossoming(float chance, double distance) { + this.chance = chance; + this.distance = distance; + } + + } + + public static class Fruiting { + + public float chance; + public double distance; + + public Fruiting(float chance, double distance) { + this.chance = chance; + this.distance = distance; + } + + } + + public static class Harvesting { + + public int bonus; + public float chance; + + public Harvesting(int bonus, float chance) { + this.bonus = bonus; + this.chance = chance; + } + + } + + } + + public static class Filter { + + public Temperature temperature; + public Downfall downfall; + public Dimension dimension; + public Biome biome; + + public Filter(Temperature temperature, Downfall downfall, Dimension dimension, Biome biome) { + this.temperature = temperature; + this.downfall = downfall; + this.dimension = dimension; + this.biome = biome; + } + + public static class Temperature { + + public float min; + public float max; + + public Temperature(float min, float max) { + this.min = min; + this.max = max; + } + + } + + public static class Downfall { + + public float min; + public float max; + + public Downfall(float min, float max) { + this.min = min; + this.max = max; + } + + } + + public static class Dimension { + + public String[] whitelist; + public String[] blacklist; + + public Dimension(String[] whitelist, String[] blacklist) { + this.whitelist = whitelist; + this.blacklist = blacklist; + } + + } + + public static class Biome { + + public String[] whitelist; + public String[] blacklist; + + public Biome(String[] whitelist, String[] blacklist) { + this.whitelist = whitelist; + this.blacklist = blacklist; + } + + } + + } + + public static class Toggle { + + public boolean whitelist; + public boolean blacklist; + + public Toggle(boolean whitelist, boolean blacklist) { + this.whitelist = whitelist; + this.blacklist = blacklist; + } + + } + + } + + public static void saveConfig() { + try { + FileWriter fileWriter = new FileWriter(file); + fileWriter.write(gson.toJson(getConfig())); + fileWriter.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void loadConfig() { + try { + if (file.exists()) { + config = gson.fromJson(Files.readString(file.toPath()), Config.class); + } else { + config = new Config(); + } + } catch (Exception e) { + e.printStackTrace(); + } + checkBounds(); + setConfig(config); + } + + public static void setConfig(Config config) { + BlossomConfig.config = config; + } + + public static Config getConfig() { + return config; + } + + public static void checkBounds() { + config.value.blossoming.chance = Math.max(Math.min(config.value.blossoming.chance, 1.0F), 0.0F); + config.value.blossoming.distance = Math.max(config.value.blossoming.distance, 0.0D); + config.value.fruiting.chance = Math.max(Math.min(config.value.fruiting.chance, 1.0F), 0.0F); + config.value.fruiting.distance = Math.max(config.value.fruiting.distance, 0.0D); + config.value.harvesting.bonus = Math.max(config.value.harvesting.bonus, 0); + config.value.harvesting.chance = Math.max(Math.min(config.value.harvesting.chance, 1.0F), 0.0F); + + float temperatureMin = Math.max(Math.min(Math.min(config.filter.temperature.min, 2.0F), config.filter.temperature.max), -2.0F); + float temperatureMax = Math.max(Math.max(Math.min(config.filter.temperature.max, 2.0F), config.filter.temperature.min), -2.0F); + config.filter.temperature.min = temperatureMin; + config.filter.temperature.max = temperatureMax; + + float downfallMin = Math.max(Math.min(Math.min(config.filter.downfall.min, 1.0F), config.filter.downfall.max), 0.0F); + float downfallMax = Math.max(Math.max(Math.min(config.filter.downfall.max, 1.0F), config.filter.downfall.min), 0.0F); + config.filter.downfall.min = downfallMin; + config.filter.downfall.max = downfallMax; + + Arrays.sort(config.filter.dimension.whitelist); + Arrays.sort(config.filter.dimension.blacklist); + Arrays.sort(config.filter.biome.whitelist); + Arrays.sort(config.filter.biome.blacklist); + + saveConfig(); + } + +} \ No newline at end of file diff --git a/src/main/java/dev/yurisuika/blossom/entity/ai/goal/BlossomGoal.java b/src/main/java/dev/yurisuika/blossom/entity/ai/goal/BlossomGoal.java index 6ad8e9b6..a6f565d8 100644 --- a/src/main/java/dev/yurisuika/blossom/entity/ai/goal/BlossomGoal.java +++ b/src/main/java/dev/yurisuika/blossom/entity/ai/goal/BlossomGoal.java @@ -27,6 +27,7 @@ import java.util.function.Predicate; import static dev.yurisuika.blossom.Blossom.*; +import static dev.yurisuika.blossom.client.option.BlossomConfig.*; import static net.minecraft.block.LeavesBlock.*; public class BlossomGoal extends Goal { diff --git a/src/main/java/dev/yurisuika/blossom/entity/ai/goal/FruitGoal.java b/src/main/java/dev/yurisuika/blossom/entity/ai/goal/FruitGoal.java index a4bb2051..6447729e 100644 --- a/src/main/java/dev/yurisuika/blossom/entity/ai/goal/FruitGoal.java +++ b/src/main/java/dev/yurisuika/blossom/entity/ai/goal/FruitGoal.java @@ -14,6 +14,7 @@ import java.util.function.Predicate; import static dev.yurisuika.blossom.Blossom.*; +import static dev.yurisuika.blossom.client.option.BlossomConfig.*; import static net.minecraft.block.LeavesBlock.*; public class FruitGoal extends BlossomGoal { diff --git a/src/main/java/dev/yurisuika/blossom/server/command/BlossomCommand.java b/src/main/java/dev/yurisuika/blossom/server/command/BlossomCommand.java index 76f8bb21..f38d66f0 100644 --- a/src/main/java/dev/yurisuika/blossom/server/command/BlossomCommand.java +++ b/src/main/java/dev/yurisuika/blossom/server/command/BlossomCommand.java @@ -16,7 +16,7 @@ import java.util.*; -import static dev.yurisuika.blossom.Blossom.*; +import static dev.yurisuika.blossom.client.option.BlossomConfig.*; import static net.minecraft.server.command.CommandManager.*; public class BlossomCommand { @@ -35,18 +35,18 @@ public static void register(CommandDispatcher dispatcher, C ) .then(literal("reset") .executes(context -> { - config.value = new Value( - new Value.Blossoming(0.2F, 10.0D), - new Value.Fruiting(0.2F, 10.0D), - new Value.Harvesting(3, 0.5714286F) + config.value = new Config.Value( + new Config.Value.Blossoming(0.2F, 10.0D), + new Config.Value.Fruiting(0.2F, 10.0D), + new Config.Value.Harvesting(3, 0.5714286F) ); - config.filter = new Filter( - new Filter.Temperature(-2.0F, 2.0F), - new Filter.Downfall(0.0F, 1.0F), - new Filter.Dimension(new String[]{"minecraft:overworld"}, new String[]{"minecraft:the_nether", "minecraft:the_end"}), - new Filter.Biome(new String[]{"minecraft:forest"}, new String[]{"minecraft:the_void"}) + config.filter = new Config.Filter( + new Config.Filter.Temperature(-2.0F, 2.0F), + new Config.Filter.Downfall(0.0F, 1.0F), + new Config.Filter.Dimension(new String[]{"minecraft:overworld"}, new String[]{"minecraft:the_nether", "minecraft:the_end"}), + new Config.Filter.Biome(new String[]{"minecraft:forest"}, new String[]{"minecraft:the_void"}) ); - config.toggle = new Toggle( + config.toggle = new Config.Toggle( false, false );