Finding the player in custom method?

Discussion in 'Plugin Development' started by TopGear93, May 24, 2012.

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

    TopGear93

    Im sure this is an easy question but how would i return the exact player from an event to use in another class? I would like to know this because a method im trying to use in another class will not pick up that player and send them the message.

    Custom method
    Show Spoiler

    PHP:
    public class rankgift {
        public 
    ReferGift plugin;
        
    Random r = new Random();
        
    PlayerListener pl = new PlayerListener(plugin);
        public 
    rankgift(ReferGift plugin){
            
    this.plugin plugin;
        }
        public 
    void referrank(Player player){
            
    String playername player.getName().toLowerCase();
            
    int playernameamount Collections.frequency(ReferGift.thereferrerplayername);
            if((
    playernameamount <= 1) || (playername == null)){
                if(
    r.nextInt(10) == plugin.getConfig().getInt("refergift.rankaward.referrank.random")){
                    
    player.sendMessage(ChatColor.DARK_RED "[" ChatColor.GRAY plugin.getConfig().getString("refergift.title") + ChatColor.DARK_RED "]-" ChatColor.WHITE plugin.getConfig().getString("refergift.rankaward.rankonemsg"));
                }
            }
    }
    [/
    spoiler]
    The listener
    Show Spoiler

    PHP:
    public class PlayerListener implements Listener{
        public 
    ReferGift plugin;
        public 
    PlayerListener(ReferGift plugin){
            
    this.plugin plugin;
        }
        @
    EventHandler
        
    public void onPlayerJoin(PlayerJoinEvent event) { 
            
    rankgift rankl = new rankgift(plugin);
            
    Player player event.getPlayer();
            
    String pname player.getName().toLowerCase();
            if(
    plugin.getConfig().getBoolean("refergift.rankaward.receive.switch") == true){
                
    rankl.referrank();
            }
    }
    [/
    spoiler]
     
  2. Offline

    Deathmarine

    Try this, on your listener
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerJoin(PlayerJoinEvent event) {
    4. Player player = event.getPlayer();
    5. //String pname = player.getName().toLowerCase();
    6. if(plugin.getConfig().getBoolean("refergift.rankaward.receive.switch") == true){
    7. plugin.referrank(player);
    8. }
    9. }
    10.  

    or you can just move the method to the listener class
    Code:java
    1.  
    2. public class PlayerListener implements Listener{
    3. public ReferGift plugin;
    4. public PlayerListener(ReferGift plugin){
    5. this.plugin = plugin;
    6. }
    7. @EventHandler
    8. public void onPlayerJoin(PlayerJoinEvent event) {
    9. rankgift rankl = new rankgift(plugin);
    10. Player player = event.getPlayer();
    11. String pname = player.getName().toLowerCase();
    12. if(plugin.getConfig().getBoolean("refergift.rankaward.receive.switch") == true){
    13. this.referrank(player);
    14. }
    15. }
    16. public void referrank(Player player){
    17. String playername = player.getName().toLowerCase();
    18. int playernameamount = Collections.frequency(ReferGift.thereferrer, playername);
    19. if((playernameamount <= 1) || (playername == null)){
    20. if(r.nextInt(10) == plugin.getConfig().getInt("refergift.rankaward.referrank.random")){
    21. player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.GRAY + plugin.getConfig().getString("refergift.title") + ChatColor.DARK_RED + "]-" + ChatColor.WHITE + plugin.getConfig().getString("refergift.rankaward.rankonemsg"));
    22. }
    23. }
    24.  
     
  3. Offline

    TopGear93

    yea i could add the method to the listener but thats only the first few lines of code. the custom method is almost 500 lines long lol. Im trying to keep everything organized into its own class.
     
  4. Offline

    Deathmarine

    Woah wait. I didn't quite see this all the way through. You were right the first time with just the rank1.referrank(player);
     
  5. Offline

    TopGear93

    hmm, i cant quite understand why my custom method wont work then. The frequency collector should pickup that the referrer's name appears once on the list. The method should fire this off then but i wont even bother.

    PHP:
    if(playernameamount == 1){
                if(
    r.nextInt(10) == plugin.getConfig().getInt("refergift.rankaward.referrank.random")){
                    if (
    plugin.getConfig().getBoolean("refergift.rankaward.block.switch") == true) {
                        
    String giftsplit plugin.getConfig().getString("refergift.rankaward.gift.one");
                        
    String[] splitstring giftsplit.split(":");
                        
    int dura0 Integer.parseInt(splitstring[0]);
                        
    short dura1 0;
                        if (
    splitstring.length == 2) {
                            
    dura1 Short.parseShort(splitstring[1]);
                        }
                        
    ItemStack item1 = new ItemStack(Material.getMaterial(dura0), plugin.getConfig().getInt("refergift.rankaward.block.amount.one"), dura1);
                        
    player.getInventory().addItem(new ItemStack[] { item1});
                    }else{
                        
    ReferGift.economy.depositPlayer(player.getName(), plugin.getConfig().getInt("refergift.rankaward.moneyamount.one"));
                        
    player.sendMessage(ChatColor.DARK_RED "[" ChatColor.GRAY plugin.getConfig().getString("refergift.title") + ChatColor.DARK_RED "]-" ChatColor.WHITE plugin.getConfig().getString("refergift.thanksfor") + player.getName());
                    }
                }
            }
     
  6. Offline

    Deathmarine

    Do you have a github? It might not be here. I'm kinda curious about your static reference ReferGift.thereferrer
     
  7. Offline

    TopGear93

  8. Offline

    Deathmarine

    Alright. So I sent 3 pull requests till I realized how much needed to change. I forked and imported it. Reworked most of it and tested it. Your command structure is a bit querky as well as since most of the commands are based off players any commands sent via the console will not work. Just pull my fork and merge it if you like. Its all yours but there were alot of changes I made to it.
     
  9. Offline

    TopGear93

    uh thanks for rebuilding most of plugin when it didnt really need it. My only question was how to fix this method issue.

    so what was the main issue for the addon method? I just needed to change the frequency collector to a for loop?
     
  10. Offline

    Deathmarine

    http://mindprod.com/jgloss/static.html
    Sorry its a pet peeve.
    "Every time a executed a static method in one class, no matter how trivial, synchronized or not, it would just hang. IntelliJ trace would just go to sleep. I was doing threading, and I had some locks internal to two ConcurrentHashMaps in the class, but they should lock on the Collection, not the whole class."
     
Thread Status:
Not open for further replies.

Share This Page