From 3e7560183243fb652f20c907232ab06281d93b38 Mon Sep 17 00:00:00 2001 From: Cameron Reed Date: Mon, 12 Dec 2022 19:06:37 -0700 Subject: [PATCH] Updated to 1.19.3 --- gradle.properties | 10 +- .../java/cmods/haxxor/client/AutoFarmer.java | 23 ++-- .../cmods/haxxor/client/FallDamageCancel.java | 5 +- .../java/cmods/haxxor/client/FlyHack.java | 5 +- .../cmods/haxxor/client/HaxxorClient.java | 8 +- .../cmods/haxxor/client/HaxxorOptions.java | 98 ---------------- .../haxxor/client/options/BooleanOption.java | 11 ++ .../haxxor/client/options/HaxxorOptions.java | 105 ++++++++++++++++++ .../haxxor/client/options/IntegerOption.java | 23 ++++ .../cmods/haxxor/client/options/Option.java | 17 +++ .../haxxor/client/ui/CropSelectScreen.java | 41 +++---- .../haxxor/client/ui/CycleButtonWidget.java | 7 +- .../haxxor/client/ui/FarmerOptionsScreen.java | 91 +++++---------- .../haxxor/client/ui/HaxxorOptionsScreen.java | 29 +++-- .../haxxor/client/ui/IntegerAdjustWidget.java | 80 +++++++++++++ .../haxxor/client/ui/OptionsSlideWidget.java | 39 +++++++ .../cmods/haxxor/client/ui/ToggleButton.java | 42 +++++++ .../client/ui/ToggleEnableButtonWidget.java | 51 --------- .../java/cmods/haxxor/mixin/HudMixin.java | 6 +- .../java/cmods/haxxor/mixin/OptionsMixin.java | 6 +- .../java/cmods/haxxor/mixin/PauseMixin.java | 6 +- src/main/resources/fabric.mod.json | 8 +- 22 files changed, 428 insertions(+), 283 deletions(-) delete mode 100644 src/main/java/cmods/haxxor/client/HaxxorOptions.java create mode 100644 src/main/java/cmods/haxxor/client/options/BooleanOption.java create mode 100644 src/main/java/cmods/haxxor/client/options/HaxxorOptions.java create mode 100644 src/main/java/cmods/haxxor/client/options/IntegerOption.java create mode 100644 src/main/java/cmods/haxxor/client/options/Option.java create mode 100644 src/main/java/cmods/haxxor/client/ui/IntegerAdjustWidget.java create mode 100644 src/main/java/cmods/haxxor/client/ui/OptionsSlideWidget.java create mode 100644 src/main/java/cmods/haxxor/client/ui/ToggleButton.java delete mode 100644 src/main/java/cmods/haxxor/client/ui/ToggleEnableButtonWidget.java diff --git a/gradle.properties b/gradle.properties index 7f8fa6b..6aa3a9e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,13 +2,13 @@ org.gradle.jvmargs=-Xmx2G # Fabric Properties # check these on https://modmuss50.me/fabric.html -minecraft_version=1.19.2 -yarn_mappings=1.19.2+build.28 -loader_version=0.14.10 +minecraft_version=1.19.3 +yarn_mappings=1.19.3+build.2 +loader_version=0.14.11 # Mod Properties -mod_version=1.1.0 +mod_version=1.2.0 maven_group=cmods archives_base_name=haxxor # Dependencies # check this on https://modmuss50.me/fabric.html -fabric_version=0.67.1+1.19.2 +fabric_version=0.68.1+1.19.3 diff --git a/src/main/java/cmods/haxxor/client/AutoFarmer.java b/src/main/java/cmods/haxxor/client/AutoFarmer.java index b52ea85..c50d049 100644 --- a/src/main/java/cmods/haxxor/client/AutoFarmer.java +++ b/src/main/java/cmods/haxxor/client/AutoFarmer.java @@ -1,5 +1,6 @@ package cmods.haxxor.client; +import cmods.haxxor.client.options.HaxxorOptions; import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import net.minecraft.block.*; @@ -71,11 +72,11 @@ public class AutoFarmer { // Really just a namespace and needed to separate thin sendStopPackets(client); scanBlocks(client); - if (options.move_seeds && options.enabled) + if (options.move_seeds.get() && options.enabled.get()) adjustInventory(client); findActions(client); - if (options.enabled) + if (options.enabled.get()) run(client); } @@ -110,7 +111,7 @@ public class AutoFarmer { // Really just a namespace and needed to separate thin for (int i = 0; i < crops.length; i++) { int slot = inventory.getSlotWithStack(new ItemStack(crops[i].seed_type)); if (crops[i].seed_type != null && crops[i].planted_on != null && block.isOf(crops[i].planted_on) && - options.crops_enabled[i] && slot != -1) { + options.crops_enabled[i].get() && slot != -1) { if (!item_in_main) { if (PlayerInventory.isValidHotbarIndex(slot)) { selectSlot(client, slot); @@ -168,9 +169,9 @@ public class AutoFarmer { // Really just a namespace and needed to separate thin potentialActions.clear(); BlockPos playerPos = client.player.getBlockPos(); - for (int y = options.min_y; y <= options.max_y; y++) { - for (int x = -options.horizontal_reach; x <= options.horizontal_reach; x++) { - for (int z = -options.horizontal_reach; z <= options.horizontal_reach; z++) { + for (int y = options.min_y.get(); y <= options.max_y.get(); y++) { + for (int x = -options.horizontal_reach.get(); x <= options.horizontal_reach.get(); x++) { + for (int z = -options.horizontal_reach.get(); z <= options.horizontal_reach.get(); z++) { BlockPos pos = playerPos.add(x, y, z); ACTION_TYPE action = checkBlock(client, pos); @@ -192,17 +193,17 @@ public class AutoFarmer { // Really just a namespace and needed to separate thin AutoFarmerCropType cropType = crops[i]; if (cropType.growth_pattern == GROWTH_PATTERN.BASIC && block.isOf(cropType.crop_type) && - options.crops_enabled[i]) { + options.crops_enabled[i].get()) { return isMature(block, cropType.crop_type.getClass()) ? ACTION_TYPE.BREAK : ACTION_TYPE.NONE; } if (cropType.growth_pattern == GROWTH_PATTERN.LEAVE_BASE && block.isOf(cropType.crop_type) && - options.crops_enabled[i]) { + options.crops_enabled[i].get()) { return isSecondBlock(client.world, pos, cropType.crop_type) ? ACTION_TYPE.BREAK : ACTION_TYPE.NONE; } if (cropType.seed_type != null && cropType.planted_on != null && block.isOf(cropType.planted_on) && - client.world.getBlockState(pos.up()).isAir() && options.seeds_enabled[i]) { + client.world.getBlockState(pos.up()).isAir() && options.seeds_enabled[i].get()) { return ACTION_TYPE.PLACE; } } @@ -226,7 +227,7 @@ public class AutoFarmer { // Really just a namespace and needed to separate thin } } - if (queuedActions.size() >= options.max_actions_per_tick) + if (queuedActions.size() >= options.max_actions_per_tick.get()) return; } } @@ -244,7 +245,7 @@ public class AutoFarmer { // Really just a namespace and needed to separate thin AutoFarmerCropType cropType = crops[i]; if (cropType.seed_type != null && cropType.planted_on != null && block.isOf(cropType.planted_on) && - client.world.getBlockState(pos.up()).isAir() && options.seeds_enabled[i]) { + client.world.getBlockState(pos.up()).isAir() && options.seeds_enabled[i].get()) { if (mainHandItem.isOf(cropType.seed_type)) { return ACTION_TYPE.PLACE_MAIN_HAND; } else if (offHandItem.isOf(cropType.seed_type)) { diff --git a/src/main/java/cmods/haxxor/client/FallDamageCancel.java b/src/main/java/cmods/haxxor/client/FallDamageCancel.java index e7c4cf5..0dc0ac8 100644 --- a/src/main/java/cmods/haxxor/client/FallDamageCancel.java +++ b/src/main/java/cmods/haxxor/client/FallDamageCancel.java @@ -1,5 +1,6 @@ package cmods.haxxor.client; +import cmods.haxxor.client.options.HaxxorOptions; import net.minecraft.client.MinecraftClient; import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket; import net.minecraft.util.math.BlockPos; @@ -13,8 +14,8 @@ public class FallDamageCancel { private static final float heightAdjust = 0.5f; public static void tick(MinecraftClient client) { - if (!options.cancel_fall_damage || client.world == null || client.player == null || client.player.isCreative() - || client.player.isOnGround() || client.player.getVelocity().getY() >= 0) + if (!options.cancel_fall_damage.get() || client.world == null || client.player == null || + client.player.isCreative() || client.player.isOnGround() || client.player.getVelocity().getY() >= 0) return; BlockPos playerPos = client.player.getBlockPos(); diff --git a/src/main/java/cmods/haxxor/client/FlyHack.java b/src/main/java/cmods/haxxor/client/FlyHack.java index 8ace884..5ba7147 100644 --- a/src/main/java/cmods/haxxor/client/FlyHack.java +++ b/src/main/java/cmods/haxxor/client/FlyHack.java @@ -1,5 +1,6 @@ package cmods.haxxor.client; +import cmods.haxxor.client.options.HaxxorOptions; import net.minecraft.client.MinecraftClient; import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket; @@ -18,9 +19,9 @@ public class FlyHack { if (client.player == null || client.world == null || client.player.isCreative()) return; - client.player.getAbilities().allowFlying = options.enabled; + client.player.getAbilities().allowFlying = options.enabled.get(); - if (!options.enabled && client.player.getAbilities().flying) { + if (!options.enabled.get() && client.player.getAbilities().flying) { client.player.getAbilities().flying = false; } diff --git a/src/main/java/cmods/haxxor/client/HaxxorClient.java b/src/main/java/cmods/haxxor/client/HaxxorClient.java index 4d38b8f..5ccd9f0 100644 --- a/src/main/java/cmods/haxxor/client/HaxxorClient.java +++ b/src/main/java/cmods/haxxor/client/HaxxorClient.java @@ -1,5 +1,6 @@ package cmods.haxxor.client; +import cmods.haxxor.client.options.HaxxorOptions; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -49,13 +50,14 @@ public class HaxxorClient implements ClientModInitializer { public static boolean toggleAutoFarmer() { auto_farm_key.setPressed(true); + options.autoFarmer.enabled.set(auto_farm_key.isPressed()); - return options.autoFarmer.enabled = auto_farm_key.isPressed(); + return options.autoFarmer.enabled.get(); } private void tick(MinecraftClient client) { - options.autoFarmer.enabled = auto_farm_key.isPressed(); - options.flyHack.enabled = fly_hack_key.isPressed(); + options.autoFarmer.enabled.set(auto_farm_key.isPressed()); + options.flyHack.enabled.set(fly_hack_key.isPressed()); AutoFarmer.tick(client); FlyHack.tick(client); diff --git a/src/main/java/cmods/haxxor/client/HaxxorOptions.java b/src/main/java/cmods/haxxor/client/HaxxorOptions.java deleted file mode 100644 index c45c2d1..0000000 --- a/src/main/java/cmods/haxxor/client/HaxxorOptions.java +++ /dev/null @@ -1,98 +0,0 @@ -package cmods.haxxor.client; - -import java.util.prefs.Preferences; - -public final class HaxxorOptions { - private static HaxxorOptions instance = null; - - public final AutoFarmerOptions autoFarmer; - public final FlyHackOptions flyHack; - - public boolean cancel_fall_damage = true; - - - public static HaxxorOptions getInstance() { - if (instance == null) - instance = new HaxxorOptions(); - - return instance; - } - - private HaxxorOptions() { - autoFarmer = new AutoFarmerOptions(); - flyHack = new FlyHackOptions(); - - load(); - } - - public void load() { - autoFarmer.load(); - } - - public void save() { - autoFarmer.save(); - } - - - - public static class AutoFarmerOptions { - private final Preferences prefs = Preferences.userNodeForPackage(AutoFarmerOptions.class); - - public boolean enabled; - public boolean move_seeds; - - public int min_y = -2; - public int max_y = 1; - - public int horizontal_reach = 4; - public int max_actions_per_tick = 30; - - public final boolean[] seeds_enabled = new boolean[AutoFarmer.crops.length]; - public final boolean[] crops_enabled = new boolean[AutoFarmer.crops.length]; - - public AutoFarmerOptions() { } - - - private void load() { - move_seeds = prefs.getBoolean("move_seeds", move_seeds); - min_y = prefs.getInt("min_y", min_y); - max_y = prefs.getInt("max_y", max_y); - horizontal_reach = prefs.getInt("horizontal_reach", horizontal_reach); - max_actions_per_tick = prefs.getInt("max_actions_per_tick", max_actions_per_tick); - - for (int i = 0; i < AutoFarmer.crops.length; i++) { - if (AutoFarmer.crops[i].seed_type != null) { - seeds_enabled[i] = prefs.getBoolean("seed_" + AutoFarmer.crops[i].seed_type.getName().getString(), - seeds_enabled[i]); - } - - if (AutoFarmer.crops[i].crop_type != null) { - crops_enabled[i] = prefs.getBoolean("crop_" + AutoFarmer.crops[i].crop_type.getName().getString(), - crops_enabled[i]); - } - } - } - - private void save() { - prefs.putBoolean("move_seeds", move_seeds); - prefs.putInt("min_y", min_y); - prefs.putInt("max_y", max_y); - prefs.putInt("horizontal_reach", horizontal_reach); - prefs.putInt("max_actions_per_tick", max_actions_per_tick); - - for (int i = 0; i < AutoFarmer.crops.length; i++) { - if (AutoFarmer.crops[i].seed_type != null) { - prefs.putBoolean("seed_" + AutoFarmer.crops[i].seed_type.getName().getString(), seeds_enabled[i]); - } - - if (AutoFarmer.crops[i].crop_type != null) { - prefs.putBoolean("crop_" + AutoFarmer.crops[i].crop_type.getName().getString(), crops_enabled[i]); - } - } - } - } - - public static class FlyHackOptions { - public boolean enabled; - } -} diff --git a/src/main/java/cmods/haxxor/client/options/BooleanOption.java b/src/main/java/cmods/haxxor/client/options/BooleanOption.java new file mode 100644 index 0000000..2401635 --- /dev/null +++ b/src/main/java/cmods/haxxor/client/options/BooleanOption.java @@ -0,0 +1,11 @@ +package cmods.haxxor.client.options; + +public class BooleanOption extends Option { + BooleanOption(Boolean default_value) { + super(default_value); + } + + public void toggle() { + value = !value; + } +} diff --git a/src/main/java/cmods/haxxor/client/options/HaxxorOptions.java b/src/main/java/cmods/haxxor/client/options/HaxxorOptions.java new file mode 100644 index 0000000..58e7d3b --- /dev/null +++ b/src/main/java/cmods/haxxor/client/options/HaxxorOptions.java @@ -0,0 +1,105 @@ +package cmods.haxxor.client.options; + +import cmods.haxxor.client.AutoFarmer; + +import java.util.prefs.Preferences; + +public final class HaxxorOptions { + private static HaxxorOptions instance = null; + + public final AutoFarmerOptions autoFarmer; + public final FlyHackOptions flyHack; + + public BooleanOption cancel_fall_damage = new BooleanOption(true); + + + public static HaxxorOptions getInstance() { + if (instance == null) + instance = new HaxxorOptions(); + + return instance; + } + + private HaxxorOptions() { + autoFarmer = new AutoFarmerOptions(); + flyHack = new FlyHackOptions(); + + load(); + } + + public void load() { + autoFarmer.load(); + } + + public void save() { + autoFarmer.save(); + } + + + + public static class AutoFarmerOptions { + private final Preferences prefs = Preferences.userNodeForPackage(AutoFarmerOptions.class); + + public BooleanOption enabled = new BooleanOption(false); + public BooleanOption move_seeds = new BooleanOption(true); + + public IntegerOption min_y = new IntegerOption(-2); + public IntegerOption max_y = new IntegerOption(1); + + public IntegerOption horizontal_reach = new IntegerOption(4); + public IntegerOption max_actions_per_tick = new IntegerOption(30); + + public final BooleanOption[] seeds_enabled = new BooleanOption[AutoFarmer.crops.length]; + public final BooleanOption[] crops_enabled = new BooleanOption[AutoFarmer.crops.length]; + + public AutoFarmerOptions() { } + + + private void load() { + move_seeds.value = prefs.getBoolean("move_seeds", move_seeds.value); + min_y.value = prefs.getInt("min_y", min_y.value); + max_y.value = prefs.getInt("max_y", max_y.value); + horizontal_reach.value = prefs.getInt("horizontal_reach", horizontal_reach.value); + max_actions_per_tick.value = prefs.getInt("max_actions_per_tick", max_actions_per_tick.value); + + for (int i = 0; i < AutoFarmer.crops.length; i++) { + seeds_enabled[i] = new BooleanOption(true); + crops_enabled[i] = new BooleanOption(true); + + if (AutoFarmer.crops[i].seed_type != null) { + seeds_enabled[i].value = prefs.getBoolean("seed_" + AutoFarmer.crops[i].seed_type.getName().getString(), + seeds_enabled[i].value); + } + + if (AutoFarmer.crops[i].crop_type != null) { + crops_enabled[i].value = prefs.getBoolean("crop_" + AutoFarmer.crops[i].crop_type.getName().getString(), + crops_enabled[i].value); + } + } + } + + private void save() { + prefs.putBoolean("move_seeds", move_seeds.value); + prefs.putInt("min_y", min_y.value); + prefs.putInt("max_y", max_y.value); + prefs.putInt("horizontal_reach", horizontal_reach.value); + prefs.putInt("max_actions_per_tick", max_actions_per_tick.value); + + for (int i = 0; i < AutoFarmer.crops.length; i++) { + if (AutoFarmer.crops[i].seed_type != null) { + prefs.putBoolean("seed_" + AutoFarmer.crops[i].seed_type.getName().getString(), + seeds_enabled[i].value); + } + + if (AutoFarmer.crops[i].crop_type != null) { + prefs.putBoolean("crop_" + AutoFarmer.crops[i].crop_type.getName().getString(), + crops_enabled[i].value); + } + } + } + } + + public static class FlyHackOptions { + public BooleanOption enabled = new BooleanOption(false); + } +} diff --git a/src/main/java/cmods/haxxor/client/options/IntegerOption.java b/src/main/java/cmods/haxxor/client/options/IntegerOption.java new file mode 100644 index 0000000..5e28cc2 --- /dev/null +++ b/src/main/java/cmods/haxxor/client/options/IntegerOption.java @@ -0,0 +1,23 @@ +package cmods.haxxor.client.options; + +public class IntegerOption extends Option { + IntegerOption(Integer default_value) { + super(default_value); + } + + public Integer inc() { + return inc(1); + } + + public Integer inc(int amount) { + return value += amount; + } + + public Integer dec() { + return dec(1); + } + + public Integer dec(int amount) { + return value -= amount; + } +} diff --git a/src/main/java/cmods/haxxor/client/options/Option.java b/src/main/java/cmods/haxxor/client/options/Option.java new file mode 100644 index 0000000..a5197b3 --- /dev/null +++ b/src/main/java/cmods/haxxor/client/options/Option.java @@ -0,0 +1,17 @@ +package cmods.haxxor.client.options; + +public class Option { + T value; + + Option(T default_value) { + value = default_value; + } + + public T get() { + return value; + } + + public void set(T new_value) { + value = new_value; + } +} diff --git a/src/main/java/cmods/haxxor/client/ui/CropSelectScreen.java b/src/main/java/cmods/haxxor/client/ui/CropSelectScreen.java index 9702f18..82cb831 100644 --- a/src/main/java/cmods/haxxor/client/ui/CropSelectScreen.java +++ b/src/main/java/cmods/haxxor/client/ui/CropSelectScreen.java @@ -1,9 +1,12 @@ package cmods.haxxor.client.ui; import cmods.haxxor.client.AutoFarmer; -import cmods.haxxor.client.HaxxorOptions; +import cmods.haxxor.client.options.HaxxorOptions; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.EmptyWidget; +import net.minecraft.client.gui.widget.GridWidget; +import net.minecraft.client.gui.widget.SimplePositioningWidget; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.screen.ScreenTexts; import net.minecraft.text.Text; @@ -26,36 +29,34 @@ public class CropSelectScreen extends Screen { if (client == null) return; - final int column1 = this.width / 2 + column1_offset; - final int column2 = this.width / 2 + column2_offset; final int startHeight = (int) Math.floor(this.height * startHeight_multiplier) + heightOffset; - final int doneButtonX = this.width / 2 + doneButtonX_offset; - int y = startHeight; + GridWidget grid = new GridWidget(); + grid.getMainPositioner().marginX(5).marginBottom(4).alignHorizontalCenter(); + GridWidget.Adder adder = grid.createAdder(2); for (int i = 0; i < AutoFarmer.crops.length; i++) { - int finalI = i; - if (AutoFarmer.crops[i].seed_type != null) { - addDrawableChild(new ToggleEnableButtonWidget(column1, y, buttonWidth, buttonHeight, - AutoFarmer.crops[i].seed_type.getName(), options.autoFarmer.seeds_enabled[i], - (button, enable) -> options.autoFarmer.seeds_enabled[finalI] = enable)); + adder.add(new ToggleButton(0, 0, buttonWidth, buttonHeight, + AutoFarmer.crops[i].seed_type.getName(), options.autoFarmer.seeds_enabled[i])); + } else { + adder.add(new EmptyWidget(0, 0)); } if (AutoFarmer.crops[i].crop_type != null) { - addDrawableChild(new ToggleEnableButtonWidget(column2, y, buttonWidth, buttonHeight, - AutoFarmer.crops[i].crop_type.getName(), options.autoFarmer.crops_enabled[i], - (button, enable) -> options.autoFarmer.crops_enabled[finalI] = enable)); + adder.add(new ToggleButton(0, 0, buttonWidth, buttonHeight, + AutoFarmer.crops[i].crop_type.getName(), options.autoFarmer.crops_enabled[i])); + } else { + adder.add(new EmptyWidget(0, 0)); } - - y += rowIncrement; } - addDrawableChild(new ButtonWidget(doneButtonX, y + doneButtonRowIncrement, doneButtonWidth, buttonHeight, - ScreenTexts.DONE, button -> { - options.save(); - client.setScreen(parent); - })); + adder.add(ButtonWidget.builder(ScreenTexts.DONE, button -> { options.save(); client.setScreen(parent); }) + .width(doneButtonWidth).build(), 2, adder.copyPositioner().marginTop(doneButtonRowIncrement)); + + grid.recalculateDimensions(); + SimplePositioningWidget.setPos(grid, 0, startHeight, this.width, this.height, 0.5f, 0.0f); + addDrawableChild(grid); } public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { diff --git a/src/main/java/cmods/haxxor/client/ui/CycleButtonWidget.java b/src/main/java/cmods/haxxor/client/ui/CycleButtonWidget.java index d75451b..307da38 100644 --- a/src/main/java/cmods/haxxor/client/ui/CycleButtonWidget.java +++ b/src/main/java/cmods/haxxor/client/ui/CycleButtonWidget.java @@ -10,11 +10,12 @@ public class CycleButtonWidget extends ButtonWidget { public CycleButtonWidget(int x, int y, int width, int height, Text[] options, int startIndex, CycleAction onCycle) { - this(x, y, width, height, options, startIndex, onCycle, EMPTY); + this(x, y, width, height, options, startIndex, onCycle, ButtonWidget.DEFAULT_NARRATION_SUPPLIER); } - public CycleButtonWidget(int x, int y, int width, int height, Text[] options, int startIndex, CycleAction onCycle, TooltipSupplier tooltipSupplier) { - super(x, y, width, height, options[startIndex], onCycle, tooltipSupplier); + public CycleButtonWidget(int x, int y, int width, int height, Text[] options, int startIndex, CycleAction onCycle, + NarrationSupplier narrationSupplier) { + super(x, y, width, height, options[startIndex], onCycle, narrationSupplier); this.options = options; this.onCycle = onCycle; this.selectedIndex = startIndex; diff --git a/src/main/java/cmods/haxxor/client/ui/FarmerOptionsScreen.java b/src/main/java/cmods/haxxor/client/ui/FarmerOptionsScreen.java index 76bb8bb..36b6734 100644 --- a/src/main/java/cmods/haxxor/client/ui/FarmerOptionsScreen.java +++ b/src/main/java/cmods/haxxor/client/ui/FarmerOptionsScreen.java @@ -1,15 +1,15 @@ package cmods.haxxor.client.ui; import cmods.haxxor.client.HaxxorClient; -import cmods.haxxor.client.HaxxorOptions; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; +import cmods.haxxor.client.options.HaxxorOptions; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.EmptyWidget; +import net.minecraft.client.gui.widget.GridWidget; +import net.minecraft.client.gui.widget.SimplePositioningWidget; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.screen.ScreenTexts; import net.minecraft.text.Text; -import org.jetbrains.annotations.NotNull; import static cmods.haxxor.client.ui.Constants.*; @@ -32,69 +32,42 @@ public class FarmerOptionsScreen extends Screen { final int column1 = this.width / 2 + column1_offset; final int column2 = this.width / 2 + column2_offset; final int startHeight = (int) Math.floor(this.height * startHeight_multiplier); - final int doneButtonX = this.width / 2 + doneButtonX_offset; - int row = 0; + GridWidget grid = new GridWidget(); + grid.getMainPositioner().marginX(5).marginBottom(4).alignHorizontalCenter(); + GridWidget.Adder adder = grid.createAdder(2); - addDrawableChild(new ButtonWidget(column1, startHeight, buttonWidth, buttonHeight, - options.autoFarmer.enabled ? enabledText : disabledText, - button -> button.setMessage(HaxxorClient.toggleAutoFarmer() ? enabledText : disabledText))); + adder.add(ButtonWidget.builder(options.autoFarmer.enabled.get() ? enabledText : disabledText, + button -> button.setMessage(HaxxorClient.toggleAutoFarmer() ? enabledText : disabledText)) + .position(column1, startHeight).build()); - addDrawableChild(new ButtonWidget(column2, startHeight, buttonWidth, buttonHeight, - Text.translatable("haxxor.options.farmer.edit_actions"), - button -> client.setScreen(new CropSelectScreen(this)))); + adder.add(ButtonWidget.builder(Text.translatable("haxxor.options.farmer.edit_actions"), + button -> client.setScreen(new CropSelectScreen(this))) + .position(column2, startHeight).build()); - row++; + adder.add(new ToggleButton(0, 0, buttonWidth, + buttonHeight, Text.literal("Move Seeds"), options.autoFarmer.move_seeds)); - addDrawableChild(new ToggleEnableButtonWidget(column1, startHeight + (rowIncrement * row), buttonWidth, - buttonHeight, Text.literal("Move Seeds"), options.autoFarmer.move_seeds, - (button, isEnabled) -> options.autoFarmer.move_seeds = isEnabled)); + adder.add(new EmptyWidget(0, 0)); - row++; + adder.add(new IntegerAdjustWidget(Text.translatable("haxxor.options.farmer.min_y"), + options.autoFarmer.min_y)); - addVariableAdjust(column1, startHeight + (rowIncrement * row), buttonWidth, buttonHeight, options.autoFarmer.min_y, - Text.translatable("haxxor.options.farmer.min_y"), - () -> --options.autoFarmer.min_y, - () -> ++options.autoFarmer.min_y); + adder.add(new IntegerAdjustWidget(Text.translatable("haxxor.options.farmer.max_y"), + options.autoFarmer.max_y)); - addVariableAdjust(column2, startHeight + (rowIncrement * row), buttonWidth, buttonHeight, options.autoFarmer.max_y, - Text.translatable("haxxor.options.farmer.max_y"), - () -> --options.autoFarmer.max_y, - () -> ++options.autoFarmer.max_y); + adder.add(new IntegerAdjustWidget(Text.translatable("haxxor.options.farmer.reach"), + options.autoFarmer.horizontal_reach)); - row++; + adder.add(new IntegerAdjustWidget(Text.translatable("haxxor.options.farmer.actions_per_tick"), + options.autoFarmer.max_actions_per_tick, 5)); - addVariableAdjust(column1, startHeight + (rowIncrement * row), buttonWidth, buttonHeight, options.autoFarmer.horizontal_reach, - Text.translatable("haxxor.options.farmer.reach"), - () -> --options.autoFarmer.horizontal_reach, - () -> ++options.autoFarmer.horizontal_reach); + adder.add(ButtonWidget.builder(ScreenTexts.DONE, button -> client.setScreen(parent)) + .width(doneButtonWidth).build(), 2, adder.copyPositioner().marginTop(doneButtonRowIncrement)); - addVariableAdjust(column2, startHeight + (rowIncrement * row), buttonWidth, buttonHeight, options.autoFarmer.max_actions_per_tick, - Text.translatable("haxxor.options.farmer.actions_per_tick"), - () -> options.autoFarmer.max_actions_per_tick -= 5, - () -> options.autoFarmer.max_actions_per_tick += 5); - - row++; - - this.addDrawableChild(new ButtonWidget(doneButtonX, - startHeight + (rowIncrement * row) + doneButtonRowIncrement, doneButtonWidth, buttonHeight, - ScreenTexts.DONE, button -> client.setScreen(parent))); - } - - @SuppressWarnings("SameParameterValue") - private void addVariableAdjust(int x, int y, int width, int height, int initialValue, @NotNull Text label, - UpdateTextAction minus, UpdateTextAction add) { - final int button_width = height + height / 4; - final String labelStr = label.getString() + ": "; - - ButtonWidget middle = new ButtonWidget(x + button_width - 1, y, width - (button_width * 2) + 2, height, - Text.literal(labelStr + initialValue), button -> {}); - this.addDrawableChild(middle); - - this.addDrawableChild(new ButtonWidget(x, y, button_width, height, Text.literal("-"), - button -> middle.setMessage(Text.literal(labelStr + minus.onUpdate())))); - this.addDrawableChild(new ButtonWidget(x + width - button_width + 1, y, button_width, height, Text.literal("+"), - button -> middle.setMessage(Text.literal(labelStr + add.onUpdate())))); + grid.recalculateDimensions(); + SimplePositioningWidget.setPos(grid, 0, startHeight, this.width, this.height, 0.5f, 0.0f); + addDrawableChild(grid); } public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { @@ -106,10 +79,4 @@ public class FarmerOptionsScreen extends Screen { public void removed() { options.save(); } - - - @Environment(EnvType.CLIENT) - private interface UpdateTextAction { - int onUpdate(); - } } diff --git a/src/main/java/cmods/haxxor/client/ui/HaxxorOptionsScreen.java b/src/main/java/cmods/haxxor/client/ui/HaxxorOptionsScreen.java index b3a11a9..2ac2792 100644 --- a/src/main/java/cmods/haxxor/client/ui/HaxxorOptionsScreen.java +++ b/src/main/java/cmods/haxxor/client/ui/HaxxorOptionsScreen.java @@ -1,9 +1,11 @@ package cmods.haxxor.client.ui; import cmods.haxxor.client.HaxxorClient; -import cmods.haxxor.client.HaxxorOptions; +import cmods.haxxor.client.options.HaxxorOptions; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.GridWidget; +import net.minecraft.client.gui.widget.SimplePositioningWidget; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.screen.ScreenTexts; import net.minecraft.text.Text; @@ -23,21 +25,24 @@ public class HaxxorOptionsScreen extends Screen { if (client == null) return; - final int column1 = this.width / 2 + column1_offset; - final int column2 = this.width / 2 + column2_offset; final int startHeight = (int) Math.floor(this.height * startHeight_multiplier); - final int doneButtonX = this.width / 2 + doneButtonX_offset; - this.addDrawableChild(new ButtonWidget(column1, startHeight, buttonWidth, buttonHeight, - Text.translatable("haxxor.options.farmer"), - button -> this.client.setScreen(new FarmerOptionsScreen(this)))); + GridWidget grid = new GridWidget(); + grid.getMainPositioner().marginX(5).marginBottom(4).alignHorizontalCenter(); + GridWidget.Adder adder = grid.createAdder(2); - this.addDrawableChild(new ToggleEnableButtonWidget(column2, startHeight, buttonWidth, buttonHeight, - Text.translatable("haxxor.options.fall_damage"), options.cancel_fall_damage, - (button, isEnabled) -> options.cancel_fall_damage = isEnabled)); + adder.add(ButtonWidget.builder(Text.translatable("haxxor.options.farmer"), + button -> this.client.setScreen(new FarmerOptionsScreen(this))).build()); - this.addDrawableChild(new ButtonWidget(doneButtonX, startHeight + rowIncrement + doneButtonRowIncrement, - doneButtonWidth, buttonHeight, ScreenTexts.DONE, button -> this.client.setScreen(this.parent))); + adder.add(new ToggleButton(0, 0, buttonWidth, buttonHeight, + Text.translatable("haxxor.options.fall_damage"), options.cancel_fall_damage)); + + adder.add(ButtonWidget.builder(ScreenTexts.DONE, button -> client.setScreen(parent)) + .width(doneButtonWidth).build(), 2, adder.copyPositioner().marginTop(doneButtonRowIncrement)); + + grid.recalculateDimensions(); + SimplePositioningWidget.setPos(grid, 0, startHeight, this.width, this.height, 0.5f, 0.0f); + addDrawableChild(grid); } public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { diff --git a/src/main/java/cmods/haxxor/client/ui/IntegerAdjustWidget.java b/src/main/java/cmods/haxxor/client/ui/IntegerAdjustWidget.java new file mode 100644 index 0000000..19dd9e1 --- /dev/null +++ b/src/main/java/cmods/haxxor/client/ui/IntegerAdjustWidget.java @@ -0,0 +1,80 @@ +package cmods.haxxor.client.ui; + +import cmods.haxxor.client.options.IntegerOption; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.ClickableWidget; +import net.minecraft.client.gui.widget.WrapperWidget; +import net.minecraft.text.Text; + +import java.util.ArrayList; +import java.util.List; + +public class IntegerAdjustWidget extends WrapperWidget { + private final ArrayList children; + private final IntegerOption option; + private final int delta; + + public IntegerAdjustWidget(Text text, IntegerOption option) { + this(text, option, 1); + } + + public IntegerAdjustWidget(Text text, IntegerOption option, int delta) { + super(0, 0, 150, 20, text); + this.option = option; + this.delta = delta; + children = new ArrayList<>(3); + + refreshDimensions(); + } + + @Override + protected List wrappedWidgets() { + return children; + } + + @Override + public void setWidth(int width) { + super.setWidth(width); + refreshDimensions(); + } + + @Override + public void setX(int x) { + super.setX(x); + refreshDimensions(); + } + + @Override + public void setY(int y) { + super.setY(y); + refreshDimensions(); + } + + @Override + public void setPos(int x, int y) { + super.setPos(x, y); + refreshDimensions(); + } + + private void refreshDimensions() { + final int button_width = 25; + final String labelStr = getMessage().getString() + ": "; + + children.clear(); + + ButtonWidget middle = ButtonWidget.builder(Text.literal(labelStr + option.get()), button -> {}) + .dimensions(getX() + button_width - 1, getY(),width - (button_width * 2) + 2, height).build(); + + ButtonWidget sub = ButtonWidget.builder(Text.literal("-"), + button -> middle.setMessage(Text.literal(labelStr + option.dec(delta)))) + .dimensions(getX(), getY(), button_width, height).build(); + + ButtonWidget add = ButtonWidget.builder(Text.literal("+"), + button -> middle.setMessage(Text.literal(labelStr + option.inc(delta)))) + .dimensions(getX() + width - button_width + 1, getY(), button_width, height).build(); + + children.add(sub); + children.add(middle); + children.add(add); + } +} diff --git a/src/main/java/cmods/haxxor/client/ui/OptionsSlideWidget.java b/src/main/java/cmods/haxxor/client/ui/OptionsSlideWidget.java new file mode 100644 index 0000000..243ee0b --- /dev/null +++ b/src/main/java/cmods/haxxor/client/ui/OptionsSlideWidget.java @@ -0,0 +1,39 @@ +package cmods.haxxor.client.ui; + +import cmods.haxxor.client.options.IntegerOption; +import net.minecraft.client.gui.widget.SliderWidget; +import net.minecraft.text.Text; + +public class OptionsSlideWidget extends SliderWidget { + private final IntegerOption option; + private final String messagePrefix; + + private final int min; + private final int max; + private final int step; + + public OptionsSlideWidget(int x, int y, int min, int max, Text text, IntegerOption option) { + this(x, y, min, max, 1, text, option); + } + + public OptionsSlideWidget(int x, int y, int min, int max, int step, Text text, IntegerOption option) { + super(x, y, 150, 20, text, ((double) option.get() - min) / (max - min)); + this.min = min; + this.max = max; + this.step = step; + this.option = option; + messagePrefix = text.getString() + ": "; + + updateMessage(); + } + + @Override + protected void updateMessage() { + setMessage(Text.literal(messagePrefix + option.get())); + } + + @Override + protected void applyValue() { + option.set((int) (((value * ((max - min) / step))) + (min / step)) * step); + } +} diff --git a/src/main/java/cmods/haxxor/client/ui/ToggleButton.java b/src/main/java/cmods/haxxor/client/ui/ToggleButton.java new file mode 100644 index 0000000..51f4553 --- /dev/null +++ b/src/main/java/cmods/haxxor/client/ui/ToggleButton.java @@ -0,0 +1,42 @@ +package cmods.haxxor.client.ui; + +import cmods.haxxor.client.options.BooleanOption; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; + +public class ToggleButton extends ButtonWidget { + private final String name; + private final BooleanOption option; + + private final String enabledText = Text.translatable("haxxor.state.enabled").getString(); + private final String disabledText = Text.translatable("haxxor.state.disabled").getString(); + + + public ToggleButton(int x, int y, int width, int height, Text name, BooleanOption option) { + this(x, y, width, height, name, option, ButtonWidget.DEFAULT_NARRATION_SUPPLIER); + } + + public ToggleButton(int x, int y, int width, int height, Text name, BooleanOption option, + NarrationSupplier narrationSupplier) { + super(x, y, width, height, name, b -> {}, narrationSupplier); + this.name = name.getString() + ": "; + this.option = option; + } + + private void updateText() { + setMessage(Text.literal(name + (option.get() ? enabledText : disabledText))); + } + + + @Override + public void onPress() { + option.toggle(); + } + + @Override + public void renderButton(MatrixStack matrices, int mouseX, int mouseY, float delta) { + updateText(); + super.renderButton(matrices, mouseX, mouseY, delta); + } +} diff --git a/src/main/java/cmods/haxxor/client/ui/ToggleEnableButtonWidget.java b/src/main/java/cmods/haxxor/client/ui/ToggleEnableButtonWidget.java deleted file mode 100644 index fa223cc..0000000 --- a/src/main/java/cmods/haxxor/client/ui/ToggleEnableButtonWidget.java +++ /dev/null @@ -1,51 +0,0 @@ -package cmods.haxxor.client.ui; - -import net.minecraft.client.gui.widget.ButtonWidget; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.Text; - -public class ToggleEnableButtonWidget extends ButtonWidget { - private final String name; - private boolean enabled; - private final ToggleAction onToggle; - - private final String enabledText = Text.translatable("haxxor.state.enabled").getString(); - private final String disabledText = Text.translatable("haxxor.state.disabled").getString(); - - - public ToggleEnableButtonWidget(int x, int y, int width, int height, Text name, boolean startEnabled, - ToggleAction onToggle) { - this(x, y, width, height, name, startEnabled, onToggle, EMPTY); - } - - public ToggleEnableButtonWidget(int x, int y, int width, int height, Text name, boolean startEnabled, - ToggleAction onToggle, TooltipSupplier tooltipSupplier) { - super(x, y, width, height, name, onToggle, tooltipSupplier); - this.name = name.getString() + ": "; - this.enabled = startEnabled; - this.onToggle = onToggle; - } - - private void updateText() { - setMessage(Text.literal(name + (enabled ? enabledText : disabledText))); - } - - - @Override - public void onPress() { - onToggle.onToggle(this, enabled = !enabled); - } - - @Override - public void renderButton(MatrixStack matrices, int mouseX, int mouseY, float delta) { - updateText(); - super.renderButton(matrices, mouseX, mouseY, delta); - } - - public interface ToggleAction extends PressAction { - void onToggle(ButtonWidget button, boolean isEnabled); - - @Override - default void onPress(ButtonWidget button) { } - } -} diff --git a/src/main/java/cmods/haxxor/mixin/HudMixin.java b/src/main/java/cmods/haxxor/mixin/HudMixin.java index a42f1cb..bed0ab9 100644 --- a/src/main/java/cmods/haxxor/mixin/HudMixin.java +++ b/src/main/java/cmods/haxxor/mixin/HudMixin.java @@ -1,7 +1,7 @@ package cmods.haxxor.mixin; import cmods.haxxor.client.AutoFarmer; -import cmods.haxxor.client.HaxxorOptions; +import cmods.haxxor.client.options.HaxxorOptions; import cmods.haxxor.client.ui.Line; import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; @@ -41,7 +41,7 @@ public abstract class HudMixin extends DrawableHelper { int green = 0x00ff00; - if (options.autoFarmer.enabled) { + if (options.autoFarmer.enabled.get()) { lines.add(new Line(Text.translatable("haxxor.hud.auto_farm_enabled") .append(Text.translatable("haxxor.state.on")), green, 0)); } else { @@ -53,7 +53,7 @@ public abstract class HudMixin extends DrawableHelper { lines.add(new Line(Text.translatable("haxxor.actions.available"), green, 1)); } - if (options.flyHack.enabled) { + if (options.flyHack.enabled.get()) { lines.add(new Line(Text.translatable("haxxor.hud.fly_hack_enabled") .append(Text.translatable("haxxor.state.on")), green, 0)); } else { diff --git a/src/main/java/cmods/haxxor/mixin/OptionsMixin.java b/src/main/java/cmods/haxxor/mixin/OptionsMixin.java index a563f8a..da9fdd6 100644 --- a/src/main/java/cmods/haxxor/mixin/OptionsMixin.java +++ b/src/main/java/cmods/haxxor/mixin/OptionsMixin.java @@ -22,8 +22,8 @@ public class OptionsMixin extends Screen { if (client == null) return; - this.addDrawableChild(new ButtonWidget(20, 20, 100, 20, - Text.translatable("haxxor.options"), (button) -> - client.setScreen(new HaxxorOptionsScreen(this)))); + addDrawableChild(ButtonWidget.builder(Text.translatable("haxxor.options"), + button -> client.setScreen(new HaxxorOptionsScreen(this))) + .position(20, 20).width(100).build()); } } diff --git a/src/main/java/cmods/haxxor/mixin/PauseMixin.java b/src/main/java/cmods/haxxor/mixin/PauseMixin.java index 0831c2e..acb29b1 100644 --- a/src/main/java/cmods/haxxor/mixin/PauseMixin.java +++ b/src/main/java/cmods/haxxor/mixin/PauseMixin.java @@ -23,8 +23,8 @@ public class PauseMixin extends Screen { if (client == null) return; - this.addDrawableChild(new ButtonWidget(20, 20, 100, 20, - Text.translatable("haxxor.options"), (button) -> - client.setScreen(new HaxxorOptionsScreen(this)))); + addDrawableChild(ButtonWidget.builder(Text.translatable("haxxor.options"), + button -> client.setScreen(new HaxxorOptionsScreen(this))) + .position(20, 20).width(100).build()); } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index c8d7a3b..d749523 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -14,17 +14,15 @@ "icon": "assets/haxxor/icon.png", "environment": "client", "entrypoints": { - "client": [ - "cmods.haxxor.client.HaxxorClient" - ], + "client": ["cmods.haxxor.client.HaxxorClient"], "modmenu": ["cmods.haxxor.client.ModMenuConfig"] }, "mixins": [ "haxxor.mixins.json" ], "depends": { - "fabricloader": ">=0.14.10", + "fabricloader": ">=0.14.11", "fabric": "*", - "minecraft": "1.19.2" + "minecraft": "1.19.3" } }