move block interaction to different place so it doesnt clash with item interaction

This commit is contained in:
Flemmli97 2021-06-29 19:55:21 +02:00
parent 003c1146ec
commit 7d815cfbaf
8 changed files with 41 additions and 38 deletions

View File

@ -2,6 +2,7 @@ package io.github.flemmli97.flan;
import me.shedaniel.architectury.annotations.ExpectPlatform;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.item.Item;
@ -29,4 +30,9 @@ public class CrossPlatformStuff {
throw new AssertionError();
}
@ExpectPlatform
public static boolean isInventoryTile(BlockEntity blockEntity) {
throw new AssertionError();
}
}

View File

@ -12,7 +12,6 @@ import io.github.flemmli97.flan.player.EnumDisplayType;
import io.github.flemmli97.flan.player.PlayerClaimData;
import net.minecraft.block.BlockState;
import net.minecraft.block.DoorBlock;
import net.minecraft.block.InventoryProvider;
import net.minecraft.block.LecternBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.LecternBlockEntity;
@ -21,7 +20,6 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket;
@ -35,8 +33,6 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import java.util.function.Function;
public class BlockInteractEvents {
public static ActionResult startBreakBlocks(PlayerEntity player, World world, Hand hand, BlockPos pos, Direction direction) {
@ -61,12 +57,8 @@ public class BlockInteractEvents {
return true;
}
public static ActionResult useBlocks(PlayerEntity p, World world, Hand hand, BlockHitResult hitResult) {
return useBlocks(p, world, hand, hitResult, BlockInteractEvents::isContainer);
}
//Right click block
public static ActionResult useBlocks(PlayerEntity p, World world, Hand hand, BlockHitResult hitResult, Function<BlockEntity, Boolean> isInventory) {
public static ActionResult useBlocks(PlayerEntity p, World world, Hand hand, BlockHitResult hitResult) {
if (world.isClient)
return ActionResult.PASS;
ServerPlayerEntity player = (ServerPlayerEntity) p;
@ -115,25 +107,18 @@ public class BlockInteractEvents {
LockedLecternScreenHandler.create(player, (LecternBlockEntity) blockEntity);
return ActionResult.FAIL;
}
if (!ConfigHandler.config.lenientBlockEntityCheck || isInventory.apply(blockEntity)) {
if (!ConfigHandler.config.lenientBlockEntityCheck || CrossPlatformStuff.isInventoryTile(blockEntity)) {
if (claim.canInteract(player, PermissionRegistry.OPENCONTAINER, hitResult.getBlockPos(), true))
return ActionResult.PASS;
PlayerClaimData.get(player).addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
return ActionResult.FAIL;
}
}
if (claim.canInteract(player, PermissionRegistry.INTERACTBLOCK, hitResult.getBlockPos(), true))
return ActionResult.PASS;
PlayerClaimData.get(player).addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
return ActionResult.FAIL;
return claim.canInteract(player, PermissionRegistry.INTERACTBLOCK, hitResult.getBlockPos(), false) ? ActionResult.PASS : ActionResult.FAIL;
}
return ActionResult.PASS;
}
private static boolean isContainer(BlockEntity blockEntity) {
return blockEntity instanceof Inventory || blockEntity instanceof InventoryProvider;
}
public static boolean alwaysAllowBlock(Identifier id, BlockEntity blockEntity) {
return ConfigHandler.config.ignoredBlocks.contains(id.toString())
|| (blockEntity != null

View File

@ -4,6 +4,10 @@ import io.github.flemmli97.flan.event.BlockInteractEvents;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
@ -11,6 +15,7 @@ import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(AbstractBlock.AbstractBlockState.class)
public abstract class AbstractBlockStateMixin {
@ -22,6 +27,18 @@ public abstract class AbstractBlockStateMixin {
}
}
/**
* Can't use the hooks from both fabric or forge cause they are too generic.
* Wouldn't be able to place blocks after cancelling them
*/
@Inject(method = "onUse", at = @At(value = "HEAD"), cancellable = true)
private void useBlock(World world, PlayerEntity player, Hand hand, BlockHitResult result, CallbackInfoReturnable<ActionResult> info) {
if (BlockInteractEvents.useBlocks(player, world, hand, result) == ActionResult.FAIL) {
info.setReturnValue(ActionResult.FAIL);
info.cancel();
}
}
@Shadow
protected abstract BlockState asBlockState();
}

View File

@ -13,7 +13,6 @@ 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;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.fabricmc.loader.api.FabricLoader;
@ -24,7 +23,6 @@ public class FlanFabric implements ModInitializer {
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);
UseItemCallback.EVENT.register(ItemInteractEvents::useItem);

View File

@ -4,7 +4,10 @@ import io.github.flemmli97.flan.FabricRegistryWrapper;
import io.github.flemmli97.flan.SimpleRegistryWrapper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.Block;
import net.minecraft.block.InventoryProvider;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
@ -36,4 +39,8 @@ public class CrossPlatformStuffImpl {
public static SimpleRegistryWrapper<Item> registryItems() {
return new FabricRegistryWrapper<>(Registry.ITEM);
}
public static boolean isInventoryTile(BlockEntity blockEntity) {
return blockEntity instanceof Inventory || blockEntity instanceof InventoryProvider;
}
}

View File

@ -22,7 +22,6 @@ public class FlanForge {
forge.addListener(ItemInteractEventsForge::useItem);
forge.addListener(BlockInteractEventsForge::startBreakBlocks);
forge.addListener(BlockInteractEventsForge::breakBlocks);
forge.addListener(BlockInteractEventsForge::useBlocks);
forge.addListener(EntityInteractEventsForge::attackEntity);
forge.addListener(EntityInteractEventsForge::useAtEntity);
forge.addListener(EntityInteractEventsForge::useEntity);

View File

@ -3,9 +3,13 @@ package io.github.flemmli97.flan.forge;
import io.github.flemmli97.flan.ForgeRegistryWrapper;
import io.github.flemmli97.flan.SimpleRegistryWrapper;
import net.minecraft.block.Block;
import net.minecraft.block.InventoryProvider;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraftforge.fml.loading.FMLPaths;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.registries.ForgeRegistries;
import java.nio.file.Path;
@ -27,4 +31,8 @@ public class CrossPlatformStuffImpl {
public static SimpleRegistryWrapper<Item> registryItems() {
return new ForgeRegistryWrapper<>(ForgeRegistries.ITEMS);
}
public static boolean isInventoryTile(BlockEntity blockEntity) {
return blockEntity instanceof Inventory || blockEntity instanceof InventoryProvider || blockEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).isPresent();
}
}

View File

@ -1,15 +1,11 @@
package io.github.flemmli97.flan.forgeevent;
import io.github.flemmli97.flan.event.BlockInteractEvents;
import net.minecraft.block.InventoryProvider;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.items.CapabilityItemHandler;
public class BlockInteractEventsForge {
@ -26,17 +22,4 @@ public class BlockInteractEventsForge {
if (!BlockInteractEvents.breakBlocks((World) event.getWorld(), event.getPlayer(), event.getPos(), event.getState(), event.getWorld().getBlockEntity(event.getPos())))
event.setCanceled(true);
}
//Right click block
public static void useBlocks(PlayerInteractEvent.RightClickBlock event) {
ActionResult result = BlockInteractEvents.useBlocks(event.getPlayer(), event.getWorld(), event.getHand(), event.getHitVec(), BlockInteractEventsForge::isContainer);
if (result != ActionResult.PASS) {
event.setCancellationResult(result);
event.setCanceled(true);
}
}
private static boolean isContainer(BlockEntity blockEntity) {
return blockEntity instanceof Inventory || blockEntity instanceof InventoryProvider || blockEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).isPresent();
}
}