-
Type:
Bug
-
Resolution: Cannot Reproduce
-
Priority:
Minor
-
git-Spigot-379750e-9a9c223
-
Yes
Code idea:
The code below intercepts the player Right Click and checks if they are holding a specifically named Name Tag. If so, it then converts the entity between baby and adult.
The event is cancelled so that the entity is not renamed and the Name Tag is not spent.
The issue:
I've been told on Discord that Ageable#setBaby and Ageable#setAdult shouldn't need Packets to notify other clients of the change, and was told to open this bug report.
If I leave the Packet (at the end of the code) commented, nearby players (other than the casting one) don't get the entity age change – they must either re-log or move away and back to see the changes to the entity.
Code:
public class TestListener implements Listener { /** * Listens to player right-click interaction and checks if the target entity is renamed to "ces_baby/adult". * TO DO * @param event Player interaction event. * **/ @EventHandler public void onPlayerInteractWithEntity(PlayerInteractEntityEvent event) { EntityUtil entityUtil = new EntityUtil(); Player sourcePlayer = event.getPlayer(); ItemStack mainHandItem = sourcePlayer.getEquipment() != null ? sourcePlayer.getEquipment().getItemInMainHand() : null; // Checks if player is holding a name tag named "ces_baby/adult" if( mainHandItem != null && entityUtil.PlayerHoldsValidNameTag( mainHandItem ) ) { // Player holds a named name tag. Must cancel the default Right-Click event event.setCancelled(true); Entity targetEntity = event.getRightClicked(); // Is entity valid for conversion? if ( entityUtil.IsAgeable(targetEntity) ) { // Valid entity. Convert to adult or baby? if ( !((Ageable) targetEntity).isAdult() ) { // ADULT // Checks if it's already been converted if( ((Breedable) targetEntity).getAgeLock() ) { // AgeLock is true, convert it back to adult ((Breedable) targetEntity).setAgeLock(false); ((Ageable) targetEntity).setAdult(); } } else { // BABY // Attempts to convert entity into a Baby ((Breedable) targetEntity).setAgeLock(true); ((Ageable) targetEntity).setBaby(); if( ((Ageable) targetEntity).isAdult() ){ // Failed. Can't be a baby ((Breedable) targetEntity).setAgeLock(false); } } } // DataWatcher watcher = ((CraftAgeable) targetEntity).getHandle().getDataWatcher(); // // for (Player player : Bukkit.getOnlinePlayers()) { // PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata( // targetEntity.getEntityId(), // Entity ID // watcher, // Data watcher which you can get by accessing a method in a NMS Entity class // false // Send All // ); // // ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); // sourcePlayer.sendRawMessage("SENT: " + player.getName() ); // } } } }
Thanks in advance!