Skip to content

Wardrobe Helper Options, and Optional third argument for config commands. #1180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.hysky.skyblocker.config;

import com.google.gson.FieldNamingPolicy;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import de.hysky.skyblocker.SkyblockerMod;
import de.hysky.skyblocker.config.categories.*;
Expand All @@ -20,13 +21,19 @@
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.command.CommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import javax.annotation.Nullable;
import java.lang.StackWalker.Option;
import java.nio.file.Path;
import java.util.stream.Stream;

import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;

public class SkyblockerConfigManager {

public static final int CONFIG_VERSION = 3;
private static final Path CONFIG_FILE = FabricLoader.getInstance().getConfigDir().resolve("skyblocker.json");
private static final ConfigClassHandler<SkyblockerConfig> HANDLER = ConfigClassHandler.createBuilder(SkyblockerConfig.class)
Expand All @@ -53,7 +60,12 @@ public static void init() {
}

HANDLER.load();
ClientCommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess) -> dispatcher.register(ClientCommandManager.literal(SkyblockerMod.NAMESPACE).then(optionsLiteral("config")).then(optionsLiteral("options")))));
ClientCommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess) -> dispatcher
.register(ClientCommandManager
.literal(SkyblockerMod.NAMESPACE)
.then(optionsLiteral("config"))
.then(optionsLiteral("options")))));

ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
if (screen instanceof GenericContainerScreen genericContainerScreen && screen.getTitle().getString().equals("SkyBlock Menu")) {
Screens.getButtons(screen).add(ButtonWidget
Expand All @@ -70,6 +82,11 @@ public static void save() {
}

public static Screen createGUI(Screen parent) {
return createGUI(parent, null);
}

// Overloaded method allows for specifying a category to open to
public static Screen createGUI(Screen parent, @Nullable String category) {
return YetAnotherConfigLib.create(HANDLER, (defaults, config, builder) -> {
builder.title(Text.translatable("skyblocker.config.title"))
.category(GeneralCategory.create(defaults, config))
Expand All @@ -89,10 +106,25 @@ public static Screen createGUI(Screen parent) {
if (Debug.debugEnabled()) {
builder.category(DebugCategory.create(defaults, config));
}

// Default method will skip the initial screen to avoid redundant checks
if (category != null) setInitialConfigScreen(builder, category);

return builder;

}).generateScreen(parent);
}


public static void setInitialConfigScreen(YetAnotherConfigLib.Builder builder, String category) {
builder.screenInit(screen ->
screen.tabManager.setCurrentTab(screen.tabNavigationBar.getTabs().stream().filter(tab ->
tab.getTitle().getString().toLowerCase().matches(category.toLowerCase() + ".*")

).findFirst().orElse(screen.tabNavigationBar.getTabs().getFirst()), false)
);
}

/**
* Registers an options command with the given name. Used for registering both options and config as valid commands.
*
Expand All @@ -101,6 +133,13 @@ public static Screen createGUI(Screen parent) {
*/
private static LiteralArgumentBuilder<FabricClientCommandSource> optionsLiteral(String name) {
// Don't immediately open the next screen as it will be closed by ChatScreen right after this command is executed
return ClientCommandManager.literal(name).executes(Scheduler.queueOpenScreenCommand(() -> createGUI(null)));
return ClientCommandManager.literal(name).executes(Scheduler.queueOpenScreenCommand(() -> createGUI(null)))
// If a category is specified, open to that category using overloaded createGUI method
.then(argument("category", StringArgumentType.word())
.suggests((ctx, builder) ->
CommandSource.suggestMatching(Stream.of("general", "ui", "helper", "dungeons", "crimson", "mining", "farming", "other", "slayers", "chat", "quick", "event", "misc"), builder))
.executes(ctx ->
Scheduler.queueOpenScreenCommand(() ->
createGUI(null, StringArgumentType.getString(ctx, "category"))).run(ctx)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,27 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createBooleanController)
.build())

// Wardrobe Helper
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.helpers.enableWardrobeHelper"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.helpers.enableWardrobeHelper.@Tooltip")))
.binding(defaults.helpers.enableWardrobeHelper,
() -> config.helpers.enableWardrobeHelper,
newValue -> config.helpers.enableWardrobeHelper = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
// Wardrobe Helper
.group(OptionGroup.createBuilder()
.name(Text.translatable("skyblocker.config.helpers.wardrobe"))
.collapsed(true)
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.helpers.wardrobe.enableWardrobeHelper"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.helpers.wardrobe.enableWardrobeHelper.@Tooltip")))
.binding(defaults.helpers.enableWardrobeHelper,
() -> config.helpers.enableWardrobeHelper,
newValue -> config.helpers.enableWardrobeHelper = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.helpers.wardrobe.showKeybinds"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.helpers.wardrobe.showKeybinds.@Tooltip")))
.binding(defaults.helpers.showKeybinds,
() -> config.helpers.showKeybinds,
newValue -> config.helpers.showKeybinds = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.build())

//Mythological Ritual
.group(OptionGroup.createBuilder()
Expand Down Expand Up @@ -228,38 +240,38 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.build())

.group(OptionGroup.createBuilder()
.name(Text.translatable("skyblocker.config.helpers.carnival"))
.collapsed(true)
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.helpers.carnival.catchAFishHelper"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.helpers.carnival.catchAFishHelper.@Tooltip")))
.binding(defaults.helpers.carnival.catchAFishHelper,
() -> config.helpers.carnival.catchAFishHelper,
newValue -> config.helpers.carnival.catchAFishHelper = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.helpers.carnival.zombieShootoutHelper"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.helpers.carnival.zombieShootoutHelper.@Tooltip")))
.binding(defaults.helpers.carnival.zombieShootoutHelper,
() -> config.helpers.carnival.zombieShootoutHelper,
newValue -> config.helpers.carnival.zombieShootoutHelper = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.build())
.name(Text.translatable("skyblocker.config.helpers.carnival"))
.collapsed(true)
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.helpers.carnival.catchAFishHelper"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.helpers.carnival.catchAFishHelper.@Tooltip")))
.binding(defaults.helpers.carnival.catchAFishHelper,
() -> config.helpers.carnival.catchAFishHelper,
newValue -> config.helpers.carnival.catchAFishHelper = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.helpers.carnival.zombieShootoutHelper"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.helpers.carnival.zombieShootoutHelper.@Tooltip")))
.binding(defaults.helpers.carnival.zombieShootoutHelper,
() -> config.helpers.carnival.zombieShootoutHelper,
newValue -> config.helpers.carnival.zombieShootoutHelper = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.build())

//Bazaar
.group(OptionGroup.createBuilder()
.name(Text.translatable("skyblocker.config.helpers.bazaar"))
.collapsed(true)
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.helpers.bazaar.enableBazaarHelper"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.helpers.bazaar.enableBazaarHelper.@Tooltip", BazaarHelper.getExpiringIcon(), BazaarHelper.getExpiredIcon(), BazaarHelper.getFilledIcon(69), BazaarHelper.getFilledIcon(100))))
.binding(defaults.helpers.bazaar.enableBazaarHelper,
() -> config.helpers.bazaar.enableBazaarHelper,
newValue -> config.helpers.bazaar.enableBazaarHelper = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.name(Text.translatable("skyblocker.config.helpers.bazaar.enableBazaarHelper"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.helpers.bazaar.enableBazaarHelper.@Tooltip", BazaarHelper.getExpiringIcon(), BazaarHelper.getExpiredIcon(), BazaarHelper.getFilledIcon(69), BazaarHelper.getFilledIcon(100))))
.binding(defaults.helpers.bazaar.enableBazaarHelper,
() -> config.helpers.bazaar.enableBazaarHelper,
newValue -> config.helpers.bazaar.enableBazaarHelper = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.helpers.itemPrice.enableItemPriceLookup"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.helpers.itemPrice.enableItemPriceLookup.@Tooltip")))
Expand All @@ -277,7 +289,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createBooleanController)
.build())
.option(ConfigUtils.createShortcutToKeybindsScreen())
.build())
.build())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class HelperConfig {
@SerialEntry
public boolean enableWardrobeHelper = true;

@SerialEntry
public boolean showKeybinds = false;

@SerialEntry
public MythologicalRitual mythologicalRitual = new MythologicalRitual();

Expand Down
102 changes: 56 additions & 46 deletions src/main/java/de/hysky/skyblocker/skyblock/WardrobeKeybinds.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,65 @@
import java.util.function.Predicate;

public class WardrobeKeybinds extends SimpleSlotTextAdder {
public static final WardrobeKeybinds INSTANCE = new WardrobeKeybinds();
public static final WardrobeKeybinds INSTANCE = new WardrobeKeybinds();

public WardrobeKeybinds() {
super("Wardrobe \\([12]/2\\)");
}
private static final MinecraftClient CLIENT = MinecraftClient.getInstance();

@Init
public static void init() {
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
if (!(screen instanceof HandledScreen<?> handledScreen) || !INSTANCE.test(handledScreen) || !INSTANCE.isEnabled() || client.interactionManager == null) return;
ScreenKeyboardEvents.allowKeyPress(handledScreen).register((ignored, keyCode, scanCode, modifiers) ->
allowInput(client, handledScreen, keybinding -> keybinding.matchesKey(keyCode, scanCode))
);
ScreenMouseEvents.allowMouseClick(handledScreen).register((ignored, mouseX, mouseY, button) ->
allowInput(client, handledScreen, keybinding -> keybinding.matchesMouse(button))
);
});
}
public WardrobeKeybinds() {
super("Wardrobe \\([12]/2\\)");
}

private static boolean allowInput(MinecraftClient client, HandledScreen<?> handledScreen, Predicate<KeyBinding> predicate) {
boolean found = false;
int i;
for (i = 0; i < client.options.hotbarKeys.length; i++) {
if (predicate.test(client.options.hotbarKeys[i])) {
found = true;
break;
}
}
if (!found) return true;
// The items start from the 5th row in the inventory. The i number we have is the column in the first row, so we have to offset it by 4 rows to get the 5th row, which is where the items start.
i += 9 * 4;
ItemStack itemStack = handledScreen.getScreenHandler().getSlot(i).getStack();
// Check if the item in the slot is a swap/unequip item before going further.
// This prevents usage when the inventory hasn't loaded fully or when the slot pressed is locked or when the slot has no armor (which would be meaningless to click)
if (!itemStack.isOf(Items.PINK_DYE) && !itemStack.isOf(Items.LIME_DYE)) return true;
assert client.interactionManager != null;
client.interactionManager.clickSlot(handledScreen.getScreenHandler().syncId, i, GLFW.GLFW_MOUSE_BUTTON_1, SlotActionType.PICKUP, client.player);
return false;
}

@Override
public @NotNull List<SlotText> getText(@Nullable Slot slot, @NotNull ItemStack stack, int slotId) {
if (!stack.isOf(Items.PINK_DYE) && !stack.isOf(Items.LIME_DYE)) return List.of();
if (!(slotId >= 36 && slotId <= 44)) return List.of();
return SlotText.bottomLeftList(Text.literal(String.valueOf(slotId - 35)).withColor(0x74c7ec));
}
@Init
public static void init() {
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
if (!(screen instanceof HandledScreen<?> handledScreen) || !INSTANCE.test(handledScreen) || !INSTANCE.isEnabled() || client.interactionManager == null) return;
ScreenKeyboardEvents.allowKeyPress(handledScreen).register((ignored, keyCode, scanCode, modifiers) ->
allowInput(client, handledScreen, keybinding -> keybinding.matchesKey(keyCode, scanCode))
);
ScreenMouseEvents.allowMouseClick(handledScreen).register((ignored, mouseX, mouseY, button) ->
allowInput(client, handledScreen, keybinding -> keybinding.matchesMouse(button))
);
});
}

private static boolean allowInput(MinecraftClient client, HandledScreen<?> handledScreen, Predicate<KeyBinding> predicate) {
boolean found = false;
int i;
for (i = 0; i < client.options.hotbarKeys.length; i++) {
if (predicate.test(client.options.hotbarKeys[i])) {
found = true;
break;
}
}
if (!found) return true;
// The items start from the 5th row in the inventory. The i number we have is the column in the first row, so we have to offset it by 4 rows to get the 5th row, which is where the items start.
i += 9 * 4;
ItemStack itemStack = handledScreen.getScreenHandler().getSlot(i).getStack();
// Check if the item in the slot is a swap/unequip item before going further.
// This prevents usage when the inventory hasn't loaded fully or when the slot pressed is locked or when the slot has no armor (which would be meaningless to click)
if (!itemStack.isOf(Items.PINK_DYE) && !itemStack.isOf(Items.LIME_DYE)) return true;
assert client.interactionManager != null;
client.interactionManager.clickSlot(handledScreen.getScreenHandler().syncId, i, GLFW.GLFW_MOUSE_BUTTON_1, SlotActionType.PICKUP, client.player);
return false;
}

@Override
public @NotNull List<SlotText> getText(@Nullable Slot slot, @NotNull ItemStack stack, int slotId) {
if (!stack.isOf(Items.PINK_DYE) && !stack.isOf(Items.LIME_DYE)) return List.of();
if (!(slotId >= 36 && slotId <= 44)) return List.of();

if (!showKeys()) return SlotText.bottomLeftList(Text.of(String.valueOf(slotId - 35)));
return SlotText.bottomLeftList(Text.of(CLIENT.options.hotbarKeys[slotId - 36].getBoundKeyLocalizedText().getString()));
}

@Override
public boolean isEnabled() {
return SkyblockerConfigManager.get().helpers.enableWardrobeHelper;
}

public boolean showKeys() {
return SkyblockerConfigManager.get().helpers.showKeybinds;
}

@Override
public boolean isEnabled() {
return SkyblockerConfigManager.get().helpers.enableWardrobeHelper;
}
}
Loading