-
Notifications
You must be signed in to change notification settings - Fork 497
/
LensPluggable.java
179 lines (148 loc) · 5.25 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
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
package buildcraft.transport.pluggable;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftTransport;
import buildcraft.api.core.EnumColor;
import buildcraft.api.core.render.ITextureStates;
import buildcraft.api.transport.IPipe;
import buildcraft.api.transport.IPipeTile;
import buildcraft.api.transport.pluggable.IPipePluggableRenderer;
import buildcraft.api.transport.pluggable.PipePluggable;
import buildcraft.core.lib.render.FakeBlock;
import buildcraft.core.lib.utils.ColorUtils;
import buildcraft.core.lib.utils.MatrixTranformations;
import buildcraft.transport.PipeIconProvider;
import buildcraft.transport.TravelingItem;
import buildcraft.transport.pipes.events.PipeEventItem;
public class LensPluggable extends PipePluggable {
public int color;
public boolean isFilter;
protected IPipeTile container;
private ForgeDirection side;
private static class LensPluggableRenderer implements IPipePluggableRenderer {
public static final IPipePluggableRenderer INSTANCE = new LensPluggableRenderer();
private static final float zFightOffset = 1 / 4096.0F;
private LensPluggableRenderer() {
}
@Override
public void renderPluggable(RenderBlocks renderblocks, IPipe pipe, ForgeDirection side, PipePluggable pipePluggable, ITextureStates blockStateMachine, int renderPass, int x, int y, int z) {
float[][] zeroState = new float[3][2];
// X START - END
zeroState[0][0] = 0.1875F;
zeroState[0][1] = 0.8125F;
// Y START - END
zeroState[1][0] = 0.000F;
zeroState[1][1] = 0.125F;
// Z START - END
zeroState[2][0] = 0.1875F;
zeroState[2][1] = 0.8125F;
if (renderPass == 1) {
for (int i = 0; i < 3; i++) {
zeroState[i][0] += zFightOffset;
zeroState[i][1] -= zFightOffset;
}
blockStateMachine.getTextureState().set(BuildCraftTransport.instance.pipeIconProvider.getIcon(PipeIconProvider.TYPE.PipeLensOverlay.ordinal()));
((FakeBlock) blockStateMachine).setColor(ColorUtils.getRGBColor(15 - ((LensPluggable) pipePluggable).color));
} else {
if (((LensPluggable) pipe).isFilter) {
blockStateMachine.getTextureState().set(BuildCraftTransport.instance.pipeIconProvider.getIcon(PipeIconProvider.TYPE.PipeFilter.ordinal()));
} else {
blockStateMachine.getTextureState().set(BuildCraftTransport.instance.pipeIconProvider.getIcon(PipeIconProvider.TYPE.PipeLens.ordinal()));
}
}
float[][] rotated = MatrixTranformations.deepClone(zeroState);
MatrixTranformations.transform(rotated, side);
renderblocks.setRenderBounds(rotated[0][0], rotated[1][0], rotated[2][0], rotated[0][1], rotated[1][1], rotated[2][1]);
renderblocks.renderStandardBlock(blockStateMachine.getBlock(), x, y, z);
((FakeBlock) blockStateMachine).setColor(0xFFFFFF);
}
}
public LensPluggable() {
}
public LensPluggable(ItemStack stack) {
color = stack.getItemDamage() & 15;
isFilter = stack.getItemDamage() >= 16;
}
@Override
public void validate(IPipeTile pipe, ForgeDirection direction) {
this.container = pipe;
this.side = direction;
}
@Override
public void invalidate() {
this.container = null;
this.side = ForgeDirection.UNKNOWN;
}
@Override
public ItemStack[] getDropItems(IPipeTile pipe) {
return new ItemStack[]{ new ItemStack(BuildCraftTransport.lensItem, 1, color | (isFilter ? 16 : 0)) };
}
@Override
public boolean isBlocking(IPipeTile pipe, ForgeDirection direction) {
return false;
}
@Override
public AxisAlignedBB getBoundingBox(ForgeDirection 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 AxisAlignedBB.getBoundingBox(bounds[0][0], bounds[1][0], bounds[2][0], bounds[0][1], bounds[1][1], bounds[2][1]);
}
@Override
public IPipePluggableRenderer getRenderer() {
return LensPluggableRenderer.INSTANCE;
}
@Override
public void readFromNBT(NBTTagCompound tag) {
color = tag.getByte("c");
isFilter = tag.getBoolean("f");
}
@Override
public void writeToNBT(NBTTagCompound tag) {
tag.setByte("c", (byte) color);
tag.setBoolean("f", isFilter);
}
@Override
public void writeData(ByteBuf data) {
data.writeByte(color | (isFilter ? 0x20 : 0));
}
@Override
public void readData(ByteBuf data) {
int flags = data.readUnsignedByte();
color = flags & 15;
isFilter = (flags & 0x20) > 0;
}
@Override
public boolean requiresRenderUpdate(PipePluggable o) {
LensPluggable other = (LensPluggable) o;
return other.color != color || other.isFilter != isFilter;
}
private void color(TravelingItem item) {
if ((item.toCenter && item.input.getOpposite() == side)
|| (!item.toCenter && item.output == side)) {
item.color = EnumColor.fromId(color);
}
}
public void eventHandler(PipeEventItem.ReachedEnd event) {
if (!isFilter) {
color(event.item);
}
}
public void eventHandler(PipeEventItem.Entered event) {
if (!isFilter) {
color(event.item);
}
}
}