33import com .robotgryphon .compactmachines .config .ServerConfig ;
44import com .robotgryphon .compactmachines .core .Registration ;
55import com .robotgryphon .compactmachines .data .CompactMachineCommonData ;
6- import com .robotgryphon .compactmachines .data .CompactMachineServerData ;
6+ import com .robotgryphon .compactmachines .data .SavedMachineData ;
77import com .robotgryphon .compactmachines .data .machines .CompactMachinePlayerData ;
88import com .robotgryphon .compactmachines .data .machines .CompactMachineRegistrationData ;
99import com .robotgryphon .compactmachines .reference .Reference ;
2121import net .minecraft .util .Direction ;
2222import net .minecraft .util .math .BlockPos ;
2323import net .minecraft .util .math .ChunkPos ;
24- import net .minecraft .world .IWorld ;
2524import net .minecraft .world .chunk .IChunk ;
2625import net .minecraft .world .server .ServerWorld ;
2726import net .minecraftforge .common .capabilities .Capability ;
3130
3231import javax .annotation .Nonnull ;
3332import 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
3935public 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 */
0 commit comments