Jump to content

[1.12.2] ItemCraftedEvent


RoyalReject

Recommended Posts

Here is my class:

public class Test {

	ArrayList<Item> items = new ArrayList<Item>();
	
	@SubscribeEvent
	public void onCraft(ItemCraftedEvent event) {	
		items.add(Item.getByNameOrId("minecraft:wooden_button"));
		if(!event.player.world.isRemote) {
			event.player.sendMessage(new TextComponentString("Test1"));
			if(isItemIn(event.crafting.getItem())) {
				event.player.sendMessage(new TextComponentString("Patented Item"));
				event.setCanceled(true);
			}
		}
	}
	
	public boolean isItemIn(Item item) {
		for(int i = 0; i < items.size(); i++) {
			if(items.get(i) == item) {
				return true;
			}
		}
		return false;
	}
}

I am trying to stop the crafting of an item in an arraylist, while it does cancel the event it still gives the crafting result while not using the wood leaving it in the crafting bench

Link to comment
Share on other sites

1. To be honest, your code is a mess.

- Use List#contains. There is no need to iterate through the list.

- You are adding the button item every crafting event, which makes no sense. If you insist to look up items based on a list, initialize the array with the desired objects.

 

2. This is not how the ItemCraftedEvent is meant to be used. Why don’t you just disable the recipe instead?

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

2 minutes ago, DavidM said:

1. To be honest, your code is a mess.

- Use List#contains. There is no need to iterate through the list.

- You are adding the button item every crafting event, which makes no sense. If you insist to look up items based on a list, initialize the array with the desired objects.

  • This is for testing later will add to list in a different way.

 

2. Why don’t you just disable the recipe instead?

  • because I dont wanna disable the recipe for all players just a few this is currently getting base logic down

 

Edited by RoyalReject
Link to comment
Share on other sites

1. Ok... That is some wild testing then.

2. AFAIK the ItemCraftedEvent happens after the event is crafted, thus canceling the event will not cancel the craft.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

1 minute ago, DavidM said:

1. Ok... That is some wild testing then.

2. The ItemCraftedEvent happens after the event is crafted AFAIK, thus canceling the event will not cancel the craft.

one yes its weird testing but it works. and b that was my thought but it still gives me the button but does not take the wood.

Link to comment
Share on other sites

36 minutes ago, RoyalReject said:

it works

1. Aspects like performance, speed and coding style also counts toward the quality of the code.

I would strongly suggest you to change your code once you've done testing.

 

2. Canceling the event is not the solution you wanted. As LexManos stated here: https://github.com/MinecraftForge/MinecraftForge/issues/3780#issuecomment-286099327, the ItemCraftedEvent is designed for stuff like posting achievements. You should not try to edit the craft inside the event handler of this event.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

2 minutes ago, DavidM said:

1. Aspects like performance, speed and coding style also counts toward the quality of the code.

I would strongly suggest you to change your code once you've done testing.

 

2. Canceling the event is not the solution you wanted.

Then how would i stop the player from crafting the items. I cannot think of anything else

Link to comment
Share on other sites

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

Well, there are many approaches.

The easiest (probably) would be to create a

Map<Recipe, Player> // Pseudocode, adjust the types based on your code

, and set the player value to null at the beginning.

Whenever a player claims a recipe, set the value of the recipe to that player (use the player's name or something) (make the recipe claimable only if the corresponding value is null, meaning no one had claimed that recipe).

When a player tries to craft something, check if that player is the value corresponding to the recipe.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

46 minutes ago, diesieben07 said:

ItemCraftedEvent cannot be used to cancel a recipe. In fact, there is no event which allows it.

One way is the topic linked above.

 

However there are also the new RecipeBook mechanic, which makes it so recipes are locked by default (and allows locking and unlocking recipes per player).

I just messed around with trying to lock Recipes through the book but i am still able to craft locked recipes

Link to comment
Share on other sites

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.