Matching Up Permissions

Discussion in 'Plugin Development' started by MGNick, Jan 7, 2012.

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

    MGNick

    Okay so can someone help me out with this. One. I'm not sure what Listener to use...but I'm pretty sure this would go into the Player Listener.

    Goal:
    If player has the permission node...group.fight they can't attack a player who also has the same permission node, but if the other player has the permission node: group.fight2, group.fight can pvp group.fight2

    How would the code look like?
     
  2. In EntityDamageEvent, after confirming that the damage is actually pvp (lots of guides out there to help you with, search for "EntityDamageByEntityEvent"), just do regular if clauses with hasPermission on both players.

    Code:
    if( (attacker.hasPermission("group.fight") && defender.hasPermission("group.fight")) || attacker.hasPermission("group.fight2") && defender.hasPermission("group.fight2")) ){
      // cancel event
    }
    Or something like that ... depends on how you actually want to handle the different cases.
     
  3. Offline

    MGNick

    Okay well I got this to work but one issue! It's ignoring the permission node. Using PermissionsEx latest version but not sure if thats the case and how would I make it work with PermissionsEx if it is the case? Here's my code:

    Code:
    public void onEntityDamage(EntityDamageEvent event) {
    
            Player player = (Player)event.getEntity();
    
            if (event instanceof EntityDamageEvent) {
    
                Entity attacker = ((EntityDamageByEntityEvent) event).getDamager();
    
                if (attacker instanceof Player) {
    
                    if( ( player.hasPermission("group.fight") && ((Player) attacker).hasPermission("group.fight"))
    
                            || player.hasPermission("group.fight2") && player.hasPermission("group.fight2")) {
                          // cancel event
    
                                event.setCancelled(true);
                                ((Player) attacker).sendMessage("You can't attack your own race");
     
                        }
    
                    else {
     
                    }
    
                }
    
            }
    }
    I got it to work!! but now in Console I'm getting these types of errors:

    Code:
    08.01 02:31:26 [Server] SEVERE Could not pass event ENTITY_DAMAGE to Test
    08.01 02:31:26 [Server] INFO     at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    08.01 02:31:26 [Server] INFO     at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:425)
    08.01 02:31:26 [Server] INFO     at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:527)
    08.01 02:31:26 [Server] INFO     at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    08.01 02:31:26 [Server] INFO     at org.getspout.spout.SpoutNetServerHandler.a(SpoutNetServerHandler.java:550)
    08.01 02:31:26 [Server] INFO     at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:93)
    08.01 02:31:26 [Server] INFO     at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    08.01 02:31:26 [Server] INFO     at net.minecraft.server.Packet7UseEntity.a(SourceFile:33)
    08.01 02:31:26 [Server] INFO     at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:916)
    08.01 02:31:26 [Server] INFO     at net.minecraft.server.EntityHuman.f(EntityHuman.java:783)
    08.01 02:31:26 [Server] INFO     at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:339)
    08.01 02:31:26 [Server] INFO     at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
    08.01 02:31:26 [Server] INFO     at org.bukkit.plugin.java.JavaPluginLoader$64.execute(JavaPluginLoader.java:711)
    08.01 02:31:26 [Server] INFO     at me.Nick.Test.TestPvP.onEntityDamage(TestPvP.java:13)
    08.01 02:31:26 [Server] INFO java.lang.ClassCastException: org.bukkit.craftbukkit.entity.CraftZombie cannot be cast to org.bukkit.entity.Player
    How do I stop these?!?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  4. Look at these 2 lines of yours:
    Code:
    public void onEntityDamage(EntityDamageEvent event) {
    
            Player player = (Player)event.getEntity();
    ...
    Now combine that with that line of your error:
    Code:
    org.bukkit.craftbukkit.entity.CraftZombie cannot be cast to org.bukkit.entity.Player
    You might get the logical issue here ... ;)

    Pro-Tip (open)
    Not every entity that dies is a player!
    Pro-Tip 2 (open)
    instanceof


    And you are comparing your permission "group.fight2" 2 times for "player" in your if-statement (or is that it what you fixed?).
     
  5. Offline

    MGNick

    Sorry nub here not sure what the fix is. Yes I get adding the other groups, but not getting how to remove the errors in console. What I'm I missing here?
     
  6. Before casting something to another thing make SURE it's a instance of that thing first, so use:
    Code:
    if(entity instanceof Player)
    {
        Player player = (Player)entity;
    
        // ...
    }
     
Thread Status:
Not open for further replies.

Share This Page