Creating a Mock/Fake Player Entity

Discussion in 'Spigot Plugin Development' started by WAS, Apr 15, 2017.

  1. WAS

    WAS

    How would one go about creating a fake Player entity with very minimal information to be able to spoof bukkit functions?

    For example. A FakePlayer which contains all the basic information to cast to Player and feed into something like the chat event, or other basic functions that utilize minimal information such as name, displayname, uuid, location, etc. This would need to be done so that it can just be easily instantiated with the information needed such as random UUID, name, etc.

    Is this possible?
     
  2. Not sure if this is exactly what you are looking for, but this is a great tutorial if you are looking for a non-player player xD
     
    • Like Like x 1
  3. WAS

    WAS

    Basically it doesn't need anything to do with packets (I don't think).

    I don't need them in-game or anything. Literally just in order to run functions that need a Player entity passed to it, but you need that information to be unique. UUID, Name, etc. Basically the FakePlayer will be a 3rd party system injecting messages, and other information in the server as a unique "Player" where that player is just a interface casted to a player so the function can run properly when it calls getDisplayName, or getName, or getUniqueId, etc. Take the function AsyncPlayerChatEvent for example:

    Code (Java):
      // This would be done elsewhere, setup by the plugin
       Location loc =  new Location(World, x, y, z, yaw, pitch);
       FakePlayer player = new FakePlayer(UUID.randomUUID(), "Roger", "&7Roger&r", loc);

       // Sending as a unique, but fake player to be reused
       AsyncPlayerChatEvent event = new AsyncPlayerChatEvent(true, (Player)player, msg, recipients);
       Bukkit.getPluginManager().callEvent(event);
    Alternatively can I take a already active Player instance and clone it, and change the UUID, name, display name, and location? To sort of use an existing player as a mock player. Since the only issue I have creating a FakePlayer interface is the fact I can't instantiate it with data I need as it is an abstract implementation of Player.
     
    #3 WAS, Apr 15, 2017
    Last edited: Apr 15, 2017