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

add item_pre_anvil event #1384

Merged
merged 4 commits into from
Jul 3, 2024
Merged

Conversation

Lildirt
Copy link
Contributor

@Lildirt Lildirt commented Jun 28, 2024

Kind of flying blind with creating a new event. It's been many years since I've written one, in an extension. 😂
Let me know if there's any validation you'd like for me to do on my end.

Example payload (pretty printed):

event_type: item_pre_anvil
first_item: {meta: {damage: 0, display: null, enchants: {silk_touch: {elevel: 1}, unbreaking: {elevel: 3}}, flags: {}, lore: null, model: null, modifiers: null, repair: 0, tags: null, unbreakable: false}, name: DIAMOND_PICKAXE, qty: 1}
item_repair_cost: 10
level_repair_cost: 10
macrotype: inventory
max_repair_cost: 40
result: {meta: {damage: 0, display: null, enchants: {efficiency: {elevel: 4}, silk_touch: {elevel: 1}, unbreaking: {elevel: 3}}, flags: {}, lore: null, model: null, modifiers: null, repair: 1, tags: null, unbreakable: false}, name: DIAMOND_PICKAXE, qty: 1}
second_item: {meta: {damage: 0, display: null, enchants: {efficiency: {elevel: 4}, unbreaking: {elevel: 3}}, flags: {}, lore: null, model: null, modifiers: null, repair: 0, tags: null, unbreakable: false}, name: DIAMOND_PICKAXE, qty: 1}
viewers: {Lildirt}

Copy link
Contributor

@Pieter12345 Pieter12345 left a comment

Choose a reason for hiding this comment

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

Quick review without having compiled or executed the code. It looks like you were successful at sticking to the project structure! Thanks for contributing :)


@Override
public int getRepairCostAmount() {
return ai.getRepairCost();
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that this should call getRepairCostAmount() instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed it should! Updated.

import java.util.List;

public interface MCPrepareAnvilEvent extends MCInventoryEvent {
@Override
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this override doing here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uh, nothing. Actually, that entire getInventory() definition doesn't need to exist. It's defined in MCInventoryEvent. Removed it.

public String docs() {
return "{}"
+ " Fires when a recipe is formed in an anvil, but the result has not yet been clicked."
+ " { viewers: all human entities looking at this anvil "
Copy link
Contributor

Choose a reason for hiding this comment

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

I would word this as all players viewing this anvil's interface or something similar that is a bit more explicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Verbage updated.

+ " { viewers: all human entities looking at this anvil "
+ " | first_item: the first item in the anvil"
+ " | second_item: the second item in the anvil"
+ " | result: the result of the anvil"
Copy link
Contributor

Choose a reason for hiding this comment

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

I would word this as:
the first ingredient in the anvil recipy
the second ingredient in the anvil recipy
the result of the anvil recipy
Or something alike. Again a bit more explicit, leaving less room for misinterpretation (what is the result of an anvil?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Verbage updated.


@Override
public Map<String, Mixed> evaluate(BindableEvent event) throws EventException {
if(event instanceof MCPrepareAnvilEvent) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Java now has a nice language feature that you can use here.

if(event instanceof MCPrepareAnvilEvent) {
    MCPrepareAnvilEvent e = (MCPrepareAnvilEvent) event;

is equivalent to:

if(event instanceof MCPrepareAnvilEvent e) {

It's up to you to decide whether or not you want to use that, but it's been used more and more in CH.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! I've updated it to use the short-hand.

@PseudoKnight
Copy link
Contributor

Looks like you matched 'viewers' from item_pre_craft, whereas we should probably get a player for both of these from the InventoryView. I don't think there can be multiple viewers, at least for these events on these GUIs. I would recommend removing 'viewers' and add 'player' for this event.

There's one hitch. InventoryView was changed from an abstract class to an interface a couple of weeks ago. Directly using methods from that interface may not work on older versions of the server. Might need reflection. Something like:

	@Override
	public MCHumanEntity getPlayer() {
		return new BukkitMCHumanEntity(ReflectionUtils.invokeMethod(e.getView(), "getPlayer"));
	}

@PseudoKnight
Copy link
Contributor

PseudoKnight commented Jun 29, 2024

Let me test the other event to see if reflection is needed here too. [edit] Confirmed. Needs reflection.

@PseudoKnight
Copy link
Contributor

PseudoKnight commented Jun 29, 2024

Actually can probably just do e.getViewers.get(0) instead of reflection. That's probably the way to go.

@Lildirt
Copy link
Contributor Author

Lildirt commented Jun 30, 2024

Actually can probably just do e.getViewers.get(0) instead of reflection. That's probably the way to go.

Pushed a commit. Something like that?

@PseudoKnight
Copy link
Contributor

That looks fine. It looks impossible to have more than one viewer for an anvil. Unlike shared inventory views, the list of tracked viewers of an anvil is created every time a call to open an anvil is used.

@PseudoKnight
Copy link
Contributor

Just noticed you have question marks in the description of the repair costs. Perhaps they were temporary?

@PseudoKnight PseudoKnight merged commit dd79361 into EngineHub:master Jul 3, 2024
2 of 4 checks passed
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.

3 participants