Easy Java Question

Discussion in 'Plugin Development' started by The_Coder, Dec 1, 2013.

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

    The_Coder

    Hello community,
    I am having a simple java issue. So I have a class, which is a manager. I also have the base class, which stores information on the player. This is my issue, when I make a player, it uses the base class then stores all of the information in a list of the base class. But when I try to access the same player again the data is not there. I have looked in the logs but nothing. The only thing that I could think of was that the instances where not the same. If that is the case then I have no idea how to fix it. If you need more information about how the classes work together then let me know. Any help would be nice.
    Thank community you rarely fail me,
    The_Coder
     
  2. Offline

    themuteoneS

    Can you post some more details? Some actual code perhaps? And what do you mean by "creating a player". All instances of the Player class should be initialized by bukkit and passed to you by events.
     
  3. Offline

    Not2EXceL

    Your name makes me die inside.

    And what are you trying to actually achieve?
     
    Minnymin3 and sgavster like this.
  4. Offline

    1Rogue

    $10 on static instances gone wrong, or incorrect passing / reference of classes.
     
    themuteoneS likes this.
  5. Offline

    The_Coder

    Ok. Well I have a base class:
    Code:java
    1. public class BasePlayer {
    2.  
    3. private String name;
    4. private String brush;
    5.  
    6. public BasePlayer(String name, String brush) {
    7.  
    8. this.name = name;
    9. this.brush = brush;
    10.  
    11. }
    12.  
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16.  
    17. public void setBrush(String brush) {
    18. this.brush = brush
    19. }
    20.  
    21. // Some more stuff...
    22.  
    23. }


    Then I have another class which holds all of these:

    Code:java
    1.  
    2. public class BasePlayerManager {
    3.  
    4. List<BasePlayer> bps = new ArrayList<BasePlayer>();
    5.  
    6. // A bunch of checking methods and getters and setters for the arraylist...
    7.  
    8. }
    9.  
    10.  


    I put the player in the list via another class, and then right after entering the data I tried to get the data out and a npc appered. I have no idea why it is doing this.

    You are soo right I think that is the problem to. Of course I was so eager to code bukkit I didn't finish all the tutorials on newboston's channel. Doesn't help that I was looking at the most confusing plugin: VoxelSniper.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  6. Offline

    Not2EXceL

    I'm still confused. Why would an NPC spawn after attempting to access a list that only holds objects that contain strings?

    The_Coder learn java to a better proficiency that the new Boston teaches. And don't look at other plugins. The Bukkit api is very well documented. Its very easy to learn
     
  7. Offline

    The_Coder

    Well if I didn't try to get the data out then it doesn't throw the npc.
     
  8. Offline

    morshu9001

    Can we see the code where you're adding a BasePlayer to that list and accessing it?

    Also, you shouldn't be initializing the ArrayList in the instance variable area of the BasePlayerManager class.
    List<BasePlayer> bps = new ArrayList<BasePlayer>(); //bad coding practice
    You should only declare it then initialize it in the constructor.
    List<BasePlayer> bps;
    ...
    public BasePlayerManager(){
    bps = new ArrayList<BasePlayer>();
    }
    That alone might be the problem, if I remember my Java correctly after abandoning it for so long. Or at least it's just bad practice with no real functional consequence.
     
  9. Offline

    The_Coder

    Well here is where the Base class gets added to the manager:
    Code:java
    1. public void addPlayer(Player player) {
    2. if(!bps.contains(getPlayerByName(player))) {
    3. BasePlayer bp = new BasePlayer();
    4. bp.setPlayer(player.getName());
    5. bp.setCurrentBrush("");
    6. }
    7. }
     
  10. Offline

    morshu9001

    You never did bps.add(bp).
     
    The_Coder likes this.
  11. Offline

    The_Coder

    Holy crap thanks man!!!!!!

    Well that is one problem but I still get the npc when getting stuff out. :/

    Got everything working thanks though.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  12. Offline

    BungeeTheCookie

    Your welcome :p
     
Thread Status:
Not open for further replies.

Share This Page