Need help!! Can't get this to work!!

Discussion in 'Plugin Development' started by kingBS11, Dec 2, 2012.

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

    kingBS11

    I have tried many different ways and I cannot get this to work. I have tried everything!
    Here is my code:
    package com.endlessshadow;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;

    public class DeathListener implements Listener {
    public EndlessHunger plugin;

    public DeathListener(EndlessHunger plugin) {
    this.plugin = plugin;
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent evt){
    Player p = evt.getEntity();
    String pname = p.getDisplayName();
    plugin.Playing.remove(pname);
    plugin.Dead.add(pname);
    evt.setDeathMessage(ChatColor.RED + pname + ChatColor.AQUA + " Has died and was kicked from the games!");
    p.kickPlayer("You have died and are out of the games!");
    if(plugin.Playing.size()== 1){
    plugin.getServer().broadcastMessage(ChatColor.AQUA + "The Games Have Ended!");
    plugin.Playing.clear();
    plugin.Dead.clear();
    }
    }
    }

    Please tell me what I am doing wrong! I can't get it right. The first part of it works(In Red) The second part doesn't(In Blue) PLEASE HELP!

    Bump.

    Also, there is no error in the console when a player dies.

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

    Tehmaker

    Have you registered the event(s) in the main class with the plugin manager?

    Also, please change your title to something more informative on the topic, if you are in this section making a thread we already assume that you are looking for help.
     
  3. Offline

    gomeow

    Can you do this with your code:
    [syntax=java]PUT YOUR CODE HERE[/syntax]
    Also, can we see your main class?
     
  4. Offline

    joby890

    What is Playing ?
     
  5. Offline

    gomeow

    I would assume a list
     
  6. Offline

    xGhOsTkiLLeRx

    We need to see the other class (EndlessHunger).
    There is no syntax error, as seen here (formatted code from above)
    Is there any error message from eclipse (or other IDE...)

    Code:java
    1. package com.endlessshadow;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.PlayerDeathEvent;
    8.  
    9. public class DeathListener implements Listener {
    10. public EndlessHunger plugin;
    11.  
    12. public DeathListener(EndlessHunger plugin) {
    13. this.plugin = plugin;
    14. }
    15.  
    16. @EventHandler
    17. public void onPlayerDeath(PlayerDeathEvent evt) {
    18. Player p = evt.getEntity();
    19. String pname = p.getDisplayName();
    20. plugin.Playing.remove(pname);
    21. plugin.Dead.add(pname);
    22. evt.setDeathMessage(ChatColor.RED + pname + ChatColor.AQUA + " Has died and was kicked from the games!");
    23. p.kickPlayer("You have died and are out of the games!");
    24. if(plugin.Playing.size()== 1){
    25. plugin.getServer().broadcastMessage(ChatColor.AQUA + "The Games Have Ended!");
    26. plugin.Playing.clear();
    27. plugin.Dead.clear();
    28. }
    29. }
    30. }
     
  7. Offline

    teunie75

    Have you tried instead of you own code this?:
    Code:
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent evt){
    Player p = evt.getPlayer();
    Look at the last word.
     
  8. Offline

    kingBS11

    Yeah I have them in my main class

    Here is the DeathListener
    Code:java
    1. package com.endlessshadow;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.PlayerDeathEvent;
    8.  
    9. public class DeathListener implements Listener {
    10. public EndlessHunger plugin;
    11.  
    12. public DeathListener(EndlessHunger plugin) {
    13. this.plugin = plugin;
    14. }
    15.  
    16. @EventHandler
    17. public void onPlayerDeath(PlayerDeathEvent evt){
    18. Player p = evt.getEntity();
    19. String pname = p.getDisplayName();
    20. plugin.Playing.remove(pname);
    21. plugin.Dead.add(pname);
    22. evt.setDeathMessage(ChatColor.RED + pname + ChatColor.AQUA + " Has died and was kicked from the games!");
    23. p.kickPlayer("You have died and are out of the games!");
    24. if(plugin.Playing.size()== 1){
    25. plugin.getServer().broadcastMessage(ChatColor.AQUA + "The Games Have Ended!");
    26. plugin.Playing.clear();
    27. plugin.Dead.clear();
    28. }
    29. }
    30. }

    Here is the main class
    Code:java
    1. package com.endlessshadow;
    2.  
    3. import java.io.File;
    4. import java.util.ArrayList;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class EndlessHunger extends JavaPlugin{
    13. Logger log;
    14. public Listener DeathListener = new DeathListener(this);
    15. public Listener MoveListener = new DeathListener(this);
    16. public ArrayList<String> Playing = new ArrayList<String>();
    17. public ArrayList<String> Dead = new ArrayList<String>();
    18. public FileConfiguration config;
    19. public CommandExecutor CommandKit = new CommandKit(this);
    20. public CommandExecutor CommandClear = new CommandClear(this);
    21.  
    22. public void onEnable(){
    23. getServer().getPluginManager().registerEvents(DeathListener, this);
    24. getServer().getPluginManager().registerEvents(MoveListener, this);
    25. getCommand("Kit").setExecutor(this.CommandKit);
    26. getCommand("Clearhg").setExecutor(this.CommandClear);
    27. getLogger().info("EndlessHunger has been enabled!");
    28. config = getConfig();
    29. config.options().copyDefaults(true);
    30. this.saveDefaultConfig();
    31. boolean file = (new File("plugins/EndlessHunger/")).mkdir();
    32. if(file){
    33. getLogger().info("Successfully created plugin directory!");
    34. }
    35. }
    36. public void onDisable(){
    37. getLogger().info("EndlessHunger has been disabled!");
    38. }
    39. }


    Nope, no error messages

    The method getPlayer() is undefined for the type PlayerDeathEvent

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

    xGhOsTkiLLeRx

    I know this question might be stupid, but you add them to the list right?
    Maybe you forogot a .getName() somewhere.

    Other thought: replace "String pname = p.getDisplayName();"
    With: String pname = p.getName();
    Since the displayName might change!
     
  10. Offline

    kingBS11

    Yeah I added them, i'll try to replace the getDisplayName with getName

    Didn't work. Bump.

    Also, just saying, everything else works except for the part where it broadcasts that the games have ended. That is the only part that does not work. Everything else works, the player get kicked, it's the right kick message, blah blah blah

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

    Tehmaker

    It's 2am so Ima say something stupid, and hope it isn't... Use bukkit to broadcast instead of the plugin, everything seems right to me, so I got no clue
     
  12. Offline

    boardinggamer

    You are removing the player before checking if there is one left. do this
    Code:java
    1. if(plugin.Playing.isEmpty()){
    2. plugin.getServer().broadcastMessage(ChatColor.AQUA + "The Games Have Ended!");
    3. plugin.Playing.clear();
    4. plugin.Dead.clear();
    5. }

    see if that works
     
  13. Offline

    kingBS11

    Wouldn't there have to be a player left to win though? Then it would be if playing size==1 but if it is empty that means all the players have died, even the player that won.
     
  14. Offline

    boardinggamer

    Right didn't think of that. ok so you should do some debugging and tests. Add messages through out the code for what happens, also add a message saying everyone who is in the "Playing" section to see if they are actually being removed. also you can change the list to Player instead of String and then just add the player and remove the player from it.

    EDIT: I also just noticed this
    getServer().getPluginManager().registerEvents(DeathListener, this);
    Try changing it to getServer().getPluginManager().registerEvents(new DeathListener(this));
     
  15. Offline

    kingBS11

    Didn't work. I think the problem is in the if(plugin.Playing.size()== 1){ because it doesn't "register" that part and doesn't do what's inside of it
     
  16. Offline

    Rprrr

    Print out the size of Playing to see what it is when there's one player left.

    And: are you sure you add players to it? Because otherwise the size is always 0.

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

    kingBS11

    Yeah when they join the server they get added.... I think. I'll test it tomorrow.
     
Thread Status:
Not open for further replies.

Share This Page