Skip to content

Commit 753ba59

Browse files
committed
floating chest - show tooltips with numbers
chunk chest - change size update
1 parent c488147 commit 753ba59

File tree

5 files changed

+88
-20
lines changed

5 files changed

+88
-20
lines changed

src/main/java/com/example/examplemod/data/ModWorldData.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public AdjustableItemStackHandler getChunkChest(int chunkx, int chunkz) {
5858
protected void onContentsChanged(int slot) {
5959
ModWorldData.this.markDirty();
6060
}
61+
@Override
62+
protected void onSizeChanged(int slot) { ModWorldData.this.markDirty(); }
6163
};
6264
if (!chunkchest.hasKey(key)) {
6365
chunkchest.setTag(key, handler.serializeNBT());

src/main/java/com/example/examplemod/gui/BaseContainer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.example.examplemod.helper.GuiHelper;
44
import com.example.examplemod.itemhandler.FloatingItemStack;
55
import com.example.examplemod.itemhandler.FloatingItemStackHandler;
6+
import com.example.examplemod.itemhandler.FloatingSlot;
67
import net.minecraft.entity.player.EntityPlayer;
78
import net.minecraft.inventory.Container;
89
import net.minecraft.inventory.IInventory;
@@ -18,15 +19,15 @@ public abstract class BaseContainer extends Container {
1819
@Override
1920
public abstract boolean canInteractWith(EntityPlayer playerIn);
2021

21-
public List<Tuple4<FloatingItemStackHandler, Integer, Integer, Integer>> floatingSlots = new ArrayList<>();
22+
public List<FloatingSlot> floatingSlots = new ArrayList<>();
2223

2324
public void drawFloatingSlots(FloatingItemStackHandler itemHandler, int offsetX, int offsetY, int offsetIndex, int shapeX, int shapeY) {
2425
int index = offsetIndex;
2526
for (int row = 0; row < shapeY; ++row) {
2627
for (int col = 0; col < shapeX; ++col) {
2728
int x = col * 18 + offsetX;
2829
int y = row * 18 + offsetY;
29-
floatingSlots.add(Tuple4.apply(itemHandler, index, x, y));
30+
floatingSlots.add(new FloatingSlot(itemHandler, index, x, y));
3031
index++;
3132
}
3233
}
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
package com.example.examplemod.gui;
22

3-
import com.example.examplemod.itemhandler.FloatingItemStack;
4-
import com.example.examplemod.itemhandler.FloatingItemStackHandler;
3+
import com.example.examplemod.helper.GuiHelper;
4+
import com.example.examplemod.itemhandler.FloatingSlot;
55
import net.minecraft.client.gui.inventory.GuiContainer;
6-
import net.minecraft.inventory.Container;
76
import net.minecraft.item.ItemStack;
8-
import scala.Tuple4;
97

108
import java.util.List;
119

12-
import static com.example.examplemod.gui.ChunkChestContainer.chestSlotX;
13-
import static com.example.examplemod.gui.ChunkChestContainer.chestSlotY;
14-
1510
public abstract class BaseGui extends GuiContainer {
1611
public BaseGui(BaseContainer inventorySlotsIn) {
1712
super(inventorySlotsIn);
1813
}
1914

15+
private FloatingSlot hoveredSlotFloating;
16+
2017
public void drawItemStack(ItemStack itemstack, int x, int y) {
2118
drawItemStack(itemstack, x, y, "");
2219
}
@@ -29,35 +26,47 @@ public void drawItemStack(ItemStack itemstack, int x, int y, String str) {
2926
@Override
3027
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
3128
super.drawScreen(mouseX, mouseY, partialTicks);
32-
List<Tuple4<FloatingItemStackHandler, Integer, Integer, Integer>> floatingSlots
29+
hoveredSlotFloating = null;
30+
List<FloatingSlot> floatingSlots
3331
= ((BaseContainer)inventorySlots).floatingSlots;
34-
for (Tuple4<FloatingItemStackHandler, Integer, Integer, Integer> tup : floatingSlots) {
35-
FloatingItemStackHandler handler = tup._1();
36-
int index = tup._2();
37-
int x = tup._3();
38-
int y = tup._4();
32+
for (FloatingSlot slot : floatingSlots) {
33+
int x = slot.getX();
34+
int y = slot.getY();
3935
int baseX = width/2 - xSize/2;
4036
int baseY = height/2 - ySize/2;
41-
FloatingItemStack stack = handler.getStackInSlotFloating(index);
42-
Double size = stack.getStackSize();
37+
Double size = slot.getSize();
4338
String str;
4439
if (Double.isNaN(size)) {
4540
str = "?";
4641
} else if (Double.isInfinite(size)) {
4742
str = "!";
4843
} else {
49-
int index_scale = (int) Math.floor(Math.max(Math.log10(Math.abs(stack.getStackSize())), 0));
44+
int index_scale = (int) Math.floor(Math.max(Math.log10(Math.abs(slot.getSize())), 0));
5045
str = Integer.toString(index_scale, 36);
5146
}
5247
if (size < 0) {
5348
str = "-" + str;
5449
}
50+
ItemStack itemStack = slot.getItemStack();
5551
drawItemStack(
56-
stack.getItemStack(),
52+
itemStack,
5753
baseX + x,
5854
baseY + y,
5955
str
6056
);
57+
if (GuiHelper.collision(baseX + x, baseY + y, 18, 18, mouseX, mouseY)) {
58+
hoveredSlotFloating = slot;
59+
}
60+
}
61+
}
62+
63+
@Override
64+
protected void renderHoveredToolTip(int mouseX, int mouseY) {
65+
super.renderHoveredToolTip(mouseX, mouseY);
66+
if (hoveredSlotFloating != null && !hoveredSlotFloating.isEmpty()) {
67+
List<String> list = hoveredSlotFloating.getTooltip(this.mc.player, () -> false);
68+
list.add("x " + hoveredSlotFloating.getSize());
69+
drawHoveringText(list, mouseX, mouseY);
6170
}
6271
}
6372
}

src/main/java/com/example/examplemod/itemhandler/AdjustableItemStackHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ public AdjustableItemStackHandler(NonNullList<ItemStack> stacks)
2222
}
2323
public void setSize(int size) {
2424
stacks = ItemHelper.withListSize(stacks, size, ItemStack.EMPTY);
25-
onContentsChanged(0);
25+
onSizeChanged(0);
2626
}
27+
28+
protected void onSizeChanged(int index) {}
29+
2730
public void modifySize(int size) {
2831
setSize(size + stacks.size());
2932
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.example.examplemod.itemhandler;
2+
3+
import net.minecraft.client.entity.EntityPlayerSP;
4+
import net.minecraft.client.util.ITooltipFlag;
5+
import net.minecraft.item.ItemStack;
6+
7+
import java.util.List;
8+
9+
public class FloatingSlot {
10+
private final FloatingItemStackHandler handler;
11+
private final Integer index;
12+
private final int x;
13+
private final int y;
14+
15+
public FloatingItemStackHandler getHandler() {
16+
return handler;
17+
}
18+
19+
public boolean isEmpty() {
20+
return handler.stacks.get(index).isEmpty();
21+
}
22+
23+
public Double getSize() {
24+
return handler.stacks.get(index).getStackSize();
25+
}
26+
27+
public Integer getIndex() {
28+
return index;
29+
}
30+
31+
public int getX() {
32+
return x;
33+
}
34+
35+
public int getY() {
36+
return y;
37+
}
38+
39+
public FloatingSlot (FloatingItemStackHandler handler, Integer index, int x, int y) {
40+
this.handler = handler;
41+
this.index = index;
42+
this.x = x;
43+
this.y = y;
44+
}
45+
46+
public List<String> getTooltip(EntityPlayerSP player, ITooltipFlag flag) {
47+
return handler.stacks.get(index).itemStack.getTooltip(player, flag);
48+
}
49+
50+
public ItemStack getItemStack() {
51+
return handler.stacks.get(index).itemStack.copy();
52+
}
53+
}

0 commit comments

Comments
 (0)