How can i do that?

Discussion in 'Spigot Plugin Development' started by ScoRpoyN, Sep 24, 2016.

  1. I have done that:

    Code (Text):

          @SuppressWarnings("deprecation")
        @EventHandler
          public void onPlayerGotHit(EntityDamageByEntityEvent e){
              Player p = (Player) e.getEntity();
              Player attacker = (Player) e.getDamager();
           
              if(p instanceof Player && attacker instanceof Player){
                  p.sendMessage("1");
              if(admin.contains(attacker.getName())){
                  p.sendMessage("2");
                  if(attacker.getItemInHand().getType() == Material.EMERALD && p.getItemInHand().getType() != Material.AIR && p.getItemInHand().getType() != null) {
                      p.sendMessage("3");
                      if(e.getEntity().getLastDamageCause() == attacker){
                          p.sendMessage("4");
                      p.kickPlayer("§4§lYou have been banned by §6§l" + attacker.getName());
                      p.setBanned(true);
                      e.setCancelled(true);
                  }
                  }else{
                      e.setCancelled(true);
                  }
              }
         }else{
             e.setCancelled(true);
         }
    }
     
    The player that need to get ban got "1" + "2" messages.
    Means that the problem is with the item check.
     
  2. No.
    You're checking if the item in the players hand is an emerald. If so, you check if the item is NOT air, which it isn't (if it got this far, its an emerald). Finally, you check if the type is null. If the p.getItemInHand() is null, THAT will throw a NPE. Check that first.
    Code (Text):

    if(p.getItemInHand() != null && p.getItemInHand().getType() == Material.EMERALD) {
    //
    }
     
    • Optimistic Optimistic x 1

  3. I tried this:
    Code (Text):


          @SuppressWarnings("deprecation")
        @EventHandler
          public void onPlayerGotHit(EntityDamageByEntityEvent e){
              Player p = (Player) e.getEntity();
              Player attacker = (Player) e.getDamager();
             
              if(p instanceof Player && attacker instanceof Player){
                  p.sendMessage("1");
              if(admin.contains(attacker.getName())){
                  p.sendMessage("2");
                  if(attacker.getItemInHand().getType() == Material.EMERALD) {
                      p.sendMessage("3");
                      if(p.getLastDamageCause() == attacker){
                          p.sendMessage("4");
                      p.kickPlayer("§4§lYou have been banned by §6§l" + attacker.getName());
                      p.setBanned(true);
                      e.setCancelled(true);
                  }
                  }else{
                      e.setCancelled(true);
                  }
              }
         }else{
             e.setCancelled(true);
         }
    }

     
    and i got to message number 3..
     
  4. Code (Text):

    Player p = (Player) e.getEntity();
    Player attacker = (Player) e.getDamager();
     
    These lines should be after you do the instanceof checks. You're setting the entities to a player before you know that the entities are players.
    After that, replace:
    Code (Text):
    if(attacker.getItemInHand().getType() == Material.EMERALD) {
    with
    Code (Text):
    if(attacker.getItemInHand() != null && attacker.getItemInHand().getType() == Material.EMERALD) {
    Code (Text):
    if(p.getLastDamageCause() == attacker){
    This should be removed. You're checking if a DamageCause (such as EntityDamageByEntityEvent.DamageCause.ENTITY) is equal to your player. This will never be true. Plus, we already know that the player's last damage is the attacker, because we're in the EntityDamageByEntityEvent.
     
  5. Thanks god, you are awesome!

    @Sploon

    Can you help me with 1 more thing?
    (BTW sorry for my english, not that good :/)

    I tried to do Unban command.
    It works, but when i write /Unban blabla or only /Unban i got this error:

    "An internal error occured while attempting to perform this command"

    than

    and i got this error:

    Code (Text):

    org.bukkit.command.CommandException: Unhandled exception executing command 'unban' in plugin Tournoment v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-b73910a-726656b]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-b73910a-726656b]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-b73910a-726656b]
            at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot.jar:git-Spigot-b73910a-726656b]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot.jar:git-Spigot-b73910a-726656b]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-b73910a-726656b]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-b73910a-726656b]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-b73910a-726656b]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot.jar:git-Spigot-b73910a-726656b]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-b73910a-726656b]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot.jar:git-Spigot-b73910a-726656b]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot.jar:git-Spigot-b73910a-726656b]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.ScoRpyoN.tournoment.Main.onCommand(Main.java:781) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-b73910a-726656b]
            ... 14 more
    >
     
    There is the code:

    Code (Text):


                            OfflinePlayer target = p.getServer().getOfflinePlayer(args[0]);
                           
                            if (!sender.hasPermission("t.ban")){
                                    p.sendMessage("§4You can't do that!");
                                    return true;
                                }
                           
                            if(cmd.getName().equalsIgnoreCase("unban")){
                            if (args.length == 0) {
                                p.sendMessage("§7> " + "§aUnBan§7: /Ban <Player>!");
                                  return false;  
                            }
                                target.setBanned(false);
                                p.sendMessage("§7> §a" + target.getName() + " §7is now unbanned!");
                                return true;
                                }

                 
                         
             

     
     
    #25 ScoRpoyN, Sep 24, 2016
    Last edited: Sep 24, 2016
    • Friendly Friendly x 1
  6. UP
     
    • Optimistic Optimistic x 1
  7. You aren't checking the args.length.
    You're getting an OfflinePlayer object with args[0], but if args[0] isn't there, you get the error. Check if the args.length is == 1 or > 0
     
    • Agree Agree x 1
  8. Dude, your questions and your code show that you do not know a substantial part of Java. Try learning Java first before starting with Bukkit. Otherwise you will ask for "help"/spoonfeeding repeatedly until you have copy-pasted your plugin together from various peoples code snippets given to you. You only get them because you do not know how to fix anything/debug/apply pesudo code and instructions like "check for null".
    Basically, learn Java and stop stealing time from people needing real help.
     
    • Informative Informative x 1
  9. I didn't asked for adviced, so don't reply here.
    I am learning Java at school but i also want to learn bukkit now with Java.
    So if you don't know it DONT REPLY
     
    • Optimistic Optimistic x 1
  10. The thing is that the rules say that you have to know your java!
    The problem is obvious, simple to solve and only made by people which do not know anything about java/programming in general. Also, how to solve it is described above.
     
  11. Dude i told you, I'm a starter, i tried to understand the porblem but i couldn't. i also tried to debug and still nothing helped me.
    This forum is here to help people with plugins...
     
  12. ....not with basic java. Please understand that you are too bad to write plugins atm as you do not know basic Java. You trying to write plugins will probably result in you posting a few post similar to this and then stop bukkit because it sucks when you have to ask back all 10 minutes.