Player wrapper?

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

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


    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:
    OwnPlayer ownplayer = (OwnPlayerevent.getPlayer();
    in an event.. Please help
  2. Online

    timtower Moderator Moderator

  3. Offline


    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 Moderator Moderator

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


    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 Moderator 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


    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:

    public interface TrolledPlayer extends Player {

    boolean isFrozen() {


    Here the error:
    Error (open)

    [20:49:45 ERROR]: Could not pass event PlayerMoveEvent to TrollSystem v1.0.0
    at$1.execute( ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a( [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$ [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at java.util.concurrent.Executors$ Source) [?:1.8.0_162]
    at 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( [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at 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( ~[?:?]
    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$1.execute( ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    ... 15 more


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


    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


    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


    You dont want to inherit Player or CraftPlayer. Instead, you want to wrap it with a class. Example:
    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