Skip to content

Commit baae66a

Browse files
committed
Added basic API to allow access to connected blocks
Someone in some issue somewhere said there is no support for a given connection type through tunnels, because compact machines does not have an API and only allows Capability based connections. This commit changes that. Other mods can now ask Machines and Tunnels to which WorldServer and BlockPos they are connected to. Maybe this results in more possible connection types and less spam in the issue inbox.
1 parent df462c7 commit baae66a

File tree

4 files changed

+102
-12
lines changed

4 files changed

+102
-12
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.dave.compactmachines3.api;
2+
3+
import net.minecraft.util.EnumFacing;
4+
import net.minecraft.util.math.BlockPos;
5+
import net.minecraft.world.WorldServer;
6+
import net.minecraftforge.fml.common.FMLCommonHandler;
7+
8+
import javax.annotation.Nullable;
9+
10+
/**
11+
* This interface is implemented on Machines and Tunnels.
12+
* The data provided here is only available on the server. Calling these
13+
* methods on the client will probably crash the game. Don't do it.
14+
*
15+
* If you need to know stuff about the other block (e.g. whether to render
16+
* a "connection-plug" or just continue with some cables) you have to
17+
* network that data yourself - but you probably are already syncing the
18+
* connection type of a blocks face from the server to the client anyway.
19+
*/
20+
public interface IRemoteBlockProvider {
21+
/**
22+
* Returns the position of the connected block. null if no block is connected.
23+
* Side is only relevant for machine blocks and is ignored for tunnel blocks.
24+
*
25+
* @param side
26+
* @return
27+
*/
28+
@Nullable
29+
BlockPos getConnectedBlockPosition(EnumFacing side);
30+
31+
/**
32+
* Returns the dimension id of the connected block.
33+
* Side is only relevant for machine blocks and is ignored for tunnel blocks.
34+
* On a machine block this always returns the id of the machine dimension (-144 by default)
35+
* This returns 0 if no block is connected, i.e. make sure to check the result of
36+
* getConnectedBlockPosition before using this information.
37+
*
38+
* @param side
39+
* @return
40+
*/
41+
int getConnectedDimensionId(EnumFacing side);
42+
43+
/**
44+
* Returns the WorldServer for the dimension of the connected block.
45+
* This is just a convenience wrapper around the getConnectedDimensionId method to
46+
* directly grab a reference to the world, which allows you to call getTileEntity
47+
* etc.
48+
*
49+
* Again and as the return value implies: Don't call this on a client.
50+
*
51+
* @param side
52+
* @return
53+
*/
54+
default WorldServer getConnectedDimension(EnumFacing side) {
55+
return FMLCommonHandler.instance().getMinecraftServerInstance().getWorld(this.getConnectedDimensionId(side));
56+
}
57+
}

src/main/java/org/dave/compactmachines3/block/BlockMachine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void neighborChanged(IBlockState state, World world, BlockPos pos, Block
135135
}
136136

137137
WorldServer machineWorld = DimensionTools.getServerMachineWorld();
138-
BlockPos neighborPos = te.getTunnelForSide(facing);
138+
BlockPos neighborPos = te.getConnectedBlockPosition(facing);
139139
if(neighborPos != null && machineWorld.getTileEntity(neighborPos) instanceof TileEntityTunnel) {
140140
machineWorld.notifyNeighborsOfStateChange(neighborPos, Blockss.tunnel, false);
141141
te.alreadyNotifiedOnTick = true;

src/main/java/org/dave/compactmachines3/tile/TileEntityMachine.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020
import net.minecraftforge.common.capabilities.ICapabilityProvider;
2121
import net.minecraftforge.common.util.Constants;
2222
import net.minecraftforge.fml.common.FMLCommonHandler;
23-
import net.minecraftforge.fml.common.network.NetworkRegistry;
24-
import org.dave.compactmachines3.CompactMachines3;
23+
import org.dave.compactmachines3.api.IRemoteBlockProvider;
2524
import org.dave.compactmachines3.block.BlockMachine;
2625
import org.dave.compactmachines3.integration.CapabilityNullHandlerRegistry;
2726
import org.dave.compactmachines3.misc.ConfigurationHandler;
28-
import org.dave.compactmachines3.network.MessageMachineChunk;
29-
import org.dave.compactmachines3.network.PackageHandler;
3027
import org.dave.compactmachines3.reference.EnumMachineSize;
3128
import org.dave.compactmachines3.utility.Logz;
3229
import org.dave.compactmachines3.world.ChunkLoadingMachines;
@@ -36,9 +33,12 @@
3633
import org.dave.compactmachines3.world.tools.SpawnTools;
3734
import org.dave.compactmachines3.world.tools.StructureTools;
3835

39-
import java.util.*;
36+
import java.util.HashMap;
37+
import java.util.HashSet;
38+
import java.util.Set;
39+
import java.util.UUID;
4040

41-
public class TileEntityMachine extends TileEntity implements ICapabilityProvider, ITickable {
41+
public class TileEntityMachine extends TileEntity implements ICapabilityProvider, ITickable, IRemoteBlockProvider {
4242
public int coords = -1;
4343
private boolean initialized = false;
4444
public boolean alreadyNotifiedOnTick = false;
@@ -359,7 +359,13 @@ public RedstoneTunnelData getRedstoneTunnelForSide(EnumFacing side) {
359359
return WorldSavedDataMachines.INSTANCE.redstoneTunnels.get(this.coords).get(side);
360360
}
361361

362-
public BlockPos getTunnelForSide(EnumFacing side) {
362+
@Override
363+
public int getConnectedDimensionId(EnumFacing side) {
364+
return ConfigurationHandler.Settings.dimensionId;
365+
}
366+
367+
@Override
368+
public BlockPos getConnectedBlockPosition(EnumFacing side) {
363369
if(WorldSavedDataMachines.INSTANCE == null || WorldSavedDataMachines.INSTANCE.tunnels == null) {
364370
return null;
365371
}
@@ -372,7 +378,7 @@ public BlockPos getTunnelForSide(EnumFacing side) {
372378
}
373379

374380
public BlockPos getMachineWorldInsetPos(EnumFacing facing) {
375-
BlockPos tunnelPos = this.getTunnelForSide(facing);
381+
BlockPos tunnelPos = this.getConnectedBlockPosition(facing);
376382
if(tunnelPos == null) {
377383
return null;
378384
}
@@ -478,7 +484,7 @@ public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
478484
return super.hasCapability(capability, facing);
479485
}
480486

481-
BlockPos tunnelPos = this.getTunnelForSide(facing);
487+
BlockPos tunnelPos = this.getConnectedBlockPosition(facing);
482488
if(tunnelPos == null) {
483489
return false;
484490
}
@@ -517,7 +523,7 @@ public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
517523
return super.getCapability(capability, facing);
518524
}
519525

520-
BlockPos tunnelPos = this.getTunnelForSide(facing);
526+
BlockPos tunnelPos = this.getConnectedBlockPosition(facing);
521527
if(tunnelPos == null) {
522528
return null;
523529
}

src/main/java/org/dave/compactmachines3/tile/TileEntityTunnel.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,40 @@
66
import net.minecraft.world.WorldServer;
77
import net.minecraftforge.common.capabilities.Capability;
88
import net.minecraftforge.common.capabilities.ICapabilityProvider;
9+
import org.dave.compactmachines3.api.IRemoteBlockProvider;
910
import org.dave.compactmachines3.integration.CapabilityNullHandlerRegistry;
1011
import org.dave.compactmachines3.utility.DimensionBlockPos;
1112
import org.dave.compactmachines3.world.WorldSavedDataMachines;
1213
import org.dave.compactmachines3.world.tools.DimensionTools;
1314
import org.dave.compactmachines3.world.tools.StructureTools;
1415

15-
public class TileEntityTunnel extends BaseTileEntityTunnel implements ICapabilityProvider {
16+
public class TileEntityTunnel extends BaseTileEntityTunnel implements ICapabilityProvider, IRemoteBlockProvider {
17+
18+
@Override
19+
public BlockPos getConnectedBlockPosition(EnumFacing side) {
20+
DimensionBlockPos dimpos = WorldSavedDataMachines.INSTANCE.machinePositions.get(StructureTools.getCoordsForPos(this.getPos()));
21+
if(dimpos == null) {
22+
return null;
23+
}
24+
25+
WorldServer realWorld = DimensionTools.getWorldServerForDimension(dimpos.getDimension());
26+
if(realWorld == null || !(realWorld.getTileEntity(dimpos.getBlockPos()) instanceof TileEntityMachine)) {
27+
return null;
28+
}
29+
30+
EnumFacing machineSide = this.getMachineSide();
31+
return dimpos.getBlockPos().offset(machineSide);
32+
}
33+
34+
@Override
35+
public int getConnectedDimensionId(EnumFacing side) {
36+
DimensionBlockPos dimpos = WorldSavedDataMachines.INSTANCE.machinePositions.get(StructureTools.getCoordsForPos(this.getPos()));
37+
if(dimpos == null) {
38+
return 0;
39+
}
40+
41+
return dimpos.getDimension();
42+
}
1643

1744
@Override
1845
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {

0 commit comments

Comments
 (0)