use of org.spongepowered.api.item.inventory.Carrier in project modules-extra by CubeEngine.
the class ItemDuctManager method findNetwork.
private void findNetwork(LastDuct last, Network network, int depth) {
if (depth > maxDepth) {
network.errors.add(last.loc);
return;
}
Map<Direction, Location<World>> map = new HashMap<>();
Queue<Location<World>> next = new LinkedList<>();
next.offer(last.loc);
do {
// TODO limit direction with glass panes
Set<Direction> dirs = directions;
for (Direction dir : dirs) {
if (!dir.equals(last.from.getOpposite())) {
Location<World> rel = last.loc.getRelative(dir);
if (last.isCompatible(rel)) {
if (// No loops allowed
network.pipes.contains(rel)) {
network.errors.add(rel);
network.errors.add(last.loc);
} else {
network.pipes.add(rel);
map.put(dir, rel);
}
}
// ExitPiston?
if (rel.getBlockType().equals(PISTON) && rel.get(Keys.DIRECTION).orElse(Direction.NONE).equals(dir)) {
if (rel.getRelative(dir).get(DuctData.class).map(d -> d.get(dir.getOpposite()).isPresent()).orElse(false)) {
network.exitPoints.put(rel, rel.getRelative(dir).get(DuctData.class).get());
}
}
// Storage Chest
if (last.storage) {
rel.getTileEntity().ifPresent(te -> {
if (te instanceof Carrier) {
network.storage.add(rel);
}
});
}
}
}
if (map.size() > 1) {
for (Map.Entry<Direction, Location<World>> entry : map.entrySet()) {
findNetwork(new LastDuct(entry.getValue(), entry.getKey()), network, depth + 1);
}
} else if (map.size() == 1) {
for (Map.Entry<Direction, Location<World>> entry : map.entrySet()) {
last.update(entry.getValue(), entry.getKey());
}
next.offer(last.loc);
}
// else nothing found here
next.poll();
map.clear();
} while (!next.isEmpty());
}
use of org.spongepowered.api.item.inventory.Carrier 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);
}
}
use of org.spongepowered.api.item.inventory.Carrier in project LanternServer by LanternPowered.
the class TestInventoryPlugin method onInit.
@Listener
public void onInit(GameInitializationEvent event) {
this.myArchetype = InventoryArchetype.builder().with(InventoryArchetypes.MENU_ROW).title(Text.of("My Fancy Title")).property(InventoryDimension.of(9, 1)).property(new GuiIdProperty(GuiIds.CHEST)).build("inventory_test:test", "Test Inventory");
Sponge.getCommandManager().register(this, CommandSpec.builder().executor((src, args) -> {
if (!(src instanceof Player)) {
throw new CommandException(t("Only players may use this command."));
}
final Inventory inventory = Inventory.builder().of(this.myArchetype).withCarrier((Carrier) src).build(this);
System.out.println(inventory.getClass().getName());
final ItemStack itemStack = ItemStack.of(ItemTypes.LOG, 64);
itemStack.offer(Keys.TREE_TYPE, TreeTypes.JUNGLE);
inventory.offer(itemStack);
((Player) src).openInventory(inventory);
return CommandResult.success();
}).build(), "test-a-inv");
Keys.COAL_TYPE.registerEvent(ItemStack.class, event1 -> {
final DataTransactionResult result = event1.getEndResult();
final List<ImmutableValue<?>> newSuccessfulData = new ArrayList<>(result.getSuccessfulData());
Iterator<ImmutableValue<?>> it = newSuccessfulData.iterator();
while (it.hasNext()) {
final ImmutableValue<?> value = it.next();
if (value.getKey() == Keys.COAL_TYPE) {
System.out.println("Changed coal type to: " + ((CoalType) value.get()).getId() + ", but this not allowed");
it.remove();
break;
}
}
final List<ImmutableValue<?>> newReplacedData = new ArrayList<>(result.getReplacedData());
it = newSuccessfulData.iterator();
while (it.hasNext()) {
final ImmutableValue<?> value = it.next();
if (value.getKey() == Keys.COAL_TYPE) {
it.remove();
break;
}
}
event1.proposeChanges(DataTransactionResult.builder().result(result.getType()).reject(result.getRejectedData()).replace(newReplacedData).success(newSuccessfulData).build());
});
}
use of org.spongepowered.api.item.inventory.Carrier in project LanternServer by LanternPowered.
the class LanternItem method tryToPickupItems.
private void tryToPickupItems() {
final Set<Entity> entities = getWorld().getIntersectingEntities(getBoundingBox().get().expand(2.0, 0.5, 2.0), entity -> entity != this && entity instanceof Carrier);
if (entities.isEmpty()) {
return;
}
ItemStack itemStack = get(Keys.REPRESENTED_ITEM).map(ItemStackSnapshot::createStack).orElse(null);
if (itemStack == null) {
remove();
return;
}
// TODO: Call pre pickup event
for (Entity entity : entities) {
// Ignore dead entities
if (entity instanceof LanternLiving && ((LanternLiving) entity).isDead()) {
continue;
}
Inventory inventory = ((Carrier) entity).getInventory();
if (inventory instanceof PlayerInventory) {
inventory = ((PlayerInventory) inventory).getMain();
}
final PeekedOfferTransactionResult peekResult = ((IInventory) inventory).peekOffer(itemStack);
final ItemStack rejected = peekResult.getRejectedItem().orElse(null);
final CauseStack causeStack = CauseStack.current();
final ChangeInventoryEvent.Pickup event;
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.addContext(LanternEventContextKeys.ORIGINAL_ITEM_STACK, itemStack);
if (rejected != null) {
frame.addContext(LanternEventContextKeys.REST_ITEM_STACK, rejected);
}
event = SpongeEventFactory.createChangeInventoryEventPickup(causeStack.getCurrentCause(), inventory, peekResult.getTransactions());
event.setCancelled(!peekResult.isSuccess());
Sponge.getEventManager().post(event);
}
if (event.isCancelled() && !isRemoved()) {
// Don't continue if the entity was removed during the event
continue;
}
event.getTransactions().stream().filter(Transaction::isValid).forEach(transaction -> transaction.getSlot().set(transaction.getFinal().createStack()));
final int added;
if (rejected != null) {
added = itemStack.getQuantity() - rejected.getQuantity();
itemStack = rejected;
} else {
added = itemStack.getQuantity();
}
if (added != 0 && entity instanceof Living) {
triggerEvent(new CollectEntityEvent((Living) entity, added));
}
if (rejected == null || isRemoved()) {
itemStack = null;
}
if (itemStack == null) {
break;
}
}
if (itemStack != null) {
offer(Keys.REPRESENTED_ITEM, itemStack.createSnapshot());
} else {
remove();
}
}
use of org.spongepowered.api.item.inventory.Carrier in project LanternServer by LanternPowered.
the class LanternLiving method handleDeath.
private void handleDeath() {
// have already been thrown.
if (getWorld() == null) {
setDead(true);
}
if (isDead()) {
return;
}
setDead(true);
final CauseStack causeStack = CauseStack.current();
// Only players can keep their inventory
final boolean keepsInventory = this instanceof LanternPlayer && getWorld().getOrCreateRule(RuleTypes.KEEP_INVENTORY).getValue();
// Post the entity destruction event
final DestructEntityEvent.Death event = SpongeEventFactory.createDestructEntityEventDeath(causeStack.getCurrentCause(), MessageChannel.TO_NONE, Optional.empty(), new MessageEvent.MessageFormatter(), this, keepsInventory, false);
postDestructEvent(event);
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
// Add the destruct event to the cause, this can be used
// to track the cause of the entity death.
frame.pushCause(event);
// Post the harvest event
handleDeath(causeStack);
}
// Clear the inventory, if keepsInventory is false in the thrown Death event
if (!event.getKeepInventory() && this instanceof Carrier) {
((Carrier) this).getInventory().clear();
}
}
Aggregations