From 5bfe95f71d5265efd680dbc3c19c94dc0485e343 Mon Sep 17 00:00:00 2001 From: Yasin Date: Tue, 20 Feb 2024 20:11:43 +0100 Subject: [PATCH 1/3] add keybinding and identifier --- .../skyblocker/mixin/HandledScreenMixin.java | 22 ++++++++++-- .../skyblocker/mixin/InGameHudMixin.java | 12 +++++++ .../mixin/MinecraftClientMixin.java | 2 ++ .../skyblock/item/ItemProtection.java | 34 ++++++++++++++++++ .../assets/skyblocker/lang/en_us.json | 1 + .../textures/gui/item_protection.png | Bin 0 -> 235 bytes 6 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/assets/skyblocker/textures/gui/item_protection.png diff --git a/src/main/java/de/hysky/skyblocker/mixin/HandledScreenMixin.java b/src/main/java/de/hysky/skyblocker/mixin/HandledScreenMixin.java index 8a1af570d4..0c779ebe2a 100644 --- a/src/main/java/de/hysky/skyblocker/mixin/HandledScreenMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixin/HandledScreenMixin.java @@ -1,6 +1,7 @@ package de.hysky.skyblocker.mixin; import com.llamalad7.mixinextras.sugar.Local; +import com.mojang.blaze3d.systems.RenderSystem; import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.skyblock.experiment.ChronomatronSolver; @@ -29,6 +30,7 @@ import net.minecraft.screen.slot.Slot; import net.minecraft.screen.slot.SlotActionType; import net.minecraft.text.Text; +import net.minecraft.util.Identifier; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Final; @@ -52,6 +54,9 @@ public abstract class HandledScreenMixin extends Screen @Unique private static final int OUT_OF_BOUNDS_SLOT = -999; + @Unique + private static final Identifier ITEM_PROTECTION = new Identifier(SkyblockerMod.NAMESPACE, "textures/gui/item_protection.png"); + @Shadow @Nullable protected Slot focusedSlot; @@ -66,8 +71,15 @@ protected HandledScreenMixin(Text title) { @Inject(at = @At("HEAD"), method = "keyPressed") public void skyblocker$keyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable cir) { - if (this.client != null && this.focusedSlot != null && keyCode != 256 && !this.client.options.inventoryKey.matchesKey(keyCode, scanCode) && WikiLookup.wikiLookup.matchesKey(keyCode, scanCode)) { - WikiLookup.openWiki(this.focusedSlot, client.player); + if (this.client != null && this.focusedSlot != null && keyCode != 256) { + //wiki lookup + if (!this.client.options.inventoryKey.matchesKey(keyCode, scanCode) && WikiLookup.wikiLookup.matchesKey(keyCode, scanCode)) { + WikiLookup.openWiki(this.focusedSlot, client.player); + } + //item protection + if (!this.client.options.inventoryKey.matchesKey(keyCode, scanCode) && ItemProtection.itemProtection.matchesKey(keyCode, scanCode)) { + ItemProtection.handleKeyPressed(this.focusedSlot.getStack()); + } } } @@ -207,5 +219,11 @@ protected HandledScreenMixin(Text title) { private void skyblocker$drawItemRarityBackground(DrawContext context, Slot slot, CallbackInfo ci) { if (Utils.isOnSkyblock() && SkyblockerConfigManager.get().general.itemInfoDisplay.itemRarityBackgrounds) ItemRarityBackgrounds.tryDraw(slot.getStack(), context, slot.x, slot.y); + // Item protection + if (ItemProtection.isItemProtected(slot.getStack())){ + RenderSystem.enableBlend(); + context.drawTexture(ITEM_PROTECTION, slot.x, slot.y, 0, 0, 16, 16, 16, 16); + RenderSystem.disableBlend(); + } } } diff --git a/src/main/java/de/hysky/skyblocker/mixin/InGameHudMixin.java b/src/main/java/de/hysky/skyblocker/mixin/InGameHudMixin.java index 88be60cd32..5e3daf3cbe 100644 --- a/src/main/java/de/hysky/skyblocker/mixin/InGameHudMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixin/InGameHudMixin.java @@ -3,6 +3,7 @@ import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import com.llamalad7.mixinextras.sugar.Local; import com.mojang.blaze3d.systems.RenderSystem; +import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.skyblock.FancyStatusBars; import de.hysky.skyblocker.skyblock.dungeon.DungeonMap; @@ -10,6 +11,7 @@ import de.hysky.skyblocker.skyblock.dungeon.DungeonScoreHUD; import de.hysky.skyblocker.skyblock.item.HotbarSlotLock; import de.hysky.skyblocker.skyblock.item.ItemCooldowns; +import de.hysky.skyblocker.skyblock.item.ItemProtection; import de.hysky.skyblocker.skyblock.item.ItemRarityBackgrounds; import de.hysky.skyblocker.utils.Utils; import net.fabricmc.api.EnvType; @@ -38,6 +40,9 @@ public abstract class InGameHudMixin { @Unique private static final Supplier SLOT_LOCK_ICON = () -> SkyblockerConfigManager.get().general.itemProtection.slotLockStyle.tex; + + @Unique + private static final Identifier ITEM_PROTECTION = new Identifier(SkyblockerMod.NAMESPACE, "textures/gui/item_protection.png"); @Unique private static final Pattern DICER_TITLE_BLACKLIST = Pattern.compile(".+? DROP!"); @@ -56,12 +61,19 @@ public abstract class InGameHudMixin { @Inject(method = "renderHotbar", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/InGameHud;renderHotbarItem(Lnet/minecraft/client/gui/DrawContext;IIFLnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/item/ItemStack;I)V", ordinal = 0)) public void skyblocker$renderHotbarItemLockOrRarityBg(float tickDelta, DrawContext context, CallbackInfo ci, @Local(ordinal = 4, name = "m") int index, @Local(ordinal = 5, name = "n") int x, @Local(ordinal = 6, name = "o") int y, @Local PlayerEntity player) { if (Utils.isOnSkyblock()) { + // slot lock if (SkyblockerConfigManager.get().general.itemInfoDisplay.itemRarityBackgrounds) ItemRarityBackgrounds.tryDraw(player.getInventory().main.get(index), context, x, y); if (HotbarSlotLock.isLocked(index)) { RenderSystem.enableBlend(); context.drawTexture(SLOT_LOCK_ICON.get(), x, y, 0, 0, 16, 16, 16, 16); RenderSystem.disableBlend(); } + //item protection + if (ItemProtection.isItemProtected(player.getInventory().main.get(index))){ + RenderSystem.enableBlend(); + context.drawTexture(ITEM_PROTECTION, x, y, 0, 0, 16, 16, 16, 16); + RenderSystem.disableBlend(); + } } } diff --git a/src/main/java/de/hysky/skyblocker/mixin/MinecraftClientMixin.java b/src/main/java/de/hysky/skyblocker/mixin/MinecraftClientMixin.java index cf8aab7838..dd671b2ecf 100644 --- a/src/main/java/de/hysky/skyblocker/mixin/MinecraftClientMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixin/MinecraftClientMixin.java @@ -1,6 +1,7 @@ package de.hysky.skyblocker.mixin; import de.hysky.skyblocker.skyblock.item.HotbarSlotLock; +import de.hysky.skyblocker.skyblock.item.ItemProtection; import de.hysky.skyblocker.utils.JoinWorldPlaceholderScreen; import de.hysky.skyblocker.utils.ReconfiguringPlaceholderScreen; import de.hysky.skyblocker.utils.Utils; @@ -34,6 +35,7 @@ public abstract class MinecraftClientMixin { public void skyblocker$handleInputEvents(CallbackInfo ci) { if (Utils.isOnSkyblock()) { HotbarSlotLock.handleInputEvents(player); + ItemProtection.handleHotbarKeyPressed(player); } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java index 2d929c2838..5fc0fed5bd 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java @@ -10,13 +10,23 @@ import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.client.option.KeyBinding; import net.minecraft.command.CommandRegistryAccess; import net.minecraft.item.ItemStack; import net.minecraft.text.Text; +import org.lwjgl.glfw.GLFW; public class ItemProtection { + public static KeyBinding itemProtection; public static void init() { + itemProtection = KeyBindingHelper.registerKeyBinding(new KeyBinding( + "key.itemProtection", + GLFW.GLFW_KEY_V, + "key.categories.skyblocker" + )); ClientCommandRegistrationCallback.EVENT.register(ItemProtection::registerCommand); } @@ -61,4 +71,28 @@ private static int protectMyItem(FabricClientCommandSource source) { return Command.SINGLE_SUCCESS; } + + public static void handleKeyPressed(ItemStack heldItem) { + if (!Utils.isOnSkyblock() || heldItem.isEmpty()) return; + + String itemUuid = ItemUtils.getItemUuid(heldItem); + if (!itemUuid.isEmpty()) { + ObjectOpenHashSet protectedItems = SkyblockerConfigManager.get().general.protectedItems; + + if (!protectedItems.contains(itemUuid)) { + protectedItems.add(itemUuid); + SkyblockerConfigManager.save(); + } else { + protectedItems.remove(itemUuid); + SkyblockerConfigManager.save(); + } + } + } + + public static void handleHotbarKeyPressed(ClientPlayerEntity player) { + while (itemProtection.wasPressed()) { + ItemStack heldItem = player.getMainHandStack(); + handleKeyPressed(heldItem); + } + } } diff --git a/src/main/resources/assets/skyblocker/lang/en_us.json b/src/main/resources/assets/skyblocker/lang/en_us.json index 60631ef1b6..508accb261 100644 --- a/src/main/resources/assets/skyblocker/lang/en_us.json +++ b/src/main/resources/assets/skyblocker/lang/en_us.json @@ -5,6 +5,7 @@ "key.skyblocker.defaultTgl": "Switch tab HUD to default view", "key.skyblocker.toggleA": "Toggle tab HUD to screen A", "key.wikiLookup": "Wiki Lookup", + "key.itemProtection": "protect Item", "text.skyblocker.open": "Open", "text.skyblocker.quit_config": "Changes Not Saved", diff --git a/src/main/resources/assets/skyblocker/textures/gui/item_protection.png b/src/main/resources/assets/skyblocker/textures/gui/item_protection.png new file mode 100644 index 0000000000000000000000000000000000000000..b76fe034481ab5321bbd13cb80b86f32a99aab6a GIT binary patch literal 235 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE3?yBabR7dyjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCikh0(?STKm1?y;XlKNyFfkzWB&HNAax}{e!>6$|7UQ}TKNUY=PdAuEM{Qf zI}E~%$MaXD00r$lT^vI!{NMIzi!m5*9KKcb-@3oQt@ET{qo*9h?hTF`9X~n>6mdE1 zV`->fet*I&DZeH2>lT=>=_P-0tT2|HQ_8&~s Date: Tue, 20 Feb 2024 22:39:24 +0100 Subject: [PATCH 2/3] capitalized "protect" --- src/main/resources/assets/skyblocker/lang/en_us.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/assets/skyblocker/lang/en_us.json b/src/main/resources/assets/skyblocker/lang/en_us.json index 508accb261..63c8163a33 100644 --- a/src/main/resources/assets/skyblocker/lang/en_us.json +++ b/src/main/resources/assets/skyblocker/lang/en_us.json @@ -5,7 +5,7 @@ "key.skyblocker.defaultTgl": "Switch tab HUD to default view", "key.skyblocker.toggleA": "Toggle tab HUD to screen A", "key.wikiLookup": "Wiki Lookup", - "key.itemProtection": "protect Item", + "key.itemProtection": "Protect Item", "text.skyblocker.open": "Open", "text.skyblocker.quit_config": "Changes Not Saved", From 6885e1affbc71c703cf6195182c7ee4053603683 Mon Sep 17 00:00:00 2001 From: Yasin Date: Tue, 20 Feb 2024 23:24:00 +0100 Subject: [PATCH 3/3] Update ItemProtection.java --- .../skyblock/item/ItemProtection.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java index 5fc0fed5bd..7e04652c8f 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java @@ -11,9 +11,11 @@ import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.option.KeyBinding; import net.minecraft.command.CommandRegistryAccess; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.text.Text; import org.lwjgl.glfw.GLFW; @@ -73,7 +75,19 @@ private static int protectMyItem(FabricClientCommandSource source) { } public static void handleKeyPressed(ItemStack heldItem) { - if (!Utils.isOnSkyblock() || heldItem.isEmpty()) return; + PlayerEntity playerEntity = MinecraftClient.getInstance().player; + if (playerEntity == null){ + return; + } + if (!Utils.isOnSkyblock()) { + playerEntity.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.unableToProtect"))); + return; + } + + if (heldItem.isEmpty()) { + playerEntity.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.noItemUuid"))); + return; + } String itemUuid = ItemUtils.getItemUuid(heldItem); if (!itemUuid.isEmpty()) { @@ -82,10 +96,16 @@ public static void handleKeyPressed(ItemStack heldItem) { if (!protectedItems.contains(itemUuid)) { protectedItems.add(itemUuid); SkyblockerConfigManager.save(); + + playerEntity.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.added", heldItem.getName()))); } else { protectedItems.remove(itemUuid); SkyblockerConfigManager.save(); + + playerEntity.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.removed", heldItem.getName()))); } + } else { + playerEntity.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.noItemUuid"))); } }