|
| 1 | +package pl.asie.computronics.integration.tis3d.serial; |
| 2 | + |
| 3 | +import li.cil.tis3d.api.serial.SerialInterface; |
| 4 | +import li.cil.tis3d.api.serial.SerialInterfaceProvider; |
| 5 | +import li.cil.tis3d.api.serial.SerialProtocolDocumentationReference; |
| 6 | +import net.minecraft.tileentity.TileEntity; |
| 7 | +import net.minecraft.util.EnumFacing; |
| 8 | +import net.minecraft.world.World; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author Sangar, Vexatos |
| 12 | + */ |
| 13 | +public abstract class TileInterfaceProvider implements SerialInterfaceProvider { |
| 14 | + |
| 15 | + protected final Class<?> tileClass; |
| 16 | + protected final String name, link; |
| 17 | + |
| 18 | + public TileInterfaceProvider(Class<?> tileClass, String name, String link) { |
| 19 | + this.tileClass = tileClass; |
| 20 | + this.name = name; |
| 21 | + this.link = link; |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public boolean worksWith(World world, int x, int y, int z, EnumFacing side) { |
| 26 | + if(tileClass == null) { |
| 27 | + // This can happen if filter classes are deduced by reflection and |
| 28 | + // the class in question is not present. |
| 29 | + return false; |
| 30 | + } |
| 31 | + final TileEntity tileEntity = world.getTileEntity(x, y, z); |
| 32 | + return tileEntity != null && tileClass.isAssignableFrom(tileEntity.getClass()); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public SerialProtocolDocumentationReference getDocumentationReference() { |
| 37 | + return new SerialProtocolDocumentationReference(name, link); |
| 38 | + } |
| 39 | + |
| 40 | + protected abstract boolean isStillValid(World world, int x, int y, int z, EnumFacing side, SerialInterface serialInterface, TileEntity tile); |
| 41 | + |
| 42 | + @Override |
| 43 | + public boolean isValid(World world, int x, int y, int z, EnumFacing side, SerialInterface serialInterface) { |
| 44 | + if(tileClass == null) { |
| 45 | + // This can happen if filter classes are deduced by reflection and |
| 46 | + // the class in question is not present. |
| 47 | + return false; |
| 48 | + } |
| 49 | + final TileEntity tile = world.getTileEntity(x, y, z); |
| 50 | + return tile != null && isStillValid(world, x, y, z, side, serialInterface, tile); |
| 51 | + } |
| 52 | +} |
0 commit comments