-
Notifications
You must be signed in to change notification settings - Fork 497
/
BuildingSlotBlock.java
executable file
·179 lines (144 loc) · 4.67 KB
/
BuildingSlotBlock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* 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.util.LinkedList;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.Constants;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.MappingNotFoundException;
import buildcraft.api.blueprints.MappingRegistry;
import buildcraft.api.blueprints.SchematicBlockBase;
import buildcraft.api.blueprints.SchematicFactory;
import buildcraft.api.blueprints.SchematicMask;
import buildcraft.api.core.Position;
public class BuildingSlotBlock extends BuildingSlot {
public int x, y, z;
public SchematicBlockBase schematic;
public enum Mode {
ClearIfInvalid, Build
};
public Mode mode = Mode.Build;
public int buildStage = 0;
@Override
public SchematicBlockBase getSchematic () {
if (schematic == null) {
return new SchematicMask(false);
} else {
return schematic;
}
}
@Override
public void writeToWorld(IBuilderContext context) {
if (mode == Mode.ClearIfInvalid) {
if (!getSchematic().isAlreadyBuilt(context, x, y, z)) {
context.world().setBlockToAir(x, y, z);
}
} else {
try {
getSchematic().writeToWorld(context, x, y, z, stackConsumed);
// Once the schematic has been written, we're going to issue
// calls
// to various functions, in particular updating the tile entity.
// If these calls issue problems, in order to avoid corrupting
// the world, we're logging the problem and setting the block to
// air.
TileEntity e = context.world().getTileEntity(x, y, z);
if (e != null) {
e.updateEntity();
}
} catch (Throwable t) {
t.printStackTrace();
context.world().setBlockToAir(x, y, z);
}
}
}
@Override
public void postProcessing (IBuilderContext context) {
getSchematic().postProcessing(context, x, y, z);
}
@Override
public LinkedList<ItemStack> getRequirements (IBuilderContext context) {
if (mode == Mode.ClearIfInvalid) {
return new LinkedList<ItemStack>();
} else {
LinkedList<ItemStack> req = new LinkedList<ItemStack>();
getSchematic().writeRequirementsToWorld(context, req);
return req;
}
}
@Override
public Position getDestination () {
return new Position (x + 0.5, y + 0.5, z + 0.5);
}
@Override
public void writeCompleted (IBuilderContext context, double complete) {
if (mode == Mode.ClearIfInvalid) {
context.world().destroyBlockInWorldPartially(0, x, y, z,
(int) (complete * 10.0F) - 1);
}
}
@Override
public boolean isAlreadyBuilt(IBuilderContext context) {
return schematic.isAlreadyBuilt(context, x, y, z);
}
@Override
public void writeToNBT (NBTTagCompound nbt, MappingRegistry registry) {
nbt.setByte("mode", (byte) mode.ordinal());
nbt.setInteger("x", x);
nbt.setInteger("y", y);
nbt.setInteger("z", z);
if (schematic != null) {
NBTTagCompound schematicNBT = new NBTTagCompound();
SchematicFactory.getFactory(schematic.getClass())
.saveSchematicToWorldNBT(schematicNBT, schematic, registry);
nbt.setTag("schematic", schematicNBT);
}
NBTTagList nbtStacks = new NBTTagList ();
if (stackConsumed != null) {
for (ItemStack stack : stackConsumed) {
NBTTagCompound nbtStack = new NBTTagCompound();
stack.writeToNBT(nbtStack);
nbtStacks.appendTag(nbtStack);
}
}
nbt.setTag("stackConsumed", nbtStacks);
}
@Override
public void readFromNBT(NBTTagCompound nbt, MappingRegistry registry) throws MappingNotFoundException {
mode = Mode.values() [nbt.getByte("mode")];
x = nbt.getInteger("x");
y = nbt.getInteger("y");
z = nbt.getInteger("z");
if (nbt.hasKey("schematic")) {
schematic = (SchematicBlockBase) SchematicFactory
.createSchematicFromWorldNBT(nbt.getCompoundTag("schematic"), registry);
}
stackConsumed = new LinkedList<ItemStack>();
NBTTagList nbtStacks = nbt.getTagList("stackConsumed", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < nbtStacks.tagCount(); ++i) {
stackConsumed.add(ItemStack.loadItemStackFromNBT(nbtStacks
.getCompoundTagAt(i)));
}
}
@Override
public LinkedList<ItemStack> getStacksToDisplay() {
if (mode == Mode.ClearIfInvalid) {
return stackConsumed;
} else {
return getSchematic ().getStacksToDisplay (stackConsumed);
}
}
@Override
public int getEnergyRequirement() {
return schematic.getEnergyRequirement(stackConsumed);
}
}