Skip to content

Commit ad656de

Browse files
committed
Update data handling; addresses #438 and #450
Overhauls how saved data is loaded and serialized; removes some of the wrapping and odd handling previously done in CompactMachineServerData. Hopefully closes #450 and addresses the issues with #438.
1 parent 95564af commit ad656de

15 files changed

+181
-121
lines changed

src/main/java/com/robotgryphon/compactmachines/block/tiles/CompactMachineTile.java

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.robotgryphon.compactmachines.config.ServerConfig;
44
import com.robotgryphon.compactmachines.core.Registration;
55
import com.robotgryphon.compactmachines.data.CompactMachineCommonData;
6-
import com.robotgryphon.compactmachines.data.CompactMachineServerData;
6+
import com.robotgryphon.compactmachines.data.SavedMachineData;
77
import com.robotgryphon.compactmachines.data.machines.CompactMachinePlayerData;
88
import com.robotgryphon.compactmachines.data.machines.CompactMachineRegistrationData;
99
import com.robotgryphon.compactmachines.reference.Reference;
@@ -21,7 +21,6 @@
2121
import net.minecraft.util.Direction;
2222
import net.minecraft.util.math.BlockPos;
2323
import net.minecraft.util.math.ChunkPos;
24-
import net.minecraft.world.IWorld;
2524
import net.minecraft.world.chunk.IChunk;
2625
import net.minecraft.world.server.ServerWorld;
2726
import net.minecraftforge.common.capabilities.Capability;
@@ -31,10 +30,7 @@
3130

3231
import javax.annotation.Nonnull;
3332
import javax.annotation.Nullable;
34-
import java.util.HashSet;
35-
import java.util.Optional;
36-
import java.util.Set;
37-
import java.util.UUID;
33+
import java.util.*;
3834

3935
public class CompactMachineTile extends TileEntity implements ICapabilityProvider, ITickableTileEntity {
4036
public int machineId = -1;
@@ -47,33 +43,37 @@ public class CompactMachineTile extends TileEntity implements ICapabilityProvide
4743
protected boolean locked = false;
4844
protected Set<String> playerWhiteList;
4945

46+
@Nullable
47+
private CompactMachinePlayerData playerData;
48+
5049
public CompactMachineTile() {
5150
super(Registration.MACHINE_TILE_ENTITY.get());
5251

5352
playerWhiteList = new HashSet<>();
53+
playerData = null;
5454
}
5555

5656
@Override
5757
public void validate() {
5858
super.validate();
5959

60-
if(ServerConfig.MACHINE_CHUNKLOADING.get())
60+
if (ServerConfig.MACHINE_CHUNKLOADING.get())
6161
doChunkload(true);
6262
}
6363

6464
@Override
6565
public void onChunkUnloaded() {
6666
super.onChunkUnloaded();
6767

68-
if(ServerConfig.MACHINE_CHUNKLOADING.get())
68+
if (ServerConfig.MACHINE_CHUNKLOADING.get())
6969
doChunkload(false);
7070
}
7171

7272
@Override
7373
public void remove() {
7474
super.remove();
7575

76-
if(ServerConfig.MACHINE_CHUNKLOADING.get())
76+
if (ServerConfig.MACHINE_CHUNKLOADING.get())
7777
doChunkload(false);
7878
}
7979

@@ -143,31 +143,31 @@ public CompoundNBT write(CompoundNBT nbt) {
143143
@Nonnull
144144
@Override
145145
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
146-
if(world.isRemote())
146+
if (world.isRemote())
147147
return super.getCapability(cap, side);
148148

149149
ServerWorld serverWorld = (ServerWorld) world;
150150
ServerWorld compactWorld = serverWorld.getServer().getWorld(Registration.COMPACT_DIMENSION);
151-
if(compactWorld == null)
151+
if (compactWorld == null)
152152
return LazyOptional.empty();
153153

154154
Set<BlockPos> tunnelPositions = TunnelHelper.getTunnelsForMachineSide(this.machineId, serverWorld, side);
155-
if(tunnelPositions.isEmpty())
155+
if (tunnelPositions.isEmpty())
156156
return LazyOptional.empty();
157157

158-
for(BlockPos possibleTunnel : tunnelPositions) {
158+
for (BlockPos possibleTunnel : tunnelPositions) {
159159
TunnelWallTile tile = (TunnelWallTile) compactWorld.getTileEntity(possibleTunnel);
160-
if(tile == null)
160+
if (tile == null)
161161
continue;
162162

163163
Optional<TunnelDefinition> tunnel = tile.getTunnelDefinition();
164-
if(!tunnel.isPresent())
164+
if (!tunnel.isPresent())
165165
continue;
166166

167167
TunnelDefinition definition = tunnel.get();
168-
if(definition instanceof ICapableTunnel) {
168+
if (definition instanceof ICapableTunnel) {
169169
LazyOptional<T> capPoss = ((ICapableTunnel) definition).getInternalCapability(compactWorld, possibleTunnel, cap, side);
170-
if(capPoss.isPresent())
170+
if (capPoss.isPresent())
171171
return capPoss;
172172
}
173173
}
@@ -186,12 +186,11 @@ public CompoundNBT getUpdateTag() {
186186
CompoundNBT base = super.getUpdateTag();
187187
base.putInt("machine", this.machineId);
188188

189-
if(world instanceof ServerWorld) {
189+
if (world instanceof ServerWorld) {
190190
Optional<CompactMachinePlayerData> playerData = Optional.empty();
191191
try {
192-
playerData = CompactMachineServerData
193-
.getInstance(world.getServer())
194-
.getPlayerData(machineId);
192+
SavedMachineData machineData = SavedMachineData.getInstance(world.getServer());
193+
playerData = machineData.getData().getPlayerData(machineId);
195194
} catch (Exception e) {
196195
e.printStackTrace();
197196
}
@@ -201,7 +200,7 @@ public CompoundNBT getUpdateTag() {
201200
base.put("players", playerNbt);
202201
});
203202

204-
if(this.owner != null)
203+
if (this.owner != null)
205204
base.putUniqueId("owner", this.owner);
206205
}
207206

@@ -213,14 +212,13 @@ public void handleUpdateTag(BlockState state, CompoundNBT tag) {
213212
super.handleUpdateTag(state, tag);
214213

215214
this.machineId = tag.getInt("machine");
216-
if(tag.contains("players")) {
215+
if (tag.contains("players")) {
217216
CompoundNBT players = tag.getCompound("players");
218-
CompactMachinePlayerData playerData = CompactMachinePlayerData.fromNBT(players);
217+
playerData = CompactMachinePlayerData.fromNBT(players);
219218

220-
CompactMachineCommonData.getInstance().updatePlayerData(playerData);
221219
}
222220

223-
if(tag.contains("owner"))
221+
if (tag.contains("owner"))
224222
owner = tag.getUniqueId("owner");
225223
}
226224

@@ -253,13 +251,14 @@ public void setMachineId(int id) {
253251
}
254252

255253
public Optional<CompactMachineRegistrationData> getMachineData() {
256-
if(this.machineId == 0)
254+
if (this.machineId == 0)
257255
return Optional.empty();
258256

259-
if(world instanceof ServerWorld) {
260-
return CompactMachineServerData
261-
.getInstance(world.getServer())
262-
.getMachineData(this.machineId);
257+
if (world instanceof ServerWorld) {
258+
return Optional.ofNullable(world.getServer())
259+
.map(SavedMachineData::getInstance)
260+
.map(SavedMachineData::getData)
261+
.flatMap(d -> d.getMachineData(this.machineId));
263262
} else {
264263
return Optional.empty();
265264
}
@@ -274,7 +273,7 @@ public boolean hasPlayersInside() {
274273
}
275274

276275
protected void doChunkload(boolean force) {
277-
if(world.isRemote)
276+
if (world == null || world.isRemote)
278277
return;
279278

280279
getMachineData().ifPresent(data -> {
@@ -289,6 +288,16 @@ public void doPostPlaced() {
289288
doChunkload(true);
290289
}
291290

291+
public void handlePlayerLeft(UUID playerID) {
292+
if(this.playerData != null)
293+
this.playerData.removePlayer(playerID);
294+
}
295+
296+
public void handlePlayerEntered(UUID playerID) {
297+
if(this.playerData != null)
298+
this.playerData.addPlayer(playerID);
299+
}
300+
292301
/*
293302
* Chunk-Loading triggers
294303
*/

src/main/java/com/robotgryphon/compactmachines/block/tiles/TunnelWallTile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public Optional<DimensionalPosition> getConnectedPosition() {
101101
if (!regData.isPlacedInWorld())
102102
return Optional.empty();
103103

104-
DimensionalPosition machinePosition = regData.getOutsidePosition(serverWorld);
104+
DimensionalPosition machinePosition = regData.getOutsidePosition(serverWorld.getServer());
105105
if (machinePosition != null) {
106106
Vector3d o = machinePosition.getPosition();
107107
BlockPos machineOutPos = new BlockPos(o.x, o.y, o.z);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.robotgryphon.compactmachines.client.machine;
2+
3+
import com.robotgryphon.compactmachines.block.tiles.CompactMachineTile;
4+
import com.robotgryphon.compactmachines.network.MachinePlayersChangedPacket;
5+
import com.robotgryphon.compactmachines.teleportation.DimensionalPosition;
6+
import net.minecraft.client.Minecraft;
7+
import net.minecraft.client.world.ClientWorld;
8+
9+
import java.util.UUID;
10+
11+
public class MachinePlayerEventHandler {
12+
13+
public static void handlePlayerMachineChanged(UUID playerID,
14+
MachinePlayersChangedPacket.EnumPlayerChangeType changeType,
15+
DimensionalPosition pos) {
16+
ClientWorld w = Minecraft.getInstance().world;
17+
18+
if (w.getDimensionKey() != pos.getDimension())
19+
return;
20+
21+
CompactMachineTile tile = (CompactMachineTile) w.getTileEntity(pos.getBlockPosition());
22+
if (tile == null)
23+
return;
24+
25+
switch (changeType) {
26+
case EXITED:
27+
tile.handlePlayerLeft(playerID);
28+
break;
29+
30+
case ENTERED:
31+
tile.handlePlayerEntered(playerID);
32+
break;
33+
}
34+
}
35+
}

src/main/java/com/robotgryphon/compactmachines/data/CompactMachineClientData.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/main/java/com/robotgryphon/compactmachines/data/CompactMachineCommonData.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ public static CompactMachineCommonData getInstance() {
2626
return INSTANCE;
2727
}
2828

29-
public void markDirty() throws Exception {
30-
}
31-
3229
public static CompoundNBT serializePlayerData(CompactMachineCommonData shared, CompoundNBT nbt) {
3330
ListNBT playerList = shared.playerData.values()
3431
.stream()

src/main/java/com/robotgryphon/compactmachines/data/CompactMachineServerData.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,11 @@ public class CompactMachineServerData extends CompactMachineCommonData {
2121

2222
private Map<Integer, CompactMachineRegistrationData> machineData;
2323

24-
@Nullable
25-
private MinecraftServer server;
26-
2724
CompactMachineServerData() {
2825
super();
2926
this.machineData = new HashMap<>();
3027
}
3128

32-
CompactMachineServerData(MinecraftServer server) {
33-
this();
34-
this.server = server;
35-
}
36-
37-
public static CompactMachineServerData getInstance(MinecraftServer server) {
38-
CompactMachineServerData serverData = SavedMachineData
39-
.getMachineData(server)
40-
.getServerData();
41-
42-
serverData.server = server;
43-
return serverData;
44-
}
45-
4629
public static CompactMachineServerData fromNbt(CompoundNBT nbt) {
4730
CompactMachineServerData d = new CompactMachineServerData();
4831
d.deserializeNBT(nbt);
@@ -77,12 +60,6 @@ public void deserializeNBT(CompoundNBT nbt) {
7760
}
7861
}
7962

80-
@Override
81-
public void markDirty() {
82-
SavedMachineData machineData = SavedMachineData.getMachineData(server);
83-
machineData.markDirty();
84-
}
85-
8663
public int getNextMachineId() {
8764
return machineData.size() + 1;
8865
}
@@ -137,7 +114,6 @@ public void updateMachineData(CompactMachineRegistrationData d) {
137114
return;
138115

139116
machineData.replace(id, d);
140-
markDirty();
141117
}
142118

143119
public Optional<CompactMachineRegistrationData> getMachineData(int machineId) {

src/main/java/com/robotgryphon/compactmachines/data/SavedMachineData.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
public class SavedMachineData extends WorldSavedData {
1818

1919
public final static String DATA_NAME = CompactMachines.MOD_ID + "_machines";
20-
private static CompactMachineServerData SERVER_DATA;
20+
private static CompactMachineServerData DATA;
2121

2222
public SavedMachineData() {
2323
super(DATA_NAME);
2424
}
2525

2626
@Nonnull
27-
public static SavedMachineData getMachineData(MinecraftServer server) {
27+
public static SavedMachineData getInstance(MinecraftServer server) {
2828
ServerWorld compactWorld = server.getWorld(Registration.COMPACT_DIMENSION);
2929
if (compactWorld == null)
3030
{
@@ -35,24 +35,27 @@ public static SavedMachineData getMachineData(MinecraftServer server) {
3535
.getOrCreate(SavedMachineData::new, DATA_NAME);
3636
}
3737

38-
if(SERVER_DATA == null)
39-
SERVER_DATA = new CompactMachineServerData(server);
40-
4138
DimensionSavedDataManager sd = compactWorld.getSavedData();
4239
return sd.getOrCreate(SavedMachineData::new, DATA_NAME);
4340
}
4441

4542
@Override
4643
public void read(CompoundNBT nbt) {
47-
SERVER_DATA = CompactMachineServerData.fromNbt(nbt);
44+
DATA = CompactMachineServerData.fromNbt(nbt);
4845
}
4946

5047
@Override
5148
public CompoundNBT write(CompoundNBT compound) {
52-
return SERVER_DATA.serializeNBT(compound);
49+
if(DATA != null)
50+
return DATA.serializeNBT(compound);
51+
52+
return compound;
5353
}
5454

55-
public CompactMachineServerData getServerData() {
56-
return SERVER_DATA;
55+
public CompactMachineServerData getData() {
56+
if(DATA == null)
57+
DATA = new CompactMachineServerData();
58+
59+
return DATA;
5760
}
5861
}

src/main/java/com/robotgryphon/compactmachines/data/machines/CompactMachineRegistrationData.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.nbt.INBT;
88
import net.minecraft.nbt.IntArrayNBT;
99
import net.minecraft.nbt.NBTUtil;
10+
import net.minecraft.server.MinecraftServer;
1011
import net.minecraft.util.RegistryKey;
1112
import net.minecraft.util.math.BlockPos;
1213
import net.minecraft.util.math.vector.Vector3d;
@@ -132,10 +133,10 @@ public void setSpawnPoint(BlockPos position) {
132133
* Gets the position of the machine in-world. (Dimension and Position info)
133134
* @return
134135
*/
135-
public DimensionalPosition getOutsidePosition(ServerWorld server) {
136+
public DimensionalPosition getOutsidePosition(MinecraftServer server) {
136137
if(this.inPlayerInventory) {
137-
List<ServerPlayerEntity> players = server.getPlayers(p -> playerUUID == p.getGameProfile().getId());
138-
ServerPlayerEntity player = players.get(0);
138+
ServerPlayerEntity player = server.getPlayerList().getPlayerByUUID(playerUUID);
139+
139140
Vector3d positionVec = player.getPositionVec();
140141
RegistryKey<World> dimensionKey = player.world.getDimensionKey();
141142

0 commit comments

Comments
 (0)