Skip to content
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

Refactor Backpack Preview #387

Merged
merged 9 commits into from
Oct 26, 2023
Merged
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
31 changes: 16 additions & 15 deletions src/main/java/de/hysky/skyblocker/mixin/HandledScreenMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
@Mixin(HandledScreen.class)
public abstract class HandledScreenMixin<T extends ScreenHandler> extends Screen {
/**
* This is the slot id returned for when a click is outside of the screen's bounds
* This is the slot id returned for when a click is outside the screen's bounds
*/
@Unique
private static final int OUT_OF_BOUNDS_SLOT = -999;

@Shadow
@Nullable
protected Slot focusedSlot;

@Shadow
@Final
protected T handler;
Expand Down Expand Up @@ -78,7 +78,7 @@ protected HandledScreenMixin(Text title) {

// Backpack Preview
boolean shiftDown = SkyblockerConfigManager.get().general.backpackPreviewWithoutShift ^ Screen.hasShiftDown();
if (shiftDown && getTitle().getString().equals("Storage") && focusedSlot.inventory != client.player.getInventory() && BackpackPreview.renderPreview(context, focusedSlot.getIndex(), x, y)) {
if (shiftDown && getTitle().getString().equals("Storage") && focusedSlot.inventory != client.player.getInventory() && BackpackPreview.renderPreview(context, this, focusedSlot.getIndex(), x, y)) {
ci.cancel();
}

Expand Down Expand Up @@ -133,57 +133,58 @@ protected HandledScreenMixin(Text title) {
}
}
}

/**
* The naming of this method in yarn is half true, its mostly to handle slot/item interactions (which are mouse or keyboard clicks)
* For example, using the drop key bind while hovering over an item will invoke this method to drop the players item
*/
@Inject(method = "onMouseClick(Lnet/minecraft/screen/slot/Slot;IILnet/minecraft/screen/slot/SlotActionType;)V", at = @At("HEAD"), cancellable = true)
private void skyblocker$onSlotInteract(Slot slot, int slotId, int button, SlotActionType actionType, CallbackInfo ci) {
if (Utils.isOnSkyblock()) {
if (Utils.isOnSkyblock()) {
// When you try and drop the item by picking it up then clicking outside of the screen
if (slotId == OUT_OF_BOUNDS_SLOT) {
ItemStack cursorStack = this.handler.getCursorStack();

if (ItemProtection.isItemProtected(cursorStack)) ci.cancel();
}

if (slot != null) {
// When you click your drop key while hovering over an item
if (actionType == SlotActionType.THROW) {
ItemStack stack = slot.getStack();

if (ItemProtection.isItemProtected(stack)) ci.cancel();
}

//Prevent salvaging
if (this.getTitle().getString().equals("Salvage Items")) {
ItemStack stack = slot.getStack();

if (ItemProtection.isItemProtected(stack)) ci.cancel();
}

//Prevent selling to NPC shops
if (this.client != null && this.handler instanceof GenericContainerScreenHandler genericContainerScreenHandler && genericContainerScreenHandler.getRows() == 6) {
ItemStack sellItem = this.handler.slots.get(49).getStack();

if (sellItem.getName().getString().equals("Sell Item") || skyblocker$doesLoreContain(sellItem, this.client, "buyback")) {
ItemStack stack = slot.getStack();

if (ItemProtection.isItemProtected(stack)) ci.cancel();
}
}
}
}
}
}

//TODO make this a util method somewhere else, eventually
private static boolean skyblocker$doesLoreContain(ItemStack stack, MinecraftClient client, String searchString) {
return stack.getTooltip(client.player, TooltipContext.BASIC).stream().map(Text::getString).anyMatch(line -> line.contains(searchString));
}

@Inject(method = "drawSlot", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/DrawContext;drawItem(Lnet/minecraft/item/ItemStack;III)V"))
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);
if (Utils.isOnSkyblock() && SkyblockerConfigManager.get().general.itemInfoDisplay.itemRarityBackgrounds)
ItemRarityBackgrounds.tryDraw(slot.getStack(), context, slot.x, slot.y);
}
}
Loading