Assorted Listener Bugs/Questions

Discussion in 'Plugin Development' started by MrJoe223, Jul 19, 2013.

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

    MrJoe223

    Hello Bukkit, long time no query.
    Alright, my bugs:

    On the PlayerRespawnListener class, when a player respawns, they are supposed to be given certain items, and teleported to a certain location. However, when they respawn, they are spawned at the /setspawn for the map. Source: http://pastebin.com/zLeF1x90

    The PlayerDeathListener: When a player dies, they add a point to a team's point list; when a team reaches 50(or a number) points, the opposite team's NPC spawns. However(I've tested this with my co-Dev), neither team's NPC has spawned. Source: http://pastebin.com/6mF4M3S5

    Also: What's the best way to keep players from dropping items upon death? I've tried getting the drops from the event, and setting it to null, but that didn't work.

    plugin.yml: http://pastebin.com/y2knUvcc

    If somebody could shed some light on these, that'd be great. :)

    Bump before it vanishes into abyss of the non-front pages.

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

    tills13

    Where is PlayerJoinListener?

    Also, for PlayerDeathListener, do you receive all the expected chat outputs?

    And players inventory... you could clear their inventory using PlayerDeathListener.

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

    KedalionPlugins

    We do not receive all of the output, like we said. When the points hit 50, nothing happens though we've programmed it to.

    We really need help with this guys D:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  4. Offline

    Tirelessly

    For the respawn: You need to use event.setRespawnLocation, not player.teleport

    drops: event.getDrops().clear()

    MrJoe223
     
  5. Offline

    KedalionPlugins

    Whats drops? Also thanks so much.

    Do you know why the NPC isn't spawning when points hit a certain number?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  6. Offline

    tills13

    Code:
    if(!(EntityType.VILLAGER.isAlive()))
                            {
                                    if(RED_POINTS == MAX && RED_POINTS > BLUE_POINTS)
                                    {
                                    Bukkit.getServer().broadcastMessage(prefix + Color.RED + "Red Team Wins!");
                                    try {
                                            Thread.sleep(4000);
                                    } catch (InterruptedException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                    }
                                    copyFile.switchMap();
                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "reload");
                            }
    fyi this block definitely will not work... EntityType.VILLAGER is just an generic villager... you want to capture the Entity spawned when you use spawnEntity and then use that to check whether or not it's still alive.
    Code:
    Entity blueKing = world.spawnEntity()...
     
  7. Offline

    KedalionPlugins

    Oh okay. Could world still be Bukkit.getWorld("world") ??
     
  8. Offline

    tills13

    e.getPlayer().getWorld() is a lot easier.
     
    KedalionPlugins likes this.
  9. Offline

    KedalionPlugins

    Ok cool. Do you see any other problems??

    So the updated DeathListener:
    http://pastebin.com/679EKW9f

    And the updated Respawn:
    http://pastebin.com/CTHY840G

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  10. Offline

    tills13

    ok so basically I couldn't test your implementation due to your PlayerJoinListener and whatnot; however, using your setup I wrote a basic implementation of what you're trying to do.

    From my testing, you should check the location you're spawning the villager at - that was the one issue I ran into (even setting it to e.getEntity().getWorld().getSpawnLocation() was a little wonky)

    Here's the setup:

    Code:
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.EventHandler;
    import org.bukkit.Location;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Entity;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
     
    import java.util.Random;
     
    public class TestProject extends JavaPlugin implements Listener {
        public static int BLUE_POINTS = 0;
        public static int RED_POINTS = 0;
        public static Entity blueKing;
        public static Entity redKing;
        public static int MAX = 2;
        public Random rand = new Random();
     
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            this.blueKing = null;
            this.redKing = null;
        }
     
        public void onDisable() {
     
        }
     
        @EventHandler
        public void onPlayerDeath(EntityDeathEvent e) {
            if (e.getEntity().equals(blueKing) || e.getEntity().equals(redKing)) getServer().broadcastMessage("The king is dead.");
        }
     
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
            if (rand.nextDouble() > 0.50) {
                BLUE_POINTS++;
            } else {
                RED_POINTS++;
            }   
     
            getServer().broadcastMessage("Red team has " + RED_POINTS + " points.");
            getServer().broadcastMessage("Red team has " + BLUE_POINTS + " points.");
     
            if(RED_POINTS == MAX) {
                blueKing = e.getEntity().getWorld().spawnEntity(e.getEntity().getWorld().getSpawnLocation(), EntityType.VILLAGER);
                getServer().broadcastMessage(ChatColor.BLUE + "The King Has Spawned For Blue Team! Kill It!");
                if(blueKing.isDead()) {
                    if(RED_POINTS == MAX && RED_POINTS > BLUE_POINTS) {
                        getServer().broadcastMessage(ChatColor.RED + "Red Team Wins!");
                    }
                }
            } else if(BLUE_POINTS == MAX) {
                redKing = e.getEntity().getWorld().spawnEntity(e.getEntity().getWorld().getSpawnLocation(), EntityType.VILLAGER);
                getServer().broadcastMessage(ChatColor.BLUE + "The King Has Spawned For Red Team! Kill It!");
                if(redKing.isDead()) {
                    if(RED_POINTS == MAX && RED_POINTS > BLUE_POINTS) {
                        getServer().broadcastMessage(ChatColor.RED + "Blue Team Wins!");
                    }
                }
            }
        }
    }
    Changes made:

    • handle king death in another function - use EntityDeathEvent and check to see if it's the red or blue king.
    • use e.getEntity().getWorld() instead of Bukkit.getWorld("world")
    • use blueKing/redKing instead of EntityType.VILLAGER
    And I just noticed that I messed up. Change the first onPlayerDeath (I just forgot to rename this) to:
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(EntityDeathEvent e) {
    3. if (e.getEntity().equals(blueKing)) {
    4. //handle red team win here
    5. } else if (e.getEntity().equals(redKing)) {
    6. //handle blue team win here
    7. }
    8. }


    You can remove the corresponding sections in onPlayerDeath() -
    Code:java
    1. if(redKing.isDead()) {
    2. if(RED_POINTS == MAX && RED_POINTS > BLUE_POINTS) {
    3. getServer().broadcastMessage(ChatColor.RED + "Blue Team Wins!");
    4. }
    5. }


    Also worth noting that I was wrong above - it's e.getEntity() and not e.getPlayer()

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

    KedalionPlugins

    Great. I assume the Points stuff wasn't actually calculating as you used Java random? Thanks a lot.

    Also are you using the same method twice? Isn't that a method override?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  12. Offline

    tills13

    You're talking about two onPlayerDeath? No, two reasons: a) I messed up, I was rushing when coding and called it that and b) it's called Function Overloading. In Java, you can have functions with the same name so long as they take different arguments.

    It's important that you handle the villager death in a onEntityDeath (using EntityDeathEvent) function instead of in the onPlayerDeath function.


    Yeah, I don't have a friend to test with, nor do I know how you handled team assignments, so I just randomly allocated points when I killed myself

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  13. Offline

    KedalionPlugins

    Functions are called methods in java
     
  14. Offline

    tills13

    ok..? You obviously know what I'm talking about so why bother correcting?
     
  15. Offline

    KedalionPlugins

    Sorry, didn't mean to be rude. Thanks for your help.
     
  16. Offline

    tills13

    It's fine, did what I post work?
     
  17. Offline

    MrJoe223

    tills13
    PlayerDeath is more or less working now, yes.

    I'm still tooling around with PlayerRespawn, trying to get it to work. I've checked and double-checked everything I know of, and for the life of me, I can't get it to recognize that a player has respawned. o.o
    PlayerRespawn: http://pastebin.com/Mfgu96kF
    If you could take another look at it, that'd be great.
    Once again, thanks so much for your help. You've been great. :)
     
  18. Offline

    tills13

    What isn't working? Are they not getting their equipment?

    Two things: why do you have identical blocks in onPlayerRespawn and in red/blueInventory? and what's with the hanging else statement at the end of the file?

    I'd recommend putting a print statement in there two check to see if that's catching everything (as in, PlayerJoinListener isn't assigning teams correctly). You've provide a ton of code - thank you for that - but could I see PlayerJoinListener? You can PM me it if you don't want to publicly post it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  19. Offline

    MrJoe223

    I have no problem posting stuff. :p

    The problem with PlayerRespawn is noticeably everything. E.g. nothing happens that would out of the ordinary with no playerrespawn class. No inventories, teleportation, etc

    Alright: As far as we can tell, playerjoin is working ok, at least in the teleportation/inventories aspect. We haven't been able to get the points aspect working. (We've set it MAX to 1, and killed each other, yet no villager spawns).

    As for the identical blocks, I was derping around and trying to get it to work; I just put that in there in case the inventory methods weren't working.

    Thanks a lot!
     
  20. Offline

    tills13

    ok so you've checked to make sure that teams are allocated correctly?
    If I was to put my money on anything, it would be this line: if (PlayerJoinListener.blue.contains(name)) and the corresponding else if statement. Also, check to make sure that, if it DOES get past these statements, that it's actually finding the player correctly. Try putting a println statement somewhere in there.
     
  21. Offline

    MrJoe223

    I put a couple System.out.printlns in, and curiously, the println only fires for blue team.


    Code:java
    1. if (blue.size() == red.size())
    2. {
    3. int rand = this.random.nextInt(1);
    4. if (rand == 0)
    5. {
    6. blue.add(event.getPlayer().getDisplayName());
    7. PlayerRespawnListener.blueinventory(event.getPlayer().getDisplayName());
    8. event.getPlayer().teleport(new Location(Bukkit.getWorld("world"), 354.0D, 65.0D, -567.0D));
    9. System.out.println("Added to blue team.");
    10. Bukkit.getServer().broadcastMessage(this.prefix + event.getPlayer().getDisplayName() + ChatColor.BLUE + " has joined blue team!");
    11. }
    12. else if (rand == 1)
    13. {
    14.  
    15. red.add(event.getPlayer().getDisplayName());
    16. PlayerRespawnListener.redinventory(event.getPlayer().getDisplayName());
    17. System.out.println("Added to red team.");
    18. event.getPlayer().teleport(new Location(Bukkit.getWorld("world"), 349.0D, 65.0D, -632.0D));
    19. Bukkit.getServer().broadcastMessage(this.prefix + event.getPlayer().getDisplayName() + ChatColor.RED + " has joined red team!");
    20. }
    21.  
    22. }
     
  22. Offline

    tills13

    I think you want something along the lines of what I PM'd you. Your logic is a little weird...
    Code:java
    1. blue.size() == red.size()

    will only really be true once. If this is what is choosing teams, it will only be executed once.
    Also, if I recall correctly, nextInt(1) will basically only output zeros... From my testing (outputting 100 rand.nextInt(1)s), this is true. Use nextDouble() and use greater than/less than 0.50

    Statistically, using the method I listed above *should* give you even teams. However, only with a large enough sample size. The code I PM'd you is guaranteed to produce even (within one with odd numbers) teams.

    Also, a better way to handle Bukkit.getServer() ... is to pass you plugin in through the constructor, and use plugin.getServer() - it'll save you an import.

    Here, use this: http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt(int)

    Since the upper bound is exclusive, rand.nextInt(1) will only return zeros.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  23. Offline

    MrJoe223

    Alright, I changed it to the nextDouble() thing.

    The println that indicates it got to the redteam add function fired, so the nextDouble() thing works(thanks). However, on respawn, nothing happens, as before. It gets to the onPlayerRespawn method in PlayerRespawnListener, as indicated by the println, but nothing fires in the if statements checking whether the player is in the list<>. I changed the arraylist and corresponding methods to Player instead of the Player's name as a String, but still no success.
     
  24. Offline

    tills13

    Player name strings are actually a better way to do it - it allows players to leave, and maintain their team.

    I'm at work right now, so I'll help more later, but, in the mean time, why don't you change blue/redInventory to Player, and pass it e.getPlayer() - getting rid of the bukkit call
     
  25. Offline

    MrJoe223

    Right. I did that, changed the things in PlayerRespawn to Player. However, I don't think that's the problem, as PlayerJoin calls the method just fine.

    In all my limited knowledge, I think there's something up with the ArrayLists. As far as I can tell, everything that doesn't depend on them works, and everything that does fails to function. I changed it so that I declare them in the main class, and init them in the onEnable(). A previous plugin I wrote(which works, by the way (lol)) used ArrayLists, used the same init, and used the same methods. I'm confuzzled. :S

    Also, anybody else reading this is welcome to help, please.

    Thanks all.
     
  26. Offline

    pointfit

    MrJoe223
    Have you tried turning it off and turning it back on?
     
    MrJoe223 likes this.
  27. Offline

    tills13

    Moving it into onEnable() shouldn't make a difference (although you shouldn't have an onEnable() in your listener).

    Oh. I just had a thought. Your onEnable() looks something like this,

    Code:
    public void onEnable() {
                    ...
            getServer().getPluginManager().registerEvents(this, new PlayerJoinListener(this));
            getServer().getPluginManager().registerEvents(this, new PlayerLeaveListener(this));
        }
    No?

    And your team lists, they aren't Static, are they? So basically, when you do PlayerJoinListener.blue, it's an empty list.
     
  28. Offline

    MrJoe223

    Yes, my onEnable looks something like that.

    I tried changing them from static, but Eclipse threw a bunch of errors about not being able to reference static variables to a non-static method, or something like that. Are you saying that if the arraylists are static, that they're effectively null?
     
  29. Offline

    tills13

    No I'm saying that if you're trying to access them from another instance of an object, they may not be what you expect them to be. Another way around that would be to do something like

    Code:
    public class Teams extends JavaPlugin implements Listener {
        public PlayerJoinListener pjlistener = new PlayerJoinListener(this);
        public PlayerLeaveListener pllistener = new PlayerLeaveListener(this);
     
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, pjlistener));
            getServer().getPluginManager().registerEvents(this, pllistener);
        }
     
        public void onDisable() {
     
        }
    }
    and then reference them via plugin.pllistener and plugin.pjlistener
     
  30. Offline

    KedalionPlugins

    Bumpedy Bump Bump, we need help.
     
Thread Status:
Not open for further replies.

Share This Page