Updating GameMode not working

Discussion in 'Plugin Help/Development/Requests' started by Manu4021, Jun 22, 2015.

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

    Manu4021

    Hey, I've ran into a little issue here. I'm trying to create a spectator-inventory useable with the SpectatorMode by Mojang. The first Problem here is, that I can't use any events while in SpectatorMode because Bukkit isn't calling them. So I've made a little hack: Before it opens the inventory your gamemode is set to Adventure and on close it should be set to Spectator again. Which works perfectly except for the part "Set his deamn Gamemode back to Spectator"...

    The methods are all called & executed, I don't have any issues with that, the only problem is the updating (And setting the gamemode multiple times to spectator doesn't help, too, neither waiting 500 ms). So here's my code:

    Code:
    @EventHandler
        public void onInteract(PlayerInteractEvent e)
        {
            Inventory inv = Bukkit.createInventory(null, 18, "Blah");
           
            // More Blah
           
            p.setGameMode(GameMode.ADVENTURE);
            p.openInventory(inv);
           
            System.out.println("Mode = Adventure oO " + p.getGameMode()); // Debug
        }
       
        @EventHandler
        public void onClose(InventoryCloseEvent e)
        {
            Player p = (Player) e.getPlayer();
           
            // Blah
           
            p.setGameMode(GameMode.SPECTATOR);
            System.out.println("Mode = Spectator oO " + p.getGameMode()); // This should print out 'Mode = Spectator oO ADVENTURE'
        }
    
    Of course the GameMode is updated before to Spectator. Hope anyone can solve this :)

    Manu
     
  2. Offline

    ark9026

    @Manu4021
    What build of bukkit are you using?
     
  3. Offline

    Manu4021

    @ark9026 "CraftBukkit version git-Spigot-50e6452-unknown (MC: 1.8) (Implementing API version 1.8-R0.1-SNAPSHOT)"
     
  4. Offline

    Zombie_Striker

    @Manu4021
    Are you sure the event is firing? Is //blah interfering with what you're doing?
     
  5. Offline

    Manu4021

    @Zombie_Striker

    Sorry for replying so late. Yep, 100% sure and nope, //blah is always code from the plugin itself checking stuff or has something to do with the logic behind. There are no errors thrown before and there is also no return of this method done before. So it has to be executed (And it is as there was a System-out after which was executed). In case you wanna be shure, here's the full code of this class ;)

    Code:
    public class PlayerSpectationInteractListener implements Listener
    {
        @EventHandler
        public void onInteract(PlayerInteractEvent e)
        {
            if (!Teams.isSpectator(e.getPlayer()))
                return;
         
            if (e.getItem() != null)
                return;
         
            Player p = e.getPlayer();
         
            List<ItemStack> invContents = new ArrayList<>();
         
            for (Player lp : Teams.getArena(Teams.getArenaName(p)))
            {
                List<String> kits = KitDBHandler.getUsedKits(lp);
                Kits kit = null;
             
                if (!kits.isEmpty())
                    kit = Kits.getKit(kits.get(0));
             
                ItemStack head = kit == null ? new ItemStack(Material.SKULL_ITEM, 1, (short) 3) : kit.getItem();
                ItemMeta meta = head.getItemMeta();
             
                meta.setDisplayName((Teams.getTeam(lp).equalsIgnoreCase("cops") ? ChatColor.BLUE : ChatColor.RED) + lp.getName());
                meta.setLore(Arrays.asList("§aTeam: §f" + Teams.getTeam(lp), "§aKits: §f" + kits.toString().replace("[", "").replace("]", ""),
                        "§aHealth: §f" + lp.getHealthScale()));
             
                head.setItemMeta(meta);
             
                invContents.add(head);
            }
         
            Inventory inv = Bukkit.createInventory(null, 18, "Arena " + Teams.getArenaName(p));
         
            for (int i = 0; i < invContents.size(); i++)
            {
                inv.setItem(i, invContents.get(i));
            }
         
            ItemStack cops = new ItemStack(Material.WOOL, 1, (short) 14);
            ItemStack thieves = new ItemStack(Material.WOOL, 1, (short) 11);
         
            ItemMeta cm = cops.getItemMeta();
            ItemMeta tm = thieves.getItemMeta();
            cm.setDisplayName("§cCops");
            tm.setDisplayName("§9Thieves");
            cops.setItemMeta(cm);
            thieves.setItemMeta(tm);
         
            inv.setItem(16, cops);
            inv.setItem(17, thieves);
         
            p.setGameMode(GameMode.ADVENTURE);
            p.openInventory(inv);
         
            System.out.println("Mode = Adventure oO " + p.getGameMode());
        }
     
        @EventHandler
        public void onClose(InventoryCloseEvent e)
        {
            Player p = (Player) e.getPlayer();
         
            if (!Teams.isSpectator(p))
                return;
         
            if (!e.getInventory().getName().equalsIgnoreCase("Arena " + Teams.getArenaName(p)))
                return;
         
            try
            {
                Thread.sleep(500);
            }
            catch (InterruptedException ex)
            {
            }
         
            for (int i = 0; i < 4; i++)
                p.setGameMode(GameMode.SPECTATOR);
        }
     
        @EventHandler(ignoreCancelled = true)
        public void onInventoryInteract(InventoryClickEvent e)
        {
            Player p = (Player) e.getWhoClicked();
         
            if (!Teams.isSpectator(p))
                return;
         
            if (!e.getInventory().getTitle().equalsIgnoreCase("Arena " + Teams.getArenaName(p)))
                return;
         
            if (e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR)
                return;
         
            e.setCancelled(true);
         
            String name = e.getCurrentItem().getItemMeta().getDisplayName().replace(ChatColor.RED.toString(), "").replace(ChatColor.BLUE.toString(), "");
         
            System.out.println(name);
         
            Player target = Bukkit.getPlayer(name);
         
            if (target == null)
            {
                if (name == null)
                    return;
             
                Object[] select = new ArenaManager(Teams.getArenaName(p)).select("cops", "thieves");
             
                if (name.equalsIgnoreCase("Cops"))
                {
                    p.teleport(CopsVSThieves.parseLocation((String) select[0], true));
                }
                else if (name.equalsIgnoreCase("Thieves"))
                {
                    p.teleport(CopsVSThieves.parseLocation((String) select[1], true));
                }
             
                return;
            }
         
            Location targetLoc = target.getLocation();
         
            if (targetLoc.getWorld().getBlockAt((int) targetLoc.getX(), (int) targetLoc.getY() + 2, (int) targetLoc.getZ()).getType() != Material.AIR)
            {
                p.teleport(targetLoc);
            }
            else
            {
                targetLoc.setY(targetLoc.getY() + 2);
                p.teleport(targetLoc);
            }
        }
    }
    
     
    Last edited: Jun 24, 2015
  6. Offline

    Zombie_Striker

    @Manu4021
    Why are you trying to set his gamemode to spectator four times? NEVER use thread.sleep();, you're creating your own lag mechine.

    If the player closes his inventory, is he a spectator, and whats the name of the inventory.
     
  7. Offline

    mrCookieSlime

    Moved to Alternatives Section.
     
  8. Offline

    Manu4021

    @Zombie_Striker yep, I know. this was just some escalation of myself in hope that it will work if I wait or update it more than once, of course I don't use Thread.sleep in the main Minecraft-Thread. And even with 4 times setting the GameMode it didn't change, so there has to be some strange things going on :confused:
     
Thread Status:
Not open for further replies.

Share This Page