Search in sources :

Example 1 with RepairBlock

use of org.cubeengine.module.itemrepair.repair.blocks.RepairBlock in project modules-extra by CubeEngine.

the class RepairBlockManager method removePlayer.

public void removePlayer(final Player player) {
    if (player == null) {
        return;
    }
    RepairBlockInventory inventory;
    for (RepairBlock repairBlock : this.repairBlocks.values()) {
        inventory = repairBlock.removeInventory(player);
        if (inventory != null) {
            final World world = player.getWorld();
            final Location loc = player.getLocation();
            Sponge.getCauseStackManager().pushCause(player);
            for (Inventory slot : inventory.inventory) {
                if (slot.peek().isPresent()) {
                    Entity drop = world.createEntity(EntityTypes.ITEM, loc.getPosition());
                    drop.offer(Keys.REPRESENTED_ITEM, slot.peek().get().createSnapshot());
                    world.spawnEntity(drop);
                }
            }
        }
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) RepairBlockInventory(org.cubeengine.module.itemrepair.repair.blocks.RepairBlock.RepairBlockInventory) RepairBlock(org.cubeengine.module.itemrepair.repair.blocks.RepairBlock) World(org.spongepowered.api.world.World) Inventory(org.spongepowered.api.item.inventory.Inventory) RepairBlockInventory(org.cubeengine.module.itemrepair.repair.blocks.RepairBlock.RepairBlockInventory) Location(org.spongepowered.api.world.Location)

Example 2 with RepairBlock

use of org.cubeengine.module.itemrepair.repair.blocks.RepairBlock 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)

Aggregations

RepairBlock (org.cubeengine.module.itemrepair.repair.blocks.RepairBlock)2 RepairBlockInventory (org.cubeengine.module.itemrepair.repair.blocks.RepairBlock.RepairBlockInventory)2 World (org.spongepowered.api.world.World)2 RepairRequest (org.cubeengine.module.itemrepair.repair.RepairRequest)1 Entity (org.spongepowered.api.entity.Entity)1 Listener (org.spongepowered.api.event.Listener)1 Primary (org.spongepowered.api.event.block.InteractBlockEvent.Primary)1 Inventory (org.spongepowered.api.item.inventory.Inventory)1 Location (org.spongepowered.api.world.Location)1