2018-12-20 00:39:10 +00:00
|
|
|
package net.shadowfacts.simplemultipart.container;
|
|
|
|
|
|
|
|
import net.fabricmc.fabric.events.PlayerInteractionEvent;
|
2018-12-25 17:08:48 +00:00
|
|
|
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) {
|
2018-12-28 02:44:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-06 15:28:36 +00:00
|
|
|
boolean success = container.breakPart(hit.view, player);
|
2018-12-20 00:39:10 +00:00
|
|
|
return success ? ActionResult.SUCCESS : ActionResult.FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|