Skip to content

Commit

Permalink
Fix ghost blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
btarg committed Dec 6, 2023
1 parent 1b18101 commit b213edd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package io.github.btarg.listeners.blocks;

import io.github.btarg.blockdata.CustomBlockPersistentData;
import io.github.btarg.definitions.CustomBlockDefinition;
import io.github.btarg.registry.RegistryHelper;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
Expand All @@ -15,19 +11,6 @@

public class CustomBlockFunctions {

public static void onCustomBlockBroken(Location location, String breakSound) {

CustomBlockPersistentData.removeBlockFromStorage(location);

if (breakSound != null && !breakSound.isEmpty()) {
try {
location.getWorld().playSound(location, Sound.valueOf(breakSound), 1, 1);
} catch (IllegalArgumentException e) {
Bukkit.getLogger().warning("Block being broken does not have a valid sound!");
}
}
}

public static void dropBlockItems(Entity entity, CustomBlockDefinition definition, Block blockBroken) {
if (definition == null) return;
if (entity instanceof Player player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,17 @@ public void onCraftItemFrame(CraftItemEvent event) {

@EventHandler
public void onBlockBroken(BlockBreakEvent e) {

Entity linkedItemDisplay = CustomBlockUtils.getLinkedItemDisplay(e.getBlock().getLocation());
if (linkedItemDisplay == null) return;
CustomBlockDefinition definition = CustomBlockUtils.getDefinitionFromItemDisplay(linkedItemDisplay);
CustomBlockDefinition definition = CustomBlockUtils.getDefinitionFromBlock(e.getBlock());
if (definition == null) return;
e.setDropItems(false);

ItemStack itemUsed = e.getPlayer().getInventory().getItemInMainHand();
Player player = e.getPlayer();
ItemStack itemUsed = player.getInventory().getItemInMainHand();

if (ToolLevelHelper.getToolLevel(itemUsed, false) >= definition.toolLevelRequired && ToolLevelHelper.checkItemTypeByString(itemUsed, definition.canBeMinedWith)) {

if (e.getPlayer().getGameMode() != GameMode.CREATIVE) {
if (player.getGameMode() != GameMode.CREATIVE) {

boolean silkTouch = false;
if (itemUsed.hasItemMeta())
Expand All @@ -154,14 +153,24 @@ public void onBlockBroken(BlockBreakEvent e) {
if (!silkTouch)
e.setExpToDrop(definition.dropExperience);

CustomBlockFunctions.dropBlockItems(e.getPlayer(), definition, e.getBlock());
CustomBlockFunctions.dropBlockItems(player, definition, e.getBlock());
}
}

// Remove item frame and remove block from database
definition.executeEvent(EventNames.ON_BROKEN.toString(), e.getPlayer());
CustomBlockFunctions.onCustomBlockBroken(e.getBlock().getLocation(), definition.breakSound);
linkedItemDisplay.remove();
if (linkedItemDisplay != null)
linkedItemDisplay.remove();

if (definition.breakSound != null && !definition.breakSound.isEmpty()) {
try {
player.getWorld().playSound(e.getBlock().getLocation(), Sound.valueOf(definition.breakSound), 1, 1);
} catch (IllegalArgumentException ex) {
Bukkit.getLogger().warning("Block being broken does not have a valid sound!");
}
}

CustomBlockPersistentData.removeBlockFromStorage(e.getBlock().getLocation());

}

Expand Down

0 comments on commit b213edd

Please sign in to comment.