From cc8c6ba61de41e97aa85cb5258e89feba89016f3 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 6 Oct 2023 14:40:09 +0200 Subject: [PATCH] Fix SmartPlaceListener sound on force place Signed-off-by: yoyosource --- .../smartplace/SmartPlaceListener.java | 3 ++ .../bausystem/utils/PlaceItemUtils.java | 37 +++++++++++-------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/smartplace/SmartPlaceListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/smartplace/SmartPlaceListener.java index 3879b175..75f60d53 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/smartplace/SmartPlaceListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/smartplace/SmartPlaceListener.java @@ -80,6 +80,9 @@ public class SmartPlaceListener implements Listener { } player.playSound(block.getLocation(), soundGroup.getPlaceSound(), soundGroup.getVolume() * 0.8F, soundGroup.getPitch() * 0.8F); }); + if (result.wasForced()) { + event.getPlayer().playSound(block.getLocation(), soundGroup.getPlaceSound(), soundGroup.getVolume() * 0.8F, soundGroup.getPitch() * 0.8F); + } } // Fix Double sound for original player } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/PlaceItemUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/utils/PlaceItemUtils.java index df31c32f..2f83c416 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/utils/PlaceItemUtils.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/utils/PlaceItemUtils.java @@ -39,6 +39,7 @@ import org.bukkit.util.RayTraceResult; import org.bukkit.util.Vector; import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; @UtilityClass @@ -143,35 +144,36 @@ public class PlaceItemUtils { Material typeToPlace = ITEM_MATERIAL_TO_BLOCK_MATERIAL.getOrDefault(itemStack.getType(), itemStack.getType()); BlockData blockData = null; + AtomicBoolean usedForcePlace = new AtomicBoolean(); if (againstSide == BlockFace.NORTH || againstSide == BlockFace.SOUTH || againstSide == BlockFace.EAST || againstSide == BlockFace.WEST) { // Try Wall Placement first blockData = toBlockData(player, blockDataMeta, BLOCK_MATERIAL_TO_WALL_BLOCK_MATERIAL.getOrDefault(typeToPlace, typeToPlace)); - if (blockData != null && !canPlace(block, blockData, force)) { + if (blockData != null && !canPlace(block, blockData, force, usedForcePlace)) { // Check if default Rotation from input could be valid BlockFace rotation = getRotation(blockData); setRotation(blockData, againstSide); - if (!canPlace(block, blockData, force)) { + if (!canPlace(block, blockData, force, usedForcePlace)) { setRotation(blockData, rotation); } } } // Try default Placement - if (blockData == null || !canPlace(block, blockData, force)) { + if (blockData == null || !canPlace(block, blockData, force, usedForcePlace)) { blockData = toBlockData(player, blockDataMeta, typeToPlace); } - if (blockData != null && !canPlace(block, blockData, force)) { + if (blockData != null && !canPlace(block, blockData, force, usedForcePlace)) { if (blockData instanceof FaceAttachable) { // FaceAttachable Blocks should be placed on the Ceiling/Floor if possible // This applies mainly to Lever and Buttons FaceAttachable faceAttachable = (FaceAttachable) blockData; boolean topFirst = isHitHalfTop(player); faceAttachable.setAttachedFace(topFirst ? FaceAttachable.AttachedFace.CEILING : FaceAttachable.AttachedFace.FLOOR); - if (!canPlace(block, blockData, force)) { + if (!canPlace(block, blockData, force, usedForcePlace)) { faceAttachable.setAttachedFace(topFirst ? FaceAttachable.AttachedFace.FLOOR : FaceAttachable.AttachedFace.CEILING); } - if (!canPlace(block, blockData, force)) { + if (!canPlace(block, blockData, force, usedForcePlace)) { return PlaceItemResult.NO_VALID_PLACEMENT; } } @@ -263,10 +265,6 @@ public class PlaceItemUtils { redstoneWire.setFace(BlockFace.WEST, RedstoneWire.Connection.SIDE); } - if (force) { - - } - if (rotateAway) { // Rotate the other way if rotateAway is set to true BlockFace blockFace = getRotation(blockData); @@ -281,7 +279,7 @@ public class PlaceItemUtils { } // Check if the Block can be build - BlockCanBuildEvent blockCanBuildEvent = new BlockCanBuildEvent(against, player, blockData, canPlace(block, blockData, force)); + BlockCanBuildEvent blockCanBuildEvent = new BlockCanBuildEvent(against, player, blockData, canPlace(block, blockData, force, usedForcePlace)); Bukkit.getPluginManager().callEvent(blockCanBuildEvent); if (!blockCanBuildEvent.isBuildable()) return PlaceItemResult.NO_BUILD; @@ -338,7 +336,7 @@ public class PlaceItemUtils { Bisected bisected = (Bisected) blockData; Block blockAbove = block.getRelative(0, 1, 0); bisected.setHalf(Bisected.Half.TOP); - if (canPlace(blockAbove, blockData, force)) { + if (canPlace(blockAbove, blockData, force, usedForcePlace)) { if (blockData instanceof Waterlogged) { ((Waterlogged) blockData).setWaterlogged(blockAbove.getType() == Material.WATER); } @@ -375,7 +373,7 @@ public class PlaceItemUtils { SoundGroup soundGroup = blockData.getSoundGroup(); block.getWorld().playSound(block.getLocation(), soundGroup.getPlaceSound(), soundGroup.getVolume() * 0.8F, soundGroup.getPitch() * 0.8F); } - return PlaceItemResult.SUCCESS; + return usedForcePlace.get() ? PlaceItemResult.SUCCESS_FORCE : PlaceItemResult.SUCCESS; } public BlockFace[] axis = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST }; @@ -523,9 +521,11 @@ public class PlaceItemUtils { return blockData.getClass().getName().contains("Tall"); } - private boolean canPlace(Block block, BlockData blockData, boolean force) { - if (!force && !block.canPlace(blockData)) return false; - if (block.getType().isSolid()) return false; + private boolean canPlace(Block block, BlockData blockData, boolean force, AtomicBoolean usedForcePlace) { + if (!block.canPlace(blockData)) { + if (force) usedForcePlace.set(true); + return force; + } return replaceables.contains(block.getType().name()); } @@ -540,8 +540,13 @@ public class PlaceItemUtils { NO_DOUBLE_HIGH_BLOCK_SPACE(false), NO_PLACE_INSIDE_PLAYER(false), SUCCESS(true), + SUCCESS_FORCE(true), ; private final boolean success; + + public boolean wasForced() { + return this == SUCCESS_FORCE; + } } }