Difference between revisions of "BKCommonLib/NetworkController"

From BergerHealer Wiki
Jump to navigation Jump to search
 
(No difference)

Latest revision as of 18:19, 9 June 2021

« Go back

Introduction

The EntityNetworkController class provides ways of altering how clients (players) observe an Entity. For example, you can use it to make cows look like pigs to all players, or some other nifty trick. The default implementation of the EntityNetworkController also contains various bugfixes and code optimizations to make your live easier.

Functionalities

  • Alter the spawn packets
  • Alter packets sent to update entity position
  • Keep track of what players can 'see' an Entity
  • Handle player viewers being added or removed

How to use

Just like Entity Controllers, you can assign it to Common Entities. They can only be assigned to entities that have been spawned on a world, mind you. If you want to spawn an Entity with a network controller, use the spawn(location, networkController) method instead. Example code to change the spawn packet:

Entity bukkitEntity = ...;
CommonEntity<?> entity = CommonEntity.get(bukkitEntity);
entity.setNetworkController(new EntityNetworkController<CommonEntity<?>>() {
    @Override
    public CommonPacket getSpawnPacket() {
        return PacketFields.VEHICLE_SPAWN.newInstance(entity.getEntity(), 13);
    }
});

In general, you probably won't be using network controllers. If you do, and wish to alter live entities, you may have to respawn the entity after attaching the network controller for new spawn messages to have effect.