Sending a player a message when he dies from a set message from killer

Discussion in 'Plugin Development' started by MajorSkillage, Nov 9, 2014.

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

    MajorSkillage

    I want to make it so it will reward the killer and it will also set a message so when someone kills someone it will send the person who died that message, here is my code what do I need to improve (also "test" is undefined outside the for statement, yes I know i just wanted to show you my idea)
    Code:
    package me.rockinroll99.money4kills;
     
    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.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener{
        String killed_player;
        public static String format(String format){
            return ChatColor.translateAlternateColorCodes('&', format);
            }
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
        }
        @EventHandler
        public void onKill(PlayerDeathEvent e){
            Player kill = e.getEntity().getKiller();
            String killer = kill.getName();
            Player pkilled = e.getEntity();
            String killed = pkilled.getName();
            Player pkiller = e.getEntity().getKiller();
            if(killer.equals(killed)){
               
            }
                int random = (int) (Math.random() * 100);
                if(random < 10){
                        getServer().dispatchCommand(getServer().getConsoleSender(), getConfig().getString("command on kill"));
                    getServer().dispatchCommand(getServer().getConsoleSender(), "eco give " + killer + " " + 10);
                } else {
            pkilled.sendMessage(format(getConfig().getString("&6Bad luck!")));
            pkiller.sendMessage(format(getConfig().getString("&6GG!")));
            getServer().dispatchCommand(getServer().getConsoleSender(), "eco give " + killer + " " + 1);
            Bukkit.broadcastMessage(ChatColor.GOLD + killer + ChatColor.GREEN + "Just destroyed " + ChatColor.BLUE + killed);
            }
        }
        public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equals("killmessage") || cmd.getName().equals("km")){
                for(int i = 0; i < args.length; i++){
                String test = args[i];
                }
                getServer().getPlayer(killed_player).sendMessage(test);
            }
            return false;
        }
    }
    
     
  2. Offline

    Concurrent

    MajorSkillage
    1. you have redundant code, no need for that if statement if the killer equals killed because you do absolutely nothing in it
    2. use vault api not in-game command
    3. learn how to use configuration correctly
    4. stop putting listeners in your main class, don't name your main class 'Main' thats bad practice
     
  3. Offline

    MajorSkillage

    i was gonna use a config but changed my mind :p and the reason why i said if(killer == killed){ } else {} is so that people wont get rewarded after killing themselves, and we're using Essentials for economy so it would only really save time and space if i just used the essentials command onDeath and making a whole different class just for this is also a waste of time when the main class is not even that big
     
  4. Offline

    Dudemister1999

    Concurrent
    1. True.
    2. Minimal difference. Just adds an extra dependency you will only need once.
    3. Also true.
    4. It's up to him how he sets up his plugin code structure. Makes no difference at the end.
    MajorSkillage
    What exactly does this plugin do? From what I see, it just gathers the name of the last killed person, and allows you to send a message to him whenever you want. As Concurrent up there said, you could use some improvement with configurations. If you need help, feel free to ask.
     
  5. Offline

    MajorSkillage

    Dudemister1999
    It sends the player a message which i will probably add in who killed the player and when the event happens console dispatches /eco give {killer} 1
    which is all working perfectly it is just i am not sure how i am going to make it so that when a user types in /killmessage I win! and then it would send a message to the person who died "I win!"

    I also got a error in console onDeathEvent something about it not being a player that is why instead i just added the prefix player then got the string of it just in case, i havn't checked since
     
  6. Offline

    Concurrent

    MajorSkillage ok but your relying on essentials actually being enabled
    you have no else attached to your if statement therefore you just check then do nothing
    bad practice. you should learn to use multiple classes, but only personal preference i guess
    also what actually seems to be the issue? i cant see anything wrong at current point except the config, does it give money and broadcast?

    a config is setup with different 'ConfigurationSections' heres a good page for help on that. conventionally a config has different sections seperated by line breaks and spaces but when attempting access you must add a '.' for every section of access. for example say i had 'playerdeath.message.killed' it would get String 'ha! you died!' using method getConfig().getString("playerdeath.message.killed");
    Code:
    playerdeath:
      message:
        killed: 'ha! you died!'
    see formatting? this is how configuration works.
     
  7. Offline

    Freelix2000

    MajorSkillage
    What he was saying about the 'if' statement being useless is that it will not prevent players from getting rewards for suicide because it does nothing about it. If you want to make that prevent players from killing themselves and getting rewarded, add a return statement inside of the 'if' statement.
     
  8. Offline

    Dudemister1999

    MajorSkillage Okay, so I got a little carried away and rewrote your plugin. Here it is, let me know if you have problems:

    [Click]
     
  9. Offline

    MajorSkillage

    forgot about that ._. thanks!

    Ok thanks! I'l check it out! :D
     
Thread Status:
Not open for further replies.

Share This Page