Search in sources :

Example 1 with CofferItem

use of com.skelril.skree.content.registry.item.currency.CofferItem in project Skree by Skelril.

the class GoldRushListener method onChestClose.

@Listener
public void onChestClose(InteractInventoryEvent.Close event) {
    Inventory inventory = event.getTargetInventory();
    if (inventory instanceof ContainerChest) {
        IInventory chestInv = ((ContainerChest) inventory).getLowerChestInventory();
        if (chestInv instanceof ILockableContainer) {
            String lockCode = ((ILockableContainer) chestInv).getLockCode().getLock();
            Optional<Player> optPlayer = Optional.ofNullable(tileEntityClaimMap.remove(lockCode));
            if (optPlayer.isPresent()) {
                Player player = optPlayer.get();
                ((ILockableContainer) chestInv).setLockCode(LockCode.EMPTY_CODE);
                Optional<GoldRushInstance> optInst = manager.getApplicableZone(player);
                if (!optInst.isPresent()) {
                    return;
                }
                // TODO Sponge port
                GoldRushInstance inst = optInst.get();
                List<org.spongepowered.api.item.inventory.ItemStack> toEvaluate = new ArrayList<>();
                ArrayDeque<org.spongepowered.api.item.inventory.ItemStack> toReturn = new ArrayDeque<>();
                for (int i = 0; i < chestInv.getSizeInventory(); ++i) {
                    ItemStack stack = chestInv.getStackInSlot(i);
                    if (stack == ItemStack.EMPTY) {
                        continue;
                    }
                    if (stack.getItem() instanceof CofferItem) {
                        toEvaluate.add(tf(stack));
                    } else {
                        toReturn.add(tf(stack));
                    }
                    chestInv.setInventorySlotContents(i, ItemStack.EMPTY);
                }
                BigDecimal value = BigDecimal.ZERO;
                for (org.spongepowered.api.item.inventory.ItemStack stack : toEvaluate) {
                    value = value.add(new BigDecimal(CofferValueMap.inst().getValue(Collections.singleton(stack)).orElse(BigInteger.ZERO)));
                }
                inst.cofferRisk.put(player.getUniqueId(), value);
                for (Inventory slot : player.getInventory().slots()) {
                    if (toReturn.isEmpty()) {
                        break;
                    }
                    if (slot.size() == 0) {
                        slot.set(toReturn.poll());
                    }
                }
                if (!toReturn.isEmpty()) {
                    new ItemDropper(player.getLocation()).dropStacks(toReturn, SpawnTypes.PLUGIN);
                }
                player.sendMessage(Text.of(TextColors.YELLOW, "You are now risking ", format(value), " coffers."));
                MessageChannel targetChannel = inst.getPlayerMessageChannel(PlayerClassifier.SPECTATOR);
                targetChannel.send(Text.of(TextColors.YELLOW, "Group risk of ", format(inst.getTotalRisk()), " coffers."));
                targetChannel.send(Text.of(TextColors.YELLOW, "Risk of lava ", inst.getChanceOfLava(), " / 100."));
                targetChannel.send(Text.of(TextColors.YELLOW, "Security system delay ", inst.getBaseTime(), " +/- ", inst.getTimeVariance(), " seconds."));
            }
        }
    }
}
Also used : ItemDropper(com.skelril.nitro.item.ItemDropper) ILockableContainer(net.minecraft.world.ILockableContainer) ContainerChest(net.minecraft.inventory.ContainerChest) IInventory(net.minecraft.inventory.IInventory) Player(org.spongepowered.api.entity.living.player.Player) BigDecimal(java.math.BigDecimal) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) CofferItem(com.skelril.skree.content.registry.item.currency.CofferItem) ItemStack(net.minecraft.item.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) IInventory(net.minecraft.inventory.IInventory) Listener(org.spongepowered.api.event.Listener)

Aggregations

ItemDropper (com.skelril.nitro.item.ItemDropper)1 CofferItem (com.skelril.skree.content.registry.item.currency.CofferItem)1 BigDecimal (java.math.BigDecimal)1 ContainerChest (net.minecraft.inventory.ContainerChest)1 IInventory (net.minecraft.inventory.IInventory)1 ItemStack (net.minecraft.item.ItemStack)1 ILockableContainer (net.minecraft.world.ILockableContainer)1 Player (org.spongepowered.api.entity.living.player.Player)1 Listener (org.spongepowered.api.event.Listener)1 Inventory (org.spongepowered.api.item.inventory.Inventory)1 MessageChannel (org.spongepowered.api.text.channel.MessageChannel)1