diff --git a/api/buildcraft/api/blueprints/BlueprintDeployer.java b/api/buildcraft/api/blueprints/BlueprintDeployer.java new file mode 100755 index 00000000..baca838b --- /dev/null +++ b/api/buildcraft/api/blueprints/BlueprintDeployer.java @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team + * http://www.mod-buildcraft.com + * + * BuildCraft is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * http://www.mod-buildcraft.com/MMPL-1.0.txt + */ +package buildcraft.api.blueprints; + +import java.io.File; + +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; + +/** + * This class is provided as a utility class for third-party mods that would + * like to easily deploy structures that are written in blueprints. It does + * not offer control on material that needs to get in, or how the structure + * is deployed, but allows to create contents of a blueprint in one cycle. + * Note that these functionalities will only work if BuildCraft is installed. + */ +public abstract class BlueprintDeployer { + + /** + * The deployed instantiated by BuildCraft. This is set by the BuildCraft + * builder mod. Mods that want to work with BuildCraft not installed should + * check for this value to be not null. + */ + public static BlueprintDeployer instance; + + /** + * Deploy the contents of the blueprints as if the builder was located at + * {x, y, z} facing the direction dir. + */ + public abstract void deployBlueprint(World world, int x, int y, int z, + ForgeDirection dir, File file); + +} diff --git a/common/buildcraft/BuildCraftBuilders.java b/common/buildcraft/BuildCraftBuilders.java index 5a6c7c12..abe0b6b1 100644 --- a/common/buildcraft/BuildCraftBuilders.java +++ b/common/buildcraft/BuildCraftBuilders.java @@ -24,6 +24,7 @@ import net.minecraftforge.client.event.TextureStitchEvent; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.config.Configuration; import net.minecraftforge.common.config.Property; +import buildcraft.api.blueprints.BlueprintDeployer; import buildcraft.api.blueprints.SchematicBlock; import buildcraft.api.blueprints.SchematicEntity; import buildcraft.api.blueprints.SchematicFactory; @@ -105,6 +106,7 @@ import buildcraft.builders.urbanism.UrbanistToolsIconProvider; import buildcraft.core.DefaultProps; import buildcraft.core.InterModComms; import buildcraft.core.Version; +import buildcraft.core.blueprints.RealBlueprintDeployer; import buildcraft.core.proxy.CoreProxy; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.event.FMLInitializationEvent; @@ -312,6 +314,8 @@ public class BuildCraftBuilders extends BuildCraftMod { SchematicFactory.registerSchematicFactory(SchematicMask.class, new SchematicFactoryMask()); SchematicFactory.registerSchematicFactory(SchematicEntity.class, new SchematicFactoryEntity()); + BlueprintDeployer.instance = new RealBlueprintDeployer(); + if (BuildCraftCore.loadDefaultRecipes) { loadRecipes(); } diff --git a/common/buildcraft/builders/blueprints/BlueprintDatabase.java b/common/buildcraft/builders/blueprints/BlueprintDatabase.java index c0da17c3..28170f24 100644 --- a/common/buildcraft/builders/blueprints/BlueprintDatabase.java +++ b/common/buildcraft/builders/blueprints/BlueprintDatabase.java @@ -156,8 +156,16 @@ public class BlueprintDatabase { return null; } - File blueprintFile = getBlueprintFile(id); + BlueprintBase bpt = load (getBlueprintFile(id)); + if (bpt != null) { + bpt.id = id; + } + + return bpt; + } + + public static BlueprintBase load (File blueprintFile) { if (blueprintFile.exists()) { try { FileInputStream f = new FileInputStream(blueprintFile); @@ -169,7 +177,6 @@ public class BlueprintDatabase { BlueprintBase blueprint = BlueprintBase.loadBluePrint(nbt); blueprint.setData(data); - blueprint.id = id; return blueprint; } catch (FileNotFoundException e) { diff --git a/common/buildcraft/core/blueprints/BptBuilderBlueprint.java b/common/buildcraft/core/blueprints/BptBuilderBlueprint.java index bafa8c90..51d5e47b 100644 --- a/common/buildcraft/core/blueprints/BptBuilderBlueprint.java +++ b/common/buildcraft/core/blueprints/BptBuilderBlueprint.java @@ -179,6 +179,58 @@ public class BptBuilderBlueprint extends BptBuilderBase { recomputeNeededItems(); } + public void deploy () { + for (BuildingSlotBlock b : buildList) { + if (b.mode == Mode.ClearIfInvalid) { + context.world.setBlockToAir(b.x, b.y, b.z); + } else { + b.stackConsumed = new LinkedList(); + + try { + for (ItemStack stk : b.getRequirements(context)) { + if (stk != null) { + b.stackConsumed.add(stk.copy()); + } + } + } catch (Throwable t) { + // Defensive code against errors in implementers + t.printStackTrace(); + BCLog.logger.throwing("BptBuilderBlueprint", "checkRequirements", t); + } + + b.writeToWorld(context); + } + } + + for (BuildingSlotEntity e : entityList) { + e.stackConsumed = new LinkedList(); + + try { + for (ItemStack stk : e.getRequirements(context)) { + if (stk != null) { + e.stackConsumed.add(stk.copy()); + } + } + } catch (Throwable t) { + // Defensive code against errors in implementers + t.printStackTrace(); + BCLog.logger.throwing("BptBuilderBlueprint", "checkRequirements", t); + } + + e.writeToWorld(context); + } + + for (BuildingSlotBlock b : buildList) { + if (b.mode != Mode.ClearIfInvalid) { + b.postProcessing(context); + } + } + + for (BuildingSlotEntity e : entityList) { + e.postProcessing(context); + } + } + private void checkDone() { recomputeNeededItems(); diff --git a/common/buildcraft/core/blueprints/RealBlueprintDeployer.java b/common/buildcraft/core/blueprints/RealBlueprintDeployer.java new file mode 100755 index 00000000..fd63fe6f --- /dev/null +++ b/common/buildcraft/core/blueprints/RealBlueprintDeployer.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team + * http://www.mod-buildcraft.com + * + * BuildCraft is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * http://www.mod-buildcraft.com/MMPL-1.0.txt + */ +package buildcraft.core.blueprints; + +import java.io.File; + +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; +import buildcraft.api.blueprints.BlueprintDeployer; +import buildcraft.builders.blueprints.BlueprintDatabase; +import buildcraft.builders.blueprints.BlueprintId; +import buildcraft.builders.blueprints.BlueprintId.Kind; + +public class RealBlueprintDeployer extends BlueprintDeployer { + + @Override + public void deployBlueprint(World world, int x, int y, int z, + ForgeDirection dir, File file) { + + Blueprint bpt = (Blueprint) BlueprintDatabase.load(file); + bpt.id = new BlueprintId(); + bpt.id.kind = Kind.Blueprint; + + new BptBuilderBlueprint(bpt, world, x, y, z).deploy (); + } + +}