org.spongepowered.api.item.inventory.property.SlotIndex Java Examples

The following examples show how to use org.spongepowered.api.item.inventory.property.SlotIndex. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VirtualChestInventory.java    From VirtualChest with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void fireClickEvent(ClickInventoryEvent e)
{
    Optional<Player> optional = Sponge.getServer().getPlayer(playerUniqueId);
    if (optional.isPresent())
    {
        Player player = optional.get();
        CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> Boolean.TRUE, executorService);
        for (SlotTransaction slotTransaction : e.getTransactions())
        {
            Slot slot = slotTransaction.getSlot();
            Container targetContainer = e.getTargetInventory();
            SlotIndex pos = Iterables.getOnlyElement(slot.parent().getProperties(slot, SlotIndex.class));
            if (slot.parent().equals(targetContainer) && slotToListen.matches(pos))
            {
                e.setCancelled(true);
                if (actionIntervalManager.allowAction(player, acceptableActionIntervalTick))
                {
                    int index = Objects.requireNonNull(pos.getValue());
                    future = future.thenCompose(b -> this.runCommand(e, player, index).thenApply(a -> a && b));
                }
            }
        }
        future.thenApply(keepInventoryOpen -> keepInventoryOpen || !plugin.getDispatcher().close(name, player));
    }
}
 
Example #2
Source File: VirtualChestInventoryBuilder.java    From VirtualChest with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public VirtualChestInventoryBuilder from(VirtualChestInventory value)
{
    this.title = value.title;
    this.height = value.height;
    this.triggerItems.clear();
    this.triggerItems.addAll(value.triggerItems);
    this.openActionCommand = value.openActionCommand;
    this.closeActionCommand = value.closeActionCommand;
    this.updateIntervalTick = value.updateIntervalTick;
    this.actionIntervalTick = value.acceptableActionIntervalTick.isPresent()
            ? Optional.of(value.acceptableActionIntervalTick.getAsInt()) : Optional.empty();
    this.items.clear();
    for (int i = 0; i < value.items.size(); i++)
    {
        this.items.putAll(SlotIndex.of(i), value.items.get(i));
    }
    return this;
}
 
Example #3
Source File: VirtualChestInventory.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
private <T> List<List<T>> createListFromMultiMap(Multimap<SlotIndex, T> list, int max)
{
    ImmutableList.Builder<List<T>> builder = ImmutableList.builder();
    for (int i = 0; i < max; ++i)
    {
        builder.add(ImmutableList.copyOf(list.get(SlotIndex.of(i))));
    }
    return builder.build();
}
 
Example #4
Source File: VirtualChestInventory.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
private EventListener(Player player, String inventoryName)
{
    this.parsedOpenAction = VirtualChestActionDispatcher.parseCommand(openActionCommand.orElse(""));
    this.parsedCloseAction = VirtualChestActionDispatcher.parseCommand(closeActionCommand.orElse(""));
    this.slotToListen = SlotIndex.lessThan(height * 9);
    this.playerUniqueId = player.getUniqueId();
    this.name = inventoryName;
}
 
Example #5
Source File: VirtualChestInventoryBuilder.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Optional<VirtualChestInventory> buildContent(DataView view) throws InvalidDataException
{
    this.items.clear();
    for (DataQuery key : view.getKeys(false))
    {
        String keyString = key.toString();
        if (keyString.startsWith(VirtualChestInventory.KEY_PREFIX))
        {
            SlotIndex slotIndex = SlotIndex.of(VirtualChestInventory.keyToSlotIndex(keyString));
            for (DataView dataView : VirtualChestItem.getViewListOrSingletonList(key, view))
            {
                VirtualChestItem item = VirtualChestItem.deserialize(plugin, dataView);
                this.items.put(slotIndex, item);
            }
        }
    }

    this.title = view.getString(VirtualChestInventory.TITLE)
            .map(TextSerializers.FORMATTING_CODE::deserialize)
            .orElseThrow(() -> new InvalidDataException("Expected title"));

    this.height = view.getInt(VirtualChestInventory.HEIGHT)
            .orElseThrow(() -> new InvalidDataException("Expected height"));

    this.triggerItems.clear();
    VirtualChestItem.getViewListOrSingletonList(VirtualChestInventory.TRIGGER_ITEM, view)
            .forEach(dataView -> this.triggerItems.add(new VirtualChestTriggerItem(dataView)));

    this.openActionCommand = view.getString(VirtualChestInventory.OPEN_ACTION_COMMAND);

    this.closeActionCommand = view.getString(VirtualChestInventory.CLOSE_ACTION_COMMAND);

    this.updateIntervalTick = view.getInt(VirtualChestInventory.UPDATE_INTERVAL_TICK).orElse(0);

    this.actionIntervalTick = view.getInt(VirtualChestInventory.ACCEPTABLE_ACTION_INTERVAL_TICK);

    return Optional.of(new VirtualChestInventory(this.plugin, this));
}
 
Example #6
Source File: EntityServlet.java    From Web-API with MIT License 4 votes vote down vote up
@PUT
@Path("/{entity}")
@Permission("modify")
@ApiOperation(
        value = "Modify an entity",
        notes = "Modify the properties of an existing entity.")
public CachedEntity modifyEntity(
        @PathParam("entity") @ApiParam("The uuid of the entity") UUID uuid,
        UpdateEntityRequest req)
        throws NotFoundException, BadRequestException {

    if (req == null) {
        throw new BadRequestException("Request body is required");
    }

    Optional<CachedEntity> optEntity = WebAPI.getCacheService().getEntity(uuid);
    if (!optEntity.isPresent()) {
        throw new NotFoundException("Entity with UUID '" + uuid + "' could not be found");
    }

    return WebAPI.runOnMain(() -> {
        Optional<Entity> optLive = optEntity.get().getLive();
        if (!optLive.isPresent())
            throw new InternalServerErrorException("Could not get live entity");

        Entity live = optLive.get();

        if (req.getWorld().isPresent()) {
            Optional<World> optWorld = req.getWorld().get().getLive();
            if (!optWorld.isPresent())
                throw new InternalServerErrorException("Could not get live world");

            if (req.getPosition() != null) {
                live.transferToWorld(optWorld.get(), req.getPosition());
            } else {
                live.transferToWorld(optWorld.get());
            }
        } else if (req.getPosition() != null) {
            live.setLocation(new Location<>(live.getWorld(), req.getPosition()));
        }
        if (req.getVelocity() != null) {
            live.setVelocity(req.getVelocity());
        }
        if (req.getRotation() != null) {
            live.setRotation(req.getRotation());
        }

        if (req.getScale() != null) {
            live.setRotation(req.getScale());
        }

        if (req.getDamage() != null) {
            DamageRequest dmgReq = req.getDamage();
            DamageSource.Builder builder = DamageSource.builder();

            if (dmgReq.getType().isPresent()) {
                Optional<DamageType> optDmgType = dmgReq.getType().get().getLive(DamageType.class);
                if (!optDmgType.isPresent())
                    throw new InternalServerErrorException("Could not get live damage type");

                builder.type(optDmgType.get());
            }

            live.damage(req.getDamage().getAmount(), builder.build());
        }

        if (req.hasInventory()) {
            if (!(live instanceof Carrier)) {
                throw new BadRequestException("Entity does not have an inventory!");
            }

            try {
                Inventory inv = ((Carrier) live).getInventory();
                for (SlotRequest slotReq : req.getInventory()) {
                    for (Inventory slot : inv.slots()) {
                        Optional<SlotIndex> optIndex = slot.getInventoryProperty(SlotIndex.class);
                        if (!optIndex.isPresent() || !slotReq.getSlotIndex().equals(optIndex.get().getValue())) {
                            continue;
                        }
                        if (slotReq.getStack().isPresent()) {
                            slot.set(slotReq.getStack().get());
                        } else {
                            slot.clear();
                        }
                    }
                }
            } catch (Exception e) {
                throw new InternalServerErrorException(e.getMessage());
            }
        }

        return new CachedEntity(live);
    });
}