Refactor MixinPlugin and add GregTech example

This commit is contained in:
Johann Bernhardt committed 2021-11-04 19:39:00 +01:00
1 parent 3a34679293
commit bcbad5e546
7 files changed
+108 -24

No files matched your search

@@ -1,5 +1,7 @@
package com.myname.mymodid;
// Use this class for Strings only. Do not import any classes here. It will lead to issues with Mixins if in use!
public class Tags {
// GRADLETOKEN_* will be replaced by your configuration values at build time
@@ -0,0 +1,27 @@
package com.myname.mymodid.mixinplugin;
import static com.myname.mymodid.mixinplugin.TargetedMod.*;
public enum Mixin {
//
// IMPORTANT: Do not make any references to any mod from this file. This file is loaded quite early on and if
// you refer to other mods you load them as well. The consequence is: You can't inject any previously loaded classes!
// Exception: Tags.java, as long as it is used for Strings only!
//
// Replace with your own mixins:
ItemEditableBookMixin("minecraft.ItemEditableBookMixin", VANILLA, false),
GT_Block_Ores_AbstractMixin("gregtech.GT_Block_Ores_AbstractMixin", GREGTECH, false);
public final String mixinClass;
public final TargetedMod targetedMod;
// Injecting into GUI classes will crash the server. Flag them as clientSideOnly!
public final boolean clientSideOnly;
Mixin(String mixinClass, TargetedMod targetedMod, boolean clientSideOnly) {
this.mixinClass = mixinClass;
this.targetedMod = targetedMod;
this.clientSideOnly = clientSideOnly;
}
}
@@ -18,7 +18,7 @@ import java.util.Set;
public class MixinPlugin implements IMixinConfigPlugin {
private static Logger LOG = LogManager.getLogger(Tags.MODID);
private static Logger LOG = LogManager.getLogger(Tags.MODID + " mixins");
@Override
public void onLoad(String mixinPackage) {
@@ -46,27 +46,23 @@ public class MixinPlugin implements IMixinConfigPlugin {
final boolean loadClientSideOnlyClasses = FMLLaunchHandler.side().isClient();
final boolean isDevelopmentEnvironment = (boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment");
//
// IMPORTANT: Do not make any references to any mod from this file. This file is loaded quite early on and if
// you refer to other mods you load them as well. The consequence is: You can't inject any previously loaded classes!
//
List<String> mixins = new ArrayList<>();
// We may add vanilla mixins at all times. Vanilla Minecraft is guaranteed to be on the class path
mixins.add("minecraft.ItemEditableBookMixin");
// If we inject into another mod we need to ensure it is available on the classpath. Some mods are, some are not.
// loadJar() will ensure it is available and return false if a mod is not installed.
// Additionally, we don't have the other mod in /mods during development runs. If you want a mixin to be applied
// at development you should allow its addition with an additional check to isDevelopmentEnvironment. If your
// dependency is optional, eg added as compileOnly dependency, you must skip the test against isDevelopmentEnvironment!
if(isDevelopmentEnvironment || loadJar("gregtech")) {
LOG.info("Found GregTech! Integrating now...");
//mixins.add("gregtech.SOMEGTMIXINEXAMPLE");
}
else {
LOG.info("Could not find GregTech! Skipping integration....");
for (TargetedMod targetedMod : TargetedMod.values()) {
if (targetedMod == TargetedMod.VANILLA
|| (targetedMod.loadInDevelopment && isDevelopmentEnvironment)
|| loadJar(targetedMod.jarNameBeginsWith)) {
LOG.info("Found " + targetedMod.modName + "! Integrating now...");
for (Mixin mixin : Mixin.values()) {
if (mixin.targetedMod == targetedMod
&& (mixin.clientSideOnly == false || loadClientSideOnlyClasses)) {
mixins.add(mixin.mixinClass);
LOG.debug("Loading mixin: " + mixin.mixinClass);
}
}
}
else {
LOG.info("Could not find " + targetedMod.modName + "! Skipping integration....");
}
}
return mixins;
@@ -80,7 +76,7 @@ public class MixinPlugin implements IMixinConfigPlugin {
return false;
}
LOG.info("Attempting to add " + jar.toString() + " to the URL Class Path");
LOG.info("Attempting to add " + jar + " to the URL Class Path");
if(!jar.exists()) {
throw new FileNotFoundException(jar.toString());
}
@@ -0,0 +1,25 @@
package com.myname.mymodid.mixinplugin;
public enum TargetedMod {
//
// IMPORTANT: Do not make any references to any mod from this file. This file is loaded quite early on and if
// you refer to other mods you load them as well. The consequence is: You can't inject any previously loaded classes!
// Exception: Tags.java, as long as it is used for Strings only!
//
// Replace with your injected mods here, but always keep VANILLA:
VANILLA("Minecraft", "unused", true),
GREGTECH("GregTech", "gregtech", false);
public final String modName;
public final String jarNameBeginsWith;
// Optional dependencies can be omitted in development. Especially skipping GT5U will drastically speed up your game start!
public final boolean loadInDevelopment;
TargetedMod(String modName, String jarNameBeginsWith, boolean loadInDevelopment) {
this.modName = modName;
this.jarNameBeginsWith = jarNameBeginsWith;
this.loadInDevelopment = loadInDevelopment;
}
}
@@ -0,0 +1,28 @@
package com.myname.mymodid.mixins.gregtech;
import com.myname.mymodid.MyMod;
import gregtech.api.items.GT_Generic_Block;
import gregtech.common.blocks.GT_Block_Ores_Abstract;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
@Mixin(value = GT_Block_Ores_Abstract.class, remap = false)
public class GT_Block_Ores_AbstractMixin extends GT_Generic_Block {
protected GT_Block_Ores_AbstractMixin() {
super(null, null, null);
}
@Override
public boolean onBlockActivated(World world, int blockX, int blockY, int blockZ, EntityPlayer player, int side, float offsetX, float offsetY, float offsetZ) {
MyMod.info("You touched a GT Ore!");
return super.onBlockActivated(world, blockX, blockY, blockZ, player, side, offsetX, offsetY, offsetZ);
}
@Override
public void onBlockClicked(World world, int blockX, int blockY, int blockZ, EntityPlayer player) {
MyMod.info("You touched a GT Ore!");
super.onBlockClicked(world, blockX, blockY, blockZ, player);
}
}