mirror of
https://github.com/GTNewHorizons/Baubles-Expanded.git
synced 2026-10-26 14:23:55 +00:00
Display type tooltip when hovering on a Bauble slot (#22)
This commit is contained in:
1 parent
031665506c
commit
0d42facd4a
4 files changed
+122
-11
No files matched your search
@@ -1,5 +1,7 @@
|
||||
package baubles.client.gui;
|
||||
|
||||
import baubles.api.IBauble;
|
||||
import baubles.api.expanded.IBaubleExpanded;
|
||||
import codechicken.lib.vec.Rectangle4i;
|
||||
import codechicken.nei.NEIClientConfig;
|
||||
import codechicken.nei.VisiblityData;
|
||||
@@ -19,6 +21,7 @@ import net.minecraft.potion.Potion;
|
||||
import net.minecraft.potion.PotionEffect;
|
||||
import net.minecraft.util.MathHelper;
|
||||
|
||||
import net.minecraft.util.StatCollector;
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
@@ -32,6 +35,7 @@ import net.minecraft.client.gui.achievement.GuiStats;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -49,11 +53,11 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler {
|
||||
private static final boolean hasLwjgl3 = Loader.isModLoaded("lwjgl3ify");
|
||||
|
||||
/**
|
||||
* x size of the inventory window in pixels. Defined as float, passed as int.
|
||||
* x size of the inventory window in pixels. Defined as float, passed as int.
|
||||
*/
|
||||
private float xSizeFloat;
|
||||
/**
|
||||
* y size of the inventory window in pixels. Defined as float, passed as int.
|
||||
* y size of the inventory window in pixels. Defined as float, passed as int.
|
||||
*/
|
||||
private float ySizeFloat;
|
||||
|
||||
@@ -66,6 +70,9 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler {
|
||||
/** True if the left mouse button was held down last time drawScreen was called. */
|
||||
private boolean wasClicking;
|
||||
|
||||
private int tooltipIndexCache = -1;
|
||||
private final List<String> tooltipCache = new ArrayList<>(2);
|
||||
|
||||
public GuiPlayerExpanded(EntityPlayer player) {
|
||||
super(new ContainerPlayerExpanded(player.inventory, !player.worldObj.isRemote, player));
|
||||
allowUserInput = true;
|
||||
@@ -103,6 +110,10 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler {
|
||||
xSizeFloat = (float) mouseX;
|
||||
ySizeFloat = (float) mouseY;
|
||||
|
||||
if(BaublesConfig.displayTooltipOnHover) {
|
||||
handleMouseHover(mouseX, mouseY);
|
||||
}
|
||||
|
||||
handleScrollbar(mouseX, mouseY);
|
||||
}
|
||||
|
||||
@@ -217,6 +228,79 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMouseHover(int mouseX, int mouseY) {
|
||||
ContainerPlayerExpanded expandedInventory = (ContainerPlayerExpanded) this.inventorySlots;
|
||||
|
||||
// Check the last cached slot first, as it is the most likely one to be hovered out of all of them
|
||||
if(tooltipIndexCache != -1) {
|
||||
Slot slot = expandedInventory.getBaubleSlot(tooltipIndexCache);
|
||||
|
||||
// Cursor inside slot rect
|
||||
if (this.func_146978_c(slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, mouseX, mouseY)) {
|
||||
ItemStack stack = expandedInventory.baubles.getStackInSlot(tooltipIndexCache);
|
||||
if(stack == null || stack.stackSize == 0) {
|
||||
// drawHoveringText with default font
|
||||
func_146283_a(tooltipCache, mouseX, mouseY);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check the other slots
|
||||
for (int slotIndex = 0; slotIndex < expandedInventory.getBaubleSlotCount(); slotIndex++) {
|
||||
if(slotIndex == tooltipIndexCache) continue;
|
||||
|
||||
Slot slot = expandedInventory.getBaubleSlot(slotIndex);
|
||||
|
||||
// Cursor inside slot rect
|
||||
if (!this.func_146978_c(slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, mouseX, mouseY)) continue;
|
||||
|
||||
ItemStack stack = expandedInventory.baubles.getStackInSlot(slotIndex);
|
||||
if(stack != null && stack.stackSize > 0) continue; // Only show tooltip on empty slots
|
||||
|
||||
tooltipIndexCache = slotIndex;
|
||||
|
||||
String slotType = BaubleExpandedSlots.getSlotType(slotIndex);
|
||||
|
||||
tooltipCache.clear();
|
||||
|
||||
// Strip formatting codes
|
||||
String strippedType = StatCollector.translateToLocal("slot." + slotType).replaceAll("§[0-9a-fklmnor]", "");
|
||||
tooltipCache.add(strippedType);
|
||||
|
||||
ItemStack heldItem = mc.thePlayer.inventory.getItemStack();
|
||||
|
||||
if (heldItem != null && heldItem.stackSize > 0) {
|
||||
boolean fitsInSlot = false;
|
||||
if (heldItem.getItem() instanceof IBaubleExpanded baubleExpandedItem) {
|
||||
String[] itemBaubleTypes = baubleExpandedItem.getBaubleTypes(heldItem);
|
||||
for(String itemBaubleType : itemBaubleTypes) {
|
||||
if (slotType.equals(itemBaubleType)) {
|
||||
fitsInSlot = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(heldItem.getItem() instanceof IBauble baubleItem) {
|
||||
String itemBaubleType = BaubleExpandedSlots.getTypeFromBaubleType(baubleItem.getBaubleType(heldItem));
|
||||
if (slotType.equals(itemBaubleType)) {
|
||||
fitsInSlot = true;
|
||||
}
|
||||
}
|
||||
|
||||
tooltipCache.add(fitsInSlot
|
||||
? StatCollector.translateToLocal("tooltip.fitsInSlot")
|
||||
: StatCollector.translateToLocal("tooltip.doesNotFitInSlot"));
|
||||
}
|
||||
|
||||
// drawHoveringText with default font
|
||||
func_146283_a(tooltipCache, mouseX, mouseY);
|
||||
return;
|
||||
}
|
||||
|
||||
tooltipIndexCache = -1;
|
||||
}
|
||||
|
||||
private boolean needsScrollBars() {
|
||||
return ((ContainerPlayerExpanded) this.inventorySlots).canScroll();
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ public class BaublesConfig {
|
||||
public static boolean useOldGuiRendering = false;
|
||||
public static boolean showUnusedSlots = false;
|
||||
public static boolean manualSlotSelection = false;
|
||||
public static boolean displayTooltipOnHover = true;
|
||||
|
||||
public static String[] overrideSlotTypes = new String[] {
|
||||
BaubleExpandedSlots.amuletType,
|
||||
@@ -51,6 +52,9 @@ public class BaublesConfig {
|
||||
manualSlotSelection = config.getBoolean("manualSlotSelection", categoryMenu, manualSlotSelection,
|
||||
"Manually override slot assignments.\n!Bauble slot types must be configured manually with this option enabled!\n"
|
||||
);
|
||||
displayTooltipOnHover = config.getBoolean("displayTooltipOnHover", categoryMenu, displayTooltipOnHover,
|
||||
"When hovering the mouse over a bauble slot, display a tooltip with the bauble type and if a held item can be equipped in that slot.\n"
|
||||
);
|
||||
|
||||
//categoryOverride
|
||||
config.getStringList("defualtSlotTypes", categoryOverride, new String[] {},
|
||||
|
||||
@@ -21,8 +21,6 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.CraftingManager;
|
||||
import net.minecraft.util.IIcon;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import static baubles.common.BaublesConfig.useOldGuiRendering;
|
||||
|
||||
public class ContainerPlayerExpanded extends Container {
|
||||
@@ -33,6 +31,11 @@ public class ContainerPlayerExpanded extends Container {
|
||||
|
||||
private final EntityPlayer thePlayer;
|
||||
|
||||
private int slotsAdded = 0;
|
||||
|
||||
private int baubleFirstSlotIndex = -1;
|
||||
private int baubleSlotCount = 0;
|
||||
|
||||
public ContainerPlayerExpanded(InventoryPlayer playerInv, boolean isClient, EntityPlayer player) {
|
||||
this.thePlayer = player;
|
||||
baubles = PlayerHandler.getPlayerBaubles(player);
|
||||
@@ -41,6 +44,7 @@ public class ContainerPlayerExpanded extends Container {
|
||||
int i;
|
||||
int j;
|
||||
|
||||
// Crafting slots
|
||||
if (!useOldGuiRendering) {
|
||||
this.addSlotToContainer(new SlotCrafting(playerInv.player, this.craftMatrix, this.craftResult, 0, 144, 36));
|
||||
|
||||
@@ -77,14 +81,16 @@ public class ContainerPlayerExpanded extends Container {
|
||||
final int slotStartY = 8;
|
||||
|
||||
// Bauble slots
|
||||
baubleFirstSlotIndex = slotsAdded;
|
||||
for (i = 0; i < BaubleExpandedSlots.slotLimit; i++) {
|
||||
String slotType = BaubleExpandedSlots.getSlotType(i);
|
||||
if (BaublesConfig.showUnusedSlots || !slotType.equals(BaubleExpandedSlots.unknownType)) {
|
||||
if (useOldGuiRendering) {
|
||||
addSlotToContainer(new SlotBauble(baubles, slotType, i, slotStartX + (slotOffset * (i / 4)), slotStartY + (slotOffset * (i % 4))));
|
||||
} else {
|
||||
addSlotToContainer(new SlotBauble(baubles, slotType, i, -18, 12 + (slotOffset * i)));
|
||||
}
|
||||
Slot slot = useOldGuiRendering
|
||||
? new SlotBauble(baubles, slotType, i, slotStartX + (slotOffset * (i / 4)), slotStartY + (slotOffset * (i % 4)))
|
||||
: new SlotBauble(baubles, slotType, i, -18, 12 + (slotOffset * i));
|
||||
|
||||
addSlotToContainer(slot);
|
||||
baubleSlotCount++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,6 +112,13 @@ public class ContainerPlayerExpanded extends Container {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Slot addSlotToContainer(Slot slot) {
|
||||
// Count slots as we add them so we can determine what the first Bauble slot index will be
|
||||
slotsAdded++;
|
||||
return super.addSlotToContainer(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCraftMatrixChanged(IInventory par1IInventory) {
|
||||
if (!useOldGuiRendering) {
|
||||
@@ -135,6 +148,15 @@ public class ContainerPlayerExpanded extends Container {
|
||||
}
|
||||
}
|
||||
|
||||
public int getBaubleSlotCount() {
|
||||
return this.baubleSlotCount;
|
||||
}
|
||||
|
||||
public SlotBauble getBaubleSlot(int slotIndex) {
|
||||
if(slotIndex < 0 || slotIndex >= baubleSlotCount) return null;
|
||||
return (SlotBauble) inventorySlots.get(baubleFirstSlotIndex + slotIndex);
|
||||
}
|
||||
|
||||
public void scrollTo(float offset) {
|
||||
if (!canScroll()) return;
|
||||
final int activeBaubleSlots = BaubleExpandedSlots.slotsCurrentlyUsed();
|
||||
@@ -149,8 +171,7 @@ public class ContainerPlayerExpanded extends Container {
|
||||
}
|
||||
|
||||
for (int i = 0; i < activeBaubleSlots && i < BaubleExpandedSlots.slotLimit; i++) {
|
||||
//TODO: Find a way to not use a magic number to get to the correct ID range for the Slot editing.
|
||||
Slot slot = (Slot) this.inventorySlots.get(9 + i);
|
||||
Slot slot = (Slot) this.inventorySlots.get(baubleFirstSlotIndex + i);
|
||||
if (i >= 0) {
|
||||
slot.yDisplayPosition = (12 - (slotOffset * 18) + (i) * 18);
|
||||
if (slot.yDisplayPosition < 12 || slot.yDisplayPosition > 8 * 18) {
|
||||
|
||||
@@ -12,6 +12,8 @@ keybind.baublesinventory=Baubles Inventory
|
||||
# Tooltips
|
||||
tooltip.compatibleslots=Equippable in the following slots:
|
||||
tooltip.shiftprompt=Hold §bSHIFT§7 for more info.
|
||||
tooltip.fitsInSlot=§aValid slot§r
|
||||
tooltip.doesNotFitInSlot=§cInvalid slot§r
|
||||
#
|
||||
# Slots
|
||||
slot.amulet=§aAmulet§7
|
||||
|
||||
Reference in new issue
Block a user