-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #302 from viciscat/compactor_preview
Personal Compactor/Deletor preview
- Loading branch information
Showing
9 changed files
with
193 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/me/xmrvizzy/skyblocker/mixin/accessor/DrawContextInvoker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package me.xmrvizzy.skyblocker.mixin.accessor; | ||
|
||
import net.minecraft.client.font.TextRenderer; | ||
import net.minecraft.client.gui.DrawContext; | ||
import net.minecraft.client.gui.tooltip.TooltipComponent; | ||
import net.minecraft.client.gui.tooltip.TooltipPositioner; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Invoker; | ||
|
||
import java.util.List; | ||
|
||
@Mixin(DrawContext.class) | ||
public interface DrawContextInvoker { | ||
|
||
@Invoker | ||
void invokeDrawTooltip(TextRenderer textRenderer, List<TooltipComponent> components, int x, int y, TooltipPositioner positioner); | ||
} |
2 changes: 1 addition & 1 deletion
2
.../skyblocker/skyblock/BackpackPreview.java → ...locker/skyblock/item/BackpackPreview.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CompactorDeletorPreview.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package me.xmrvizzy.skyblocker.skyblock.item; | ||
|
||
import it.unimi.dsi.fastutil.ints.IntIntPair; | ||
import it.unimi.dsi.fastutil.ints.IntObjectPair; | ||
import me.xmrvizzy.skyblocker.mixin.accessor.DrawContextInvoker; | ||
import me.xmrvizzy.skyblocker.skyblock.itemlist.ItemRegistry; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.gui.DrawContext; | ||
import net.minecraft.client.gui.screen.Screen; | ||
import net.minecraft.client.gui.tooltip.HoveredTooltipPositioner; | ||
import net.minecraft.client.gui.tooltip.TooltipComponent; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Formatting; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
public class CompactorDeletorPreview { | ||
/** | ||
* The width and height in slots of the compactor/deletor | ||
*/ | ||
private static final Map<String, IntIntPair> DIMENSIONS = Map.of( | ||
"4000", IntIntPair.of(1, 1), | ||
"5000", IntIntPair.of(1, 3), | ||
"6000", IntIntPair.of(1, 7), | ||
"7000", IntIntPair.of(2, 6) | ||
); | ||
private static final IntIntPair DEFAULT_DIMENSION = IntIntPair.of(1, 6); | ||
public static final Pattern NAME = Pattern.compile("PERSONAL_(?<type>COMPACTOR|DELETOR)_(?<size>\\d+)"); | ||
private static final MinecraftClient client = MinecraftClient.getInstance(); | ||
|
||
public static boolean drawPreview(DrawContext context, ItemStack stack, String type, String size, int x, int y) { | ||
List<Text> tooltips = Screen.getTooltipFromItem(client, stack); | ||
int targetIndex = getTargetIndex(tooltips); | ||
if (targetIndex == -1) return false; | ||
|
||
// Get items in compactor or deletor | ||
NbtCompound nbt = stack.getNbt(); | ||
if (nbt == null || !nbt.contains("ExtraAttributes", 10)) { | ||
return false; | ||
} | ||
NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); | ||
// Get the slots and their items from the nbt, which is in the format personal_compact_<slot_number> or personal_deletor_<slot_number> | ||
List<IntObjectPair<ItemStack>> slots = extraAttributes.getKeys().stream().filter(slot -> slot.contains(type.toLowerCase().substring(0, 7))).map(slot -> IntObjectPair.of(Integer.parseInt(slot.substring(17)), ItemRegistry.getItemStack(extraAttributes.getString(slot)))).toList(); | ||
|
||
List<TooltipComponent> components = tooltips.stream().map(Text::asOrderedText).map(TooltipComponent::of).collect(Collectors.toList()); | ||
IntIntPair dimensions = DIMENSIONS.getOrDefault(size, DEFAULT_DIMENSION); | ||
|
||
// If there are no items in compactor or deletor | ||
if (slots.isEmpty()) { | ||
int slotsCount = dimensions.leftInt() * dimensions.rightInt(); | ||
components.add(targetIndex, TooltipComponent.of(Text.literal(slotsCount + (slotsCount == 1 ? " slot" : " slots")).formatted(Formatting.GRAY).asOrderedText())); | ||
|
||
((DrawContextInvoker) context).invokeDrawTooltip(client.textRenderer, components, x, y, HoveredTooltipPositioner.INSTANCE); | ||
return true; | ||
} | ||
|
||
// Add the preview tooltip component | ||
components.add(targetIndex, new CompactorPreviewTooltipComponent(slots, dimensions)); | ||
|
||
// Render accompanying text | ||
components.add(targetIndex, TooltipComponent.of(Text.literal("Contents:").asOrderedText())); | ||
if (extraAttributes.contains("PERSONAL_DELETOR_ACTIVE")) { | ||
components.add(targetIndex, TooltipComponent.of(Text.literal("Active: ") | ||
.append(extraAttributes.getBoolean("PERSONAL_DELETOR_ACTIVE") ? Text.literal("YES").formatted(Formatting.BOLD).formatted(Formatting.GREEN) : Text.literal("NO").formatted(Formatting.BOLD).formatted(Formatting.RED)).asOrderedText())); | ||
} | ||
((DrawContextInvoker) context).invokeDrawTooltip(client.textRenderer, components, x, y, HoveredTooltipPositioner.INSTANCE); | ||
return true; | ||
} | ||
|
||
/** | ||
* Finds the target index to insert the preview component, which is the second empty line | ||
*/ | ||
private static int getTargetIndex(List<Text> tooltips) { | ||
int targetIndex = -1; | ||
int lineCount = 0; | ||
for (int i = 0; i < tooltips.size(); i++) { | ||
if (tooltips.get(i).getString().isEmpty()) { | ||
lineCount += 1; | ||
} | ||
if (lineCount == 2) { | ||
targetIndex = i; | ||
break; | ||
} | ||
} | ||
return targetIndex; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CompactorPreviewTooltipComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package me.xmrvizzy.skyblocker.skyblock.item; | ||
|
||
import it.unimi.dsi.fastutil.ints.IntIntPair; | ||
import it.unimi.dsi.fastutil.ints.IntObjectPair; | ||
import me.xmrvizzy.skyblocker.SkyblockerMod; | ||
import net.minecraft.client.font.TextRenderer; | ||
import net.minecraft.client.gui.DrawContext; | ||
import net.minecraft.client.gui.tooltip.TooltipComponent; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.util.Identifier; | ||
|
||
public class CompactorPreviewTooltipComponent implements TooltipComponent { | ||
private static final Identifier INVENTORY_TEXTURE = new Identifier(SkyblockerMod.NAMESPACE, "textures/gui/inventory_background.png"); | ||
private final Iterable<IntObjectPair<ItemStack>> items; | ||
private final IntIntPair dimensions; | ||
|
||
public CompactorPreviewTooltipComponent(Iterable<IntObjectPair<ItemStack>> items, IntIntPair dimensions) { | ||
this.items = items; | ||
this.dimensions = dimensions; | ||
} | ||
|
||
@Override | ||
public int getHeight() { | ||
return dimensions.leftInt() * 18 + 14; | ||
} | ||
|
||
@Override | ||
public int getWidth(TextRenderer textRenderer) { | ||
return dimensions.rightInt() * 18 + 14; | ||
} | ||
|
||
@Override | ||
public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { | ||
context.drawTexture(INVENTORY_TEXTURE, x, y, 0, 0, 7 + dimensions.rightInt() * 18, 7); | ||
context.drawTexture(INVENTORY_TEXTURE, x + 7 + dimensions.rightInt() * 18, y, 169, 0, 7, 7); | ||
|
||
for (int i = 0; i < dimensions.leftInt(); i++) { | ||
context.drawTexture(INVENTORY_TEXTURE, x, y + 7 + i * 18, 0, 7, 7, 18); | ||
for (int j = 0; j < dimensions.rightInt(); j++) { | ||
context.drawTexture(INVENTORY_TEXTURE, x + 7 + j * 18, y + 7 + i * 18, 7, 7, 18, 18); | ||
} | ||
context.drawTexture(INVENTORY_TEXTURE, x + 7 + dimensions.rightInt() * 18, y + 7 + i * 18, 169, 7, 7, 18); | ||
} | ||
context.drawTexture(INVENTORY_TEXTURE, x, y + 7 + dimensions.leftInt() * 18, 0, 25, 7 + dimensions.rightInt() * 18, 7); | ||
context.drawTexture(INVENTORY_TEXTURE, x + 7 + dimensions.rightInt() * 18, y + 7 + dimensions.leftInt() * 18, 169, 25, 7, 7); | ||
|
||
for (IntObjectPair<ItemStack> entry : items) { | ||
int itemX = x + entry.leftInt() % dimensions.rightInt() * 18 + 8; | ||
int itemY = y + entry.leftInt() / dimensions.rightInt() * 18 + 8; | ||
context.drawItem(entry.right(), itemX, itemY); | ||
context.drawItemInSlot(textRenderer, entry.right(), itemX, itemY); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters