Search in sources :

Example 1 with RepairRequest

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

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

the class RepairBlock method requestRepair.

public RepairRequest requestRepair(RepairBlockInventory inventory) {
    Player player = inventory.player;
    Map<Integer, ItemStack> items = this.itemProvider.getRepairableItems(inventory.inventory);
    if (items.size() > 0) {
        Double price = calculatePrice(items.values());
        Text format = economy.getDefaultCurrency().format(new BigDecimal(price));
        if (this.config.breakPercentage > 0) {
            i18n.send(player, NEGATIVE, "Items will break with a chance of {decimal:2}%", this.config.breakPercentage);
        }
        if (this.config.failPercentage > 0) {
            i18n.send(player, NEGATIVE, "Items will not repair with a chance of {decimal:2}%", this.config.failPercentage);
        }
        if (this.config.looseEnchantmentsPercentage > 0) {
            i18n.send(player, NEGATIVE, "Items will loose all enchantments with a chance of {decimal:2}%", this.config.looseEnchantmentsPercentage);
        }
        if (this.config.costPercentage > 100) {
            i18n.send(player, NEUTRAL, "The repair would cost {txt#amount} (+{decimal:2}%)", format, this.config.costPercentage - 100);
        } else if (this.config.costPercentage < 100) {
            i18n.send(player, NEUTRAL, "The repair would cost {txt#amount} (-{decimal:2}%)", format, 100 - this.config.costPercentage);
        } else {
            i18n.send(player, NEUTRAL, "The repair would cost {txt#amount}", format);
        }
        UniqueAccount acc = economy.getOrCreateAccount(player.getUniqueId()).get();
        i18n.send(player, NEUTRAL, "You currently have {txt#balance}", economy.getDefaultCurrency().format(acc.getBalance(economy.getDefaultCurrency())));
        i18n.send(player, POSITIVE, "{text:Leftclick} again to repair all your damaged items.");
        return new RepairRequest(this, inventory, items, price);
    } else {
        i18n.send(player, NEGATIVE, "There are no items to repair!");
    }
    return null;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount) Text(org.spongepowered.api.text.Text) ItemStack(org.spongepowered.api.item.inventory.ItemStack) RepairRequest(org.cubeengine.module.itemrepair.repair.RepairRequest) BigDecimal(java.math.BigDecimal)

Aggregations

RepairRequest (org.cubeengine.module.itemrepair.repair.RepairRequest)2 BigDecimal (java.math.BigDecimal)1 RepairBlock (org.cubeengine.module.itemrepair.repair.blocks.RepairBlock)1 RepairBlockInventory (org.cubeengine.module.itemrepair.repair.blocks.RepairBlock.RepairBlockInventory)1 Player (org.spongepowered.api.entity.living.player.Player)1 Listener (org.spongepowered.api.event.Listener)1 Primary (org.spongepowered.api.event.block.InteractBlockEvent.Primary)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1 UniqueAccount (org.spongepowered.api.service.economy.account.UniqueAccount)1 Text (org.spongepowered.api.text.Text)1 World (org.spongepowered.api.world.World)1