Jump to content

Recommended Posts

Posted

Ijust use the AnvilUpdateEvent to transfer enchantments from an item to a book all work fine but i cant take the item from the output slot

I just creat this:

 

	@SubscribeEvent
	public static void AnvilUpdateEvent(net.minecraftforge.event.AnvilUpdateEvent event) {
		
		ItemStack inputLeft = event.getLeft();
		ItemStack inputRight = event.getRight();
		
		if (inputLeft != null && inputRight != null) {
			
			if (inputLeft.isEnchanted()) {
				
				Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(inputLeft);
				
				if (inputRight.getItem() instanceof BookItem) {
					
					if (!inputRight.isEnchanted()) {
						
						ItemStack output = new ItemStack(Items.ENCHANTED_BOOK);
						
						for(Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
							
							Enchantment enchantment = entry.getKey();
							output.addEnchantment(enchantment, entry.getValue());
							
						}
						
						
						event.setOutput(output);
						
					}
					
				}
				
			}
			
		}
		
	}

 

 

2020-12-30_13_56_26.thumb.png.6f2b124bc318720df11493a956a9db7a.png

Posted (edited)
19 minutes ago, diesieben07 said:

For enchanted books EnchantedBookItem.addEnchantment must be used instead of ItemStack#addEnchantment.

The Enchanted Book looks now like a "normal" EnchantedBook but i cant still take the item (the Enchanted Book) form the slot

 

	@SubscribeEvent
	public static void AnvilUpdateEvent(net.minecraftforge.event.AnvilUpdateEvent event) {
		
		ItemStack inputLeft = event.getLeft();
		ItemStack inputRight = event.getRight();
		
		if (inputLeft != null && inputRight != null) {
			
			if (inputLeft.isEnchanted()) {
				
				Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(inputLeft);
				
				if (inputRight.getItem() instanceof BookItem) {
					
					if (!inputRight.isEnchanted()) {
						
						ItemStack output = new ItemStack(Items.ENCHANTED_BOOK);
						
						for(Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
							
							Enchantment enchantment = entry.getKey();
							EnchantedBookItem.addEnchantment(output, new EnchantmentData(enchantment, entry.getValue()));
							
						}
						
						event.setOutput(output);
						
					}
					
				}
				
			}
			
		}
		
	}

 

Edited by Luis_ST
add code
Posted (edited)
2 hours ago, diesieben07 said:

You need to set the maximum cost as well.

It works fine but if I try to give the player the item from which he "removes" a random enchantment, the player gets the item 4 times and if I take the unenchanted book from the slot I get a new random enchantment, is there a way to prevent this

 

this is now my code:

	@SubscribeEvent
	public static void AnvilUpdateEvent(net.minecraftforge.event.AnvilUpdateEvent event) {
		
		ItemStack inputLeft = event.getLeft();
		ItemStack inputRight = event.getRight();
		PlayerEntity player = event.getPlayer();
		
		if (inputLeft != null && inputRight != null) {
			
			if (inputLeft.isEnchanted()) {
				
				Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(inputLeft);
				int mapSize = enchantments.size();
				
				if (inputRight.getItem() instanceof BookItem) {
					
					if (!inputRight.isEnchanted()) {
						
						ItemStack output = new ItemStack(Items.ENCHANTED_BOOK);
						
						List<Enchantment> enchantmenList = new ArrayList<Enchantment>(enchantments.keySet());
						int random = new Random().nextInt(enchantmenList.size());
						Enchantment randomEnchantment = enchantmenList.get(random);
						
						List<Integer> levelList = new ArrayList<Integer>(enchantments.values());
						int randomEnchantmentLevel = levelList.get(random);
								
						EnchantedBookItem.addEnchantment(output, new EnchantmentData(randomEnchantment, randomEnchantmentLevel));
						
						event.setCost(1 + mapSize);
						event.setOutput(output);
						
						ItemStack item = inputLeft.copy();
						enchantments.remove(randomEnchantment);
						EnchantmentHelper.setEnchantments(enchantments, item);
						ItemHandlerHelper.giveItemToPlayer(player, item);
						
					}
					
				}
				
			}
			
		}
		
	}

 

Edited by Luis_ST
add code

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.