Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/main/java/mrjake/aunis/block/DHDBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,7 @@ public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, Entity
world.setBlockState(pos, state.withProperty(AunisProps.ROTATION_HORIZONTAL, facing), 3);

DHDTile dhdTile = (DHDTile) world.getTileEntity(pos);
BlockPos closestGate = LinkingHelper.findClosestUnlinked(world, pos, LinkingHelper.getDhdRange(), AunisBlocks.STARGATE_MILKY_WAY_BASE_BLOCK);

if (closestGate != null) {
StargateMilkyWayBaseTile gateTile = (StargateMilkyWayBaseTile) world.getTileEntity(closestGate);

dhdTile.setLinkedGate(closestGate);
gateTile.setLinkedDHD(pos);
}
dhdTile.updateLinkStatus(world, pos);
}
}

Expand Down Expand Up @@ -149,7 +142,7 @@ public void breakBlock(World world, BlockPos pos, IBlockState state) {
StargateMilkyWayBaseTile gateTile = (StargateMilkyWayBaseTile) dhdTile.getLinkedGate(world);

if (gateTile != null)
gateTile.setLinkedDHD(null);
gateTile.setLinkedDHD(null, -1);

ItemHandlerHelper.dropInventoryItems(world, pos, dhdTile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null));
}
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/mrjake/aunis/block/TRControllerBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,7 @@ public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, Entity
TRControllerTile controllerTile = (TRControllerTile) world.getTileEntity(pos);

if (!world.isRemote) {
BlockPos closestRings = LinkingHelper.findClosestUnlinked(world, pos, new BlockPos(10, 5, 10), AunisBlocks.TRANSPORT_RINGS_BLOCK);

if (closestRings != null) {
TransportRingsTile ringsTile = (TransportRingsTile) world.getTileEntity(closestRings);

controllerTile.setLinkedRings(closestRings);
ringsTile.setLinkedController(pos);
}
controllerTile.updateLinkStatus();
}
}

Expand All @@ -107,7 +100,7 @@ public void breakBlock(World world, BlockPos pos, IBlockState state) {
TRControllerTile controllerTile = (TRControllerTile) world.getTileEntity(pos);

if (!world.isRemote && controllerTile.isLinked())
controllerTile.getLinkedRingsTile(world).setLinkedController(null);
controllerTile.getLinkedRingsTile(world).setLinkedController(null, -1);

super.breakBlock(world, pos, state);
}
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/mrjake/aunis/block/TransportRingsBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,7 @@ public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, Entity
TransportRingsTile ringsTile = (TransportRingsTile) world.getTileEntity(pos);

if (!world.isRemote) {
BlockPos closestController = LinkingHelper.findClosestUnlinked(world, pos, new BlockPos(10, 5, 10), AunisBlocks.TR_CONTROLLER_BLOCK);

if (closestController != null) {
TRControllerTile controllerTile = (TRControllerTile) world.getTileEntity(closestController);

controllerTile.setLinkedRings(pos);
ringsTile.setLinkedController(closestController);
}
ringsTile.updateLinkStatus();
}
}

Expand All @@ -74,7 +67,7 @@ public void breakBlock(World world, BlockPos pos, IBlockState state) {
TransportRingsTile ringsTile = (TransportRingsTile) world.getTileEntity(pos);

if (ringsTile.isLinked())
ringsTile.getLinkedControllerTile(world).setLinkedRings(null);
ringsTile.getLinkedControllerTile(world).setLinkedRings(null, -1);

ringsTile.removeAllRings();
}
Expand Down
41 changes: 37 additions & 4 deletions src/main/java/mrjake/aunis/tileentity/DHDTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class DHDTile extends TileEntity implements ILinkable, IUpgradable, State
// Gate linking

private BlockPos linkedGate = null;

private int linkId = -1;

@Override
public void rotate(Rotation rotation) {
Expand All @@ -71,8 +73,9 @@ public void rotate(Rotation rotation) {
world.setBlockState(pos, state.withProperty(AunisProps.ROTATION_HORIZONTAL, rotation.rotate(rotationOrig, 16)));
}

public void setLinkedGate(BlockPos gate) {
public void setLinkedGate(BlockPos gate, int linkId) {
this.linkedGate = gate;
this.linkId = linkId;

markDirty();
}
Expand All @@ -92,6 +95,24 @@ public StargateAbstractBaseTile getLinkedGate(IBlockAccess world) {
public boolean canLinkTo() {
return !isLinked();
}

@Override
public int getLinkId() {
return linkId;
}

public void updateLinkStatus(World world, BlockPos pos) {
BlockPos closestGate = LinkingHelper.findClosestUnlinked(world, pos, LinkingHelper.getDhdRange(), AunisBlocks.STARGATE_MILKY_WAY_BASE_BLOCK, this.getLinkId());
int linkId = -1;

if (closestGate != null) {
linkId = LinkingHelper.getLinkId();
StargateMilkyWayBaseTile gateTile = (StargateMilkyWayBaseTile) world.getTileEntity(closestGate);
gateTile.setLinkedDHD(pos, linkId);
}

setLinkedGate(closestGate, linkId);
}

// ---------------------------------------------------------------------------------------------------
// Renderer state
Expand All @@ -105,6 +126,8 @@ public DHDRendererState getRendererStateClient() {
// ---------------------------------------------------------------------------------------------------
// Loading and ticking

private BlockPos lastPos = BlockPos.ORIGIN;

private TargetPoint targetPoint;
private ReactorStateEnum reactorState = ReactorStateEnum.STANDBY;

Expand All @@ -127,13 +150,17 @@ public void onLoad() {
@Override
public void update() {
if (!world.isRemote) {

if (!lastPos.equals(pos)) {
lastPos = pos;
this.updateLinkStatus(world, pos);
}

// Has crystal
if (hasControlCrystal()) {
if (isLinked()) {
StargateAbstractBaseTile gateTile = getLinkedGate(world);
if (gateTile == null) {
setLinkedGate(null);
setLinkedGate(null, -1);
Aunis.logger.error("Gate didn't unlink properly, forcing...");
return;
}
Expand Down Expand Up @@ -527,8 +554,10 @@ else if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY && (facin

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
if (linkedGate != null)
if (linkedGate != null) {
compound.setLong("linkedGate", linkedGate.toLong());
compound.setInteger("linkId", linkId);
}

compound.setTag("itemStackHandler", itemStackHandler.serializeNBT());

Expand All @@ -548,6 +577,10 @@ public void readFromNBT(NBTTagCompound compound) {
if (linkedGate.equals(new BlockPos(0, 0, 0))) // 1.8 fix
linkedGate = null;
}

if (compound.hasKey("linkId")) {
linkId = compound.getInteger("linkId");
}

itemStackHandler.deserializeNBT(compound.getCompoundTag("itemStackHandler"));

Expand Down
39 changes: 35 additions & 4 deletions src/main/java/mrjake/aunis/tileentity/TRControllerTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public void update() {
if (world.getTotalWorldTime() % 40 == 0) {
biomeOverlay = BiomeOverlayEnum.updateBiomeOverlay(world, pos, SUPPORTED_OVERLAYS);
}

if (!lastPos.equals(pos)) {
lastPos = pos;

updateLinkStatus();
markDirty();
}
}
}

Expand All @@ -48,9 +55,11 @@ public void update() {
// ------------------------------------------------------------------------
// Rings
private BlockPos linkedRings;
private int linkId = -1;

public void setLinkedRings(BlockPos pos) {
public void setLinkedRings(BlockPos pos, int linkId) {
this.linkedRings = pos;
this.linkId = linkId;

markDirty();
}
Expand All @@ -71,22 +80,44 @@ public TransportRingsTile getLinkedRingsTile(World world) {
public boolean canLinkTo() {
return !isLinked();
}


@Override
public int getLinkId() {
return linkId;
}

public void updateLinkStatus() {
BlockPos closestRings = LinkingHelper.findClosestUnlinked(world, pos, new BlockPos(10, 5, 10), AunisBlocks.TRANSPORT_RINGS_BLOCK, linkId);
int linkId = -1;

if (closestRings != null) {
linkId = LinkingHelper.getLinkId();
TransportRingsTile ringsTile = (TransportRingsTile) world.getTileEntity(closestRings);
ringsTile.setLinkedController(pos, linkId);
}

setLinkedRings(closestRings, linkId);
}

// ------------------------------------------------------------------------
// NBT

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
if (linkedRings != null)
if (linkedRings != null) {
compound.setLong("linkedRings", linkedRings.toLong());
compound.setInteger("linkId", linkId);
}

return super.writeToNBT(compound);
}

@Override
public void readFromNBT(NBTTagCompound compound) {
if (compound.hasKey("linkedRings"))
if (compound.hasKey("linkedRings")) {
linkedRings = BlockPos.fromLong(compound.getLong("linkedRings"));
linkId = compound.getInteger("linkId");
}

super.readFromNBT(compound);
}
Expand Down
68 changes: 55 additions & 13 deletions src/main/java/mrjake/aunis/tileentity/TransportRingsTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public class TransportRingsTile extends TileEntity implements ITickable, Rendere
public void update() {
if (!world.isRemote) {
ScheduledTask.iterate(scheduledTasks, world.getTotalWorldTime());

if (!lastPos.equals(pos)) {
lastPos = pos;

getRings().setPos(pos);
setRingsParams(getRings().getAddress(), getRings().getName());
updateLinkStatus();

markDirty();
}
}
}

Expand Down Expand Up @@ -314,9 +324,11 @@ private void setBarrierBlocks(boolean set, boolean passable) {
// ---------------------------------------------------------------------------------
// Controller
private BlockPos linkedController;

public void setLinkedController(BlockPos pos) {
private int linkId = -1;

public void setLinkedController(BlockPos pos, int linkId) {
this.linkedController = pos;
this.linkId = linkId;

markDirty();
}
Expand All @@ -326,7 +338,7 @@ public BlockPos getLinkedController() {
}

public boolean isLinked() {
return linkedController != null;
return linkedController != null && world.getTileEntity(linkedController) instanceof TRControllerTile;
}

public TRControllerTile getLinkedControllerTile(World world) {
Expand All @@ -337,7 +349,25 @@ public TRControllerTile getLinkedControllerTile(World world) {
public boolean canLinkTo() {
return !isLinked();
}

@Override
public int getLinkId() {
return linkId;
}

public void updateLinkStatus() {
BlockPos closestController = LinkingHelper.findClosestUnlinked(world, pos, new BlockPos(10, 5, 10), AunisBlocks.TR_CONTROLLER_BLOCK, linkId);
int linkId = -1;

if (closestController != null) {
linkId = LinkingHelper.getLinkId();
TRControllerTile controllerTile = (TRControllerTile) world.getTileEntity(closestController);
controllerTile.setLinkedRings(pos, linkId);
}

setLinkedController(closestController, linkId);
}

// ---------------------------------------------------------------------------------
// Rings network
private TransportRings rings;
Expand Down Expand Up @@ -380,16 +410,24 @@ public void addRings(TransportRingsTile caller) {
}
}

public void removeRings(int address) {
if (ringsMap.remove(address) != null)
markDirty();
public void removeRingsFromMap(int address) {
if (ringsMap.remove(address) != null) markDirty();
}


public void removeRings(int address) {
TransportRings rings = ringsMap.get(address);
if (rings != null) {
TileEntity tile = world.getTileEntity(rings.getPos());
if (tile instanceof TransportRingsTile) {
((TransportRingsTile) tile).removeRingsFromMap(getRings().getAddress());
}
}
removeRingsFromMap(address);
}

public void removeAllRings() {
for (TransportRings rings : ringsMap.values()) {

TransportRingsTile ringsTile = (TransportRingsTile) world.getTileEntity(rings.getPos());
ringsTile.removeRings(getRings().getAddress());
for (int address : new ArrayList<>(ringsMap.keySet())) {
removeRings(address);
}
}

Expand Down Expand Up @@ -445,8 +483,10 @@ public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setTag("rendererState", rendererState.serializeNBT());

compound.setTag("ringsData", getRings().serializeNBT());
if (linkedController != null)
if (linkedController != null) {
compound.setLong("linkedController", linkedController.toLong());
compound.setInteger("linkId", linkId);
}

compound.setInteger("ringsMapLength", ringsMap.size());

Expand Down Expand Up @@ -510,8 +550,10 @@ public void readFromNBT(NBTTagCompound compound) {
if (compound.hasKey("ringsData"))
getRings().deserializeNBT(compound.getCompoundTag("ringsData"));

if (compound.hasKey("linkedController"))
if (compound.hasKey("linkedController")) {
linkedController = BlockPos.fromLong(compound.getLong("linkedController"));
linkId = compound.getInteger("linkId");
}

if (compound.hasKey("ringsMapLength")) {
int len = compound.getInteger("ringsMapLength");
Expand Down
Loading