Add attribute/method to player class ?

Discussion in 'Spigot Plugin Development' started by RedRikudo, Jun 10, 2018.

  1. Hello,

    I would like to make a mini games with some guns. While I was making the plugin, I was wondering : is it possible to do something like :
    Code (Java):
    or even :
    Code (Java):

    Until now I've been doing it with arrays : I did a 2 row array to associate each player with a number of Ammos. But I just realised it is a bit messy and boring to browse each array (because I don't only have ammos) everytime I want to get this info. Any way ?
  2. foncused

    Moderator Patron

    You could create a class to wrap all the information you want to store (the player's ammo, experience, stats, etc.) and provide the getter and setter methods in that class. From there you would store all instances of the class in some sort of collection (Map<UUID, MyPlayer>?).
    • Agree Agree x 1
  3. Not a bad idea ! I will try that :)
  4. You could also try extending the Player class then just instantiate your newly created player class each time instead of using Player.class if you get me. you can then override getName() etcetera or something if you would want to manipulate that idk why you would tho lol
    • Agree Agree x 1
  5. Not sure if you can extend an interface (what Player is in spigot-api) (come on, basic java), and implementing Player would just be horrendous. The wrapper idea is just much better then.
    • Agree Agree x 1
  6. Benz56

    Moderator Supporter

    Manager class:
    Code (Java):
    public class PlayerDataManager {

       private final static Map<UUID, PlayerData> playerData = new HashMap<>();

       public static PlayerData getPlayerData(UUID uuid) {
            return playerData.computeIfAbsent(uuid, k -> new PlayerData());

    Code (Java):
    public class PlayerData {
        private int ammo = 10;
        private int age = 20;
        private boolean isCool = true;

        //Getters and Setters below
    This is loosely typed but should be what you are looking for.
    #6 Benz56, Jun 10, 2018
    Last edited: Jun 10, 2018
  7. interfaces can extend interfaces my idea was probably shit, you could also parse the Spigot Player as a parameter in a classes constructor, and just reference the player whenever, idk
    #7 NaJated, Jun 10, 2018
    Last edited: Jun 10, 2018
  8. Im learning new things hehe
  9. foncused

    Moderator Patron

    Remember to follow Liskov Substitution Principle (program against abstractions instead of implementations):
    Code (Java):
    private final Map<UUID, PlayerData> data = new HashMap<>();
    • Agree Agree x 2
  10. Benz56

    Moderator Supporter

    But yea, you are 100% right. I’ll correct it.
  11. Also known as a wrapper class :p:p:p
  12. JanTuck


    With Lombok you can do this:
    • Winner Winner x 1
    • Useful Useful x 1
  14. I love lombok so much haha.