Player Damage Event

Discussion in 'Plugin Development' started by SuperEvan200, Mar 29, 2013.

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

    SuperEvan200

    In my plugin, when a player executes:
    /k join kingdomname(Out of 4)
    He gets a specific prefix for his kingdom and a string is set inside the config.yml. When a player joins the game and is not located in any Kingdom, he/she gets the prefix "[Nomad]". However, the config file remains empty!
    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            String prefix = getConfig().getString("prefixes."+player.getDisplayName());
            if(prefix == null) {
                prefix = "[Nomad]";
            }
            player.setDisplayName(prefix + player.getDisplayName());
        }
    I want to disable PvP for Nomad players. I also want to disable PvP between Nomad players and other kingdoms. Here is the code for other Kingdoms, which correctly defines the prefix in the config.yml file:
    Code:
    else if(commandLabel.equalsIgnoreCase("k"))
            {
                if(args[0].equalsIgnoreCase("join"))
                {
                    if(args[1].equalsIgnoreCase("delolia"))
                    {
                        player.setDisplayName(player.getName());
                        player.setDisplayName(kingdom1 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom1);     
                            saveConfig();
                            reloadConfig();
                    }
    And here is how the config file looks after I execute the command /k join delolia:
    Code:
    prefixes:
      SuperEvan200: §1[Delolia] §r
    
    Now, heres the event handler I coded to prevent PvP for Nomads:
    Code:
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent e)
        {
        Player Damager = (Player) e.getDamager();
        if(e.getEntity() instanceof Player && Damager instanceof Player)
        {
     
            Player player = (Player) e.getEntity();
            String prefix = getConfig().getString("prefixes."+player.getDisplayName());
            if(prefix == "[Nomad]")
            {
            e.setCancelled(true);
            }
        }
        }
    Every aspect of the plugin works except, when I tested the plugin in game with 2 players with the prefix "[Nomad]", PvP was working and giving damage.

    Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. Offline

    caseif

    Well, the code which prevents PvP disables it for players who have the prefix "[Nomad]" associated with their username in the config. This can never be so, as the [Nomad] prefix is a result of not having a prefix defined. To fix this, either change the PvP code to check if the prefix is not set, or set it when the player joins for the first time. Additionally, I would recommend checking if a player has a prefix associated with getConfig().containsKey(key), rather than getting the value and checking if it's null. Hope this helps!

    //EDIT:
    Please only bump once every twelve hours.
     
  3. Offline

    SuperEvan200

    AngryNerd

    Sorry about bumping. Anyways, here is the code I have changed (When a player joins):
    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            String prefix = getConfig().getString("prefixes."+player.getDisplayName());
            if(prefix == null) {
                getConfig().getString("prefixes.", "Default Value");
                this.getConfig().set("prefixes."+player.getName(),"[Nomad]");
                player.setDisplayName(prefix + player.getDisplayName());
                saveConfig();
                reloadConfig();
            }
            player.setDisplayName(prefix + player.getDisplayName());
        }
        
    The config.yml writes it correctly but I can still PvP.

    I also don't get what you mean by:
    getConfig().containsKey(key)

    For (key) do I put [Nomad] inside the parentheses?
     
  4. Offline

    caseif

    First, that line of code does nothing. There's also no need for a second argument, as it simply tells the method to return it if the key is not found. Thirdly, you're not getting a proper key. You would need to add on the player's username for it to be valid. But, like I said, there's no need for it at all. I'm just telling you this for future reference.
    This will never, ever return true. When comparing strings, you need to use the .equals() or .equalsIgnoreCase() methods. Don't ask me why, that's just the way it works. Make these changes and the plugin should work properly.
     
  5. Offline

    SuperEvan200

    AngryNerd
    Doesn't work, but this time I get an error message in console and log:
    Code:
    2013-03-29 16:13:18 [SEVERE] Could not pass event EntityDamageByEntityEvent to Kingdoms v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at org.bukkit.craftbukkit.v1_5_R1.event.CraftEventFactory.callEvent(CraftEventFactory.java:81)
        at org.bukkit.craftbukkit.v1_5_R1.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:369)
        at org.bukkit.craftbukkit.v1_5_R1.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:391)
        at net.minecraft.server.v1_5_R1.EntityLiving.damageEntity(EntityLiving.java:693)
        at net.minecraft.server.v1_5_R1.EntityHuman.damageEntity(EntityHuman.java:683)
        at net.minecraft.server.v1_5_R1.EntityPlayer.damageEntity(EntityPlayer.java:349)
        at net.minecraft.server.v1_5_R1.EntityHuman.attack(EntityHuman.java:854)
        at net.minecraft.server.v1_5_R1.PlayerConnection.a(PlayerConnection.java:1103)
        at net.minecraft.server.v1_5_R1.Packet7UseEntity.handle(SourceFile:36)
        at net.minecraft.server.v1_5_R1.NetworkManager.b(NetworkManager.java:292)
        at net.minecraft.server.v1_5_R1.PlayerConnection.d(PlayerConnection.java:113)
        at net.minecraft.server.v1_5_R1.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.v1_5_R1.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_5_R1.MinecraftServer.r(MinecraftServer.java:580)
        at net.minecraft.server.v1_5_R1.DedicatedServer.r(DedicatedServer.java:225)
        at net.minecraft.server.v1_5_R1.MinecraftServer.q(MinecraftServer.java:476)
        at net.minecraft.server.v1_5_R1.MinecraftServer.run(MinecraftServer.java:409)
        at net.minecraft.server.v1_5_R1.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
        at me.superevan200.kingdoms.Kingdoms.onEntityDamageByEntity(Kingdoms.java:57)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 21 more
    I removed the useless line of code you told me about and replaced the if statement with this:
    Code:
            if(prefix.equalsIgnoreCase("[Nomad]"));
    My prefix is also null and not [Nomad] until I logout of the server and log back in.
     
  6. Offline

    caseif

    Sorry, I missed that. You need to update the prefix variable after setting it. Alternatively, you could just replace it with "[Nomad]" since it will always be equal to that.
     
  7. Offline

    SuperEvan200

    AngryNerd

    I updated my code to this:
    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            String prefix = getConfig().getString("prefixes."+player.getDisplayName());
            if(prefix == null) {
                this.getConfig().set("prefixes."+player.getName(),"[Nomad]"); 
                player.setDisplayName("[Nomad]" + player.getDisplayName());
                saveConfig();
                reloadConfig();
            }
        }
    The players with [Nomad] still take damage from PvP. How would I "Update the prefix variable"? Maybe that should work instead of replacing it with Nomad (What I did).
     
  8. Offline

    caseif

    Again, you need to use .equals() or .equalsIgnoreCase().
     
  9. Offline

    SuperEvan200

    AngryNerd

    I don't know what you mean by "Update" though. In my player damage event I use:
    Code:
            if(prefix.equalsIgnoreCase("[Nomad]"));
     
  10. Offline

    caseif

    I meant, redefine it using the same code.
     
  11. Offline

    SuperEvan200

    AngryNerd
    I still don't understand what you are trying to tell me. I changed the code to this. Now when I first join, I have no prefix. When I leave and join back in I get the prefix "prefixes.". Then one player can hit the other, but the other can't.

    Here is my code for a playerjoinevent:
    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            String prefix = getConfig().getString("prefixes."+player.getDisplayName());
            if(prefix == null)
            {
                this.getConfig().set("prefixes."+player.getDisplayName(),"[Nomad]");
                saveConfig();
                reloadConfig();
            }
            else if(prefix != null)
            {
                player.setDisplayName("prefixes." + player.getDisplayName());
            }
        }
    Here is my config.yml file:
    Code:
    prefixes:
      SuperEvan200: '[Nomad]'
      SuperEvan20: '[Nomad]'
    
     
  12. Offline

    SuperEvan200

  13. Offline

    Burnett1

    Can you post your class's please.
     
  14. Offline

    SuperEvan200

    Burnett1

    Code:
    package me.superevan200.kingdoms;
     
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Kingdoms extends JavaPlugin implements Listener{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Kingdoms plugin;
       
       
        @Override
        public void onDisable() {
            saveConfig();
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled!");
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
            Bukkit.getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            String prefix = getConfig().getString("prefixes."+player.getDisplayName());
            if(prefix == null)
            {
                this.getConfig().set("prefixes."+player.getDisplayName(),"[Nomad]");
                saveConfig();
                reloadConfig();
            }
            else if(prefix != null)
            {
                player.setDisplayName("prefixes." + player.getDisplayName());
            }
        }
       
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent e)
        {
        Player Damager = (Player) e.getDamager();
        if(e.getEntity() instanceof Player && Damager instanceof Player)
        {
       
            Player player = (Player) e.getEntity();
            String prefix = getConfig().getString("prefixes."+player.getDisplayName());
            if(prefix.equalsIgnoreCase("[Nomad]"));
            {
            e.setCancelled(true);
            }
        }
        }
       
        String kingdom1 = ChatColor.DARK_BLUE + "[Delolia] " + ChatColor.RESET;
        String kingdom2 = ChatColor.DARK_AQUA + "[Syviel] " + ChatColor.RESET;
        String kingdom3 = ChatColor.DARK_GREEN + "[Natura] " + ChatColor.RESET;
        String kingdom4 = ChatColor.DARK_RED + "[Valkyrie] " + ChatColor.RESET;
       
        public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("kingdom"))
            {
                if(args[0].equalsIgnoreCase("help"))
                    player.sendMessage(ChatColor.DARK_RED + "To join a kingdom, say /k join Delolia, Siviel, Natura, or Valkyerie");
            }
            else if(commandLabel.equalsIgnoreCase("k"))
            {
                if(args[0].equalsIgnoreCase("join"))
                {
                    if(args[1].equalsIgnoreCase("delolia"))
                    {
                        player.setDisplayName(player.getName());
                        player.setDisplayName(kingdom1 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom1);       
                            saveConfig();
                            reloadConfig();
                    }
                    if(args[1].equalsIgnoreCase("syviel"))
                    {
                        player.setDisplayName(player.getName());
                        player.setDisplayName(kingdom2 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom2);       
                            saveConfig();
                            reloadConfig();
                    }
                    if(args[1].equalsIgnoreCase("natura"))
                    {
                        player.setDisplayName(player.getName());
                        player.setDisplayName(kingdom3 + player.getDisplayName());   
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom3);       
                            saveConfig();
                            reloadConfig();
                    }
                    if(args[1].equalsIgnoreCase("valkyrie"))
                    {
                        player.setDisplayName(player.getName());
                        player.setDisplayName(kingdom4 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom4);       
                            saveConfig();
                            reloadConfig();
                    }
                }
            }
            return true;
           
        }
    }
    
     
  15. Offline

    SuperEvan200

  16. Offline

    SuperEvan200

  17. Offline

    SuperEvan200

    Can someone help?? Bump.

    Why is this being ignored? Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  18. Offline

    SuperEvan200

    My fifth bump in a row! Halfway down to 10. x____x
     
  19. Offline

    SuperEvan200

    Help!

    Bump.. bump... bump..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  20. Offline

    SuperEvan200

    Please. Someone! :'(
     
  21. Offline

    SuperEvan200

    Ok, last bump, then I'll just start a new topic :p.
     
    joeygallegos likes this.
  22. Offline

    joeygallegos

Thread Status:
Not open for further replies.

Share This Page