Copying Player object to a new object implementing modified Player interface (extends Player)

Discussion in 'Plugin Development' started by Wolfy9247, May 25, 2013.

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

    Wolfy9247

    My wording might be a bit confusing, but here's what I'm needing to accomplish:

    I've currently created an ActivePlayer.java class implementing my SAPlayer.java interface that extends Player (org.bukkit.entity.Player). The issue I'm running into is that, for every online player at a certain point, I need to convert them all to an ActivePlayer instead of a Player; however, I have no idea how I would accomplish this.
     
  2. Offline

    Wingzzz

    Wolfy9247
    Well I guess there are a few ways you could do this.
    • Make a listener for the PlayerJoinEvent and when a player joins you can add them to a list/map/collection/set as a new ActivePlayer. Map for example:
    Code:
    Player player = event.getPlayer();
    String playerName = player.getName();
    ActivePlayer activePlayer = new ActivePlayer(playerName); // Assuming it's constructor takes a string for a name
    activePlayerMap.put(playerName, activePlayer);
    
    Ensure if you do it this way you similarly remove the player from the map on the PlayerQuitEvent.
    • You could have a repeating task run every X amount of time to parse through all online players and add / remove from a map/collection/list/set accordingly.
     
  3. You should make your class extend CraftPlayer (since you are messing with Craftbukkit stuff when implementing the bukkit api) and have to replace the default playerlist with yours. I don't see why you can't just have a HashMap or something to save stuff for every online player. Thats way easier than what you are attempting.
     
  4. Offline

    Wolfy9247

    mncat77: Creating a LinkedHashMap has been my alternative for this project; however, it would help to be more integrated with what I'm doing if I could have a player with both my added methods, and the default Player methods.

    For example: A new game is started, a list is updated with all current active players on the server. Those players are assigned both a target, and a killer. Let's say that a target quits, the killer will need to be assigned a new target. Easily I can get the target player by getting the ActivePlayer object with the Player object as a key from a HashMap. From this, I can call player.getAssassin() and get an ActivePlayer instance of the assassin in that map. Now I need to send that player a message, which I could do by putting the Player instance in the ActivePlayer object and calling player.getPlayer().sendMessage(), but I just hate having to do that instead of just doing player.sendMessage() (and I do realize that I could implement sendMessage() in there, but I don't want to make a copy of every method to call on another method and so on).
     
  5. Offline

    Wingzzz

    Wolfy9247
    You don't need to extend, or implement anything now that I think of it...
    Simply make something like this:
    Code:
    package com.github.pixelwingz;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
     
    public class ActivePlayer {
     
        private Player player;
        private String name;
        private String target;
       
        public ActivePlayer(String name) {
            this.name = name;
            setPlayer(this.name);
        }
       
        public Player getPlayer() {
            return this.player;
        }
       
        public void setPlayer(String name) {
            this.player = Bukkit.getPlayer(this.name);
        }
       
        public String getTarget() {
            return this.target;
        }
       
        public void setTarget(String target) {
            this.target = target;
        } 
       
    }
    
    If you want to use Player methods, simply use the getPlayer() method...
    Make a new ActivePlayer like this: (would be useful onJoin or something)
    Code:
    ActivePlayer aPlayer = new ActivePlayer(event.getPlayer().getName());
    
    Messaging Class I whipped up for you:
    https://gist.github.com/pixelwingz/d5137b986f994976c090
    Ensure you make a static method in your main (the class that extends JavaPlugin) like this perhaps:
    Code:
    public static Plugin getInstance() {
        return this;
    }
    
    This will allow you to use the Messaging.java class to it's full extent... I use methods of a Plugin in the class so if you wish not to have to do this simply remove anything related or make it a non-static class (since I am using it as an all static method class and it can't be instantiated at the moment).
     
Thread Status:
Not open for further replies.

Share This Page