Skip to content

Commit 573080d

Browse files
committed
steal PlayerInventoryGuiFactory from retro sophisticated backpacks with baubles compat
1 parent d93e315 commit 573080d

File tree

13 files changed

+362
-5
lines changed

13 files changed

+362
-5
lines changed

dependencies.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@
2323
*/
2424
dependencies {
2525
embed 'org.mariuszgromada.math:MathParser.org-mXparser:6.1.0'
26+
27+
implementation(rfg.deobf("curse.maven:baubles-227083:2518667"))
2628
}

src/main/java/com/cleanroommc/modularui/ModularUI.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class ModularUI {
3838
private static boolean blurLoaded = false;
3939
private static boolean sorterLoaded = false;
4040
private static boolean jeiLoaded = false;
41+
private static boolean baubleLoaded = false;
4142

4243
static {
4344
// confirm mXparser license
@@ -49,6 +50,7 @@ public void preInit(FMLPreInitializationEvent event) {
4950
blurLoaded = Loader.isModLoaded("blur");
5051
sorterLoaded = Loader.isModLoaded(BOGO_SORT);
5152
jeiLoaded = Loader.isModLoaded("jei");
53+
baubleLoaded = Loader.isModLoaded("baubles");
5254
proxy.preInit(event);
5355
}
5456

@@ -73,4 +75,8 @@ public static boolean isSortModLoaded() {
7375
public static boolean isJeiLoaded() {
7476
return jeiLoaded;
7577
}
78+
79+
public static boolean isBaubleLoaded() {
80+
return baubleLoaded;
81+
}
7682
}

src/main/java/com/cleanroommc/modularui/factory/AbstractUIFactory.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@
88
import com.cleanroommc.modularui.screen.UISettings;
99
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
1010

11+
import net.minecraft.entity.player.EntityPlayer;
12+
import net.minecraft.entity.player.EntityPlayerMP;
13+
1114
import org.jetbrains.annotations.NotNull;
1215

1316
import java.util.Objects;
1417

1518
public abstract class AbstractUIFactory<T extends GuiData> implements UIFactory<T> {
1619

20+
protected static EntityPlayerMP verifyServerSide(EntityPlayer player) {
21+
if (player == null) throw new NullPointerException("Can't open UI for null player!");
22+
if (player instanceof EntityPlayerMP entityPlayerMP) return entityPlayerMP;
23+
throw new IllegalStateException("Synced GUI must be opened from server side!");
24+
}
25+
1726
private final String name;
1827

1928
protected AbstractUIFactory(String name) {

src/main/java/com/cleanroommc/modularui/factory/GuiFactories.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ public static SidedTileEntityGuiFactory sidedTileEntity() {
1616
return SidedTileEntityGuiFactory.INSTANCE;
1717
}
1818

19+
@Deprecated
1920
public static ItemGuiFactory item() {
2021
return ItemGuiFactory.INSTANCE;
2122
}
2223

24+
public static PlayerInventoryGuiFactory playerInventory() {
25+
return PlayerInventoryGuiFactory.INSTANCE;
26+
}
27+
2328
public static SimpleGuiFactory createSimple(String name, IGuiHolder<GuiData> holder) {
2429
return new SimpleGuiFactory(name, holder);
2530
}
@@ -33,6 +38,7 @@ public static void init() {
3338
GuiManager.registerFactory(tileEntity());
3439
GuiManager.registerFactory(sidedTileEntity());
3540
GuiManager.registerFactory(item());
41+
GuiManager.registerFactory(playerInventory());
3642
}
3743

3844
private GuiFactories() {}

src/main/java/com/cleanroommc/modularui/factory/ItemGuiFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.util.Objects;
1313

14+
@Deprecated
1415
public class ItemGuiFactory extends AbstractUIFactory<HandGuiData> {
1516

1617
public static final ItemGuiFactory INSTANCE = new ItemGuiFactory();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.cleanroommc.modularui.factory;
2+
3+
import com.cleanroommc.modularui.factory.inventory.InventoryType;
4+
5+
import net.minecraft.entity.player.EntityPlayer;
6+
import net.minecraft.item.ItemStack;
7+
8+
public class PlayerInventoryGuiData extends GuiData {
9+
10+
private final InventoryType inventoryType;
11+
private final int slotIndex;
12+
13+
public PlayerInventoryGuiData(EntityPlayer player, InventoryType inventoryType, int slotIndex) {
14+
super(player);
15+
this.inventoryType = inventoryType;
16+
this.slotIndex = slotIndex;
17+
}
18+
19+
public InventoryType getInventoryType() {
20+
return inventoryType;
21+
}
22+
23+
public int getSlotIndex() {
24+
return slotIndex;
25+
}
26+
27+
public ItemStack getUsedItemStack() {
28+
return getInventoryType().getStackInSlot(getPlayer(), this.slotIndex);
29+
}
30+
31+
public void setUsedItemStack(ItemStack stack) {
32+
getInventoryType().setStackInSlot(getPlayer(), this.slotIndex, stack);
33+
}
34+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.cleanroommc.modularui.factory;
2+
3+
import com.cleanroommc.modularui.ModularUI;
4+
import com.cleanroommc.modularui.api.IGuiHolder;
5+
import com.cleanroommc.modularui.factory.inventory.InventoryType;
6+
7+
import com.cleanroommc.modularui.factory.inventory.InventoryTypes;
8+
9+
import net.minecraft.entity.player.EntityPlayer;
10+
import net.minecraft.network.PacketBuffer;
11+
12+
import net.minecraft.util.EnumHand;
13+
14+
import org.jetbrains.annotations.NotNull;
15+
16+
import java.util.Objects;
17+
18+
public class PlayerInventoryGuiFactory extends AbstractUIFactory<PlayerInventoryGuiData> {
19+
20+
public static final PlayerInventoryGuiFactory INSTANCE = new PlayerInventoryGuiFactory();
21+
22+
public void openFromPlayerInventory(EntityPlayer player, int index) {
23+
GuiManager.open(
24+
this, new PlayerInventoryGuiData(player, InventoryTypes.PLAYER, index), verifyServerSide(player));
25+
}
26+
27+
public void openFromHand(EntityPlayer player, EnumHand hand) {
28+
openFromPlayerInventory(player, hand == EnumHand.OFF_HAND ? 40 : player.inventory.currentItem);
29+
}
30+
31+
public void openFromBaubles(EntityPlayer player, int index) {
32+
if (!ModularUI.isBaubleLoaded()) {
33+
throw new IllegalArgumentException("Can't open UI for baubles item when bauble is not loaded!");
34+
}
35+
GuiManager.open(
36+
this, new PlayerInventoryGuiData(player, InventoryTypes.BAUBLES, index), verifyServerSide(player));
37+
}
38+
39+
public void open(EntityPlayer player, InventoryType type, int index) {
40+
GuiManager.open(this, new PlayerInventoryGuiData(player, type, index), verifyServerSide(player));
41+
}
42+
43+
private PlayerInventoryGuiFactory() {
44+
super("mui:player_inv");
45+
}
46+
47+
@Override
48+
public @NotNull IGuiHolder<PlayerInventoryGuiData> getGuiHolder(PlayerInventoryGuiData data) {
49+
return Objects.requireNonNull(castGuiHolder(data.getUsedItemStack().getItem()), "Item was not a gui holder!");
50+
}
51+
52+
@Override
53+
public void writeGuiData(PlayerInventoryGuiData guiData, PacketBuffer buffer) {
54+
guiData.getInventoryType().write(buffer);
55+
buffer.writeVarInt(guiData.getSlotIndex());
56+
}
57+
58+
@Override
59+
public @NotNull PlayerInventoryGuiData readGuiData(EntityPlayer player, PacketBuffer buffer) {
60+
return new PlayerInventoryGuiData(player, InventoryType.read(buffer), buffer.readVarInt());
61+
}
62+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.cleanroommc.modularui.factory.inventory;
2+
3+
import net.minecraft.entity.player.EntityPlayer;
4+
import net.minecraft.inventory.IInventory;
5+
import net.minecraft.item.ItemStack;
6+
7+
public abstract class Inventory extends InventoryType {
8+
9+
public Inventory(String id) {
10+
super(id);
11+
}
12+
13+
public abstract IInventory getInventory(EntityPlayer player);
14+
15+
@Override
16+
public ItemStack getStackInSlot(EntityPlayer player, int index) {
17+
return getInventory(player).getStackInSlot(index);
18+
}
19+
20+
@Override
21+
public void setStackInSlot(EntityPlayer player, int index, ItemStack stack) {
22+
getInventory(player).setInventorySlotContents(index, stack);
23+
}
24+
25+
@Override
26+
public int getSlotCount(EntityPlayer player) {
27+
return getInventory(player).getSizeInventory();
28+
}
29+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.cleanroommc.modularui.factory.inventory;
2+
3+
import com.cleanroommc.modularui.network.NetworkUtils;
4+
5+
import com.cleanroommc.modularui.utils.Platform;
6+
7+
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
8+
9+
import net.minecraft.entity.player.EntityPlayer;
10+
import net.minecraft.item.ItemStack;
11+
import net.minecraft.network.PacketBuffer;
12+
import net.minecraftforge.items.ItemHandlerHelper;
13+
14+
import org.apache.commons.lang3.tuple.Pair;
15+
16+
import java.util.Collection;
17+
import java.util.Collections;
18+
import java.util.Map;
19+
20+
/**
21+
* A way of finding and setting an item in an inventory, that is owned by a player.
22+
* This includes the normal player inventory with all its slots including main hand, off-hand and armor slots.
23+
* It can also be used for bauble inventory if baubles is loaded.
24+
* An inventory type has an id assigned used for syncing. It is crucial, types are created in the same order on client and server.
25+
* Currently, the amount of types is limited to 16, but I don't think we will ever fill that.
26+
* @see InventoryTypes InventoryTypes for default implementations
27+
*/
28+
public abstract class InventoryType {
29+
30+
private static final Map<String, InventoryType> inventoryTypes = new Object2ObjectOpenHashMap<>();
31+
32+
private final String id;
33+
34+
public InventoryType(String id) {
35+
this.id = id;
36+
inventoryTypes.put(id, this);
37+
}
38+
39+
public String getId() {
40+
return id;
41+
}
42+
43+
public abstract ItemStack getStackInSlot(EntityPlayer player, int index);
44+
45+
public abstract void setStackInSlot(EntityPlayer player, int index, ItemStack stack);
46+
47+
public abstract int getSlotCount(EntityPlayer player);
48+
49+
public int findFirstStackable(EntityPlayer player, ItemStack stack) {
50+
for (int i = 0, n = getSlotCount(player); i < n; ++i) {
51+
ItemStack stackInSlot = getStackInSlot(player, i);
52+
if (Platform.isStackEmpty(stackInSlot)) {
53+
if (Platform.isStackEmpty(stack)) {
54+
return i;
55+
}
56+
continue;
57+
}
58+
if (ItemHandlerHelper.canItemStacksStack(stackInSlot, stack)) {
59+
return i;
60+
}
61+
}
62+
return -1;
63+
}
64+
65+
public boolean visitAllStackable(EntityPlayer player, ItemStack stack, InventoryVisitor visitor) {
66+
for (int i = 0, n = getSlotCount(player); i < n; ++i) {
67+
ItemStack stackInSlot = getStackInSlot(player, i);
68+
if (Platform.isStackEmpty(stackInSlot)) {
69+
if (Platform.isStackEmpty(stack)) {
70+
if (visitor.visit(this, i, stackInSlot)) {
71+
return true;
72+
}
73+
}
74+
continue;
75+
}
76+
if (ItemHandlerHelper.canItemStacksStack(stackInSlot, stack)) {
77+
if (visitor.visit(this, i, stackInSlot)) {
78+
return true;
79+
}
80+
}
81+
}
82+
return false;
83+
}
84+
85+
public boolean visitAll(EntityPlayer player, InventoryVisitor visitor) {
86+
for (int i = 0, n = getSlotCount(player); i < n; ++i) {
87+
ItemStack stackInSlot = getStackInSlot(player, i);
88+
if (visitor.visit(this, i, stackInSlot)) {
89+
return true;
90+
}
91+
}
92+
return false;
93+
}
94+
95+
public void write(PacketBuffer buf) {
96+
NetworkUtils.writeStringSafe(buf, id);
97+
}
98+
99+
public static InventoryType read(PacketBuffer buf) {
100+
return getFromId(NetworkUtils.readStringSafe(buf));
101+
}
102+
103+
public static InventoryType getFromId(String id) {
104+
return inventoryTypes.get(id);
105+
}
106+
107+
public static Collection<InventoryType> getAll() {
108+
return Collections.unmodifiableCollection(inventoryTypes.values());
109+
}
110+
111+
public static Pair<InventoryType, Integer> findFirstStackableInAll(EntityPlayer player, ItemStack stack) {
112+
for (InventoryType type : getAll()) {
113+
int i = type.findFirstStackable(player, stack);
114+
if (i >= 0) return Pair.of(type, i);
115+
}
116+
return null;
117+
}
118+
119+
public static void visitAllStackableInAll(EntityPlayer player, ItemStack stack, InventoryVisitor visitor) {
120+
for (InventoryType type : getAll()) {
121+
if (type.visitAllStackable(player, stack, visitor)) {
122+
return;
123+
}
124+
}
125+
}
126+
127+
public static void visitAllInAll(EntityPlayer player, InventoryVisitor visitor) {
128+
for (InventoryType type : getAll()) {
129+
if (type.visitAll(player, visitor)) {
130+
return;
131+
}
132+
}
133+
}
134+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.cleanroommc.modularui.factory.inventory;
2+
3+
import baubles.api.BaublesApi;
4+
5+
import com.cleanroommc.modularui.ModularUI;
6+
7+
import net.minecraft.entity.player.EntityPlayer;
8+
import net.minecraft.inventory.IInventory;
9+
import net.minecraftforge.items.IItemHandlerModifiable;
10+
11+
public class InventoryTypes {
12+
13+
public static final InventoryType PLAYER = new Inventory("player") {
14+
@Override
15+
public IInventory getInventory(EntityPlayer player) {
16+
return player.inventory;
17+
}
18+
};
19+
20+
public static final InventoryType BAUBLES = new ItemHandler("baubles") {
21+
@Override
22+
public IItemHandlerModifiable getInventory(EntityPlayer player) {
23+
if (ModularUI.isBaubleLoaded()) {
24+
return BaublesApi.getBaublesHandler(player);
25+
}
26+
throw new IllegalArgumentException("Tried to receive bauble item, but bauble is not loaded");
27+
}
28+
};
29+
}

0 commit comments

Comments
 (0)