-
Notifications
You must be signed in to change notification settings - Fork 497
/
BlueprintBuilder.java
179 lines (149 loc) · 4.15 KB
/
BlueprintBuilder.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) SpaceToad, 2011-2012
* 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.builders.blueprints;
import buildcraft.api.builder.BlockHandler;
import buildcraft.api.core.IAreaProvider;
import buildcraft.builders.blueprints.BlueprintBuilder.SchematicBuilder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import static net.minecraftforge.common.ForgeDirection.EAST;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public class BlueprintBuilder implements IAreaProvider {
public final Blueprint blueprint;
public final ForgeDirection orientation;
public final World worldObj;
public final int x, y, z;
private final IInventory inv;
private final LinkedList<Schematic> buildList;
private final List<SchematicBuilder> builders;
public BlueprintBuilder(Blueprint blueprint, World world, int x, int y, int z, ForgeDirection orientation, IInventory inv) {
this.blueprint = blueprint;
this.orientation = orientation;
this.worldObj = world;
this.x = translateX(x, -blueprint.anchorX, -blueprint.anchorZ);
this.y = y - blueprint.anchorY;
this.z = translateZ(z, -blueprint.anchorX, -blueprint.anchorZ);
this.inv = inv;
this.buildList = blueprint.getBuildList();
builders = new ArrayList<SchematicBuilder>(buildList.size());
for (Schematic schematic : buildList) {
BlockHandler handler = schematic.getHandler();
if (handler != null)
builders.add(new SchematicBuilder(schematic, handler));
}
}
public List<SchematicBuilder> getBuilders() {
return Collections.unmodifiableList(builders);
}
@Override
public int xMin() {
return x;
}
@Override
public int yMin() {
return y;
}
@Override
public int zMin() {
return z;
}
@Override
public int xMax() {
return translateX(x, blueprint.sizeX - 1, blueprint.sizeZ - 1);
}
@Override
public int yMax() {
return y + blueprint.sizeY - 1;
}
@Override
public int zMax() {
return translateZ(z, blueprint.sizeX - 1, blueprint.sizeZ - 1);
}
@Override
public void removeFromWorld() {
}
private int translateX(int corner, int x, int z) {
switch (orientation) {
case SOUTH:
return corner - x;
case EAST:
return corner - z;
case WEST:
return corner + z;
default:
return corner + x;
}
}
private int translateZ(int corner, int x, int z) {
switch (orientation) {
case SOUTH:
return corner - z;
case EAST:
return corner + x;
case WEST:
return corner - x;
default:
return corner + z;
}
}
public class SchematicBuilder {
public final Schematic schematic;
public final BlockHandler handler;
private boolean complete;
private SchematicBuilder(Schematic schematic, BlockHandler handler) {
this.schematic = schematic;
this.handler = handler;
}
public int getX() {
return translateX(x, schematic.x, schematic.z);
}
public int getY() {
return y + schematic.y;
}
public int getZ() {
return translateZ(z, schematic.x, schematic.z);
}
public boolean blockExists() {
return handler.doesBlockMatchSchematic(worldObj, getX(), getY(), getZ(), orientation, schematic.data);
}
public boolean canBuild() {
return handler.canPlaceNow(worldObj, getX(), getY(), getZ(), orientation, schematic.data);
}
public boolean build(EntityPlayer bcPlayer) {
// if (blockExists()) {
// markComplete();
// return false;
// }
// if (!BlockUtil.canChangeBlock(worldObj, getX(), getY(), getZ()))
// return false;
if (!canBuild())
return false;
boolean built = handler.readBlockFromSchematic(worldObj, getX(), getY(), getZ(), orientation, schematic.data, inv, bcPlayer);
if (built) {
markComplete();
}
return built;
}
public boolean isComplete() {
return complete;
}
public void markComplete() {
complete = true;
}
}
}