Do not allow to close Custom Inventory

Discussion in 'Spigot Plugin Help' started by Nighterance, Apr 18, 2017.

  1. Hello guys,
    I am currently trying to make so Player is not allowed to exit inventory(custom, i will check with if)
    I tryed with InventoryCancelEvent, but it did not allow to use e.setCanceled and that is why I started to check it on internet, but I was not able to find something like this. Would be nice if any of you would have any ideas! Thanks!
     
  2. You cannot prevent the client from closing an inventory. However, you can reopen the inventory as soon as they close it.
     
  3. And can you tell me please how to do it or direct me to the answer :D?
     
  4. on InventoryCloseEvent you can check if the inventory's name equals your inventory's name, and if it does then open it up when it's closed
    (Make sure to add more conditions so you can close it when you want to)
     
  5. Hmm, Thanks, now it is not allowing player to exit inventory, but it is working with error.
    When I freeze player, it opens inventory, then if player closes, he can walk(altought he shouldnt) and then inventory opens once again, but it brings a lot of error on console

    This is error:
    Code (Text):

    at sun.reflect.GeneratedMethodAccessor20.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_121]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleInventoryCloseEvent(CraftEventFactory.java:852) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1411) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callInventoryOpenEvent(CraftEventFactory.java:720) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.EntityPlayer.openContainer(EntityPlayer.java:663) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.entity.CraftHumanEntity.openInventory(CraftHumanEntity.java:185) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at me.Nighterance.main.Freeze.createMenu(Freeze.java:92) ~[?:?]
            at me.Nighterance.main.Freeze.onInvClose(Freeze.java:257) ~[?:?]

    Line 92 and 257
    Code (Text):

    92:      p.openInventory(inv);
    257:    createMenu(p);
     
     
  6. Send the complete class please.
     
  7. createMenu opens inventory for player
    Code (Text):

    public void createMenu(Player p){
            Inventory inv = Bukkit.getServer().createInventory(null, 9,  "Frozen! Choose option");
        //This is nr 1 item
         ItemStack item1 = new ItemStack(Material.BOOK_AND_QUILL);
         ItemMeta item1Meta = item1.getItemMeta();

         ArrayList<String> item1Lore = new ArrayList<String>();
         item1Lore.add(ChatColor.GREEN + "Skype -" + " " + "skype");
         
         item1Meta.setDisplayName(ChatColor.RED + "Skype");

         item1Meta.setLore(item1Lore);
         
         item1.setItemMeta(item1Meta);
         
         inv.setItem(2, item1);
        //This is nr 2 item
         
         
         ItemStack item2 = new ItemStack(Material.TNT);
         ItemMeta item2Meta = item2.getItemMeta();
         
         
         ArrayList<String> item2Lore = new ArrayList<String>();
         
         item2Lore.add(ChatColor.GREEN + "number2");
         
         item2Meta.setDisplayName(ChatColor.RED + "number2");

         item2Meta.setLore(item2Lore);
         
         item2.setItemMeta(item2Meta);
         
         inv.setItem(4, item2);
        //This is nr 3 item
         ItemStack item3 = new ItemStack(Material.SIGN);
         ItemMeta item3Meta = item3.getItemMeta();
         
         
         ArrayList<String> item3Lore = new ArrayList<String>();
         
         item3Lore.add(ChatColor.GREEN + "number3");
         
         item3Meta.setDisplayName(ChatColor.RED + "number3");

         item3Meta.setLore(item3Lore);
         
         item3.setItemMeta(item3Meta);
         
         inv.setItem(6, item3);
         p.openInventory(inv);
        }
     
    Code (Text):

      @EventHandler
      public void onInvClose(InventoryCloseEvent e)
      {
        Player p = (Player) e.getPlayer();
        if ((this.frozen.contains(p))) {
       
            this.createMenu(p); // have tryed without this as well
        }
      }

     
    Code (Text):
     if (cmd.getName().equalsIgnoreCase("freeze")) {
          if ((sender instanceof Player))
          {
            Player p = (Player)sender;
            if (p.hasPermission("nfreeze.freeze"))
            {
              if (args.length == 0)
              {
                p.sendMessage(this.prefix + ChatColor.YELLOW + ChatColor.translateAlternateColorCodes('&', getCustomConfig().getString("chooseplayer")));
                return true;
              }
              Player t = Bukkit.getServer().getPlayer(args[0]);
              if (t == null)
              {
                 
                p.sendMessage(this.prefix + ChatColor.RED + ChatColor.translateAlternateColorCodes('&', getCustomConfig().getString("not-online").replace("%Player%", args[0])));
                return true;
              }
              Player p1 = (Player)sender;
              String mySkype = getConfig().getString("adminskype." + p1.getName());
              this.frozen.add(t);
              createMenu(t);
              t.setAllowFlight(true);
              t.setHealth(20.0D);
              p1.sendMessage(this.prefix + ChatColor.GREEN + ChatColor.translateAlternateColorCodes('&', getCustomConfig().getString("is-now-frozen").replace("%Player%", t.getName())));
              if (getConfig().getString("messageway").equals("Skype")) {
                t.sendMessage(this.prefix + ChatColor.GREEN + "You have been frozen by " + ChatColor.YELLOW + p1.getName() + ChatColor.GREEN + " Add to skype" + ChatColor.YELLOW + " " + mySkype + ChatColor.GREEN + " to share your screen.");
              } else if (getConfig().getString("messageway").equals("TS3")) {
                t.sendMessage(this.prefix + ChatColor.GREEN + "You have been frozen by " + ChatColor.YELLOW + p1.getName() + ChatColor.GREEN + " Join to Our TS3 IP - " + ChatColor.YELLOW + " " + getConfig().getString("TS3-IP") + ChatColor.GREEN + " to share your screen.");
              } else if(getConfig().getString("messageway").equals("Link")){
                 t.sendMessage(this.prefix + ChatColor.GREEN + "You have been frozen by " + ChatColor.YELLOW + p1.getName() + ChatColor.GREEN + " Go to - " + ChatColor.YELLOW + " " + getConfig().getString("Link"));
              }else {
                t.sendMessage(this.prefix + ChatColor.GREEN + "You have been frozen by " + ChatColor.YELLOW + p1.getName());
              }
              t.sendMessage(this.prefix + ChatColor.GREEN + "Do not leave the server or you will be" + ChatColor.RED + " " + "banned!");
              t.sendMessage(this.prefix + ChatColor.BLUE + "Do you want to admit of hacking and get smaller ban length? Write" + " " + ChatColor.RED + "/ac or /admitcheating");
              return true;
            }
            p.sendMessage(this.prefix + ChatColor.YELLOW + "Plugin made by" + " " + ChatColor.GREEN + "Nighterance");
            p.sendMessage(this.prefix + ChatColor.RED + getCustomConfig().getString("nopermfreeze"));
          }
          else
          {
            sender.sendMessage("As a console, you can't freeze players. This feature may be created later on.");
          }
     
  8. Hmm, i found out when error occures, that when player exits menu for first time, there is nothing for 4 seconds, then plugin opens gui nonstop and after like 3 seconds it starts to work as it should. So maybe now anyone has idea :D?