From 66b77ee75a24dac13f9cbf95242b504e1494dde7 Mon Sep 17 00:00:00 2001 From: Intelli Date: Thu, 12 Oct 2023 11:28:53 -0600 Subject: [PATCH] Improved safety block handling during rollback teleports (#328) --- .../java/net/coreprotect/CoreProtect.java | 14 +++++++++++ .../net/coreprotect/language/Language.java | 2 +- .../net/coreprotect/utility/Teleport.java | 25 ++++++++++++++++--- .../java/net/coreprotect/utility/Util.java | 4 +++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/coreprotect/CoreProtect.java b/src/main/java/net/coreprotect/CoreProtect.java index ba9d9ea..7530af1 100755 --- a/src/main/java/net/coreprotect/CoreProtect.java +++ b/src/main/java/net/coreprotect/CoreProtect.java @@ -1,9 +1,13 @@ package net.coreprotect; import java.io.File; +import java.util.Iterator; +import java.util.Map.Entry; import org.bstats.bukkit.MetricsLite; import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.block.data.BlockData; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; @@ -24,6 +28,7 @@ import net.coreprotect.thread.NetworkHandler; import net.coreprotect.thread.Scheduler; import net.coreprotect.utility.Chat; import net.coreprotect.utility.Color; +import net.coreprotect.utility.Teleport; import net.coreprotect.utility.Util; public final class CoreProtect extends JavaPlugin { @@ -168,6 +173,15 @@ public final class CoreProtect extends JavaPlugin { } } + if (!ConfigHandler.isFolia) { + Iterator> iterator = Teleport.revertBlocks.entrySet().iterator(); + while (iterator.hasNext()) { + Entry entry = iterator.next(); + entry.getKey().getBlock().setBlockData(entry.getValue()); + iterator.remove(); + } + } + ConfigHandler.serverRunning = false; long shutdownTime = System.currentTimeMillis(); long alertTime = shutdownTime + (10 * 1000); diff --git a/src/main/java/net/coreprotect/language/Language.java b/src/main/java/net/coreprotect/language/Language.java index 12b0cb0..d390701 100644 --- a/src/main/java/net/coreprotect/language/Language.java +++ b/src/main/java/net/coreprotect/language/Language.java @@ -52,7 +52,7 @@ public class Language { phrases.put(Phrase.DATABASE_LOCKED_4, "Disabling database locking can result in data corruption."); phrases.put(Phrase.DATABASE_UNREACHABLE, "Database is unreachable. Discarding data and shutting down."); phrases.put(Phrase.DEVELOPMENT_BRANCH, "Development branch detected, skipping patch scripts."); - phrases.put(Phrase.DIRT_BLOCK, "Placed a dirt block under you."); + phrases.put(Phrase.DIRT_BLOCK, "Placed a temporary safety block under you."); phrases.put(Phrase.DISABLE_SUCCESS, "Success! Disabled {0}"); phrases.put(Phrase.ENABLE_FAILED, "{0} was unable to start."); phrases.put(Phrase.ENABLE_SUCCESS, "{0} has been successfully enabled!"); diff --git a/src/main/java/net/coreprotect/utility/Teleport.java b/src/main/java/net/coreprotect/utility/Teleport.java index c6eee12..adbec23 100644 --- a/src/main/java/net/coreprotect/utility/Teleport.java +++ b/src/main/java/net/coreprotect/utility/Teleport.java @@ -3,17 +3,21 @@ package net.coreprotect.utility; import java.util.Arrays; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.data.BlockData; import org.bukkit.entity.Player; +import net.coreprotect.CoreProtect; import net.coreprotect.config.ConfigHandler; import net.coreprotect.language.Phrase; import net.coreprotect.model.BlockGroup; import net.coreprotect.paper.PaperAdapter; +import net.coreprotect.thread.Scheduler; public class Teleport { @@ -21,6 +25,8 @@ public class Teleport { throw new IllegalStateException("Utility class"); } + public static ConcurrentHashMap revertBlocks = new ConcurrentHashMap<>(); + public static void performSafeTeleport(Player player, Location location, boolean enforceTeleport) { try { Set unsafeBlocks = new HashSet<>(Arrays.asList(Material.LAVA)); @@ -46,20 +52,33 @@ public class Teleport { Material type1 = block1.getType(); Material type2 = block2.getType(); - if (!Util.solidBlock(type1) && !Util.solidBlock(type2)) { + if (Util.passableBlock(block1) && Util.passableBlock(block2)) { if (unsafeBlocks.contains(type1)) { placeSafe = true; } else { safeBlock = true; - if (placeSafe) { + if (placeSafe && player.getGameMode() == GameMode.SURVIVAL) { int below = checkY - 1; Block blockBelow = location.getWorld().getBlockAt(playerX, below, playerZ); if (checkY < worldHeight && unsafeBlocks.contains(blockBelow.getType())) { alert = true; - block1.setType(Material.DIRT); + Location revertLocation = block1.getLocation(); + BlockData revertBlockData = block1.getBlockData(); + revertBlocks.put(revertLocation, revertBlockData); + if (!ConfigHandler.isFolia) { + block1.setType(Material.BARRIER); + } + else { + block1.setType(Material.DIRT); + } checkY++; + + Scheduler.scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> { + block1.setBlockData(revertBlockData); + revertBlocks.remove(revertLocation); + }, revertLocation, 1200); } } } diff --git a/src/main/java/net/coreprotect/utility/Util.java b/src/main/java/net/coreprotect/utility/Util.java index 6c233e0..e58724d 100755 --- a/src/main/java/net/coreprotect/utility/Util.java +++ b/src/main/java/net/coreprotect/utility/Util.java @@ -993,6 +993,10 @@ public class Util extends Queue { return type.isSolid(); } + public static boolean passableBlock(Block block) { + return block.isPassable(); + } + public static Material getType(Block block) { // Temp code return block.getType();