Search in sources :

Example 1 with EasterlynCaptchas

use of com.easterlyn.EasterlynCaptchas in project Easterlyn by Easterlyn.

the class CompilationAmalgamator method update.

private void update(Inventory inventory, @Nullable Inventory captchaInv, ConfigurationSection storage) {
    ItemStack captchaTarget = null;
    for (ItemStack item : inventory.getContents()) {
        if (item == null || item.getType() == Material.AIR) {
            continue;
        }
        if (item.getAmount() != item.getMaxStackSize()) {
            continue;
        }
        if (item.getMaxStackSize() > 1 && item.getType().getMaxDurability() == 0 && !item.hasItemMeta()) {
            captchaTarget = item.clone();
            break;
        }
        // This is safe, no CME because we're iterating over a copied array
        inventory.removeItem(item);
        if (inventory.getLocation() != null) {
            this.ejectItem(inventory.getLocation(), item, storage);
        }
    }
    if (captchaTarget == null) {
        return;
    }
    if (inventory.getLocation() == null) {
        return;
    }
    if (captchaInv == null) {
        Location captchaStorage = inventory.getLocation().add(this.getDirection(storage).getRelativeDirection(Direction.NORTH).getRelativeVector(new Vector(0, 0, 1)));
        BlockState blockState = captchaStorage.getBlock().getState();
        if (!(blockState instanceof InventoryHolder)) {
            return;
        }
        captchaInv = ((InventoryHolder) blockState).getInventory();
    }
    boolean usedCaptcha = false;
    for (int i = 0; i < captchaInv.getSize(); i++) {
        ItemStack captcha = captchaInv.getItem(i);
        if (EasterlynCaptchas.isBlankCaptcha(captcha)) {
            captchaInv.setItem(i, ItemUtil.decrement(captcha, 1));
            usedCaptcha = true;
            break;
        }
    }
    if (!usedCaptcha) {
        return;
    }
    RegisteredServiceProvider<EasterlynCaptchas> registration = getMachines().getServer().getServicesManager().getRegistration(EasterlynCaptchas.class);
    if (registration == null) {
        return;
    }
    ItemStack newCaptcha = registration.getProvider().getCaptchaForItem(captchaTarget);
    inventory.removeItem(captchaTarget);
    this.ejectItem(inventory.getLocation(), newCaptcha, storage);
}
Also used : BlockState(org.bukkit.block.BlockState) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) InventoryHolder(org.bukkit.inventory.InventoryHolder) Location(org.bukkit.Location) EasterlynCaptchas(com.easterlyn.EasterlynCaptchas)

Example 2 with EasterlynCaptchas

use of com.easterlyn.EasterlynCaptchas in project Easterlyn by Easterlyn.

the class Dublexor method unCaptcha.

private Pair<ItemStack, Integer> unCaptcha(ItemStack potentialCaptcha) {
    try {
        Class.forName("com.easterlyn.EasterlynCaptchas");
    } catch (ClassNotFoundException e) {
        return new Pair<>(potentialCaptcha, 1);
    }
    RegisteredServiceProvider<EasterlynCaptchas> registration = getMachines().getServer().getServicesManager().getRegistration(EasterlynCaptchas.class);
    if (registration == null) {
        return new Pair<>(potentialCaptcha, 1);
    }
    int multiplier = 1;
    while (EasterlynCaptchas.isUsedCaptcha(potentialCaptcha)) {
        ItemStack newModInput = registration.getProvider().getItemByCaptcha(potentialCaptcha);
        if (newModInput == null || potentialCaptcha.isSimilar(newModInput)) {
            // Broken captcha, don't infinitely loop.
            potentialCaptcha = barrier;
            break;
        }
        multiplier = Math.multiplyExact(multiplier, Math.max(1, Math.abs(potentialCaptcha.getAmount())));
        potentialCaptcha = newModInput;
    }
    return new Pair<>(potentialCaptcha, multiplier);
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) EasterlynCaptchas(com.easterlyn.EasterlynCaptchas) Pair(com.github.jikoo.planarwrappers.tuple.Pair)

Aggregations

EasterlynCaptchas (com.easterlyn.EasterlynCaptchas)2 ItemStack (org.bukkit.inventory.ItemStack)2 Pair (com.github.jikoo.planarwrappers.tuple.Pair)1 Location (org.bukkit.Location)1 BlockState (org.bukkit.block.BlockState)1 InventoryHolder (org.bukkit.inventory.InventoryHolder)1 Vector (org.bukkit.util.Vector)1