Sharing Variables With PlayerListener Class

Discussion in 'Plugin Development' started by Jason Malouin, Nov 20, 2015.

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

    Jason Malouin

    Hey Guys, I have an issue today. I am trying to access a distance variable from my PlayerListener class so that when a player does /enemy, it shows the distance to the enemy player. Here's what I have so far.

    Code:
        if(cmd.getName().equalsIgnoreCase("enemy"))
        {
            if(!this.getConfig().get(player.getName().toString() + "E").equals("No Enemy")){
    
           player.sendMessage(ChatColor.GOLD + "Enemy: " + this.getConfig().get(player.getName() + "E"));
           player.sendMessage(ChatColor.GOLD + "Distance: " + this.getConfig().get(player.getName().toString() + "D"));
            }else{
                player.sendMessage(ChatColor.DARK_RED + "You Do Not Have An Enemy");
            }
        }
    
    



    Code:
      @EventHandler
      public void onPlayerDeath(PlayerDeathEvent event)
      {
        
         final Player player = (Player)event.getEntity();
         final Player target = (Player)player.getKiller();
         final double distance = player.getLocation().distance(target.getLocation());
        
         tour.getServer().getScheduler().scheduleSyncRepeatingTask(tour, new Runnable() {
    public void run(){
         tour.getConfig().set(player.getName().toString() + "D", distance);
         tour.getConfig().set(player.getName().toString() + "E", target.getName());
         tour.getConfig().options().copyDefaults(true);
          tour.saveConfig();
             }
         },20L, 20L);
         target.sendMessage(ChatColor.GOLD + "Player " + player.getName() + " Is Now Your Enemy ! Watch Out !");
         player.sendMessage(ChatColor.GOLD + "Player " + target.getName() + " Is Now Your Enemy !");
      }
    
    
    

    I want to share my distance variable with my main class where my commands are called. Is there a way to do this ? Thanks !
     
  2. Offline

    RoboticPlayer

    @Jason Malouin create a private field (may want to use a HashMap however, if you are dealing with a specific player) along with a getter and setter. Then instead of creating a local variable for the distance, just assign the value to the field/HashMap.
     
  3. Offline

    Jason Malouin

    Thanks for the suggestion. I looked up some threads on how I would go about doing this, but I'm still not exactly sure. I was wondering is you could show me an example of this ? Thanks.
     
  4. Offline

    Mrs. bwfctower

  5. Offline

    Jason Malouin

    well , its not that easy if I cant make a public static variable in the PlayerListener class
     
  6. Offline

    Mrs. bwfctower

    @Jason Malouin First of all, it's not good practice to have public field in a class because any class with an instance can change the object the field is referring to. When you make it static, you just make the problem worse because any class can now access the field. It's better to have a private field with getters and setters.

    (Use non-static private field)
    1. Pass the instance of the class containing the field to the class you want to access it from.
    2. Call the getter method you have created.
    3. You now have the variable!

    Another thing, if you want to be sure that I see your message, please reply to a post of mine or tag me.

    It is still a part of basic Java to be able to pass around class instances and get variables from other classes. As I said before,
    I mean this in the most sincere and positive way possible. I don't want to discourage you from learning Java and eventually using the Bukkit API to make plugins. I want you to learn, but to learn it right.
     
  7. Offline

    Jason Malouin

    @Mrs. bwfctower Okay, so this is whit I've come up with, although it says "an internal error has occurred" when I do /enemy.

    Code:
    public class PlayerListener
      implements Listener
    {
        private double distance;
        public double getDistance() {return distance;}
         
          public void setDistance(double distance) {this.distance = distance;}
    
    

    Code:
    //Setting distance variable
      @EventHandler
      public void onPlayerDeath(PlayerDeathEvent event)
      {
         
         final Player player = (Player)event.getEntity();
         final Player target = (Player)player.getKiller();
         distance = player.getLocation().distance(target.getLocation());
    
    

    Code:
    //Where I attempt to call the distance
        if(cmd.getName().equalsIgnoreCase("enemy"))
        {
            if(!this.getConfig().get(player.getName().toString() + "E").equals("No Enemy")){
    
           player.sendMessage(ChatColor.GOLD + "Enemy: " + this.getConfig().get(player.getName() + "E"));
           player.sendMessage(ChatColor.GOLD + "Distance: " + playerListener.getDistance());
            }else{
                player.sendMessage(ChatColor.DARK_RED + "You Do Not Have An Enemy");
            }
        }
    
    {/Code]
    
    
    I know I messed something up, and I tried a few things. I just can't seem to get the correct output.
     
  8. Offline

    Mrs. bwfctower

  9. Offline

    Jason Malouin

    @Mrs. bwfctower Oh im sorry,

    Code:
      public Tour(PlayerListener playerListener) {
    
    
      this.playerListener = playerListener;
    
    
      }
    
    
     
  10. Offline

    Mrs. bwfctower

    @Jason Malouin And where do you instantiate Tour? Or is Tour the class extending JavaPlugin?
     
  11. Offline

    Jason Malouin

    Last edited: Nov 20, 2015
  12. Offline

    Mrs. bwfctower

  13. Offline

    Jason Malouin

    @Mrs. bwfctower I can't I get errors

    "Remove Invalid Modifiers"
    "Create Class "PlayerListener" in Type "Tour"

    Code:
      public void onEnable()
      {
       
        PluginDescriptionFile pdfFile = getDescription();
        this.logger.info(pdfFile.getName() + " Has Been Enabled");
        PluginManager pm = getServer().getPluginManager();
        pm.registerEvents(new PlayerListener(this), this);
        Listener listener = new PlayerListener(null);
        private PlayerListener playerListener;
    
    
        public Tour(PlayerListener playerListener) {
    
    
        this.playerListener = playerListener;
    
    
        }
    
    
    
     
  14. Offline

    mcdorli

    Learn java, that's the only thing I can say.
     
Thread Status:
Not open for further replies.

Share This Page