/
DrawbridgeLogicBase.java
267 lines (204 loc) · 7.1 KB
/
DrawbridgeLogicBase.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
package slimeknights.tmechworks.blocks.logic;
import com.google.common.base.Predicates;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.SoundEvents;
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.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.FakePlayer;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import slimeknights.mantle.tileentity.TileInventory;
import slimeknights.tmechworks.library.Util;
public abstract class DrawbridgeLogicBase extends TileInventory implements IDisguisable, ITickable {
private static final float TICK_TIME = 0.05F;
private FakePlayer fakePlayer;
private DrawbridgeStats statistics = new DrawbridgeStats();
private int redstoneState;
private int extendState;
private boolean isExtended;
private boolean isExtending;
private float cooldown;
private boolean isFirstTick = true;
private EnumFacing facingDirection = EnumFacing.NORTH;
private long lastWorldTime;
public DrawbridgeLogicBase(String name, int inventorySize) {
super(name, inventorySize + 1);
}
public void updateRedstone() {
int oldPow = redstoneState;
int idPow = worldObj.isBlockIndirectlyGettingPowered(pos);
int sidePow = 0;
for(EnumFacing face : EnumFacing.HORIZONTALS) {
BlockPos miscPos = new BlockPos(pos.getX() + face.getFrontOffsetX(), pos.getY() + face
.getFrontOffsetY(), pos.getZ() + face.getFrontOffsetZ());
IBlockState miscState = worldObj.getBlockState(miscPos);
if(!miscState.canProvidePower()) {
continue;
}
int pow = miscState.getStrongPower(worldObj, miscPos, face.getOpposite());
if(pow > sidePow) {
sidePow = pow;
}
}
redstoneState = idPow > sidePow ? idPow : sidePow;
updateExtension();
if(oldPow != redstoneState) {
markDirty();
for(EntityPlayerMP player : worldObj.getPlayers(EntityPlayerMP.class, Predicates.alwaysTrue())) {
player.connection.sendPacket(getUpdatePacket());
}
}
}
public void updateExtension() {
if(isExtended && redstoneState <= 0) {
isExtended = false;
isExtending = true;
} else if(!isExtended && redstoneState > 0) {
isExtended = true;
isExtending = true;
}
}
public void playExtendSound() {
worldObj.playSound(null, (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos
.getZ() + 0.5D, SoundEvents.BLOCK_PISTON_EXTEND, SoundCategory.BLOCKS, 0.25F, worldObj.rand
.nextFloat() * 0.25F + 0.6F);
}
public void playRetractSound() {
worldObj.playSound(null, (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos
.getZ() + 0.5D, SoundEvents.BLOCK_PISTON_CONTRACT, SoundCategory.BLOCKS, 0.25F, worldObj.rand
.nextFloat() * 0.15F + 0.6F);
}
public int getRedstoneState() {
return redstoneState;
}
public int getExtendState() {
return extendState;
}
public boolean getExtended() {
return isExtended;
}
public boolean getExtending() {
return isExtending;
}
public EnumFacing getFacingDirection() {
return facingDirection;
}
public void setFacingDirection(EnumFacing direction) {
facingDirection = direction;
}
@Override
public void onLoad() {
super.onLoad();
fakePlayer = Util.createFakePlayer(worldObj);
setupStatistics(statistics);
lastWorldTime = worldObj.getWorldTime();
}
@Override
public void update() {
if(isFirstTick) {
updateRedstone();
isFirstTick = false;
}
if(isExtending) {
if(cooldown > 0) {
cooldown -= (worldObj.getTotalWorldTime() - lastWorldTime) * TICK_TIME;
} else if(isExtended) {
if(extendState == statistics.extendLength) {
isExtending = false;
} else if(extendNext()) {
extendState++;
cooldown = statistics.extendDelay;
playExtendSound();
}
} else {
if(extendState <= 0) {
isExtending = false;
} else if(retractNext()) {
extendState--;
cooldown = statistics.extendDelay;
playRetractSound();
}
}
}
lastWorldTime = worldObj.getTotalWorldTime();
}
@Override
public void invalidate() {
super.invalidate();
if(fakePlayer != null) {
worldObj.removeEntity(fakePlayer);
fakePlayer = null;
}
}
@Override
public void validate() {
super.validate();
if(fakePlayer == null) {
fakePlayer = Util.createFakePlayer(worldObj);
}
}
@Override
public ItemStack getDisguiseBlock() {
return getStackInSlot(getSizeInventory() - 1);
}
@Override
public void setDisguiseBlock(ItemStack disguise) {
setInventorySlotContents(getSizeInventory() - 1, disguise);
}
/* NBT */
@Override
public void readFromNBT(NBTTagCompound tags) {
super.readFromNBT(tags);
extendState = tags.getInteger("ExtendState");
isExtended = tags.getBoolean("IsExtended");
isExtending = tags.getBoolean("IsExtending");
cooldown = tags.getFloat("Cooldown");
facingDirection = EnumFacing.values()[tags.getInteger("Facing")];
}
@Nonnull
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tags) {
NBTTagCompound data = super.writeToNBT(tags);
data.setInteger("ExtendState", extendState);
data.setBoolean("IsExtended", isExtended);
data.setBoolean("IsExtending", isExtending);
data.setFloat("Cooldown", cooldown);
data.setInteger("Facing", facingDirection.ordinal());
return data;
}
@Override
public boolean shouldRefresh(World world, BlockPos pos, @Nonnull IBlockState oldState, @Nonnull IBlockState newSate) {
return oldState.getBlock() != newSate.getBlock();
}
@Override
@Nullable
public SPacketUpdateTileEntity getUpdatePacket() {
NBTTagCompound tags = new NBTTagCompound();
tags = writeToNBT(tags);
SPacketUpdateTileEntity packet = new SPacketUpdateTileEntity(pos, worldObj.getBlockState(pos).getBlock()
.getMetaFromState(worldObj
.getBlockState(pos)), tags);
return packet;
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
NBTTagCompound tags = pkt.getNbtCompound();
readFromNBT(tags);
updateRedstone();
}
public abstract void setupStatistics(DrawbridgeStats ds);
public abstract boolean extendNext();
public abstract boolean retractNext();
final class DrawbridgeStats {
public int extendLength = 16;
public float extendDelay = 0.5F;
}
}