Jump to content

[1.10.2] prevent specified Player to craft some items?


MSandro

Recommended Posts

Hi, and heave a nice day!

I want to make a mod with many abilities. One part of it is to handle tasks or checkpoints to trigger "ages". So I want to block some crafting recipes until a pleyer reach the needed "level". Remove a recipe server-wide is easy but I don't know how to remove recipes only for players corectly.

 

I tried a very primitiv work-around but it has several problems:

@SubscribeEvent
public void onCraft(ItemCraftedEvent event) {
	if (event.crafting.getItem().getRegistryName().toString().contains("minecraft:wooden_sword")) {
		event.player.inventory.clearMatchingItems(Item.getByNameOrId("minecraft:wooden_sword"), -1, 1, null);
	}
}

@SubscribeEvent
public void pickUp(EntityItemPickupEvent event) {
	if (event.getEntity() instanceof EntityPlayer) {
		if (event.getItem().getEntityItem().getItem().getRegistryName().toString().contains("minecraft:wooden_sword")) {
			event.setCanceled(true);
		}
	}
}

The first part is removing the output from the players inventory. But the player is loosing the ingridients.
The second part destroying the items on pickup, because the player can pres "Q" in the workbench to drop the item.

 

Is it posible to prevent the player from taking the result out of the workbench? The ingridients should stay in the bench. I tried to cancel the event, but then the client crashes.  Also is there a event for "event.getEntityPlayer().inventory.inventoryChanged"? I want to drop the item when a player gets a forbidden item.

 

 

Thank you very much for your help.

Edited by MSandro
Link to comment
Share on other sites

First of all, to get the wooden sword item, see the Items class (net.minecraft.init.Items). It contains a list of all the items in vanilla. To test if an item is a wooden sword:

if (item == Items.WOODEN_SWORD)

To clear the player's inventory of wooden swords:

player.inventory.clearMatchingItems(Items.WOODEN_SWORD, -1, 1, null);

 

As for player-sensitive recipes, consider making a custom IRecipe implementation. Diesieben07 made a tutorial on how to do this.

catch(Exception e)

{

 

}

Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).

Link to comment
Share on other sites

Thank you very much.

Yes I know about the item-comparison but later I want to use a config file to check the items, so I must use strings because it is the simpliest way to interact with the config.

I will take a look to your tutorial, later.

Link to comment
Share on other sites

Read values from the configuration, get the Item instances from those values. Store your own references to those instances and use those instead. Calling Item.getByNameOrId is slow. 

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.

Link to comment
Share on other sites

2 minutes ago, Draco18s said:

Read values from the configuration, get the Item instances from those values. Store your own references to those instances and use those instead. Calling Item.getByNameOrId is slow. 

Thanks for the tipp.

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.