/
RedstoneMachineTileEntity.java
269 lines (206 loc) · 6.95 KB
/
RedstoneMachineTileEntity.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package slimeknights.tmechworks.common.blocks.tileentity;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.common.thread.EffectiveSide;
import slimeknights.mantle.tileentity.InventoryTileEntity;
import slimeknights.tmechworks.common.blocks.RedstoneMachineBlock;
import slimeknights.tmechworks.integration.IInformationProvider;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
public abstract class RedstoneMachineTileEntity extends InventoryTileEntity implements IInventory, IDisguisable, ITickableTileEntity, IInformationProvider {
private Inventory disguiseInventory;
private int redstoneState;
private boolean isFirstTick = true;
public RedstoneMachineTileEntity(TileEntityType<?> type, ITextComponent name, int inventorySize) {
this(type, name, inventorySize, 64);
}
public RedstoneMachineTileEntity(TileEntityType<?> type, ITextComponent name, int inventorySize, int maxStackSize) {
super(type, name, inventorySize, maxStackSize);
disguiseInventory = new Inventory(1);
}
/**
* Updates redstone state
*/
public void updateRedstone() {
if (isFirstTick)
return;
int oldPow = redstoneState;
int sidePow = 0;
Direction facing = getWorld().getBlockState(getPos()).get(RedstoneMachineBlock.FACING);
for (Direction face : Direction.values()) {
int pow = world.getRedstonePower(pos.offset(face), face);
if (face != facing && pow > 0) {
if (pow > sidePow) {
sidePow = pow;
}
}
}
int pow = world.getRedstonePower(pos, Direction.DOWN);
if (pow > sidePow) {
sidePow = pow;
}
redstoneState = sidePow;
if(pow != oldPow) {
onRedstoneUpdate();
}
onBlockUpdate();
if (oldPow != redstoneState) {
sync();
}
}
/**
* Called when the redstone state changes
*/
public void onRedstoneUpdate() {
}
/**
* Called when redstone state is updated, but the redstone state remains unchanged
*/
public void onBlockUpdate() {}
/**
* @return The redstone power level
*/
public int getRedstoneState() {
return redstoneState;
}
@Override
public void tick() {
if (isFirstTick) {
isFirstTick = false;
updateRedstone();
init();
sync();
}
}
/**
* Gets called the first tick this tile exists
*/
public void init() {
}
@Override
public ItemStack getDisguiseBlock() {
return disguiseInventory.getStackInSlot(0);
}
@Override
public void setDisguiseBlock(ItemStack disguise) {
disguiseInventory.setInventorySlotContents(0, disguise);
}
@Override
public boolean canEditDisguise() {
return true;
}
/**
* Writes inventory information
*/
public CompoundNBT writeItemData(CompoundNBT tags) {
tags.putInt("InventorySize", getSizeInventory());
writeInventoryToNBT(tags);
if(this.hasCustomName()) {
tags.putString("CustomName", ITextComponent.Serializer.toJson(this.inventoryTitle));
}
ItemStack disguise = getDisguiseBlock();
if (!disguise.isEmpty()) {
CompoundNBT itemNBT = new CompoundNBT();
itemNBT = disguise.write(itemNBT);
tags.put("Disguise", itemNBT);
}
return tags;
}
/**
* Reads inventory information
*/
public void readItemData(CompoundNBT tags) {
super.read(tags);
if(tags.contains("Disguise")){
CompoundNBT itemNBT = tags.getCompound("Disguise");
ItemStack disguise = ItemStack.read(itemNBT);
setDisguiseBlock(disguise);
}
}
@Override
@Nonnull
public CompoundNBT write(CompoundNBT tags) {
super.write(tags);
tags = writeItemData(tags);
tags.putInt("Redstone", redstoneState);
return tags;
}
@Override
public void read(CompoundNBT tags) {
readItemData(tags);
redstoneState = tags.getInt("Redstone");
}
@Override
@Nullable
public SUpdateTileEntityPacket getUpdatePacket() {
CompoundNBT tags = new CompoundNBT();
write(tags);
return new SUpdateTileEntityPacket(pos, 0, tags);
}
@Override
public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
CompoundNBT tags = pkt.getNbtCompound();
handleUpdateTag(tags);
}
@Override
public CompoundNBT getUpdateTag() {
return write(new CompoundNBT());
}
@Override
public void handleUpdateTag(@Nonnull CompoundNBT tag) {
read(tag);
// Mark block range for render update (if still needed)
}
public void sync() {
markDirty();
// Mark block range for render update (if still needed)
if (EffectiveSide.get() == LogicalSide.SERVER) {
SUpdateTileEntityPacket packetUpdateTileEntity = getUpdatePacket();
if (packetUpdateTileEntity == null) {
return;
}
for (PlayerEntity player : world.getPlayers()) {
((ServerPlayerEntity)player).connection.sendPacket(packetUpdateTileEntity);
}
}
}
/**
* Stores information in an itemstack
*/
public ItemStack storeTileData(ItemStack stack) {
CompoundNBT tags = writeItemData(new CompoundNBT());
stack.setTagInfo("BlockEntityTag", tags);
if(this.hasCustomName()){
CompoundNBT name = new CompoundNBT();
name.putString("Name", ITextComponent.Serializer.toJson(inventoryTitle));
stack.setTagInfo("display", name);
}
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());
}
@Nullable
@Override
public Container createMenu(int p_createMenu_1_, PlayerInventory p_createMenu_2_, PlayerEntity p_createMenu_3_) {
return null;
}
}