From 8bdffdb09f688eceabe8a190cfcdaab14b4f4e27 Mon Sep 17 00:00:00 2001 From: vodofrede Date: Mon, 11 Oct 2021 19:10:32 +0200 Subject: [PATCH] Added a configuration screen; bumped version --- build.gradle | 11 ---- gradle.properties | 3 +- .../java/dk/palmoe/immersivexp/Config.java | 63 +++++++++++++++++++ .../java/dk/palmoe/immersivexp/Events.java | 13 ++-- src/main/java/dk/palmoe/immersivexp/Util.java | 6 +- .../immersivexp/mixin/ImmersiveXPMixin.java | 10 --- src/main/resources/fabric.mod.json | 5 +- src/main/resources/immersivexp.mixins.json | 4 +- 8 files changed, 80 insertions(+), 35 deletions(-) create mode 100644 src/main/java/dk/palmoe/immersivexp/Config.java delete mode 100644 src/main/java/dk/palmoe/immersivexp/mixin/ImmersiveXPMixin.java diff --git a/build.gradle b/build.gradle index c5c7aa8..4a35a5e 100644 --- a/build.gradle +++ b/build.gradle @@ -11,24 +11,13 @@ version = project.mod_version group = project.maven_group repositories { - // Add repositories to retrieve artifacts from in here. - // You should only use this when depending on other mods because - // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. - // See https://docs.gradle.org/current/userguide/declaring_repositories.html - // for more information about repositories. } dependencies { - // To change the versions see the gradle.properties file minecraft "com.mojang:minecraft:${project.minecraft_version}" mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" - - // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - - // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. - // You may need to force-disable transitiveness on them. } processResources { diff --git a/gradle.properties b/gradle.properties index 0a49ef3..b0556da 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,13 +2,12 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties - # check these on https://fabricmc.net/versions.html minecraft_version=1.17.1 yarn_mappings=1.17.1+build.39 loader_version=0.11.6 # Mod Properties - mod_version = 1.0.1 + mod_version = 1.0.2 maven_group = dk.palmoe archives_base_name = immersivexp diff --git a/src/main/java/dk/palmoe/immersivexp/Config.java b/src/main/java/dk/palmoe/immersivexp/Config.java new file mode 100644 index 0000000..9ea1ad7 --- /dev/null +++ b/src/main/java/dk/palmoe/immersivexp/Config.java @@ -0,0 +1,63 @@ +package dk.palmoe.immersivexp; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.Reader; + +import com.google.gson.Gson; +import com.google.gson.stream.JsonWriter; + +import org.apache.commons.io.IOUtils;; + +public class Config { + public int logBaseXP = 1; + public int logRandXP = 2; + public int cropBaseXP = 1; + public int cropRandXP = 1; + public int berriesBaseXP = 0; + public int berriesRandXP = 1; + + public static final File FILE = new File("config/immersivexp.json"); + public static Config INSTANCE = null; + + public static Config getInstance() { + if (INSTANCE == null) { + INSTANCE = new Config().read(); + } + return INSTANCE; + } + + private Config read() { + if (!FILE.exists()) + return new Config().write(); + + Reader reader = null; + try { + return new Gson().fromJson(reader = new FileReader(FILE), Config.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(reader); + } + } + + private Config write() { + Gson gson = new Gson(); + JsonWriter writer = null; + try { + writer = gson.newJsonWriter(new FileWriter(FILE)); + writer.setIndent(" "); + + gson.toJson(gson.toJsonTree(this, Config.class), writer); + } catch (Exception e) { + ImmersiveXP.LOGGER.error("Couldn't save config"); + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(writer); + } + return this; + } +} diff --git a/src/main/java/dk/palmoe/immersivexp/Events.java b/src/main/java/dk/palmoe/immersivexp/Events.java index 8eb2356..ee44040 100644 --- a/src/main/java/dk/palmoe/immersivexp/Events.java +++ b/src/main/java/dk/palmoe/immersivexp/Events.java @@ -7,7 +7,6 @@ import net.minecraft.block.CropBlock; import net.minecraft.block.MelonBlock; import net.minecraft.block.SweetBerryBushBlock; import net.minecraft.server.world.ServerWorld; -import net.minecraft.state.property.Property; import net.minecraft.tag.BlockTags; import net.minecraft.util.ActionResult; import net.minecraft.util.Identifier; @@ -16,10 +15,10 @@ import net.minecraft.util.registry.Registry; public class Events { public static void init() { PlayerBlockBreakEvents.AFTER.register((world, player, pos, state, entity) -> { - // TODO: Make experience amount configurable if (world instanceof ServerWorld) { if (state.getBlock() instanceof CropBlock || state.getBlock() instanceof MelonBlock) { - Util.spawnExp((ServerWorld) world, pos, 1, 1); + Config config = Config.getInstance(); + Util.spawnXp((ServerWorld) world, pos, config.cropBaseXP, config.cropRandXP); } if (state.isIn(BlockTags.LOGS)) { @@ -27,20 +26,22 @@ public class Events { boolean isGrown = Util.isTreeNaturallyGrown(world, pos); if (holdingAxe && isGrown) { - Util.spawnExp((ServerWorld) world, pos, 1, 4); + Config config = Config.getInstance(); + Util.spawnXp((ServerWorld) world, pos, config.logBaseXP, config.logRandXP); } } } }); UseBlockCallback.EVENT.register(((player, world, hand, hitResult) -> { - // TODO: Make experience amount configurable BlockState state = world.getBlockState(hitResult.getBlockPos()); if (state.getBlock() instanceof SweetBerryBushBlock && world instanceof ServerWorld) { int age = state.get(SweetBerryBushBlock.AGE); if (age == 3) { - Util.spawnExp((ServerWorld) world, hitResult.getBlockPos(), 1, 1); + Config config = Config.getInstance(); + Util.spawnXp((ServerWorld) world, hitResult.getBlockPos(), config.berriesBaseXP, + config.berriesRandXP); } } diff --git a/src/main/java/dk/palmoe/immersivexp/Util.java b/src/main/java/dk/palmoe/immersivexp/Util.java index 09622e6..d105f38 100644 --- a/src/main/java/dk/palmoe/immersivexp/Util.java +++ b/src/main/java/dk/palmoe/immersivexp/Util.java @@ -39,8 +39,8 @@ public class Util { return false; } - public static void spawnExp(ServerWorld world, BlockPos pos, int min, int cap) { - Vec3d exp_pos = new Vec3d(pos.getX(), pos.getY(), pos.getZ()); - ExperienceOrbEntity.spawn(world, exp_pos, min + world.random.nextInt(cap)); + public static void spawnXp(ServerWorld world, BlockPos pos, int min, int cap) { + Vec3d xp_pos = new Vec3d(pos.getX(), pos.getY(), pos.getZ()); + ExperienceOrbEntity.spawn(world, xp_pos, min + world.random.nextInt(cap)); } } diff --git a/src/main/java/dk/palmoe/immersivexp/mixin/ImmersiveXPMixin.java b/src/main/java/dk/palmoe/immersivexp/mixin/ImmersiveXPMixin.java deleted file mode 100644 index 04157a2..0000000 --- a/src/main/java/dk/palmoe/immersivexp/mixin/ImmersiveXPMixin.java +++ /dev/null @@ -1,10 +0,0 @@ -package dk.palmoe.immersivexp.mixin; - -import org.spongepowered.asm.mixin.Mixin; - -import net.minecraft.client.gui.screen.TitleScreen; - -@Mixin(TitleScreen.class) -public class ImmersiveXPMixin { - -} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index aef8150..154432b 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -18,7 +18,7 @@ "entrypoints": { "main": ["dk.palmoe.immersivexp.ImmersiveXP"] }, - "mixins": ["immersivexp.mixins.json"], + "mixins": [], "depends": { "fabricloader": ">=0.11.3", @@ -28,5 +28,8 @@ }, "suggests": { "another-mod": "*" + }, + "custom": { + "modmenu": {} } } diff --git a/src/main/resources/immersivexp.mixins.json b/src/main/resources/immersivexp.mixins.json index 1cb3b7e..e490900 100644 --- a/src/main/resources/immersivexp.mixins.json +++ b/src/main/resources/immersivexp.mixins.json @@ -3,8 +3,8 @@ "minVersion": "0.8", "package": "dk.palmoe.immersivexp.mixin", "compatibilityLevel": "JAVA_16", - "mixins": ["ImmersiveXPMixin"], - "client": ["ImmersiveXPMixin"], + "mixins": [], + "client": [], "injectors": { "defaultRequire": 1 }