33import com .robotgryphon .compactmachines .config .ServerConfig ;
44import com .robotgryphon .compactmachines .core .Registration ;
55import com .robotgryphon .compactmachines .data .machine .CompactMachineInternalData ;
6+ import com .robotgryphon .compactmachines .data .persistent .CompactRoomData ;
7+ import com .robotgryphon .compactmachines .data .persistent .MachineConnections ;
68import com .robotgryphon .compactmachines .data .player .CompactMachinePlayerData ;
7- import com .robotgryphon .compactmachines .data .persistent .ExternalMachineData ;
8- import com .robotgryphon .compactmachines .data .persistent .InternalMachineData ;
9+ import com .robotgryphon .compactmachines .data .persistent .CompactMachineData ;
910import com .robotgryphon .compactmachines .reference .Reference ;
1011import com .robotgryphon .compactmachines .api .tunnels .TunnelDefinition ;
1112import com .robotgryphon .compactmachines .teleportation .DimensionalPosition ;
2324import net .minecraft .util .Direction ;
2425import net .minecraft .util .math .BlockPos ;
2526import net .minecraft .util .math .ChunkPos ;
26- import net .minecraft .world .chunk .IChunk ;
2727import net .minecraft .world .server .ServerWorld ;
2828import net .minecraftforge .common .capabilities .Capability ;
2929import net .minecraftforge .common .capabilities .ICapabilityProvider ;
@@ -211,8 +211,15 @@ public CompoundNBT getUpdateTag() {
211211
212212 public Optional <ChunkPos > getInternalChunkPos () {
213213 if (level instanceof ServerWorld ) {
214- ExternalMachineData emd = ExternalMachineData .get (level .getServer ());
215- return emd .getChunkLocation (this .machineId );
214+ MinecraftServer serv = level .getServer ();
215+ if (serv == null )
216+ return Optional .empty ();
217+
218+ MachineConnections connections = MachineConnections .get (serv );
219+ if (connections == null )
220+ return Optional .empty ();
221+
222+ return connections .graph .getConnectedRoom (this .machineId );
216223 }
217224
218225 return Optional .empty ();
@@ -261,31 +268,6 @@ public void setMachineId(int id) {
261268 this .setChanged ();
262269 }
263270
264- public Optional <CompactMachineInternalData > getInternalData () {
265- if (this .machineId == 0 )
266- return Optional .empty ();
267-
268- if (level instanceof ServerWorld ) {
269- MinecraftServer serv = level .getServer ();
270- if (serv == null )
271- return Optional .empty ();
272-
273- // Get external machine info (which, this should have some if it has a machine ID
274- ExternalMachineData extern = ExternalMachineData .get (serv );
275- if (extern == null )
276- return Optional .empty ();
277-
278- Optional <ChunkPos > chunkLocation = extern .getChunkLocation (machineId );
279- if (!chunkLocation .isPresent ())
280- return Optional .empty ();
281-
282- InternalMachineData intern = InternalMachineData .get (serv );
283- return intern .forChunk (chunkLocation .get ());
284- } else {
285- return Optional .empty ();
286- }
287- }
288-
289271 public boolean hasPlayersInside () {
290272 return false ;
291273 // TODO
@@ -300,11 +282,10 @@ protected void doChunkload(boolean force) {
300282 if (level == null || level .isClientSide )
301283 return ;
302284
303- getInternalData ().ifPresent (data -> {
285+ getInternalChunkPos ().ifPresent (chunk -> {
304286 ServerWorld compact = this .level .getServer ().getLevel (Registration .COMPACT_DIMENSION );
305- IChunk machineChunk = compact .getChunk (data .getCenter ());
306- ChunkPos chunkPos = machineChunk .getPos ();
307- compact .setChunkForced (chunkPos .x , chunkPos .z , force );
287+ compact .setChunkForced (chunk .x , chunk .z , force );
288+
308289 });
309290 }
310291
@@ -321,7 +302,7 @@ public void doPostPlaced() {
321302 this .worldPosition
322303 );
323304
324- ExternalMachineData extern = ExternalMachineData .get (serv );
305+ CompactMachineData extern = CompactMachineData .get (serv );
325306 extern .setMachineLocation (this .machineId , dp );
326307
327308 doChunkload (true );
@@ -339,23 +320,34 @@ public void handlePlayerEntered(UUID playerID) {
339320 }
340321
341322 public boolean mapped () {
342- return getInternalData ().isPresent ();
323+ return getInternalChunkPos ().isPresent ();
343324 }
344325
345- /*
346- * Chunk-Loading triggers
347- */
326+ public Optional <BlockPos > getSpawn () {
327+ if (level instanceof ServerWorld ) {
328+ ServerWorld serverWorld = (ServerWorld ) level ;
329+ MinecraftServer serv = serverWorld .getServer ();
348330
349- // private void initialize() {
350- // if (this.getWorld().isRemote) {
351- // return;
352- // }
353- //
354- // if (!ChunkLoadingMachines.isMachineChunkLoaded(this.coords)) {
355- // ChunkLoadingMachines.forceChunk(this.coords);
356- // }
357- //
358- // }
331+ MachineConnections connections = MachineConnections .get (serv );
332+ if (connections == null )
333+ return Optional .empty ();
334+
335+ Optional <ChunkPos > connectedRoom = connections .graph .getConnectedRoom (machineId );
336+
337+ if (!connectedRoom .isPresent ())
338+ return Optional .empty ();
339+
340+ CompactRoomData roomData = CompactRoomData .get (serv );
341+ if (roomData == null )
342+ return Optional .empty ();
343+
344+ ChunkPos chunk = connectedRoom .get ();
345+ return roomData .forChunk (chunk )
346+ .map (CompactMachineInternalData ::getSpawn );
347+ }
348+
349+ return Optional .empty ();
350+ }
359351
360352// @Override
361353// public void update() {
@@ -419,137 +411,4 @@ public boolean mapped() {
419411//
420412// return StructureTools.getCoordsForPos(this.getPos()) == this.coords;
421413// }
422- //
423- // public ItemStack getConnectedPickBlock(EnumFacing facing) {
424- // BlockPos insetPos = getMachineWorldInsetPos(facing);
425- // if (insetPos == null) {
426- // return ItemStack.EMPTY;
427- // }
428- //
429- // WorldServer machineWorld = DimensionTools.getServerMachineWorld();
430- // IBlockState state = machineWorld.getBlockState(insetPos);
431- // return state.getBlock().getItem(machineWorld, insetPos, state);
432- // }
433- //
434- // public int getRedstonePowerOutput(EnumFacing facing) {
435- // if (this.coords == -1) {
436- // return 0;
437- // }
438- //
439- // // We don't know the actual power on the client-side, which does not have the worldsaveddatamachines instance
440- // if (WorldSavedDataMachines.INSTANCE == null || WorldSavedDataMachines.INSTANCE.redstoneTunnels == null) {
441- // return 0;
442- // }
443- //
444- // HashMap<EnumFacing, RedstoneTunnelData> tunnelMapping = WorldSavedDataMachines.INSTANCE.redstoneTunnels.get(this.coords);
445- // if (tunnelMapping == null) {
446- // return 0;
447- // }
448- //
449- // RedstoneTunnelData tunnelData = tunnelMapping.get(facing);
450- // if (tunnelData == null) {
451- // return 0;
452- // }
453- //
454- //
455- // if (!tunnelData.isOutput) {
456- // return 0;
457- // }
458- //
459- // WorldServer machineWorld = DimensionTools.getServerMachineWorld();
460- // if (!(machineWorld.getTileEntity(tunnelData.pos) instanceof TileEntityRedstoneTunnel)) {
461- // return 0;
462- // }
463- //
464- // EnumFacing insetDirection = StructureTools.getInsetWallFacing(tunnelData.pos, this.getSize().getDimension());
465- // BlockPos insetPos = tunnelData.pos.offset(insetDirection);
466- // IBlockState insetBlockState = machineWorld.getBlockState(insetPos);
467- //
468- // int power = 0;
469- // if (insetBlockState.getBlock() instanceof BlockRedstoneWire) {
470- // power = insetBlockState.getValue(BlockRedstoneWire.POWER);
471- // } else {
472- // power = machineWorld.getRedstonePower(insetPos, insetDirection);
473- // }
474- //
475- // return power;
476- // }
477- //
478- // @Override
479- // public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
480- // if (isInsideItself()) {
481- // return false;
482- // }
483- //
484- // if (world.isRemote || facing == null) {
485- // if (CapabilityNullHandlerRegistry.hasNullHandler(capability)) {
486- // return true;
487- // }
488- //
489- // return super.hasCapability(capability, facing);
490- // }
491- //
492- // BlockPos tunnelPos = this.getConnectedBlockPosition(facing);
493- // if (tunnelPos == null) {
494- // return false;
495- // }
496- //
497- // World machineWorld = DimensionTools.getServerMachineWorld();
498- // if (!(machineWorld.getTileEntity(tunnelPos) instanceof TileEntityTunnel)) {
499- // return false;
500- // }
501- //
502- // EnumFacing insetDirection = StructureTools.getInsetWallFacing(tunnelPos, this.getSize().getDimension());
503- // BlockPos insetPos = tunnelPos.offset(insetDirection);
504- //
505- // TileEntity te = machineWorld.getTileEntity(insetPos);
506- // if (te != null && te instanceof ICapabilityProvider && te.hasCapability(capability, insetDirection.getOpposite())) {
507- // return true;
508- // }
509- //
510- // if (CapabilityNullHandlerRegistry.hasNullHandler(capability)) {
511- // return true;
512- // }
513- //
514- // return false;
515- // }
516- //
517- // @Override
518- // public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
519- // if (isInsideItself()) {
520- // return null;
521- // }
522- //
523- // if (this.getWorld().isRemote || facing == null) {
524- // if (CapabilityNullHandlerRegistry.hasNullHandler(capability)) {
525- // return CapabilityNullHandlerRegistry.getNullHandler(capability);
526- // }
527- //
528- // return super.getCapability(capability, facing);
529- // }
530- //
531- // BlockPos tunnelPos = this.getConnectedBlockPosition(facing);
532- // if (tunnelPos == null) {
533- // return null;
534- // }
535- //
536- // WorldServer machineWorld = DimensionTools.getServerMachineWorld();
537- // if (!(machineWorld.getTileEntity(tunnelPos) instanceof TileEntityTunnel)) {
538- // return null;
539- // }
540- //
541- // EnumFacing insetDirection = StructureTools.getInsetWallFacing(tunnelPos, this.getSize().getDimension());
542- // BlockPos insetPos = tunnelPos.offset(insetDirection);
543- //
544- // TileEntity te = machineWorld.getTileEntity(insetPos);
545- // if (te instanceof ICapabilityProvider && te.hasCapability(capability, insetDirection.getOpposite())) {
546- // return machineWorld.getTileEntity(insetPos).getCapability(capability, insetDirection.getOpposite());
547- // }
548- //
549- // if (CapabilityNullHandlerRegistry.hasNullHandler(capability)) {
550- // return CapabilityNullHandlerRegistry.getNullHandler(capability);
551- // }
552- //
553- // return null;
554- // }
555414}
0 commit comments