left click blocks check

This commit is contained in:
Flemmli97 2021-06-29 14:40:39 +02:00
parent 25107f2223
commit 5c95c9aed2
4 changed files with 17 additions and 1 deletions

View File

@ -31,11 +31,16 @@ import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
public class BlockInteractEvents {
public static ActionResult startBreakBlocks(PlayerEntity player, World world, Hand hand, BlockPos pos, Direction direction) {
return breakBlocks(world, player, pos, world.getBlockState(pos), world.getBlockEntity(pos)) ? ActionResult.PASS : ActionResult.FAIL;
}
public static boolean breakBlocks(World world, PlayerEntity p, BlockPos pos, BlockState state, BlockEntity tile) {
if (world.isClient || p.isSpectator())
return true;

View File

@ -10,6 +10,7 @@ import io.github.flemmli97.flan.integration.playerability.PlayerAbilityEvents;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
@ -22,6 +23,7 @@ public class FlanFabric implements ModInitializer {
@Override
public void onInitialize() {
PlayerBlockBreakEvents.BEFORE.register(BlockInteractEvents::breakBlocks);
AttackBlockCallback.EVENT.register(BlockInteractEvents::startBreakBlocks);
UseBlockCallback.EVENT.register(BlockInteractEvents::useBlocks);
UseEntityCallback.EVENT.register(EntityInteractEvents::useAtEntity);
AttackEntityCallback.EVENT.register(EntityInteractEvents::attackEntity);

View File

@ -20,6 +20,7 @@ public class FlanForge {
forge.addListener(WorldEventsForge::pistonCanPush);
forge.addListener(WorldEventsForge::preventMobSpawn);
forge.addListener(ItemInteractEventsForge::useItem);
forge.addListener(BlockInteractEventsForge::startBreakBlocks);
forge.addListener(BlockInteractEventsForge::breakBlocks);
forge.addListener(BlockInteractEventsForge::useBlocks);
forge.addListener(EntityInteractEventsForge::attackEntity);

View File

@ -1,6 +1,7 @@
package io.github.flemmli97.flan.forgeevent;
import io.github.flemmli97.flan.event.BlockInteractEvents;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
@ -8,8 +9,15 @@ import net.minecraftforge.event.world.BlockEvent;
public class BlockInteractEventsForge {
public static void startBreakBlocks(PlayerInteractEvent.LeftClickBlock event) {
if (!(event.getWorld() instanceof ServerWorld))
return;
if (BlockInteractEvents.startBreakBlocks(event.getPlayer(), event.getWorld(), event.getHand(), event.getPos(), event.getFace()) == ActionResult.FAIL)
event.setCanceled(true);
}
public static void breakBlocks(BlockEvent.BreakEvent event) {
if (!(event.getWorld() instanceof World))
if (!(event.getWorld() instanceof ServerWorld))
return;
if (!BlockInteractEvents.breakBlocks((World) event.getWorld(), event.getPlayer(), event.getPos(), event.getState(), event.getWorld().getBlockEntity(event.getPos())))
event.setCanceled(true);