use of org.spongepowered.api.block.tileentity.carrier.Chest in project AdamantineShield by Karanum.
the class InventoryChangeListener method onInventoryTransfer.
@Listener
public void onInventoryTransfer(AffectSlotEvent e, @First Player p) {
if (e.getTransactions().isEmpty())
return;
if (!(e.getTransactions().get(0).getSlot().parent() instanceof CarriedInventory))
return;
BlockCarrier carrier = null;
CarriedInventory<?> c = (CarriedInventory<?>) e.getTransactions().get(0).getSlot().parent();
if (c.getCarrier().get() instanceof BlockCarrier) {
carrier = (BlockCarrier) c.getCarrier().get();
}
if (carrier == null)
return;
if (!logContainers && !(carrier instanceof Chest))
return;
long timestamp = new Date().getTime();
int containerSize = c.iterator().next().capacity();
for (SlotTransaction transaction : e.getTransactions()) {
int slotId = transaction.getSlot().getProperty(SlotIndex.class, "slotindex").map(SlotIndex::getValue).orElse(-1);
if (slotId >= containerSize)
continue;
ItemStackSnapshot origItem = transaction.getOriginal();
ItemStackSnapshot finalItem = transaction.getFinal();
if (origItem == finalItem)
continue;
if (origItem.createGameDictionaryEntry().matches(finalItem.createStack()) && ItemStackComparators.ITEM_DATA.compare(origItem.createStack(), finalItem.createStack()) == 0) {
if (origItem.getQuantity() > finalItem.getQuantity()) {
ItemStackSnapshot stack = ItemStack.builder().itemType(origItem.getType()).quantity(origItem.getQuantity() - finalItem.getQuantity()).build().createSnapshot();
db.addToQueue(new InventoryQueueEntry(carrier, slotId, stack, ActionType.CONTAINER_REMOVE, p, timestamp));
} else if (origItem.getQuantity() < finalItem.getQuantity()) {
ItemStackSnapshot stack = ItemStack.builder().itemType(origItem.getType()).quantity(finalItem.getQuantity() - origItem.getQuantity()).build().createSnapshot();
db.addToQueue(new InventoryQueueEntry(carrier, slotId, stack, ActionType.CONTAINER_ADD, p, timestamp));
}
} else {
if (origItem.getType() != ItemTypes.NONE) {
db.addToQueue(new InventoryQueueEntry(carrier, slotId, origItem, ActionType.CONTAINER_REMOVE, p, timestamp));
}
if (finalItem.getType() != ItemTypes.NONE) {
db.addToQueue(new InventoryQueueEntry(carrier, slotId, finalItem, ActionType.CONTAINER_ADD, p, timestamp));
}
}
}
}
use of org.spongepowered.api.block.tileentity.carrier.Chest in project modules-extra by CubeEngine.
the class ItemDuctTransferListener method activate.
private void activate() {
List<Network> networks = new ArrayList<>();
for (Iterator<Location<World>> it = this.promptedActivations.keySet().iterator(); it.hasNext(); ) {
Location<World> loc = it.next();
if (this.promptedActivations.get(loc) - 1000 > System.currentTimeMillis()) {
promptedActivations.clear();
continue;
}
it.remove();
// Check if data is still present
Optional<DuctData> data = loc.get(DuctData.class);
if (data.isPresent()) {
for (Direction dir : Direction.values()) {
if (dir.isCardinal() || dir.isUpright()) {
BlockType type = loc.getRelative(dir).getBlockType();
if (STICKY_PISTON.equals(type) || OBSERVER.equals(type)) {
Optional<List<ItemStack>> filters = data.get().get(dir);
if (filters.isPresent()) {
Network network = manager.findNetwork(loc.getRelative(dir));
TileEntity te = loc.getTileEntity().get();
Inventory inventory = ((Carrier) te).getInventory();
if (te instanceof Chest) {
inventory = ((Chest) te).getDoubleChestInventory().orElse(inventory);
}
network.activate(inventory, filters.get());
networks.add(network);
}
}
}
}
}
}
for (Network network : networks) {
for (Location<World> exitLoc : network.exitPoints.keySet()) {
Direction exitDir = exitLoc.get(Keys.DIRECTION).orElse(Direction.NONE).getOpposite();
exitLoc = exitLoc.getRelative(exitDir.getOpposite());
promptActivation(exitLoc.getTileEntity().filter(t -> t instanceof Carrier).map(Carrier.class::cast).orElse(null), true, null);
}
}
if (promptedActivations.isEmpty()) {
task.cancel();
}
}
use of org.spongepowered.api.block.tileentity.carrier.Chest in project modules-extra by CubeEngine.
the class Network method activate.
public void activate(Inventory inventory, List<ItemStack> filters) {
if (isStorage()) {
pullFromStorage(inventory, filters);
return;
}
for (Map.Entry<Location<World>, DuctData> entry : exitPoints.entrySet()) {
Inventory pollFrom = inventory;
Location<World> loc = entry.getKey();
DuctData data = entry.getValue();
Direction dir = loc.get(Keys.DIRECTION).orElse(Direction.NONE).getOpposite();
Location<World> targetLoc = loc.getRelative(dir.getOpposite());
TileEntity te = targetLoc.getTileEntity().get();
Inventory target = ((Carrier) te).getInventory();
if (te instanceof Dropper) {
Network nw = manager.findNetwork(targetLoc);
nw.transferToStorage(queryFiltered(filters, inventory), data.get(dir).get());
continue;
}
if (te instanceof Chest) {
target = ((Chest) te).getDoubleChestInventory().orElse(target);
}
Optional<List<ItemStack>> targetFilter = data.get(dir);
if (targetFilter.isPresent()) {
if (// Only allow to extract items in the filter
!filters.isEmpty()) {
// TODO more filters
pollFrom = queryFiltered(filters, inventory);
}
Inventory pollFromTo = pollFrom;
if (// Only allow to insert items in the filter
!targetFilter.get().isEmpty()) {
// TODO more filters
pollFromTo = queryFiltered(targetFilter.get(), inventory);
}
// For all filtered slots
doTransfer(pollFromTo, target);
}
}
}
use of org.spongepowered.api.block.tileentity.carrier.Chest in project modules-extra by CubeEngine.
the class Network method transferToStorage.
private void transferToStorage(Inventory inventory, List<ItemStack> filters) {
if (// Only allow to extract items in the filter
!filters.isEmpty()) {
// TODO more filters
inventory = queryFiltered(filters, inventory);
}
for (Location<World> targetLoc : storage) {
TileEntity te = targetLoc.getTileEntity().get();
Inventory target = ((Carrier) te).getInventory();
if (te instanceof Chest) {
target = ((Chest) te).getDoubleChestInventory().orElse(target);
}
doTransfer(inventory, target);
}
}
use of org.spongepowered.api.block.tileentity.carrier.Chest in project modules-extra by CubeEngine.
the class Network method pullFromStorage.
private void pullFromStorage(Inventory inventory, List<ItemStack> filters) {
for (Location<World> targetLoc : storage) {
TileEntity te = targetLoc.getTileEntity().get();
Inventory pollFrom = ((Carrier) te).getInventory();
if (te instanceof Chest) {
pollFrom = ((Chest) te).getDoubleChestInventory().orElse(pollFrom);
}
if (// Only allow to extract items in the filter
!filters.isEmpty()) {
// TODO more filters
pollFrom = queryFiltered(filters, inventory);
}
doTransfer(pollFrom, inventory);
}
}
Aggregations