Player wrapper?

Discussion in 'Plugin Development' started by OverDodo, Apr 21, 2018.

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

    OverDodo

    Does somebody know how to create a Player wrapper? Is that even possible? I mean an Interface that i can cast a Player to. Like:
    PHP:
    OwnPlayer ownplayer = (OwnPlayerevent.getPlayer();
    in an event.. Please help
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    OverDodo

    Lets me access all methods of the normal player but allows me to use my own methods as well (In my case void freeze, boolean isFrozen, void onFreeze and List frozenplayers)
     
  4. Online

    timtower Administrator Administrator Moderator

    @OverDodo Class that contains the player might be better.
     
  5. Offline

    OverDodo

    But that will not let me cast Player to it and not give me all methods from Player without getting the Player stored in it first
     
  6. Online

    timtower Administrator Administrator Moderator

    Then you need to extend it, but that also means that you can't have variables due to the casting.
     
  7. The easiest way to do this is just have a Map<Player, PlayerWrapper> and a forPlayer method that gets or creates a new wrapper. Sure, implement Player in it if you want, then handle all the functions with your root player (there's a lot!), but it's easier to just have a getRoot() method or something.

    Sent from my SM-G903F using Tapatalk
     
  8. Offline

    OverDodo

    Isnt it possible with interfaces?

    Okay imma try that, thanks c:


    Edit: It lets me cast it etc but how can i define the method now? If i implement the interface it wants me to implement all player methods.. But those are defined already, arent they? I´d just need to define the isFrozen() boolean.. How do I do that?


    Edit2: It does let me cast it, but it throws a ClassCastException telling me I cannot cast it to that.. My Interface looks like that rn:


    PHP:
    public interface TrolledPlayer extends Player {


       default 
    boolean isFrozen() {
           return 
    MySQL_Util_MTH.isFrozen(getUniqueId().toString().replaceAll("-",""));
       }


    }

    Here the error:
    Error (open)

    [20:49:45 ERROR]: Could not pass event PlayerMoveEvent to TrollSystem v1.0.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:270) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PacketPlayInFlying.a(SourceFile:126) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PacketPlayInFlying$PacketPlayInLook.a(SourceFile:88) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_162]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_162]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_162]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer cannot be cast to me.overdodo.trollsystem.TrolledPlayer
    at me.overdodo.trollsystem.listeners.PlayerMove_LST.onMove(PlayerMove_LST.java:19) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_162]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_162]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_162]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_162]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    ... 15 more


    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 21, 2018
  9. Offline

    RcExtract

    Player is an interface and you cannot "extends" it. But if you "implements" it, you will need to define all methods, which is very annoying, but you should not inherit the CraftPlayer class inside the craftbukkit because it is version dependent (package name changes every version). Therefore, wrapper is the best solution for storing many extra information towards a player outside of craftbukkit.

    If you only want to store very few extra information, consider using map instead.

    A wrapper class of Player should have a field storing which player it is storing properties for, and it should not be changeable after instantiation, just like you cannot instantiate an Entity and then add properties to it to make it to Player.

    EDIT: sorry i found that actually you was declaring an interface extending Player which is legal. But, still, you will need to implement all methods inside Player.
     
  10. Offline

    OverDodo

    How would I go with that? Would I need to extend all methods Player extends too or is it enough to just extend player? If i implement all the methods in the Interface, they are empty so how would I define the methods of the player?
     
  11. Offline

    RcExtract

    You dont want to inherit Player or CraftPlayer. Instead, you want to wrap it with a class. Example:
    Code:
    public class Something {
        private final Player player;
    }
    Make sure it is a final variable. I explained why in my previous post.
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page