diff --git a/src/main/java/me/zeroeightsix/kami/command/commands/SetCommand.java b/src/main/java/me/zeroeightsix/kami/command/commands/SetCommand.java index 8ab346c2b..9c8e9747b 100644 --- a/src/main/java/me/zeroeightsix/kami/command/commands/SetCommand.java +++ b/src/main/java/me/zeroeightsix/kami/command/commands/SetCommand.java @@ -44,7 +44,7 @@ public class SetCommand extends Command { } if (args[1] == null) { - String settings = m.getSettingList().stream().map(Setting::getName).collect(Collectors.joining(", ")); + String settings = m.getFullSettingList().stream().map(Setting::getName).collect(Collectors.joining(", ")); if (settings.isEmpty()) sendChatMessage("Module &b" + m.getName().getValue() + "&r has no settings."); else { @@ -55,7 +55,7 @@ public class SetCommand extends Command { return; } - Optional> optionalSetting = m.getSettingList().stream().filter(setting1 -> setting1.getName().equalsIgnoreCase(args[1])).findFirst(); + Optional> optionalSetting = m.getFullSettingList().stream().filter(setting1 -> setting1.getName().equalsIgnoreCase(args[1])).findFirst(); if (!optionalSetting.isPresent()) { sendChatMessage("Unknown setting &b" + args[1] + "&r in &b" + m.getName().getValue() + "&r!"); return; diff --git a/src/main/java/me/zeroeightsix/kami/command/commands/SettingsCommand.java b/src/main/java/me/zeroeightsix/kami/command/commands/SettingsCommand.java index c45a8090b..ff24be5e1 100644 --- a/src/main/java/me/zeroeightsix/kami/command/commands/SettingsCommand.java +++ b/src/main/java/me/zeroeightsix/kami/command/commands/SettingsCommand.java @@ -33,7 +33,7 @@ public class SettingsCommand extends Command { try { Module m = ModuleManager.getModule(args[0]); - List> settings = m.getSettingList(); + List> settings = m.getFullSettingList(); String[] result = new String[settings.size()]; for (int i = 0; i < settings.size(); i++) { Setting setting = settings.get(i); diff --git a/src/main/java/me/zeroeightsix/kami/command/syntax/parsers/ValueParser.java b/src/main/java/me/zeroeightsix/kami/command/syntax/parsers/ValueParser.java index 3a642afb4..175e69036 100644 --- a/src/main/java/me/zeroeightsix/kami/command/syntax/parsers/ValueParser.java +++ b/src/main/java/me/zeroeightsix/kami/command/syntax/parsers/ValueParser.java @@ -23,7 +23,7 @@ public class ValueParser extends AbstractParser { Module m = ModuleManager.getModule(module); HashMap> possibilities = new HashMap<>(); - for (Setting v : m.getSettingList()) { + for (Setting v : m.getFullSettingList()) { if (v.getName().toLowerCase().startsWith(chunkValue.toLowerCase())) possibilities.put(v.getName(), v); } diff --git a/src/main/java/me/zeroeightsix/kami/gui/kami/component/SettingsPanel.java b/src/main/java/me/zeroeightsix/kami/gui/kami/component/SettingsPanel.java index 013ea82e3..897863a05 100644 --- a/src/main/java/me/zeroeightsix/kami/gui/kami/component/SettingsPanel.java +++ b/src/main/java/me/zeroeightsix/kami/gui/kami/component/SettingsPanel.java @@ -47,8 +47,8 @@ public class SettingsPanel extends OrganisedContainer { setVisible(false); return; } - if (!module.getSettingList().isEmpty()) { - for (Setting setting : module.getSettingList()) { + if (!module.getFullSettingList().isEmpty()) { + for (Setting setting : module.getFullSettingList()) { if (!setting.isVisible()) continue; String name = setting.getName(); boolean isNumber = setting instanceof NumberSetting; diff --git a/src/main/java/me/zeroeightsix/kami/module/Module.kt b/src/main/java/me/zeroeightsix/kami/module/Module.kt index 6070995f0..f641b651f 100644 --- a/src/main/java/me/zeroeightsix/kami/module/Module.kt +++ b/src/main/java/me/zeroeightsix/kami/module/Module.kt @@ -64,11 +64,16 @@ open class Module { /* End of annotations */ /* Settings */ - val settingList = ArrayList>() + val fullSettingList = ArrayList>() + val settingList: List> by lazy { fullSettingList.filter { + it != name && it != bind && it != enabled && it != showOnArray && it != default + } } + val name = register(Settings.s("Name", originalName)) - val bind = register(Settings.custom("Bind", Bind.none(), BindConverter()).build()) - private val enabled = register(Settings.booleanBuilder("Enabled").withVisibility { false }.withValue(annotation.enabledByDefault || annotation.alwaysEnabled).build()) + val bind = register(Settings.custom("Bind", Bind.none(), BindConverter())) + private val enabled = register(Settings.booleanBuilder("Enabled").withVisibility { false }.withValue(annotation.enabledByDefault || annotation.alwaysEnabled)) private val showOnArray = register(Settings.e("Visible", annotation.showOnArray)) + private val default = Settings.booleanBuilder("Default").withValue(false).withVisibility { settingList.isNotEmpty() }.build().also { fullSettingList.add(it) } /* End of settings */ /* Properties */ @@ -137,13 +142,13 @@ open class Module { /* Setting registering */ protected fun register(setting: Setting): Setting { - settingList.add(setting) + fullSettingList.add(setting) return SettingBuilder.register(setting, "modules.$originalName") } protected fun register(builder: SettingBuilder): Setting { val setting = builder.build() - settingList.add(setting) + fullSettingList.add(setting) return SettingBuilder.register(setting, "modules.$originalName") } /* End of setting registering */ @@ -182,6 +187,16 @@ open class Module { } /* End of key binding */ + init { + default.settingListener = Setting.SettingListeners { + if (default.value) { + settingList.forEach { it.resetValue() } + default.value = false + MessageSendHelper.sendChatMessage("$chatName Set to defaults!") + } + } + } + protected companion object { @JvmField val mc: Minecraft = Minecraft.getMinecraft() } diff --git a/src/main/java/me/zeroeightsix/kami/module/modules/misc/AutoFish.kt b/src/main/java/me/zeroeightsix/kami/module/modules/misc/AutoFish.kt index 5fce1d3fe..393f1fd03 100644 --- a/src/main/java/me/zeroeightsix/kami/module/modules/misc/AutoFish.kt +++ b/src/main/java/me/zeroeightsix/kami/module/modules/misc/AutoFish.kt @@ -4,12 +4,10 @@ import me.zeroeightsix.kami.event.events.PacketEvent import me.zeroeightsix.kami.event.events.SafeTickEvent import me.zeroeightsix.kami.mixin.extension.rightClickMouse import me.zeroeightsix.kami.module.Module -import me.zeroeightsix.kami.setting.Setting.SettingListeners import me.zeroeightsix.kami.setting.Settings import me.zeroeightsix.kami.util.BlockUtils.isWater import me.zeroeightsix.kami.util.TimerUtils.TickTimer import me.zeroeightsix.kami.util.event.listener -import me.zeroeightsix.kami.util.text.MessageSendHelper import net.minecraft.init.Items import net.minecraft.network.play.server.SPacketSoundEffect import java.lang.Math.random @@ -28,7 +26,6 @@ import kotlin.math.abs ) object AutoFish : Module() { private val mode = register(Settings.e("Mode", Mode.BOUNCE)) - private val defaultSetting = register(Settings.b("Defaults", false)) private val autoCast = register(Settings.b("AutoCast", true)) private val castDelay = register(Settings.integerBuilder("AutoCastDelay(s)").withValue(5).withRange(1, 20).withVisibility { autoCast.value }) private val catchDelay = register(Settings.integerBuilder("CatchDelay(ms)").withValue(300).withRange(50, 2000)) @@ -143,18 +140,4 @@ object AutoFish : Module() { val offset = if (variation.value > 0) (random() * (variation.value * 2) - variation.value).toLong() else 0 timer.reset(offset) } - - private fun defaults() { - autoCast.value = true - castDelay.value = 5 - catchDelay.value = 300 - recastDelay.value = 450 - variation.value = 100 - defaultSetting.value = false - MessageSendHelper.sendChatMessage("$chatName Set to defaults!") - } - - init { - defaultSetting.settingListener = SettingListeners { if (defaultSetting.value) defaults() } - } } \ No newline at end of file diff --git a/src/main/java/me/zeroeightsix/kami/module/modules/movement/ElytraFlight.kt b/src/main/java/me/zeroeightsix/kami/module/modules/movement/ElytraFlight.kt index 376ab6e45..c6b290e68 100644 --- a/src/main/java/me/zeroeightsix/kami/module/modules/movement/ElytraFlight.kt +++ b/src/main/java/me/zeroeightsix/kami/module/modules/movement/ElytraFlight.kt @@ -34,7 +34,6 @@ import kotlin.math.* object ElytraFlight : Module() { private val mode = register(Settings.enumBuilder(ElytraFlightMode::class.java).withName("Mode").withValue(ElytraFlightMode.CONTROL)) private val page = register(Settings.e("Page", Page.GENERIC_SETTINGS)) - private val defaultSetting = register(Settings.b("Defaults", false)) private val durabilityWarning = register(Settings.booleanBuilder("DurabilityWarning").withValue(true).withVisibility { page.value == Page.GENERIC_SETTINGS }) private val threshold = register(Settings.integerBuilder("Broken%").withValue(5).withRange(1, 50).withStep(1).withVisibility { durabilityWarning.value && page.value == Page.GENERIC_SETTINGS }) private val autoLanding = register(Settings.booleanBuilder("AutoLanding").withValue(false).withVisibility { page.value == Page.GENERIC_SETTINGS }) @@ -502,62 +501,7 @@ object ElytraFlight : Module() { hoverTarget = -1.0 /* For control mode */ } - private fun defaults() { - mc.player?.let { - durabilityWarning.value = true - threshold.value = 5 - autoLanding.value = false - - easyTakeOff.value = true - timerControl.value = true - highPingOptimize.value = false - minTakeoffHeight.value = 0.5f - - accelerateStartSpeed.value = 100 - accelerateTime.value = 0.0f - autoReset.value = false - - spoofPitch.value = true - blockInteract.value = false - forwardPitch.value = 0 - - elytraSounds.value = true - swingSpeed.value = 1.0f - swingAmount.value = 0.8f - - speedBoost.value = 1.0f - upSpeedBoost.value = 1.0f - downSpeedBoost.value = 1.0f - - boostPitchControl.value = 20 - ncpStrict.value = true - legacyLookBoost.value = false - altitudeHoldControl.value = false - dynamicDownSpeed.value = false - speedControl.value = 1.81f - fallSpeedControl.value = 0.00000000000003f - downSpeedControl.value = 1.0f - fastDownSpeedControl.value = 2.0f - - speedCreative.value = 1.8f - fallSpeedCreative.value = 0.00001f - upSpeedCreative.value = 1.0f - downSpeedCreative.value = 1.0f - - speedPacket.value = 1.8f - fallSpeedPacket.value = 0.00001f - downSpeedPacket.value = 1.0f - - defaultSetting.value = false - sendChatMessage("$chatName Set to defaults!") - } - } - init { - defaultSetting.settingListener = SettingListeners { - if (defaultSetting.value) defaults() - } - /* Reset isFlying states when switching mode */ mode.settingListener = SettingListeners { reset(true) diff --git a/src/main/java/me/zeroeightsix/kami/setting/Setting.java b/src/main/java/me/zeroeightsix/kami/setting/Setting.java index 75f14e09d..d8c7ddd3e 100644 --- a/src/main/java/me/zeroeightsix/kami/setting/Setting.java +++ b/src/main/java/me/zeroeightsix/kami/setting/Setting.java @@ -1,5 +1,6 @@ package me.zeroeightsix.kami.setting; +import com.google.gson.JsonElement; import com.google.gson.JsonParser; import me.zeroeightsix.kami.setting.converter.Convertable; @@ -11,34 +12,26 @@ import java.util.function.Predicate; */ public abstract class Setting implements ISettingUnknown, Convertable { - String name; - - T value; - + private final String name; + private final T defaultValue; + private final Class valueType; /** * Returns false if the value is "out of bounds" */ - private Predicate restriction; - - private Predicate visibilityPredicate; - - private BiConsumer consumer; - - private final Class valueType; - + private final Predicate restriction; + private final Predicate visibilityPredicate; + private final BiConsumer consumer; public SettingListeners settingListener; - - public interface SettingListeners { - public void onSettingChange(final Setting setting); - } + private T value; public Setting(T value, Predicate restriction, BiConsumer consumer, String name, Predicate visibilityPredicate) { + this.name = name; this.value = value; + this.defaultValue = value; this.valueType = value.getClass(); this.restriction = restriction; - this.consumer = consumer; - this.name = name; this.visibilityPredicate = visibilityPredicate; + this.consumer = consumer; } @Override @@ -50,8 +43,12 @@ public abstract class Setting implements ISettingUnknown, Convertable { return value; } + public T getDefaultValue() { + return defaultValue; + } + @Override - public Class getValueClass() { + public Class getValueClass() { return valueType; } @@ -66,10 +63,17 @@ public abstract class Setting implements ISettingUnknown, Convertable { this.value = value; consumer.accept(old, value); if (settingListener != null) - settingListener.onSettingChange(this); + settingListener.onSettingChange(); return true; } + /** + * Reset value to default + */ + public void resetValue() { + this.value = defaultValue; + } + @Override public boolean isVisible() { return visibilityPredicate.test(getValue()); @@ -97,6 +101,15 @@ public abstract class Setting implements ISettingUnknown, Convertable { @Override public String toString() { - return this.converter().convert(getValue()).toString(); + JsonElement converted = this.converter().convert(getValue()); + if (converted != null) { + return converted.toString(); + } else { + return ""; + } + } + + public interface SettingListeners { + void onSettingChange(); } }