diff options
author | Jakub <53441451+kuba6000@users.noreply.github.com> | 2023-06-15 17:53:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-15 17:53:16 +0200 |
commit | cb383c824c3f799e678fb98f29661d79b5a13836 (patch) | |
tree | c27ec3672eb0cb1726565bf28ae2655404496231 | |
parent | b2c2a6dfe91696d4ecada95e5e43806ddb144ece (diff) | |
download | GT5-Unofficial-cb383c824c3f799e678fb98f29661d79b5a13836.tar.gz GT5-Unofficial-cb383c824c3f799e678fb98f29661d79b5a13836.tar.bz2 GT5-Unofficial-cb383c824c3f799e678fb98f29661d79b5a13836.zip |
Use mixins accessors + some misc fixes (#77)
* Change reflections to mixins
* Wrap witchery checking
* Remove more repeating code
* hmm
* test generation
* test
* client sided
* Update CommandCustomDrops.java
* Update MobRecipeLoader.java
* Save to static variable
* Imports
* Log message
* Convert InfernalHelper to mixin accessors
* Update build.gradle
* One more
* Return class nodes to optimize
* Translations mixin
* Automatically add commands
* Fixes
* Fix https://github.com/GTNewHorizons/GT-New-Horizons-Modpack/issues/12021
* Update kubatech.java
* Update CommonProxy.java
* Unnecessary qualified reference
* Simplify ItemUtils
* Check if single player diffrently
* Remove accessor for infernal-mobs
33 files changed, 980 insertions, 766 deletions
diff --git a/build.gradle b/build.gradle index 3930990407..f56a438f84 100644 --- a/build.gradle +++ b/build.gradle @@ -879,6 +879,7 @@ def getManifestAttributes() { "ForceLoadAsMod": !containsMixinsAndOrCoreModOnly.toBoolean() ] } + manifestAttributes += ["Main-class": "kubatech.standalone"] return manifestAttributes } diff --git a/dependencies.gradle b/dependencies.gradle index 54717f7e1f..67de402103 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,12 +1,46 @@ -// Add your dependencies here +/* + * Add your dependencies here. Supported configurations: + * - api("group:name:version:classifier"): if you use the types from this dependency in the public API of this mod + * Available at runtime and compiletime for mods depending on this mod + * - implementation("g:n:v:c"): if you need this for internal implementation details of the mod, but none of it is visible via the public API + * Available at runtime but not compiletime for mods depending on this mod + * - compileOnly("g:n:v:c"): if the mod you're building doesn't need this dependency during runtime at all, e.g. for optional mods + * Not available at all for mods depending on this mod, only visible at compiletime for this mod + * - compileOnlyApi("g:n:v:c"): like compileOnly, but also visible at compiletime for mods depending on this mod + * Available at compiletime but not runtime for mods depending on this mod + * - runtimeOnlyNonPublishable("g:n:v:c"): if you want to include a mod in this mod's runClient/runServer runs, but not publish it as a dependency + * Not available at all for mods depending on this mod, only visible at runtime for this mod + * - devOnlyNonPublishable("g:n:v:c"): a combination of runtimeOnlyNonPublishable and compileOnly for dependencies present at both compiletime and runtime, + * but not published as Maven dependencies - useful for RFG-deobfuscated dependencies or local testing + * - runtimeOnly("g:n:v:c"): if you don't need this at compile time, but want it to be present at runtime + * Available at runtime for mods depending on this mod + * - annotationProcessor("g:n:v:c"): mostly for java compiler plugins, if you know you need this, use it, otherwise don't worry + * - testCONFIG("g:n:v:c") - replace CONFIG by one of the above (except api), same as above but for the test sources instead of main + * + * - shadowImplementation("g:n:v:c"): effectively the same as API, but the dependency is included in your jar under a renamed package name + * Requires you to enable usesShadowedDependencies in gradle.properties + * + * - compile("g:n:v:c"): deprecated, replace with "api" (works like the old "compile") or "implementation" (can be more efficient) + * + * You can exclude transitive dependencies (dependencies of the chosen dependency) by appending { transitive = false } if needed, + * but use this sparingly as it can break using your mod as another mod's dependency if you're not careful. + * + * To depend on obfuscated jars you can use `devOnlyNonPublishable(rfg.deobf("dep:spec:1.2.3"))` to fetch an obfuscated jar from maven, + * or `devOnlyNonPublishable(rfg.deobf(project.files("libs/my-mod-jar.jar")))` to use a file. + * + * Gradle names for some of the configuration can be misleading, compileOnlyApi and runtimeOnly both get published as dependencies in Maven, but compileOnly does not. + * The buildscript adds runtimeOnlyNonPublishable to also have a runtime dependency that's not published. + * + * For more details, see https://docs.gradle.org/8.0.1/userguide/java_library_plugin.html#sec:java_library_configurations_graph + */ dependencies { api('com.github.GTNewHorizons:GT5-Unofficial:5.09.43.82:dev') api("com.github.GTNewHorizons:EnderCore:0.2.16:dev") api("com.github.GTNewHorizons:EnderIO:2.4.18:dev") - api("com.github.GTNewHorizons:Infernal-Mobs:1.7.8-GTNH:dev") api("com.github.GTNewHorizons:ForestryMC:4.6.7:dev") api("com.github.GTNewHorizons:ModularUI:1.1.10:dev") + devOnlyNonPublishable("com.github.GTNewHorizons:Infernal-Mobs:1.7.9-GTNH:dev") //compileOnly("curse.maven:extrautilities-225561:2264384") { // transitive = false //} diff --git a/src/main/java/kubatech/CommonProxy.java b/src/main/java/kubatech/CommonProxy.java index b0d8446211..267efdab21 100644 --- a/src/main/java/kubatech/CommonProxy.java +++ b/src/main/java/kubatech/CommonProxy.java @@ -36,11 +36,7 @@ import cpw.mods.fml.common.event.FMLServerStartingEvent; import cpw.mods.fml.common.event.FMLServerStoppedEvent; import cpw.mods.fml.common.event.FMLServerStoppingEvent; import kubatech.api.LoaderReference; -import kubatech.commands.CommandBees; -import kubatech.commands.CommandConfig; import kubatech.commands.CommandHandler; -import kubatech.commands.CommandHelp; -import kubatech.commands.CommandTea; import kubatech.config.Config; import kubatech.loaders.MTLoader; import kubatech.loaders.RecipeLoader; @@ -76,10 +72,6 @@ public class CommonProxy { public void serverStarting(FMLServerStartingEvent event) { RecipeLoader.addRecipesLate(); CommandHandler cmd = new CommandHandler(); - cmd.addCommand(new CommandHelp()); - cmd.addCommand(new CommandConfig()); - cmd.addCommand(new CommandBees()); - cmd.addCommand(new CommandTea()); event.registerServerCommand(cmd); } diff --git a/src/main/java/kubatech/api/helpers/InfernalHelper.java b/src/main/java/kubatech/api/helpers/InfernalHelper.java deleted file mode 100644 index e3c0db456b..0000000000 --- a/src/main/java/kubatech/api/helpers/InfernalHelper.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * spotless:off - * KubaTech - Gregtech Addon - * Copyright (C) 2022 - 2023 kuba6000 - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library. If not, see <https://www.gnu.org/licenses/>. - * spotless:on - */ - -package kubatech.api.helpers; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.ArrayList; - -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.item.ItemStack; - -import atomicstryker.infernalmobs.common.InfernalMobsCore; -import atomicstryker.infernalmobs.common.mods.api.ModifierLoader; - -@SuppressWarnings("unchecked") -public class InfernalHelper { - - private static Method isClassAllowed = null; - - public static boolean isClassAllowed(EntityLivingBase e) { - try { - if (isClassAllowed == null) { - isClassAllowed = InfernalMobsCore.class.getDeclaredMethod("isClassAllowed", EntityLivingBase.class); - isClassAllowed.setAccessible(true); - } - return (boolean) isClassAllowed.invoke(InfernalMobsCore.instance(), e); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return false; - } - - private static Method checkEntityClassForced = null; - - public static boolean checkEntityClassForced(EntityLivingBase e) { - try { - if (checkEntityClassForced == null) { - checkEntityClassForced = InfernalMobsCore.class - .getDeclaredMethod("checkEntityClassForced", EntityLivingBase.class); - checkEntityClassForced.setAccessible(true); - } - return (boolean) checkEntityClassForced.invoke(InfernalMobsCore.instance(), e); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return false; - } - - private static Field modifierLoaders = null; - - public static ArrayList<ModifierLoader<?>> getModifierLoaders() { - try { - if (modifierLoaders == null) { - modifierLoaders = InfernalMobsCore.class.getDeclaredField("modifierLoaders"); - modifierLoaders.setAccessible(true); - } - return (ArrayList<ModifierLoader<?>>) modifierLoaders.get(InfernalMobsCore.instance()); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return new ArrayList<>(); - } - - private static Field eliteRarity; - - public static int getEliteRarity() { - try { - if (eliteRarity == null) { - eliteRarity = InfernalMobsCore.class.getDeclaredField("eliteRarity"); - eliteRarity.setAccessible(true); - } - return eliteRarity.getInt(InfernalMobsCore.instance()); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return 15; - } - - private static Field ultraRarity; - - public static int getUltraRarity() { - try { - if (ultraRarity == null) { - ultraRarity = InfernalMobsCore.class.getDeclaredField("ultraRarity"); - ultraRarity.setAccessible(true); - } - return ultraRarity.getInt(InfernalMobsCore.instance()); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return 15; - } - - private static Field infernoRarity; - - public static int getInfernoRarity() { - try { - if (infernoRarity == null) { - infernoRarity = InfernalMobsCore.class.getDeclaredField("infernoRarity"); - infernoRarity.setAccessible(true); - } - return infernoRarity.getInt(InfernalMobsCore.instance()); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return 15; - } - - private static Field minEliteModifiers; - - public static int getMinEliteModifiers() { - try { - if (minEliteModifiers == null) { - minEliteModifiers = InfernalMobsCore.class.getDeclaredField("minEliteModifiers"); - minEliteModifiers.setAccessible(true); - } - return minEliteModifiers.getInt(InfernalMobsCore.instance()); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return 15; - } - - private static Field minUltraModifiers; - - public static int getMinUltraModifiers() { - try { - if (minUltraModifiers == null) { - minUltraModifiers = InfernalMobsCore.class.getDeclaredField("minUltraModifiers"); - minUltraModifiers.setAccessible(true); - } - return minUltraModifiers.getInt(InfernalMobsCore.instance()); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return 15; - } - - private static Field minInfernoModifiers; - - public static int getMinInfernoModifiers() { - try { - if (minInfernoModifiers == null) { - minInfernoModifiers = InfernalMobsCore.class.getDeclaredField("minInfernoModifiers"); - minInfernoModifiers.setAccessible(true); - } - return minInfernoModifiers.getInt(InfernalMobsCore.instance()); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return 15; - } - - private static Field dimensionBlackList; - - public static ArrayList<Integer> getDimensionBlackList() { - try { - if (dimensionBlackList == null) { - dimensionBlackList = InfernalMobsCore.class.getDeclaredField("dimensionBlackList"); - dimensionBlackList.setAccessible(true); - } - return (ArrayList<Integer>) dimensionBlackList.get(InfernalMobsCore.instance()); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return new ArrayList<>(); - } - - private static Field dropIdListElite; - - public static ArrayList<ItemStack> getDropIdListElite() { - try { - if (dropIdListElite == null) { - dropIdListElite = InfernalMobsCore.class.getDeclaredField("dropIdListElite"); - dropIdListElite.setAccessible(true); - } - return (ArrayList<ItemStack>) dropIdListElite.get(InfernalMobsCore.instance()); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return new ArrayList<>(); - } - - private static Field dropIdListUltra; - - public static ArrayList<ItemStack> getDropIdListUltra() { - try { - if (dropIdListUltra == null) { - dropIdListUltra = InfernalMobsCore.class.getDeclaredField("dropIdListUltra"); - dropIdListUltra.setAccessible(true); - } - return (ArrayList<ItemStack>) dropIdListUltra.get(InfernalMobsCore.instance()); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return new ArrayList<>(); - } - - private static Field dropIdListInfernal; - - public static ArrayList<ItemStack> getDropIdListInfernal() { - try { - if (dropIdListInfernal == null) { - dropIdListInfernal = InfernalMobsCore.class.getDeclaredField("dropIdListInfernal"); - dropIdListInfernal.setAccessible(true); - } - return (ArrayList<ItemStack>) dropIdListInfernal.get(InfernalMobsCore.instance()); - } catch (Throwable exception) { - exception.printStackTrace(); - } - return new ArrayList<>(); - } -} diff --git a/src/main/java/kubatech/api/helpers/ReflectionHelper.java b/src/main/java/kubatech/api/helpers/ReflectionHelper.java index 63fd6bd633..8f2234f052 100644 --- a/src/main/java/kubatech/api/helpers/ReflectionHelper.java +++ b/src/main/java/kubatech/api/helpers/ReflectionHelper.java @@ -20,9 +20,23 @@ package kubatech.api.helpers; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.net.URL; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; +import java.util.Objects; +import java.util.jar.JarFile; +import java.util.stream.Collectors; + +import net.minecraft.launchwrapper.Launch; + +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.tree.ClassNode; public class ReflectionHelper { @@ -140,4 +154,53 @@ public class ReflectionHelper { return defaultValue; } } + + /** + * Gets all classes in a specific package path, works only for jar files. + * + * @param packageName The package name + * @return The class nodes + */ + public static Collection<ClassNode> getClasses(String packageName) throws IOException, SecurityException { + ClassLoader classLoader = Thread.currentThread() + .getContextClassLoader(); + assert classLoader != null; + String packagePath = packageName.replace('.', '/'); + URL resource = classLoader.getResource(packagePath); + if (resource == null) throw new FileNotFoundException(); + if (!resource.getProtocol() + .equals("jar")) return Collections.emptySet(); + String jarPath = resource.getPath(); + + try (JarFile jar = new JarFile(jarPath.substring(5, jarPath.indexOf('!')))) { + return jar.stream() + .filter( + j -> !j.isDirectory() && j.getName() + .startsWith(packagePath) + && j.getName() + .endsWith(".class")) + .map(j -> { + try { + String name = j.getName(); + URL jarResource = Launch.classLoader.getResource(name); + if (jarResource == null) return null; + byte[] bytes; + try (InputStream is = jarResource.openStream()) { + bytes = new byte[(int) j.getSize()]; + if (is.read(bytes) != bytes.length) return null; + if (is.available() > 0) return null; + } + + ClassNode cn = new ClassNode(); + ClassReader cr = new ClassReader(bytes); + cr.accept(cn, 0); + + return cn; + } catch (IOException ignored) {} + return null; + }) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + } + } } diff --git a/src/main/java/kubatech/api/network/CustomTileEntityPacket.java b/src/main/java/kubatech/api/network/CustomTileEntityPacket.java index cd65d08d57..67a310ecf5 100644 --- a/src/main/java/kubatech/api/network/CustomTileEntityPacket.java +++ b/src/main/java/kubatech/api/network/CustomTileEntityPacket.java @@ -132,7 +132,7 @@ public class CustomTileEntityPacket implements IMessage { @Override public IMessage onMessage(CustomTileEntityPacket message, MessageContext ctx) { - if (!ModUtils.isClientSided) return null; + if (!ModUtils.isClientThreaded()) return null; Minecraft mc = Minecraft.getMinecraft(); if (mc == null) return null; if (mc.thePlayer == null) return null; diff --git a/src/main/java/kubatech/api/utils/ItemUtils.java b/src/main/java/kubatech/api/utils/ItemUtils.java new file mode 100644 index 0000000000..2fc34057c3 --- /dev/null +++ b/src/main/java/kubatech/api/utils/ItemUtils.java @@ -0,0 +1,26 @@ +package kubatech.api.utils; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +public class ItemUtils { + + public static NBTTagCompound writeItemStackToNBT(ItemStack stack) { + NBTTagCompound compound = new NBTTagCompound(); + + stack.writeToNBT(compound); + compound.setInteger("IntCount", stack.stackSize); + + return compound; + } + + public static ItemStack readItemStackFromNBT(NBTTagCompound compound) { + ItemStack stack = ItemStack.loadItemStackFromNBT(compound); + + if (stack == null) return null; + + if (compound.hasKey("IntCount")) stack.stackSize = compound.getInteger("IntCount"); + + return stack; + } +} diff --git a/src/main/java/kubatech/api/utils/MobUtils.java b/src/main/java/kubatech/api/utils/MobUtils.java index ad48c51cbf..d3ec59757a 100644 --- a/src/main/java/kubatech/api/utils/MobUtils.java +++ b/src/main/java/kubatech/api/utils/MobUtils.java @@ -20,8 +20,6 @@ package kubatech.api.utils; -import java.lang.reflect.Field; - import net.minecraft.client.model.ModelBase; import net.minecraft.client.model.ModelBox; import net.minecraft.client.model.ModelRenderer; @@ -32,11 +30,10 @@ import net.minecraft.entity.EntityLiving; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import kubatech.mixin.mixins.minecraft.RendererLivingEntityAccessor; public class MobUtils { - private static Field mainmodelfield = null; - @SideOnly(Side.CLIENT) public static float getDesiredScale(EntityLiving e, float desiredHeight) { return getDesiredScale(getMobHeight(e), desiredHeight); @@ -50,17 +47,12 @@ public class MobUtils { @SideOnly(Side.CLIENT) public static float getMobHeight(EntityLiving e) { try { - if (mainmodelfield == null) { - mainmodelfield = RendererLivingEntity.class - .getDeclaredField(ModUtils.isDeobfuscatedEnvironment ? "mainModel" : "field_77045_g"); - mainmodelfield.setAccessible(true); - } float eheight = e.height; float ewidth = e.width; Render r = RenderManager.instance.getEntityRenderObject(e); - if (r instanceof RendererLivingEntity && mainmodelfield != null) { - ModelBase mainmodel = (ModelBase) mainmodelfield.get(r); - for (Object box : mainmodel.boxList) { + if (r instanceof RendererLivingEntity) { + ModelBase mainModel = ((RendererLivingEntityAccessor) r).getMainModel(); + for (Object box : mainModel.boxList) { if (box instanceof ModelRenderer) { float minY = 999f; float minX = 999f; diff --git a/src/main/java/kubatech/api/utils/ModUtils.java b/src/main/java/kubatech/api/utils/ModUtils.java index cd02d28dba..61352cce9d 100644 --- a/src/main/java/kubatech/api/utils/ModUtils.java +++ b/src/main/java/kubatech/api/utils/ModUtils.java @@ -32,6 +32,7 @@ import javax.xml.bind.DatatypeConverter; import net.minecraft.launchwrapper.Launch; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.ModContainer; import kubatech.kubatech; @@ -41,6 +42,19 @@ public class ModUtils { public static final boolean isDeobfuscatedEnvironment = (boolean) Launch.blackboard .get("fml.deobfuscatedEnvironment"); public static boolean isClientSided = false; + + public static boolean isClientThreaded() { + return FMLCommonHandler.instance() + .getEffectiveSide() + .isClient(); + } + + @FunctionalInterface + public interface TriConsumer<T, U, V> { + + void accept(T t, U u, V v); + } + private static final HashMap<String, String> classNamesToModIDs = new HashMap<>(); private static final Map.Entry<String, String> emptyEntry = new AbstractMap.SimpleEntry<>("", ""); diff --git a/src/main/java/kubatech/commands/CommandBees.java b/src/main/java/kubatech/commands/CommandBees.java index 8d01080500..edf3b04ce6 100644 --- a/src/main/java/kubatech/commands/CommandBees.java +++ b/src/main/java/kubatech/commands/CommandBees.java @@ -28,7 +28,6 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.List; -import net.minecraft.client.Minecraft; import net.minecraft.command.CommandBase; import net.minecraft.command.ICommandSender; import net.minecraft.item.ItemStack; @@ -40,7 +39,9 @@ import com.google.common.io.Files; import forestry.api.apiculture.IAlleleBeeSpecies; import forestry.api.apiculture.IBee; import forestry.api.apiculture.IBeeGenome; +import kubatech.api.utils.ModUtils; +@CommandHandler.ChildCommand public class CommandBees extends CommandBase { @Override @@ -62,8 +63,7 @@ public class CommandBees extends CommandBase { @Override public void processCommand(ICommandSender p_71515_1_, String[] p_71515_2_) { - if (!Minecraft.getMinecraft() - .isSingleplayer()) { + if (!ModUtils.isClientSided) { p_71515_1_ .addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "This command is single-player only!")); return; diff --git a/src/main/java/kubatech/commands/CommandConfig.java b/src/main/java/kubatech/commands/CommandConfig.java index e4ffe9753d..cf43bc88ba 100644 --- a/src/main/java/kubatech/commands/CommandConfig.java +++ b/src/main/java/kubatech/commands/CommandConfig.java @@ -36,6 +36,7 @@ import kubatech.config.Config; import kubatech.kubatech; import kubatech.loaders.MobRecipeLoader; +@CommandHandler.ChildCommand public class CommandConfig extends CommandBase { enum Translations { diff --git a/src/main/java/kubatech/commands/CommandCustomDrops.java b/src/main/java/kubatech/commands/CommandCustomDrops.java new file mode 100644 index 0000000000..80148eb796 --- /dev/null +++ b/src/main/java/kubatech/commands/CommandCustomDrops.java @@ -0,0 +1,67 @@ +/* + * spotless:off + * KubaTech - Gregtech Addon + * Copyright (C) 2022 - 2023 kuba6000 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see <https://www.gnu.org/licenses/>. + * spotless:on + */ + +package kubatech.commands; + +import java.io.File; + +import net.minecraft.command.CommandBase; +import net.minecraft.command.ICommandSender; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.EnumChatFormatting; + +import kubatech.api.utils.ModUtils; +import kubatech.loaders.MobRecipeLoader; + +@CommandHandler.ChildCommand +public class CommandCustomDrops extends CommandBase { + + @Override + public String getCommandName() { + return "customdrops"; + } + + @Override + public String getCommandUsage(ICommandSender p_71518_1_) { + return "customdrops"; + } + + @Override + public int getRequiredPermissionLevel() { + return 4; + } + + @Override + public void processCommand(ICommandSender p_71515_1_, String[] p_71515_2_) { + + if (!ModUtils.isClientSided) { + p_71515_1_ + .addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "This command is single-player only!")); + return; + } + + File f = MobRecipeLoader.makeCustomDrops(); + + if (f == null) { + p_71515_1_.addChatMessage( + new ChatComponentText(EnumChatFormatting.RED + "There was an error! Look in the console")); + } else p_71515_1_.addChatMessage(new ChatComponentText(f.getAbsolutePath())); + } +} diff --git a/src/main/java/kubatech/commands/CommandHandler.java b/src/main/java/kubatech/commands/CommandHandler.java index 0daea862ca..89f1db7429 100644 --- a/src/main/java/kubatech/commands/CommandHandler.java +++ b/src/main/java/kubatech/commands/CommandHandler.java @@ -25,6 +25,10 @@ import static kubatech.commands.CommandHandler.Translations.GENERIC_HELP; import static kubatech.commands.CommandHandler.Translations.INVALID; import static kubatech.commands.CommandHandler.Translations.USAGE; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -38,6 +42,8 @@ import net.minecraft.util.ChatComponentTranslation; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.StatCollector; +import kubatech.kubatech; + public class CommandHandler extends CommandBase { enum Translations { @@ -121,7 +127,29 @@ public class CommandHandler extends CommandBase { return true; } - public void addCommand(ICommand command) { + public static void addCommand(ICommand command) { commands.put(command.getCommandName(), command); } + + static { + String ChildCommandDesc = "L" + ChildCommand.class.getName() + .replace(".", "/") + ";"; + kubatech.myClasses.stream() + .filter( + clazz -> clazz.invisibleAnnotations != null && clazz.invisibleAnnotations.stream() + .anyMatch(ann -> ann.desc.equals(ChildCommandDesc))) + .forEach(clazz -> { + try { + addCommand( + (ICommand) Class.forName(clazz.name.replace("/", ".")) + .newInstance()); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { + throw new RuntimeException(e); + } + }); + } + + @Target(ElementType.TYPE) + @Retention(RetentionPolicy.CLASS) + public @interface ChildCommand {} } diff --git a/src/main/java/kubatech/commands/CommandHelp.java b/src/main/java/kubatech/commands/CommandHelp.java index 47a448afe1..d7e9fbee59 100644 --- a/src/main/java/kubatech/commands/CommandHelp.java +++ b/src/main/java/kubatech/commands/CommandHelp.java @@ -28,6 +28,7 @@ import net.minecraft.command.ICommandSender; import net.minecraft.util.ChatComponentText; import net.minecraft.util.StatCollector; +@CommandHandler.ChildCommand public class CommandHelp extends CommandBase { enum Translations { diff --git a/src/main/java/kubatech/commands/CommandTea.java b/src/main/java/kubatech/commands/CommandTea.java index 70a924d9c7..64412f8ef3 100644 --- a/src/main/java/kubatech/commands/CommandTea.java +++ b/src/main/java/kubatech/commands/CommandTea.java @@ -38,6 +38,7 @@ import net.minecraft.util.StatCollector; import kubatech.api.helpers.UUIDFinder; import kubatech.api.tea.TeaNetwork; +@CommandHandler.ChildCommand public class CommandTea extends CommandBase { enum Translations { diff --git a/src/main/java/kubatech/kubatech.java b/src/main/java/kubatech/kubatech.java index e13f1c36e0..6cb6bd299c 100644 --- a/src/main/java/kubatech/kubatech.java +++ b/src/main/java/kubatech/kubatech.java @@ -22,6 +22,8 @@ package kubatech; import static kubatech.api.enums.ItemList.LegendaryRedTea; +import java.io.IOException; +import java.util.Collection; import java.util.List; import net.minecraft.creativetab.CreativeTabs; @@ -30,6 +32,7 @@ import net.minecraft.item.ItemStack; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.objectweb.asm.tree.ClassNode; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.SidedProxy; @@ -45,6 +48,7 @@ import cpw.mods.fml.common.event.FMLServerStoppingEvent; import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; import cpw.mods.fml.relauncher.Side; import kubatech.api.enums.ItemList; +import kubatech.api.helpers.ReflectionHelper; import kubatech.api.network.CustomTileEntityPacket; import kubatech.api.network.LoadConfigPacket; @@ -67,7 +71,7 @@ import kubatech.api.network.LoadConfigPacket; + "after: Forestry; " + "after: DraconicEvolution; " + "after: Avaritia; " - + " after: dreamcraft; ") + + "after: dreamcraft; ") public class kubatech { public static kubatech instance = null; @@ -112,9 +116,22 @@ public class kubatech { @SidedProxy(clientSide = Tags.MODID + ".ClientProxy", serverSide = Tags.MODID + ".CommonProxy") public static CommonProxy proxy; + public static Collection<ClassNode> myClasses; + @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { instance = this; + final long timeStart = System.currentTimeMillis(); + try { + myClasses = ReflectionHelper.getClasses( + this.getClass() + .getPackage() + .getName()); + } catch (IOException e) { + throw new RuntimeException(e); + } + final long timeToLoad = System.currentTimeMillis() - timeStart; + info("Class discovery took " + timeToLoad + "ms ! Found " + myClasses.size() + " classes."); proxy.preInit(event); } diff --git a/src/main/java/kubatech/loaders/MobRecipeLoader.java b/src/main/java/kubatech/loaders/MobRecipeLoader.java index ebb1a2a906..da54d44117 100644 --- a/src/main/java/kubatech/loaders/MobRecipeLoader.java +++ b/src/main/java/kubatech/loaders/MobRecipeLoader.java @@ -29,8 +29,6 @@ import static kubatech.tileentity.gregtech.multiblock.GT_MetaTileEntity_ExtremeE import java.io.File; import java.io.Reader; import java.io.Writer; -import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -41,6 +39,8 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Random; +import java.util.UUID; +import java.util.function.Supplier; import java.util.stream.Collectors; import net.minecraft.block.Block; @@ -49,7 +49,6 @@ import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityList; import net.minecraft.entity.EntityLiving; -import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.monster.EntitySkeleton; import net.minecraft.entity.monster.EntitySlime; @@ -59,18 +58,23 @@ import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.DamageSource; import net.minecraft.util.StatCollector; import net.minecraft.world.World; +import net.minecraftforge.common.ForgeHooks; +import net.minecraftforge.common.util.FakePlayer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.google.common.io.Files; import com.google.gson.Gson; +import com.mojang.authlib.GameProfile; import atomicstryker.infernalmobs.common.InfernalMobsCore; import atomicstryker.infernalmobs.common.MobModifier; import atomicstryker.infernalmobs.common.mods.api.ModifierLoader; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import gregtech.api.util.GT_Utility; @@ -78,14 +82,19 @@ import gregtech.common.GT_DummyWorld; import kubatech.Tags; import kubatech.api.LoaderReference; import kubatech.api.helpers.EnderIOHelper; -import kubatech.api.helpers.InfernalHelper; import kubatech.api.helpers.ProgressBarWrapper; import kubatech.api.mobhandler.MobDrop; import kubatech.api.network.LoadConfigPacket; +import kubatech.api.utils.FastRandom; import kubatech.api.utils.GSONUtils; import kubatech.api.utils.ModUtils; +import kubatech.api.utils.ModUtils.TriConsumer; import kubatech.config.Config; import kubatech.config.OverridesConfig; +import kubatech.mixin.mixins.minecraft.EntityAccessor; +import kubatech.mixin.mixins.minecraft.EntityLivingAccessor; +import kubatech.mixin.mixins.minecraft.EntityLivingBaseAccessor; +import kubatech.mixin.mixins.minecraft.EntitySlimeAccessor; import kubatech.nei.Mob_Handler; import kubatech.tileentity.gregtech.multiblock.GT_MetaTileEntity_ExtremeExterminationChamber; import minetweaker.MineTweakerAPI; @@ -99,12 +108,8 @@ public class MobRecipeLoader { private static final Logger LOG = LogManager.getLogger(Tags.MODID + "[Mob Recipe Loader]"); - private static final String dropFewItemsName = isDeobfuscatedEnvironment ? "dropFewItems" : "func_70628_a"; - private static final String dropRareDropName = isDeobfuscatedEnvironment ? "dropRareDrop" : "func_70600_l"; - private static final String setSlimeSizeName = isDeobfuscatedEnvironment ? "setSlimeSize" : "func_70799_a"; private static final String addRandomArmorName = isDeobfuscatedEnvironment ? "addRandomArmor" : "func_82164_bB"; private static final String enchantEquipmentName = isDeobfuscatedEnvironment ? "enchantEquipment" : "func_82162_bC"; - private static final String randName = isDeobfuscatedEnvironment ? "rand" : "field_70146_Z"; private static boolean alreadyGenerated = false; public static boolean isInGenerationProcess = false; @@ -158,57 +163,65 @@ public class MobRecipeLoader { @SuppressWarnings("unchecked") private MobRecipe(EntityLiving e, String entityID, ArrayList<MobDrop> outputs) { - if (infernaldrops == null && LoaderReference.InfernalMobs) { - infernaldrops = new droplist(); - LOG.info("Generating Infernal drops"); - ArrayList<ModifierLoader<?>> modifierLoaders = (ArrayList<ModifierLoader<?>>) InfernalHelper - .getModifierLoaders() - .clone(); - int i = 0; - for (ModifierLoader<?> modifierLoader : modifierLoaders) { - MobModifier nextMod = modifierLoader.make(null); - if (nextMod.getBlackListMobClasses() != null) - for (Class<?> cl : nextMod.getBlackListMobClasses()) if (e.getClass() - .isAssignableFrom(cl)) break; - i++; - } - if (i > 0) { - double chance = InfernalHelper.checkEntityClassForced(e) ? 1d - : (1d / InfernalHelper.getEliteRarity()); - ArrayList<ItemStack> elitelist = InfernalHelper.getDropIdListElite(); - for (ItemStack stack : elitelist) { - dropinstance instance = infernaldrops - .add(new dropinstance(stack.copy(), infernaldrops), chance / elitelist.size()); - instance.isEnchatmentRandomized = true; - // noinspection ConstantConditions - instance.enchantmentLevel = stack.getItem() - .getItemEnchantability(); - } - ArrayList<ItemStack> ultralist = InfernalHelper.getDropIdListUltra(); - chance *= 1d / InfernalHelper.getUltraRarity(); - for (ItemStack stack : ultralist) { - dropinstance instance = infernaldrops - .add(new dropinstance(stack.copy(), infernaldrops), chance / ultralist.size()); - instance.isEnchatmentRandomized = true; - // noinspection ConstantConditions - instance.enchantmentLevel = stack.getItem() - .getItemEnchantability(); + if (LoaderReference.InfernalMobs) { + InfernalMobsCore infernalMobsCore = InfernalMobsCore.instance(); + if (infernaldrops == null) { + infernaldrops = new droplist(); + LOG.info("Generating Infernal drops"); + ArrayList<ModifierLoader<?>> modifierLoaders = (ArrayList<ModifierLoader<?>>) infernalMobsCore + .getModifierLoaders() + .clone(); + int i = 0; + for (ModifierLoader<?> modifierLoader : modifierLoaders) { + MobModifier nextMod = modifierLoader.make(null); + if (nextMod.getBlackListMobClasses() != null) + for (Class<?> cl : nextMod.getBlackListMobClasses()) if (e.getClass() + .isAssignableFrom(cl)) break; + i++; } - ArrayList<ItemStack> infernallist = InfernalHelper.getDropIdListInfernal(); - chance *= 1d / InfernalHelper.getInfernoRarity(); - for (ItemStack stack : infernallist) { - dropinstance instance = infernaldrops - .add(new dropinstance(stack.copy(), infernaldrops), chance / infernallist.size()); - instance.isEnchatmentRandomized = true; - // noinspection ConstantConditions - instance.enchantmentLevel = stack.getItem() - .getItemEnchantability(); + if (i > 0) { + double chance = infernalMobsCore.checkEntityClassForced(e) ? 1d + : (1d / infernalMobsCore.getEliteRarity()); + ArrayList<ItemStack> elitelist = infernalMobsCore.getDropIdListElite(); + for (ItemStack stack : elitelist) { + dropinstance instance = infernaldrops + .add(new dropinstance(stack.copy(), infernaldrops), chance / elitelist.size()); + instance.isEnchatmentRandomized = true; + // noinspection ConstantConditions + instance.enchantmentLevel = stack.getItem() + .getItemEnchantability(); + } + ArrayList<ItemStack> ultralist = infernalMobsCore.getDropIdListUltra(); + chance *= 1d / infernalMobsCore.getUltraRarity(); + for (ItemStack stack : ultralist) { + dropinstance instance = infernaldrops + .add(new dropinstance(stack.copy(), infernaldrops), chance / ultralist.size()); + instance.isEnchatmentRandomized = true; + // noinspection ConstantConditions + instance.enchantmentLevel = stack.getItem() + .getItemEnchantability(); + } + ArrayList<ItemStack> infernallist = infernalMobsCore.getDropIdListInfernal(); + chance *= 1d / infernalMobsCore.getInfernoRarity(); + for (ItemStack stack : infernallist) { + dropinstance instance = infernaldrops + .add(new dropinstance(stack.copy(), infernaldrops), chance / infernallist.size()); + instance.isEnchatmentRandomized = true; + // noinspection ConstantConditions + instance.enchantmentLevel = stack.getItem() + .getItemEnchantability(); + } } } - } else if (infernaldrops == null) infernaldrops = new droplist(); + infernalityAllowed = infernalMobsCore.isClassAllowed(e); + alwaysinfernal = infernalMobsCore.checkEntityClassForced(e); + } else { + infernalityAllowed = false; + alwaysinfernal = false; + } + + if (infernaldrops == null) infernaldrops = new droplist(); - infernalityAllowed = InfernalHelper.isClassAllowed(e); - alwaysinfernal = InfernalHelper.checkEntityClassForced(e); isPeacefulAllowed = !(e instanceof IMob); mOutputs = (ArrayList<MobDrop>) outputs.clone(); @@ -266,44 +279,48 @@ public class MobRecipeLoader { } } - if (infernalityAllowed && mEUt * 8 < MTE.getMaxInputVoltage() - && !InfernalHelper.getDimensionBlackList() - .contains( - MTE.getBaseMetaTileEntity() - .getWorld().provider.dimensionId)) { - int p = 0; - int mods = 0; - if (alwaysinfernal || (preferInfernalDrops && rnd.nextInt(InfernalHelper.getEliteRarity()) == 0)) { - p = 1; - if (rnd.nextInt(InfernalHelper.getUltraRarity()) == 0) { - p = 2; - if (rnd.nextInt(InfernalHelper.getInfernoRarity()) == 0) p = 3; + if (LoaderReference.InfernalMobs) { + InfernalMobsCore infernalMobsCore = InfernalMobsCore.instance(); + if (infernalityAllowed && mEUt * 8 < MTE.getMaxInputVoltage() + && !infernalMobsCore.getDimensionBlackList() + .contains( + MTE.getBaseMetaTileEntity() + .getWorld().provider.dimensionId)) { + int p = 0; + int mods = 0; + if (alwaysinfernal + || (preferInfernalDrops && rnd.nextInt(infernalMobsCore.getEliteRarity()) == 0)) { + p = 1; + if (rnd.nextInt(infernalMobsCore.getUltraRarity()) == 0) { + p = 2; + if (rnd.nextInt(infernalMobsCore.getInfernoRarity()) == 0) p = 3; + } + } + ArrayList<ItemStack> infernalstacks = null; + if (p > 0) if (p == 1) { + infernalstacks = infernalMobsCore.getDropIdListElite(); + mods = infernalMobsCore.getMinEliteModifiers(); + } else if (p == 2) { + infernalstacks = infernalMobsCore.getDropIdListUltra(); + mods = infernalMobsCore.getMinUltraModifiers(); + } else { + infernalstacks = infernalMobsCore.getDropIdListInfernal(); + mods = infernalMobsCore.getMinInfernoModifiers(); + } + if (infernalstacks != null) { + ItemStack infernalstack = infernalstacks.get(rnd.nextInt(infernalstacks.size())) + .copy(); + // noinspection ConstantConditions + EnchantmentHelper.addRandomEnchantment( + rnd, + infernalstack, + infernalstack.getItem() + .getItemEnchantability()); + stacks.add(infernalstack); + MTE.lEUt *= 8L; + MTE.mMaxProgresstime *= mods * InfernalMobsCore.instance() + .getMobModHealthFactor(); } - } - ArrayList<ItemStack> infernalstacks = null; - if (p > 0) if (p == 1) { - infernalstacks = InfernalHelper.getDropIdListElite(); - mods = InfernalHelper.getMinEliteModifiers(); - } else if (p == 2) { - infernalstacks = InfernalHelper.getDropIdListUltra(); - mods = InfernalHelper.getMinUltraModifiers(); - } else { - infernalstacks = InfernalHelper.getDropIdListInfernal(); - mods = InfernalHelper.getMinInfernoModifiers(); - } - if (infernalstacks != null) { - ItemStack infernalstack = infernalstacks.get(rnd.nextInt(infernalstacks.size())) - .copy(); - // noinspection ConstantConditions - EnchantmentHelper.addRandomEnchantment( - rnd, - infernalstack, - infernalstack.getItem() - .getItemEnchantability()); - stacks.add(infernalstack); - MTE.lEUt *= 8L; - MTE.mMaxProgresstime *= mods * InfernalMobsCore.instance() - .getMobModHealthFactor(); } } @@ -685,32 +702,6 @@ public class MobRecipeLoader { long time = System.currentTimeMillis(); - Method setSlimeSize; - Method dropFewItems; - Method dropRareDrop; - Method addRandomArmor; - Method enchantEquipment; - Field rand; - - try { - setSlimeSize = EntitySlime.class.getDeclaredMethod(setSlimeSizeName, int.class); - setSlimeSize.setAccessible(true); - dropFewItems = EntityLivingBase.class.getDeclaredMethod(dropFewItemsName, boolean.class, int.class); - dropFewItems.setAccessible(true); - dropRareDrop = EntityLivingBase.class.getDeclaredMethod(dropRareDropName, int.class); - dropRareDrop.setAccessible(true); - addRandomArmor = EntityLiving.class.getDeclaredMethod(addRandomArmorName); - addRandomArmor.setAccessible(true); - enchantEquipment = EntityLiving.class.getDeclaredMethod(enchantEquipmentName); - enchantEquipment.setAccessible(true); - rand = Entity.class.getDeclaredField(randName); - rand.setAccessible(true); - } catch (Exception ex) { - LOG.error("Failed to obtain methods"); - isInGenerationProcess = false; - return; - } - dropCollector collector = new dropCollector(); // Stupid MC code, I need to cast myself @@ -758,372 +749,299 @@ public class MobRecipeLoader { // POWERFULL GENERATION - e.captureDrops = true; - - if (e instanceof EntitySlime) try { - setSlimeSize.invoke(e, 1); - } catch (Exception ex) { - ex.printStackTrace(); - return; - } - try { - rand.set(e, frand); - } catch (Exception ex) { - ex.printStackTrace(); - return; - } - droplist drops = new droplist(); - droplist raredrops = new droplist(); - droplist superraredrops = new droplist(); - droplist additionaldrops = new droplist(); - droplist dropslooting = new droplist(); + e.captureDrops = true; - frand.newRound(); - collector.newRound(); + if (e instanceof EntitySlime) ((EntitySlimeAccessor) e).callSetSlimeSize(1); - if (v.getName() - .startsWith("com.emoniph.witchery")) { - try { - dropFewItems.invoke(e, true, 0); - } catch (Exception ex) { - ex.printStackTrace(); - return; - } - frand.newRound(); - frand.exceptionOnEnchantTry = true; - boolean enchantmentDetected = false; - try { - dropFewItems.invoke(e, true, 0); - } catch (Exception ex) { - enchantmentDetected = true; - } - int w = frand.walkCounter; - frand.newRound(); - if (enchantmentDetected) { - frand.maxWalkCount = w; - collector.booksAlwaysRandomlyEnchanted = true; - } - e.capturedDrops.clear(); - } + ((EntityAccessor) e).setRand(frand); - boolean second = false; - do { - try { - dropFewItems.invoke(e, true, 0); - } catch (Exception ex) { - ex.printStackTrace(); - return; - } - collector.addDrop(drops, e.capturedDrops, frand.chance); - - if (second && frand.chance < 0.0000001d) { - LOG.warn("Skipping " + k + " normal dropmap because it's too randomized"); - break; - } - second = true; + droplist drops = new droplist(); + droplist raredrops = new droplist(); + droplist superraredrops = new droplist(); + droplist additionaldrops = new droplist(); + droplist dropslooting = new droplist(); + droplist dropscustom = new droplist(); - } while (frand.nextRound()); - - frand.newRound(); - collector.newRound(); - - if (v.getName() - .startsWith("com.emoniph.witchery")) { - try { - dropFewItems.invoke(e, true, 0); - } catch (Exception ex) { - ex.printStackTrace(); - return; - } frand.newRound(); - frand.exceptionOnEnchantTry = true; - boolean enchantmentDetected = false; - try { - dropFewItems.invoke(e, true, 0); - } catch (Exception ex) { - enchantmentDetected = true; - } - int w = frand.walkCounter; - frand.newRound(); - if (enchantmentDetected) { - frand.maxWalkCount = w; - collector.booksAlwaysRandomlyEnchanted = true; - } - e.capturedDrops.clear(); - } - - second = false; - do { - try { - dropFewItems.invoke(e, true, 1); - } catch (Exception ex) { - ex.printStackTrace(); - return; - } - collector.addDrop(dropslooting, e.capturedDrops, frand.chance); - - if (second && frand.chance < 0.0000001d) { - LOG.warn("Skipping " + k + " normal dropmap because it's too randomized"); - break; - } - second = true; + collector.newRound(); + + Runnable checkForWitchery = () -> { + if (v.getName() + .startsWith("com.emoniph.witchery")) { + ((EntityLivingBaseAccessor) e).callDropFewItems(true, 0); + frand.newRound(); + frand.exceptionOnEnchantTry = true; + boolean enchantmentDetected = false; + try { + ((EntityLivingBaseAccessor) e).callDropFewItems(true, 0); + } catch (Exception ex) { + enchantmentDetected = true; + } + int w = frand.walkCounter; + frand.newRound(); + if (enchantmentDetected) { + frand.maxWalkCount = w; + collector.booksAlwaysRandomlyEnchanted = true; + } + e.capturedDrops.clear(); + } + }; - } while (frand.nextRound()); + TriConsumer<Supplier<Boolean>, droplist, String> doTheDrop = (callerCanceller, dList, dListName) -> { + boolean second = false; + do { + if (!callerCanceller.get()) break; - frand.newRound(); - collector.newRound(); + collector.addDrop(dList, e.capturedDrops, frand.chance); - second = false; - do { - try { - dropRareDrop.invoke(e, 0); - } catch (Exception ex) { - ex.printStackTrace(); - return; - } - collector.addDrop(raredrops, e.capturedDrops, frand.chance); + if (second && frand.chance < 0.0000001d) { + LOG.warn("Skipping " + k + " " + dListName + " dropmap because it's too randomized"); + break; + } + second = true; + + } while (frand.nextRound()); + + frand.newRound(); + collector.newRound(); + }; + + checkForWitchery.run(); + + doTheDrop.accept(() -> { + ((EntityLivingBaseAccessor) e).callDropFewItems(true, 0); + return true; + }, drops, "normal"); + + checkForWitchery.run(); + + doTheDrop.accept(() -> { + ((EntityLivingBaseAccessor) e).callDropFewItems(true, 1); + return true; + }, dropslooting, "normal"); + + doTheDrop.accept(() -> { + ((EntityLivingBaseAccessor) e).callDropRareDrop(0); + return true; + }, raredrops, "rare"); + + doTheDrop.accept(() -> { + ((EntityLivingBaseAccessor) e).callDropRareDrop(1); + return true; + }, superraredrops, "rare"); + + if (registeringWitherSkeleton && e instanceof EntitySkeleton && k.equals("witherSkeleton")) { + dropinstance i = new dropinstance(new ItemStack(Items.stone_sword), additionaldrops); + i.isDamageRandomized = true; + int maxdamage = i.stack.getMaxDamage(); + int max = Math.max(maxdamage - 25, 1); + for (int d = Math.min(max, 25); d <= max; d++) i.damagesPossible.put(d, 1); + additionaldrops.add(i, 1d); + } else try { + Class<?> cl = e.getClass(); + boolean detectedException; + do { + detectedException = false; + try { + cl.getDeclaredMethod(addRandomArmorName); + } catch (Exception ex) { + detectedException = true; + cl = cl.getSuperclass(); + } + } while (detectedException && !cl.equals(Entity.class)); + if (cl.equals(EntityLiving.class) || cl.equals(Entity.class)) throw new Exception(); + cl = e.getClass(); + do { + detectedException = false; + try { + cl.getDeclaredMethod(enchantEquipmentName); + } catch (Exception ex) { + detectedException = true; + cl = cl.getSuperclass(); + } + } while (detectedException && !cl.equals(EntityLiving.class)); + boolean usingVanillaEnchantingMethod = cl.equals(EntityLiving.class); + double chanceModifierLocal = 1f; + if (v.getName() + .startsWith("twilightforest.entity")) { + frand.forceFloatValue = 0f; + chanceModifierLocal = 0.25f; + } + boolean second = false; + do { + ((EntityLivingAccessor) e).callAddRandomArmor(); + if (!usingVanillaEnchantingMethod) ((EntityLivingAccessor) e).callEnchantEquipment(); + ItemStack[] lastActiveItems = e.getLastActiveItems(); + for (int j = 0, lastActiveItemsLength = lastActiveItems.length; j + < lastActiveItemsLength; j++) { + ItemStack stack = lastActiveItems[j]; + if (stack != null) { + if (LoaderReference.Thaumcraft) + if (stack.getItem() instanceof ItemWandCasting) continue; // crashes the game when + // rendering in GUI + + int randomenchant = -1; + if (stack.hasTagCompound() + && stack.stackTagCompound.hasKey(randomEnchantmentDetectedString)) { + randomenchant = stack.stackTagCompound.getInteger(randomEnchantmentDetectedString); + stack.stackTagCompound.removeTag("ench"); + } + dropinstance i = additionaldrops.add( + new dropinstance(stack.copy(), additionaldrops), + frand.chance * chanceModifierLocal + * (usingVanillaEnchantingMethod ? (j == 0 ? 0.75d : 0.5d) : 1d)); + if (!i.isDamageRandomized && i.stack.isItemStackDamageable()) { + i.isDamageRandomized = true; + int maxdamage = i.stack.getMaxDamage(); + int max = Math.max(maxdamage - 25, 1); + for (int d = Math.min(max, 25); d <= max; d++) i.damagesPossible.put(d, 1); + } + if (!i.isEnchatmentRandomized && randomenchant != -1) { + i.isEnchatmentRandomized = true; + i.enchantmentLevel = randomenchant; + } + if (usingVanillaEnchantingMethod) { + if (!stack.hasTagCompound()) stack.stackTagCompound = new NBTTagCompound(); + stack.stackTagCompound.setInteger(randomEnchantmentDetectedString, 14); + dropinstance newdrop = additionaldrops.add( + new dropinstance(stack.copy(), additionaldrops), + frand.chance * chanceModifierLocal * (j == 0 ? 0.25d : 0.5d)); + newdrop.isEnchatmentRandomized = true; + newdrop.enchantmentLevel = 14; + newdrop.isDamageRandomized = i.isDamageRandomized; + newdrop.damagesPossible = (HashMap<Integer, Integer>) i.damagesPossible.clone(); + } + } + } + Arrays.fill(e.getLastActiveItems(), null); - if (second && frand.chance < 0.0000001d) { - LOG.warn("Skipping " + k + " rare dropmap because it's too randomized"); - break; - } - second = true; + if (second && frand.chance < 0.0000001d) { + LOG.warn("Skipping " + k + " additional dropmap because it's too randomized"); + break; + } + second = true; - } while (frand.nextRound()); + } while (frand.nextRound()); + } catch (Exception ignored) {} - frand.newRound(); - collector.newRound(); + frand.newRound(); + collector.newRound(); - second = false; - do { - try { - dropRareDrop.invoke(e, 1); - } catch (Exception ex) { - ex.printStackTrace(); + if (drops.isEmpty() && raredrops.isEmpty() && additionaldrops.isEmpty()) { + ArrayList<MobDrop> arr = new ArrayList<>(); + GeneralMobList.put(k, new GeneralMappedMob(e, MobRecipe.generateMobRecipe(e, k, arr), arr)); + LOG.info("Mapped " + k); return; } - collector.addDrop(superraredrops, e.capturedDrops, frand.chance); - - if (second && frand.chance < 0.0000001d) { - LOG.warn("Skipping " + k + " rare dropmap because it's too randomized"); - break; - } - second = true; - - } while (frand.nextRound()); - frand.newRound(); - collector.newRound(); + ArrayList<MobDrop> moboutputs = new ArrayList<>( + drops.size() + raredrops.size() + additionaldrops.size()); - if (registeringWitherSkeleton && e instanceof EntitySkeleton && k.equals("witherSkeleton")) { - dropinstance i = new dropinstance(new ItemStack(Items.stone_sword), additionaldrops); - i.isDamageRandomized = true; - int maxdamage = i.stack.getMaxDamage(); - int max = Math.max(maxdamage - 25, 1); - for (int d = Math.min(max, 25); d <= max; d++) i.damagesPossible.put(d, 1); - additionaldrops.add(i, 1d); - } else try { - Class<?> cl = e.getClass(); - boolean detectedException; - do { - detectedException = false; - try { - cl.getDeclaredMethod(addRandomArmorName); - } catch (Exception ex) { - detectedException = true; - cl = cl.getSuperclass(); + for (dropinstance drop : drops.drops) { + ItemStack stack = drop.stack; + if (stack.hasTagCompound()) stack.stackTagCompound.removeTag(randomEnchantmentDetectedString); + int chance = drop.getchance(10000); + if (chance > 10000) { + int div = (int) Math.ceil(chance / 10000d); + stack.stackSize *= div; + chance /= div; } - } while (detectedException && !cl.equals(Entity.class)); - if (cl.equals(EntityLiving.class) || cl.equals(Entity.class)) throw new Exception(); - cl = e.getClass(); - do { - detectedException = false; - try { - cl.getDeclaredMethod(enchantEquipmentName); - } catch (Exception ex) { - detectedException = true; - cl = cl.getSuperclass(); + if (chance == 0) { + LOG.warn("Detected 0% loot, setting to 0.01%"); + chance = 1; } - } while (detectedException && !cl.equals(EntityLiving.class)); - boolean usingVanillaEnchantingMethod = cl.equals(EntityLiving.class); - double chanceModifierLocal = 1f; - if (v.getName() - .startsWith("twilightforest.entity")) { - frand.forceFloatValue = 0f; - chanceModifierLocal = 0.25f; + dropinstance dlooting = dropslooting.get(drop); + moboutputs.add( + new MobDrop( + stack, + MobDrop.DropType.Normal, + chance, + drop.isEnchatmentRandomized ? drop.enchantmentLevel : null, + drop.isDamageRandomized ? drop.damagesPossible : null, + dlooting != null && dlooting.dropcount > drop.dropcount, + false)); } - second = false; - do { - addRandomArmor.invoke(e); - if (!usingVanillaEnchantingMethod) enchantEquipment.invoke(e); - ItemStack[] lastActiveItems = e.getLastActiveItems(); - for (int j = 0, lastActiveItemsLength = lastActiveItems.length; j < lastActiveItemsLength; j++) { - ItemStack stack = lastActiveItems[j]; - if (stack != null) { - if (LoaderReference.Thaumcraft) if (stack.getItem() instanceof ItemWandCasting) continue; // crashes - // the - // game - // when - // rendering - // in - // GUI - - int randomenchant = -1; - if (stack.hasTagCompound() - && stack.stackTagCompound.hasKey(randomEnchantmentDetectedString)) { - randomenchant = stack.stackTagCompound.getInteger(randomEnchantmentDetectedString); - stack.stackTagCompound.removeTag("ench"); - } - dropinstance i = additionaldrops.add( - new dropinstance(stack.copy(), additionaldrops), - frand.chance * chanceModifierLocal - * (usingVanillaEnchantingMethod ? (j == 0 ? 0.75d : 0.5d) : 1d)); - if (!i.isDamageRandomized && i.stack.isItemStackDamageable()) { - i.isDamageRandomized = true; - int maxdamage = i.stack.getMaxDamage(); - int max = Math.max(maxdamage - 25, 1); - for (int d = Math.min(max, 25); d <= max; d++) i.damagesPossible.put(d, 1); - } - if (!i.isEnchatmentRandomized && randomenchant != -1) { - i.isEnchatmentRandomized = true; - i.enchantmentLevel = randomenchant; - } - if (usingVanillaEnchantingMethod) { - if (!stack.hasTagCompound()) stack.stackTagCompound = new NBTTagCompound(); - stack.stackTagCompound.setInteger(randomEnchantmentDetectedString, 14); - dropinstance newdrop = additionaldrops.add( - new dropinstance(stack.copy(), additionaldrops), - frand.chance * chanceModifierLocal * (j == 0 ? 0.25d : 0.5d)); - newdrop.isEnchatmentRandomized = true; - newdrop.enchantmentLevel = 14; - newdrop.isDamageRandomized = i.isDamageRandomized; - newdrop.damagesPossible = (HashMap<Integer, Integer>) i.damagesPossible.clone(); - } - } + for (dropinstance drop : raredrops.drops) { + ItemStack stack = drop.stack; + if (stack.hasTagCompound()) stack.stackTagCompound.removeTag(randomEnchantmentDetectedString); + int chance = drop.getchance(250); + if (chance > 10000) { + int div = (int) Math.ceil(chance / 10000d); + stack.stackSize *= div; + chance /= div; } - Arrays.fill(e.getLastActiveItems(), null); - - if (second && frand.chance < 0.0000001d) { - LOG.warn("Skipping " + k + " additional dropmap because it's too randomized"); - break; + if (chance == 0) { + LOG.warn("Detected 0% loot, setting to 0.01%"); + chance = 1; } - second = true; - - } while (frand.nextRound()); - } catch (Exception ignored) {} - - frand.newRound(); - collector.newRound(); - - if (drops.isEmpty() && raredrops.isEmpty() && additionaldrops.isEmpty()) { - ArrayList<MobDrop> arr = new ArrayList<>(); - GeneralMobList.put(k, new GeneralMappedMob(e, MobRecipe.generateMobRecipe(e, k, arr), arr)); - LOG.info("Mapped " + k); - return; - } - - ArrayList<MobDrop> moboutputs = new ArrayList<>(drops.size() + raredrops.size() + additionaldrops.size()); - - for (dropinstance drop : drops.drops) { - ItemStack stack = drop.stack; - if (stack.hasTagCompound()) stack.stackTagCompound.removeTag(randomEnchantmentDetectedString); - int chance = drop.getchance(10000); - if (chance > 10000) { - int div = (int) Math.ceil(chance / 10000d); - stack.stackSize *= div; - chance /= div; - } - if (chance == 0) { - LOG.warn("Detected 0% loot, setting to 0.01%"); - chance = 1; - } - dropinstance dlooting = dropslooting.get(drop); - moboutputs.add( - new MobDrop( - stack, - MobDrop.DropType.Normal, - chance, - drop.isEnchatmentRandomized ? drop.enchantmentLevel : null, - drop.isDamageRandomized ? drop.damagesPossible : null, - dlooting != null && dlooting.dropcount > drop.dropcount, - false)); - } - for (dropinstance drop : raredrops.drops) { - ItemStack stack = drop.stack; - if (stack.hasTagCompound()) stack.stackTagCompound.removeTag(randomEnchantmentDetectedString); - int chance = drop.getchance(250); - if (chance > 10000) { - int div = (int) Math.ceil(chance / 10000d); - stack.stackSize *= div; - chance /= div; - } - if (chance == 0) { - LOG.warn("Detected 0% loot, setting to 0.01%"); - chance = 1; - } - moboutputs.add( - new MobDrop( - stack, - MobDrop.DropType.Rare, - chance, - drop.isEnchatmentRandomized ? drop.enchantmentLevel : null, - drop.isDamageRandomized ? drop.damagesPossible : null, - false, - false)); - } - for (dropinstance drop : superraredrops.drops) { - if (raredrops.contains(drop)) continue; - ItemStack stack = drop.stack; - if (stack.hasTagCompound()) stack.stackTagCompound.removeTag(randomEnchantmentDetectedString); - int chance = drop.getchance(50); - if (chance > 10000) { - int div = (int) Math.ceil(chance / 10000d); - stack.stackSize *= div; - chance /= div; + moboutputs.add( + new MobDrop( + stack, + MobDrop.DropType.Rare, + chance, + drop.isEnchatmentRandomized ? drop.enchantmentLevel : null, + drop.isDamageRandomized ? drop.damagesPossible : null, + false, + false)); } - if (chance == 0) { - LOG.warn("Detected 0% loot, setting to 0.01%"); - chance = 1; - } - moboutputs.add( - new MobDrop( - stack, - MobDrop.DropType.Rare, - chance, - drop.isEnchatmentRandomized ? drop.enchantmentLevel : null, - drop.isDamageRandomized ? drop.damagesPossible : null, - false, - false)); - } - for (dropinstance drop : additionaldrops.drops) { - ItemStack stack = drop.stack; - if (stack.hasTagCompound()) stack.stackTagCompound.removeTag(randomEnchantmentDetectedString); - int chance = drop.getchance(850); - if (chance > 10000) { - int div = (int) Math.ceil(chance / 10000d); - stack.stackSize *= div; - chance /= div; + for (dropinstance drop : superraredrops.drops) { + if (raredrops.contains(drop)) continue; + ItemStack stack = drop.stack; + if (stack.hasTagCompound()) stack.stackTagCompound.removeTag(randomEnchantmentDetectedString); + int chance = drop.getchance(50); + if (chance > 10000) { + int div = (int) Math.ceil(chance / 10000d); + stack.stackSize *= div; + chance /= div; + } + if (chance == 0) { + LOG.warn("Detected 0% loot, setting to 0.01%"); + chance = 1; + } + moboutputs.add( + new MobDrop( + stack, + MobDrop.DropType.Rare, + chance, + drop.isEnchatmentRandomized ? drop.enchantmentLevel : null, + drop.isDamageRandomized ? drop.damagesPossible : null, + false, + false)); } - if (chance == 0) { - LOG.warn("Detected 0% loot, setting to 0.01%"); - chance = 1; + for (dropinstance drop : additionaldrops.drops) { + ItemStack stack = drop.stack; + if (stack.hasTagCompound()) stack.stackTagCompound.removeTag(randomEnchantmentDetectedString); + int chance = drop.getchance(850); + if (chance > 10000) { + int div = (int) Math.ceil(chance / 10000d); + stack.stackSize *= div; + chance /= div; + } + if (chance == 0) { + LOG.warn("Detected 0% loot, setting to 0.01%"); + chance = 1; + } + moboutputs.add( + new MobDrop( + stack, + MobDrop.DropType.Additional, + chance, + drop.isEnchatmentRandomized ? drop.enchantmentLevel : null, + drop.isDamageRandomized ? drop.damagesPossible : null, + false, + false)); } - moboutputs.add( - new MobDrop( - stack, - MobDrop.DropType.Additional, - chance, - drop.isEnchatmentRandomized ? drop.enchantmentLevel : null, - drop.isDamageRandomized ? drop.damagesPossible : null, - false, - false)); - } - GeneralMobList.put(k, new GeneralMappedMob(e, MobRecipe.generateMobRecipe(e, k, moboutputs), moboutputs)); + GeneralMobList + .put(k, new GeneralMappedMob(e, MobRecipe.generateMobRecipe(e, k, moboutputs), moboutputs)); - LOG.info("Mapped " + k); + LOG.info("Mapped " + k); + } catch (Exception ex) { + LOG.error("Something went wrong while generating recipes for " + k + ", stacktrace: "); + ex.printStackTrace(); + } }); if (registeringWitherSkeleton) stringToClassMapping.remove("witherSkeleton"); @@ -1157,6 +1075,96 @@ public class MobRecipeLoader { } } + @SuppressWarnings("UnstableApiUsage") + public static File makeCustomDrops() { + + dropCollector collector = new dropCollector(); + Map<String, OverridesConfig.MobOverride> customDropsMap = new HashMap<>(); + isInGenerationProcess = true; + + for (Map.Entry<String, GeneralMappedMob> mob : GeneralMobList.entrySet()) { + EntityLiving e = mob.getValue().mob; + String k = mob.getKey(); + + // There is high probability that the hooks are using custom random class + // so we are just going to do approximation by calling it 10k times :LoL: + ((EntityAccessor) e).setRand(new FastRandom()); + e.worldObj.isRemote = false; + + FakePlayer fp = new FakePlayer( + FMLCommonHandler.instance() + .getMinecraftServerInstance().worldServers[0], + new GameProfile( + UUID.nameUUIDFromBytes("[MobRecipeLoader]".getBytes(StandardCharsets.UTF_8)), + "[MobRecipeLoader]")); + + droplist dropscustom = new droplist(); + + try { + + for (int i = 0; i < 10000; i++) { + if (ForgeHooks + .onLivingDrops(e, DamageSource.causePlayerDamage(fp), e.capturedDrops, 0, true, 100)) { + LOG.warn("Event onLivingDrops for " + k + " has been cancelled, I am gonna ignore that!"); + break; + } + collector.addDrop(dropscustom, e.capturedDrops, 1.d / 10000.d); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + e.worldObj.isRemote = true; + + collector.newRound(); + + if (!dropscustom.isEmpty()) { + OverridesConfig.MobOverride override = new OverridesConfig.MobOverride(); + for (dropinstance drop : dropscustom.drops) { + ItemStack stack = drop.stack; + if (stack.hasTagCompound()) stack.stackTagCompound.removeTag(randomEnchantmentDetectedString); + int chance = drop.getchance(10000); + if (chance > 10000) { + int div = (int) Math.ceil(chance / 10000d); + stack.stackSize *= div; + chance /= div; + } + override.additions.add( + new MobDrop( + drop.stack, + MobDrop.DropType.Normal, + chance, + drop.isEnchatmentRandomized ? drop.enchantmentLevel : null, + drop.isDamageRandomized ? drop.damagesPossible : null, + false, + false)); + customDropsMap.put(k, override); + } + } + + } + + Writer writer = null; + Gson gson = GSONUtils.GSON_BUILDER_PRETTY.create(); + File f = new File("CustomDropsMap.json"); + try { + writer = Files.newWriter(f, StandardCharsets.UTF_8); + gson.toJson(customDropsMap, writer); + writer.flush(); + } catch (Exception ex) { + ex.printStackTrace(); + f = null; + } finally { + if (writer != null) try { + writer.close(); + } catch (Exception ignored) {} + } + + isInGenerationProcess = false; + + return f; + } + public static void processMobRecipeMap() { LOG.info("Loading config"); @@ -1216,7 +1224,7 @@ public class MobRecipeLoader { @SideOnly(Side.CLIENT) public static void processMobRecipeMap(HashSet<String> mobs, HashMap<String, OverridesConfig.MobOverride> overrides) { - if (isClientSided) Mob_Handler.clearRecipes(); + Mob_Handler.clearRecipes(); MobNameToRecipeMap.clear(); mobs.forEach(k -> { GeneralMappedMob v = GeneralMobList.get(k); diff --git a/src/main/java/kubatech/mixin/Mixin.java b/src/main/java/kubatech/mixin/Mixin.java index 74f0ca5263..19e3b32610 100644 --- a/src/main/java/kubatech/mixin/Mixin.java +++ b/src/main/java/kubatech/mixin/Mixin.java @@ -12,7 +12,17 @@ public enum Mixin { // Minecraft EnchantmentHelperMixin("minecraft.EnchantmentHelperMixin", VANILLA), - WorldMixin("minecraft.WorldMixin", VANILLA); + WorldMixin("minecraft.WorldMixin", VANILLA), + EntityAccessor("minecraft.EntityAccessor", VANILLA), + EntityLivingAccessor("minecraft.EntityLivingAccessor", VANILLA), + EntityLivingBaseAccessor("minecraft.EntityLivingBaseAccessor", VANILLA), + EntitySlimeAccessor("minecraft.EntitySlimeAccessor", VANILLA), + RendererLivingEntityAccessor("minecraft.RendererLivingEntityAccessor", VANILLA), + StringTranslateMixin("minecraft.StringTranslateMixin", VANILLA), + LanguageRegistryMixin("minecraft.LanguageRegistryMixin", VANILLA), + LocaleMixin("minecraft.LocaleMixin", Side.CLIENT, VANILLA), + + ; public final String mixinClass; public final List<TargetedMod> targetedMods; diff --git a/src/main/java/kubatech/mixin/MixinsVariablesHelper.java b/src/main/java/kubatech/mixin/MixinsVariablesHelper.java new file mode 100644 index 0000000000..1875be6efc --- /dev/null +++ b/src/main/java/kubatech/mixin/MixinsVariablesHelper.java @@ -0,0 +1,6 @@ +package kubatech.mixin; + +public class MixinsVariablesHelper { + + public static String currentlyTranslating = null; +} diff --git a/src/main/java/kubatech/mixin/TargetedMod.java b/src/main/java/kubatech/mixin/TargetedMod.java index 6152a91236..65717c7e12 100644 --- a/src/main/java/kubatech/mixin/TargetedMod.java +++ b/src/main/java/kubatech/mixin/TargetedMod.java @@ -6,7 +6,9 @@ import com.google.common.io.Files; public enum TargetedMod { - VANILLA("Minecraft", "unused", true),; + VANILLA("Minecraft", "unused", true), + + ; public final String modName; public final String jarNamePrefixLowercase; diff --git a/src/main/java/kubatech/mixin/mixins/minecraft/EntityAccessor.java b/src/main/java/kubatech/mixin/mixins/minecraft/EntityAccessor.java new file mode 100644 index 0000000000..67e42acf6a --- /dev/null +++ b/src/main/java/kubatech/mixin/mixins/minecraft/EntityAccessor.java @@ -0,0 +1,15 @@ +package kubatech.mixin.mixins.minecraft; + +import java.util.Random; + +import net.minecraft.entity.Entity; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(value = Entity.class) +public interface EntityAccessor { + + @Accessor + void setRand(Random rand); +} diff --git a/src/main/java/kubatech/mixin/mixins/minecraft/EntityLivingAccessor.java b/src/main/java/kubatech/mixin/mixins/minecraft/EntityLivingAccessor.java new file mode 100644 index 0000000000..59f8ea66d6 --- /dev/null +++ b/src/main/java/kubatech/mixin/mixins/minecraft/EntityLivingAccessor.java @@ -0,0 +1,16 @@ +package kubatech.mixin.mixins.minecraft; + +import net.minecraft.entity.EntityLiving; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +@Mixin(value = EntityLiving.class) +public interface EntityLivingAccessor { + + @Invoker + void callAddRandomArmor(); + + @Invoker + void callEnchantEquipment(); +} diff --git a/src/main/java/kubatech/mixin/mixins/minecraft/EntityLivingBaseAccessor.java b/src/main/java/kubatech/mixin/mixins/minecraft/EntityLivingBaseAccessor.java new file mode 100644 index 0000000000..95370b872b --- /dev/null +++ b/src/main/java/kubatech/mixin/mixins/minecraft/EntityLivingBaseAccessor.java @@ -0,0 +1,16 @@ +package kubatech.mixin.mixins.minecraft; + +import net.minecraft.entity.EntityLivingBase; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +@Mixin(value = EntityLivingBase.class) +public interface EntityLivingBaseAccessor { + + @Invoker + void callDropFewItems(boolean recentlyHit, int lootingLevel); + + @Invoker + void callDropRareDrop(int lootingLevel); +} diff --git a/src/main/java/kubatech/mixin/mixins/minecraft/EntitySlimeAccessor.java b/src/main/java/kubatech/mixin/mixins/minecraft/EntitySlimeAccessor.java new file mode 100644 index 0000000000..d7b4eaafb8 --- /dev/null +++ b/src/main/java/kubatech/mixin/mixins/minecraft/EntitySlimeAccessor.java @@ -0,0 +1,13 @@ +package kubatech.mixin.mixins.minecraft; + +import net.minecraft.entity.monster.EntitySlime; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +@Mixin(value = EntitySlime.class) +public interface EntitySlimeAccessor { + + @Invoker + void callSetSlimeSize(int size); +} diff --git a/src/main/java/kubatech/mixin/mixins/minecraft/LanguageRegistryMixin.java b/src/main/java/kubatech/mixin/mixins/minecraft/LanguageRegistryMixin.java new file mode 100644 index 0000000000..77d99d2ab4 --- /dev/null +++ b/src/main/java/kubatech/mixin/mixins/minecraft/LanguageRegistryMixin.java @@ -0,0 +1,27 @@ +package kubatech.mixin.mixins.minecraft; + +import static kubatech.mixin.MixinsVariablesHelper.currentlyTranslating; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import cpw.mods.fml.common.ModContainer; +import cpw.mods.fml.common.registry.LanguageRegistry; +import cpw.mods.fml.relauncher.Side; + +@SuppressWarnings("unused") +@Mixin(value = LanguageRegistry.class) +public class LanguageRegistryMixin { + + @Inject(method = "loadLanguagesFor", at = @At(value = "HEAD"), remap = false, require = 1) + private void loadLanguagesForHEAD(ModContainer container, Side side, CallbackInfo callbackInfo) { + currentlyTranslating = container.getModId(); + } + + @Inject(method = "loadLanguagesFor", at = @At(value = "RETURN"), remap = false, require = 1) + private void loadLanguagesForRETURN(ModContainer container, Side side, CallbackInfo callbackInfo) { + currentlyTranslating = null; + } +} diff --git a/src/main/java/kubatech/mixin/mixins/minecraft/LocaleMixin.java b/src/main/java/kubatech/mixin/mixins/minecraft/LocaleMixin.java new file mode 100644 index 0000000000..e591878c32 --- /dev/null +++ b/src/main/java/kubatech/mixin/mixins/minecraft/LocaleMixin.java @@ -0,0 +1,47 @@ +package kubatech.mixin.mixins.minecraft; + +import static kubatech.mixin.MixinsVariablesHelper.currentlyTranslating; + +import java.util.regex.Matcher; + +import net.minecraft.client.resources.Locale; +import net.minecraft.util.ResourceLocation; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyArg; +import org.spongepowered.asm.mixin.injection.Redirect; + +import kubatech.Tags; + +@SuppressWarnings("unused") +@Mixin(value = Locale.class) +public class LocaleMixin { + + @ModifyArg( + method = "loadLocaleDataFiles", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/resources/IResourceManager;getAllResources(Lnet/minecraft/util/ResourceLocation;)Ljava/util/List;"), + index = 0, + require = 1) + private ResourceLocation loadLocaleDataFiles(ResourceLocation resourceLocation) { + currentlyTranslating = resourceLocation.getResourceDomain(); + return resourceLocation; + } + + @Redirect( + method = "loadLocaleData(Ljava/io/InputStream;)V", + at = @At( + value = "INVOKE", + target = "Ljava/util/regex/Matcher;replaceAll(Ljava/lang/String;)Ljava/lang/String;", + remap = false), + require = 1) + private String replaceAll(Matcher matcher, String replace) { + if (currentlyTranslating != null && currentlyTranslating.equals(Tags.MODID) && matcher.find()) { + return matcher.replaceFirst(matcher.group()); + } + return matcher.replaceAll(replace); + } + +} diff --git a/src/main/java/kubatech/mixin/mixins/minecraft/RendererLivingEntityAccessor.java b/src/main/java/kubatech/mixin/mixins/minecraft/RendererLivingEntityAccessor.java new file mode 100644 index 0000000000..a2fe93954f --- /dev/null +++ b/src/main/java/kubatech/mixin/mixins/minecraft/RendererLivingEntityAccessor.java @@ -0,0 +1,15 @@ +package kubatech.mixin.mixins.minecraft; + +import net.minecraft.client.model.ModelBase; +import net.minecraft.client.renderer.entity.RendererLivingEntity; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(value = RendererLivingEntity.class) +public interface RendererLivingEntityAccessor { + + @Accessor + ModelBase getMainModel(); + +} diff --git a/src/main/java/kubatech/mixin/mixins/minecraft/StringTranslateMixin.java b/src/main/java/kubatech/mixin/mixins/minecraft/StringTranslateMixin.java new file mode 100644 index 0000000000..2841ccc0c0 --- /dev/null +++ b/src/main/java/kubatech/mixin/mixins/minecraft/StringTranslateMixin.java @@ -0,0 +1,33 @@ +package kubatech.mixin.mixins.minecraft; + +import static kubatech.mixin.MixinsVariablesHelper.currentlyTranslating; + +import java.util.regex.Matcher; + +import net.minecraft.util.StringTranslate; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +import kubatech.Tags; + +@SuppressWarnings("unused") +@Mixin(value = StringTranslate.class) +public class StringTranslateMixin { + + @Redirect( + method = "parseLangFile", + at = @At( + value = "INVOKE", + target = "Ljava/util/regex/Matcher;replaceAll(Ljava/lang/String;)Ljava/lang/String;", + remap = false), + remap = false, + require = 1) + private static String replaceAll(Matcher matcher, String replace) { + if (currentlyTranslating != null && currentlyTranslating.equals(Tags.MODID) && matcher.find()) { + return matcher.replaceFirst(matcher.group()); + } + return matcher.replaceAll(replace); + } +} diff --git a/src/main/java/kubatech/mixin/mixins/minecraft/WorldMixin.java b/src/main/java/kubatech/mixin/mixins/minecraft/WorldMixin.java index 4d8aaf10e6..ed28dc60c4 100644 --- a/src/main/java/kubatech/mixin/mixins/minecraft/WorldMixin.java +++ b/src/main/java/kubatech/mixin/mixins/minecraft/WorldMixin.java @@ -5,8 +5,8 @@ import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import com.gtnewhorizon.mixinextras.injector.ModifyReturnValue; import kubatech.loaders.BlockLoader; @@ -15,9 +15,9 @@ import kubatech.loaders.BlockLoader; public class WorldMixin { @SuppressWarnings("ConstantConditions") - @Inject(method = "getBlock", at = @At("RETURN"), require = 1) - private void getBlockDetector(int x, int y, int z, CallbackInfoReturnable<Block> callbackInfoReturnable) { - if (callbackInfoReturnable.getReturnValue() == BlockLoader.kubaBlock) - BlockLoader.kubaBlock.setLastBlockAccess((World) (Object) this, x, y, z); + @ModifyReturnValue(method = "getBlock", at = @At("RETURN"), require = 1) + private Block getBlockDetector(Block block, int x, int y, int z) { + if (block == BlockLoader.kubaBlock) BlockLoader.kubaBlock.setLastBlockAccess((World) (Object) this, x, y, z); + return block; } } diff --git a/src/main/java/kubatech/nei/Mob_Handler.java b/src/main/java/kubatech/nei/Mob_Handler.java index f6e859eceb..10f932ec71 100644 --- a/src/main/java/kubatech/nei/Mob_Handler.java +++ b/src/main/java/kubatech/nei/Mob_Handler.java @@ -38,7 +38,6 @@ import static kubatech.nei.Mob_Handler.Translations.PLAYER_ONLY; import static kubatech.nei.Mob_Handler.Translations.RARE_DROPS; import java.awt.*; -import java.lang.reflect.Field; import java.nio.FloatBuffer; import java.util.ArrayList; import java.util.List; @@ -49,7 +48,6 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.gui.inventory.GuiInventory; import net.minecraft.client.renderer.Tessellator; -import net.minecraft.client.renderer.entity.RendererLivingEntity; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.EntityList; import net.minecraft.entity.EntityLiving; @@ -70,6 +68,7 @@ import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; import org.lwjgl.util.glu.GLU; +import atomicstryker.infernalmobs.common.InfernalMobsCore; import codechicken.lib.gui.GuiDraw; import codechicken.nei.NEIClientUtils; import codechicken.nei.PositionedStack; @@ -85,7 +84,6 @@ import crazypants.enderio.machine.spawner.BlockPoweredSpawner; import gregtech.api.util.GT_Utility; import kubatech.Tags; import kubatech.api.LoaderReference; -import kubatech.api.helpers.InfernalHelper; import kubatech.api.mobhandler.MobDrop; import kubatech.api.utils.FastRandom; import kubatech.api.utils.MobUtils; @@ -232,18 +230,6 @@ public class Mob_Handler extends TemplateRecipeHandler { return "kubatech:textures/gui/MobHandler.png"; } - private static final Field mainmodelfield; - - static { - try { - mainmodelfield = RendererLivingEntity.class - .getDeclaredField(ModUtils.isDeobfuscatedEnvironment ? "mainModel" : "field_77045_g"); - mainmodelfield.setAccessible(true); - } catch (NoSuchFieldException e) { - throw new RuntimeException(e); - } - } - @Override public void drawBackground(int recipe) { GL11.glColor4f(1f, 1f, 1f, 1f); @@ -530,8 +516,8 @@ public class Mob_Handler extends TemplateRecipeHandler { if (isPlayerOnly) { extraTooltip.add(EnumChatFormatting.RESET + PLAYER_ONLY.get()); extraTooltip.add( - EnumChatFormatting.RESET - + EEC_CHANCE.get(((double) chance / 100d) * Config.MobHandler.playerOnlyDropsModifier)); + EnumChatFormatting.RESET + EEC_CHANCE + .get(((double) ((int) ((double) chance * Config.MobHandler.playerOnlyDropsModifier)) / 100d))); } extraTooltip.add(EnumChatFormatting.RESET + AVERAGE_REMINDER.get()); @@ -603,8 +589,9 @@ public class Mob_Handler extends TemplateRecipeHandler { if (!LoaderReference.InfernalMobs) infernaltype = -1; // not supported else { - if (!InfernalHelper.isClassAllowed(mob)) infernaltype = 0; // not allowed - else if (InfernalHelper.checkEntityClassForced(mob)) infernaltype = 2; // forced + InfernalMobsCore infernalMobsCore = InfernalMobsCore.instance(); + if (!infernalMobsCore.isClassAllowed(mob)) infernaltype = 0; // not allowed + else if (infernalMobsCore.checkEntityClassForced(mob)) infernaltype = 2; // forced else infernaltype = 1; // normal } } diff --git a/src/main/java/kubatech/standalone.java b/src/main/java/kubatech/standalone.java new file mode 100644 index 0000000000..4c63a43c7a --- /dev/null +++ b/src/main/java/kubatech/standalone.java @@ -0,0 +1,15 @@ +package kubatech; + +import javax.swing.*; + +public class standalone { + + public static void main(String[] args) { + JOptionPane.showMessageDialog( + null, + "Get some TEA", + "TEA", + JOptionPane.ERROR_MESSAGE, + new ImageIcon(standalone.class.getResource("/assets/kubatech/textures/gui/green_tea.png"))); + } +} diff --git a/src/main/java/kubatech/tileentity/gregtech/multiblock/GT_MetaTileEntity_ExtremeIndustrialGreenhouse.java b/src/main/java/kubatech/tileentity/gregtech/multiblock/GT_MetaTileEntity_ExtremeIndustrialGreenhouse.java index 4b86cda1e7..0c8dab2722 100644 --- a/src/main/java/kubatech/tileentity/gregtech/multiblock/GT_MetaTileEntity_ExtremeIndustrialGreenhouse.java +++ b/src/main/java/kubatech/tileentity/gregtech/multiblock/GT_MetaTileEntity_ExtremeIndustrialGreenhouse.java @@ -31,6 +31,8 @@ import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_FRONT_DISTILLATION_ import static gregtech.api.util.GT_StructureUtility.ofHatchAdder; import static kubatech.api.Variables.Author; import static kubatech.api.Variables.StructureHologram; +import static kubatech.api.utils.ItemUtils.readItemStackFromNBT; +import static kubatech.api.utils.ItemUtils.writeItemStackToNBT; import java.io.IOException; import java.lang.ref.WeakReference; @@ -1097,27 +1099,22 @@ public class GT_MetaTileEntity_ExtremeIndustrialGreenhouse public NBTTagCompound toNBTTagCompound() { NBTTagCompound aNBT = new NBTTagCompound(); - aNBT.setTag("input", input.writeToNBT(new NBTTagCompound())); + aNBT.setTag("input", writeItemStackToNBT(input)); aNBT.setBoolean("isValid", isValid); aNBT.setBoolean("isIC2Crop", isIC2Crop); if (!isIC2Crop) { aNBT.setInteger("crop", Block.getIdFromBlock(crop)); if (customDrops != null && customDrops.size() > 0) { aNBT.setInteger("customDropsCount", customDrops.size()); - for (int i = 0; i < customDrops.size(); i++) aNBT.setTag( - "customDrop." + i, - customDrops.get(i) - .writeToNBT(new NBTTagCompound())); + for (int i = 0; i < customDrops.size(); i++) + aNBT.setTag("customDrop." + i, writeItemStackToNBT(customDrops.get(i))); } aNBT.setInteger("dropscount", drops.size()); - for (int i = 0; i < drops.size(); i++) aNBT.setTag( - "drop." + i, - drops.get(i) - .writeToNBT(new NBTTagCompound())); + for (int i = 0; i < drops.size(); i++) aNBT.setTag("drop." + i, writeItemStackToNBT(drops.get(i))); aNBT.setInteger("optimalgrowth", optimalgrowth); aNBT.setBoolean("needsreplanting", needsreplanting); } else { - if (undercrop != null) aNBT.setTag("undercrop", undercrop.writeToNBT(new NBTTagCompound())); + if (undercrop != null) aNBT.setTag("undercrop", writeItemStackToNBT(undercrop)); aNBT.setInteger("generationscount", generations.size()); for (int i = 0; i < generations.size(); i++) { aNBT.setInteger( @@ -1128,9 +1125,9 @@ public class GT_MetaTileEntity_ExtremeIndustrialGreenhouse .size(); j++) aNBT.setTag( "generation." + i + "." + j, - generations.get(i) - .get(j) - .writeToNBT(new NBTTagCompound())); + writeItemStackToNBT( + generations.get(i) + .get(j))); } aNBT.setInteger("growthticks", growthticks); aNBT.setBoolean("noHumidity", noHumidity); @@ -1142,29 +1139,28 @@ public class GT_MetaTileEntity_ExtremeIndustrialGreenhouse super(null, 3, 3); isIC2Crop = aNBT.getBoolean("isIC2Crop"); isValid = aNBT.getBoolean("isValid"); - input = ItemStack.loadItemStackFromNBT(aNBT.getCompoundTag("input")); + input = readItemStackFromNBT(aNBT.getCompoundTag("input")); if (!isIC2Crop) { crop = Block.getBlockById(aNBT.getInteger("crop")); if (aNBT.hasKey("customDropsCount")) { int imax = aNBT.getInteger("customDropsCount"); customDrops = new ArrayList<>(imax); for (int i = 0; i < imax; i++) - customDrops.add(ItemStack.loadItemStackFromNBT(aNBT.getCompoundTag("customDrop." + i))); + customDrops.add(readItemStackFromNBT(aNBT.getCompoundTag("customDrop." + i))); } drops = new ArrayList<>(); for (int i = 0; i < aNBT.getInteger("dropscount"); i++) - drops.add(ItemStack.loadItemStackFromNBT(aNBT.getCompoundTag("drop." + i))); + drops.add(readItemStackFromNBT(aNBT.getCompoundTag("drop." + i))); optimalgrowth = aNBT.getInteger("optimalgrowth"); if (optimalgrowth == 0) optimalgrowth = 7; if (aNBT.hasKey("needsreplanting")) needsreplanting = aNBT.getBoolean("needsreplanting"); } else { - if (aNBT.hasKey("undercrop")) - undercrop = ItemStack.loadItemStackFromNBT(aNBT.getCompoundTag("undercrop")); + if (aNBT.hasKey("undercrop")) undercrop = readItemStackFromNBT(aNBT.getCompoundTag("undercrop")); generations = new ArrayList<>(); for (int i = 0; i < aNBT.getInteger("generationscount"); i++) { generations.add(new ArrayList<>()); for (int j = 0; j < aNBT.getInteger("generation." + i + ".count"); j++) generations.get(i) - .add(ItemStack.loadItemStackFromNBT(aNBT.getCompoundTag("generation." + i + "." + j))); + .add(readItemStackFromNBT(aNBT.getCompoundTag("generation." + i + "." + j))); } growthticks = aNBT.getInteger("growthticks"); noHumidity = aNBT.getBoolean("noHumidity"); diff --git a/src/main/java/kubatech/tileentity/gregtech/multiblock/GT_MetaTileEntity_MegaIndustrialApiary.java b/src/main/java/kubatech/tileentity/gregtech/multiblock/GT_MetaTileEntity_MegaIndustrialApiary.java index baabda3211..43fea0f182 100644 --- a/src/main/java/kubatech/tileentity/gregtech/multiblock/GT_MetaTileEntity_MegaIndustrialApiary.java +++ b/src/main/java/kubatech/tileentity/gregtech/multiblock/GT_MetaTileEntity_MegaIndustrialApiary.java @@ -38,6 +38,8 @@ import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_FRONT_DISTILLATION_ import static gregtech.api.util.GT_StructureUtility.buildHatchAdder; import static kubatech.api.Variables.StructureHologram; import static kubatech.api.Variables.buildAuthorList; +import static kubatech.api.utils.ItemUtils.readItemStackFromNBT; +import static kubatech.api.utils.ItemUtils.writeItemStackToNBT; import java.io.IOException; import java.lang.ref.WeakReference; @@ -1126,7 +1128,7 @@ public class GT_MetaTileEntity_MegaIndustrialApiary } public BeeSimulator(NBTTagCompound tag) { - queenStack = ItemStack.loadItemStackFromNBT(tag.getCompoundTag("queenStack")); + queenStack = readItemStackFromNBT(tag.getCompoundTag("queenStack")); isValid = tag.getBoolean("isValid"); drops = new ArrayList<>(); specialDrops = new ArrayList<>(); @@ -1151,7 +1153,7 @@ public class GT_MetaTileEntity_MegaIndustrialApiary public NBTTagCompound toNBTTagCompound() { NBTTagCompound tag = new NBTTagCompound(); - tag.setTag("queenStack", queenStack.writeToNBT(new NBTTagCompound())); + tag.setTag("queenStack", writeItemStackToNBT(queenStack)); tag.setBoolean("isValid", isValid); tag.setInteger("dropssize", drops.size()); for (int i = 0; i < drops.size(); i++) tag.setTag( @@ -1244,7 +1246,7 @@ public class GT_MetaTileEntity_MegaIndustrialApiary } public BeeDrop(NBTTagCompound tag) { - stack = ItemStack.loadItemStackFromNBT(tag.getCompoundTag("stack")); + stack = readItemStackFromNBT(tag.getCompoundTag("stack")); chance = tag.getFloat("chance"); beeSpeed = tag.getFloat("beeSpeed"); t = tag.getFloat("t"); @@ -1254,7 +1256,7 @@ public class GT_MetaTileEntity_MegaIndustrialApiary public NBTTagCompound toNBTTagCompound() { NBTTagCompound tag = new NBTTagCompound(); - tag.setTag("stack", stack.writeToNBT(new NBTTagCompound())); + tag.setTag("stack", writeItemStackToNBT(stack)); tag.setFloat("chance", chance); tag.setFloat("beeSpeed", beeSpeed); tag.setFloat("t", t); |