From 7d815cfbafb37ca9d7903ddbdb2010a70898e533 Mon Sep 17 00:00:00 2001 From: Flemmli97 Date: Tue, 29 Jun 2021 19:55:21 +0200 Subject: [PATCH] move block interaction to different place so it doesnt clash with item interaction --- .../flemmli97/flan/CrossPlatformStuff.java | 6 ++++++ .../flan/event/BlockInteractEvents.java | 21 +++---------------- .../flan/mixin/AbstractBlockStateMixin.java | 17 +++++++++++++++ .../io/github/flemmli97/flan/FlanFabric.java | 2 -- .../flan/fabric/CrossPlatformStuffImpl.java | 7 +++++++ .../io/github/flemmli97/flan/FlanForge.java | 1 - .../flan/forge/CrossPlatformStuffImpl.java | 8 +++++++ .../forgeevent/BlockInteractEventsForge.java | 17 --------------- 8 files changed, 41 insertions(+), 38 deletions(-) diff --git a/common/src/main/java/io/github/flemmli97/flan/CrossPlatformStuff.java b/common/src/main/java/io/github/flemmli97/flan/CrossPlatformStuff.java index a926702..d8dc837 100644 --- a/common/src/main/java/io/github/flemmli97/flan/CrossPlatformStuff.java +++ b/common/src/main/java/io/github/flemmli97/flan/CrossPlatformStuff.java @@ -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(); + } + } diff --git a/common/src/main/java/io/github/flemmli97/flan/event/BlockInteractEvents.java b/common/src/main/java/io/github/flemmli97/flan/event/BlockInteractEvents.java index 01d2cf2..95cee7a 100644 --- a/common/src/main/java/io/github/flemmli97/flan/event/BlockInteractEvents.java +++ b/common/src/main/java/io/github/flemmli97/flan/event/BlockInteractEvents.java @@ -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 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 diff --git a/common/src/main/java/io/github/flemmli97/flan/mixin/AbstractBlockStateMixin.java b/common/src/main/java/io/github/flemmli97/flan/mixin/AbstractBlockStateMixin.java index 8ec3eb1..18a2b4e 100644 --- a/common/src/main/java/io/github/flemmli97/flan/mixin/AbstractBlockStateMixin.java +++ b/common/src/main/java/io/github/flemmli97/flan/mixin/AbstractBlockStateMixin.java @@ -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 info) { + if (BlockInteractEvents.useBlocks(player, world, hand, result) == ActionResult.FAIL) { + info.setReturnValue(ActionResult.FAIL); + info.cancel(); + } + } + @Shadow protected abstract BlockState asBlockState(); } diff --git a/fabric/src/main/java/io/github/flemmli97/flan/FlanFabric.java b/fabric/src/main/java/io/github/flemmli97/flan/FlanFabric.java index b1d47ff..b7a8ab5 100644 --- a/fabric/src/main/java/io/github/flemmli97/flan/FlanFabric.java +++ b/fabric/src/main/java/io/github/flemmli97/flan/FlanFabric.java @@ -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); diff --git a/fabric/src/main/java/io/github/flemmli97/flan/fabric/CrossPlatformStuffImpl.java b/fabric/src/main/java/io/github/flemmli97/flan/fabric/CrossPlatformStuffImpl.java index 2b2a561..d06bc32 100644 --- a/fabric/src/main/java/io/github/flemmli97/flan/fabric/CrossPlatformStuffImpl.java +++ b/fabric/src/main/java/io/github/flemmli97/flan/fabric/CrossPlatformStuffImpl.java @@ -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 registryItems() { return new FabricRegistryWrapper<>(Registry.ITEM); } + + public static boolean isInventoryTile(BlockEntity blockEntity) { + return blockEntity instanceof Inventory || blockEntity instanceof InventoryProvider; + } } diff --git a/forge/src/main/java/io/github/flemmli97/flan/FlanForge.java b/forge/src/main/java/io/github/flemmli97/flan/FlanForge.java index 6598b1d..7e291d6 100644 --- a/forge/src/main/java/io/github/flemmli97/flan/FlanForge.java +++ b/forge/src/main/java/io/github/flemmli97/flan/FlanForge.java @@ -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); diff --git a/forge/src/main/java/io/github/flemmli97/flan/forge/CrossPlatformStuffImpl.java b/forge/src/main/java/io/github/flemmli97/flan/forge/CrossPlatformStuffImpl.java index 8a06db0..d164bde 100644 --- a/forge/src/main/java/io/github/flemmli97/flan/forge/CrossPlatformStuffImpl.java +++ b/forge/src/main/java/io/github/flemmli97/flan/forge/CrossPlatformStuffImpl.java @@ -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 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(); + } } diff --git a/forge/src/main/java/io/github/flemmli97/flan/forgeevent/BlockInteractEventsForge.java b/forge/src/main/java/io/github/flemmli97/flan/forgeevent/BlockInteractEventsForge.java index 586fdc9..93ee831 100644 --- a/forge/src/main/java/io/github/flemmli97/flan/forgeevent/BlockInteractEventsForge.java +++ b/forge/src/main/java/io/github/flemmli97/flan/forgeevent/BlockInteractEventsForge.java @@ -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(); - } }