Kick people with runnable

Discussion in 'Plugin Help/Development/Requests' started by XgXXSnipz, Jan 29, 2015.

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

    XgXXSnipz

    Hey, im trying to make it so when I click a certain item in a GUI it kicks them in 3 seconds, well im getting errors on this runnable:
    Code:
     Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this ,new Runnable() {
    ERROR:
    Code:
    Could not pass event InventoryClickEvent to Logout v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[Spigot.jar:git-Spigot-1649]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Spigot.jar:git-Spigot-1649]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:514) [Spigot.jar:git-Spigot-1649]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:499) [Spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:1477) [Spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PacketPlayInWindowClick.a(PacketPlayInWindowClick.java:15) [Spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PacketPlayInWindowClick.handle(PacketPlayInWindowClick.java:65) [Spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [Spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [Spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [Spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [Spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [Spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [Spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [Spigot.jar:git-Spigot-1649]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R4.CraftServer cannot be cast to org.bukkit.plugin.Plugin
        at me.CreeperSwagg34.Logout.GUI.onInventoryClick(GUI.java:64) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_60]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_60]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_60]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_60]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[Spigot.jar:git-Spigot-1649]
        ... 13 more
    
    CODE:
    Code:
     private Inventory inv;
         private ItemStack w, a;
        
         public GUI(Plugin p) {
             inv = Bukkit.getServer().createInventory(null, 63, ChatColor.GREEN.toString() + ChatColor.BOLD + "Game Menu");
           
             w = createItem(new ItemStack(Material.DIRT), ChatColor.RED.toString() + ChatColor.BOLD + "Logout of server" + ChatColor.YELLOW + "<=> Click me" );
             a = createItem(new ItemStack(Material.ARROW), ChatColor.YELLOW.toString() + ChatColor.BOLD + "Go back" + ChatColor.YELLOW + "<= Click me" );
      
           
            
            
             inv.setItem(4, w);
             inv.setItem(54, a);
            
             Bukkit.getServer().getPluginManager().registerEvents(this, p);
         }
         private ItemStack createItem(ItemStack itemStack, String name) {
             ItemStack i = new ItemStack(itemStack);
             ItemMeta im = i.getItemMeta();
             im.setDisplayName(name);
             im.setLore(Arrays.asList(ChatColor.YELLOW.toString() + ChatColor.BOLD + "^Preform command"));
             i.setItemMeta(im);
             return i;
        }
        public void show(Player p) {
             p.openInventory(inv);
         }
        
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
           
           
            final Player p = (Player) e.getWhoClicked();
           
           
                if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
                if (e.getCurrentItem().getItemMeta() == null) return;
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Logout of server" + ChatColor.YELLOW + "<=> Click me")) {
                        e.setCancelled(true);
                        e.getWhoClicked().closeInventory();
                      
                       
                        Bukkit.broadcastMessage(ChatColor.RED + "You are are leaving in 3 seconds");
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this ,new Runnable() {
                            public void run() {
                                   p.kickPlayer(ChatColor.GREEN.toString() + ChatColor.BOLD + "[Hostel Network]" + ChatColor.YELLOW + "You have left the server");{
                                   
                                }
                            }
                      
                        }, 60);
                    }
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains( ChatColor.YELLOW.toString() + ChatColor.BOLD + "Go back" + ChatColor.YELLOW + "<= Click me")) {
                e.setCancelled(true);
                e.getWhoClicked().closeInventory();
                e.getWhoClicked().playEffect(EntityEffect.FIREWORK_EXPLODE);
          
            }
                           
        }
    Also how can I give a player a item in a certain inv. slot

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 30, 2015
  2. Offline

    8jy89hui

    How to give a person an item in a certain inv slot: 2 options: Use Player.getInventory().getContents() Which will return an array of they players inv then replace the slot number with the item then just Player.getInventory().setContents(Contents) Or use Player.getInventory().setItem(int, ItemStack) (I think it means SlotId, ItemStack but Idk because I have never used this.)

    For you original question this is how to do a Delayed task:
    Code:
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
      public void run() {
          getServer().broadcastMessage("Something");
      }
    }, 60L);
    I imagine that your problem is that you did },60); instead of }, 60L);
    Also preferably you would want to make the main class a static object called myPlugin or something which you can then use for things like :
    Code:
    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
    
     
  3. Invisible

    nverdier

    @8jy89hui Why would you want to make a static object? Do you know what a constructor is?
     
  4. Offline

    8jy89hui

    Yes m8.... I was giving advice. Personally I find it much easier to work with objects instead of class code. This is a personal preference that many other people use as well... I am simply trying to help the new coder understand a practice that many bukkit coders do.
     
  5. Invisible

    nverdier

    @8jy89hui It doesn't matter how many programmers use statics. There is nearly no situation where they are optimal, and they cause memory leaks. By posting that, you are showing 'new coders' bad practices, and how is that good? I hope you know that it's bad practice, but it seems you don't as you posted this as advice. Object Oriented Programming. It's based off of the concept of Objects. Statics completely go against this. Java happens to be an OO language. Statics are nearly never a good choice.
    How is using a constructor not using Objects? You are passing an Object to a new instance of the class... Passing an object! And by class code.. I don't even know what you are talking about.

    So please. Don't give bad advice, and don't teach bad practices. There are definitely 'new coders' on these forums, and they might find this. They will say "Oh, he's using statics. Eclipse is telling me to use a static! I'm going to use statics to fix all of my problems!" And then cause memory leaks for servers that download this plugin of theirs.

    tl;dr Read it! :p
     
  6. Offline

    8jy89hui

    I now understand what you are meaning. I still see no problem using statics.... I understand that you could simply place a constructor in every class but do you really want a constructor that is basically Public X (){} ? If you dont want to pass in any data to the constructor this seems like an EXTREMELY hackish way to make a class OO. Why not instead define the object in the way you are going to use it as. For example I have methods in Class X that I want to use. Putting public X () {} Is not really they way to go... I understand what you are getting at although I disagree.

    tl;dr Hackish much?
     
  7. Invisible

    nverdier

    @8jy89hui You don't need an empty constructor, if you have no constructor at all, you can still do 'TestClass tc = new TestClass()'. Then you can use tc.methodName(args_here) and it's fine. The problem with statics is memory leaks. You nearly always want instances for classes. In fact, I can't really think of a situation that would require otherwise...
     
  8. Offline

    8jy89hui

    @nverdier Yes. This is exactly what I mean. That is what I ment by making it an object. I just like having static objects because they are easier to transfer between classes. ( I understand that there can be problems but if done correctly there will not be)
     
  9. Invisible

    nverdier

    @8jy89hui when done correctly, you will have constructors, and they really aren't that difficult... Statics pose so many problems, and it goes against the purpose of OOP.
     
  10. Offline

    8jy89hui

    @nverdier okkkkkkkk..... I understand.... I still dont agree..... but I have no farther points that I can present to you that will convince you that statics are good and I dont believe you have any more points against statics. I agree to differ and will still use statics although I understand your dislike of statics.
     
  11. Invisible

    nverdier

    @8jy89hui This is getting really off-topic, so make a post in that section so we can continue this there.
     
  12. Offline

    XgXXSnipz

  13. Invisible

    nverdier

    @XgXXSnipz Well, what does this tell you?
     
  14. Offline

    XgXXSnipz

  15. Invisible

    nverdier

    @XgXXSnipz Yes, CraftServer ≠ org.bukkit.plugin.Plugin, of course. What could you do about fixing that... Hmmm.
     
  16. Offline

    XgXXSnipz

    @nverdier hmm.. go to spigot :p, JK I tried using Bukkit.getServer() and I tried using "this"
     
  17. Invisible

    nverdier

    @XgXXSnipz :p Yup those obviously aren't the same type... :D
     
  18. Offline

    XgXXSnipz

  19. Invisible

    nverdier

Thread Status:
Not open for further replies.

Share This Page