Solved Events not getting called

Discussion in 'Spigot Plugin Development' started by Spedwards, Jun 14, 2016.

  1. I'm customising Essentials a bit, adding in events, and the first one I've added isn't working.

    Essentials Trade.java:

    Code (Text):
    public Map<Integer, ItemStack> pay(final IUser user, final OverflowType type) throws MaxMoneyException {
        EssentialsPayUserEvent EssentialsPayUserEvent = new EssentialsPayUserEvent(this);
        if (!EssentialsPayUserEvent.isCancelled()) {
            if (EssentialsPayUserEvent.getMoney() != null && EssentialsPayUserEvent.getMoney().signum() > 0) {
                if (ess.getSettings().isDebug()) {
                    ess.getLogger().log(Level.INFO, "paying user " + user.getName() + " via trade " + EssentialsPayUserEvent.getMoney().toPlainString());
                }
                user.giveMoney(EssentialsPayUserEvent.getMoney());
            }
            if (EssentialsPayUserEvent.getItemStack() != null) {
                // This stores the would be overflow
                Map<Integer, ItemStack> overFlow = InventoryWorkaround.addAllItems(user.getBase().getInventory(), EssentialsPayUserEvent.getItemStack());

                if (overFlow != null) {
                    switch (type) {
                        case ABORT:
                            if (ess.getSettings().isDebug()) {
                                ess.getLogger().log(Level.INFO, "abort paying " + user.getName() + " itemstack " + EssentialsPayUserEvent.getItemStack().toString() + " due to lack of inventory space ");
                            }

                            return overFlow;

                        case RETURN:
                            // Pay the user the items, and return overflow
                            final Map<Integer, ItemStack> returnStack = InventoryWorkaround.addItems(user.getBase().getInventory(), EssentialsPayUserEvent.getItemStack());
                            user.getBase().updateInventory();

                            if (ess.getSettings().isDebug()) {
                                ess.getLogger().log(Level.INFO, "paying " + user.getName() + " partial itemstack " + EssentialsPayUserEvent.getItemStack().toString() + " with overflow " + returnStack.get(0).toString());
                            }

                            return returnStack;

                        case DROP:
                            // Pay the users the items directly, and drop the rest, will always return no overflow.
                            final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItems(user.getBase().getInventory(), EssentialsPayUserEvent.getItemStack());
                            final Location loc = user.getBase().getLocation();
                            for (ItemStack loStack : leftOver.values()) {
                                final int maxStackSize = loStack.getType().getMaxStackSize();
                                final int stacks = loStack.getAmount() / maxStackSize;
                                final int leftover = loStack.getAmount() % maxStackSize;
                                final Item[] itemStacks = new Item[stacks + (leftover > 0 ? 1 : 0)];
                                for (int i = 0; i < stacks; i++) {
                                    final ItemStack stack = loStack.clone();
                                    stack.setAmount(maxStackSize);
                                    itemStacks[i] = loc.getWorld().dropItem(loc, stack);
                                }
                                if (leftover > 0) {
                                    final ItemStack stack = loStack.clone();
                                    stack.setAmount(leftover);
                                    itemStacks[stacks] = loc.getWorld().dropItem(loc, stack);
                                }
                            }
                            if (ess.getSettings().isDebug()) {
                                ess.getLogger().log(Level.INFO, "paying " + user.getName() + " partial itemstack " + EssentialsPayUserEvent.getItemStack().toString() + " and dropping overflow " + leftOver.get(0).toString());
                            }
                    }
                } else if (ess.getSettings().isDebug()) {
                    ess.getLogger().log(Level.INFO, "paying " + user.getName() + " itemstack " + EssentialsPayUserEvent.getItemStack().toString());
                }
                user.getBase().updateInventory();
            }
            if (EssentialsPayUserEvent.getExperience() != null) {
                SetExpFix.setTotalExperience(user.getBase(), SetExpFix.getTotalExperience(user.getBase()) + EssentialsPayUserEvent.getExperience());
            }
        }
        return null;
    }
    EssentialsPayUserEvent.java

    Code (Text):
    public class EssentialsPayUserEvent extends Event implements Cancellable {
       
        private static final HandlerList handlers = new HandlerList();
        private Trade trade;
        private BigDecimal money;
        private ItemStack itemStack;
        private Integer exp;
        private boolean cancelled;
       
        public EssentialsPayUserEvent(Trade trade) {
            this.trade = trade;
            this.money = trade.getMoney();
            this.itemStack = trade.getItemStack();
            this.exp = trade.getExperience();
        }
       
        public ItemStack getItemStack() {
            return this.itemStack;
        }
       
        public void setItemStack(ItemStack itemStack) {
            this.itemStack = itemStack;
        }
       
        public BigDecimal getMoney() {
            return this.money;
        }
       
        public void setMoney(BigDecimal money) {
            this.money = money;
        }
       
        public Integer getExperience() {
            return this.exp;
        }
       
        public void setExperience(Integer exp) {
            this.exp = exp;
        }
       
        public Trade getTrade() {
            return this.trade;
        }
       
        public SignType getSignType() {
            return this.trade.getSign().getType();
        }
       
        public boolean isCancelled() {
            return this.cancelled;
        }
       
        public void setCancelled(boolean cancel) {
            this.cancelled = cancel;
        }
       
        public HandlerList getHandlers() {
            return handlers;
        }
       
        public static HandlerList getHandlerList() {
            return handlers;
        }
       
    }
    And then there's my perfectly fine listener:

    Code (Text):

    public class PayListener implements Listener {
       
        @EventHandler
        public void onPayment(EssentialsPayUserEvent event) {
            Main.log("Please work.");
        }

    }
    Main.java:

    Code (Text):
    PluginManager pm = getServer().getPluginManager();
    // ...
    pm.registerEvents(new PayListener(), this);
    The thing is, the log in the listener never goes off. Am I missing something stupidly obvious, or is there something else going on here?
     
  2. Just making the event doesn't call it. You have to call it with Bukkit.getServer().getPluginManager().callEvent
     
    • Informative Informative x 1
  3. Thank you! I knew I was forgetting something.