-
Notifications
You must be signed in to change notification settings - Fork 18
/
TileCollector.java
114 lines (101 loc) · 4.04 KB
/
TileCollector.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
package knightminer.inspirations.utility.tileentity;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import knightminer.inspirations.utility.InspirationsUtility;
import knightminer.inspirations.utility.inventory.ContainerCollector;
import net.minecraft.entity.item.ItemEntity;
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.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
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;
public class TileCollector extends InventoryTileEntity {
public static ITextComponent TITLE = new TranslationTextComponent("gui.inspirations.collector");
public TileCollector() {
super(InspirationsUtility.tileCollector, TITLE, 9);
}
public void collect(Direction facing) {
BlockPos offset = pos.offset(facing);
TileEntity te = world.getTileEntity(offset);
// if we have a TE and its an item handler, try extracting from that
if(te != null) {
te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite()).ifPresent((neighbor) -> {
// basically, we iterate every slot, trying to remove a single item
for(int i = 0; i < neighbor.getSlots(); i++) {
ItemStack simulated = neighbor.extractItem(i, 1, true);
// as soon as we find one we can extract, we try inserting it
if(!simulated.isEmpty()) {
// if it successfully inserts, extract it from the original inventory
if(ItemHandlerHelper.insertItemStacked(itemHandler, simulated, false).isEmpty()) {
neighbor.extractItem(i, 1, false);
break;
}
}
}
});
}
// collect items from world
AxisAlignedBB aabb = new AxisAlignedBB(offset.getX(), offset.getY(), offset.getZ(), offset.getX()+1, offset.getY()+1, offset.getZ()+1);
boolean collected = false;
for(ItemEntity entity : world.getEntitiesWithinAABB(ItemEntity.class, aabb)) {
ItemStack insert = entity.getItem();
// no need to simulate, if successful we have to modify the stack regardless
ItemStack remainder = ItemHandlerHelper.insertItemStacked(itemHandler, insert, false);
// if the stack changed, we were successful
if(remainder.getCount() < insert.getCount()) {
collected = true;
// empty means item is gone
if(remainder.isEmpty()) {
entity.remove();
} else {
entity.setItem(remainder);
}
}
}
// play sound. Plays dispenser dispense if success and dispenser fail if not
world.playEvent(collected ? 1000 : 1001, pos, 0);
}
@Override
public boolean isItemValidForSlot(int slot, @Nonnull ItemStack itemstack) {
// mantle checks stack size which breaks some things when using stacks bigger than 1
return slot < getSizeInventory();
}
/*
* GUI
*/
@Nullable
@Override
public Container createMenu(int winId, @Nonnull PlayerInventory playerInv, @Nonnull PlayerEntity player) {
return new ContainerCollector(winId, playerInv, 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(tag);
}
}