From 35b67744cdef7e2a700fb5b58915efb6b4cb873a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sun, 27 Dec 2020 16:36:03 +0100 Subject: [PATCH] Allow sneak-wrenching any block in the mod to break it. Move playSound for block break/place/etc to util method. --- .../cil/oc2/common/block/BusCableBlock.java | 4 +-- .../cil/oc2/common/block/ComputerBlock.java | 11 ++---- .../li/cil/oc2/common/item/WrenchItem.java | 35 ++++++++++++++++--- .../li/cil/oc2/common/util/WorldUtils.java | 12 +++++++ 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/main/java/li/cil/oc2/common/block/BusCableBlock.java b/src/main/java/li/cil/oc2/common/block/BusCableBlock.java index 02abb656..7d0fa9d9 100644 --- a/src/main/java/li/cil/oc2/common/block/BusCableBlock.java +++ b/src/main/java/li/cil/oc2/common/block/BusCableBlock.java @@ -112,7 +112,7 @@ public final class BusCableBlock extends Block { if (!world.isRemote()) { world.setBlockState(pos, state.with(property, ConnectionType.PLUG)); onConnectionTypeChanged(world, pos, side); - world.playSound(null, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, soundType.getPlaceSound(), SoundCategory.BLOCKS, (soundType.getVolume() + 1f) / 2f, soundType.getPitch() * 0.8f); + WorldUtils.playSound(world, pos, state.getSoundType(), SoundType::getPlaceSound); } return true; } @@ -171,7 +171,7 @@ public final class BusCableBlock extends Block { }); } - world.playSound(null, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, soundType.getBreakSound(), SoundCategory.BLOCKS, (soundType.getVolume() + 1f) / 2f, soundType.getPitch() * 0.8f); + WorldUtils.playSound(world, pos, state.getSoundType(), SoundType::getBreakSound); return ActionResultType.SUCCESS; } diff --git a/src/main/java/li/cil/oc2/common/block/ComputerBlock.java b/src/main/java/li/cil/oc2/common/block/ComputerBlock.java index dee23f38..9821202b 100644 --- a/src/main/java/li/cil/oc2/common/block/ComputerBlock.java +++ b/src/main/java/li/cil/oc2/common/block/ComputerBlock.java @@ -25,7 +25,6 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ActionResultType; import net.minecraft.util.Direction; import net.minecraft.util.Hand; -import net.minecraft.util.SoundCategory; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockRayTraceResult; import net.minecraft.util.text.ITextComponent; @@ -86,14 +85,8 @@ public final class ComputerBlock extends HorizontalBlock { final ComputerTileEntity computer = (ComputerTileEntity) tileEntity; final ItemStack heldItem = player.getHeldItem(hand); if (Wrenches.isWrench(heldItem)) { - if (!world.isRemote() && player instanceof ServerPlayerEntity) { - final ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player; - if (player.isSneaking()) { - serverPlayer.interactionManager.tryHarvestBlock(pos); - world.playSound(null, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, soundType.getBreakSound(), SoundCategory.BLOCKS, (soundType.getVolume() + 1f) / 2f, soundType.getPitch() * 0.8f); - } else { - openContainerScreen(computer, serverPlayer); - } + if (player instanceof ServerPlayerEntity) { + openContainerScreen(computer, (ServerPlayerEntity) player); } } else { if (player.isSneaking()) { diff --git a/src/main/java/li/cil/oc2/common/item/WrenchItem.java b/src/main/java/li/cil/oc2/common/item/WrenchItem.java index 71996fbf..bc7ab508 100644 --- a/src/main/java/li/cil/oc2/common/item/WrenchItem.java +++ b/src/main/java/li/cil/oc2/common/item/WrenchItem.java @@ -1,10 +1,20 @@ package li.cil.oc2.common.item; +import li.cil.oc2.api.API; +import li.cil.oc2.common.util.WorldUtils; +import net.minecraft.block.BlockState; +import net.minecraft.block.SoundType; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemUseContext; +import net.minecraft.server.management.PlayerInteractionManager; +import net.minecraft.util.ActionResultType; +import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; -import net.minecraft.world.IWorldReader; +import net.minecraft.world.World; + +import java.util.Objects; public final class WrenchItem extends Item { public WrenchItem(final Properties properties) { @@ -12,7 +22,24 @@ public final class WrenchItem extends Item { } @Override - public boolean doesSneakBypassUse(final ItemStack stack, final IWorldReader world, final BlockPos pos, final PlayerEntity player) { - return true; + public ActionResultType onItemUse(final ItemUseContext context) { + final World world = context.getWorld(); + final BlockPos pos = context.getPos(); + final BlockState state = world.getBlockState(pos); + final ResourceLocation registryName = state.getBlock().getRegistryName(); + if (registryName == null || !Objects.equals(registryName.getNamespace(), API.MOD_ID)) { + return super.onItemUse(context); + } + + final PlayerEntity player = context.getPlayer(); + if (player instanceof ServerPlayerEntity) { + final PlayerInteractionManager interactionManager = ((ServerPlayerEntity) player).interactionManager; + if (interactionManager.tryHarvestBlock(pos)) { + WorldUtils.playSound(world, pos, state.getSoundType(), SoundType::getBreakSound); + return ActionResultType.SUCCESS; + } + } + + return super.onItemUse(context); } } diff --git a/src/main/java/li/cil/oc2/common/util/WorldUtils.java b/src/main/java/li/cil/oc2/common/util/WorldUtils.java index b18758b1..6d662a3b 100644 --- a/src/main/java/li/cil/oc2/common/util/WorldUtils.java +++ b/src/main/java/li/cil/oc2/common/util/WorldUtils.java @@ -1,13 +1,17 @@ package li.cil.oc2.common.util; import net.minecraft.block.Block; +import net.minecraft.block.SoundType; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ResourceLocation; +import net.minecraft.util.SoundCategory; +import net.minecraft.util.SoundEvent; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; import net.minecraft.world.IWorld; import javax.annotation.Nullable; +import java.util.function.Function; public final class WorldUtils { @Nullable @@ -49,4 +53,12 @@ public final class WorldUtils { return block.getClass().getSimpleName(); } + + public static void playSound(final IWorld world, final BlockPos pos, final SoundType soundType, Function soundEvent) { + playSound(world, pos, soundType, soundEvent.apply(soundType)); + } + + public static void playSound(final IWorld world, final BlockPos pos, final SoundType soundType, final SoundEvent soundEvent) { + world.playSound(null, pos, soundEvent, SoundCategory.BLOCKS, (soundType.getVolume() + 1f) / 2f, soundType.getPitch() * 0.8f); + } }