-
Notifications
You must be signed in to change notification settings - Fork 10
/
IronShulkerBoxTileEntity.java
440 lines (371 loc) · 13.4 KB
/
IronShulkerBoxTileEntity.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package com.progwml6.ironshulkerbox.common.tileentity;
import com.progwml6.ironshulkerbox.common.blocks.ShulkerBoxBlock;
import com.progwml6.ironshulkerbox.common.blocks.ShulkerBoxType;
import com.progwml6.ironshulkerbox.common.core.IronShulkerBoxBlocks;
import com.progwml6.ironshulkerbox.common.inventory.ShulkerBoxContainer;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.material.PushReaction;
import net.minecraft.entity.Entity;
import net.minecraft.entity.MoverType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.DyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.LockableLootTileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.NonNullList;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import javax.annotation.Nullable;
import java.util.List;
import java.util.stream.IntStream;
public class IronShulkerBoxTileEntity extends LockableLootTileEntity implements ISidedInventory, ITickableTileEntity
{
private final int[] SLOTS;
private NonNullList<ItemStack> items;
private int openCount;
private IronShulkerBoxTileEntity.AnimationStatus animationStatus = IronShulkerBoxTileEntity.AnimationStatus.CLOSED;
private float progress;
private float progressOld;
private DyeColor color;
private ShulkerBoxType shulkerBoxType;
private List<ShulkerBoxBlock> blocksToUse;
private boolean needsColorFromWorld;
public IronShulkerBoxTileEntity()
{
this(null);
this.needsColorFromWorld = true;
}
public IronShulkerBoxTileEntity(@Nullable DyeColor colorIn)
{
this(ShulkerBoxTileEntityType.IRON_SHULKER_BOX, colorIn, ShulkerBoxType.IRON, IronShulkerBoxBlocks.ironShulkerBoxes);
}
public IronShulkerBoxTileEntity(TileEntityType<?> typeIn, @Nullable DyeColor colorIn, ShulkerBoxType shulkerBoxTypeIn, List<ShulkerBoxBlock> blockListIn)
{
super(typeIn);
this.SLOTS = IntStream.range(0, shulkerBoxTypeIn.size).toArray();
this.items = NonNullList.<ItemStack>withSize(shulkerBoxTypeIn.size, ItemStack.EMPTY);
this.color = colorIn;
this.shulkerBoxType = shulkerBoxTypeIn;
this.blocksToUse = blockListIn;
if (colorIn == null)
{
this.needsColorFromWorld = true;
}
}
@Override
public void tick()
{
this.updateAnimation();
if (this.animationStatus == IronShulkerBoxTileEntity.AnimationStatus.OPENING || this.animationStatus == IronShulkerBoxTileEntity.AnimationStatus.CLOSING)
{
this.moveCollidedEntities();
}
}
protected void updateAnimation()
{
this.progressOld = this.progress;
switch (this.animationStatus)
{
case CLOSED:
this.progress = 0.0F;
break;
case OPENING:
this.progress += 0.1F;
if (this.progress >= 1.0F)
{
this.moveCollidedEntities();
this.animationStatus = IronShulkerBoxTileEntity.AnimationStatus.OPENED;
this.progress = 1.0F;
this.updateNeighbors();
}
break;
case CLOSING:
this.progress -= 0.1F;
if (this.progress <= 0.0F)
{
this.animationStatus = IronShulkerBoxTileEntity.AnimationStatus.CLOSED;
this.progress = 0.0F;
this.updateNeighbors();
}
break;
case OPENED:
this.progress = 1.0F;
}
}
public IronShulkerBoxTileEntity.AnimationStatus getAnimationStatus()
{
return this.animationStatus;
}
public AxisAlignedBB getBoundingBox(BlockState state)
{
return this.getBoundingBox(state.get(ShulkerBoxBlock.FACING));
}
public AxisAlignedBB getBoundingBox(Direction direction)
{
float f = this.getProgress(1.0F);
return VoxelShapes.fullCube().getBoundingBox().expand((double) (0.5F * f * (float) direction.getXOffset()), (double) (0.5F * f * (float) direction.getYOffset()), (double) (0.5F * f * (float) direction.getZOffset()));
}
private AxisAlignedBB getTopBoundingBox(Direction direction)
{
Direction opposite = direction.getOpposite();
return this.getBoundingBox(direction).contract((double) opposite.getXOffset(), (double) opposite.getYOffset(), (double) opposite.getZOffset());
}
private void moveCollidedEntities()
{
BlockState blockstate = this.world.getBlockState(this.getPos());
if (blockstate.getBlock() instanceof ShulkerBoxBlock)
{
Direction direction = blockstate.get(ShulkerBoxBlock.FACING);
AxisAlignedBB axisalignedbb = this.getTopBoundingBox(direction).offset(this.pos);
List<Entity> list = this.world.getEntitiesWithinAABBExcludingEntity(null, axisalignedbb);
if (!list.isEmpty())
{
for (int i = 0; i < list.size(); ++i)
{
Entity entity = list.get(i);
if (entity.getPushReaction() != PushReaction.IGNORE)
{
double d0 = 0.0D;
double d1 = 0.0D;
double d2 = 0.0D;
AxisAlignedBB axisalignedbb1 = entity.getBoundingBox();
switch (direction.getAxis())
{
case X:
if (direction.getAxisDirection() == Direction.AxisDirection.POSITIVE)
{
d0 = axisalignedbb.maxX - axisalignedbb1.minX;
}
else
{
d0 = axisalignedbb1.maxX - axisalignedbb.minX;
}
d0 = d0 + 0.01D;
break;
case Y:
if (direction.getAxisDirection() == Direction.AxisDirection.POSITIVE)
{
d1 = axisalignedbb.maxY - axisalignedbb1.minY;
}
else
{
d1 = axisalignedbb1.maxY - axisalignedbb.minY;
}
d1 = d1 + 0.01D;
break;
case Z:
if (direction.getAxisDirection() == Direction.AxisDirection.POSITIVE)
{
d2 = axisalignedbb.maxZ - axisalignedbb1.minZ;
}
else
{
d2 = axisalignedbb1.maxZ - axisalignedbb.minZ;
}
d2 = d2 + 0.01D;
}
entity.move(MoverType.SHULKER_BOX, new Vec3d(d0 * (double) direction.getXOffset(), d1 * (double) direction.getYOffset(), d2 * (double) direction.getZOffset()));
}
}
}
}
}
@Override
public int getSizeInventory()
{
return this.items.size();
}
@Override
public boolean receiveClientEvent(int id, int type)
{
if (id == 1)
{
this.openCount = type;
if (type == 0)
{
this.animationStatus = IronShulkerBoxTileEntity.AnimationStatus.CLOSING;
this.updateNeighbors();
}
if (type == 1)
{
this.animationStatus = IronShulkerBoxTileEntity.AnimationStatus.OPENING;
this.updateNeighbors();
}
return true;
}
else
{
return super.receiveClientEvent(id, type);
}
}
private void updateNeighbors()
{
this.getBlockState().updateNeighbors(this.getWorld(), this.getPos(), 3);
}
@Override
public void openInventory(PlayerEntity player)
{
if (!player.isSpectator())
{
if (this.openCount < 0)
{
this.openCount = 0;
}
++this.openCount;
this.world.addBlockEvent(this.pos, this.getBlockState().getBlock(), 1, this.openCount);
if (this.openCount == 1)
{
this.world.playSound((PlayerEntity) null, this.pos, SoundEvents.BLOCK_SHULKER_BOX_OPEN, SoundCategory.BLOCKS, 0.5F, this.world.rand.nextFloat() * 0.1F + 0.9F);
}
}
}
@Override
public void closeInventory(PlayerEntity player)
{
if (!player.isSpectator())
{
--this.openCount;
this.world.addBlockEvent(this.pos, this.getBlockState().getBlock(), 1, this.openCount);
if (this.openCount <= 0)
{
this.world.playSound((PlayerEntity) null, this.pos, SoundEvents.BLOCK_SHULKER_BOX_CLOSE, SoundCategory.BLOCKS, 0.5F, this.world.rand.nextFloat() * 0.1F + 0.9F);
}
}
}
@Override
protected ITextComponent getDefaultName()
{
return new TranslationTextComponent("container.shulkerBox");
}
@Override
public void read(CompoundNBT compound)
{
super.read(compound);
this.loadFromNbt(compound);
}
@Override
public CompoundNBT write(CompoundNBT compound)
{
super.write(compound);
return this.saveToNbt(compound);
}
public void loadFromNbt(CompoundNBT compound)
{
this.items = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
if (!this.checkLootAndRead(compound) && compound.contains("Items", 9))
{
ItemStackHelper.loadAllItems(compound, this.items);
}
}
public CompoundNBT saveToNbt(CompoundNBT compound)
{
if (!this.checkLootAndWrite(compound))
{
ItemStackHelper.saveAllItems(compound, this.items, false);
}
return compound;
}
@Override
public NonNullList<ItemStack> getItems()
{
return this.items;
}
@Override
public void setItems(NonNullList<ItemStack> itemsIn)
{
this.items = NonNullList.<ItemStack>withSize(this.getShulkerBoxType().size, ItemStack.EMPTY);
for (int i = 0; i < itemsIn.size(); i++)
{
if (i < this.items.size())
{
this.getItems().set(i, itemsIn.get(i));
}
}
}
@Override
public boolean isEmpty()
{
for (ItemStack itemstack : this.items)
{
if (!itemstack.isEmpty())
{
return false;
}
}
return true;
}
@Override
public int[] getSlotsForFace(Direction side)
{
return SLOTS;
}
@Override
public boolean canInsertItem(int index, ItemStack itemStackIn, @Nullable Direction direction)
{
return !(Block.getBlockFromItem(itemStackIn.getItem()) instanceof ShulkerBoxBlock) || !(Block.getBlockFromItem(itemStackIn.getItem()) instanceof net.minecraft.block.ShulkerBoxBlock);
}
@Override
public boolean canExtractItem(int index, ItemStack stack, Direction direction)
{
return true;
}
public float getProgress(float p_190585_1_)
{
return MathHelper.lerp(p_190585_1_, this.progressOld, this.progress);
}
@OnlyIn(Dist.CLIENT)
public DyeColor getColor()
{
if (this.needsColorFromWorld)
{
this.color = ShulkerBoxBlock.getColorFromBlock(this.getBlockState().getBlock());
this.needsColorFromWorld = false;
}
return this.color;
}
@Override
protected Container createMenu(int id, PlayerInventory playerInventory)
{
return ShulkerBoxContainer.createIronContainer(id, playerInventory, this);
}
public ShulkerBoxType getShulkerBoxType()
{
ShulkerBoxType type = ShulkerBoxType.IRON;
if (this.hasWorld())
{
ShulkerBoxType typeNew = ShulkerBoxBlock.getTypeFromBlock(this.getBlockState().getBlock());
if (typeNew != null)
{
type = typeNew;
}
}
return type;
}
public Block getBlockToUse()
{
return this.blocksToUse.get(color.getId());
}
public enum AnimationStatus
{
CLOSED,
OPENING,
OPENED,
CLOSING
}
}