From 764d5602193905335c0eb83318dd573a3745da02 Mon Sep 17 00:00:00 2001 From: brachy84 Date: Tue, 16 Jul 2024 10:43:23 +0200 Subject: [PATCH] fix TileEntityPistonMixin conflict with UT --- sandbox_issues.md | 5 - .../core/mixin/TileEntityPistonMixin.java | 153 +++++------------- 2 files changed, 36 insertions(+), 122 deletions(-) delete mode 100644 sandbox_issues.md diff --git a/sandbox_issues.md b/sandbox_issues.md deleted file mode 100644 index aefeacef7..000000000 --- a/sandbox_issues.md +++ /dev/null @@ -1,5 +0,0 @@ -# Sandbox Issues - -* Extending any class results in an error -* Calling bracket handlers inside a custom class results in an error -* Getting a global var inside a custom class results in an error diff --git a/src/main/java/com/cleanroommc/groovyscript/core/mixin/TileEntityPistonMixin.java b/src/main/java/com/cleanroommc/groovyscript/core/mixin/TileEntityPistonMixin.java index c2fbdaa36..a616a4c60 100644 --- a/src/main/java/com/cleanroommc/groovyscript/core/mixin/TileEntityPistonMixin.java +++ b/src/main/java/com/cleanroommc/groovyscript/core/mixin/TileEntityPistonMixin.java @@ -1,22 +1,24 @@ package com.cleanroommc.groovyscript.core.mixin; import com.cleanroommc.groovyscript.compat.vanilla.VanillaModule; -import com.google.common.collect.Lists; +import com.llamalad7.mixinextras.sugar.Local; +import com.llamalad7.mixinextras.sugar.Share; +import com.llamalad7.mixinextras.sugar.ref.LocalBooleanRef; +import com.llamalad7.mixinextras.sugar.ref.LocalIntRef; +import com.llamalad7.mixinextras.sugar.ref.LocalRef; import net.minecraft.block.BlockPistonMoving; import net.minecraft.block.material.EnumPushReaction; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.Entity; -import net.minecraft.entity.MoverType; import net.minecraft.entity.item.EntityItem; import net.minecraft.tileentity.TileEntityPiston; import net.minecraft.util.EnumFacing; -import net.minecraft.util.math.AxisAlignedBB; -import net.minecraft.util.math.BlockPos; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Overwrite; 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 java.util.List; @@ -26,123 +28,40 @@ public abstract class TileEntityPistonMixin { @Shadow private boolean extending; - @Shadow - private EnumFacing pistonFacing; - - @Shadow - private float progress; - - @Shadow - protected abstract IBlockState getCollisionRelatedBlockState(); - - @Shadow - protected abstract AxisAlignedBB moveByPositionAndProgress(AxisAlignedBB p_190607_1_); - - @Shadow - protected abstract AxisAlignedBB getMinMaxPiecesAABB(List p_191515_1_); - - @Shadow - protected abstract AxisAlignedBB getMovementArea(AxisAlignedBB p_190610_1_, EnumFacing p_190610_2_, double p_190610_3_); - - @Shadow - private IBlockState pistonState; - - @Shadow - protected abstract double getMovement(AxisAlignedBB p_190612_1_, EnumFacing facing, AxisAlignedBB p_190612_3_); - - @Shadow - @Final - private static ThreadLocal MOVING_ENTITY; - @Shadow private boolean shouldHeadBeRendered; - @Shadow - protected abstract void fixEntityWithinPistonBase(Entity p_190605_1_, EnumFacing p_190605_2_, double p_190605_3_); - - /** - * @author brachy84 - * @reason easier recipe implementation - */ - @Overwrite - private void moveCollidedEntities(float progress) { - TileEntityPiston thisTile = (TileEntityPiston) (Object) this; - - EnumFacing enumfacing = this.extending ? this.pistonFacing : this.pistonFacing.getOpposite(); - double d0 = progress - this.progress; - List list = Lists.newArrayList(); - this.getCollisionRelatedBlockState().addCollisionBoxToList(thisTile.getWorld(), BlockPos.ORIGIN, new AxisAlignedBB(BlockPos.ORIGIN), list, null, true); - - if (!list.isEmpty()) { - AxisAlignedBB axisalignedbb = this.moveByPositionAndProgress(this.getMinMaxPiecesAABB(list)); - List list1 = thisTile.getWorld().getEntitiesWithinAABBExcludingEntity(null, this.getMovementArea(axisalignedbb, enumfacing, d0).union(axisalignedbb)); - - if (!list1.isEmpty()) { - boolean flag = this.pistonState.getBlock().isStickyBlock(this.pistonState); - - // groovyscript start - int tryRecipesUntil = list1.size(); - IBlockState pushingAgainst = null; - boolean checkRecipes = !thisTile.getWorld().isRemote && - this.extending && - this.shouldHeadBeRendered && - progress >= 1.0f && - !((pushingAgainst = thisTile.getWorld().getBlockState(thisTile.getPos().offset(enumfacing))).getBlock() instanceof BlockPistonMoving) && - pushingAgainst.getMaterial() != Material.AIR; - // groovyscript end - - for (int i = 0; i < list1.size(); i++) { - Entity entity = list1.get(i); - if (entity.getPushReaction() != EnumPushReaction.IGNORE) { - // groovyscript start - if (checkRecipes && i < tryRecipesUntil && entity instanceof EntityItem entityItem) { - VanillaModule.inWorldCrafting.pistonPush.findAndRunRecipe(entityItem1 -> { - entityItem1.getEntityWorld().spawnEntity(entityItem1); - list1.add(entityItem1); - }, entityItem, pushingAgainst); - } - // groovyscript end - - groovyscript$pushEntity(list, d0, enumfacing, entity, flag); - } - } - } + @Inject(method = "moveCollidedEntities", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;isStickyBlock(Lnet/minecraft/block/state/IBlockState;)Z", shift = At.Shift.AFTER), remap = false) + private void moveCollidedEntitiesPre(float f, CallbackInfo ci, @Local(ordinal = 0) EnumFacing facing, + @Local(ordinal = 1) List list1, + @Share("tryRecipesUntil") LocalIntRef tryRecipesUntil, + @Share("pushingAgainst") LocalRef pushingAgainst, + @Share("checkRecipes") LocalBooleanRef checkRecipes) { + TileEntityPiston piston = (TileEntityPiston) (Object) this; + tryRecipesUntil.set(list1.size()); + boolean b = !piston.getWorld().isRemote && + this.extending && + this.shouldHeadBeRendered && + f >= 1.0f; + if (b) { + pushingAgainst.set(piston.getWorld().getBlockState(piston.getPos().offset(facing))); + checkRecipes.set(!(pushingAgainst.get() instanceof BlockPistonMoving) && pushingAgainst.get().getMaterial() != Material.AIR); + } else { + checkRecipes.set(false); } } - private void groovyscript$pushEntity(List list, double d0, EnumFacing enumfacing, Entity entity, boolean sticky) { - if (sticky) { - switch (enumfacing.getAxis()) { - case X -> entity.motionX = enumfacing.getXOffset(); - case Y -> entity.motionY = enumfacing.getYOffset(); - case Z -> entity.motionZ = enumfacing.getZOffset(); - } - } - - double d1 = 0.0D; - - for (AxisAlignedBB axisAlignedBB : list) { - AxisAlignedBB axisalignedbb1 = this.getMovementArea(this.moveByPositionAndProgress(axisAlignedBB), enumfacing, d0); - AxisAlignedBB axisalignedbb2 = entity.getEntityBoundingBox(); - - if (axisalignedbb1.intersects(axisalignedbb2)) { - d1 = Math.max(d1, this.getMovement(axisalignedbb1, enumfacing, axisalignedbb2)); - - if (d1 >= d0) { - break; - } - } - } - - if (d1 > 0.0D) { - d1 = Math.min(d1, d0) + 0.01D; - MOVING_ENTITY.set(enumfacing); - entity.move(MoverType.PISTON, d1 * enumfacing.getXOffset(), d1 * enumfacing.getYOffset(), d1 * enumfacing.getZOffset()); - MOVING_ENTITY.set(null); - - if (!this.extending && this.shouldHeadBeRendered) { - this.fixEntityWithinPistonBase(entity, enumfacing, d0); - } + @Inject(method = "moveCollidedEntities", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;getPushReaction()Lnet/minecraft/block/material/EnumPushReaction;", shift = At.Shift.AFTER)) + private void moveCollidedEntitiesPost(float p_184322_1_, CallbackInfo ci, @Local int index, @Local(ordinal = 1) List list1, + @Share("tryRecipesUntil") LocalIntRef tryRecipesUntil, @Share("pushingAgainst") LocalRef pushingAgainst, + @Share("checkRecipes") LocalBooleanRef checkRecipes) { + Entity entity = list1.get(index); + if (entity.getPushReaction() == EnumPushReaction.IGNORE) return; + if (checkRecipes.get() && index < tryRecipesUntil.get() && entity instanceof EntityItem entityItem) { + VanillaModule.inWorldCrafting.pistonPush.findAndRunRecipe(entityItem1 -> { + entityItem1.getEntityWorld().spawnEntity(entityItem1); + list1.add(entityItem1); + }, entityItem, pushingAgainst.get()); } } }