Search in sources :

Example 6 with Container

use of org.spongepowered.api.item.inventory.Container in project modules-extra by CubeEngine.

the class BackpackManager method onInventoryClose.

@Listener
public void onInventoryClose(InteractInventoryEvent.Close event, @First Player player) {
    Container inventory = event.getTargetInventory();
    if (inventory instanceof CarriedInventory) {
        if (((CarriedInventory) inventory).getCarrier().orElse(null) instanceof BackpackHolder) {
            BackpackHolder holder = (BackpackHolder) ((CarriedInventory) inventory).getCarrier().get();
            holder.getBackpack().closeInventory(inventory, player);
        }
    }
}
Also used : Container(org.spongepowered.api.item.inventory.Container) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Listener(org.spongepowered.api.event.Listener)

Example 7 with Container

use of org.spongepowered.api.item.inventory.Container in project LanternServer by LanternPowered.

the class CraftingTableInteractionBehavior method tryInteract.

@Override
public BehaviorResult tryInteract(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final Optional<Player> optPlayer = context.getContext(ContextKeys.PLAYER);
    if (optPlayer.isPresent()) {
        final CraftingTableInventory craftingTableInventory = VanillaInventoryArchetypes.CRAFTING_TABLE.builder().withCarrier(new LanternBlockCarrier(context.requireContext(ContextKeys.BLOCK_LOCATION))).build(Lantern.getMinecraftPlugin());
        final Optional<Container> optContainer = optPlayer.get().openInventory(craftingTableInventory);
        if (optContainer.isPresent()) {
            return BehaviorResult.SUCCESS;
        }
    }
    return BehaviorResult.PASS;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) CraftingTableInventory(org.lanternpowered.server.inventory.vanilla.block.CraftingTableInventory) Container(org.spongepowered.api.item.inventory.Container) LanternBlockCarrier(org.lanternpowered.server.inventory.carrier.LanternBlockCarrier)

Example 8 with Container

use of org.spongepowered.api.item.inventory.Container in project Nucleus by NucleusPowered.

the class KitListener method onPlayerInteractInventoryClose.

@Listener
public void onPlayerInteractInventoryClose(final InteractInventoryEvent.Close event, @Root final Player player, @Getter("getTargetInventory") final Container inventory) {
    handler.getCurrentlyOpenInventoryCommandKit(inventory).ifPresent(x -> {
        // Set the commands.
        Kit kitInfo = x.getFirst();
        List<String> c = Lists.newArrayList();
        // For each slot, is it a written book?
        x.getSecond().slots().forEach(slot -> slot.poll().ifPresent(item -> {
            if (item.getType().equals(ItemTypes.WRITTEN_BOOK)) {
                item.get(Keys.BOOK_PAGES).ifPresent(y -> c.add(fixup(y)));
            } else if (item.getType().equals(ItemTypes.WRITABLE_BOOK)) {
                item.get(Keys.BOOK_PAGES).ifPresent(page -> c.add(getCommandFromText(page)));
            } else {
                // Drop the item.
                item.get(Keys.ITEM_BLOCKSTATE).ifPresent(z -> {
                    World world = player.getLocation().getExtent();
                    Entity e = world.createEntity(EntityTypes.ITEM, player.getLocation().getPosition());
                    e.offer(Keys.ITEM_BLOCKSTATE, z);
                    world.spawnEntity(e);
                });
            }
            kitInfo.setCommands(c);
            handler.saveKit(kitInfo);
        }));
        player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.command.edit.success", kitInfo.getName()));
        handler.removeKitCommandInventoryFromListener(inventory);
    });
    handler.removeViewer(inventory);
}
Also used : KitRedeemException(io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException) Getter(org.spongepowered.api.event.filter.Getter) Keys(org.spongepowered.api.data.key.Keys) ItemTypes(org.spongepowered.api.item.ItemTypes) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) Lists(com.google.common.collect.Lists) PermissionRegistry(io.github.nucleuspowered.nucleus.internal.PermissionRegistry) KitUserDataModule(io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule) EntityTypes(org.spongepowered.api.entity.EntityTypes) Text(org.spongepowered.api.text.Text) ListenerBase(io.github.nucleuspowered.nucleus.internal.ListenerBase) Exclude(org.spongepowered.api.event.filter.type.Exclude) KitHandler(io.github.nucleuspowered.nucleus.modules.kit.handlers.KitHandler) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) UserDataManager(io.github.nucleuspowered.nucleus.dataservices.loaders.UserDataManager) InteractInventoryEvent(org.spongepowered.api.event.item.inventory.InteractInventoryEvent) Entity(org.spongepowered.api.entity.Entity) Collectors(java.util.stream.Collectors) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) Root(org.spongepowered.api.event.filter.cause.Root) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) List(java.util.List) Container(org.spongepowered.api.item.inventory.Container) NucleusFirstJoinEvent(io.github.nucleuspowered.nucleus.api.events.NucleusFirstJoinEvent) World(org.spongepowered.api.world.World) KitService(io.github.nucleuspowered.nucleus.dataservices.KitService) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) KitConfigAdapter(io.github.nucleuspowered.nucleus.modules.kit.config.KitConfigAdapter) Entity(org.spongepowered.api.entity.Entity) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) World(org.spongepowered.api.world.World) Listener(org.spongepowered.api.event.Listener)

Example 9 with Container

use of org.spongepowered.api.item.inventory.Container in project Nucleus by NucleusPowered.

the class KitCreateCommand method executeCommand.

@Override
public CommandResult executeCommand(final CommandSource source, CommandContext args) throws ReturnMessageException {
    String kitName = args.<String>getOne(name).get();
    if (KIT_HANDLER.getKitNames().stream().anyMatch(kitName::equalsIgnoreCase)) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.add.alreadyexists", kitName));
    }
    if (source instanceof Player) {
        final Player player = (Player) source;
        Inventory inventory = Util.getKitInventoryBuilder().property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.create.title", kitName))).build(plugin);
        Container container = player.openInventory(inventory).orElseThrow(() -> ReturnMessageException.fromKey("command.kit.create.notcreated"));
        Sponge.getEventManager().registerListeners(plugin, new TemporaryEventListener(inventory, container, kitName));
    } else {
        try {
            KIT_HANDLER.saveKit(KIT_HANDLER.createKit(kitName));
            source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.addempty.success", kitName));
        } catch (IllegalArgumentException ex) {
            throw ReturnMessageException.fromKey("command.kit.create.failed", kitName);
        }
    }
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Container(org.spongepowered.api.item.inventory.Container) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) Inventory(org.spongepowered.api.item.inventory.Inventory)

Aggregations

Container (org.spongepowered.api.item.inventory.Container)9 Player (org.spongepowered.api.entity.living.player.Player)5 Inventory (org.spongepowered.api.item.inventory.Inventory)4 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)3 Listener (org.spongepowered.api.event.Listener)3 Kit (io.github.nucleuspowered.nucleus.api.nucleusdata.Kit)2 KitHandler (io.github.nucleuspowered.nucleus.modules.kit.handlers.KitHandler)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 CarriedInventory (org.spongepowered.api.item.inventory.type.CarriedInventory)2 Text (org.spongepowered.api.text.Text)2 RPContainer (br.net.fabiozumbi12.RedProtect.Sponge.RPContainer)1 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)1 Lists (com.google.common.collect.Lists)1 Multimap (com.google.common.collect.Multimap)1 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)1 Util (io.github.nucleuspowered.nucleus.Util)1 NucleusFirstJoinEvent (io.github.nucleuspowered.nucleus.api.events.NucleusFirstJoinEvent)1 KitRedeemException (io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException)1