Jump to content

[Solved] How to cancel PlayerEvent.ItemCraftedEvent?


Recommended Posts

Posted

You can call Event#setCancelled on cancellable events.

IIRC you should not be cancelling this event though because it results in the items in the crafting matrix being consumed but the item not being given to the player. What are you trying to achieve, from an end-user’s (a player) perspective?

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted
1 minute ago, Cadiboo said:

You can call Event#setCancelled on cancellable events.

IIRC you should not be cancelling this event though because it results in the items in the crafting matrix being consumed but the item not being given to the player. What are you trying to achieve, from an end-user’s (a player) perspective?

The player has todo something first before they can craft the item. I tried making a custom condition, but I can't seem how todo that because what the docs says isn't working for me 

Github: https://github.com/DJ1TJOO/fantasy20/

Posted
Just now, DJ1TJOO said:

I tried making a custom condition

This is what you’re meant to do. What is the exact issue that you’re having? Forge provides custom conditions too so even if you’re having trouble with the docs, you can look at Forge’s code to see how it works

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted
5 minutes ago, Cadiboo said:

This is what you’re meant to do. What is the exact issue that you’re having? Forge provides custom conditions too so even if you’re having trouble with the docs, you can look at Forge’s code to see how it works

I read the ItemExsistsCondition.class and it has a constructor with a variable and in the docs they say that the constructor must be empty so I don't understand what I need todo

Posted

What is the condition that you’re trying to implement meant to do?

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted
Just now, Cadiboo said:

What is the condition that you’re trying to implement meant to do?

I have a capability on each player with the Items they have "learned" if the item is not in the capability list then the player cannot craft it

Posted

Ok so you would want a condition called something like “has_learned_item”. I’m not sure if you get a player object in the conditions, let me check

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted
2 minutes ago, Cadiboo said:

Ok so you would want a condition called something like “has_learned_item”. I’m not sure if you get a player object in the conditions, let me check

Yes I called it researched because the way you do it is in the research table

Posted
33 minutes ago, Cadiboo said:

Ok so you would want a condition called something like “has_learned_item”. I’m not sure if you get a player object in the conditions, let me check

Al ready have an idea?

Posted
2 hours ago, diesieben07 said:

You already asked about recipe conditions here:

 

However they won't help you make player-specific conditions. For that you need a custom recipe type (check IRecipeSerializer) and then check the player inside the recipe implementation.

I tried to make it but I think I need to register it somewhere. I've updated the github and the doorlock recipe has my type (for now just the copied normal shapedrecipe) as recipe. 

The error I'm getting when joining a world is:
com.google.gson.JsonSyntaxException: Invalid or unsupported recipe type 'fantasy20:shaped_researched_recipe'
 

Posted

...You do know that there's a game rule that already covers this, right? If you don't have the recipe in your recipe book, you can't craft it when the rule is enabled.

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
9 hours ago, diesieben07 said:

IRecipeSerializer is a registry entry, just like blocks or items.

It works now but how would I get the player in my IRecipe because the CraftingInventory doesn't hold the owner of the inv. And its all server side so I can't use the Minecraft.getInstance()

Posted (edited)
20 minutes ago, diesieben07 said:

CraftingInventory has a reference to the Container (field_70465_c, you'll need reflection).

You can then look through Container#listeners which should contain the player using this inventory.

 

@SuppressWarnings("rawtypes")
		Class invClass = inv.getClass();
		try {
			Field field = invClass.getField("field_70465_c");
			Container container = (Container) field.get(this);
			@SuppressWarnings("rawtypes")
			Class containerClass = container.getClass();
			@SuppressWarnings("unchecked")
			List<IContainerListener> listeners = (List<IContainerListener>) containerClass.getField("listeners").get(this);
			for (IContainerListener iContainerListener : listeners) {
				iContainerListener.
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

I have this now but I don't see how I can get the player from IContainerListener.
Edit: Oh I can cast it to a serverplayer 
Changed this -> inv

Edited by DJ1TJOO
Posted
2 minutes ago, diesieben07 said:

ServerPlayerEntity implements IContainerListener. You do an instanceof check.

 

Also, why are you using raw types?

How would I do it else then I've just searched reflection on the internet it's new for me

Posted
22 minutes ago, diesieben07 said:

Please learn about Java generics. This is not a Java school.

I've this code now and I get the error that the field field_70465_c does not exist
 

try {
			Class<? extends CraftingInventory> invClass = inv.getClass();
			Field field = invClass.getField("field_70465_c");
			Container container = (Container) field.get(inv);
			Class<? extends Container> containerClass = container.getClass();
			Field field2 = containerClass.getField("listeners");
			@SuppressWarnings("unchecked")
			List<IContainerListener> listeners = (List<IContainerListener>) field2.get(inv);
			for (IContainerListener iContainerListener : listeners) {
				if(iContainerListener instanceof ServerPlayerEntity) {
					ServerPlayerEntity p = (ServerPlayerEntity) iContainerListener;
					if(!p.getCapability(CapabilityResearchProvider.RESEARCH_CAPABILITY, p.getHorizontalFacing()).map(r -> {
						if(!r.getResearched().contains(getCraftingResult(inv).getItem())) {
							return false;
						}
						return true;
					}).orElse(Boolean.FALSE)) {
						return false;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

 

Posted

You can use a single line of code with ObfuscationReflectionHelper. You can also use an AT (Access Transformer)

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.