forked from SpongePowered/Sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle last compatibility issue with Architectury API
- Loading branch information
Showing
7 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...rc/mixins/java/dk/nelind/loofah/mixin/compat/architectury/world/level/ExplosionMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* This file is part of Loofah, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) Nelind <https://www.nelind.dk> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package dk.nelind.loofah.mixin.compat.architectury.world.level; | ||
|
||
import net.minecraft.world.level.Explosion; | ||
import net.minecraft.world.level.Level; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.spongepowered.asm.mixin.Debug; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.List; | ||
|
||
/** | ||
* Replaces Architectury's detonate event call with a Loofah/SpongeCommon compatible implementation if | ||
* Architectury is installed | ||
*/ | ||
@Debug(export = true) | ||
@Mixin(Explosion.class) | ||
public class ExplosionMixin { | ||
@Shadow @Final private Level level; | ||
|
||
@Inject( | ||
method = "explode", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/world/phys/Vec3;<init>(DDD)V", | ||
ordinal = 0 | ||
), | ||
locals = LocalCapture.CAPTURE_FAILHARD | ||
) | ||
private void callArchitecturyExplodePostEvent(CallbackInfo ci, float q, int k, int l, int r, int s, int t, int u, List list) { | ||
// Call the event using reflection. It's technically a bit brittle, but I think it'll be fine. | ||
try { | ||
var eventClass = Class.forName("dev.architectury.event.Event"); | ||
var eventInkoverMethod = eventClass.getDeclaredMethod("invoker"); | ||
|
||
var explosionEventClass = Class.forName("dev.architectury.event.events.common.ExplosionEvent"); | ||
var detonateEventClass = Class.forName("dev.architectury.event.events.common.ExplosionEvent$Detonate"); | ||
var detonateEventInstance = explosionEventClass.getField("DETONATE"); | ||
var eventInvoker = eventInkoverMethod.invoke(detonateEventInstance.get(null)); | ||
var eventCallMethod = detonateEventClass.getDeclaredMethod("explode", Level.class, Explosion.class, List.class); | ||
|
||
eventCallMethod.invoke(eventInvoker, this.level, this, list); | ||
LogManager.getLogger().info("Called arch event"); | ||
} catch ( | ||
ClassNotFoundException | | ||
NoSuchFieldException | | ||
NoSuchMethodException | | ||
InvocationTargetException | | ||
IllegalAccessException e | ||
) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
loofah/src/mixins/java/dk/nelind/loofah/mixin/plugin/FabricCompatMixinPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* This file is part of Loofah, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) Nelind <https://www.nelind.dk> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package dk.nelind.loofah.mixin.plugin; | ||
|
||
import net.fabricmc.loader.api.FabricLoader; | ||
import org.spongepowered.common.mixin.plugin.AbstractMixinConfigPlugin; | ||
|
||
public class FabricCompatMixinPlugin extends AbstractMixinConfigPlugin { | ||
@Override | ||
public boolean shouldApplyMixin(final String targetClassName, final String mixinClassName) { | ||
|
||
if ( | ||
mixinClassName.equals("dk.nelind.loofah.mixin.compat.architectury.world.level.ExplosionMixin") && | ||
FabricLoader.getInstance().isModLoaded("architectury") | ||
) { | ||
FabricMixinPlugin.LOGGER.info("Architectury API detected. Enabling compatibility features in Loofah"); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
loofah/src/mixins/java/dk/nelind/loofah/mixin/plugin/FabricMixinAnnotationAdjuster.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* This file is part of Loofah, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) Nelind <https://www.nelind.dk> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package dk.nelind.loofah.mixin.plugin; | ||
|
||
import com.bawnorton.mixinsquared.adjuster.tools.AdjustableAnnotationNode; | ||
import com.bawnorton.mixinsquared.api.MixinAnnotationAdjuster; | ||
import org.objectweb.asm.tree.MethodNode; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
|
||
import java.util.List; | ||
|
||
public class FabricMixinAnnotationAdjuster implements MixinAnnotationAdjuster { | ||
@Override | ||
public AdjustableAnnotationNode adjust(List<String> targetClassNames, String mixinClassName, MethodNode handlerNode, AdjustableAnnotationNode annotationNode) { | ||
// Don't inject Architectury's MixinExplosion inject as the priorities are in the wrong order | ||
// and even if they were in the right order the LVT has been changed by an overwrite in | ||
// SpongeCommon ExplosionMixin and the local capture would fail. Not the pretties thing ever but | ||
// sometimes modding is just inherently ugly. If some other better solution presents itself at some point | ||
// that is mostly likely preferable | ||
// | ||
// See also dk.nelind.loofah.mixin.compat.world.level.ExplosionMixin | ||
if (mixinClassName.equals("dev.architectury.mixin.fabric.MixinExplosion") && annotationNode.is(Inject.class)) { | ||
return null; | ||
} | ||
|
||
return annotationNode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"required": true, | ||
"parent": "mixins.sponge.parent.json", | ||
"package": "dk.nelind.loofah.mixin.compat", | ||
"plugin": "dk.nelind.loofah.mixin.plugin.FabricCompatMixinPlugin", | ||
"mixinPriority": 1301, | ||
"mixins": [ | ||
"architectury.world.level.ExplosionMixin" | ||
], | ||
"overwrites": { | ||
"conformVisibility": true | ||
} | ||
} |