-
-
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.
Refactor CompactorDeletorPreview and add option
- Loading branch information
1 parent
63f8b9f
commit 3881303
Showing
6 changed files
with
92 additions
and
125 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
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
140 changes: 55 additions & 85 deletions
140
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 |
---|---|---|
@@ -1,122 +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.MutableText; | ||
import net.minecraft.text.Style; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Formatting; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
public class CompactorDeletorPreview { | ||
|
||
private static final MinecraftClient mcClient = MinecraftClient.getInstance(); | ||
private static final Map<String, int[]> personalCompactorTypeToSlot = new HashMap<>(); | ||
// Lines, and slots per lines | ||
static { | ||
personalCompactorTypeToSlot.put("4000", new int[]{1,1}); | ||
personalCompactorTypeToSlot.put("5000", new int[]{1,3}); | ||
personalCompactorTypeToSlot.put("6000", new int[]{1,7}); | ||
personalCompactorTypeToSlot.put("7000", new int[]{2,6}); | ||
personalCompactorTypeToSlot.put("default", new int[]{1,6}); | ||
} | ||
|
||
public static boolean displayCompactorDeletorPreview(DrawContextInvoker context, int x, int y, ItemStack stack) { | ||
String internalName = ItemRegistry.getInternalName(stack); | ||
|
||
String prefix; | ||
String itemSlotPrefix; | ||
if (internalName.contains("PERSONAL_COMPACTOR_")) { | ||
prefix = "PERSONAL_COMPACTOR_"; | ||
itemSlotPrefix = "personal_compact_"; | ||
} else { | ||
prefix = "PERSONAL_DELETOR_"; | ||
itemSlotPrefix = "personal_deletor_"; | ||
} | ||
|
||
// Find the line to insert component | ||
int targetIndex = -1; | ||
int lineCount = 0; | ||
|
||
List<Text> tooltips = Screen.getTooltipFromItem(mcClient, stack); | ||
for (int i = 0; i < tooltips.size(); i++) { | ||
if (tooltips.get(i).getString().isEmpty()) { | ||
lineCount += 1; | ||
} | ||
if (lineCount == 2) { | ||
targetIndex = i; | ||
break; | ||
} | ||
} | ||
/** | ||
* 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; | ||
List<TooltipComponent> components = new java.util.ArrayList<>(tooltips.stream().map(Text::asOrderedText).map(TooltipComponent::of).toList()); | ||
|
||
// STUFF | ||
String internalID = ItemRegistry.getInternalName(stack); | ||
String compactorType = internalID.replaceFirst(prefix, ""); | ||
int[] dimensions = personalCompactorTypeToSlot.containsKey(compactorType) ? personalCompactorTypeToSlot.get(compactorType) : personalCompactorTypeToSlot.get("default"); | ||
|
||
// Get items in compactor or deletor | ||
NbtCompound nbt = stack.getNbt(); | ||
if (nbt == null || !nbt.contains("ExtraAttributes", 10)) { | ||
return false; | ||
} | ||
NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); | ||
Set<String> attributesKeys = extraAttributes.getKeys(); | ||
List<String> compactorItems = attributesKeys.stream().filter(s -> s.contains(itemSlotPrefix)).toList(); | ||
Map<Integer, ItemStack> slotAndItem = new HashMap<>(); | ||
// 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 (compactorItems.isEmpty()) { | ||
int slotsCount = (dimensions[0] * dimensions[1]); | ||
components.add(targetIndex, TooltipComponent.of(Text.literal( | ||
slotsCount + (slotsCount == 1 ? " slot": " slots")) | ||
.fillStyle(Style.EMPTY.withColor(Formatting.DARK_GRAY)).asOrderedText())); | ||
// 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())); | ||
|
||
context.invokeDrawTooltip(mcClient.textRenderer, components, x, y, HoveredTooltipPositioner.INSTANCE); | ||
((DrawContextInvoker) context).invokeDrawTooltip(client.textRenderer, components, x, y, HoveredTooltipPositioner.INSTANCE); | ||
return true; | ||
} | ||
|
||
compactorItems.forEach(s -> slotAndItem.put(getNumberAtEnd(s, itemSlotPrefix), ItemRegistry.getItemStack(extraAttributes.getString(s)))); | ||
// Add the preview tooltip component | ||
components.add(targetIndex, new CompactorPreviewTooltipComponent(slots, dimensions)); | ||
|
||
|
||
components.add(targetIndex, new CompactorPreviewTooltipComponent(slotAndItem, dimensions)); | ||
components.add(targetIndex, TooltipComponent.of(Text.literal(" ").append( | ||
Text.literal("Contents:").fillStyle(Style.EMPTY | ||
.withItalic(true))) | ||
.asOrderedText())); | ||
if (attributesKeys.stream().anyMatch(s -> s.contains("PERSONAL_DELETOR_ACTIVE"))) { | ||
MutableText isActiveText = Text.literal("Active: "); | ||
if (extraAttributes.getBoolean("PERSONAL_DELETOR_ACTIVE")) { | ||
components.add(targetIndex, TooltipComponent.of(isActiveText.append( | ||
Text.literal("YES").fillStyle(Style.EMPTY.withBold(true).withColor(Formatting.GREEN)) | ||
).asOrderedText() | ||
)); | ||
} else { | ||
components.add(targetIndex, TooltipComponent.of(isActiveText.append( | ||
Text.literal("NO").fillStyle(Style.EMPTY.withBold(true).withColor(Formatting.RED)) | ||
).asOrderedText() | ||
)); | ||
} | ||
// 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())); | ||
} | ||
context.invokeDrawTooltip(mcClient.textRenderer, components, x, y, HoveredTooltipPositioner.INSTANCE); | ||
((DrawContextInvoker) context).invokeDrawTooltip(client.textRenderer, components, x, y, HoveredTooltipPositioner.INSTANCE); | ||
return true; | ||
} | ||
|
||
private static Integer getNumberAtEnd(String str, String attributesKey) { | ||
try { | ||
String numberPartOfTheString = str.replace(attributesKey, ""); | ||
return Integer.parseInt(numberPartOfTheString); | ||
} catch (NumberFormatException e) { | ||
return 0; | ||
/** | ||
* 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; | ||
} | ||
} |
48 changes: 21 additions & 27 deletions
48
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 |
---|---|---|
@@ -1,60 +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.client.util.math.MatrixStack; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.util.Identifier; | ||
|
||
import java.util.Map; | ||
|
||
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; | ||
|
||
Map<Integer, ItemStack> items; | ||
int[] dimensions; | ||
|
||
public CompactorPreviewTooltipComponent(Map<Integer, ItemStack> items, int[] dimensions) { | ||
public CompactorPreviewTooltipComponent(Iterable<IntObjectPair<ItemStack>> items, IntIntPair dimensions) { | ||
this.items = items; | ||
this.dimensions = dimensions; | ||
} | ||
|
||
@Override | ||
public int getHeight() { | ||
return dimensions[0] * 18 + 14; | ||
return dimensions.leftInt() * 18 + 14; | ||
} | ||
|
||
@Override | ||
public int getWidth(TextRenderer textRenderer) { | ||
return dimensions[1] * 18 + 14; | ||
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[1] * 18, 7); | ||
context.drawTexture(INVENTORY_TEXTURE, x + 7 + dimensions[1] * 18, y, 169, 0, 7, 7); | ||
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[0]; i++) { | ||
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[1]; j++) { | ||
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[1] * 18, y + 7 + i * 18, 169, 7, 7, 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[0] * 18, 0, 25, 7 + dimensions[1] * 18, 7); | ||
context.drawTexture(INVENTORY_TEXTURE, x + 7 + dimensions[1] * 18, y + 7 + dimensions[0] * 18, 169, 25, 7, 7); | ||
|
||
MatrixStack matrices = context.getMatrices(); | ||
for (Integer i : items.keySet()) { | ||
int itemX = x + i % dimensions[1] * 18 + 8; | ||
int itemY = y + i / dimensions[1] * 18 + 8; | ||
matrices.push(); | ||
matrices.translate(0, 0, 200); | ||
context.drawItem(items.get(i), itemX, itemY); | ||
context.drawItemInSlot(textRenderer, items.get(i), itemX, itemY); | ||
matrices.pop(); | ||
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