Skip to content

Commit 9efd2f7

Browse files
committed
allow customization of canInteractWith() (supersede #101)
1 parent 9ac3b65 commit 9efd2f7

4 files changed

Lines changed: 76 additions & 4 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static <T extends GuiData> void open(@NotNull UIFactory<T> factory, @NotN
7272
PanelSyncManager syncManager = new PanelSyncManager();
7373
ModularPanel panel = factory.createPanel(guiData, syncManager);
7474
WidgetTree.collectSyncValues(syncManager, panel);
75-
ModularContainer container = new ModularContainer(player, syncManager, panel.getName());
75+
ModularContainer container = new ModularContainer(player, syncManager, panel.getName(), guiData);
7676
// sync to client
7777
player.getNextWindowId();
7878
player.closeContainer();
@@ -98,7 +98,7 @@ public static <T extends GuiData> void open(int windowId, @NotNull UIFactory<T>
9898
WidgetTree.collectSyncValues(syncManager, panel);
9999
ModularScreen screen = factory.createScreen(guiData, panel);
100100
screen.getContext().setJeiSettings(jeiSettings);
101-
ModularContainer container = new ModularContainer(player, syncManager, panel.getName());
101+
ModularContainer container = new ModularContainer(player, syncManager, panel.getName(), guiData);
102102
IMuiScreen wrapper = factory.createScreenWrapper(container, screen);
103103
if (!(wrapper.getGuiScreen() instanceof GuiContainer guiContainer)) {
104104
throw new IllegalStateException("The wrapping screen must be a GuiContainer for synced GUIs!");

src/main/java/com/cleanroommc/modularui/screen/ContainerCustomizer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.jetbrains.annotations.Nullable;
1818

1919
import java.util.Objects;
20+
import java.util.function.Predicate;
2021

2122
/**
2223
* Sometimes you need special behaviour. This class allows you to override common methods from {@link net.minecraft.inventory.Container Container}.
@@ -30,6 +31,7 @@ public class ContainerCustomizer {
3031
private static final int RIGHT_MOUSE = 1;
3132

3233
private ModularContainer container;
34+
private Predicate<EntityPlayer> canInteractWith;
3335

3436
void initialize(ModularContainer container) {
3537
this.container = container;
@@ -42,6 +44,14 @@ public ModularContainer getContainer() {
4244
return container;
4345
}
4446

47+
public Predicate<EntityPlayer> getCanInteractWith() {
48+
return canInteractWith;
49+
}
50+
51+
public void setCanInteractWith(Predicate<EntityPlayer> canInteractWith) {
52+
this.canInteractWith = canInteractWith;
53+
}
54+
4555
public void onContainerClosed() {}
4656

4757
public @NotNull ItemStack slotClick(int slotId, int mouseButton, @NotNull ClickType clickTypeIn, EntityPlayer player) {

src/main/java/com/cleanroommc/modularui/screen/ModularContainer.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.cleanroommc.modularui.ModularUI;
44
import com.cleanroommc.modularui.core.mixin.ContainerAccessor;
5+
import com.cleanroommc.modularui.factory.GuiData;
6+
import com.cleanroommc.modularui.factory.PosGuiData;
57
import com.cleanroommc.modularui.network.NetworkUtils;
68
import com.cleanroommc.modularui.value.sync.ModularSyncManager;
79
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
@@ -29,6 +31,7 @@
2931
import org.jetbrains.annotations.Nullable;
3032

3133
import java.util.*;
34+
import java.util.function.Predicate;
3235

3336
@Interface(modid = ModularUI.BOGO_SORT, iface = "com.cleanroommc.bogosorter.api.ISortableContainer")
3437
public class ModularContainer extends Container implements ISortableContainer {
@@ -46,19 +49,34 @@ public static ModularContainer getCurrent(EntityPlayer player) {
4649
private final List<ModularSlot> slots = new ArrayList<>();
4750
private final List<ModularSlot> shiftClickSlots = new ArrayList<>();
4851
private ContainerCustomizer containerCustomizer;
52+
private Predicate<EntityPlayer> canInteractWith;
4953

5054
@SideOnly(Side.CLIENT)
5155
private ModularScreen optionalScreen;
5256

53-
public ModularContainer(EntityPlayer player, PanelSyncManager panelSyncManager, String mainPanelName) {
57+
public ModularContainer(EntityPlayer player, PanelSyncManager panelSyncManager, String mainPanelName, GuiData guiData) {
5458
this.player = player;
5559
this.syncManager = new ModularSyncManager(this);
5660
this.syncManager.construct(mainPanelName, panelSyncManager);
5761
this.containerCustomizer = panelSyncManager.getContainerCustomizer();
5862
if (this.containerCustomizer == null) {
5963
this.containerCustomizer = new ContainerCustomizer();
64+
panelSyncManager.setContainerCustomizer(this.containerCustomizer);
6065
}
6166
this.containerCustomizer.initialize(this);
67+
if (this.containerCustomizer.getCanInteractWith() == null) {
68+
if (guiData instanceof PosGuiData posGuiData) {
69+
panelSyncManager.canInteractWithinDefaultRange(posGuiData);
70+
} else {
71+
// store current pos of player
72+
// gui will close when the player somehow moves away while the gui is open
73+
double x = guiData.getPlayer().posX;
74+
double y = guiData.getPlayer().posY;
75+
double z = guiData.getPlayer().posZ;
76+
panelSyncManager.canInteractWithinDefaultRange(x, y, z);
77+
}
78+
}
79+
this.canInteractWith = this.containerCustomizer.getCanInteractWith();
6280
sortShiftClickSlots();
6381
}
6482

@@ -190,9 +208,13 @@ public List<ModularSlot> getShiftClickSlots() {
190208
return Collections.unmodifiableList(this.shiftClickSlots);
191209
}
192210

211+
public void setCanInteractWith(Predicate<EntityPlayer> canInteractWith) {
212+
this.canInteractWith = canInteractWith;
213+
}
214+
193215
@Override
194216
public boolean canInteractWith(@NotNull EntityPlayer playerIn) {
195-
return true;
217+
return this.canInteractWith.test(playerIn);
196218
}
197219

198220
@Override

src/main/java/com/cleanroommc/modularui/value/sync/PanelSyncManager.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.cleanroommc.modularui.ModularUI;
44
import com.cleanroommc.modularui.api.IPanelHandler;
5+
import com.cleanroommc.modularui.factory.PosGuiData;
56
import com.cleanroommc.modularui.screen.ContainerCustomizer;
67
import com.cleanroommc.modularui.screen.ModularContainer;
78
import com.cleanroommc.modularui.widgets.slot.ModularSlot;
@@ -10,6 +11,7 @@
1011
import net.minecraft.entity.player.EntityPlayer;
1112
import net.minecraft.item.ItemStack;
1213
import net.minecraft.network.PacketBuffer;
14+
import net.minecraft.util.math.BlockPos;
1315
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;
1416

1517
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
@@ -21,9 +23,12 @@
2123
import java.io.IOException;
2224
import java.util.*;
2325
import java.util.function.Consumer;
26+
import java.util.function.Predicate;
2427

2528
public class PanelSyncManager {
2629

30+
private static final double DEFAULT_INTERACT_RANGE = 8.0;
31+
2732
private final Map<String, SyncHandler> syncHandlers = new Object2ObjectLinkedOpenHashMap<>();
2833
private final Map<String, SlotGroup> slotGroups = new Object2ObjectOpenHashMap<>();
2934
private final Map<SyncHandler, String> reverseSyncHandlers = new Object2ObjectOpenHashMap<>();
@@ -127,6 +132,11 @@ public ContainerCustomizer getContainerCustomizer() {
127132
}
128133

129134
public void setContainerCustomizer(ContainerCustomizer containerCustomizer) {
135+
if (this.containerCustomizer != null &&
136+
this.containerCustomizer.getCanInteractWith() != null &&
137+
containerCustomizer.getCanInteractWith() == null) {
138+
containerCustomizer.setCanInteractWith(this.containerCustomizer.getCanInteractWith());
139+
}
130140
this.containerCustomizer = containerCustomizer;
131141
}
132142

@@ -254,6 +264,36 @@ public PanelSyncManager addCloseListener(Consumer<EntityPlayer> listener) {
254264
return this;
255265
}
256266

267+
public PanelSyncManager canInteractWith(Predicate<EntityPlayer> canInteractWith) {
268+
if (this.containerCustomizer == null) this.containerCustomizer = new ContainerCustomizer();
269+
this.containerCustomizer.setCanInteractWith(canInteractWith);
270+
return this;
271+
}
272+
273+
public PanelSyncManager canInteractWithinRange(double x, double y, double z, double range) {
274+
return canInteractWith(player -> player.getDistanceSq(x, y, z) <= range * range);
275+
}
276+
277+
public PanelSyncManager canInteractWithinRange(BlockPos pos, double range) {
278+
return canInteractWith(player -> player.getDistanceSqToCenter(pos) <= range * range);
279+
}
280+
281+
public PanelSyncManager canInteractWithinRange(PosGuiData guiData, double range) {
282+
return canInteractWithinRange(guiData.getX() + 0.5, guiData.getY() + 0.5, guiData.getZ() + 0.5, range);
283+
}
284+
285+
public PanelSyncManager canInteractWithinDefaultRange(double x, double y, double z) {
286+
return canInteractWithinRange(x, y, z, DEFAULT_INTERACT_RANGE);
287+
}
288+
289+
public PanelSyncManager canInteractWithinDefaultRange(BlockPos pos) {
290+
return canInteractWithinRange(pos, DEFAULT_INTERACT_RANGE);
291+
}
292+
293+
public PanelSyncManager canInteractWithinDefaultRange(PosGuiData guiData) {
294+
return canInteractWithinRange(guiData, DEFAULT_INTERACT_RANGE);
295+
}
296+
257297
public SlotGroup getSlotGroup(String name) {
258298
return this.slotGroups.get(name);
259299
}

0 commit comments

Comments
 (0)