|
| 1 | +package dev.ithundxr.railwaystweaks.mixin.createdeco; |
| 2 | + |
| 3 | +import com.github.talrey.createdeco.blocks.CatwalkRailingBlock; |
| 4 | +import com.simibubi.create.content.equipment.wrench.IWrenchable; |
| 5 | +import net.minecraft.core.BlockPos; |
| 6 | +import net.minecraft.core.Direction; |
| 7 | +import net.minecraft.world.InteractionResult; |
| 8 | +import net.minecraft.world.entity.player.Player; |
| 9 | +import net.minecraft.world.item.ItemStack; |
| 10 | +import net.minecraft.world.item.context.UseOnContext; |
| 11 | +import net.minecraft.world.level.Level; |
| 12 | +import net.minecraft.world.level.block.Block; |
| 13 | +import net.minecraft.world.level.block.state.BlockState; |
| 14 | +import net.minecraft.world.level.block.state.properties.BooleanProperty; |
| 15 | +import net.minecraft.world.phys.Vec3; |
| 16 | +import org.spongepowered.asm.mixin.Final; |
| 17 | +import org.spongepowered.asm.mixin.Mixin; |
| 18 | +import org.spongepowered.asm.mixin.Overwrite; |
| 19 | +import org.spongepowered.asm.mixin.Shadow; |
| 20 | + |
| 21 | +@Mixin(CatwalkRailingBlock.class) |
| 22 | +public abstract class CatwalkRailingBlockMixin implements IWrenchable { |
| 23 | + @Shadow public static BooleanProperty fromDirection(Direction face) { throw new AssertionError(); } |
| 24 | + |
| 25 | + @Shadow @Final public static BooleanProperty EAST_FENCE; |
| 26 | + @Shadow @Final public static BooleanProperty WEST_FENCE; |
| 27 | + @Shadow @Final public static BooleanProperty NORTH_FENCE; |
| 28 | + @Shadow @Final public static BooleanProperty SOUTH_FENCE; |
| 29 | + |
| 30 | + /** |
| 31 | + * @author IThundxr |
| 32 | + * @reason Create deco load's client classes on the server; |
| 33 | + */ |
| 34 | + @Overwrite |
| 35 | + public InteractionResult onSneakWrenched(BlockState state, UseOnContext context) { |
| 36 | + BlockPos pos = context.getClickedPos(); |
| 37 | + Vec3 subbox = context.getClickLocation().subtract(pos.getCenter()); |
| 38 | + Direction face = context.getClickedFace(); |
| 39 | + Level level = context.getLevel(); |
| 40 | + Player player = context.getPlayer(); |
| 41 | + var x = subbox.x; |
| 42 | + var z = subbox.z; |
| 43 | + |
| 44 | + if (level.isClientSide) |
| 45 | + return InteractionResult.PASS; |
| 46 | + |
| 47 | + //check if the top face is wrenched, remove side |
| 48 | + if (face == Direction.UP) { |
| 49 | + boolean bottomleft = x < -z; |
| 50 | + boolean topleft = x < z; |
| 51 | + var dir = Direction.WEST; |
| 52 | + if (!bottomleft && topleft) dir = Direction.SOUTH; |
| 53 | + if (!bottomleft && !topleft) dir = Direction.EAST; |
| 54 | + if (bottomleft && !topleft) dir = Direction.NORTH; |
| 55 | + if (bottomleft && topleft) dir = Direction.WEST; |
| 56 | + |
| 57 | + //obscure edge case where a corner of the top face cannot be wrenched |
| 58 | + if (state.getValue(fromDirection(dir))) { |
| 59 | + state = state.setValue(fromDirection(dir), false); |
| 60 | + level.setBlock(pos, state, 3); |
| 61 | + playRemoveSound(level, pos); |
| 62 | + if (!player.getAbilities().instabuild) player.addItem(new ItemStack(state.getBlock().asItem())); |
| 63 | + return InteractionResult.SUCCESS; |
| 64 | + } |
| 65 | + else return InteractionResult.PASS; |
| 66 | + } |
| 67 | + |
| 68 | + //check for wrenching the inside faces |
| 69 | + if (x == 0.375 || x == -0.375 || z == 0.375 || z == -0.375) state = state.setValue(fromDirection(face.getOpposite()), false); |
| 70 | + |
| 71 | + //check for wrenching the outside faces |
| 72 | + if (x == 0.5 || x == -0.5 || z == 0.5 || z == -0.5) { |
| 73 | + if (!state.getValue(fromDirection(face))) { |
| 74 | + if (x >= 0.375) state = state.setValue(EAST_FENCE, false); |
| 75 | + if (x <= -0.375) state = state.setValue(WEST_FENCE, false); |
| 76 | + if (z <= -0.375) state = state.setValue(NORTH_FENCE, false); |
| 77 | + if (z >= 0.375) state = state.setValue(SOUTH_FENCE, false); |
| 78 | + } |
| 79 | + else state = state.setValue(fromDirection(face), false); |
| 80 | + } |
| 81 | + |
| 82 | + level.setBlock(pos, state, 3); |
| 83 | + playRemoveSound(level, pos); |
| 84 | + if (!player.getAbilities().instabuild) player.addItem(new ItemStack(state.getBlock().asItem())); |
| 85 | + return InteractionResult.SUCCESS; |
| 86 | + } |
| 87 | +} |
0 commit comments