jinpachi23
jinpachi23

Reputation: 27

I have an issue with casting Player to a MyPlayer

Hello I started learning to code minecraft bukkit plugins just few days a go, so please don't blame me if my issue is stiupid. I want to create a new MyPlayer class that will be Player subclass. I have already figured out that Player's root is org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer so I can make somethink like this: public class OticPlayer extends org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer. I want MyClass to contain all of his parent methods, but add some it's own. The problem is when i use: Bukkit.getPlayerExact(arg3[1]) it return the reference to a Player type object. I have a Lobby class with method addPlayer(MyPlayer arg0), so I need a reference to the MyPlayer type object, not Player. When I will try to cast a Player type reference to MyPlayer, it's throws an exception: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer cannot be cast to me.gtddd.my.MyPlayer. I need to pass MyPlayer type reference to the addPlayer() method, because I want to make some kind of stats system (K/D/A), each of these stats must be a pool accesable, by MyPlayer type object. So how can I cast Player to MyPlayer? Example code that generates this problem:

package test;

import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_12_R1.CraftServer;
import org.bukkit.plugin.java.JavaPlugin;

import me.gtddd.otic.OticPlayer;
import net.minecraft.server.v1_12_R1.EntityPlayer;

public class MyPlayer extends org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer {
    public MyPlayer(CraftServer server, EntityPlayer entity) {
        super(server, entity);
    }

    int kills = 0;
    int deaths = 0;
    int assists = 0;

    public int getKda() {
        return kills/deaths/assists;
    }
}

public class Lobby {
    MyPlayer[] players = new MyPlayer[10];

    public void addPlayer(MyPlayer arg0) {
        players[players.length] = arg0;
    }

    public MyPlayer getPlayer(int slot) {
        return players[slot-1];
    }
}




public class Main extends JavaPlugin { 
    @Override
    public void onEnable() {
        Lobby lobby1 = new Lobby();
        MyPlayer player = (MyPlayer) Bukkit.getPlayerExact("Notch");
        lobby1.addPlayer(player);
        System.out.println(lobby1.getPlayer(1).getKda());
    }
}

I looked lot to find the answer, but I was not able to find anythink what would satisfy me. From top: Thanks for all answers! If somethink is unclear ask.

Upvotes: 0

Views: 635

Answers (1)

LeoColman
LeoColman

Reputation: 7143

Instead of extending a Player, you should create a Wrapper around Bukkit's Player Object, more specifically wrapping the Player's Name or UUID (recommended) as it's not possible to edit Bukkit's Player instances without a lot of effort, it's implementation is very obscure, and you won't be able to include your MyPlayer in Bukkit's server.


So, for example, you could create a MyPlayer as following:

class MyPlayer {
    private String playerName;

    public MyPlayer(Player player) {
        playerName = player.getDisplayName();   //Something like this to store the player
    }
    //More of your code, for example counting the kills

    public Player recoverPlayerObject() {
         return Bukkit.getPlayer(playerName);
   }

And if you want to do things like adding a kill to the player, and then teleporting him, you could use your MyPlayer instance to modify your players' attributes, and using the recoverPlayerObject if you need to interact directly with Bukkit Player object.


For more information on Wrapper/Decorator, Iluwatar has a very nice Github repository about Design Patterns, including the Decorator.

Upvotes: 1

Related Questions