factorized out some of the code

This commit is contained in:
SpaceToad 2014-05-04 18:34:08 +02:00
parent 949d8054c4
commit e230d9a3f2
4 changed files with 36 additions and 60 deletions

View file

@ -175,12 +175,7 @@ public class BlueprintDatabase {
f.read (data);
f.close();
NBTTagCompound nbt = CompressedStreamTools.decompress(data);
BlueprintBase blueprint = BlueprintBase.loadBluePrint(nbt);
blueprint.setData(data);
return blueprint;
return load(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
@ -191,6 +186,21 @@ public class BlueprintDatabase {
return null;
}
public static BlueprintBase load(byte[] data) {
try {
NBTTagCompound nbt = CompressedStreamTools.decompress(data);
BlueprintBase blueprint = BlueprintBase.loadBluePrint(nbt);
blueprint.setData(data);
return blueprint;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public ArrayList<BlueprintId> getPage (int pageId) {
ArrayList<BlueprintId> result = new ArrayList<BlueprintId>();

View file

@ -38,7 +38,7 @@ public abstract class BptBuilderBase implements IAreaProvider {
protected TreeSet<BlockIndex> clearedLocations = new TreeSet<BlockIndex>();
protected TreeSet<BlockIndex> builtLocations = new TreeSet<BlockIndex>();
protected int x, y, z;
private boolean initialized = false;
protected boolean initialized = false;
public BptBuilderBase(BlueprintBase bluePrint, World world, int x, int y, int z) {
this.blueprint = bluePrint;

View file

@ -181,6 +181,11 @@ public class BptBuilderBlueprint extends BptBuilderBase {
}
public void deploy () {
if (!initialized) {
initialize();
initialized = true;
}
for (BuildingSlotBlock b : buildList) {
if (b.mode == Mode.ClearIfInvalid) {
context.world.setBlockToAir(b.x, b.y, b.z);

View file

@ -9,12 +9,11 @@
package buildcraft.core.blueprints;
import java.io.File;
import java.io.IOException;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blueprints.BlueprintDeployer;
import buildcraft.api.blueprints.Translation;
import buildcraft.builders.blueprints.BlueprintDatabase;
@ -27,7 +26,17 @@ public class RealBlueprintDeployer extends BlueprintDeployer {
public void deployBlueprint(World world, int x, int y, int z,
ForgeDirection dir, File file) {
Blueprint bpt = (Blueprint) BlueprintDatabase.load(file);
deployBlueprint(world, x, y, z, dir, (Blueprint) BlueprintDatabase.load(file));
}
@Override
public void deployBlueprintFromFileStream(World world, int x, int y, int z,
ForgeDirection dir, byte [] data) {
deployBlueprint(world, x, y, z, dir, (Blueprint) BlueprintDatabase.load(data));
}
private void deployBlueprint(World world, int x, int y, int z, ForgeDirection dir, Blueprint bpt) {
bpt.id = new BlueprintId();
bpt.id.kind = Kind.Blueprint;
@ -56,55 +65,7 @@ public class RealBlueprintDeployer extends BlueprintDeployer {
bpt.transformToWorld(transform);
BptBuilderBlueprint deployer = new BptBuilderBlueprint(bpt, world, x, y, z);
deployer.initialize();
deployer.deploy();
}
@Override
public void deployBlueprintFromFileStream(World world, int x, int y, int z,
ForgeDirection dir, byte [] data) {
NBTTagCompound nbt;
try {
nbt = CompressedStreamTools.decompress(data);
BlueprintBase blueprint = BlueprintBase.loadBluePrint(nbt);
Blueprint bpt = (Blueprint) blueprint;
bpt.setData(data);
bpt.id = new BlueprintId();
bpt.id.kind = Kind.Blueprint;
BptContext context = bpt.getContext(world, bpt.getBoxForPos(x, y, z));
if (bpt.rotate) {
if (dir == ForgeDirection.EAST) {
// Do nothing
} else if (dir == ForgeDirection.SOUTH) {
bpt.rotateLeft(context);
} else if (dir == ForgeDirection.WEST) {
bpt.rotateLeft(context);
bpt.rotateLeft(context);
} else if (dir == ForgeDirection.NORTH) {
bpt.rotateLeft(context);
bpt.rotateLeft(context);
bpt.rotateLeft(context);
}
}
Translation transform = new Translation();
transform.x = x - bpt.anchorX;
transform.y = y - bpt.anchorY;
transform.z = z - bpt.anchorZ;
bpt.transformToWorld(transform);
BptBuilderBlueprint deployer = new BptBuilderBlueprint(bpt, world, x, y, z);
deployer.initialize();
deployer.deploy();
} catch (IOException e) {
e.printStackTrace();
}
new BptBuilderBlueprint(bpt, world, x, y, z).deploy();
}
}