-
Notifications
You must be signed in to change notification settings - Fork 497
/
TravelingItem.java
211 lines (175 loc) · 5.65 KB
/
TravelingItem.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
/**
* Copyright (c) SpaceToad, 2011 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.transport;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.Position;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.EnumColor;
import java.util.EnumSet;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraftforge.common.ForgeDirection;
public class TravelingItem {
private static int maxId = 0;
protected float speed = 0.01F;
protected ItemStack item;
protected TileEntity container;
public double xCoord, yCoord, zCoord;
public final int id;
public boolean toCenter = true;
public EnumColor color;
public ForgeDirection input = ForgeDirection.UNKNOWN;
public ForgeDirection output = ForgeDirection.UNKNOWN;
public final EnumSet<ForgeDirection> blacklist = EnumSet.noneOf(ForgeDirection.class);
private NBTTagCompound extraData;
/* CONSTRUCTORS */
public TravelingItem() {
this(maxId < Short.MAX_VALUE ? ++maxId : (maxId = Short.MIN_VALUE));
}
public TravelingItem(int id) {
this.id = id;
}
public TravelingItem(double x, double y, double z, ItemStack stack) {
this();
this.xCoord = x;
this.yCoord = y;
this.zCoord = z;
this.item = stack.copy();
}
/* GETTING & SETTING */
public void setPosition(double x, double y, double z) {
this.xCoord = x;
this.yCoord = y;
this.zCoord = z;
}
public void movePosition(double x, double y, double z) {
this.xCoord += x;
this.yCoord += y;
this.zCoord += z;
}
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
}
public ItemStack getItemStack() {
return item;
}
public void setItemStack(ItemStack item) {
this.item = item;
}
public TileEntity getContainer() {
return container;
}
public void setContainer(TileEntity container) {
this.container = container;
}
public NBTTagCompound getExtraData() {
if (extraData == null)
extraData = new NBTTagCompound();
return extraData;
}
public boolean hasExtraData() {
return extraData != null;
}
public void reset() {
toCenter = true;
blacklist.clear();
input = ForgeDirection.UNKNOWN;
output = ForgeDirection.UNKNOWN;
}
/* SAVING & LOADING */
public void readFromNBT(NBTTagCompound data) {
setPosition(data.getDouble("x"), data.getDouble("y"), data.getDouble("z"));
setSpeed(data.getFloat("speed"));
setItemStack(ItemStack.loadItemStackFromNBT(data.getCompoundTag("Item")));
toCenter = data.getBoolean("toCenter");
input = ForgeDirection.getOrientation(data.getInteger("input"));
output = ForgeDirection.getOrientation(data.getInteger("output"));
byte c = data.getByte("color");
if (c != -1)
color = EnumColor.fromId(c);
if (data.hasKey("extraData"))
extraData = data.getCompoundTag("extraData");
}
public void writeToNBT(NBTTagCompound data) {
data.setDouble("x", xCoord);
data.setDouble("y", yCoord);
data.setDouble("z", zCoord);
data.setFloat("speed", getSpeed());
NBTTagCompound nbttagcompound2 = new NBTTagCompound();
getItemStack().writeToNBT(nbttagcompound2);
data.setCompoundTag("Item", nbttagcompound2);
data.setBoolean("toCenter", toCenter);
data.setInteger("input", input.ordinal());
data.setInteger("output", output.ordinal());
data.setByte("color", color != null ? (byte) color.ordinal() : -1);
if (extraData != null)
data.setTag("extraData", extraData);
}
public EntityItem toEntityItem(ForgeDirection dir) {
if (container != null && !CoreProxy.proxy.isRenderWorld(container.worldObj)) {
if (getItemStack().stackSize <= 0)
return null;
Position motion = new Position(0, 0, 0, dir);
motion.moveForwards(0.1 + getSpeed() * 2F);
EntityItem entityitem = new EntityItem(container.worldObj, xCoord, yCoord, zCoord, getItemStack());
entityitem.lifespan = BuildCraftCore.itemLifespan;
entityitem.delayBeforeCanPickup = 10;
float f3 = 0.00F + container.worldObj.rand.nextFloat() * 0.04F - 0.02F;
entityitem.motionX = (float) container.worldObj.rand.nextGaussian() * f3 + motion.x;
entityitem.motionY = (float) container.worldObj.rand.nextGaussian() * f3 + motion.y;
entityitem.motionZ = (float) container.worldObj.rand.nextGaussian() * f3 + +motion.z;
container.worldObj.spawnEntityInWorld(entityitem);
return entityitem;
}
return null;
}
public float getEntityBrightness(float f) {
int i = MathHelper.floor_double(xCoord);
int j = MathHelper.floor_double(zCoord);
if (container.worldObj.blockExists(i, 128 / 2, j)) {
double d = 0.66000000000000003D;
int k = MathHelper.floor_double(yCoord + d);
return container.worldObj.getLightBrightness(i, k, j);
} else
return 0.0F;
}
public boolean isCorrupted() {
return getItemStack() == null || getItemStack().stackSize <= 0 || Item.itemsList[getItemStack().itemID] == null;
}
/**
* @return the can this item be moved into this specific inventory type.
* (basic BuildCraft PipedItems always return true)
*/
public boolean canSinkTo(TileEntity entity) {
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 67 * hash + this.id;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final TravelingItem other = (TravelingItem) obj;
if (this.id != other.id)
return false;
return true;
}
}