diff --git a/src/main/java/me/gallowsdove/foxymachines/listeners/GhostBlockListener.java b/src/main/java/me/gallowsdove/foxymachines/listeners/GhostBlockListener.java index e101a8d..278952e 100644 --- a/src/main/java/me/gallowsdove/foxymachines/listeners/GhostBlockListener.java +++ b/src/main/java/me/gallowsdove/foxymachines/listeners/GhostBlockListener.java @@ -1,14 +1,25 @@ package me.gallowsdove.foxymachines.listeners; import me.gallowsdove.foxymachines.implementation.materials.GhostBlock; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.util.Vector; +import me.gallowsdove.foxymachines.FoxyMachines; +import org.bukkit.entity.Entity; import org.bukkit.entity.FallingBlock; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.player.PlayerFishEvent; import org.bukkit.persistence.PersistentDataType; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + public class GhostBlockListener implements Listener { + private final Map preExplosionLocations = new ConcurrentHashMap<>(); @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onHitByFishingRod(PlayerFishEvent e) { if (e.getCaught() instanceof FallingBlock b && @@ -16,4 +27,25 @@ private void onHitByFishingRod(PlayerFishEvent e) { e.setCancelled(true); } } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + private void onEntityExplode(EntityExplodeEvent e) { + for (Entity ent : e.getLocation().getWorld().getNearbyEntities(e.getLocation(), 6, 6, 6)) { + if (ent instanceof FallingBlock b && GhostBlock.isGhostBlock(b)) { + preExplosionLocations.put(b.getUniqueId(), b.getLocation()); + } + } + + Bukkit.getScheduler().runTaskLater(FoxyMachines.getInstance(), () -> { + for (Map.Entry entry : preExplosionLocations.entrySet()) { + Entity ent = Bukkit.getEntity(entry.getKey()); + if (ent instanceof FallingBlock fb && GhostBlock.isGhostBlock(fb)) { + fb.teleport(entry.getValue()); + fb.setVelocity(new Vector(0, 0, 0)); + fb.setGravity(false); + } + preExplosionLocations.remove(entry.getKey()); + } + }, 1L); + } } diff --git a/src/main/java/me/gallowsdove/foxymachines/tasks/GhostBlockTask.java b/src/main/java/me/gallowsdove/foxymachines/tasks/GhostBlockTask.java index ce8b4b8..78e9d28 100644 --- a/src/main/java/me/gallowsdove/foxymachines/tasks/GhostBlockTask.java +++ b/src/main/java/me/gallowsdove/foxymachines/tasks/GhostBlockTask.java @@ -4,6 +4,7 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Entity; import org.bukkit.entity.FallingBlock; +import org.bukkit.util.Vector; import org.bukkit.scheduler.BukkitRunnable; import java.util.HashSet; @@ -14,12 +15,15 @@ public class GhostBlockTask extends BukkitRunnable { public void run() { for (UUID uuid : new HashSet<>(GhostBlock.BLOCK_CACHE)) { Entity entity = Bukkit.getEntity(uuid); - if (!(entity instanceof FallingBlock)) { + if (!(entity instanceof FallingBlock) || !GhostBlock.isGhostBlock(entity)) { GhostBlock.BLOCK_CACHE.remove(uuid); continue; } - entity.setTicksLived(1); + FallingBlock b = (FallingBlock) entity; + b.setGravity(false); + b.setVelocity(new Vector(0, 0, 0)); + b.setTicksLived(1); } } -} +} \ No newline at end of file