finished initial save / load loop, for #1921

This commit is contained in:
SpaceToad 2014-06-29 18:57:46 +02:00
parent 27cf9473c6
commit 1ab3bcef10
9 changed files with 99 additions and 41 deletions

View file

@ -70,11 +70,21 @@ public class AIRobot {
public final void abort() {
abortDelegateAI();
end();
if (parentAI != null) {
parentAI.delegateAI = null;
parentAI.delegateAIAborted(this);
try {
end();
if (parentAI != null) {
parentAI.delegateAI = null;
parentAI.delegateAIAborted(this);
}
} catch (Throwable e) {
e.printStackTrace();
delegateAI = null;
if (parentAI != null) {
parentAI.delegateAI = null;
}
}
}

2
api/buildcraft/api/transport/IPipePluggable.java Normal file → Executable file
View file

@ -28,5 +28,5 @@ public interface IPipePluggable {
void invalidate();
void validate();
void validate(IPipeTile pipe, ForgeDirection direction);
}

View file

@ -41,6 +41,12 @@ public class AIRobotGotoDock extends AIRobot {
}
}
@Override
public void update() {
// this should not happen, always terminate in this situation
terminate();
}
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotGotoBlock) {

View file

@ -23,6 +23,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EntityDamageSource;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
import cpw.mods.fml.relauncher.Side;
@ -93,6 +94,11 @@ public class EntityRobot extends EntityRobotBase implements
private double mjStored;
private int loadedDockX = 0;
private int loadedDockY = 0;
private int loadedDockZ = 0;
private ForgeDirection loadedDockSide = null;
public EntityRobot(World world, NBTTagCompound boardNBT) {
this(world);
@ -217,18 +223,33 @@ public class EntityRobot extends EntityRobotBase implements
}
if (!worldObj.isRemote) {
mainAI.cycle();
if (linkedDockingStation == null) {
// try to load the docking station. If the chunk can be loaded
// but not the docking station, then the expecting docking
// station is not around, kill the robot.
Chunk chunk = worldObj.getChunkFromChunkCoords(loadedDockX >> 4, loadedDockZ >> 4);
if (mjStored <= 0) {
setDead();
if (chunk != null) {
DockingStation station = (DockingStation) DockingStationRegistry.getStation(
loadedDockX,
loadedDockY,
loadedDockZ,
loadedDockSide);
if (station == null) {
setDead();
} else {
linkToStation(station);
}
}
}
if (linkedDockingStation == null) {
// Defensive code. There should always be a station linked, but
// in case there's not (consecutive to a wrong load for
// example), just kill the robot.
if (linkedDockingStation != null) {
mainAI.cycle();
setDead();
if (mjStored <= 0) {
setDead();
}
}
}
@ -340,6 +361,11 @@ public class EntityRobot extends EntityRobotBase implements
nbt.setInteger("dockY", linkedDockingStation.pipe.yCoord);
nbt.setInteger("dockZ", linkedDockingStation.pipe.zCoord);
nbt.setInteger("dockSide", linkedDockingStation.side.ordinal());
} else {
nbt.setInteger("dockX", loadedDockX);
nbt.setInteger("dockY", loadedDockY);
nbt.setInteger("dockZ", loadedDockZ);
nbt.setInteger("dockSide", loadedDockSide.ordinal());
}
NBTTagCompound nbtLaser = new NBTTagCompound();
@ -348,6 +374,13 @@ public class EntityRobot extends EntityRobotBase implements
nbt.setDouble("mjStored", mjStored);
if (itemInUse != null) {
NBTTagCompound itemNBT = new NBTTagCompound();
itemInUse.writeToNBT(itemNBT);
nbt.setTag("itemInUse", itemNBT);
nbt.setBoolean("itemActive", itemActive);
}
for (int i = 0; i < inv.length; ++i) {
NBTTagCompound stackNbt = new NBTTagCompound();
@ -373,19 +406,20 @@ public class EntityRobot extends EntityRobotBase implements
public void readEntityFromNBT(NBTTagCompound nbt) {
super.readEntityFromNBT(nbt);
if (nbt.hasKey("dockX")) {
// FIXME: what happens if the chunk is not yet loaded?
linkedDockingStation = (DockingStation) DockingStationRegistry.getStation(
nbt.getInteger("dockX"),
nbt.getInteger("dockY"),
nbt.getInteger("dockZ"),
ForgeDirection.values()[nbt.getInteger("dockSide")]);
}
loadedDockX = nbt.getInteger("dockX");
loadedDockY = nbt.getInteger("dockY");
loadedDockZ = nbt.getInteger("dockZ");
loadedDockSide = ForgeDirection.values()[nbt.getInteger("dockSide")];
laser.readFromNBT(nbt.getCompoundTag("laser"));
mjStored = nbt.getDouble("mjStored");
if (nbt.hasKey("itemInUse")) {
itemInUse = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("itemInUse"));
itemActive = nbt.getBoolean("itemActive");
}
for (int i = 0; i < inv.length; ++i) {
inv[i] = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("inv[" + i + "]"));
}
@ -724,9 +758,18 @@ public class EntityRobot extends EntityRobotBase implements
@Override
public void setDead() {
if (!worldObj.isRemote && !isDead) {
// FIXME: placing a robot gives it full energy, so if it's dropped
// for lack of energy, it's a cheap way to refuel it. Find
// some other way to cope with that problem - such as a manual
// charger?
ItemStack robotStack = new ItemStack (BuildCraftSilicon.robotItem);
NBTUtils.getItemData(robotStack).setTag("board", originalBoardNBT);
entityDropItem(robotStack, 0);
if (linkedDockingStation != null) {
linkedDockingStation.unlink(this);
}
}
super.setDead();

2
common/buildcraft/transport/ItemFacade.java Normal file → Executable file
View file

@ -475,7 +475,7 @@ public class ItemFacade extends ItemBuildCraft {
}
@Override
public void validate() {
public void validate(IPipeTile pipe, ForgeDirection direction) {
}
}

2
common/buildcraft/transport/ItemPlug.java Normal file → Executable file
View file

@ -93,7 +93,7 @@ public class ItemPlug extends ItemBuildCraft {
}
@Override
public void validate() {
public void validate(IPipeTile pipe, ForgeDirection direction) {
}
}

View file

@ -57,6 +57,7 @@ public class ItemRobotStation extends ItemBuildCraft {
public static class RobotStationPluggable implements IPipePluggable {
private DockingStation station;
private boolean isValid = false;
public RobotStationPluggable() {
@ -79,22 +80,12 @@ public class ItemRobotStation extends ItemBuildCraft {
@Override
public void onAttachedPipe(IPipeTile pipe, ForgeDirection direction) {
TileGenericPipe realPipe = (TileGenericPipe) pipe;
if (!realPipe.getWorld().isRemote) {
station = new DockingStation(realPipe, direction);
validate();
//DockingStationRegistry.registerStation(station = new DockingStation(realPipe, direction));
}
validate(pipe, direction);
}
@Override
public void onDetachedPipe(IPipeTile pipe, ForgeDirection direction) {
invalidate();
/*
* TileGenericPipe realPipe = (TileGenericPipe) pipe; if
* (!realPipe.getWorld().isRemote && station != null) {
* DockingStationRegistry.removeStation(station); }
*/
}
public DockingStation getStation() {
@ -110,13 +101,19 @@ public class ItemRobotStation extends ItemBuildCraft {
public void invalidate() {
if (station != null && !station.pipe.getWorld().isRemote) {
DockingStationRegistry.removeStation(station);
isValid = false;
}
}
@Override
public void validate() {
if (station != null && !station.pipe.getWorld().isRemote) {
public void validate(IPipeTile pipe, ForgeDirection direction) {
if (!isValid && !((TileGenericPipe) pipe).getWorld().isRemote) {
if (station == null) {
station = new DockingStation((TileGenericPipe) pipe, direction);
}
DockingStationRegistry.registerStation(station);
isValid = true;
}
}
}

10
common/buildcraft/transport/TileGenericPipe.java Normal file → Executable file
View file

@ -246,10 +246,12 @@ public class TileGenericPipe extends TileEntity implements IPowerReceptor, IFlui
}
}
public void validate() {
for (IPipePluggable p : pluggables) {
public void validate(TileGenericPipe pipe) {
for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) {
IPipePluggable p = pluggables[d.ordinal()];
if (p != null) {
p.validate();
p.validate(pipe, d);
}
}
}
@ -332,7 +334,7 @@ public class TileGenericPipe extends TileEntity implements IPowerReceptor, IFlui
pipe.validate();
}
sideProperties.validate();
sideProperties.validate(this);
}
private void notifyBlockChanged() {

2
common/buildcraft/transport/gates/ItemGate.java Normal file → Executable file
View file

@ -183,7 +183,7 @@ public class ItemGate extends ItemBuildCraft {
}
@Override
public void validate() {
public void validate(IPipeTile pipe, ForgeDirection direction) {
}
}