Solved Very Odd Problem...

Discussion in 'Plugin Development' started by BTTFHamsterZK, Jul 13, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    BTTFHamsterZK

    Hello people. I am here right now, with a problem. I have no problem coding, and I know how to code very well, but I may have came to an error, or just a derp. No matter what I do, this one listener does not work at all.
    Here is the code with the inventory, and the listener.
    Code:
    package me.BattleHamster.Inventories;
    
    import java.sql.SQLException;
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.BattleHamster.MySQL.Stats;
    
    public class PurchaseInv implements Listener {
       
        public static void openPurchaseInventory(Player player) throws SQLException {
            Inventory inv = Bukkit.createInventory(player, 54, "§aPurchase Inventory");
            {
                ItemStack coins = new ItemStack(Material.GOLD_INGOT);
                ItemMeta im = coins.getItemMeta();
                im.setDisplayName("§e" + Stats.getCoins(player) + "§c Coins");
                coins.setItemMeta(im);
                inv.setItem(4, coins);
               
                ItemStack border = new ItemStack(Material.STAINED_GLASS_PANE);
                 ItemMeta item2 = border.getItemMeta();
                 item2.setDisplayName("§7§oBORDER");
                 border.setItemMeta(item2);
                 inv.setItem(0, border);
                 inv.setItem(1, border);
                 inv.setItem(2, border);
                 inv.setItem(3, border);
                 inv.setItem(5, border);
                 inv.setItem(6, border);
                 inv.setItem(7, border);
                 inv.setItem(8, border);
                
                ItemStack huntsman = new ItemStack(Material.ENDER_CHEST);
                ItemMeta im0 = huntsman.getItemMeta();
                im0.setDisplayName("§aPurchase Huntsman Crate");
                ArrayList<String> lore = new ArrayList<String>();
                lore.add("§cPrice: §e100 Coins");
                lore.add("§7 You have (" + Stats.getHuntsman(player) + ") of this item");
                im0.setLore(lore);
                huntsman.setItemMeta(im0);
                inv.setItem(19, huntsman);
            }
            player.openInventory(inv);
        }
       
        public static void openPurchaseConfirmInventory(Player player, String purchase){
            Inventory inv1 = Bukkit.createInventory(player, 9, "§aConfirm Purchase");
            {
                ItemStack cancel = new ItemStack(Material.REDSTONE_BLOCK);
                ItemMeta im = cancel.getItemMeta();
                im.setDisplayName("§cCancel Purchase");
                ArrayList<String> lore = new ArrayList<String>();
                lore.add("§7No fees will be given to your account.");
                im.setLore(lore);
                cancel.setItemMeta(im);
                inv1.setItem(1, cancel);
               
                ItemStack item = new ItemStack(Material.GOLD_INGOT);
                ItemMeta im0 = item.getItemMeta();
                ArrayList<String> lore0 = new ArrayList<String>();
                if(purchase == "huntsman"){
                    im0.setDisplayName("§aHuntsman Crate x1");
                    lore0.add("§7Price: §e100");
                }
                im0.setLore(lore0);
                item.setItemMeta(im0);
                inv1.setItem(4, item);
               
                ItemStack confirm = new ItemStack(Material.EMERALD_BLOCK);
                ItemMeta im1 = confirm.getItemMeta();
                im1.setDisplayName("§aConfirm Purchase");
                ArrayList<String> lore1 = new ArrayList<String>();
                lore1.add("§7Fees may be charged to your account.");
                im1.setLore(lore1);
                confirm.setItemMeta(im1);
                inv1.setItem(8, confirm);
            }
            player.openInventory(inv1);
        }
       
        @EventHandler
        public void onInvClick(InventoryClickEvent event){
            Player player = (Player) event.getWhoClicked();
            ItemStack clicked = event.getCurrentItem();
            if(event.getInventory().getName().equals("§aPurchase Inventory")){
                if(clicked != null){
                    if(clicked.hasItemMeta()){
                       
                        if(clicked.getType() == Material.STAINED_GLASS_PANE){
                            event.setCancelled(true);
                        }
                       
                        if(clicked.getType() == Material.GOLD_INGOT){
                            event.setCancelled(true);
                        }
                       
                        if(clicked.getType() == Material.ENDER_CHEST){
                            if(clicked.getItemMeta().getDisplayName().equals("§aPurchase Huntsman Crate")){
                                openPurchaseConfirmInventory(player, "huntsman");
                            }
                        }                   
                    }
                }
            }
            if(event.getInventory().getName().equals("§aConfirm Purchase")){
                if(clicked != null){
                    if(clicked.getType() == Material.REDSTONE_BLOCK){
                        player.closeInventory();
                        event.setCancelled(true);
                    }
                    if(clicked.getType() == Material.GOLD_INGOT){
                        event.setCancelled(true);
                    }
                    if(clicked.getType() == Material.EMERALD_BLOCK){
                        event.setCancelled(true);
                    }
                }
            }
        }
    }
    
    There is no stacktrace, or I would provide that.
    Here is my Main class, proving that I have registered the listener.
    Code:
    package me.BattleHamster.Core;
    
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
    
    import me.BattleHamster.Commands.Build;
    import me.BattleHamster.Commands.CheckLag;
    import me.BattleHamster.Commands.Clear;
    import me.BattleHamster.Commands.Fly;
    import me.BattleHamster.Commands.MOTD;
    import me.BattleHamster.Commands.MSG;
    import me.BattleHamster.Commands.Ping;
    import me.BattleHamster.Commands.Promote;
    import me.BattleHamster.Commands.Purchase;
    import me.BattleHamster.Commands.Restart;
    import me.BattleHamster.Commands.Seen;
    import me.BattleHamster.Commands.Spy;
    import me.BattleHamster.Commands.StaffChat;
    import me.BattleHamster.Commands.Transfer;
    import me.BattleHamster.Commands.Vanish;
    import me.BattleHamster.Crates.CrateInventory;
    import me.BattleHamster.Crates.CoinBox.COINInventoryController;
    import me.BattleHamster.Crates.Huntsman.HUNTSMANInventoryController;
    import me.BattleHamster.Crates.Wizard.WIZARDInventoryController;
    import me.BattleHamster.Inventories.Cosmetics;
    import me.BattleHamster.Inventories.PlayersInventory;
    import me.BattleHamster.Inventories.PurchaseInv;
    import me.BattleHamster.Inventories.WorldInventory;
    import me.BattleHamster.Listeners.CMDBlock;
    import me.BattleHamster.Listeners.ChatLayout;
    import me.BattleHamster.Listeners.ColorCodes;
    import me.BattleHamster.Listeners.DisplayName;
    import me.BattleHamster.Listeners.FoodEvent;
    import me.BattleHamster.Listeners.GrapplingHookController;
    import me.BattleHamster.Listeners.HubDamageCancel;
    import me.BattleHamster.Listeners.InventoryListener;
    import me.BattleHamster.Listeners.ItemDrop;
    import me.BattleHamster.Listeners.ItemInteract;
    import me.BattleHamster.Listeners.JoinMessage;
    import me.BattleHamster.Listeners.JoinTeleport;
    import me.BattleHamster.Listeners.LaunchPads;
    import me.BattleHamster.Listeners.MOTDChanger;
    import me.BattleHamster.Listeners.NewJoin;
    import me.BattleHamster.Listeners.NoBreak;
    import me.BattleHamster.Listeners.PerWorldChat;
    import me.BattleHamster.Listeners.PlayerJoin;
    import me.BattleHamster.Listeners.PlayerLeave;
    import me.BattleHamster.Listeners.SwearFilter;
    import me.BattleHamster.Listeners.WeatherChange;
    import me.BattleHamster.Listeners.WorldHide;
    import me.BattleHamster.MySQL.MySQLManager;
    
    
    public class Core extends JavaPlugin {
        public static ConsoleCommandSender console;
        public static Plugin plugin;
        public static boolean useMySQL = false;
        public static BukkitScheduler scheduler = Bukkit.getScheduler();
        public static ArrayList<Player> builders = new ArrayList<Player>();
        public static ArrayList<Player> hiddenplayers = new ArrayList<Player>();
        public MySQLManager mysql = new MySQLManager(this);
       
       
        @Override
        public void onEnable(){
            try {
                mysql.setupDB();
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
                Bukkit.getPluginManager().disablePlugin(this);
            }
            if(!(getDataFolder().exists())){
                saveDefaultConfig();
            }
            plugin = this;
            BukkitScheduler scheduler = (BukkitScheduler) Bukkit.getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable(){
                @Override
                public void run(){
                    CheckLag.sendLagMessage();
                }
            }, 0L, 300L);
            getCommand("SC").setExecutor(new StaffChat());
            getCommand("Clear").setExecutor(new Clear());
            getCommand("Ping").setExecutor(new Ping());
            getCommand("SetRank").setExecutor(new Promote());
            getCommand("Purchase").setExecutor(new Purchase());
            getCommand("checkLag").setExecutor(new CheckLag());
            getCommand("Seen").setExecutor(new Seen());
            getCommand("Fly").setExecutor(new Fly());
            getCommand("MSG").setExecutor(new MSG());
            getCommand("Spy").setExecutor(new Spy());
            getCommand("MOTD").setExecutor(new MOTD());
            getCommand("Color").setExecutor(new ColorCodes());
            getCommand("Build").setExecutor(new Build());
            getCommand("GM").setExecutor(new Build());
            getCommand("RestartCMD").setExecutor(new Restart());
            getCommand("Vanish").setExecutor(new Vanish());
            getCommand("Transfer").setExecutor(new Transfer());
            getServer().getPluginManager().registerEvents(new PurchaseInv(), this);
            getServer().getPluginManager().registerEvents(new StaffChat(), this);
            getServer().getPluginManager().registerEvents(new Spy(), this);
            getServer().getPluginManager().registerEvents(new CMDBlock(), this);
            getServer().getPluginManager().registerEvents(new PerWorldChat(), this);
            getServer().getPluginManager().registerEvents(new PlayerLeave(), this);
            getServer().getPluginManager().registerEvents(new Restart(), this);
            getServer().getPluginManager().registerEvents(new ColorCodes(), this);
            getServer().getPluginManager().registerEvents(new WorldHide(), this);
            getServer().getPluginManager().registerEvents(new NewJoin(), this);
            getServer().getPluginManager().registerEvents(new WorldInventory(), this);
            getServer().getPluginManager().registerEvents(new JoinTeleport(), this);
            getServer().getPluginManager().registerEvents(new MOTDChanger(), this);
            getServer().getPluginManager().registerEvents(new ItemDrop(), this);
            getServer().getPluginManager().registerEvents(new GrapplingHookController(), this);
            getServer().getPluginManager().registerEvents(new COINInventoryController(), this);
            getServer().getPluginManager().registerEvents(new WIZARDInventoryController(), this);
            getServer().getPluginManager().registerEvents(new PlayersInventory(), this);
            getServer().getPluginManager().registerEvents(new CrateInventory(), this);
            getServer().getPluginManager().registerEvents(new HUNTSMANInventoryController(), this);
            getServer().getPluginManager().registerEvents(new Cosmetics(), this);
            getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
            getServer().getPluginManager().registerEvents(new FoodEvent(), this);
            getServer().getPluginManager().registerEvents(new ItemInteract(), this);
            getServer().getPluginManager().registerEvents(new InventoryListener(), this);
            getServer().getPluginManager().registerEvents(new ChatLayout(), this);
            getServer().getPluginManager().registerEvents(new SwearFilter(), this);
            getServer().getPluginManager().registerEvents(new DisplayName(), this);
            getServer().getPluginManager().registerEvents(new JoinMessage(), this);
            getServer().getPluginManager().registerEvents(new WeatherChange(), this);
            getServer().getPluginManager().registerEvents(new HubDamageCancel(), this);
            getServer().getPluginManager().registerEvents(new NoBreak(), this);
            getServer().getPluginManager().registerEvents(new LaunchPads(), this);
            scheduler.scheduleSyncRepeatingTask(plugin, new Runnable(){
                @Override
                public void run(){
                    for(Player ppl : builders){
                        ppl.sendMessage("§8§l>>§r§7 You are in build mode. If you are not building, run the command '/build' to disable.");
                    }
                }
            }, 0L, 600L);
            scheduler.scheduleSyncRepeatingTask(plugin, new Runnable(){
                @Override
                public void run(){
                    Random random = new Random();
                    int randomInt = random.nextInt(2);
                    if(randomInt == 0){
                        Bukkit.broadcastMessage("§b§l>>§r§a Were you previously a staff rank? Contact an Owner/Developer to sort out the issue!");
                    }
                    if(randomInt == 1){
                        Bukkit.broadcastMessage("§b§l>>§r§a Find any bugs? Missing features? Have a suggestion? Contact a Developer!");
                    }
                }
            }, 0L, 6000L);
        }
       
        @Override
        public void onDisable(){
            try {
                mysql.closeDB();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            for(Player player : Bukkit.getOnlinePlayers()){
                player.kickPlayer("§4Server has reloaded. Please rejoin.");
            }
            plugin = null;
        }
    
    }
    
    Please let me know what my problem is. The inventory that doesn't work is the one called "§aConfirm Purchase". the other one in it called "§aPurchase Inventory" works fine.
     
  2. use inventory.getTitle() instead of inventory.getName()
     
  3. Offline

    BTTFHamsterZK

    @FisheyLP This is not the problem. I tried using getTitle() instead of getName() but still had the same result.
    Is there even a reason this is not working? I don't see anything wrong with my code.
     
  4. Offline

    ampayne2

    You need a slight delay of 3 ticks before opening the purchase confirm inventory, the purchase inventory needs time to close first
     
  5. Offline

    BTTFHamsterZK

    The inventory opens up fine, but just none of the listeners read the inventory. It's like i didn't register it, but I did.
    None the less, I will still try that.

    WOW That actually worked! Thank you so much! I will have to remember this for future projects. I have been wondering what was going on. I thought it was just a derp, but apparently not XD Thank you my friend @ampayne2

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  6. Offline

    ampayne2

    Happy to help! :)

    Also if you run into any more issues try manually closing the first inventory after 1 tick as well.
    I'm not sure if this is still necessary but here is what I have used for a while:
    https://github.com/ampayne2/Fallout...inja/amp/fallout/menu/ItemMenu.java#L237-L258
    If I'm just closing the inventory it only schedules the first runnable, if I'm opening another it schedules both
     
Thread Status:
Not open for further replies.

Share This Page