Solved [1.7.10/1.7.10-1.14] Creating a fake player to modify an offline player

Discussion in 'Plugin Development' started by PintsizedSix40, Aug 7, 2019.

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

    PintsizedSix40

    Hello! I am attempting to create a plugin that will allow (among other things) offline players to run commands. To do this, I have attempted to create a fake player by creating a new EntityPlayer based off of an offline player's UUID and display name. Next, I cast it to a Player object, run the command, and kick the player. I feel as though I am doing something wrong here since when I cast my EntityPlayer to a Player, I receive an error stating that an EntityPlayer cannot be cast to a Player object. I am looking to a solution to this problem, that if possible, would not alter other plugins of the player joining, as well as being independent of the Minecraft version (although if it were to be using EntityPlayer, I'm sure a check to see if certain packages exist and use an appropriate EntityPlayer). Also, here is my current code:
    Code:
                    OfflinePlayer p = Util.UUIDtoOfflinePlayer(uuid);
                    EntityPlayer play = new EntityPlayer(MinecraftServer.getServer(), ((CraftWorld)Bukkit.getServer().getWorlds().get(0)).getHandle(), new GameProfile(p.getUniqueId(), p.getName()), new PlayerInteractManager(((CraftWorld) Bukkit.getServer().getWorlds().get(0)).getHandle()));
                    System.out.println(play);
                    Player pl = ((Player)play);
                    pl.loadData();
                    pl.teleport(Bukkit.getServer().getWorlds().get(0).getSpawnLocation());
                    pl.saveData();
                    pl.kickPlayer("");
    I have tested to see what the values for p's UUID and DisplayName and they are correct, but if it is helpful, Util#UUIDtoOfflinePlayer is
    Code:
    public static OfflinePlayer UUIDtoOfflinePlayer(String uuid) {
            return Bukkit.getOfflinePlayer(UUID.fromString(uuid));
        }
     
  2. Online

    timtower Moderator Moderator

    @PintsizedSix40 Why not run the commands when the player gets back online?
     
  3. Offline

    PintsizedSix40

    Hmm, I forgot about that one. Is there any way to get a player object before a player is loaded into the world? Say the player (or server) crashes right after they join. Would it be possible to grab the Player before anything chunks are loaded in so I can modify certain values or run commands as them?
     
  4. Online

    timtower Moderator Moderator

    @PintsizedSix40 Not that I know off.
    Why do you need to run commands as the player without letting the player know about it?
     
  5. Offline

    Kars

    @PintsizedSix40 every @EventHandler method gets fired before the event, so player join events catch the player before they join and start loading chunks.
     
  6. Offline

    PintsizedSix40

    @Kars Alright, I'll see about using player join.
    @timtower They would be making the command run themselves, just not through the mc chat. I think it would just be easier to implement using different plugin's APIs instead of using a raw command, which should work better.
     
  7. Online

    timtower Moderator Moderator

    @PintsizedSix40 Would be more work to implement into API's.
    Raw commands should work fine.
     
  8. Offline

    PintsizedSix40

    I'm fine with implementing APIs so I can get a clean response and receive that response immediately, rather than when the player joins. I was though able to use the player join event to run commands/modify the player, and I think that works fine. I'll mark this thread as solved, thanks for the help!
     
Thread Status:
Not open for further replies.

Share This Page