Solved Issue with removing arrows from a player w/ Datawatchers etc

Discussion in 'Spigot Plugin Development' started by MaximusCarlos, Apr 18, 2017.

  1. Code (Text):
        public void onEntityHit(EntityDamageByEntityEvent event){
            if(event.getDamager() instanceof Arrow){
                ((CraftPlayer)event.getEntity()).getHandle().getDataWatcher().set(new DataWatcherObject<>(10, DataWatcherRegistry.b),0);
    I read elsewhere that there's a lack of information about removing arrows and if it's even possible.

    The code is being executed. The arrows are being removed off the player, but each time, the last arrow isn't removed. I uploaded a video to better understand.

    In the video, the previous arrow is being deleted but the arrow being fired isn't getting deleted. I need it to delete every one of them... Without the plugin, the arrows would accumulate

  2. Probable cause is the arrow is being added after the event is called, hence there always being a left-over arrow.

    Two solutions you could try is either a) run the code after a tick or b) find another event that is called after the arrow is added to the entity.
    • Like Like x 1
  3. if you want to control the amount of arrows in any player at all times (for example no arrows, ever), listen for the ENTITY_METADATA (use protocollib) and then use

    Code (Text):

    PacketContainer pc = event.getPacket();
    List<WrappedWatchableObject> entityData = pc.getWatchableCollectionModifier().read(0);
    entityData.add(new WrappedWatchableObject(new WrappedDataWatcher.WrappedDataWatcherObject(
            10, WrappedDataWatcher.Registry.get(Integer.class)), <amount of arrows here (e.g. 40)>

    to specify the amount of arrows in the player. You should obviously also do some checks as to whether the entity is a player etc.
    • Like Like x 1

  4. Both of these methods work, thank you so much!