Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

  • Author
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

  • Author
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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.