SimpleMultipart/src/main/java/net/shadowfacts/simplemultipart/container/ContainerEventHandler.java

49 lines
1.5 KiB
Java
Raw Normal View History

2018-12-20 00:39:10 +00:00
package net.shadowfacts.simplemultipart.container;
import net.fabricmc.fabric.events.PlayerInteractionEvent;
import net.minecraft.block.Block;
2018-12-20 00:39:10 +00:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import net.shadowfacts.simplemultipart.util.MultipartHitResult;
import net.shadowfacts.simplemultipart.SimpleMultipart;
import net.shadowfacts.simplemultipart.util.MultipartHelper;
/**
* @author shadowfacts
*/
2018-12-25 15:30:46 +00:00
public class ContainerEventHandler {
2018-12-20 00:39:10 +00:00
public static void register() {
2018-12-25 15:30:46 +00:00
PlayerInteractionEvent.ATTACK_BLOCK.register(ContainerEventHandler::handleBlockAttack);
2018-12-20 00:39:10 +00:00
}
private static ActionResult handleBlockAttack(PlayerEntity player, World world, Hand hand, BlockPos pos, Direction direction) {
if (world.isClient) {
return ActionResult.PASS;
}
Block block = world.getBlockState(pos).getBlock();
if (block != SimpleMultipart.containerBlock && block != SimpleMultipart.tickableContainerBlock) {
2018-12-20 00:39:10 +00:00
return ActionResult.PASS;
}
2018-12-25 15:30:46 +00:00
MultipartContainer container = (MultipartContainer)world.getBlockEntity(pos);
2018-12-20 00:39:10 +00:00
if (container == null) {
return ActionResult.FAILURE;
}
MultipartHitResult hit = MultipartHelper.rayTrace(container, world, pos, player);
if (hit == null) {
return ActionResult.FAILURE;
}
boolean success = container.breakPart(hit.view, player);
2018-12-20 00:39:10 +00:00
return success ? ActionResult.SUCCESS : ActionResult.FAILURE;
}
}