-
Notifications
You must be signed in to change notification settings - Fork 497
/
FacadePluggable.java
196 lines (163 loc) · 6.01 KB
/
FacadePluggable.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
package buildcraft.transport;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumWorldBlockLayer;
import net.minecraftforge.common.util.Constants;
import buildcraft.api.transport.IPipe;
import buildcraft.api.transport.IPipeTile;
import buildcraft.api.transport.pluggable.IFacadePluggable;
import buildcraft.api.transport.pluggable.PipePluggable;
import buildcraft.core.lib.utils.MatrixTranformations;
import buildcraft.transport.client.model.ModelKeyFacade;
import io.netty.buffer.ByteBuf;
public class FacadePluggable extends PipePluggable implements IFacadePluggable {
public ItemFacade.FacadeState[] states;
private ItemFacade.FacadeState activeState;
private IPipeTile pipe;
// Client sync
private IBlockState state;
private boolean transparent, renderAsHollow;
public FacadePluggable(ItemFacade.FacadeState[] states) {
this.states = states;
prepareStates();
}
public FacadePluggable() {}
@Override
public void invalidate() {
this.pipe = null;
}
@Override
public void validate(IPipeTile pipe, EnumFacing direction) {
this.pipe = pipe;
}
@Override
public boolean requiresRenderUpdate(PipePluggable o) {
FacadePluggable other = (FacadePluggable) o;
return other.state != state || other.transparent != transparent || other.renderAsHollow != renderAsHollow;
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
if (states != null) {
nbt.setTag("states", ItemFacade.FacadeState.writeArray(states));
}
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
if (nbt.hasKey("states")) {
states = ItemFacade.FacadeState.readArray(nbt.getTagList("states", Constants.NBT.TAG_COMPOUND));
}
}
@Override
public ItemStack[] getDropItems(IPipeTile pipe) {
if (states != null) {
return new ItemStack[] { ItemFacade.getFacade(states) };
} else {
return new ItemStack[] { ItemFacade.getFacade(new ItemFacade.FacadeState(getCurrentState(), null, isHollow())) };
}
}
@Override
public boolean isBlocking(IPipeTile pipe, EnumFacing direction) {
return !isHollow();
}
@Override
public IBlockState getCurrentState() {
prepareStates();
return activeState == null ? state : activeState.state;
}
@Override
public boolean isTransparent() {
prepareStates();
return activeState == null ? transparent : activeState.transparent;
}
@Override
public boolean isHollow() {
prepareStates();
return activeState == null ? renderAsHollow : activeState.hollow;
}
@Override
public AxisAlignedBB getBoundingBox(EnumFacing side) {
float[][] bounds = new float[3][2];
// X START - END
bounds[0][0] = 0.0F;
bounds[0][1] = 1.0F;
// Y START - END
bounds[1][0] = 0.0F;
bounds[1][1] = TransportConstants.FACADE_THICKNESS;
// Z START - END
bounds[2][0] = 0.0F;
bounds[2][1] = 1.0F;
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]);
}
@Override
public boolean isSolidOnSide(IPipeTile pipe, EnumFacing direction) {
return !isHollow();
}
@Override
public ModelKeyFacade getModelRenderKey(EnumWorldBlockLayer layer, EnumFacing side) {
if (layer == EnumWorldBlockLayer.CUTOUT) {
return new ModelKeyFacade(side, state, isHollow());
}
return null;
}
@Override
public void writeData(ByteBuf data) {
prepareStates();
if (activeState == null || activeState.state == null) {
data.writeShort(0);
} else {
data.writeShort(Block.getIdFromBlock(activeState.state.getBlock()));
}
data.writeByte((activeState != null && activeState.transparent ? 128 : 0) | (activeState != null && activeState.hollow ? 64 : 0)
| (activeState == null ? 0 : activeState.state.getBlock().getMetaFromState(activeState.state)));
}
@Override
public void readData(ByteBuf data) {
int blockId = data.readUnsignedShort();
Block block;
if (blockId > 0) {
block = Block.getBlockById(blockId);
} else {
block = Blocks.stone;
}
int flags = data.readUnsignedByte();
int meta = flags & 0x0F;
state = block.getStateFromMeta(meta);
transparent = (flags & 0x80) > 0;
renderAsHollow = (flags & 0x40) > 0;
}
private void prepareStates() {
if (states != null && states.length > 1) {
if (pipe == null || pipe.getPipe() == null) {
activeState = states[0];
return;
}
IPipe p = pipe.getPipe();
int defaultStateId = -1;
int activeStateId = -1;
for (int i = 0; i < states.length; i++) {
ItemFacade.FacadeState state = states[i];
if (state.wire == null) {
defaultStateId = i;
continue;
}
if (p.isWireActive(state.wire)) {
activeStateId = i;
break;
}
}
activeState = activeStateId < 0 ? (defaultStateId < 0 ? states[0] : states[defaultStateId]) : states[activeStateId];
} else if (activeState == null) {
activeState = states != null && states.length > 0 ? states[0] : null;
}
}
public void setActiveState(int activeStateIndex) {
activeStateIndex = ((activeStateIndex % states.length) + activeStateIndex) % states.length;
activeState = states[activeStateIndex];
}
}