How to get my listener to work with Spigot Plugin?

Recently, I coded this plugin that would open up an inventory when you clicked on a player, but for some reason, the inventory isn't opening.

Main:

package me.jacobsscoots.main;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

import me.jacobsscoots.listeners.Admin;

public class Main extends JavaPlugin{

Admin admin;

List<String> toggle = new ArrayList<String>();

public void onEnable() {
    getLogger().info("Utils is now enabled");
    admin = new Admin();
    Bukkit.getServer().getPluginManager().registerEvents(admin, this);
}

public void onDisable() {
    getLogger().info("Utils is now disabled");
}

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    Player player = (Player) sender;
    PlayerInventory pi = player.getInventory();
    if(label.equalsIgnoreCase("admin")){
        if(!(sender instanceof Player)){
            sender.sendMessage(ChatColor.RED + "Staff Mode" + ChatColor.WHITE + " >> " + ChatColor.GRAY + "You muse be a player to use this command!");
            return false;
        }
        if(toggle.contains(player.getName())){
            player.sendMessage(ChatColor.RED + "Staff Mode" + ChatColor.WHITE + " >> " + ChatColor.GRAY + "You are no longer in staff mode.");
            toggle.remove(player.getName());

            pi.clear();

            return true;
        }

        player.sendMessage(ChatColor.RED + "Staff Mode" + ChatColor.WHITE + " >> " + ChatColor.GRAY + "You are now in staff mode.");
        toggle.add(player.getName());


        ItemStack paper = new ItemStack(Material.PAPER, 1);
        ItemMeta meta = paper.getItemMeta();
        meta.setDisplayName(ChatColor.RED + "Punish");
        paper.setItemMeta(meta);

        pi.addItem(paper);

        admin.setEnabled(true);

        return true;
    }
    return false;
}
}

Listener class:

package me.jacobsscoots.listeners;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import net.md_5.bungee.api.ChatColor;

public class Admin implements Listener{

private boolean enabled = false;

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onAdminEvent(PlayerInteractEntityEvent e) {
    if (!(e.getRightClicked() instanceof Player))
        return;

    if(enabled == false){
        return;
    }
    Player p = e.getPlayer();
    Player target = (Player) e.getRightClicked();

    if(p.isOp() && p.getInventory().getItemInMainHand().equals(Material.PAPER) && e.getRightClicked().getType() == EntityType.PLAYER){
        openGUI(p, target);
    }
}



@EventHandler
public void onInventoryClick(InventoryClickEvent event)
{
    if (event.isCancelled())
        return;

    if (!event.getInventory().getName().contains(" Punishment"))
        return;

    Player player = (Player) event.getWhoClicked();
    Player target = Bukkit.getPlayer(ChatColor.stripColor(event.getClickedInventory().getName().split(" ")[0]));
    ItemStack item = event.getCurrentItem();
    Integer slot = event.getSlot();

    if (item == null || item.getType() == Material.AIR || !item.hasItemMeta())
        return;

    if (slot == 20){
        target.kickPlayer(player.getName() + " Banned you for: " + item.getItemMeta().getDisplayName());
    }
    event.setCancelled(true);
}

private void openGUI(Player player, Player target) {

    Inventory inv = Bukkit.createInventory(null, 54, ChatColor.GRAY + target.getName() + " Punishment");

    ItemStack Ban_1 = new ItemStack(Material.IRON_BARDING, 1);
    ItemMeta BanMeta_1 = Ban_1.getItemMeta();
    BanMeta_1.setDisplayName(ChatColor.RED + "Hacked Clients, Cheats and Exploits");
    Ban_1.setItemMeta(BanMeta_1);

    ItemStack Ban_2 = new ItemStack(Material.IRON_BARDING, 1);
    ItemMeta BanMeta_2 = Ban_2.getItemMeta();
    BanMeta_2.setDisplayName(ChatColor.RED + "Spamming/Advertising");
    Ban_2.setItemMeta(BanMeta_2);

    ItemStack Ban_3 = new ItemStack(Material.IRON_BARDING, 1);
    ItemMeta BanMeta_3 = Ban_3.getItemMeta();
    BanMeta_3.setDisplayName(ChatColor.RED + "Not Respecting Players or Staff Members");
    Ban_3.setItemMeta(BanMeta_3);

    ItemStack Ban_4 = new ItemStack(Material.IRON_BARDING, 1);
    ItemMeta BanMeta_4 = Ban_4.getItemMeta();
    BanMeta_4.setDisplayName(ChatColor.RED + "DDOS/Dox Jokes and Threats");
    Ban_4.setItemMeta(BanMeta_4);

    ItemStack Ban_5 = new ItemStack(Material.IRON_BARDING, 1);
    ItemMeta BanMeta_5 = Ban_5.getItemMeta();
    BanMeta_5.setDisplayName(ChatColor.RED + "Mods that provide unfair advantages");
    Ban_5.setItemMeta(BanMeta_5);

    ItemStack Ban_6 = new ItemStack(Material.IRON_BARDING, 1);
    ItemMeta BanMeta_6 = Ban_6.getItemMeta();
    BanMeta_6.setDisplayName(ChatColor.RED + "Racism and Sexism");
    Ban_6.setItemMeta(BanMeta_6);

    ItemStack Ban_7 = new ItemStack(Material.IRON_BARDING, 1);
    ItemMeta BanMeta_7 = Ban_7.getItemMeta();
    BanMeta_7.setDisplayName(ChatColor.RED + "False Reporting");
    Ban_7.setItemMeta(BanMeta_7);

    ItemStack Ban_8 = new ItemStack(Material.IRON_BARDING, 1);
    ItemMeta BanMeta_8 = Ban_8.getItemMeta();
    BanMeta_8.setDisplayName(ChatColor.RED + "Greifing/Block Spam");
    Ban_8.setItemMeta(BanMeta_8);

    ItemStack Pane_1 = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 11);

    ItemStack Pane_2 = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 14);
    ItemMeta PaneMeta_2 = Pane_2.getItemMeta();
    PaneMeta_2.setDisplayName(ChatColor.RED + "Kick");
    Pane_2.setItemMeta(PaneMeta_2);

    ItemStack Pane_3 = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 10);
    ItemMeta PaneMeta_3 = Pane_3.getItemMeta();
    PaneMeta_3.setDisplayName(ChatColor.GREEN + "Warn");
    Pane_3.setItemMeta(PaneMeta_3);

    ItemStack Exit = new ItemStack(Material.BARRIER, 1, (short) 10);
    ItemMeta ExitMeta = Exit.getItemMeta();
    ExitMeta.setDisplayName(ChatColor.RED + "Exit GUI");
    Exit.setItemMeta(ExitMeta);

    ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);

    inv.setItem(0, Pane_1);
    inv.setItem(1, Pane_1);
    inv.setItem(2, Pane_1);
    inv.setItem(3, Pane_2);
    inv.setItem(4, skull);
    inv.setItem(5, Pane_2);
    inv.setItem(6, Pane_1);
    inv.setItem(7, Pane_1);
    inv.setItem(8, Pane_1);
    inv.setItem(20, Ban_1);
    inv.setItem(21, Ban_2);
    inv.setItem(22, Ban_3);
    inv.setItem(23, Ban_4);
    inv.setItem(24, Ban_5);
    inv.setItem(30, Ban_6);
    inv.setItem(31, Ban_7);
    inv.setItem(32, Ban_8);
    inv.setItem(45, Pane_1);
    inv.setItem(46, Pane_1);
    inv.setItem(47, Pane_1);
    inv.setItem(48, Pane_3);
    inv.setItem(49, Exit);
    inv.setItem(50, Pane_3);
    inv.setItem(51, Pane_1);
    inv.setItem(52, Pane_1);
    inv.setItem(53, Pane_1);

    player.openInventory(inv);
}

public boolean isEnabled(){
    return enabled;
}

public void setEnabled(boolean enabled){
    this.enabled = enabled;
}
}

Plugin.yml:

name: Utils
version: 1.4.1
description: Staff mode plugin for servers
author: SickNation
main: me.jacobsscoots.main.Main
database: false
commands:
admin:
description: Default toggle command for staff mode.
usage: Syntax error! Simply type /&lt;command&gt; to go into staff mode.

Answers


The comparison in the last statement of the PlayerInteractEntityEvent method:

p.getInventory().getItemInMainHand().equals(Material.PAPER)

will always be false, because getItemInMainHand() returns an ItemStack, Material.PAPER is an enumerator and the equals method in the ItemStack interface contains the line:

if (!(obj instanceof ItemStack)) {
    return false;
}

I'm guessing you meant to write

getItemInMainHand().getType().equals(Material.PAPER)

... comparing two Material enumerators. When I tested your code and changed that line, the GUI opened.


Need Your Help

Display iframe video at different sizes for different browsers?

html iframe video-streaming embed

I'm having a bit an issue with embedding an iframe video. I was wondering if it is possible to set the height and width of the iframe based on which browser is being used.

Why do we need to use NSAutoreleasepool for each thread?

objective-c ios nsautoreleasepool

In iOS app development, we are using NSAutoreleasePool to relinquish ownership of objects at a later point in time.