Jump to content

[1.16.5] Enchantments can Apply to all Tools


Luis_ST

Recommended Posts

I created some enchantments in my mod. Then I checked whether my enchantments could be traded by my custom villager (profession) and found that there were some enchantments traded with incompactable enchantments,

or several enchantments with the same level on a item. When I try to enchant the item with the enchant command, the normal vanilla error message comes up.

But if I combine enchantment in the Amposs with a item that dosent sopport the enchantment it works with all Vanilla Enchantments,

for example: i can enchant protection on a sword. Now my question is this a bug or is it because of the test version of Minecraft which use the IDE?

 

2021-01-27_08_20_01.thumb.png.5849dfc66d06d3de2064faf632360aac.png

Edited by Luis_ST
Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Show how you are creating your enchanted book itemstacks.

first the problem with two enchantments on the same tool:

	public static BasicTrade creatTradeEmeraldForEnchantedItem(Item item, int level, boolean allowTreasure, int tradeLevel) {
		
		int count = rng.nextInt(level + level) + level;		
		ItemStack stack = new ItemStack(item);
		EnchantmentHelper.addRandomEnchantment(rng, stack, level, allowTreasure);
		return new BasicTrade(new ItemStack(Items.EMERALD, count), ItemStack.EMPTY, stack, 16, randomXp(tradeLevel), 0.2f);
		
	}

 

second why i can add the protection Enchantment to a Sword?

 

and third i can trade at the villager with the profession "enchanter" enchanted books with ench level 0

this is the way i creat books

	
// i creat a list of Trades because when i use one trade i got every time the same Enchantment (it will reset when i start minecraft again
public static List<BasicTrade> creatTradeEmeraldForEnchantedBookList(int size, int tradeLevel) {
		
		List<BasicTrade> trades = new ArrayList<>();
		for (int i = 0; i < size; i++) {
			trades.add(enchantedBook(randomXp(tradeLevel)));
		}
		
		return trades;
		
	}
                                                      
    	
private static BasicTrade enchantedBook(int xp) {
		
		ItemStack book = new ItemStack(Items.ENCHANTED_BOOK);
		List<Enchantment> enchantments = Registry.ENCHANTMENT.stream().filter((enchantment) -> {
			return !enchantment.isCurse();
		}).collect(Collectors.toList());
		Enchantment enchantment = enchantments.get(rng.nextInt(enchantments.size()));
		int enchLevel = Math.min(enchantment.getMaxLevel(), rng.nextInt(5));
		EnchantedBookItem.addEnchantment(book, new EnchantmentData(enchantment, enchLevel));
		int count = 2 + rng.nextInt(5 + enchLevel * 10) + 3 * enchLevel + 5;
		
		return new BasicTrade(new ItemStack(Items.EMERALD, count > 64 ? 64 : count), new ItemStack(Items.BOOK), book, 16, xp, 0.2f);
		
	}                                                  

 

Link to comment
Share on other sites

9 minutes ago, diesieben07 said:

Look at addRandomEnchantment / buildEnchantmentList. It does not necessarily only add one enchantment.

i know i just look at this methods but why i can get the an enchantment twice with the same level?

and how do I prevent that?

 

12 minutes ago, diesieben07 said:

Because you are in creative mode.

okay thats clear

 

12 minutes ago, diesieben07 said:

int enchLevel = Math.min(enchantment.getMaxLevel(), rng.nextInt(5));

okay that's a mistake on my part -> int enchLevel = Math.min(enchantment.getMaxLevel(), rng.nextInt(5) + 1); will fix that

Link to comment
Share on other sites

i tested the minecraft code a bit and vanilla trades work fine. there are no enchantment twice?

so i creat my custom addRandomEnchantment and it works fine but: when i remove incompatible enchantments i got an error:

	private static Map<Enchantment, Integer> removeIncompatible(Map<Enchantment, Integer> enchantments) {
		
		Map<Enchantment, Integer> returnMap = new HashMap<Enchantment, Integer>();
		List<Enchantment> enchantmentList = enchantments.keySet().stream().collect(Collectors.toList());
		List<Integer> levelList = enchantments.values().stream().collect(Collectors.toList());
		
		for (int i = 0; i < enchantmentList.size(); i++) {
			
			for (Enchantment enchantment : enchantmentList) {
				
				if (!enchantment.isCompatibleWith(enchantmentList.get(i))) {
					
					enchantmentList.remove(i);
					levelList.remove(i);
					
				}
				
			}
			
		}
		
		for (int i = 0; i < enchantmentList.size(); i++) {
			
			returnMap.put(enchantmentList.get(i), levelList.get(i));
			
		}
		
		return returnMap;
		
	}

 

and the error:

java.util.ConcurrentModificationException: null
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1013) ~[?:?] {}
	at java.util.ArrayList$Itr.next(ArrayList.java:967) ~[?:?] {}
	at net.luis.cave.lib.EnchantmentManager.removeIncompatible(EnchantmentManager.java:144) ~[main/:?] {re:classloading}
	at net.luis.cave.lib.EnchantmentManager.addRandomEnchantment(EnchantmentManager.java:116) ~[main/:?] {re:classloading}
	at net.luis.cave.events.entity.player.interact.OnRightClickBlockEvent.PlayerInteract(OnRightClickBlockEvent.java:34) ~[main/:?] {re:classloading}
	at net.minecraftforge.eventbus.ASMEventHandler_24_OnRightClickBlockEvent_PlayerInteract_RightClickBlock.invoke(.dynamic) ~[?:?] {}
	at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.common.ForgeHooks.onRightClickBlock(ForgeHooks.java:803) ~[forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading}
	at net.minecraft.client.multiplayer.PlayerController.func_217292_a(PlayerController.java:286) ~[forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1407) ~[forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.processKeyBinds(Minecraft.java:1692) ~[forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.runTick(Minecraft.java:1507) ~[forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:979) ~[forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.run(Minecraft.java:612) ~[forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.main.Main.main(Main.java:184) ~[forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[?:?] {}
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}
	at java.lang.reflect.Method.invoke(Method.java:564) ~[?:?] {}
	at net.minecraftforge.userdev.FMLUserdevClientLaunchProvider.lambda$launchService$0(FMLUserdevClientLaunchProvider.java:52) ~[forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.0.9.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.0.9.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.0.9.jar:?] {}
	at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.0.9.jar:?] {}
	at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.0.9.jar:?] {}
	at net.minecraftforge.userdev.LaunchTesting.main(LaunchTesting.java:105) [forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {}

 

Link to comment
Share on other sites

18 minutes ago, diesieben07 said:
		while (enchantmentIterator.hasNext()) {
			
			Enchantment enchantment = (Enchantment) enchantmentIterator.next();
			
			if (!enchantment.isCompatibleWith((Enchantment) enchantmentIterator)) {
				
				enchantmentIterator.remove();
				
			}
			
		}

so is this correct?

and how to remove now the level of the enchantment?

Link to comment
Share on other sites

2 minutes ago, diesieben07 said:

By calling remove on the iterator you remove the current element from the collection you are iterating.

than this

		while (enchantmentIterator.hasNext()) {
			
			Enchantment enchantment = (Enchantment) enchantmentIterator;
			
			if (!enchantment.isCompatibleWith(enchantmentIterator.next())) {
				
				enchantmentIterator.remove();
				
			}
			
		}

 

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.