From ddc0436e4385d2f513a53d44474e6c876e035702 Mon Sep 17 00:00:00 2001 From: asofold Date: Fri, 26 May 2017 14:12:50 +0200 Subject: [PATCH] Fix boatsanywhere not recognizing other than oak boats. Other: * Add and use BlockProperties.isWater. --- .../checks/blockplace/BlockPlaceListener.java | 7 +-- .../nocheatplus/utilities/InventoryUtil.java | 49 +++++++++++++++++++ .../utilities/map/BlockProperties.java | 21 +++++++- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java index 13638416..cb616de6 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java @@ -48,6 +48,7 @@ import fr.neatmonster.nocheatplus.compat.Bridge1_9; import fr.neatmonster.nocheatplus.compat.BridgeMisc; import fr.neatmonster.nocheatplus.permissions.Permissions; import fr.neatmonster.nocheatplus.stats.Counters; +import fr.neatmonster.nocheatplus.utilities.InventoryUtil; import fr.neatmonster.nocheatplus.utilities.ReflectionUtil; import fr.neatmonster.nocheatplus.utilities.TickTask; import fr.neatmonster.nocheatplus.utilities.map.BlockProperties; @@ -331,7 +332,7 @@ public class BlockPlaceListener extends CheckListener { final BlockPlaceConfig cc = BlockPlaceConfig.getConfig(player); final Material type = stack.getType(); - if (type == Material.BOAT) { + if (InventoryUtil.isBoat(type)) { if (cc.preventBoatsAnywhere) { checkBoatsAnywhere(player, event); } @@ -351,7 +352,7 @@ public class BlockPlaceListener extends CheckListener { final Material mat = block.getType(); // TODO: allow lava ? - if (mat == Material.WATER || mat == Material.STATIONARY_WATER) { + if (BlockProperties.isWater(mat)) { return; } @@ -359,7 +360,7 @@ public class BlockPlaceListener extends CheckListener { final Material relMat = relBlock.getType(); // TODO: Placing inside of water, but not "against" ? - if (relMat == Material.WATER || relMat == Material.STATIONARY_WATER) { + if (BlockProperties.isWater(relMat)) { return; } diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/InventoryUtil.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/InventoryUtil.java index 24a0749b..504f9414 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/InventoryUtil.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/InventoryUtil.java @@ -14,6 +14,11 @@ */ package fr.neatmonster.nocheatplus.utilities; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryType; @@ -32,6 +37,50 @@ import fr.neatmonster.nocheatplus.utilities.map.BlockProperties; */ public class InventoryUtil { + // TODO: Better location for the boat/item stuff than InventoryUtil. + private static final Set boats = new HashSet(); + + static { + boats.add(Material.BOAT); + boats.addAll(collectItemsByPrefix("BOAT_")); // Oops: prefix. + } + + /** + * Collect non-block items by suffix of their Material name (case insensitive). + * @param suffix + * @return + */ + public static List collectItemsBySuffix(String suffix) { + suffix = suffix.toLowerCase(); + final List res = new LinkedList(); + for (final Material mat : Material.values()) { + if (!mat.isBlock() && mat.name().toLowerCase().endsWith(suffix)) { + res.add(mat); + } + } + return res; + } + + /** + * Collect non-block items by suffix of their Material name (case insensitive). + * @param prefix + * @return + */ + public static List collectItemsByPrefix(String prefix) { + prefix = prefix.toLowerCase(); + final List res = new LinkedList(); + for (final Material mat : Material.values()) { + if (!mat.isBlock() && mat.name().toLowerCase().startsWith(prefix)) { + res.add(mat); + } + } + return res; + } + + public static boolean isBoat(final Material mat) { + return boats.contains(mat); + } + /** * Does not account for special slots like armor. * diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/map/BlockProperties.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/map/BlockProperties.java index 2b32bef3..5c138ec6 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/map/BlockProperties.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/map/BlockProperties.java @@ -623,7 +623,7 @@ public class BlockProperties { public static final long F_MIN_HEIGHT16_1 = 0x80000000; // TODO: Lily pad min height of MC versions? // TODO: Convenience constants combining all height / minheight flags. - + // TODO: When flags are out, switch to per-block classes :p. // Special case activation flags. @@ -2100,6 +2100,25 @@ public class BlockProperties { return (blockFlags[id] & F_LIQUID) != 0; } + /** + * Test for water type of blocks. + * + * @param blockType + * @return + */ + public static final boolean isWater(final Material blockType) { + return isWater(getId(blockType)); + } + + /** + * Test for water type of blocks. + * @param id + * @return + */ + public static final boolean isWater(final int id) { + return (blockFlags[id] & F_WATER) != 0; + } + /** * Checks if is ice. *