Skip to content

Commit

Permalink
Merge pull request #562 from LifeIsAParadox/Item-Protection-improvement
Browse files Browse the repository at this point in the history
Improve Item Protection feature
  • Loading branch information
LifeIsAParadox authored Feb 20, 2024
2 parents a15ce2a + 6885e1a commit 1410e25
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main/java/de/hysky/skyblocker/mixin/HandledScreenMixin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -53,6 +55,9 @@ public abstract class HandledScreenMixin<T extends ScreenHandler> 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;
Expand All @@ -67,8 +72,15 @@ protected HandledScreenMixin(Text title) {

@Inject(at = @At("HEAD"), method = "keyPressed")
public void skyblocker$keyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> 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());
}
}
}

Expand Down Expand Up @@ -239,5 +251,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();
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/de/hysky/skyblocker/mixin/InGameHudMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
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;
import de.hysky.skyblocker.skyblock.dungeon.DungeonScore;
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;
Expand Down Expand Up @@ -38,6 +40,9 @@
public abstract class InGameHudMixin {
@Unique
private static final Supplier<Identifier> 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!");

Expand All @@ -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();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,6 +35,7 @@ public abstract class MinecraftClientMixin {
public void skyblocker$handleInputEvents(CallbackInfo ci) {
if (Utils.isOnSkyblock()) {
HotbarSlotLock.handleInputEvents(player);
ItemProtection.handleHotbarKeyPressed(player);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@
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.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;

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);
}

Expand Down Expand Up @@ -61,4 +73,46 @@ private static int protectMyItem(FabricClientCommandSource source) {

return Command.SINGLE_SUCCESS;
}

public static void handleKeyPressed(ItemStack heldItem) {
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()) {
ObjectOpenHashSet<String> protectedItems = SkyblockerConfigManager.get().general.protectedItems;

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")));
}
}

public static void handleHotbarKeyPressed(ClientPlayerEntity player) {
while (itemProtection.wasPressed()) {
ItemStack heldItem = player.getMainHandStack();
handleKeyPressed(heldItem);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/skyblocker/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1410e25

Please sign in to comment.