Skip to content

Commit

Permalink
[1.20.1] Mixin fixes (#626)
Browse files Browse the repository at this point in the history
* Fix CampfireMixin

* Fix EnchantApplyMixin. Refactor EnchantEvent to contain levels spent instead of enchantment details

* Revert "Fix EnchantApplyMixin. Refactor EnchantEvent to contain levels spent instead of enchantment details"

This reverts commit dff1d53.

* Implement MixinPlugin. Use selective mixin loading for dev/prod environments

* Split Campfire Mixin into Dev and Prod variants

* Improve MixinPlugin - filter prod/dev designation and accept default

* Fix EnchantApplyMixin

* update mixin to remove local capture logic

* fix call side to not fail on dedicated server

---------

Co-authored-by: dankos <dankos@nope.com>
Co-authored-by: cubiferous <->
Co-authored-by: Caltinor <62700786+Caltinor@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 22, 2024
1 parent 5918d02 commit 7c49be7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 35 deletions.
26 changes: 26 additions & 0 deletions src/main/java/harmonised/pmmo/mixin/CampfireMixin_Dev.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package harmonised.pmmo.mixin;

import harmonised.pmmo.api.events.FurnaceBurnEvent;
import net.minecraft.core.BlockPos;
import net.minecraft.world.Container;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.CampfireBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.MinecraftForge;
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 org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(CampfireBlockEntity.class)
public class CampfireMixin_Dev {
@Inject(at=@At(value = "INVOKE",
target = "Lnet/minecraft/world/Containers;dropItemStack(Lnet/minecraft/world/level/Level;DDDLnet/minecraft/world/item/ItemStack;)V"),
method = "cookTick(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/entity/CampfireBlockEntity;)V",
locals = LocalCapture.CAPTURE_FAILHARD)
private static void postFurnaceBurnEvent(Level pLevel, BlockPos pPos, BlockState pState, CampfireBlockEntity pBlockEntity, CallbackInfo ci, boolean flag, int i, ItemStack itemstack, int j, Container container, ItemStack itemstack1) {
MinecraftForge.EVENT_BUS.post(new FurnaceBurnEvent(itemstack1, pLevel, pPos));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(CampfireBlockEntity.class)
public class CampfireMixin {
@Inject(at=@At(
value = "INVOKE",
target = "Lnet/minecraft/world/Containers;dropItemStack(Lnet/minecraft/world/level/Level;DDDLnet/minecraft/world/item/ItemStack;)V"),
method = "cookTick(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/entity/CampfireBlockEntity;)V",
locals = LocalCapture.CAPTURE_FAILHARD)
private static void projectmmo$handleCampfireCook(Level pLevel, BlockPos pPos, BlockState pState, CampfireBlockEntity pBlockEntity, CallbackInfo ci, boolean flag, int i, ItemStack itemstack, Container container, ItemStack itemstack1) {
public class CampfireMixin_Prod {
@Inject(at=@At(value = "INVOKE",
target = "Lnet/minecraft/world/Containers;dropItemStack(Lnet/minecraft/world/level/Level;DDDLnet/minecraft/world/item/ItemStack;)V"),
method = "cookTick(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/entity/CampfireBlockEntity;)V",
locals = LocalCapture.CAPTURE_FAILHARD)
private static void postFurnaceBurnEvent(Level pLevel, BlockPos pPos, BlockState pState, CampfireBlockEntity pBlockEntity, CallbackInfo ci, boolean flag, int i, ItemStack itemstack, Container container, ItemStack itemstack1) {
MinecraftForge.EVENT_BUS.post(new FurnaceBurnEvent(itemstack1, pLevel, pPos));
}
}
27 changes: 11 additions & 16 deletions src/main/java/harmonised/pmmo/mixin/EnchantApplyMixin.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
package harmonised.pmmo.mixin;

import java.util.List;

import org.spongepowered.asm.mixin.Debug;
import net.minecraft.world.item.enchantment.EnchantmentInstance;
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 org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import harmonised.pmmo.api.events.EnchantEvent;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.EnchantmentMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentInstance;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.MinecraftForge;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Debug
@Mixin(EnchantmentMenu.class)
public class EnchantApplyMixin {

@Inject(method = "lambda$clickMenuButton$1(Lnet/minecraft/world/item/ItemStack;ILnet/minecraft/world/entity/player/Player;ILnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;)V",
public class EnchantApplyMixin {
@Inject(method = "clickMenuButton(Lnet/minecraft/world/entity/player/Player;I)Z",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/item/ItemStack;enchant(Lnet/minecraft/world/item/enchantment/Enchantment;I)V"),
locals = LocalCapture.CAPTURE_FAILSOFT)
private void projectmmo$$enchantHandle(ItemStack $$stack, int $$1x, Player player, int $$3x, ItemStack $$4x, Level $$5, BlockPos $$6, CallbackInfo ci, ItemStack stack, List<?> $$8, boolean $$9, int $$11, EnchantmentInstance instance) {
MinecraftForge.EVENT_BUS.post(new EnchantEvent(player, stack, instance));
target = "Lnet/minecraft/world/inventory/ContainerLevelAccess;execute(Ljava/util/function/BiConsumer;)V"))
private void projectmmo$$enchantHandle(Player player, int pId, CallbackInfoReturnable<Boolean> cir) {
EnchantmentMenu menu = ((EnchantmentMenu)(Object)this);
ItemStack stack = menu.enchantSlots.getItem(0);
for (EnchantmentInstance instance : menu.getEnchantmentList(stack, pId, menu.costs[pId])) {
MinecraftForge.EVENT_BUS.post(new EnchantEvent(player, stack, instance));
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/harmonised/pmmo/mixin/MixinPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package harmonised.pmmo.mixin;

import com.google.common.collect.ImmutableMap;
import net.minecraftforge.fml.loading.FMLLoader;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;

import java.util.List;
import java.util.Set;


public final class MixinPlugin implements IMixinConfigPlugin {
@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
if (mixinClassName.endsWith("_Dev") && !isDevelopmentEnv()) return false;
if (mixinClassName.endsWith("_Prod") && isDevelopmentEnv()) return false;
return true;
}

@Override
public void onLoad(String mixinPackage) {}

@Override
public String getRefMapperConfig() { return null; }

@Override
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {}

@Override
public List<String> getMixins() { return null; }

@Override
public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {}

@Override
public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {}

private boolean isDevelopmentEnv() {
return !FMLLoader.isProduction();
}
}
12 changes: 1 addition & 11 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@

#Firework Boom
public net.minecraft.world.entity.projectile.FireworkRocketEntity m_37080_()V # func_213893_k
#Firework Initialization issues
public net.minecraft.world.entity.projectile.FireworkRocketEntity f_37022_ # fireworkAge
public net.minecraft.world.entity.projectile.FireworkRocketEntity f_37023_ # lifetime
public net.minecraft.world.entity.projectile.FireworkRocketEntity f_37019_ # FIREWORK_ITEM

#Block Hardness
public net.minecraft.block.AbstractBlock.AbstractBlockState field_235705_i_ # blockHardness

#Set Pose
public net.minecraft.world.entity.Entity m_20124_(Lnet/minecraft/world/entity/Pose;)V # setPose

#Loot Conditions
public net.minecraft.world.level.storage.loot.predicates.LootItemConditions m_81831_(Ljava/lang/String;Lnet/minecraft/world/level/storage/loot/Serializer;)Lnet/minecraft/world/level/storage/loot/predicates/LootItemConditionType;

#EnchantingMenu Option Enchants
public net.minecraft.world.inventory.EnchantmentMenu m_39471_(Lnet/minecraft/world/item/ItemStack;II)Ljava/util/List; # getEnchantmentList
public net.minecraft.world.inventory.EnchantmentMenu f_39449_ # enchantSlots

# Screen
public net.minecraft.client.gui.screens.inventory.AbstractContainerScreen f_97735_ # leftPos
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/pmmo.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"package": "harmonised.pmmo.mixin",
"compatibilityLevel": "JAVA_17",
"refmap": "pmmo.refmap.json",
"plugin": "harmonised.pmmo.mixin.MixinPlugin",
"mixins": [
"AbstractFurnaceTileEntityShrinkMixin",
"CampfireMixin",
"CampfireMixin_Prod",
"CampfireMixin_Dev",
"ServerLevelMixin"
],
"server": [
Expand Down

0 comments on commit 7c49be7

Please sign in to comment.