3

After much research and much time wasted, I still can't find out how to hide an entity to a player.

What I'm trying to do is create a disguise command. I've now gotten everything worked out, except the issue is that the entity is still visible, and once stationary you can't interact with anything because the mob's hitbox is in the way. I want to hide the entity from the player so that you can do this. I know with players you can use Player#hidePlayer(), but this does not work with entities. I've tried using solutions such as this, but it gave an error while following the example. (And many things were depreciated, so I assumed it was out of date. I'm using Spigot 1.11.2). Any help would be very much appreciated.

PS: If you're wondering why I don't just use an already made plugin, it's because none of them work from what I've found.

Twijn
  • 33
  • 1
  • 3
  • That can be made using packages, I would recommend ProtocolLib for that. The link you provided should work as a guide, not a sure-fire – LeoColman Jan 26 '17 at 03:09
  • I've figured it out. Thanks! The thing wrong I was doing was getting the code from the forum thread, which is out of date. The gist link provided in the post luckily has been updated. It's working perfectly now. – Twijn Jan 26 '17 at 04:34
  • Make sure to answer your own question with the answer, making sure that future users can solve it too! (I can do that if you're lazy :P) – LeoColman Jan 26 '17 at 04:35
  • I used ProtocolLib to solve it. – Twijn Feb 12 '17 at 05:34

1 Answers1

3

To accomplish what you want, you must use packets to cancel what the player sees.

I strongly recommend ProtocolLib, have it in your server and use in your plugin.


Bearing that in mind, Bukkit user Comphenix has developed a class for protocollib to hide entities. You can find it in github.

Comphenix also provides an example of usage, as you can see below:

    public class ExampleMod extends JavaPlugin {
    private EntityHider entityHider;

    private static final int TICKS_PER_SECOND = 20;

    @Override
    public void onEnable() {
        entityHider = new EntityHider(this, Policy.BLACKLIST);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            final Player player = (Player) sender;
            final Sheep sheep = player.getWorld().spawn(player.getLocation(), Sheep.class);

            // Show a particular entity
            entityHider.toggleEntity(player, sheep);

            getServer().getScheduler().scheduleSyncDelayedTask(this, new         Runnable() {
                @Override
                public void run() {
                    entityHider.toggleEntity(player, sheep);
                }
            }, 10 * TICKS_PER_SECOND);
        }
        return true;
    }
}
LeoColman
  • 6,950
  • 7
  • 34
  • 63