-
Notifications
You must be signed in to change notification settings - Fork 497
/
LensPluggable.java
147 lines (124 loc) · 4.3 KB
/
LensPluggable.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
package buildcraft.transport.pluggable;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.EnumFacing;
import buildcraft.BuildCraftTransport;
import buildcraft.api.transport.IPipeTile;
import buildcraft.api.transport.pluggable.IPluggableStaticBaker;
import buildcraft.api.transport.pluggable.PipePluggable;
import buildcraft.core.lib.utils.MatrixTranformations;
import buildcraft.core.lib.utils.NBTUtils;
import buildcraft.transport.TravelingItem;
import buildcraft.transport.client.model.LensPluggableModel;
import buildcraft.transport.pipes.events.PipeEventItem;
import io.netty.buffer.ByteBuf;
public class LensPluggable extends PipePluggable {
public EnumDyeColor dyeColor;
public boolean isFilter;
protected IPipeTile container;
private EnumFacing side;
public LensPluggable() {
}
public LensPluggable(ItemStack stack) {
dyeColor = EnumDyeColor.byMetadata(stack.getItemDamage() & 15);
isFilter = stack.getItemDamage() >= 16;
if (stack.getItemDamage() >= 32) {
isFilter = stack.getItemDamage() == 33;
dyeColor = null;
}
}
@Override
public void validate(IPipeTile pipe, EnumFacing direction) {
this.container = pipe;
this.side = direction;
}
@Override
public void invalidate() {
this.container = null;
this.side = null;
}
@Override
public ItemStack[] getDropItems(IPipeTile pipe) {
int colourMeta;
if (dyeColor == null) {
colourMeta = isFilter ? 33 : 32;
} else {
colourMeta = dyeColor.getMetadata() | (isFilter ? 16 : 0);
}
return new ItemStack[] { new ItemStack(BuildCraftTransport.lensItem, 1, colourMeta) };
}
@Override
public boolean isBlocking(IPipeTile pipe, EnumFacing direction) {
return false;
}
@Override
public AxisAlignedBB getBoundingBox(EnumFacing side) {
float[][] bounds = new float[3][2];
// X START - END
bounds[0][0] = 0.25F - 0.0625F;
bounds[0][1] = 0.75F + 0.0625F;
// Y START - END
bounds[1][0] = 0.000F;
bounds[1][1] = 0.125F;
// Z START - END
bounds[2][0] = 0.25F - 0.0625F;
bounds[2][1] = 0.75F + 0.0625F;
MatrixTranformations.transform(bounds, side);
return new AxisAlignedBB(bounds[0][0], bounds[1][0], bounds[2][0], bounds[0][1], bounds[1][1], bounds[2][1]);
}
public IPluggableStaticBaker getRenderer() {
return LensPluggableModel.INSTANCE;
}
@Override
public void readFromNBT(NBTTagCompound tag) {
if (tag.hasKey("colour")) {
dyeColor = NBTUtils.readEnum(tag.getTag("colour"), EnumDyeColor.class);
} else {
dyeColor = EnumDyeColor.byMetadata(tag.getByte("c"));
}
isFilter = tag.getBoolean("f");
}
@Override
public void writeToNBT(NBTTagCompound tag) {
tag.setTag("colour", NBTUtils.writeEnum(dyeColor));
tag.setBoolean("f", isFilter);
}
@Override
public void writeData(ByteBuf data) {
int col = dyeColor == null ? 0 : dyeColor.getMetadata() + 1;
data.writeByte((col & 0x1F) | (isFilter ? 0x20 : 0));
}
@Override
public void readData(ByteBuf data) {
int flags = data.readUnsignedByte();
int col = (flags & 0x1F);
if (col == 0) dyeColor = null;
else dyeColor = EnumDyeColor.byMetadata(col - 1);
isFilter = (flags & 0x20) > 0;
}
@Override
public boolean requiresRenderUpdate(PipePluggable o) {
LensPluggable other = (LensPluggable) o;
return other.dyeColor != dyeColor || other.isFilter != isFilter;
}
private void color(TravelingItem item) {
if ((item.toCenter && item.input.getOpposite() == side) || (!item.toCenter && item.output == side)) {
item.color = dyeColor;
}
}
public void eventHandler(PipeEventItem.ReachedEnd event) {
if (!isFilter) {
color(event.item);
}
}
public void eventHandler(PipeEventItem.Entered event) {
if (!isFilter) {
color(event.item);
}
}
public EnumDyeColor getColour() {
return dyeColor;
}
}