Carpet

Carpet

2M Downloads

Suggestion: More event handler functions (hunger, xp, item transfer)

James103 opened this issue ยท 6 comments

commented

I would like if the following event handler suggestions were implemented:

  • __on_player_gains_xp(player, xp_amount) (Called when player gains XP points)
  • __on_player_loses_xp(player, level_amount) (Called when player loses XP levels)
  • __on_player_item_transfer(player, item_tuple, source, destination) (Called when items are transferred between player's inventory and a container or vice versa, the source and destination can be either entity or block) (not feasible)
  • __on_player_equips_item(player, item_tuple, slot) (Called when player equips an armor piece or other item in any of the armor slots (head, chest, legs, feet)) (not feasible)
  • __on_player_loses_hunger(player, amount, reason) (Called when player gains exhaustion points or loses food points for any reason, amount is specified as fractional food points (+0.4 exhaustion returns 0.1)) (not needed, already possible with existing events)
commented

I'm closing this as it could be done using existing methods in Scarpet.

commented

The latter two can be generalized to some sort of inventory event. Will check how this can be effectively monitored.

commented

Would __on_player_item_transfer(player, item_tuple, source_inventory, source_slot, destination_inventory, destination_slot) be a good replacement? It would be called when items are transferred by a player from one inventory to another, or to a different slot within the same inventory (including armor slots).

commented

The first can be done already:

global_hungers=m();
global_change_hunger=m();
__on_tick()->(
    for(players('*'),
        h=_~'nbt':'Hunger';
        if(global_hungers:_!=h,
            cause='';
            if(player~'sprinting',cause='sprinting');
            if(player~'health'!=player~'maxhealth',cause='healing');//bla bla bla
            global_change_hunger:_=l(_,h-global_hungers:_,cause);
            global_hungers:_=h,
            global_change_hunger:_=l(null,0,'')
        )
    )
)

Then you just import the map global_change_hunger to your app, and say:

import('hunger_change_listener','global_change_hunger');
__on_tick()->(
    for(players('*'),
        if(global_change_hunger:_,
            l(player, hunger_change, cause)=global_change_hunger:_;
            do_stuff
        )
    )
)
commented

same can be done for xp

commented

And the last two wont work anyways cos if you don't shift click the items, there will be a delay of n ticks, cos you don't know if youre gonna pick up the item, or not, or if youre simply gonna chuck it outta your inventory.