Solved How would I technically speaking right an API/Extenstion to the player that I could use like Ess?

Discussion in 'Plugin Development' started by TheEnderCrafter9, Jul 26, 2016.

Thread Status:
Not open for further replies.
  1. Let's say johnny would want to right a class that extends (as in adds, not the java (I forgot the word)) the player. How would you achieve this? I've looked through the plugin Essentials which uses a custom player base. However, non of the code seems to implement/extend the player class (speaking java) , which makes it look like a lot of work. What would the steps be to create a class that does this?

    (I'm talking about a class (the final product [as in class, this is just to explain what I'm looking for. ] I expect the final thing to be more then half a dozen classes filled with code (Like Essentials).) that would hopefully look not to complicated, something like this (this doesn't work))
    cool.png
     
  2. @TheEnderCrafter9 Learning Java before using a Java API is always a good idea. How do I know you haven't?
    1. Boolean - You're using the Boolean object.
    2. == true
    3. You return booleans yet check a Boolean just to return a Boolean.
     
    Zombie_Striker and ArsenArsen like this.
  3. Offline

    ArsenArsen

    To add to all of the above, interfaces are not extendable. You make a stack overflow in your isOp. You should just make a 'wrapper' class, or just use a Map<UUID, Boolean> to store isAfk isGod. Also use boolean while defining variables. Not Boolean. As it is a primitive.
     
  4. I typed this up in a hurry, D: didn't notice any of the things you guys mentioned.

    I might have not made myself clear. I can, I'm just curious how developers would write an api (like a todo list).

    I know interfaces are not extendable. I'm going to use a map and set this as solved because IDC at this point
     
  5. Offline

    I Al Istannen

    @TheEnderCrafter9
    To explain what @ArsenArsen said:

    I haven't looked at the essentials code, but I would assume they make a wrapper for a Player. So just a class which contains a Player object (or an UUID), and provides the additional things you need. Then you make a method returning the Player/UUID and can use that to interact with the normal player object.

    An example follows.
    The code is copy-pastable, which probably classifies it as spoonfeeding, but it is just really basic and easy skeleton and outlines what to do far better that I could do with words.
    It is hard to use bad practises in such a short snippet, but I would still like if somebody points it out.
    You can also make this with a Factory, but this works too.
    Code:
    package me.ialistannen.inventory_profiles;
    
    import java.util.Optional;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.entity.Player;
    
    /**
    * Wraps a player in a new object, to add some functionality (here AFK status)
    */
    public class PlayerWrapper {
    
        private boolean afk;
        // The UUID of the player that is being wrapped.
        private UUID playerID;
       
        /**
         * Creates a wrapper for a UUID
         *
         * @param playerID The UUID of the player
         */
        public PlayerWrapper(UUID playerID) {
            this.playerID = playerID;
        }
       
        /**
         * Creates a wrapper for the player
         *
         * @param player The Player to wrap
         */
        public PlayerWrapper(OfflinePlayer player) {
            this(player.getUniqueId());
        }
    
        /**
         * Checks the AFK status of a player
         *
         * @return True if the player is AFK
         */
        public boolean isAfk() {
            return afk;
        }
       
        /**
         * Sets the AFK status of a player
         *
         * @param afk If the player is AFK
         */
        public void setAfk(boolean afk) {
            this.afk = afk;
        }
       
        /**
         * Returns the player's UUID
         *
         * @return The UUID of the player
         */
        public UUID getPlayerID() {
            return playerID;
        }
       
        /**
         * Returns the player if he is online
         *
         * @return The player if he is online or an empty Optional if not
         */
        public Optional<Player> getPlayer() {
            return Optional.ofNullable(Bukkit.getPlayer(getPlayerID()));
        }
    }
    
     
  6. I don't know a lot about Wrapper classes as I am somewhat new to java (I have a basic understanding), but I am assuming this is a wrapper because of this code:

    Code:
        public PlayerWrapper(UUID playerID) {
            this.playerID = playerID;
        }
    
    [Correct me if I'm wrong]. So If I'm right, the class gets the UUID of the player and then adds some functions to it.
     
  7. Offline

    ArsenArsen

    Wrapper is a class thst holds a reference to another class and some data to go with it. Nothing complex.
     
  8. Thank you for the explanation. Also, you spelled "thst" (that) wrong :'(


    What would be the correct way to compare booleans?
     
  9. Offline

    I Al Istannen

    @TheEnderCrafter9
    A "Boolean" is the wrapper object for a "boolean". It is only needed when you want to insert it in a Collection or any other thing that takes objects.

    Boolean comparing and checking is pretty straightforward.

    If you have two booleans:
    "boolean first, second;"

    You just do it like this:
    if(first == second)
    if(first)
    if(second)
    if(!first)
    if(!second)
    if(first && second)
    if(first || second)
    if(!first && second)
    ...​

    You can make any expression you want with booleans, it is called "boolean algebra".

    EDIT: And ninja. But thanks for the link @bwfcwalshy, forgot about that :)
     
  10. TheEnderCrafter9 likes this.
Thread Status:
Not open for further replies.

Share This Page