From f75c6373424ecf080eaf49621a92f449a2e5db3e Mon Sep 17 00:00:00 2001 From: Intybyte Date: Sun, 25 Aug 2024 23:04:18 +0200 Subject: [PATCH 1/5] Add tag --- .../java/net/countercraft/movecraft/craft/type/CraftType.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java index d0f66b287..e1718999e 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java @@ -82,6 +82,7 @@ final public class CraftType { private static final NamespacedKey CAN_FLY = buildKey("can_fly"); // Private key used to calculate BLOCKED_BY_WATER public static final NamespacedKey REQUIRE_WATER_CONTACT = buildKey("require_water_contact"); + public static final NamespacedKey WATERLOGGED_MAX_AMOUNT = buildKey("waterlogged_max_amount"); public static final NamespacedKey TRY_NUDGE = buildKey("try_nudge"); public static final NamespacedKey MOVE_BLOCKS = buildKey("move_blocks"); public static final NamespacedKey CAN_CRUISE = buildKey("can_cruise"); @@ -412,6 +413,7 @@ public static void registerTypeValidator(Predicate validator, String registerProperty(new BooleanProperty("blockedByWater", BLOCKED_BY_WATER, type -> true)); registerProperty(new BooleanProperty("canFly", CAN_FLY, type -> type.getBoolProperty(BLOCKED_BY_WATER))); registerProperty(new BooleanProperty("requireWaterContact", REQUIRE_WATER_CONTACT, type -> false)); + registerProperty(new StringProperty("waterloggedMaxAmount", WATERLOGGED_MAX_AMOUNT, type -> "0")); registerProperty(new BooleanProperty("tryNudge", TRY_NUDGE, type -> false)); registerProperty(new RequiredBlockProperty("moveblocks", MOVE_BLOCKS, type -> new HashSet<>())); registerProperty(new BooleanProperty("canCruise", CAN_CRUISE, type -> false)); From b018ac6e3ab60391706a09ec8b7cf8900a05a74d Mon Sep 17 00:00:00 2001 From: Intybyte Date: Mon, 26 Aug 2024 09:05:29 +0200 Subject: [PATCH 2/5] Add detection task --- .../tasks/detection/DetectionTask.java | 4 +- .../validators/WaterloggedBlockValidator.java | 83 +++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/WaterloggedBlockValidator.java diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/DetectionTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/DetectionTask.java index 1cf1eff8c..fad2cb615 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/DetectionTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/DetectionTask.java @@ -26,6 +26,7 @@ import net.countercraft.movecraft.processing.tasks.detection.validators.PilotSignValidator; import net.countercraft.movecraft.processing.tasks.detection.validators.SizeValidator; import net.countercraft.movecraft.processing.tasks.detection.validators.WaterContactValidator; +import net.countercraft.movecraft.processing.tasks.detection.validators.WaterloggedBlockValidator; import net.countercraft.movecraft.util.AtomicLocationSet; import net.countercraft.movecraft.util.CollectionUtils; import net.countercraft.movecraft.util.Tags; @@ -95,7 +96,8 @@ public class DetectionTask implements Supplier { private static final List>>> COMPLETION_VALIDATORS = List.of( new SizeValidator(), new FlyBlockValidator(), - new DetectionBlockValidator() + new DetectionBlockValidator(), + new WaterloggedBlockValidator() ); private static final List>>> VISITED_VALIDATORS = List.of( new WaterContactValidator() diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/WaterloggedBlockValidator.java b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/WaterloggedBlockValidator.java new file mode 100644 index 000000000..d198ccbad --- /dev/null +++ b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/WaterloggedBlockValidator.java @@ -0,0 +1,83 @@ +package net.countercraft.movecraft.processing.tasks.detection.validators; + +import net.countercraft.movecraft.MovecraftLocation; +import net.countercraft.movecraft.craft.type.CraftType; +import net.countercraft.movecraft.processing.MovecraftWorld; +import net.countercraft.movecraft.processing.functions.DetectionPredicate; +import net.countercraft.movecraft.processing.functions.Result; +import org.bukkit.Material; +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.Waterlogged; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Deque; +import java.util.Map; + +public class WaterloggedBlockValidator implements DetectionPredicate>> { + @Override + public @NotNull Result validate(@NotNull Map> materialDequeMap, @NotNull CraftType type, @NotNull MovecraftWorld world, @Nullable Player player) { + String allowedAmountString = type.getStringProperty(CraftType.WATERLOGGED_MAX_AMOUNT); + boolean blockNumber = !allowedAmountString.startsWith("N"); //if false use block percentage + final String errorMessage = "Too many waterlogged blocks on craft"; + + int maxAmount; + try { + if (blockNumber) { + maxAmount = Integer.parseInt(allowedAmountString.substring(1)); + } else { + maxAmount = Integer.parseInt(allowedAmountString); + } + } catch (NumberFormatException e) { + return Result.failWithMessage("waterloggedMaxAmount wasn't configurated properly"); + } + + final int waterLoggedBlocks = getWaterLoggedAmount(materialDequeMap, world); + if (waterLoggedBlocks == 0) + return Result.succeed(); + + if (maxAmount == 0) + return Result.failWithMessage(errorMessage); + + if(blockNumber) { + if (waterLoggedBlocks <= maxAmount) { + return Result.succeed(); + } else { + return Result.failWithMessage(errorMessage); + } + } else { + int allBlocks = getTotalBlocks(materialDequeMap); + double percentage = ((double) waterLoggedBlocks / allBlocks) * 100; + + if (percentage <= maxAmount) { + return Result.succeed(); + } else { + return Result.failWithMessage(errorMessage); + } + } + } + + public int getWaterLoggedAmount(Map> materialDequeMap, MovecraftWorld world) { + int amount = 0; + for (var locationList : materialDequeMap.values()) { + for (var location : locationList) { + BlockData blockData = world.getData(location); + if (blockData instanceof Waterlogged waterlogged) { + if (waterlogged.isWaterlogged()) amount++; + } + } + } + + return amount; + } + + public int getTotalBlocks(Map> materialDequeMap) { + int amount = 0; + for (var locationList : materialDequeMap.values()) { + amount += locationList.size(); + } + + return amount; + } +} From 7c472d6a89de22d0e138b250935f42569b36f216 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Fri, 30 Aug 2024 19:43:04 +0200 Subject: [PATCH 3/5] Name changes --- .../movecraft/processing/tasks/detection/DetectionTask.java | 4 ++-- ...terloggedBlockValidator.java => LiquidBlockValidator.java} | 4 ++-- .../java/net/countercraft/movecraft/craft/type/CraftType.java | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) rename Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/{WaterloggedBlockValidator.java => LiquidBlockValidator.java} (95%) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/DetectionTask.java b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/DetectionTask.java index fad2cb615..6a58b98e7 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/DetectionTask.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/DetectionTask.java @@ -26,7 +26,7 @@ import net.countercraft.movecraft.processing.tasks.detection.validators.PilotSignValidator; import net.countercraft.movecraft.processing.tasks.detection.validators.SizeValidator; import net.countercraft.movecraft.processing.tasks.detection.validators.WaterContactValidator; -import net.countercraft.movecraft.processing.tasks.detection.validators.WaterloggedBlockValidator; +import net.countercraft.movecraft.processing.tasks.detection.validators.LiquidBlockValidator; import net.countercraft.movecraft.util.AtomicLocationSet; import net.countercraft.movecraft.util.CollectionUtils; import net.countercraft.movecraft.util.Tags; @@ -97,7 +97,7 @@ public class DetectionTask implements Supplier { new SizeValidator(), new FlyBlockValidator(), new DetectionBlockValidator(), - new WaterloggedBlockValidator() + new LiquidBlockValidator() ); private static final List>>> VISITED_VALIDATORS = List.of( new WaterContactValidator() diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/WaterloggedBlockValidator.java b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/LiquidBlockValidator.java similarity index 95% rename from Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/WaterloggedBlockValidator.java rename to Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/LiquidBlockValidator.java index d198ccbad..cec92d072 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/WaterloggedBlockValidator.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/LiquidBlockValidator.java @@ -15,10 +15,10 @@ import java.util.Deque; import java.util.Map; -public class WaterloggedBlockValidator implements DetectionPredicate>> { +public class LiquidBlockValidator implements DetectionPredicate>> { @Override public @NotNull Result validate(@NotNull Map> materialDequeMap, @NotNull CraftType type, @NotNull MovecraftWorld world, @Nullable Player player) { - String allowedAmountString = type.getStringProperty(CraftType.WATERLOGGED_MAX_AMOUNT); + String allowedAmountString = type.getStringProperty(CraftType.LIQUIDS_MAX_AMOUNT); boolean blockNumber = !allowedAmountString.startsWith("N"); //if false use block percentage final String errorMessage = "Too many waterlogged blocks on craft"; diff --git a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java index e1718999e..840b4e56b 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/type/CraftType.java @@ -82,7 +82,7 @@ final public class CraftType { private static final NamespacedKey CAN_FLY = buildKey("can_fly"); // Private key used to calculate BLOCKED_BY_WATER public static final NamespacedKey REQUIRE_WATER_CONTACT = buildKey("require_water_contact"); - public static final NamespacedKey WATERLOGGED_MAX_AMOUNT = buildKey("waterlogged_max_amount"); + public static final NamespacedKey LIQUIDS_MAX_AMOUNT = buildKey("liquids_max_amount"); public static final NamespacedKey TRY_NUDGE = buildKey("try_nudge"); public static final NamespacedKey MOVE_BLOCKS = buildKey("move_blocks"); public static final NamespacedKey CAN_CRUISE = buildKey("can_cruise"); @@ -413,7 +413,7 @@ public static void registerTypeValidator(Predicate validator, String registerProperty(new BooleanProperty("blockedByWater", BLOCKED_BY_WATER, type -> true)); registerProperty(new BooleanProperty("canFly", CAN_FLY, type -> type.getBoolProperty(BLOCKED_BY_WATER))); registerProperty(new BooleanProperty("requireWaterContact", REQUIRE_WATER_CONTACT, type -> false)); - registerProperty(new StringProperty("waterloggedMaxAmount", WATERLOGGED_MAX_AMOUNT, type -> "0")); + registerProperty(new StringProperty("liquidsMaxAmount", LIQUIDS_MAX_AMOUNT, type -> "0")); registerProperty(new BooleanProperty("tryNudge", TRY_NUDGE, type -> false)); registerProperty(new RequiredBlockProperty("moveblocks", MOVE_BLOCKS, type -> new HashSet<>())); registerProperty(new BooleanProperty("canCruise", CAN_CRUISE, type -> false)); From a1bd4aa60e86f399cf71e18a020a190ca2401977 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Fri, 30 Aug 2024 19:51:54 +0200 Subject: [PATCH 4/5] Add checks for liquids --- .../validators/LiquidBlockValidator.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/LiquidBlockValidator.java b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/LiquidBlockValidator.java index cec92d072..eb8e9691c 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/LiquidBlockValidator.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/LiquidBlockValidator.java @@ -5,6 +5,7 @@ import net.countercraft.movecraft.processing.MovecraftWorld; import net.countercraft.movecraft.processing.functions.DetectionPredicate; import net.countercraft.movecraft.processing.functions.Result; +import net.countercraft.movecraft.util.Tags; import org.bukkit.Material; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Waterlogged; @@ -30,25 +31,25 @@ public class LiquidBlockValidator implements DetectionPredicate> materialDequeMap, MovecraftWorld world) { + public int getLiquidAmount(Map> materialDequeMap, MovecraftWorld world) { int amount = 0; - for (var locationList : materialDequeMap.values()) { - for (var location : locationList) { + for (var locationList : materialDequeMap.entrySet()) { + final Deque locations = locationList.getValue(); + + if (Tags.FLUID.contains(locationList.getKey())) { + amount += locations.size(); + continue; + } + + for (var location : locations) { BlockData blockData = world.getData(location); - if (blockData instanceof Waterlogged waterlogged) { - if (waterlogged.isWaterlogged()) amount++; + + if (blockData instanceof Waterlogged waterlogged && waterlogged.isWaterlogged()) { + amount++; } } } From 98b6d5b6c22a74e452594738c530f513ae30209f Mon Sep 17 00:00:00 2001 From: Intybyte Date: Thu, 5 Sep 2024 15:29:19 +0200 Subject: [PATCH 5/5] Water only --- .../tasks/detection/validators/LiquidBlockValidator.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/LiquidBlockValidator.java b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/LiquidBlockValidator.java index eb8e9691c..42a77b6d5 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/LiquidBlockValidator.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/processing/tasks/detection/validators/LiquidBlockValidator.java @@ -5,7 +5,6 @@ import net.countercraft.movecraft.processing.MovecraftWorld; import net.countercraft.movecraft.processing.functions.DetectionPredicate; import net.countercraft.movecraft.processing.functions.Result; -import net.countercraft.movecraft.util.Tags; import org.bukkit.Material; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Waterlogged; @@ -64,7 +63,7 @@ public int getLiquidAmount(Map> materialDeque for (var locationList : materialDequeMap.entrySet()) { final Deque locations = locationList.getValue(); - if (Tags.FLUID.contains(locationList.getKey())) { + if (locationList.getKey() == Material.WATER || locationList.getKey() == Material.BUBBLE_COLUMN) { amount += locations.size(); continue; }