Making my own Player-class?

Discussion in 'Plugin Development' started by hayer, Mar 13, 2011.

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

    hayer

    Hi

    How can I make my own player class? So I can do something like this:
    ..inside onCommand:
    Code:
    ((MyPlayerClass)sender).getGold()
    
    This is what I have so far, but well..
    Code:
    import org.bukkit.entity.*;
    import org.bukkit.event.player.*;
    
    import org.bukkit.craftbukkit.CraftServer;
    import org.bukkit.craftbukkit.entity.CraftPlayer;
    import net.minecraft.server.EntityPlayer;
    
    public class MyPlayer extends CraftPlayer{ //extends Player {    
        public MyPlayer(CraftServer serv, EntityPlayer player)
        {
            super(serv,player);
        }
        
        public int Gold = 1000;
    }
    
     
  2. Offline

    Edward Hand

    If all you're doing is saving bank balances then it might be simpler to just use a HashMap or something.
     
  3. Offline

    Afforess

    You might want to make a player wrapper, not a player class.
     
  4. Offline

    Infernus

    How would you even replace the original player? In UnrealScript I thought it was hard, but at least it's possible there, IIRC it isn't here.
     
  5. Offline

    gnftoxic

    You simply need a class that implements Player. Not the whole extending CraftPlayer crap.
    In your Main class, you should create a arraylist or hashmap of your custom player class.
    Code:
    public HashMap<Player, MegaPlayer> customPlayers = new HashMap<Player, MegaPlayer>();
    In your PlayerListener class, you should initiate a MegaPlayer (your custom player class) and store it in that hashmap via the hook: PLAYER_LOGIN.
    Code:
    @Override
    public void onPlayerLogin(PlayerLoginEvent event)
    {
        plugin.customPlayers(event.getPlayer(), new MegaPlayer(event.getPlayer()));
    }
    Your MegaPlayer class should look like this:
    Code:
    public class MegaPlayer implements Player
    {
        private Player basePlayer;
        public MegaPlayer(Player player)
        {
            basePlayer = player;
        }
        // TODO: Your code here.
    
        // TODO: All Player's implemented crap here.
    }
    I do not know if this code works or not, this is going off of memory from my first mod, a private general plugin.
     
  6. Offline

    Edward Hand

    Erm...Player is an interface. You implement it not extend it.
     
  7. Offline

    gnftoxic

    ... mah bad. As I said, was going off of memory :p
    Updated the code, fixed the post.
     
  8. Offline

    hayer

    Ah, there got it working - of course I'm not only going to have "gold". Adding some more snacks to it as well.
     
Thread Status:
Not open for further replies.

Share This Page