-
Notifications
You must be signed in to change notification settings - Fork 497
/
ItemMapLocation.java
executable file
·339 lines (288 loc) · 11.1 KB
/
ItemMapLocation.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/** Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team http://www.mod-buildcraft.com
* <p/>
* 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;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import buildcraft.api.core.IAreaProvider;
import buildcraft.api.core.IBox;
import buildcraft.api.core.IPathProvider;
import buildcraft.api.core.IZone;
import buildcraft.api.items.IMapLocation;
import buildcraft.core.lib.items.ItemBuildCraft;
import buildcraft.core.lib.utils.BCStringUtils;
import buildcraft.core.lib.utils.ModelHelper;
import buildcraft.core.lib.utils.NBTUtils;
import buildcraft.robotics.ZonePlan;
public class ItemMapLocation extends ItemBuildCraft implements IMapLocation {
private static final String[] STORAGE_TAGS = "x,y,z,side,xMin,xMax,yMin,yMax,zMin,zMax,path,chunkMapping,name".split(",");
public ItemMapLocation() {
super(BCCreativeTab.get("main"));
setHasSubtypes(true);
}
@Override
public int getItemStackLimit(ItemStack stack) {
return MapLocationType.getFromStack(stack) == MapLocationType.CLEAN ? 16 : 1;
}
@Override
public void addInformation(ItemStack stack, EntityPlayer player, @SuppressWarnings("rawtypes") List list, boolean advanced) {
@SuppressWarnings("unchecked")
List<String> strings = list;
NBTTagCompound cpt = NBTUtils.getItemData(stack);
if (cpt.hasKey("name")) {
String name = cpt.getString("name");
if (name.length() > 0) {
strings.add(name);
}
}
MapLocationType type = MapLocationType.getFromStack(stack);
switch (type) {
case SPOT: {
int x = cpt.getInteger("x");
int y = cpt.getInteger("y");
int z = cpt.getInteger("z");
EnumFacing side = EnumFacing.values()[cpt.getByte("side")];
strings.add(BCStringUtils.localize("{" + x + ", " + y + ", " + z + ", " + side + "}"));
break;
}
case AREA: {
int x = cpt.getInteger("xMin");
int y = cpt.getInteger("yMin");
int z = cpt.getInteger("zMin");
int xLength = cpt.getInteger("xMax") - x + 1;
int yLength = cpt.getInteger("yMax") - y + 1;
int zLength = cpt.getInteger("zMax") - z + 1;
strings.add(BCStringUtils.localize("{" + x + ", " + y + ", " + z + "} + {" + xLength + " x " + yLength + " x " + zLength + "}"));
break;
}
case PATH: {
NBTTagList pathNBT = (NBTTagList) cpt.getTag("path");
BlockPos first = NBTUtils.readBlockPos(pathNBT.get(0));
int x = first.getX();
int y = first.getY();
int z = first.getZ();
strings.add(BCStringUtils.localize("{" + x + ", " + y + ", " + z + "} + " + (pathNBT.tagCount() - 1) + " elements"));
break;
}
default: {
break;
}
}
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
if (world.isRemote) {
return stack;
}
if (player.isSneaking()) {
return clearMarkerData(stack);
}
return stack;
}
private static ItemStack clearMarkerData(ItemStack stack) {
if (MapLocationType.getFromStack(stack) == MapLocationType.CLEAN) {
return stack;
}
NBTTagCompound nbt = NBTUtils.getItemData(stack);
for (String key : STORAGE_TAGS) {
nbt.removeTag(key);
}
if (nbt.hasNoTags()) {
stack.setTagCompound(null);
}
MapLocationType.CLEAN.setToStack(stack);
return stack;
}
@Override
public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float par8, float par9, float par10) {
if (world.isRemote) {
return false;
}
if (MapLocationType.getFromStack(stack) != MapLocationType.CLEAN) {
return false;
}
ItemStack modified = stack;
if (stack.stackSize > 1) {
modified = stack.copy();
stack.stackSize--;
modified.stackSize = 1;
}
TileEntity tile = world.getTileEntity(pos);
NBTTagCompound cpt = NBTUtils.getItemData(modified);
if (tile instanceof IPathProvider) {
MapLocationType.PATH.setToStack(modified);
NBTTagList pathNBT = new NBTTagList();
for (BlockPos index : ((IPathProvider) tile).getPath()) {
pathNBT.appendTag(NBTUtils.writeBlockPos(index));
}
cpt.setTag("path", pathNBT);
} else if (tile instanceof IAreaProvider) {
MapLocationType.AREA.setToStack(modified);
IAreaProvider areaTile = (IAreaProvider) tile;
cpt.setInteger("xMin", areaTile.min().getX());
cpt.setInteger("yMin", areaTile.min().getY());
cpt.setInteger("zMin", areaTile.min().getZ());
cpt.setInteger("xMax", areaTile.max().getX());
cpt.setInteger("yMax", areaTile.max().getY());
cpt.setInteger("zMax", areaTile.max().getZ());
} else {
MapLocationType.SPOT.setToStack(modified);
cpt.setByte("side", (byte) side.getIndex());
cpt.setInteger("x", pos.getX());
cpt.setInteger("y", pos.getY());
cpt.setInteger("z", pos.getZ());
}
if (stack != modified) {
if (!player.inventory.addItemStackToInventory(modified)) {
player.dropItem(modified, false, true);
} else {
player.inventoryContainer.detectAndSendChanges();
}
}
return true;
}
@Override
public IBox getBox(ItemStack item) {
MapLocationType type = MapLocationType.getFromStack(item);
switch (type) {
case AREA: {
return getAreaBox(item);
}
case SPOT: {
return getPointBox(item);
}
default: {
return null;
}
}
}
public static IBox getAreaBox(ItemStack item) {
NBTTagCompound cpt = NBTUtils.getItemData(item);
int xMin = cpt.getInteger("xMin");
int yMin = cpt.getInteger("yMin");
int zMin = cpt.getInteger("zMin");
BlockPos min = new BlockPos(xMin, yMin, zMin);
int xMax = cpt.getInteger("xMax");
int yMax = cpt.getInteger("yMax");
int zMax = cpt.getInteger("zMax");
BlockPos max = new BlockPos(xMax, yMax, zMax);
return new Box(min, max);
}
public static IBox getPointBox(ItemStack item) {
NBTTagCompound cpt = NBTUtils.getItemData(item);
MapLocationType type = MapLocationType.getFromStack(item);
switch (type) {
case SPOT: {
int x = cpt.getInteger("x");
int y = cpt.getInteger("y");
int z = cpt.getInteger("z");
BlockPos pos = new BlockPos(x, y, z);
return new Box(pos, pos);
}
default: {
return null;
}
}
}
public static EnumFacing getPointFace(ItemStack stack) {
NBTTagCompound cpt = NBTUtils.getItemData(stack);
return EnumFacing.VALUES[cpt.getByte("side")];
}
@Override
public EnumFacing getPointSide(ItemStack item) {
NBTTagCompound cpt = NBTUtils.getItemData(item);
MapLocationType type = MapLocationType.getFromStack(item);
if (type == MapLocationType.SPOT) {
return EnumFacing.values()[cpt.getByte("side")];
} else {
return null;
}
}
@Override
public BlockPos getPoint(ItemStack item) {
NBTTagCompound cpt = NBTUtils.getItemData(item);
MapLocationType type = MapLocationType.getFromStack(item);
if (type == MapLocationType.SPOT) {
return new BlockPos(cpt.getInteger("x"), cpt.getInteger("y"), cpt.getInteger("z"));
} else {
return null;
}
}
@Override
public IZone getZone(ItemStack item) {
NBTTagCompound cpt = NBTUtils.getItemData(item);
MapLocationType type = MapLocationType.getFromStack(item);
switch (type) {
case ZONE: {
ZonePlan plan = new ZonePlan();
plan.readFromNBT(cpt);
return plan;
}
case AREA: {
return getBox(item);
}
case PATH: {
return getPointBox(item);
}
default: {
return null;
}
}
}
@Override
public List<BlockPos> getPath(ItemStack item) {
NBTTagCompound cpt = NBTUtils.getItemData(item);
MapLocationType type = MapLocationType.getFromStack(item);
switch (type) {
case PATH: {
List<BlockPos> indexList = new ArrayList<>();
NBTTagList pathNBT = cpt.getTagList("path", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < pathNBT.tagCount(); i++) {
indexList.add(NBTUtils.readBlockPos(pathNBT.getCompoundTagAt(i)));
}
return indexList;
}
case SPOT: {
List<BlockPos> indexList = new ArrayList<>();
indexList.add(new BlockPos(cpt.getInteger("x"), cpt.getInteger("y"), cpt.getInteger("z")));
return indexList;
}
default: {
return null;
}
}
}
public static void setZone(ItemStack item, ZonePlan plan) {
NBTTagCompound cpt = NBTUtils.getItemData(item);
MapLocationType.ZONE.setToStack(item);
plan.writeToNBT(cpt);
}
@Override
public String getName(ItemStack item) {
return NBTUtils.getItemData(item).getString("name");
}
@Override
public boolean setName(ItemStack item, String name) {
NBTTagCompound cpt = NBTUtils.getItemData(item);
cpt.setString("name", name);
return true;
}
@Override
public void registerModels() {
ModelHelper.registerItemModel(this, MapLocationType.CLEAN.meta, "/clean");
ModelHelper.registerItemModel(this, MapLocationType.SPOT.meta, "/spot");
ModelHelper.registerItemModel(this, MapLocationType.AREA.meta, "/area");
ModelHelper.registerItemModel(this, MapLocationType.PATH.meta, "/path");
ModelHelper.registerItemModel(this, MapLocationType.ZONE.meta, "/zone");
}
}