use of org.spongepowered.api.item.inventory.Carrier 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.item.inventory.Carrier 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.item.inventory.Carrier 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.item.inventory.Carrier in project SpongeCommon by SpongePowered.
the class AdvancementTest method onChangeInventory.
@SuppressWarnings("ConstantConditions")
@Listener
public void onChangeInventory(ChangeInventoryEvent event, @First Player player, @Getter("getTargetInventory") CarriedInventory<?> container) {
if (!container.getName().get().equals("Furnace")) {
return;
}
final Carrier carrier = container.getCarrier().orElse(null);
if (!(carrier instanceof Furnace)) {
return;
}
final Furnace furnace = (Furnace) carrier;
final int passed = furnace.passedBurnTime().get();
final int max = furnace.maxBurnTime().get();
if (max <= 0 || passed >= max) {
return;
}
for (SlotTransaction transaction : event.getTransactions()) {
if (transaction.getSlot().getInventoryProperty(SlotIndex.class).get().getValue() == 0) {
if (transaction.getFinal().getType() == ItemTypes.DIRT) {
player.getProgress(this.cookDirtAdvancement).grant();
} else if (this.suicidalAdvancement != null && (transaction.getFinal().getType() == ItemTypes.TNT || transaction.getFinal().getType() == ItemTypes.TNT_MINECART)) {
player.getProgress(this.suicidalAdvancement).grant();
final Explosion explosion = Explosion.builder().location(furnace.getLocation()).shouldBreakBlocks(true).canCauseFire(true).shouldDamageEntities(true).radius(7).build();
explosion.getWorld().triggerExplosion(explosion);
}
}
}
}
use of org.spongepowered.api.item.inventory.Carrier in project SpongeCommon by SpongePowered.
the class CustomInventoryTest method interactCarrier.
private void interactCarrier(InteractBlockEvent.Primary event, Player player, Location<World> loc) {
loc.getTileEntity().ifPresent(te -> {
if (te instanceof Carrier) {
BasicCarrier myCarrier = new BasicCarrier();
Inventory custom = Inventory.builder().from(((Carrier) te).getInventory()).property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of("Custom ", ((Carrier) te).getInventory().getName()))).withCarrier(myCarrier).build(this);
myCarrier.init(custom);
Sponge.getCauseStackManager().pushCause(player);
player.openInventory(custom);
Sponge.getCauseStackManager().popCause();
event.setCancelled(true);
}
});
}
Aggregations