/
PipeTileEntity.java
145 lines (119 loc) · 4.07 KB
/
PipeTileEntity.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
package knightminer.inspirations.utility.tileentity;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.utility.InspirationsUtility;
import knightminer.inspirations.utility.block.PipeBlock;
import knightminer.inspirations.utility.inventory.PipeContainer;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
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.HopperTileEntity;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import slimeknights.mantle.tileentity.InventoryTileEntity;
import javax.annotation.Nonnull;
public class PipeTileEntity extends InventoryTileEntity implements ITickableTileEntity {
public static ITextComponent TITLE = new TranslationTextComponent("gui.inspirations.pipe");
private short cooldown = 0;
public PipeTileEntity() {
super(InspirationsUtility.tilePipe, TITLE, 1);
}
@Override
public void tick() {
if(world == null || world.isRemote) {
return;
}
// do not function if facing up when disallowed
Direction facing = this.getFacing();
if(!Config.pipeUpwards.get() && facing == Direction.UP) {
return;
}
cooldown--;
if(cooldown > 0) {
return;
}
cooldown = 0;
// got just 1 stack, makes life easy
ItemStack stack = this.getStackInSlot(0);
if(stack.isEmpty()) {
return;
}
// if we have a TE and its an item handler, try inserting into that
TileEntity te = world.getTileEntity(pos.offset(facing));
if(te != null) {
te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite()).ifPresent((neighbor) -> {
ItemStack copy = stack.copy();
copy.setCount(1);
// if we successfully place it in, shrink it here
if(ItemHandlerHelper.insertItemStacked(neighbor, copy, false).isEmpty()) {
if(te instanceof HopperTileEntity) {
((HopperTileEntity)te).setTransferCooldown(7);
}
// remove the stack if empty
stack.shrink(1);
if(stack.isEmpty()) {
this.setInventorySlotContents(0, ItemStack.EMPTY);
}
cooldown = 8;
this.markDirty();
}
});
}
}
@Override
public void setInventorySlotContents(int slot, @Nonnull ItemStack itemstack) {
super.setInventorySlotContents(slot, itemstack);
cooldown = 7; // set the cooldown to prevent instant retransfer
}
private Direction getFacing() {
return this.getBlockState().get(PipeBlock.FACING);
}
/* GUI */
@Nonnull
@Override
public Container createMenu(int winId, @Nonnull PlayerInventory inv, @Nonnull PlayerEntity entity) {
return new PipeContainer(winId, inv, this);
}
/* Networking */
@Nonnull
@Override
public CompoundNBT getUpdateTag() {
// new tag instead of super since default implementation calls the super of writeToNBT
return write(new CompoundNBT());
}
@Override
public SUpdateTileEntityPacket getUpdatePacket() {
// note that this sends all of the tile data. you should change this if you use additional tile data
CompoundNBT tag = getTileData().copy();
write(tag);
return new SUpdateTileEntityPacket(this.getPos(), 0, tag);
}
@Override
public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
CompoundNBT tag = pkt.getNbtCompound();
read(this.getBlockState(), tag);
}
/* NBT */
private static final String TAG_COOLDOWN = "cooldown";
@Override
public void read(BlockState state, CompoundNBT tags) {
super.read(state, tags);
this.cooldown = tags.getShort(TAG_COOLDOWN);
}
@Nonnull
@Override
public CompoundNBT write(CompoundNBT tags) {
super.write(tags);
tags.putShort(TAG_COOLDOWN, this.cooldown);
return tags;
}
}