Skip to content

Commit

Permalink
Add temporary solution to crafting unit transform recipes being hard-…
Browse files Browse the repository at this point in the history
…coded against AE2's own unit block
  • Loading branch information
62832 committed Dec 11, 2024
1 parent 32b1802 commit b7b3a1b
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package gripe._90.megacells.mixin;

import java.util.Objects;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.sugar.Local;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;

import appeng.block.AEBaseBlock;
import appeng.block.crafting.AbstractCraftingUnitBlock;
import appeng.block.crafting.ICraftingUnitType;
import appeng.core.definitions.AEBlocks;
import appeng.recipes.AERecipeTypes;

import gripe._90.megacells.MEGACells;
import gripe._90.megacells.block.MEGACraftingUnitType;
import gripe._90.megacells.definition.MEGABlocks;

/**
* Temporary solution to the current crafting unit transform recipe system being hard-coded against AE2's own crafting
* unit block.
* <p>
* See also: {@link CraftingBlockItemMixin}
*/
@Mixin(AbstractCraftingUnitBlock.class)
public abstract class AbstractCraftingUnitBlockMixin extends AEBaseBlock {
@Shadow
@Final
public ICraftingUnitType type;

protected AbstractCraftingUnitBlockMixin(Properties props) {
super(props);
}

@Shadow
public abstract InteractionResult removeUpgrade(Level level, Player player, BlockPos pos, BlockState newState);

// spotless:off
@Redirect(
method = "useWithoutItem",
at = @At(
value = "INVOKE",
target = "Lappeng/block/crafting/AbstractCraftingUnitBlock;removeUpgrade(Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;)Lnet/minecraft/world/InteractionResult;"))
// spotless:on
private InteractionResult removeMegaUpgrade(
AbstractCraftingUnitBlock<?> instance, Level level, Player player, BlockPos pos, BlockState newState) {
var unitBlock = Objects.requireNonNull(instance.getRegistryName())
.getNamespace()
.equals(MEGACells.MODID)
? MEGABlocks.MEGA_CRAFTING_UNIT
: AEBlocks.CRAFTING_UNIT;
return removeUpgrade(level, player, pos, unitBlock.block().defaultBlockState());
}

@ModifyExpressionValue(
method = "removeUpgrade",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;isClientSide()Z"))
private boolean isMegaUnit(boolean original) {
return original || type == MEGACraftingUnitType.UNIT;
}

// spotless:off
@ModifyExpressionValue(
method = "upgrade",
at = @At(
value = "INVOKE",
target = "Lappeng/recipes/game/CraftingUnitTransformRecipe;getUpgradedBlock(Lnet/minecraft/world/level/Level;Lnet/minecraft/world/item/ItemStack;)Lnet/minecraft/world/level/block/Block;"))
// spotless:on
private Block isMegaRecipe(
Block original, @Local(argsOnly = true) ItemStack heldItem, @Local(argsOnly = true) Level level) {
if (original == null) {
return null;
}

return Objects.requireNonNull(getRegistryName()).getNamespace().equals(MEGACells.MODID)
&& !mega$isFromMegaRecipe(level, heldItem)
? null
: original;
}

@Unique
private static boolean mega$isFromMegaRecipe(Level level, ItemStack heldItem) {
for (var holder : level.getRecipeManager().getAllRecipesFor(AERecipeTypes.CRAFTING_UNIT_TRANSFORM)) {
if (heldItem.is(holder.value().getUpgradeItem())) {
return holder.id().getNamespace().equals(MEGACells.MODID);
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package gripe._90.megacells.mixin;

import com.llamalad7.mixinextras.sugar.Local;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;

import appeng.block.crafting.CraftingBlockItem;
import appeng.core.definitions.AEBlocks;

import gripe._90.megacells.MEGACells;
import gripe._90.megacells.definition.MEGABlocks;

/**
* Temporary solution to the current crafting unit transform recipe system being hard-coded against AE2's own crafting
* unit block.
* <p>
* See also: {@link AbstractCraftingUnitBlockMixin}
*/
@Mixin(CraftingBlockItem.class)
public abstract class CraftingBlockItemMixin {
// spotless:off
@Redirect(
method = "use",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/player/Inventory;placeItemBackInInventory(Lnet/minecraft/world/item/ItemStack;)V",
ordinal = 1))
// spotless:on
private void placeBackMegaUnit(Inventory inv, ItemStack stack, @Local int itemCount) {
var unit = BuiltInRegistries.ITEM
.getKey((CraftingBlockItem) (Object) this)
.getNamespace()
.equals(MEGACells.MODID)
? MEGABlocks.MEGA_CRAFTING_UNIT
: AEBlocks.CRAFTING_UNIT;
inv.placeItemBackInInventory(unit.stack(itemCount));
}
}
2 changes: 2 additions & 0 deletions src/main/resources/megacells.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"minVersion": "0.8.5",
"package": "gripe._90.megacells.mixin",
"mixins": [
"AbstractCraftingUnitBlockMixin",
"CellWorkbenchBlockEntityMixin",
"CellWorkbenchMenuMixin",
"CraftingBlockItemMixin",
"ItemEntityMixin",
"ItemMenuHostMixin",
"PatternProviderMenuAccessor"
Expand Down

0 comments on commit b7b3a1b

Please sign in to comment.