Posted January 27, 20214 yr 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? Edited January 27, 20214 yr by Luis_ST
January 27, 20214 yr Author 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); }
January 27, 20214 yr Author 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
January 27, 20214 yr Author 7 minutes ago, diesieben07 said: Not sure, that should not be happening. From the code you posted I can't tell. if there is no way to prevent it then i try to create my own method that enchants the item
January 27, 20214 yr Author 3 hours ago, diesieben07 said: There probably is something wrong - please post a Git repo so I can use the debugger. https://github.com/Luis-st/Forge-1.16.5-36.0.1-mdk Edited January 27, 20214 yr by Luis_ST
January 27, 20214 yr Author 16 minutes ago, diesieben07 said: Yeah, looking at this, this is normal behavior. Even vanilla villagers should have the same effect, they use the same code. so there is no error
January 28, 20214 yr Author 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:?] {}
January 28, 20214 yr Author 20 minutes ago, diesieben07 said: You can't remove from a list while you are iterating over it. If you want to do that, you need to remove using the Iterator#remove method. so i have to convert the list to an iterator?
January 28, 20214 yr Author 15 minutes ago, diesieben07 said: You have to do manual iteration instead of just an enhanced for loop, yes. but how to do that
January 28, 20214 yr Author 18 minutes ago, diesieben07 said: https://www.geeksforgeeks.org/how-to-use-iterator-in-java/ 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?
January 28, 20214 yr Author 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(); } }
January 28, 20214 yr Author i currently fixed the error so it works thanks Edited January 28, 20214 yr by Luis_ST
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.