Object Oriented Coding

Discussion in 'Plugin Development' started by RealDope, Feb 20, 2013.

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

    RealDope

    Okay I understand the concept of objects, creating new instances of them, storing values for them, etc.

    I don't understand how to get that object though.. I have an object that is made for each player, would I just create a method that returns this object, takes a name for a parameter, and just creates a new object everytime it's called? Example:


    Code:JAVA
    1.  
    2. public class Character {
    3. String name;
    4. public Character(String name) {
    5. this.name = name;
    6. }
    7. }
    8.  


    Code:JAVA
    1.  
    2. public class Plugin extends JavaPlugin {
    3. public Character getCharacter(String name) {
    4. return new Character(name);
    5. }
    6. }
    7.  
     
  2. Offline

    ZeusAllMighty11

    I'd like to know this too!
     
  3. Offline

    bcbwilla

    To create a new Character object, you would do something like this:

    Code:
    Character character = new Character("bcbwilla");
    This creates a new Character object with the name bcbwilla that is stored in the variable character.

    See here for more details.

    Edit: Oh! I misunderstood what you are asking. Sorry. :)
     
  4. I belive you are asking how to store it and retrieve it by inputting player name ?
    If so, use a Map !
    Map<String, YourClass> playerStuff = new HashMap<String, YourClass>();

    And you can easily add/replace, get or remove you just use playerStuff.put(player.getName(), ...), playerStuff.get(), playerStuff.containsKey() and playerStuff.remove().
    A bit of warning, get() and containsKey() are awesomely fast, however containsValue() is very unefficient and slow because a hashmap is not meant to do that.

    Also, Character is an existing Java class, I suggest you take on a more unique name :p
     
  5. Offline

    RealDope

    Yeah that was just for my example :p

    Okay thanks, I guess I will just keep a Map, adding the "Character" object for them to the map when they join and removing it when they leave.

    While we're talking about Maps, what's the difference between using a HashMap (What I always have used), a Map, and a TreeMap. I assume there are others but these are the ones I have heard of. Treemaps are somehow ordered right?
     
  6. Offline

    Tirelessly

    You'd need to store a Set of the objects. Then, if you wanted to get one by it's name, make a method that returns your object type and iterates through the set, returning one if it matches the given rules.
     
  7. That's very bad.
    Sets are good for finding if an object exists in them, iteration is not their strong point, it might as well be a list because it's better at iteration.
    But neither are useful when you can use a Map if you need something by key and String is one of the best keys you can use for hash stuff.

    Map is the interface and allows you to set any kind of map to it (like tree map, hash map, etc)
    And yes, TreeMap preserves the order and you can sort the entries... and HashMap is just another type of map... for technical details and stuff I suggest you look at the manual for it provided by Java.
    Example of Map interface:
    Code:
    Map<String, String> myTree = new TreeMap<String, String>();
    Map<String, String> myHash = new HashMap<String, String>();
    // etc...
     
  8. Offline

    Tirelessly

    That's why you use an iterator..
     
    Fuzzwolf likes this.
  9. Offline

    Mango

    RealDope Cryptographers code; programmers program.
     
Thread Status:
Not open for further replies.

Share This Page