/
RedstoneMachineLogicBase.java
239 lines (178 loc) · 6.33 KB
/
RedstoneMachineLogicBase.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
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
package slimeknights.tmechworks.blocks.logic;
import com.google.common.base.Predicates;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.relauncher.Side;
import slimeknights.mantle.tileentity.TileInventory;
import slimeknights.tmechworks.integration.IInformationProvider;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
public abstract class RedstoneMachineLogicBase extends TileInventory implements IDisguisable, ITickable, IInformationProvider {
private InventoryBasic disguiseInventory;
private int redstoneState;
private boolean isFirstTick = true;
/**
* The facing direction of the redstone machine, some machines may choose to completely ignore this attribute
*/
private EnumFacing facingDirection = EnumFacing.NORTH;
public RedstoneMachineLogicBase(String name, int inventorySize) {
this(name, inventorySize, 64);
}
public RedstoneMachineLogicBase(String name, int inventorySize, int maxStackSize) {
super(name, inventorySize, maxStackSize);
disguiseInventory = new InventoryBasic(name + ".disguise", false, 1);
}
public void updateRedstone() {
if (isFirstTick)
return;
int oldPow = redstoneState;
int sidePow = 0;
for (EnumFacing face : EnumFacing.values()) {
int pow = world.getRedstonePower(pos.offset(face), face);
if (face != getFacingDirection() && pow > 0) {
if (pow > sidePow) {
sidePow = pow;
}
}
}
int pow = world.getRedstonePower(pos, EnumFacing.DOWN);
if (pow > sidePow) {
sidePow = pow;
}
redstoneState = sidePow;
if(pow != oldPow) {
onRedstoneUpdate();
}
onBlockUpdate();
if (oldPow != redstoneState) {
sync();
}
}
public void onRedstoneUpdate() {
}
public void onBlockUpdate() {}
public int getRedstoneState() {
return redstoneState;
}
public EnumFacing getFacingDirection() {
return facingDirection;
}
public void setFacingDirection(EnumFacing direction) {
facingDirection = direction;
markDirty();
}
@Override
public void update() {
if (isFirstTick) {
isFirstTick = false;
updateRedstone();
loadData();
sync();
}
}
public void loadData() {
}
@Override
public ItemStack getDisguiseBlock() {
return disguiseInventory.getStackInSlot(0);
}
@Override
public void setDisguiseBlock(ItemStack disguise) {
disguiseInventory.setInventorySlotContents(0, disguise);
}
@Override
public boolean canEditDisguise() {
return true;
}
public NBTTagCompound writeItemData(NBTTagCompound tags) {
tags.setInteger("InventorySize", getSizeInventory());
writeInventoryToNBT(tags);
if (this.hasCustomName()) {
tags.setString("CustomName", this.inventoryTitle);
}
ItemStack disguise = getDisguiseBlock();
if (!disguise.isEmpty()) {
NBTTagCompound itemNBT = new NBTTagCompound();
itemNBT = disguise.writeToNBT(itemNBT);
tags.setTag("Disguise", itemNBT);
}
return tags;
}
public void readItemData(NBTTagCompound tags) {
if (tags.hasKey("Disguise")) {
NBTTagCompound itemNBT = tags.getCompoundTag("Disguise");
ItemStack disguise = new ItemStack(itemNBT);
setDisguiseBlock(disguise);
}
}
@Override
@Nonnull
public NBTTagCompound writeToNBT(NBTTagCompound tags) {
tags = super.writeToNBT(tags);
tags = writeItemData(tags);
tags.setInteger("Redstone", redstoneState);
tags.setInteger("Facing", facingDirection.ordinal());
return tags;
}
@Override
public void readFromNBT(NBTTagCompound tags) {
super.readFromNBT(tags);
readItemData(tags);
redstoneState = tags.getInteger("Redstone");
facingDirection = EnumFacing.values()[tags.getInteger("Facing")];
}
@Override
@Nullable
public SPacketUpdateTileEntity getUpdatePacket() {
NBTTagCompound tags = new NBTTagCompound();
writeToNBT(tags);
return new SPacketUpdateTileEntity(pos, world.getBlockState(pos).getBlock().getMetaFromState(world.getBlockState(pos)), tags);
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
NBTTagCompound tags = pkt.getNbtCompound();
handleUpdateTag(tags);
}
@Override
public NBTTagCompound getUpdateTag() {
return writeToNBT(new NBTTagCompound());
}
@Override
public void handleUpdateTag(@Nonnull NBTTagCompound tag) {
readFromNBT(tag);
world.markBlockRangeForRenderUpdate(pos, pos);
}
public void sync() {
markDirty();
world.markBlockRangeForRenderUpdate(pos, pos);
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) {
SPacketUpdateTileEntity packetUpdateTileEntity = getUpdatePacket();
if (packetUpdateTileEntity == null) {
return;
}
for (EntityPlayerMP player : world.getPlayers(EntityPlayerMP.class, Predicates.alwaysTrue())) {
player.connection.sendPacket(packetUpdateTileEntity);
}
}
}
public ItemStack storeTileData(ItemStack stack) {
NBTTagCompound nbttagcompound = writeItemData(new NBTTagCompound());
stack.setTagInfo("BlockEntityTag", nbttagcompound);
return stack;
}
@Override
public void getInformation(@Nonnull List<String> info, InformationType type) {
if(type != InformationType.BODY)
return;
info.add(I18n.format("hud.msg.power") + ": " + getRedstoneState());
}
}