Skip to content

Commit 922ddb4

Browse files
committed
More codec migrations, tests, API improvements
1 parent 9bc31d4 commit 922ddb4

File tree

23 files changed

+482
-591
lines changed

23 files changed

+482
-591
lines changed

src/main/java/com/robotgryphon/compactmachines/block/BlockCompactMachine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public EnumMachineSize getSize() {
354354

355355
@Override
356356
public void addProbeData(IProbeData data, PlayerEntity player, World world, BlockState state) {
357-
CompactMachineProvider.exec(data, player, world, state);
357+
CompactMachineProvider.exec(data, world);
358358
}
359359

360360
// 1.12.1 code

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

Lines changed: 41 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import com.robotgryphon.compactmachines.config.ServerConfig;
44
import com.robotgryphon.compactmachines.core.Registration;
55
import com.robotgryphon.compactmachines.data.machine.CompactMachineInternalData;
6+
import com.robotgryphon.compactmachines.data.persistent.CompactRoomData;
7+
import com.robotgryphon.compactmachines.data.persistent.MachineConnections;
68
import 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;
910
import com.robotgryphon.compactmachines.reference.Reference;
1011
import com.robotgryphon.compactmachines.api.tunnels.TunnelDefinition;
1112
import com.robotgryphon.compactmachines.teleportation.DimensionalPosition;
@@ -23,7 +24,6 @@
2324
import net.minecraft.util.Direction;
2425
import net.minecraft.util.math.BlockPos;
2526
import net.minecraft.util.math.ChunkPos;
26-
import net.minecraft.world.chunk.IChunk;
2727
import net.minecraft.world.server.ServerWorld;
2828
import net.minecraftforge.common.capabilities.Capability;
2929
import 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
}

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import com.robotgryphon.compactmachines.api.tunnels.TunnelDefinition;
66
import com.robotgryphon.compactmachines.block.walls.TunnelWallBlock;
77
import com.robotgryphon.compactmachines.core.Registration;
8-
import com.robotgryphon.compactmachines.data.persistent.ExternalMachineData;
8+
import com.robotgryphon.compactmachines.data.persistent.CompactMachineData;
9+
import com.robotgryphon.compactmachines.data.persistent.MachineConnections;
910
import com.robotgryphon.compactmachines.network.NetworkHandler;
1011
import com.robotgryphon.compactmachines.network.TunnelAddedPacket;
1112
import com.robotgryphon.compactmachines.teleportation.DimensionalPosition;
@@ -26,8 +27,8 @@
2627

2728
import javax.annotation.Nonnull;
2829
import javax.annotation.Nullable;
30+
import java.util.Collection;
2931
import java.util.Optional;
30-
import java.util.Set;
3132

3233
public class TunnelWallTile extends TileEntity {
3334

@@ -47,7 +48,7 @@ public void load(BlockState state, CompoundNBT nbt) {
4748
this.tunnelType = type;
4849
}
4950

50-
if(nbt.contains("machine")) {
51+
if (nbt.contains("machine")) {
5152
this.connectedMachine = nbt.getInt("machine");
5253
}
5354
}
@@ -76,7 +77,7 @@ public void handleUpdateTag(BlockState state, CompoundNBT tag) {
7677
this.tunnelType = new ResourceLocation(tag.getString("tunnel_type"));
7778
}
7879

79-
if(tag.contains("machine")) {
80+
if (tag.contains("machine")) {
8081
this.connectedMachine = tag.getInt("machine");
8182
}
8283
}
@@ -88,37 +89,36 @@ public Optional<DimensionalPosition> getConnectedPosition() {
8889
ServerWorld serverWorld = (ServerWorld) level;
8990
MinecraftServer serv = serverWorld.getServer();
9091

91-
ExternalMachineData extern = ExternalMachineData.get(serv);
92-
if(extern == null)
92+
MachineConnections connections = MachineConnections.get(serv);
93+
CompactMachineData extern = CompactMachineData.get(serv);
94+
if (connections == null || extern == null)
9395
return Optional.empty();
9496

9597
if (this.connectedMachine <= 0) {
96-
Optional<Integer> mid = tryFindExternalMachineByChunkPos(extern);
98+
Optional<Integer> mid = tryFindExternalMachineByChunkPos(connections);
9799

98100
// Map the results - either it found an ID and we can map, or it found nothing
99101
return mid.map(i -> {
100102
this.connectedMachine = i;
101-
DimensionalPosition pos = extern.getMachineLocation(i);
102-
if(pos == null)
103-
return null;
104-
105-
BlockPos bumped = pos.getBlockPosition().relative(getConnectedSide(), 1);
106-
return new DimensionalPosition(pos.getDimension(), bumped);
103+
Optional<DimensionalPosition> pos = extern.getMachineLocation(i);
104+
return pos.map(p -> {
105+
BlockPos bumped = p.getBlockPosition().relative(getConnectedSide(), 1);
106+
return new DimensionalPosition(p.getDimension(), bumped);
107+
}).orElse(null);
107108
});
108109
}
109110

110-
DimensionalPosition pos = extern.getMachineLocation(this.connectedMachine);
111-
if(pos == null)
112-
return Optional.empty();
113-
114-
BlockPos bumped = pos.getBlockPosition().relative(getConnectedSide(), 1);
115-
DimensionalPosition bdp = new DimensionalPosition(pos.getDimension(), bumped);
116-
return Optional.of(bdp);
111+
Optional<DimensionalPosition> pos = extern.getMachineLocation(this.connectedMachine);
112+
return pos.map(p -> {
113+
BlockPos bumped = p.getBlockPosition().relative(getConnectedSide(), 1);
114+
DimensionalPosition bdp = new DimensionalPosition(p.getDimension(), bumped);
115+
return bdp;
116+
});
117117
}
118118

119-
private Optional<Integer> tryFindExternalMachineByChunkPos(ExternalMachineData extern) {
119+
private Optional<Integer> tryFindExternalMachineByChunkPos(MachineConnections connections) {
120120
ChunkPos thisMachineChunk = new ChunkPos(worldPosition);
121-
Set<Integer> externalMachineIDs = extern.getExternalMachineIDs(thisMachineChunk);
121+
Collection<Integer> externalMachineIDs = connections.graph.getMachinesFor(thisMachineChunk);
122122

123123
// This shouldn't happen - there should always be at least one machine attached externally
124124
// If this DOES happen, it's probably a migration failure or the block was destroyed without notification

0 commit comments

Comments
 (0)