Search in sources :

Example 1 with Primary

use of org.spongepowered.api.event.block.InteractBlockEvent.Primary in project modules-extra by CubeEngine.

the class ItemRepairListener method onPlayerInteract.

@Listener
public void onPlayerInteract(InteractBlockEvent event, @First Player player) {
    if (!(event instanceof InteractBlockEvent.Primary.MainHand) && !(event instanceof InteractBlockEvent.Secondary.MainHand)) {
        return;
    }
    final Location<World> block = event.getTargetBlock().getLocation().orElse(null);
    if (block == null) {
        return;
    }
    RepairBlock repairBlock = this.rbm.getRepairBlock(block);
    if (repairBlock == null) {
        return;
    }
    event.setCancelled(true);
    if (!player.hasPermission(repairBlock.getPermission().getId())) {
        i18n.send(player, NEGATIVE, "You are not allowed to use this repair block!");
        return;
    }
    RepairBlockInventory inventory = repairBlock.getInventory(player);
    boolean primary = event instanceof Primary;
    if (!primary) {
        this.cancelRequest(event, player);
        Sponge.getCauseStackManager().pushCause(player);
        player.openInventory(inventory.inventory);
    } else {
        event.setCancelled(true);
        if (this.repairRequests.containsKey(player.getUniqueId())) {
            RepairRequest request = this.repairRequests.get(player.getUniqueId());
            if (request.getRepairBlock() == repairBlock) {
                repairBlock.repair(request);
                this.repairRequests.remove(player.getUniqueId());
            }
        } else {
            if (!this.repairRequests.containsKey(player.getUniqueId())) {
                RepairRequest request = repairBlock.requestRepair(inventory);
                if (request != null) {
                    this.repairRequests.put(player.getUniqueId(), request);
                }
            }
        }
    }
}
Also used : RepairBlockInventory(org.cubeengine.module.itemrepair.repair.blocks.RepairBlock.RepairBlockInventory) Primary(org.spongepowered.api.event.block.InteractBlockEvent.Primary) RepairBlock(org.cubeengine.module.itemrepair.repair.blocks.RepairBlock) World(org.spongepowered.api.world.World) RepairRequest(org.cubeengine.module.itemrepair.repair.RepairRequest) Listener(org.spongepowered.api.event.Listener)

Example 2 with Primary

use of org.spongepowered.api.event.block.InteractBlockEvent.Primary in project modules-extra by CubeEngine.

the class MarketSignListener method onClick.

@Listener
public void onClick(InteractBlockEvent event, @First Player player) {
    if (!event.getTargetBlock().getLocation().isPresent()) {
        return;
    }
    Location<World> loc = event.getTargetBlock().getLocation().get();
    BlockType blockType = loc.getBlockType();
    if (!Arrays.asList(STANDING_SIGN, WALL_SIGN).contains(blockType)) {
        // Not even a sign -> ignore
        return;
    }
    boolean editMode = module.getEditModeCommand().hasUser(player);
    Boolean sneaking = player.get(Keys.IS_SNEAKING).get();
    boolean punch = event instanceof Primary;
    Optional<MarketSignData> mSignData = event.getTargetBlock().get(ImmutableMarketSignData.class).map(ImmutableMarketSignData::asMutable);
    if (mSignData.isPresent()) {
        manager.updateSignText(mSignData.get(), loc);
    }
    if (editMode) {
        if (!player.hasPermission(module.perms().EDIT_USE.getId())) {
            i18n.send(player, NEGATIVE, "You are not allowed to edit MarketSigns here");
            manager.exitEditMode(player);
            module.getEditModeCommand().removeUser(player);
            return;
        }
        if (// Its the active MarketSign -> Break or Modify Item
        manager.isActive(loc, player)) {
            if (// Do nothing if sneaking
            sneaking) {
                return;
            }
            if (punch) {
                if (manager.tryBreakActive(player)) {
                    return;
                } else {
                    // TODO resend signtext?
                    event.setCancelled(true);
                }
            }
            if (!punch && player.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
                manager.modifyItemActive(player, player.getItemInHand(HandTypes.MAIN_HAND).get());
            }
            event.setCancelled(true);
            return;
        }
        if (// Its another MarketSign
        mSignData.isPresent()) {
            // Set Current Sign
            manager.setSign(loc, player);
            event.setCancelled(true);
            return;
        }
        // Its a sign ; but no data yet
        if (// Sneak Interact -> Allow Interaction
        sneaking && !punch) {
            return;
        }
        if (// Sneak AND Punch to convert sign
        sneaking && punch) {
            // sneak + punch -> convert it!
            MarketSignData data = new MarketSignData();
            data.setID(UUID.randomUUID());
            data.setOwner(player.getUniqueId());
            // Sign converted! Now set active
            loc.offer(data);
            manager.setSign(loc, player);
            event.setCancelled(true);
            return;
        }
        i18n.send(player, NEGATIVE, "That is not a market sign!");
        i18n.send(player, NEUTRAL, "Sneak and punch the sign convert it.");
        event.setCancelled(true);
        return;
    }
    if (!mSignData.isPresent()) {
        return;
    }
    MarketSignData data = mSignData.get();
    manager.executeSignAction(data, loc, player, event instanceof InteractBlockEvent.Secondary);
    if (blockType != AIR) {
        // TODO sign somehow is retained /w some invalid data
        loc.offer(data);
        manager.updateSignText(data, loc);
    // event.setCancelled(true);
    }
}
Also used : ImmutableMarketSignData(org.cubeengine.module.signmarket.data.ImmutableMarketSignData) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) BlockType(org.spongepowered.api.block.BlockType) Primary(org.spongepowered.api.event.block.InteractBlockEvent.Primary) MarketSignData(org.cubeengine.module.signmarket.data.MarketSignData) ImmutableMarketSignData(org.cubeengine.module.signmarket.data.ImmutableMarketSignData) World(org.spongepowered.api.world.World) Listener(org.spongepowered.api.event.Listener)

Aggregations

Listener (org.spongepowered.api.event.Listener)2 Primary (org.spongepowered.api.event.block.InteractBlockEvent.Primary)2 World (org.spongepowered.api.world.World)2 RepairRequest (org.cubeengine.module.itemrepair.repair.RepairRequest)1 RepairBlock (org.cubeengine.module.itemrepair.repair.blocks.RepairBlock)1 RepairBlockInventory (org.cubeengine.module.itemrepair.repair.blocks.RepairBlock.RepairBlockInventory)1 ImmutableMarketSignData (org.cubeengine.module.signmarket.data.ImmutableMarketSignData)1 MarketSignData (org.cubeengine.module.signmarket.data.MarketSignData)1 BlockType (org.spongepowered.api.block.BlockType)1 InteractBlockEvent (org.spongepowered.api.event.block.InteractBlockEvent)1