diff --git a/src/main/java/com/buuz135/functionalstorage/FunctionalStorage.java b/src/main/java/com/buuz135/functionalstorage/FunctionalStorage.java index 533d6fee..22c8665a 100644 --- a/src/main/java/com/buuz135/functionalstorage/FunctionalStorage.java +++ b/src/main/java/com/buuz135/functionalstorage/FunctionalStorage.java @@ -31,6 +31,7 @@ import com.hrznstudio.titanium.recipe.generator.IJsonFile; import com.hrznstudio.titanium.recipe.generator.TitaniumSerializableProvider; import com.hrznstudio.titanium.recipe.serializer.GenericSerializer; +import com.hrznstudio.titanium.util.TileUtil; import net.minecraft.client.renderer.ItemBlockRenderTypes; import net.minecraft.client.renderer.RenderType; import net.minecraft.data.recipes.FinishedRecipe; @@ -62,6 +63,7 @@ import net.minecraftforge.common.crafting.CraftingHelper; import net.minecraftforge.common.util.NonNullLazy; import net.minecraftforge.data.event.GatherDataEvent; +import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.event.level.BlockEvent; import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.common.Mod; @@ -178,6 +180,22 @@ public FunctionalStorage() { NBTManager.getInstance().scanTileClassForAnnotations(FluidDrawerTile.class); NBTManager.getInstance().scanTileClassForAnnotations(SimpleCompactingDrawerTile.class); NBTManager.getInstance().scanTileClassForAnnotations(FramedSimpleCompactingDrawerTile.class); + + EventManager.forge(PlayerInteractEvent.LeftClickBlock.class) + .process(event -> { + var state = event.getLevel().getBlockState(event.getPos()); + if (event.getLevel().getBlockState(event.getPos()).getBlock() instanceof Drawer drawer) { + final int hit = drawer.getHit(state, event.getLevel(), event.getPos(), event.getEntity()); + if (hit != -1) { + TileUtil.getTileEntity(event.getLevel(), event.getPos(), ControllableDrawerTile.class) + .ifPresent(be -> { + be.onClicked(event.getEntity(), hit); + event.setCanceled(true); + }); + } + } + }) + .subscribe(); } diff --git a/src/main/java/com/buuz135/functionalstorage/block/CompactingDrawerBlock.java b/src/main/java/com/buuz135/functionalstorage/block/CompactingDrawerBlock.java index 5a9152f4..30d1c936 100644 --- a/src/main/java/com/buuz135/functionalstorage/block/CompactingDrawerBlock.java +++ b/src/main/java/com/buuz135/functionalstorage/block/CompactingDrawerBlock.java @@ -61,7 +61,7 @@ import java.util.Optional; import java.util.function.Consumer; -public class CompactingDrawerBlock extends RotatableBlock { +public class CompactingDrawerBlock extends RotatableBlock implements Drawer { public static Multimap CACHED_SHAPES = MultimapBuilder.hashKeys().arrayListValues().build(); @@ -143,6 +143,7 @@ public void attack(BlockState state, Level worldIn, BlockPos pos, Player player) TileUtil.getTileEntity(worldIn, pos, CompactingDrawerTile.class).ifPresent(drawerTile -> drawerTile.onClicked(player, getHit(state, worldIn, pos, player))); } + @Override public int getHit(BlockState state, Level worldIn, BlockPos pos, Player player) { HitResult result = RayTraceUtils.rayTraceSimple(worldIn, player, 32, 0); if (result instanceof BlockHitResult) { diff --git a/src/main/java/com/buuz135/functionalstorage/block/Drawer.java b/src/main/java/com/buuz135/functionalstorage/block/Drawer.java new file mode 100644 index 00000000..e1b2ba83 --- /dev/null +++ b/src/main/java/com/buuz135/functionalstorage/block/Drawer.java @@ -0,0 +1,10 @@ +package com.buuz135.functionalstorage.block; + +import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.state.BlockState; + +public interface Drawer { + int getHit(BlockState state, Level worldIn, BlockPos pos, Player player); +} diff --git a/src/main/java/com/buuz135/functionalstorage/block/DrawerBlock.java b/src/main/java/com/buuz135/functionalstorage/block/DrawerBlock.java index e8ff2ba5..ea6d74fc 100644 --- a/src/main/java/com/buuz135/functionalstorage/block/DrawerBlock.java +++ b/src/main/java/com/buuz135/functionalstorage/block/DrawerBlock.java @@ -68,7 +68,7 @@ import java.util.Optional; import java.util.function.Consumer; -public class DrawerBlock extends RotatableBlock { +public class DrawerBlock extends RotatableBlock implements Drawer { public static HashMap> CACHED_SHAPES = new HashMap<>(); @@ -188,6 +188,7 @@ public void attack(BlockState state, Level worldIn, BlockPos pos, Player player) TileUtil.getTileEntity(worldIn, pos, DrawerTile.class).ifPresent(drawerTile -> drawerTile.onClicked(player, getHit(state, worldIn, pos, player))); } + @Override public int getHit(BlockState state, Level worldIn, BlockPos pos, Player player) { HitResult result = RayTraceUtils.rayTraceSimple(worldIn, player, 32, 0); if (result instanceof BlockHitResult) { diff --git a/src/main/java/com/buuz135/functionalstorage/block/EnderDrawerBlock.java b/src/main/java/com/buuz135/functionalstorage/block/EnderDrawerBlock.java index 8f0d8fd6..f3b52977 100644 --- a/src/main/java/com/buuz135/functionalstorage/block/EnderDrawerBlock.java +++ b/src/main/java/com/buuz135/functionalstorage/block/EnderDrawerBlock.java @@ -52,7 +52,7 @@ import static com.buuz135.functionalstorage.block.DrawerBlock.LOCKED; -public class EnderDrawerBlock extends RotatableBlock { +public class EnderDrawerBlock extends RotatableBlock implements Drawer { public EnderDrawerBlock() { super("ender_drawer", Properties.copy(Blocks.ENDER_CHEST), EnderDrawerTile.class); @@ -125,6 +125,7 @@ public void attack(BlockState state, Level worldIn, BlockPos pos, Player player) TileUtil.getTileEntity(worldIn, pos, EnderDrawerTile.class).ifPresent(drawerTile -> drawerTile.onClicked(player, getHit(state, worldIn, pos, player))); } + @Override public int getHit(BlockState state, Level worldIn, BlockPos pos, Player player) { HitResult result = RayTraceUtils.rayTraceSimple(worldIn, player, 32, 0); if (result instanceof BlockHitResult) { diff --git a/src/main/java/com/buuz135/functionalstorage/block/FluidDrawerBlock.java b/src/main/java/com/buuz135/functionalstorage/block/FluidDrawerBlock.java index 32cc4df5..0e4fdda7 100644 --- a/src/main/java/com/buuz135/functionalstorage/block/FluidDrawerBlock.java +++ b/src/main/java/com/buuz135/functionalstorage/block/FluidDrawerBlock.java @@ -58,7 +58,7 @@ import java.util.Optional; import java.util.function.Consumer; -public class FluidDrawerBlock extends RotatableBlock { +public class FluidDrawerBlock extends RotatableBlock implements Drawer { /** * Framed version @@ -139,6 +139,7 @@ public void attack(BlockState state, Level worldIn, BlockPos pos, Player player) TileUtil.getTileEntity(worldIn, pos, FluidDrawerTile.class).ifPresent(drawerTile -> drawerTile.onClicked(player, getHit(state, worldIn, pos, player))); } + @Override public int getHit(BlockState state, Level worldIn, BlockPos pos, Player player) { HitResult result = RayTraceUtils.rayTraceSimple(worldIn, player, 32, 0); if (result instanceof BlockHitResult) {