Solved Extending player class

Discussion in 'Plugin Development' started by thegendolz, Nov 9, 2013.

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


    Dear bukkit developers,

    I have a question / problem. I am currently making my own bukkit economy. It's full finished now, for my plugin i use database connection. The problem is that the Mysql server has to handle around 200 / 1000 queries per second and this is nearly impossible to handle for my database. So i was thinking how i could solve this problem.

    Then i thought about the idea of extending the player class. When the player logs in on the server, i do i query to get 3 variables and when the player logs of i set the variables to the database. What i want to do is, set 3 variables with getters and setters in the player class. So i can use
    player.removeMoneyByAmountVariable1(int amount);

    player.setVariable1() = UPDATE user SET money = "variable1" WHERE player = "player.getName()";

    But how can i extend the player class. I searched on the internet but none of the answers gave me the answer i want.

    It would really help me is someone could tell me how to do this. I want to mention that i am a experienced java programmer so i know how extends work.

    PS: my english is not great
  2. You could also make a hashmap, and store the player name and money as pairs inside the hashmap
  3. Offline


    This is true, but i think it's easier to use an extend so you can just call it from the player class.

    I will explain a bit more. When you kill a mob, you get money. So when the EntityKilledbyEntityEvent is called and i get the entity which is a player. I want to quickly use player.giveMoneyByAmountVariable1(int amount). So i don't have to iterate through the whole hashmap to add some money
  4. it is actually harder to do when you extends the player class, because you need to edit al the craftbukkit using reflection to inject your own player class. You also need to use casts at al your listeners to use the special functions.
    The advantage of a hashmap is that 2 plugins can use the hashmap approach, but 2 plugins cannot use the custom player concept together.
  5. Offline


    There is a library called lombok that will allow you to do this.
  6. Offline


    Create a wrapper for your player object to store all of your new variables, as Bukkit does.
    drtshock likes this.
  7. Offline


    Can you give me an example of this? if you don't mind.
  8. Offline


    Or you could just use meta data. That's what I use for things like this, then only save to mysql / files when you need to :)
    DSH105 likes this.
  9. Offline


    • Create a new class that allows you to access your player object.
    • Store the wrappers in a List (or HashSet)
    • Add in your own methods, including one to find the appropriate wrapper for a certain Player
    Honestly though, using a HashMap, as ferrybig suggested would be a lot easier. An extension of a player class simply for convenience is going to be a lot harder than using a HashMap. You can create methods that retrieve and save data to and from the HashMap anyway.
  10. Offline


    Or just create a sudo-wrapper class for Player:

    1. public class YourPlayer {
    3. private final String name;
    4. /* other fields */
    6. public YourPlayer (String name) {
    7. = name;
    8. /* other initialization */
    9. }
    11. public String getName() {
    12. return;
    13. }
    15. /* other getters / setters / etc */
    17. }

    And then save relevant info there.
  11. Offline


    Have a HashMap of String -> CustomPlayers and create the CustomPlayer class.

    (It's only pseudo code)
    2. public class CustomPlayer
    3. {
    4. private Player player;
    5. //custom variables
    6. public CustomPlayer(Player player)
    7. {
    8. this.player = player;
    9. }
    11. //custom methods
    12. }

    Since you are storing a direct Player object, you will want to ensure you remove any references to the CustomPlayer instance when they log off to avoid memory leaks.
  12. Offline


    So i solved the problem by using a hashmap. It was way easier then i expected. so what i do is:

    1. private HashMap<String, Integer[]> players = new HashMap<String, Integer[3]>();

    Then i can use:

    1. Integer[] data = players.get(p.getName());
    2. data[0] = variable1
    3. data[1] = variable2
    4. data[2] = variable3

    I want to thank you :).
Thread Status:
Not open for further replies.

Share This Page