Skip to content

Commit 62d1e70

Browse files
committed
improve holo syncing
1 parent c0d12e2 commit 62d1e70

3 files changed

Lines changed: 59 additions & 66 deletions

File tree

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.factory;
22

3+
import com.cleanroommc.modularui.ModularUI;
34
import com.cleanroommc.modularui.api.JeiSettings;
45
import com.cleanroommc.modularui.api.UIFactory;
56
import com.cleanroommc.modularui.holoui.HoloUI;
@@ -12,10 +13,8 @@
1213

1314
import net.minecraft.client.Minecraft;
1415
import net.minecraft.client.entity.EntityPlayerSP;
15-
import net.minecraft.entity.player.EntityPlayer;
1616
import net.minecraft.entity.player.EntityPlayerMP;
1717
import net.minecraft.network.PacketBuffer;
18-
import net.minecraft.util.ResourceLocation;
1918
import net.minecraftforge.client.event.GuiOpenEvent;
2019
import net.minecraftforge.common.MinecraftForge;
2120
import net.minecraftforge.common.util.FakePlayer;
@@ -28,32 +27,33 @@
2827
import io.netty.buffer.Unpooled;
2928
import org.jetbrains.annotations.NotNull;
3029

31-
import java.util.ArrayList;
32-
import java.util.List;
33-
3430
public class HoloGuiManager extends GuiManager {
3531

3632

3733
private static GuiScreenWrapper lastMui;
38-
private static final List<EntityPlayer> openedContainers = new ArrayList<>(4);
3934

4035
public static <T extends GuiData> void open(@NotNull UIFactory<T> factory, @NotNull T guiData, EntityPlayerMP player) {
41-
if (player instanceof FakePlayer || openedContainers.contains(player)) return;
42-
openedContainers.add(player);
36+
if (player instanceof FakePlayer) return;
4337
// create panel, collect sync handlers and create container
4438
guiData.setJeiSettings(JeiSettings.DUMMY);
4539
GuiSyncManager syncManager = new GuiSyncManager(player);
4640
ModularPanel panel = factory.createPanel(guiData, syncManager);
4741
WidgetTree.collectSyncValues(syncManager, panel);
4842
ModularContainer container = new ModularContainer(syncManager);
49-
HoloUI.registerSyncedHoloUI(new ResourceLocation("holo", panel.getName()), panel, null);
43+
HoloUI.builder()
44+
.inFrontOf(player, 5, true)
45+
.open(screen -> {
46+
screen.setContainer(container);
47+
screen.setPanel(panel);
48+
HoloUI.registerSyncedHoloUI(panel, screen);
49+
}, player.getEntityWorld());
5050
// sync to client
51-
player.getNextWindowId();
51+
// player.getNextWindowId();
5252
// player.closeContainer();
53-
int windowId = player.currentWindowId;
53+
// int windowId = player.currentWindowId;
5454
PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
5555
factory.writeGuiData(guiData, buffer);
56-
NetworkHandler.sendToPlayer(new OpenGuiPacket<>(windowId, factory, buffer), player);
56+
NetworkHandler.sendToPlayer(new OpenGuiPacket<>(0, factory, buffer), player);
5757
// open container // this mimics forge behaviour
5858
// player.openContainer = container;
5959
// player.openContainer.windowId = windowId;
@@ -74,11 +74,14 @@ public static <T extends GuiData> void open(int windowId, @NotNull UIFactory<T>
7474
screen.getContext().setJeiSettings(jeiSettings);
7575
GuiScreenWrapper guiScreenWrapper = new GuiScreenWrapper(new ModularContainer(syncManager), screen);
7676
guiScreenWrapper.inventorySlots.windowId = windowId;
77-
HoloUI.registerSyncedHoloUI(new ResourceLocation("holo", panel.getName()), panel, guiScreenWrapper);
7877
HoloUI.builder()
7978
// .screenScale(0.25f)
8079
.inFrontOf(player, 5, true)
81-
.open(guiScreenWrapper);
80+
.open(screen1 -> {
81+
screen1.setPanel(panel);
82+
screen1.setWrapper(guiScreenWrapper);
83+
HoloUI.registerSyncedHoloUI(panel, screen1);
84+
}, player.getEntityWorld());
8285
}
8386

8487
//todo make this a mixin instead of using event to cancel arm animation stuff

src/main/java/com/cleanroommc/modularui/holoui/HoloScreenEntity.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.cleanroommc.modularui.holoui;
22

3-
import com.cleanroommc.modularui.screen.GuiContainerWrapper;
4-
import com.cleanroommc.modularui.screen.ModularScreen;
3+
import com.cleanroommc.modularui.screen.*;
54

65
import net.minecraft.block.Block;
76
import net.minecraft.client.Minecraft;
@@ -26,7 +25,8 @@
2625
public class HoloScreenEntity extends Entity {
2726

2827
private GuiContainerWrapper wrapper;
29-
private ModularScreen screen;
28+
private ModularContainer container;
29+
private ModularPanel panel;
3030
private final Plane3D plane3D;
3131
private static final DataParameter<Byte> ORIENTATION = EntityDataManager.createKey(HoloScreenEntity.class, DataSerializers.BYTE);
3232

@@ -39,19 +39,24 @@ public HoloScreenEntity(World world) {
3939
this(world, new Plane3D());
4040
}
4141

42-
public void setScreen(ModularScreen screen) {
43-
this.screen = screen;
44-
this.screen.getContext().holoScreen = this;
45-
}
46-
4742
public void setWrapper(GuiContainerWrapper wrapper) {
48-
this.setScreen(wrapper.getScreen());
4943
this.wrapper = wrapper;
5044
this.wrapper.setWorldAndResolution(Minecraft.getMinecraft(), (int) this.plane3D.getWidth(), (int) this.plane3D.getHeight());
45+
this.getScreen().getContext().holoScreen = this;
46+
this.getScreen().getContext().isHoloScreen = true;
47+
setContainer(wrapper.getScreen().getContainer());
48+
}
49+
50+
public void setContainer(ModularContainer container) {
51+
this.container = container;
52+
}
53+
54+
public void setPanel(ModularPanel panel) {
55+
this.panel = panel;
5156
}
5257

5358
public ModularScreen getScreen() {
54-
return this.screen;
59+
return this.getWrapper().getScreen();
5560
}
5661

5762
public GuiContainerWrapper getWrapper() {
@@ -96,12 +101,16 @@ public void onEntityUpdate() {
96101
if (this.world.isRemote) {
97102
this.extinguish();
98103
int w = (int) this.plane3D.getWidth(), h = (int) this.plane3D.getHeight();
104+
if (this.wrapper == null) {
105+
this.getEntityWorld().removeEntity(this);
106+
return;
107+
}
99108
if (w != this.wrapper.width || h != this.wrapper.height) {
100109
this.wrapper.onResize(Minecraft.getMinecraft(), w, h);
101110
}
111+
this.wrapper.getScreen().onUpdate();
102112
}
103113

104-
this.screen.onUpdate();
105114
this.firstUpdate = false;
106115
this.world.profiler.endSection();
107116
}

src/main/java/com/cleanroommc/modularui/holoui/HoloUI.java

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
11
package com.cleanroommc.modularui.holoui;
22

3-
import com.cleanroommc.modularui.screen.GuiScreenWrapper;
43
import com.cleanroommc.modularui.screen.ModularPanel;
54

6-
import net.minecraft.client.Minecraft;
75
import net.minecraft.entity.player.EntityPlayer;
8-
import net.minecraft.util.ResourceLocation;
96
import net.minecraft.util.math.BlockPos;
107
import net.minecraft.util.math.Vec3d;
8+
import net.minecraft.world.World;
119

1210
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
1311
import org.jetbrains.annotations.ApiStatus;
14-
import org.jetbrains.annotations.Nullable;
1512

1613
import java.util.Map;
14+
import java.util.function.Consumer;
1715

1816
/**
1917
* Highly experimental
2018
*/
2119
@ApiStatus.Experimental
2220
public class HoloUI {
2321

24-
private final Map<ResourceLocation, Data> syncedHolos = new Object2ObjectOpenHashMap<>();
25-
private static final HoloUI INSTANCE = new HoloUI();
22+
private static final Map<String, HoloScreenEntity> syncedHolos = new Object2ObjectOpenHashMap<>();
2623

27-
public static void registerSyncedHoloUI(ResourceLocation loc, ModularPanel mainPanel, GuiScreenWrapper screenWrapperSupplier) {
28-
var old = INSTANCE.register(loc, mainPanel, screenWrapperSupplier);
29-
// if (old != null && old.getPanel() != null) {
30-
// old.getPanel().closeIfOpen(true);
31-
// }
24+
public static void registerSyncedHoloUI(ModularPanel mainPanel, HoloScreenEntity entity) {
25+
syncedHolos.put(mainPanel.getName(), entity);
3226
}
3327

34-
private Data register(ResourceLocation location, ModularPanel panelSupplier, GuiScreenWrapper screenWrapperSupplier) {
35-
return this.syncedHolos.put(location, new Data(panelSupplier, screenWrapperSupplier));
28+
public static boolean isOpen(ModularPanel panel) {
29+
return syncedHolos.containsKey(panel.getName());
3630
}
3731

3832
public static Builder builder() {
@@ -97,40 +91,27 @@ public Builder plane(Plane3D plane) {
9791
return this;
9892
}
9993

100-
public void open(GuiScreenWrapper wrapper) {
94+
public void open(Consumer<HoloScreenEntity> entityConsumer, World world) {
10195
// JeiSettingsImpl jeiSettings = new JeiSettingsImpl();
10296
// jeiSettings.disableJei();
10397
// screen.getContext().setJeiSettings(jeiSettings);
104-
wrapper.getScreen().getContext().isHoloScreen = true;
105-
HoloScreenEntity holoScreenEntity = new HoloScreenEntity(Minecraft.getMinecraft().world, this.plane3D);
106-
holoScreenEntity.setPosition(this.x, this.y, this.z);
107-
holoScreenEntity.setWrapper(wrapper);
108-
holoScreenEntity.spawnInWorld();
109-
holoScreenEntity.setOrientation(this.orientation);
110-
}
111-
}
112-
113-
public static class Data {
114-
private final ModularPanel panelSupplier;
115-
@Nullable
116-
private final GuiScreenWrapper screenWrapperSupplier;
117-
118-
Data(ModularPanel panelSupplier, GuiScreenWrapper screenWrapperSupplier) {
119-
this.panelSupplier = panelSupplier;
120-
this.screenWrapperSupplier = screenWrapperSupplier;
121-
}
122-
123-
Data(ModularPanel panelSupplier) {
124-
this.panelSupplier = panelSupplier;
125-
this.screenWrapperSupplier = null;
126-
}
12798

128-
public ModularPanel getPanel() {
129-
return panelSupplier;
99+
// wrapper.getScreen().getContext().isHoloScreen = true;
100+
HoloScreenEntity screen = new HoloScreenEntity(world, this.plane3D);
101+
screen.setPosition(this.x, this.y, this.z);
102+
screen.setOrientation(this.orientation);
103+
entityConsumer.accept(screen);
104+
screen.spawnInWorld();
105+
// holoScreenEntity.setPosition(this.x, this.y, this.z);
106+
// holoScreenEntity.setWrapper(wrapper);
107+
// holoScreenEntity.spawnInWorld();
108+
// holoScreenEntity.setOrientation(this.orientation);
130109
}
131110

132-
public GuiScreenWrapper getScreenWrapper() {
133-
return screenWrapperSupplier;
111+
public void reposition(String name) {
112+
var screen = syncedHolos.get(name);
113+
screen.setPosition(this.x, this.y, this.z);
114+
screen.setOrientation(this.orientation);
134115
}
135116
}
136117
}

0 commit comments

Comments
 (0)