DoubleDoors/src/main/java/net/shadowfacts/doubledoors/DoubleDoors.java

45 lines
1.8 KiB
Java

package net.shadowfacts.doubledoors;
import net.fabricmc.api.ModInitializer;
import net.minecraft.block.BlockState;
import net.minecraft.block.DoorBlock;
import net.minecraft.block.enums.DoorHinge;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Hand;
import net.minecraft.util.Pair;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
/**
* @author shadowfacts
*/
public class DoubleDoors implements ModInitializer {
@Override
public void onInitialize() {
}
public static void onDoorActivated(BlockState newDoorState, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult result) {
Pair<BlockPos, BlockState> opposingDoor = getOpposingDoor(newDoorState, world, pos);
if (opposingDoor != null && opposingDoor.getRight().get(DoorBlock.OPEN) != newDoorState.get(DoorBlock.OPEN)) {
BlockState newOpposingDoorState = opposingDoor.getRight().cycle(DoorBlock.OPEN);
world.setBlockState(opposingDoor.getLeft(), newOpposingDoorState, 10);
}
}
private static Pair<BlockPos, BlockState> getOpposingDoor(BlockState doorState, World world, BlockPos pos) {
Direction facing = doorState.get(DoorBlock.FACING);
DoorHinge hinge = doorState.get(DoorBlock.HINGE);
Direction offsetDirection = hinge == DoorHinge.LEFT ? facing.rotateYClockwise() : facing.rotateYCounterclockwise();
BlockPos opposingDoorPos = pos.offset(offsetDirection);
BlockState possibleOpposingDoorState = world.getBlockState(opposingDoorPos);
if (possibleOpposingDoorState.getBlock() instanceof DoorBlock && possibleOpposingDoorState.get(DoorBlock.HINGE) != doorState.get(DoorBlock.HINGE)) {
return new Pair<>(opposingDoorPos, possibleOpposingDoorState);
} else {
return null;
}
}
}