Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix drop boss bag hook not working properly #131

Open
wants to merge 7 commits into
base: upcoming
Choose a base branch
from

Conversation

sors89
Copy link

@sors89 sors89 commented Jun 19, 2024

@sors89
Copy link
Author

sors89 commented Jun 19, 2024

gonna make this pull request draft as i encoutered some errors with HookResult while testing

@sors89 sors89 marked this pull request as draft June 19, 2024 15:09
@sors89 sors89 marked this pull request as ready for review June 21, 2024 11:56
Copy link
Owner

@SignatureBeef SignatureBeef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR and apologies for delays, usually don't get time to work on this project.

Have a question in relation to the new cancel code - see review comment.

Typically with OTAPI hooks, "HookResult.Cancel" would not run the event/action, so i currently feel this may need extra adjustments or we consider that changing to DropItemLocalPerClientAndSetNPCMoneyTo0 warrants a relook at how the event occurs.

Few thoughts on the direction, pending additional clarification wrt review comment:

  • If we want to still use return -1; some additional IL could be injected to insert a if(num == -1) return;. This will then be able to prevent new items being created, and packets being sent.
  • maybe less ideal - change focus from the NewItem call, to around DropItemLocalPerClientAndSetNPCMoneyTo0 instead, and if cancelled, the consumer is responsible for distributing loot (customised, or not).
    e.g. patch instead of modify (not monomod runtime hook), or via the new static hooks (WIP, may be expanded to full coverage)

Comment on lines 112 to 118
{
#if TerrariaServer_EntitySourcesActive || Terraria_EntitySourcesActive || tModLoader_EntitySourcesActive
return Terraria.Item.NewItem(Source, args.X, args.Y, args.Width, args.Height, args.Type, args.Stack, args.NoBroadcast, args.Pfix, args.NoGrabDelay, args.ReverseLookup);
#else
return Terraria.Item.NewItem(args.X, args.Y, args.Width, args.Height, args.Type, args.Stack, args.NoBroadcast, args.Pfix, args.NoGrabDelay, args.ReverseLookup);
#endif
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i may not be up to scratch with these hooks nowadays, but if it was cancelled, should a new item be created still? And if so, why?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to how "HookResult.Cancel" tend to do, we should not let the new item be created, and i completely agree with that. Sorry, it was because of my lack of knowledge that i overlooked it.

@sors89
Copy link
Author

sors89 commented Dec 28, 2024

Thanks for your reply. Don't worry, the delays were not bothered me too much because i had more time to improve my IL editing skill. At the time i submitted this pr, i don't have much experience in this field but somehow the code worked and here we are lol.

  • If we want to still use return -1; some additional IL could be injected to insert a if(num == -1) return;. This will then be able to prevent new items being created, and packets being sent.

Ah, this is a good improvement and easy to implement too!

  • maybe less ideal - change focus from the NewItem call, to around DropItemLocalPerClientAndSetNPCMoneyTo0 instead, and if cancelled, the consumer is responsible for distributing loot (customised, or not).
    e.g. patch instead of modify (not monomod runtime hook), or via the new static hooks (WIP, may be expanded to full coverage)

A great idea to me. I personally choose this one. Some example code for the function you mentioned would help a lot! And i need some clarifications about the final code to check if i get the idea right:

public static void DropItemLocalPerClientAndSetNPCMoneyTo0(NPC npc, int itemId, int stack, bool interactionRequired = true)
{
      if (HookResult.Cancel)
      {
            CustomWayOfDistributingLoot(npc, itemId, stack, true);                 
      }
      else
      {
            //vanilla code goes here...
      }
}

@sors89
Copy link
Author

sors89 commented Dec 31, 2024

i'm happy with it

@sors89 sors89 requested a review from SignatureBeef January 4, 2025 03:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants