Allow sneak-wrenching any block in the mod to break it.

Move playSound for block break/place/etc to util method.
This commit is contained in:
Florian Nücke
2020-12-27 16:36:03 +01:00
parent c6d95fc55f
commit 35b67744cd
4 changed files with 47 additions and 15 deletions

View File

@@ -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;
}

View File

@@ -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()) {

View File

@@ -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);
}
}

View File

@@ -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<SoundType, SoundEvent> 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);
}
}