-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathCoverBehavior.java
More file actions
248 lines (205 loc) · 8.27 KB
/
CoverBehavior.java
File metadata and controls
248 lines (205 loc) · 8.27 KB
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
package gregtech.api.cover;
import codechicken.lib.raytracer.CuboidRayTraceResult;
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Cuboid6;
import codechicken.lib.vec.Matrix4;
import com.google.common.collect.Lists;
import gregtech.api.GTValues;
import gregtech.api.capability.ConfigurationContext;
import gregtech.api.capability.IConfigurable;
import gregtech.api.gui.IUIHolder;
import gregtech.api.items.metaitem.MetaItem;
import gregtech.api.items.metaitem.MetaItem.MetaValueItem;
import gregtech.api.render.SimpleSidedCubeRenderer.RenderSide;
import gregtech.api.render.Textures;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.List;
import java.util.function.Consumer;
/**
* Represents cover instance attached on the specific side of meta tile entity
* Cover filters out interaction and logic of meta tile entity
* <p>
* Can implement {@link net.minecraft.util.ITickable} to listen to meta tile entity updates
*/
public abstract class CoverBehavior implements IUIHolder, IConfigurable {
private CoverDefinition coverDefinition;
public final ICoverable coverHolder;
public final EnumFacing attachedSide;
private int redstoneSignalOutput;
public CoverBehavior(ICoverable coverHolder, EnumFacing attachedSide) {
this.coverHolder = coverHolder;
this.attachedSide = attachedSide;
}
final void setCoverDefinition(CoverDefinition coverDefinition) {
this.coverDefinition = coverDefinition;
}
public final CoverDefinition getCoverDefinition() {
return coverDefinition;
}
public final void setRedstoneSignalOutput(int redstoneSignalOutput) {
this.redstoneSignalOutput = redstoneSignalOutput;
coverHolder.notifyBlockUpdate();
coverHolder.markDirty();
}
public final int getRedstoneSignalOutput() {
return redstoneSignalOutput;
}
public final int getRedstoneSignalInput() {
return coverHolder.getInputRedstoneSignal(attachedSide, true);
}
public void onRedstoneInputSignalChange(int newSignalStrength) {
}
public boolean canConnectRedstone() {
return false;
}
public void writeToNBT(NBTTagCompound tagCompound) {
if(redstoneSignalOutput > 0) {
tagCompound.setInteger("RedstoneSignal", redstoneSignalOutput);
}
}
public void readFromNBT(NBTTagCompound tagCompound) {
this.redstoneSignalOutput = tagCompound.getInteger("RedstoneSignal");
}
@Override
public ResourceLocation getConfigurationID() {
return getCoverDefinition().getCoverId();
}
@Override
public String getConfigurationName() {
// FIXME: how to do this properly?
final ItemStack coverStack = getCoverDefinition().getDropItemStack();
final Item item = coverStack.getItem();
if (item instanceof MetaItem) {
MetaItem<?> metaItem = (MetaItem<?>) item;
MetaItem<?>.MetaValueItem metaValueItem = metaItem.getItem(coverStack);
return String.format("metaitem.%s.name", metaValueItem.unlocalizedName);
}
return String.format("%s.name", coverStack.getTranslationKey());
}
@Override
public NBTTagCompound copyConfiguration(final ConfigurationContext context) {
return new NBTTagCompound();
}
@Override
public void pasteConfiguration(final ConfigurationContext context, final NBTTagCompound configuration) {
// nothing by default
}
public void writeInitialSyncData(PacketBuffer packetBuffer) {
}
public void readInitialSyncData(PacketBuffer packetBuffer) {
}
public void readUpdateData(int id, PacketBuffer packetBuffer) {
}
public final void writeUpdateData(int id, Consumer<PacketBuffer> writer) {
coverHolder.writeCoverData(this, id, writer);
}
/**
* Called on server side to check whether cover can be attached to given meta tile entity
*
* @return true if cover can be attached, false otherwise
*/
public abstract boolean canAttach();
/**
* Will be called on server side after the cover attachment to the meta tile entity
* Cover can change it's internal state here and it will be synced to client with {@link #writeInitialSyncData(PacketBuffer)}
*
* @param itemStack the item cover was attached from
*/
public void onAttached(ItemStack itemStack) {
}
public boolean shouldCoverInteractWithOutputside() {
return false;
}
public ItemStack getPickItem() {
return coverDefinition.getDropItemStack();
}
public List<ItemStack> getDrops() {
return Lists.newArrayList(getPickItem());
}
/**
* Called prior to cover removing on the server side
* Will also be called during machine dismantling, as machine loses installed covers after that
*/
public void onRemoved() {
}
public boolean shouldRenderConnected() {
return true;
}
public boolean canPipePassThrough() {
return false;
}
public boolean canRenderBackside() {
return true;
}
public boolean onLeftClick(EntityPlayer entityPlayer, CuboidRayTraceResult hitResult) {
return false;
}
public EnumActionResult onRightClick(EntityPlayer playerIn, EnumHand hand, CuboidRayTraceResult hitResult) {
return EnumActionResult.PASS;
}
public EnumActionResult onScrewdriverClick(EntityPlayer playerIn, EnumHand hand, CuboidRayTraceResult hitResult) {
return EnumActionResult.PASS;
}
/**
* Will be called for each capability request to meta tile entity
* Cover can override meta tile entity capabilities, modify their values, or deny accessing them
*
* @param defaultValue value of the capability from meta tile entity itself
* @return result capability value external caller will receive
*/
public <T> T getCapability(Capability<T> capability, T defaultValue) {
return defaultValue;
}
/**
* Called on client side to render this cover on the machine's face
* It will be automatically translated to prevent Z-fighting with machine faces
*/
@SideOnly(Side.CLIENT)
public abstract void renderCover(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline, Cuboid6 plateBox, BlockRenderLayer layer);
@SideOnly(Side.CLIENT)
public boolean canRenderInLayer(BlockRenderLayer renderLayer) {
return renderLayer == BlockRenderLayer.CUTOUT;
}
@SideOnly(Side.CLIENT)
public void renderCoverPlate(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline, Cuboid6 plateBox, BlockRenderLayer layer) {
TextureAtlasSprite casingSide = getPlateSprite();
for (EnumFacing coverPlateSide : EnumFacing.VALUES) {
boolean isAttachedSide = attachedSide.getAxis() == coverPlateSide.getAxis();
if (isAttachedSide) {
Textures.renderFace(renderState, translation, pipeline, coverPlateSide, plateBox, casingSide);
} else if (coverHolder.getCoverAtSide(coverPlateSide) == null) {
Textures.renderFace(renderState, translation, pipeline, coverPlateSide, plateBox, casingSide);
}
}
}
@SideOnly(Side.CLIENT)
protected TextureAtlasSprite getPlateSprite() {
return Textures.VOLTAGE_CASINGS[GTValues.LV].getSpriteOnSide(RenderSide.SIDE);
}
@Override
public final boolean isValid() {
return coverHolder.isValid() && coverHolder.getCoverAtSide(attachedSide) == this;
}
@Override
public boolean isRemote() {
return coverHolder.getWorld().isRemote;
}
@Override
public final void markAsDirty() {
coverHolder.markDirty();
}
}