Solved Issues with Listeners

Discussion in 'Plugin Development' started by Minterrr, Jul 21, 2016.

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

    Minterrr

    Hello!
    I'm having issues with my plugin that I am coding. Any help would be appreciated. Nothing that I have asked to happen has happened.

    Code:
    package me.admin.myrescb;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    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.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
       
        List<Player> playerfirstJoin = new ArrayList<Player>();
       
        public void onEnable(){
           
            getLogger().info("MyresCB has loaded succesfully!");
           
            getServer().getPluginManager().registerEvents(this, this);
           
           
        }
        public void onDisable(){
           
        }
       
        @EventHandler
        public void onPlayerDamage(EntityDamageByEntityEvent event){
               
            Entity entity = event.getEntity();
           
                if(entity.getType().equals(EntityType.PLAYER)){
                    if(playerfirstJoin.contains(entity.getName())){
                    if(event.getDamager() instanceof Player){
                        event.setCancelled(true);
                    }
                           
                    }
                   
                }
                   
                    }
           
               
               
                   
               
       
        @EventHandler
            public void onPlayerJoin(PlayerJoinEvent event){
           
            Player p = event.getPlayer();
           
                if(!(p.hasPlayedBefore())){
                    playerfirstJoin.add(p);
                }
            }
               
       
           
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event){
           
            Player p = event.getPlayer();
           
            if(playerfirstJoin.contains(p)){
                playerfirstJoin.remove(p);
            }else{
               
            }
        }
       
    }
    
    
    
     
  2. Offline

    CaptainMike2828

    What's actually wrong?Error on startup?Maybe you could post a log?


     
  3. @Minterrr
    Exactly what doesn't happen?

    Also, don't log that your plugin enables, bukkit does this for you. Your onDisable method also does nothing and can be removed.
     
  4. Offline

    Minterrr

    If the player hasnt joined before, they either arent added to the ArrayList or the damage event isnt canceled.
    @CaptainMike2828

    No errors are logged in console
     
    Last edited: Jul 21, 2016
  5. @Minterrr
    What happens if you put a print statement in the event handler methods?
     
  6. Offline

    Zombie_Striker

    Don't log your own plugin. bukkit does this for you. Remove this lne.

    This method does nothing. Delete it.


    Fix your formatting. If you have to, use CTRL+Shift+F if you're using eclipse.

    1# Use == when comparing enum values.
    2# Be consistent with the way you check what entities are.

    This does nothing. Remove it.

    Are you sure this will be true? So far, your code looks like it should work. Are you sure the player has been added to the array?
     
    SuperSniper likes this.
  7. Offline

    Minterrr

    Let me show you what I have now. And I don't think the player is being added to the arraylist. How would I check? I also thought it should work, I don't know why it isn't.
    Code:
    package me.admin.myrescb;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    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.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        List<Player> playerfirstJoin = new ArrayList<Player>();
    
        public void onEnable() {
    
            getServer().getPluginManager().registerEvents(this, this);
    
        }
    
        @EventHandler
    
        public void onPlayerDamage(EntityDamageByEntityEvent event) {
    
            Entity entity = event.getEntity();
    
            if (entity.getType() == EntityType.PLAYER) {
                if (playerfirstJoin.contains(entity.getName())) {
                    if (event.getDamager() instanceof Player) {
                        event.setCancelled(true);
                    }
    
                }
    
            }
    
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
    
            Player p = event.getPlayer();
    
            if (!(p.hasPlayedBefore())) {
                playerfirstJoin.add(p);
    
                ;
            }
        }
    
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event) {
    
            Player p = event.getPlayer();
    
            if (playerfirstJoin.contains(p)) {
                playerfirstJoin.remove(p);
            }
    
        }
    }
    
     
  8. @Minterrr
    Print a message to console whenever you add an entry to the list.
     
  9. Offline

    Minterrr

    Attached Files:

  10. @Minterrr
    Alright, that works, try putting a print statement in the EntityDamageByEntityEvent
     
  11. Offline

    Minterrr

    @AlvinB nothing came up in console
     

    Attached Files:

  12. Offline

    Zombie_Striker

    @Minterrr
    Here's the problem; You're putting in a Player instance, and trying to see if it contains a String. The object you store has to be consistent, and I would highly recommend changing the collection type to a UUID instead of a player. Since storing players can create memory leaks and players can change their names, UUIDs are the only thing that stays the same.
     
    bwfcwalshy likes this.
  13. Offline

    Minterrr

    I am not familiar with using UUID's could you please show me how to do it?

    @Zombie_Striker
     
  14. Offline

    ArsenArsen

  15. Offline

    Zombie_Striker

    @Minterrr
    To change your collection to store UUIDs, use the following
    Code:
    List<UUID> ......
    To get player's UUIDs
    Code:
    player.getUniqueID()
     
  16. Offline

    Minterrr

    @Zombie_Striker

    I have changed it to UUID but it still isn't working. No errors in console...
    Code:
    package me.admin.myrescb;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    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.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        List<UUID> playerfirstJoin = new ArrayList<UUID>();
    
        public void onEnable() {
    
            getServer().getPluginManager().registerEvents(this, this);
    
        }
    
        @EventHandler
    
        public void onPlayerDamage(EntityDamageByEntityEvent event) {
    
            Entity entity = event.getEntity();
    
            if (entity.getType() == EntityType.PLAYER) {
                if (playerfirstJoin.contains(entity.getName())) {
                    event.setCancelled(true);
                }
    
            }
    
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
    
            Player p = event.getPlayer();
            UUID id = p.getUniqueId();
    
            if (!(p.hasPlayedBefore())) {
                playerfirstJoin.add(id);
    
            }
        }
    
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event) {
    
            Player p = event.getPlayer();
            UUID id = p.getUniqueId();
            if (playerfirstJoin.contains(id)) {
                playerfirstJoin.remove(id);
            }
    
        }
    }
    
     
  17. Offline

    Zombie_Striker

    You still have not changed this line. Replace it with this:
     
  18. Offline

    Minterrr

Thread Status:
Not open for further replies.

Share This Page