From ab3d6f87dc99f61e934283ac2d994899e98eab70 Mon Sep 17 00:00:00 2001 From: TheDarkDnKTv Date: Sun, 25 Apr 2021 23:20:17 +0300 Subject: [PATCH] Fixed enchant system Now you can allow any enchants to be set to exact GT tool in Others config --- src/main/java/gregtechmod/GT_Mod.java | 8 +--- .../gregtechmod/api/items/GT_Tool_Item.java | 45 +++++++++++++------ .../loaders/preload/GT_ItemLoader.java | 13 +++++- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/main/java/gregtechmod/GT_Mod.java b/src/main/java/gregtechmod/GT_Mod.java index 1210358..cc6743b 100644 --- a/src/main/java/gregtechmod/GT_Mod.java +++ b/src/main/java/gregtechmod/GT_Mod.java @@ -10,7 +10,6 @@ import gregtechmod.api.enums.Materials; import gregtechmod.api.enums.OrePrefixes; import gregtechmod.api.interfaces.IGT_Mod; import gregtechmod.api.interfaces.IMetaTileEntity; -import gregtechmod.api.items.GT_Tool_Item; import gregtechmod.api.metatileentity.BaseMetaPipeEntity; import gregtechmod.api.metatileentity.BaseMetaTileEntity; import gregtechmod.api.metatileentity.MetaPipeEntity; @@ -99,7 +98,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; -import java.util.stream.Collectors; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; @@ -271,6 +269,8 @@ public class GT_Mod implements IGT_Mod { GregTech_API.sSpecialFile = new GT_Config(new Configuration(new File(gtDir, "Other.cfg"))); GregTech_API.sIDFile = new GT_Config(GT_Config.sConfigFileIDs = new Configuration(new File(gtDir, "IDs.cfg"))); + GregTech_API.sSpecialFile.mConfig.getCategory("enchants").setComment("There you can set allowed enchant IDs to specific GT tools\nIt will let you apply choosen enchant in anvil to GT tool"); + mDoNotInit = (!tFile.getAbsolutePath().toLowerCase().contains("voltz")) && (tFile.getAbsolutePath().toLowerCase().contains(".technic") || tFile.getAbsolutePath().toLowerCase().contains("tekkit")); if (mDoNotInit) { GT_Log.log.warn("Detected Technic Launcher."); @@ -294,10 +294,6 @@ public class GT_Mod implements IGT_Mod { return; } - GT_Tool_Item.allowedEnchantIDs.addAll( // TODO make per different tool type - Arrays.stream(tConfig1.get("general", "allowedEnchantmnetsIDs", new int[0], "List of enchantment IDs allowed to use any GT tools in anvil (No enchantment table :P)").getIntList()) - .boxed().collect(Collectors.toList())); - GT_Log.mOreDictLogFile = new File(aEvent.getModConfigurationDirectory().getParentFile(), "logs/GT_OreDict.log"); if(!GT_Log.mOreDictLogFile.exists()) { try { diff --git a/src/main/java/gregtechmod/api/items/GT_Tool_Item.java b/src/main/java/gregtechmod/api/items/GT_Tool_Item.java index 4100467..e62bbbd 100644 --- a/src/main/java/gregtechmod/api/items/GT_Tool_Item.java +++ b/src/main/java/gregtechmod/api/items/GT_Tool_Item.java @@ -6,6 +6,7 @@ import gregtechmod.api.util.GT_OreDictUnificator; import gregtechmod.api.util.GT_Utility; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -29,6 +30,7 @@ import net.minecraft.world.World; import com.google.common.collect.Maps; import com.google.common.collect.Multimap; +import com.google.common.collect.MultimapBuilder; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -37,8 +39,8 @@ import cpw.mods.fml.relauncher.SideOnly; * This is just a basic Tool, which has normal durability and could break Blocks. */ public class GT_Tool_Item extends GT_Generic_Item { - - public static final List allowedEnchantIDs = new ArrayList<>(); + + private static final Multimap toolEnchants = MultimapBuilder.hashKeys().hashSetValues().build(); public final List mEffectiveAgainstList = new ArrayList(); public final List mEffectiveBlocksList = new ArrayList(); @@ -77,6 +79,17 @@ public class GT_Tool_Item extends GT_Generic_Item { GT_ModHandler.registerBoxableItemToToolBox(this); } + // Registry for enhantability config + public final static void registerTool(GT_Tool_Item item) { + if (item.delegate == null || GT_Utility.isStringInvalid(item.delegate.name())) + throw new IllegalArgumentException("Wrong item was supplied! May be it still not registered in GameRegistry"); + String regName = item.delegate.name(); + int[] enchantIDs = GregTech_API.sSpecialFile.mConfig.get("enchants", regName, new int[0]).getIntList(); + for (int i : enchantIDs) { + toolEnchants.put(regName, Integer.valueOf(i)); + } + } + public final GT_Tool_Item addToEffectiveList(String aEntityClassName) { mEffectiveAgainstList.add(aEntityClassName.substring(aEntityClassName.lastIndexOf(".")+1)); return this; @@ -201,12 +214,15 @@ public class GT_Tool_Item extends GT_Generic_Item { public void checkEnchantmentEffects(ItemStack aStack) { if (aStack != null) { + Collection allowedEnchantIDs = toolEnchants.get(delegate.name()); + if (aStack.isItemEnchanted()) { @SuppressWarnings("unchecked") Map enchs = EnchantmentHelper.getEnchantments(aStack); - enchs = Maps.filterEntries(enchs, e -> allowedEnchantIDs.contains(e.getKey())); + enchs = Maps.filterEntries(enchs, e -> allowedEnchantIDs != null && allowedEnchantIDs.contains(e.getKey())); EnchantmentHelper.setEnchantments(enchs, aStack); } + if (!GT_ModHandler.isElectricItem(aStack) || GT_ModHandler.canUseElectricItem(aStack, mEUperBrokenBlock < 0 ? getDamagePerBlockBreak() * 1000 : mEUperBrokenBlock)) { if (mSilklevel > 0) aStack.addEnchantment(Enchantment.silkTouch , mSilklevel); if (mFortunelevel > 0) aStack.addEnchantment(Enchantment.fortune , mFortunelevel); @@ -217,10 +233,14 @@ public class GT_Tool_Item extends GT_Generic_Item { /** * Will called through hook in ContainerRepair - * TODO: add configurable mappings, remove class check */ public boolean checkEnchant(Enchantment ench, ItemStack stack) { - return allowedEnchantIDs.contains(ench.effectId) && stack.getItem() instanceof GT_Saw_Item; + Collection IDs; + if (this == stack.getItem() && (IDs = toolEnchants.get(delegate.name())) != null) { + return IDs.contains(ench.effectId); + } + + return false; } @Override @@ -272,13 +292,12 @@ public class GT_Tool_Item extends GT_Generic_Item { @Override public boolean isBookEnchantable(ItemStack aStack, ItemStack aBook) { - if (aBook.hasEffect(0)) { - @SuppressWarnings("unchecked") - Map enchs = EnchantmentHelper.getEnchantments(aBook); - Entry enchant = enchs.entrySet().parallelStream().findAny().get(); - if (enchant != null) { - return allowedEnchantIDs.contains(enchant.getKey()); - } + @SuppressWarnings("unchecked") + Map enchs = EnchantmentHelper.getEnchantments(aBook); + Entry enchant = enchs.entrySet().parallelStream().findAny().get(); + Collection IDs; + if (enchant != null && (IDs = toolEnchants.get(delegate.name())) != null) { + return IDs.contains(enchant.getKey()); } @@ -316,7 +335,7 @@ public class GT_Tool_Item extends GT_Generic_Item { // checkEnchantmentEffects(aStack); // Seriosly? pinging every render tick poor item? @SuppressWarnings("unchecked") Set enchsIds = EnchantmentHelper.getEnchantments(aStack).keySet(); - enchsIds.retainAll(allowedEnchantIDs); + enchsIds.retainAll(toolEnchants.get(delegate.name())); return enchsIds.size() > 0; } diff --git a/src/main/java/gregtechmod/loaders/preload/GT_ItemLoader.java b/src/main/java/gregtechmod/loaders/preload/GT_ItemLoader.java index 2f62cc6..f4912be 100644 --- a/src/main/java/gregtechmod/loaders/preload/GT_ItemLoader.java +++ b/src/main/java/gregtechmod/loaders/preload/GT_ItemLoader.java @@ -23,6 +23,7 @@ import gregtechmod.api.items.GT_Spray_Hardener_Item; import gregtechmod.api.items.GT_Spray_Hydration_Item; import gregtechmod.api.items.GT_Spray_Ice_Item; import gregtechmod.api.items.GT_Spray_Pepper_Item; +import gregtechmod.api.items.GT_Tool_Item; import gregtechmod.api.util.GT_FoodStat; import gregtechmod.api.util.GT_Log; import gregtechmod.api.util.GT_ModHandler; @@ -52,6 +53,7 @@ import gregtechmod.common.items.GT_Vanilla_Sword; import net.minecraft.block.material.Material; import net.minecraft.init.Items; import net.minecraft.item.EnumAction; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.potion.Potion; @@ -513,7 +515,7 @@ public class GT_ItemLoader implements Runnable { GameRegistry.registerItem(GregTech_API.sItemList[118], "tungstensteel_saw"); GameRegistry.registerItem(GregTech_API.sItemList[119], "electric_saw"); GameRegistry.registerItem(GregTech_API.sItemList[120], "advanced_saw"); - GameRegistry.registerItem(GregTech_API.sItemList[121], "empty_electric_Saw"); + GameRegistry.registerItem(GregTech_API.sItemList[121], "empty_electric_saw"); GameRegistry.registerItem(GregTech_API.sItemList[122], "empty_advanced_saw"); GameRegistry.registerItem(GregTech_API.sItemList[123], "advanced_drill"); GameRegistry.registerItem(GregTech_API.sItemList[124], "flint_sword"); @@ -536,6 +538,15 @@ public class GT_ItemLoader implements Runnable { GameRegistry.registerItem(GregTech_API.sItemList[141], "empty_electric_screwdriver"); GameRegistry.registerItem(GregTech_API.sItemList[142], "plastic_mallet"); + GT_Log.log.info("Loading tools"); + for (Item item : GregTech_API.sItemList) { + if (item instanceof GT_Tool_Item) { + GT_Tool_Item.registerTool((GT_Tool_Item)item); + } + } + + GregTech_API.sSpecialFile.mConfig.save(); + GT_Log.log.info("Loading item related stuff"); GT_OreDictUnificator.addToBlacklist(GT_Items.Circuit_Integrated.getWildcard(1));