From 8de7ae1c59c75a526a3ee3757bb311b755a02f2c Mon Sep 17 00:00:00 2001 From: Rammelkast Date: Thu, 27 Aug 2020 11:11:47 +0200 Subject: [PATCH] Fix #108, update XMaterial, start BoatFly --- .../check/movement/BoatFlyCheck.java | 27 ++ .../check/movement/FlightCheck.java | 4 +- .../anticheatreloaded/util/Utilities.java | 27 ++ .../anticheatreloaded/util/XMaterial.java | 409 +++++++++++------- 4 files changed, 301 insertions(+), 166 deletions(-) create mode 100644 src/main/java/com/rammelkast/anticheatreloaded/check/movement/BoatFlyCheck.java diff --git a/src/main/java/com/rammelkast/anticheatreloaded/check/movement/BoatFlyCheck.java b/src/main/java/com/rammelkast/anticheatreloaded/check/movement/BoatFlyCheck.java new file mode 100644 index 0000000..2a752ab --- /dev/null +++ b/src/main/java/com/rammelkast/anticheatreloaded/check/movement/BoatFlyCheck.java @@ -0,0 +1,27 @@ +/* + * AntiCheatReloaded for Bukkit and Spigot. + * Copyright (c) 2012-2015 AntiCheat Team + * Copyright (c) 2016-2020 Rammelkast + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.rammelkast.anticheatreloaded.check.movement; + +import com.rammelkast.anticheatreloaded.check.CheckResult; + +public class BoatFlyCheck { + + private static final CheckResult PASS = new CheckResult(CheckResult.Result.PASSED); + +} diff --git a/src/main/java/com/rammelkast/anticheatreloaded/check/movement/FlightCheck.java b/src/main/java/com/rammelkast/anticheatreloaded/check/movement/FlightCheck.java index 6600a7c..8da0e85 100644 --- a/src/main/java/com/rammelkast/anticheatreloaded/check/movement/FlightCheck.java +++ b/src/main/java/com/rammelkast/anticheatreloaded/check/movement/FlightCheck.java @@ -101,7 +101,7 @@ public class FlightCheck { && !Utilities.couldBeOnBoat(player) && (System.currentTimeMillis() - movementManager.lastTeleport >= checksConfig .getInteger(CheckType.FLIGHT, "airFlight", "accountForTeleports")) - && !VersionUtil.isSlowFalling(player) && !Utilities.isInWeb(player) + && !VersionUtil.isSlowFalling(player) && !Utilities.isNearWeb(player) && movementManager.elytraEffectTicks <= 25) return new CheckResult(CheckResult.Result.FAILED, "AirFlight", "had too little Y dropoff (diff=" + Math.abs(movementManager.motionY - movementManager.lastMotionY) + ")"); @@ -155,7 +155,7 @@ public class FlightCheck { && movementManager.motionY < 0 && !backend.justVelocity(player) && (System.currentTimeMillis() - movementManager.lastTeleport >= checksConfig .getInteger(CheckType.FLIGHT, "gravity", "accountForTeleports")) - && !Utilities.isInWeb(player) && movementManager.elytraEffectTicks <= 25 + && !Utilities.isNearWeb(player) && movementManager.elytraEffectTicks <= 25 && !VersionUtil.isSlowFalling(player)) { double gravitatedY = (movementManager.lastMotionY - 0.08) * GRAVITY_FRICTION; double offset = Math.abs(gravitatedY - movementManager.motionY); diff --git a/src/main/java/com/rammelkast/anticheatreloaded/util/Utilities.java b/src/main/java/com/rammelkast/anticheatreloaded/util/Utilities.java index 45363a2..5b0807e 100644 --- a/src/main/java/com/rammelkast/anticheatreloaded/util/Utilities.java +++ b/src/main/java/com/rammelkast/anticheatreloaded/util/Utilities.java @@ -504,6 +504,33 @@ public final class Utilities { && location.getBlock().getRelative(BlockFace.SOUTH_EAST).isLiquid() && location.getBlock().getRelative(BlockFace.SOUTH_WEST).isLiquid(); } + + /** + * Determine whether a player is near a web + * + * @param player player to check + * @return true if near a web + */ + public static boolean isNearWeb(Player player) { + return isWeb(player.getLocation().getBlock()) + || isWeb(player.getLocation().getBlock().getRelative(BlockFace.DOWN)) + || isWeb(player.getLocation().getBlock().getRelative(BlockFace.UP)) + || isWeb(player.getLocation().getBlock().getRelative(BlockFace.NORTH)) + || isWeb(player.getLocation().getBlock().getRelative(BlockFace.SOUTH)) + || isWeb(player.getLocation().getBlock().getRelative(BlockFace.EAST)) + || isWeb(player.getLocation().getBlock().getRelative(BlockFace.WEST)); + } + + + /** + * Determine whether a block is a web + * + * @param block block to check + * @return true if web + */ + public static boolean isWeb(Block block) { + return block.getType() == XMaterial.COBWEB.parseMaterial(); + } /** * Determine whether a player is in a web diff --git a/src/main/java/com/rammelkast/anticheatreloaded/util/XMaterial.java b/src/main/java/com/rammelkast/anticheatreloaded/util/XMaterial.java index 51a8b03..57dfea5 100644 --- a/src/main/java/com/rammelkast/anticheatreloaded/util/XMaterial.java +++ b/src/main/java/com/rammelkast/anticheatreloaded/util/XMaterial.java @@ -22,12 +22,26 @@ */ package com.rammelkast.anticheatreloaded.util; -import com.google.common.base.Enums; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.Validate; import org.apache.commons.lang.WordUtils; @@ -35,19 +49,18 @@ import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.*; -import java.util.concurrent.TimeUnit; -import java.util.regex.Pattern; +import com.google.common.base.Enums; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; /** * XMaterial - Data Values/Pre-flattening
* 1.13 and above as priority. *

- * This class is mainly designed to support ItemStacks. - * If you want to use it on blocks you'll have to - * use XBlock + * This class is mainly designed to support ItemStacks. If you want to use it on blocks, you'll have to use + * XBlock *

* Pre-flattening: https://minecraft.gamepedia.com/Java_Edition_data_values/Pre-flattening * Materials: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html @@ -55,9 +68,13 @@ import java.util.regex.Pattern; * Material IDs: https://minecraft-ids.grahamedgecombe.com/ * Material Source Code: https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/Material.java * XMaterial v1: https://www.spigotmc.org/threads/329630/ + *

+ * This class will throw a "unsupported material" error if someone tries to use an item with an invalid data value which can only happen in 1.12 servers and below. + * To get an invalid item, (aka Missing Texture Block) you can use the command + * /give @p minecraft:dirt 1 10 where 1 is the item amount, and 10 is the data value. The material {@link #DIRT} with a data value of {@code 10} doesn't exist. * * @author Crypto Morin - * @version 5.0.1 + * @version 6.0.2 * @see Material * @see ItemStack */ @@ -72,11 +89,11 @@ public enum XMaterial { ACACIA_PLANKS(4, "WOOD"), ACACIA_PRESSURE_PLATE("WOOD_PLATE"), ACACIA_SAPLING(4, "SAPLING"), - ACACIA_SIGN("SIGN"), + ACACIA_SIGN("SIGN_POST", "SIGN"), ACACIA_SLAB(4, "WOOD_STEP", "WOODEN_SLAB", "WOOD_DOUBLE_STEP"), ACACIA_STAIRS, ACACIA_TRAPDOOR("TRAP_DOOR"), - ACACIA_WALL_SIGN("SIGN_POST", "WALL_SIGN"), + ACACIA_WALL_SIGN("WALL_SIGN"), ACACIA_WOOD("LOG_2"), ACTIVATOR_RAIL, /** @@ -132,22 +149,22 @@ public enum XMaterial { BIRCH_PLANKS(2, "WOOD"), BIRCH_PRESSURE_PLATE("WOOD_PLATE"), BIRCH_SAPLING(2, "SAPLING"), - BIRCH_SIGN("SIGN"), + BIRCH_SIGN("SIGN_POST", "SIGN"), BIRCH_SLAB(2, "WOOD_STEP", "WOODEN_SLAB", "WOOD_DOUBLE_STEP"), BIRCH_STAIRS("BIRCH_WOOD_STAIRS"), BIRCH_TRAPDOOR("TRAP_DOOR"), - BIRCH_WALL_SIGN("SIGN_POST", "WALL_SIGN"), + BIRCH_WALL_SIGN("WALL_SIGN"), BIRCH_WOOD(2, "LOG"), BLACKSTONE("1.16"), BLACKSTONE_SLAB("1.16"), BLACKSTONE_STAIRS("1.16"), BLACKSTONE_WALL("1.16"), - BLACK_BANNER("BANNER", "STANDING_BANNER"), + BLACK_BANNER("STANDING_BANNER", "BANNER"), BLACK_BED(15, "BED_BLOCK", "BED"), BLACK_CARPET(15, "CARPET"), BLACK_CONCRETE(15, "CONCRETE"), BLACK_CONCRETE_POWDER(15, "CONCRETE_POWDER"), - BLACK_DYE("1.14", "INK_SACK"), + BLACK_DYE("1.14", "INK_SACK", "INK_SAC"), BLACK_GLAZED_TERRACOTTA(15, "1.12", "HARD_CLAY", "STAINED_CLAY", "BLACK_TERRACOTTA"), BLACK_SHULKER_BOX, BLACK_STAINED_GLASS(15, "STAINED_GLASS"), @@ -159,7 +176,7 @@ public enum XMaterial { BLAZE_POWDER, BLAZE_ROD, BLAZE_SPAWN_EGG(61, "MONSTER_EGG"), - BLUE_BANNER(4, "BANNER", "STANDING_BANNER"), + BLUE_BANNER(4, "STANDING_BANNER", "BANNER"), BLUE_BED(11, "BED_BLOCK", "BED"), BLUE_CARPET(11, "CARPET"), BLUE_CONCRETE(11, "CONCRETE"), @@ -192,7 +209,7 @@ public enum XMaterial { BRICK_SLAB(4, "STEP"), BRICK_STAIRS, BRICK_WALL, - BROWN_BANNER(3, "BANNER", "STANDING_BANNER"), + BROWN_BANNER(3, "STANDING_BANNER", "BANNER"), BROWN_BED(12, "BED_BLOCK", "BED"), BROWN_CARPET(12, "CARPET"), BROWN_CONCRETE(12, "CONCRETE"), @@ -309,19 +326,19 @@ public enum XMaterial { CRIMSON_PLANKS("1.16"), CRIMSON_PRESSURE_PLATE("1.16"), CRIMSON_ROOTS("1.16"), - CRIMSON_SIGN("1.16"), + CRIMSON_SIGN("1.16", "SIGN_POST"), CRIMSON_SLAB("1.16"), CRIMSON_STAIRS("1.16"), CRIMSON_STEM("1.16"), CRIMSON_TRAPDOOR("1.16"), - CRIMSON_WALL_SIGN("1.16"), + CRIMSON_WALL_SIGN("1.16", "WALL_SIGN"), CROSSBOW, CRYING_OBSIDIAN("1.16"), CUT_RED_SANDSTONE("1.13"), CUT_RED_SANDSTONE_SLAB("STONE_SLAB2"), CUT_SANDSTONE("1.13"), CUT_SANDSTONE_SLAB("STEP"), - CYAN_BANNER(6, "BANNER", "STANDING_BANNER"), + CYAN_BANNER(6, "STANDING_BANNER", "BANNER"), CYAN_BED(9, "BED_BLOCK", "BED"), CYAN_CARPET(9, "CARPET"), CYAN_CONCRETE(9, "CONCRETE"), @@ -346,11 +363,11 @@ public enum XMaterial { DARK_OAK_PLANKS(5, "WOOD"), DARK_OAK_PRESSURE_PLATE("WOOD_PLATE"), DARK_OAK_SAPLING(5, "SAPLING"), - DARK_OAK_SIGN("SIGN"), + DARK_OAK_SIGN("SIGN_POST", "SIGN"), DARK_OAK_SLAB(5, "WOOD_STEP", "WOODEN_SLAB", "WOOD_DOUBLE_STEP"), DARK_OAK_STAIRS, DARK_OAK_TRAPDOOR("TRAP_DOOR"), - DARK_OAK_WALL_SIGN("SIGN_POST", "WALL_SIGN"), + DARK_OAK_WALL_SIGN("WALL_SIGN"), DARK_OAK_WOOD(1, "LOG", "LOG_2"), DARK_PRISMARINE(1, "PRISMARINE"), DARK_PRISMARINE_SLAB("1.13"), @@ -437,7 +454,7 @@ public enum XMaterial { FARMLAND("SOIL"), FEATHER, FERMENTED_SPIDER_EYE, - FERN(1, "LONG_GRASS"), + FERN(2, "LONG_GRASS"), /** * For some reasons filled map items are really special. * Their data value starts from 0 and every time a player @@ -467,6 +484,7 @@ public enum XMaterial { FURNACE_MINECART("POWERED_MINECART"), GHAST_SPAWN_EGG(56, "MONSTER_EGG"), GHAST_TEAR, + GILDED_BLACKSTONE("1.16"), GLASS, GLASS_BOTTLE, GLASS_PANE("THIN_GLASS"), @@ -494,11 +512,11 @@ public enum XMaterial { GRANITE_SLAB, GRANITE_STAIRS, GRANITE_WALL, - GRASS, + GRASS(1, "LONG_GRASS"), GRASS_BLOCK("GRASS"), GRASS_PATH, GRAVEL, - GRAY_BANNER(8, "BANNER", "STANDING_BANNER"), + GRAY_BANNER(8, "STANDING_BANNER", "BANNER"), GRAY_BED(7, "BED_BLOCK", "BED"), GRAY_CARPET(7, "CARPET"), GRAY_CONCRETE(7, "CONCRETE"), @@ -511,7 +529,7 @@ public enum XMaterial { GRAY_TERRACOTTA(7, "HARD_CLAY", "STAINED_CLAY"), GRAY_WALL_BANNER(8, "WALL_BANNER"), GRAY_WOOL(7, "WOOL"), - GREEN_BANNER(2, "BANNER", "STANDING_BANNER"), + GREEN_BANNER(2, "STANDING_BANNER", "BANNER"), GREEN_BED(13, "BED_BLOCK", "BED"), GREEN_CARPET(13, "CARPET"), GREEN_CONCRETE(13, "CONCRETE"), @@ -550,6 +568,11 @@ public enum XMaterial { INFESTED_MOSSY_STONE_BRICKS(3, "MONSTER_EGGS"), INFESTED_STONE("MONSTER_EGGS"), INFESTED_STONE_BRICKS(2, "MONSTER_EGGS", "SMOOTH_BRICK"), + /** + * We will only add "INK_SAC" for {@link #BLACK_DYE} since it's + * the only material (linked with this material) that is added + * after 1.13, which means it can use both INK_SACK and INK_SAC. + */ INK_SAC("INK_SACK"), IRON_AXE, IRON_BARS("IRON_FENCE"), @@ -582,11 +605,11 @@ public enum XMaterial { JUNGLE_PLANKS(3, "WOOD"), JUNGLE_PRESSURE_PLATE("WOOD_PLATE"), JUNGLE_SAPLING(3, "SAPLING"), - JUNGLE_SIGN("SIGN"), + JUNGLE_SIGN("SIGN_POST", "SIGN"), JUNGLE_SLAB(3, "WOOD_STEP", "WOODEN_SLAB", "WOOD_DOUBLE_STEP"), JUNGLE_STAIRS("JUNGLE_WOOD_STAIRS"), JUNGLE_TRAPDOOR("TRAP_DOOR"), - JUNGLE_WALL_SIGN("SIGN_POST", "WALL_SIGN"), + JUNGLE_WALL_SIGN("WALL_SIGN"), JUNGLE_WOOD(3, "LOG"), KELP("1.13"), KELP_PLANT("1.13"), @@ -608,7 +631,7 @@ public enum XMaterial { LEATHER_LEGGINGS, LECTERN("1.14", "BOOKSHELF", ""), LEVER, - LIGHT_BLUE_BANNER(12, "BANNER", "STANDING_BANNER"), + LIGHT_BLUE_BANNER(12, "STANDING_BANNER", "BANNER"), LIGHT_BLUE_BED(3, "BED_BLOCK", "BED"), LIGHT_BLUE_CARPET(3, "CARPET"), LIGHT_BLUE_CONCRETE(3, "CONCRETE"), @@ -619,9 +642,9 @@ public enum XMaterial { LIGHT_BLUE_STAINED_GLASS(3, "STAINED_GLASS"), LIGHT_BLUE_STAINED_GLASS_PANE(3, "THIN_GLASS", "STAINED_GLASS_PANE"), LIGHT_BLUE_TERRACOTTA(3, "STAINED_CLAY"), - LIGHT_BLUE_WALL_BANNER(12, "WALL_BANNER", "BANNER", "STANDING_BANNER"), + LIGHT_BLUE_WALL_BANNER(12, "WALL_BANNER", "STANDING_BANNER", "BANNER"), LIGHT_BLUE_WOOL(3, "WOOL"), - LIGHT_GRAY_BANNER(7, "BANNER", "STANDING_BANNER"), + LIGHT_GRAY_BANNER(7, "STANDING_BANNER", "BANNER"), LIGHT_GRAY_BED(8, "BED_BLOCK", "BED"), LIGHT_GRAY_CARPET(8, "CARPET"), LIGHT_GRAY_CONCRETE(8, "CONCRETE"), @@ -642,7 +665,7 @@ public enum XMaterial { LILAC(1, "DOUBLE_PLANT"), LILY_OF_THE_VALLEY(15, "1.14", "WHITE_DYE", ""), LILY_PAD("WATER_LILY"), - LIME_BANNER(10, "BANNER", "STANDING_BANNER"), + LIME_BANNER(10, "STANDING_BANNER", "BANNER"), LIME_BED(5, "BED_BLOCK", "BED"), LIME_CARPET(5, "CARPET"), LIME_CONCRETE(5, "CONCRETE"), @@ -659,7 +682,7 @@ public enum XMaterial { LLAMA_SPAWN_EGG(103, "MONSTER_EGG"), LODESTONE("1.16"), LOOM("1.14"), - MAGENTA_BANNER(13, "BANNER", "STANDING_BANNER"), + MAGENTA_BANNER(13, "STANDING_BANNER", "BANNER"), MAGENTA_BED(2, "BED_BLOCK", "BED"), MAGENTA_CARPET(2, "CARPET"), MAGENTA_CONCRETE(2, "CONCRETE"), @@ -711,6 +734,7 @@ public enum XMaterial { MUSIC_DISC_FAR("RECORD_6"), MUSIC_DISC_MALL("RECORD_7"), MUSIC_DISC_MELLOHI("RECORD_8"), + MUSIC_DISC_PIGSTEP("1.16"), MUSIC_DISC_STAL("RECORD_9"), MUSIC_DISC_STRAD("RECORD_10"), MUSIC_DISC_WAIT("RECORD_11"), @@ -761,16 +785,16 @@ public enum XMaterial { OAK_PLANKS("WOOD"), OAK_PRESSURE_PLATE("WOOD_PLATE"), OAK_SAPLING("SAPLING"), - OAK_SIGN("SIGN"), + OAK_SIGN("SIGN_POST", "SIGN"), OAK_SLAB("WOOD_STEP", "WOODEN_SLAB", "WOOD_DOUBLE_STEP"), OAK_STAIRS("WOOD_STAIRS"), OAK_TRAPDOOR("TRAP_DOOR"), - OAK_WALL_SIGN("SIGN_POST", "WALL_SIGN"), + OAK_WALL_SIGN("WALL_SIGN"), OAK_WOOD("LOG"), OBSERVER, OBSIDIAN, OCELOT_SPAWN_EGG(98, "MONSTER_EGG"), - ORANGE_BANNER(14, "BANNER", "STANDING_BANNER"), + ORANGE_BANNER(14, "STANDING_BANNER", "BANNER"), ORANGE_BED(1, "BED_BLOCK", "BED"), ORANGE_CARPET(1, "CARPET"), ORANGE_CONCRETE(1, "CONCRETE"), @@ -794,9 +818,11 @@ public enum XMaterial { PETRIFIED_OAK_SLAB("WOOD_STEP"), PHANTOM_MEMBRANE("1.13"), PHANTOM_SPAWN_EGG("1.13", "MONSTER_EGG", ""), + PIGLIN_BANNER_PATTERN("1.16"), + PIGLIN_SPAWN_EGG(57, "MONSTER_EGG"), PIG_SPAWN_EGG(90, "MONSTER_EGG"), PILLAGER_SPAWN_EGG("1.14"), - PINK_BANNER(9, "BANNER", "STANDING_BANNER"), + PINK_BANNER(9, "STANDING_BANNER", "BANNER"), PINK_BED(6, "BED_BLOCK", "BED"), PINK_CARPET(6, "CARPET"), PINK_CONCRETE(6, "CONCRETE"), @@ -852,6 +878,8 @@ public enum XMaterial { POTTED_BROWN_MUSHROOM("FLOWER_POT"), POTTED_CACTUS("FLOWER_POT"), POTTED_CORNFLOWER, + POTTED_CRIMSON_FUNGUS("1.16"), + POTTED_CRIMSON_ROOTS("1.16"), POTTED_DANDELION("YELLOW_FLOWER", "FLOWER_POT"), POTTED_DARK_OAK_SAPLING(5, "SAPLING", "FLOWER_POT"), POTTED_DEAD_BUSH("FLOWER_POT"), @@ -866,6 +894,8 @@ public enum XMaterial { POTTED_RED_MUSHROOM("FLOWER_POT"), POTTED_RED_TULIP(4, "RED_ROSE", "FLOWER_POT"), POTTED_SPRUCE_SAPLING(1, "SAPLING", "FLOWER_POT"), + POTTED_WARPED_FUNGUS("1.16"), + POTTED_WARPED_ROOTS("1.16"), POTTED_WHITE_TULIP(6, "RED_ROSE", "FLOWER_POT"), POTTED_WITHER_ROSE, POWERED_RAIL, @@ -885,7 +915,7 @@ public enum XMaterial { PUMPKIN_PIE, PUMPKIN_SEEDS, PUMPKIN_STEM, - PURPLE_BANNER(5, "BANNER", "STANDING_BANNER"), + PURPLE_BANNER(5, "STANDING_BANNER", "BANNER"), PURPLE_BED(10, "BED_BLOCK", "BED"), PURPLE_CARPET(10, "CARPET"), PURPLE_CONCRETE(10, "CONCRETE"), @@ -904,6 +934,7 @@ public enum XMaterial { PURPUR_STAIRS, QUARTZ, QUARTZ_BLOCK, + QUARTZ_BRICKS("1.16"), QUARTZ_PILLAR(2, "QUARTZ_BLOCK"), QUARTZ_SLAB(7, "STEP"), QUARTZ_STAIRS, @@ -932,7 +963,7 @@ public enum XMaterial { REDSTONE_TORCH("REDSTONE_TORCH_OFF", "REDSTONE_TORCH_ON"), REDSTONE_WALL_TORCH, REDSTONE_WIRE, - RED_BANNER(1, "BANNER", "STANDING_BANNER"), + RED_BANNER(1, "STANDING_BANNER", "BANNER"), /** * Data value 14 or 0 */ @@ -940,10 +971,7 @@ public enum XMaterial { RED_CARPET(14, "CARPET"), RED_CONCRETE(14, "CONCRETE"), RED_CONCRETE_POWDER(14, "CONCRETE_POWDER"), - /** - * Data value 0 or 1? - */ - RED_DYE(0, "INK_SACK", "ROSE_RED"), + RED_DYE(1, "INK_SACK", "ROSE_RED"), RED_GLAZED_TERRACOTTA(14, "1.12", "HARD_CLAY", "STAINED_CLAY", "RED_TERRACOTTA"), RED_MUSHROOM, RED_MUSHROOM_BLOCK("RED_MUSHROOM", "HUGE_MUSHROOM_2"), @@ -963,7 +991,7 @@ public enum XMaterial { RED_TULIP(4, "RED_ROSE"), RED_WALL_BANNER(1, "WALL_BANNER"), RED_WOOL(14, "WOOL"), - REPEATER("DIODE", "DIODE_BLOCK_ON", "DIODE_BLOCK_OFF"), + REPEATER("DIODE_BLOCK_ON", "DIODE_BLOCK_OFF", "DIODE"), REPEATING_COMMAND_BLOCK("COMMAND", "COMMAND_REPEATING"), RESPAWN_ANCHOR("1.16"), ROSE_BUSH(4, "DOUBLE_PLANT"), @@ -1032,16 +1060,16 @@ public enum XMaterial { SPRUCE_DOOR("SPRUCE_DOOR_ITEM", "SPRUCE_DOOR"), SPRUCE_FENCE, SPRUCE_FENCE_GATE, - SPRUCE_LEAVES(1, "LEAVES"), + SPRUCE_LEAVES(1, "LEAVES", "LEAVES_2"), SPRUCE_LOG(1, "LOG"), SPRUCE_PLANKS(1, "WOOD"), SPRUCE_PRESSURE_PLATE("WOOD_PLATE"), SPRUCE_SAPLING(1, "SAPLING"), - SPRUCE_SIGN("SIGN"), + SPRUCE_SIGN("SIGN_POST", "SIGN"), SPRUCE_SLAB(1, "WOOD_STEP", "WOODEN_SLAB", "WOOD_DOUBLE_STEP"), SPRUCE_STAIRS("SPRUCE_WOOD_STAIRS"), SPRUCE_TRAPDOOR("TRAP_DOOR"), - SPRUCE_WALL_SIGN("SIGN_POST", "WALL_SIGN"), + SPRUCE_WALL_SIGN("WALL_SIGN"), SPRUCE_WOOD(1, "LOG"), SQUID_SPAWN_EGG(94, "MONSTER_EGG"), STICK, @@ -1062,6 +1090,7 @@ public enum XMaterial { STONE_STAIRS, STONE_SWORD, STRAY_SPAWN_EGG(6, "MONSTER_EGG"), + STRIDER_SPAWN_EGG("1.16"), STRING, STRIPPED_ACACIA_LOG("LOG_2"), STRIPPED_ACACIA_WOOD("LOG_2"), @@ -1144,12 +1173,12 @@ public enum XMaterial { WARPED_PLANKS("1.16"), WARPED_PRESSURE_PLATE("1.16"), WARPED_ROOTS("1.16"), - WARPED_SIGN("1.16"), + WARPED_SIGN("1.16", "SIGN_POST"), WARPED_SLAB("1.16"), WARPED_STAIRS("1.16"), WARPED_STEM("1.16"), WARPED_TRAPDOOR("1.16"), - WARPED_WALL_SIGN("1.16"), + WARPED_WALL_SIGN("1.16", "WALL_SIGN"), WARPED_WART_BLOCK("1.16"), /** * This is used for blocks only. @@ -1169,7 +1198,7 @@ public enum XMaterial { */ WHEAT("CROPS"), WHEAT_SEEDS("SEEDS"), - WHITE_BANNER(15, "BANNER", "STANDING_BANNER"), + WHITE_BANNER(15, "STANDING_BANNER", "BANNER"), WHITE_BED("BED_BLOCK", "BED"), WHITE_CARPET("CARPET"), WHITE_CONCRETE("CONCRETE"), @@ -1196,7 +1225,7 @@ public enum XMaterial { WOODEN_SWORD("WOOD_SWORD"), WRITABLE_BOOK("BOOK_AND_QUILL"), WRITTEN_BOOK, - YELLOW_BANNER(11, "BANNER", "STANDING_BANNER"), + YELLOW_BANNER(11, "STANDING_BANNER", "BANNER"), YELLOW_BED(4, "BED_BLOCK", "BED"), YELLOW_CARPET(4, "CARPET"), YELLOW_CONCRETE(4, "CONCRETE"), @@ -1209,22 +1238,31 @@ public enum XMaterial { YELLOW_TERRACOTTA(4, "HARD_CLAY", "STAINED_CLAY"), YELLOW_WALL_BANNER(11, "WALL_BANNER"), YELLOW_WOOL(4, "WOOL"), + ZOGLIN_SPAWN_EGG("1.16"), ZOMBIE_HEAD(2, "SKULL", "SKULL_ITEM"), ZOMBIE_HORSE_SPAWN_EGG(29, "MONSTER_EGG"), ZOMBIE_SPAWN_EGG(54, "MONSTER_EGG"), ZOMBIE_VILLAGER_SPAWN_EGG(27, "MONSTER_EGG"), ZOMBIE_WALL_HEAD(2, "SKULL", "SKULL_ITEM"), - PIGLIN_SPAWN_EGG(57, "MONSTER_EGG"), ZOMBIFIED_PIGLIN_SPAWN_EGG(57, "MONSTER_EGG", "ZOMBIE_PIGMAN_SPAWN_EGG"); /** - * An immutable cached set of {@link XMaterial#values()} to avoid allocating memory for + * Cached set of {@link XMaterial#values()} to avoid allocating memory for * calling the method every time. + * This list is unmodifiable. * * @since 2.0.0 */ - public static final EnumSet VALUES = EnumSet.allOf(XMaterial.class); + public static final List VALUES = Collections.unmodifiableList(Arrays.asList(values())); + + /** + * We don't want to use {@link Enums#getIfPresent(Class, String)} to avoid a few checks. + * + * @since 5.1.0 + */ + private static final Map NAMES = new HashMap<>(); + /** * A set of material names that can be damaged. *

@@ -1233,13 +1271,13 @@ public enum XMaterial { * * @since 1.0.0 */ - private static final ImmutableSet DAMAGEABLE = ImmutableSet.of( + private static final Set DAMAGEABLE = new HashSet<>(Arrays.asList( "HELMET", "CHESTPLATE", "LEGGINGS", "BOOTS", "SWORD", "AXE", "PICKAXE", "SHOVEL", "HOE", "ELYTRA", "TRIDENT", "HORSE_ARMOR", "BARDING", "SHEARS", "FLINT_AND_STEEL", "BOW", "FISHING_ROD", "CARROT_ON_A_STICK", "CARROT_STICK", "SPADE", "SHIELD" - ); + )); /** * XMaterial Paradox (Duplication Check) *

@@ -1250,29 +1288,25 @@ public enum XMaterial { * * @since 3.0.0 */ - private static final ImmutableMap duplicated = Maps.immutableEnumMap(ImmutableMap.builder() - .put(MELON, MELON_SLICE) - .put(CARROT, CARROTS) - .put(POTATO, POTATOES) - .put(BEETROOT, BEETROOTS) - .put(BROWN_MUSHROOM, BROWN_MUSHROOM_BLOCK) - .put(BRICK, BRICKS) - .put(NETHER_BRICK, NETHER_BRICKS) - - // Illegal Elements - // Since both 1.12 and 1.13 have _DOOR XMaterial will use it - // for 1.12 to parse the material, but it needs _DOOR_ITEM. - // We'll trick XMaterial into thinking this needs to be parsed - // using the old methods. - // These materials have their enum name added to the legacy list as well. - .put(DARK_OAK_DOOR, DARK_OAK_DOOR) - .put(ACACIA_DOOR, ACACIA_DOOR) - .put(BIRCH_DOOR, BIRCH_DOOR) - .put(JUNGLE_DOOR, JUNGLE_DOOR) - .put(SPRUCE_DOOR, SPRUCE_DOOR) - - .build() - ); + private static final EnumMap DUPLICATED = new EnumMap(XMaterial.class); + /** + * Guava (Google Core Libraries for Java)'s cache for performance and timed caches. + * For strings that match a certain XMaterial. Mostly cached for configs. + * + * @since 1.0.0 + */ + private static final Cache NAME_CACHE = CacheBuilder.newBuilder() + .expireAfterAccess(15, TimeUnit.MINUTES) + .build(); + /** + * Guava (Google Core Libraries for Java)'s cache for performance and timed caches. + * For XMaterials that are already parsed once. + * + * @since 3.0.0 + */ + private static final Cache> PARSED_CACHE = CacheBuilder.newBuilder() + .expireAfterAccess(10, TimeUnit.MINUTES) + .build(); /* * A set of all the legacy names without duplicates. *

@@ -1287,36 +1321,24 @@ public enum XMaterial { .filter(m -> m.charAt(1) == '.') .collect(Collectors.collectingAndThen(Collectors.toSet(), ImmutableSet::copyOf)); */ - /** - * Guava (Google Core Libraries for Java)'s cache for performance and timed caches. - * For strings that match a certain XMaterial. Mostly cached for configs. + * This is used for {@link #isOneOf(Collection)} * - * @since 1.0.0 + * @since 3.4.0 */ - private static final Cache NAME_CACHE = CacheBuilder.newBuilder() - .softValues() - .expireAfterAccess(15, TimeUnit.MINUTES) - .build(); - /** - * Guava (Google Core Libraries for Java)'s cache for performance and timed caches. - * For XMaterials that are already parsed once. - * - * @since 3.0.0 - */ - private static final Cache> PARSED_CACHE = CacheBuilder.newBuilder() - .softValues() - .expireAfterAccess(10, TimeUnit.MINUTES) - .concurrencyLevel(Runtime.getRuntime().availableProcessors()) - .build(); - - /** - * Pre-compiled RegEx pattern. - * Include both replacements to avoid recreating string multiple times with multiple RegEx checks. - * - * @since 3.0.0 - */ - private static final Pattern FORMAT_PATTERN = Pattern.compile("\\W+"); + private static final LoadingCache CACHED_REGEX = CacheBuilder.newBuilder() + .expireAfterAccess(1, TimeUnit.HOURS) + .build(new CacheLoader() { + @Override + public Pattern load(@Nonnull String str) { + try { + return Pattern.compile(str); + } catch (PatternSyntaxException ex) { + ex.printStackTrace(); + return null; + } + } + }); /** * The current version of the server in the a form of a major version. * @@ -1330,6 +1352,35 @@ public enum XMaterial { * @since 3.0.0 */ private static final boolean ISFLAT = supports(13); + + static { + DUPLICATED.put(MELON, MELON_SLICE); + DUPLICATED.put(CARROT, CARROTS); + DUPLICATED.put(POTATO, POTATOES); + DUPLICATED.put(BEETROOT, BEETROOTS); + DUPLICATED.put(BROWN_MUSHROOM, BROWN_MUSHROOM_BLOCK); + DUPLICATED.put(BRICK, BRICKS); + DUPLICATED.put(NETHER_BRICK, NETHER_BRICKS); + + // Illegal Elements + // Since both 1.12 and 1.13 have _DOOR XMaterial will use it + // for 1.12 to parse the material, but it needs _DOOR_ITEM. + // We'll trick XMaterial into thinking this needs to be parsed + // using the old methods. + // Some of these materials have their enum name added to the legacy list as well. + DUPLICATED.put(DARK_OAK_DOOR, DARK_OAK_DOOR); + DUPLICATED.put(ACACIA_DOOR, ACACIA_DOOR); + DUPLICATED.put(BIRCH_DOOR, BIRCH_DOOR); + DUPLICATED.put(JUNGLE_DOOR, JUNGLE_DOOR); + DUPLICATED.put(SPRUCE_DOOR, SPRUCE_DOOR); + DUPLICATED.put(CAULDRON, CAULDRON); + DUPLICATED.put(BREWING_STAND, BREWING_STAND); + } + + static { + for (XMaterial material : VALUES) NAMES.put(material.name(), material); + } + /** * The data value of this material https://minecraft.gamepedia.com/Java_Edition_data_values/Pre-flattening * @@ -1382,7 +1433,7 @@ public enum XMaterial { * An invocation of this method yields exactly the same result as the expression: *

*

- * {@link #getVersion()} == 1.8 + * !{@link #supports(int)} 9 *
* * @since 2.0.0 @@ -1391,6 +1442,18 @@ public enum XMaterial { return !supports(9); } + /** + * Gets the {@link XMaterial} with this name without throwing an exception. + * + * @param name the name of the material. + * @return an optional that can be empty. + * @since 5.1.0 + */ + @Nonnull + private static Optional getIfPresent(@Nonnull String name) { + return Optional.ofNullable(NAMES.get(name)); + } + /** * The current version of the server. * @@ -1442,8 +1505,9 @@ public enum XMaterial { Validate.notEmpty(name, "Cannot check for null or empty material name"); name = format(name); - for (XMaterial materials : VALUES) + for (XMaterial materials : VALUES) { if (materials.name().equals(name)) return true; + } return false; } @@ -1463,13 +1527,13 @@ public enum XMaterial { /** * Parses material name and data value from the specified string. - * The seperators are: , or : + * The separator for the material name and its data value is {@code :} * Spaces are allowed. Mostly used when getting materials from config for old school minecrafters. *

* Examples *

      *     {@code INK_SACK:1 -> RED_DYE}
-     *     {@code WOOL, 14  -> RED_WOOL}
+     *     {@code WOOL: 14  -> RED_WOOL}
      * 
* * @param name the material string that consists of the material name, data and separator character. @@ -1477,14 +1541,17 @@ public enum XMaterial { * @see #matchXMaterial(String) * @since 3.0.0 */ + @Nonnull private static Optional matchXMaterialWithData(String name) { - for (char separator : new char[]{',', ':'}) { - int index = name.indexOf(separator); - if (index == -1) continue; - + int index = name.indexOf(':'); + if (index != -1) { String mat = format(name.substring(0, index)); - byte data = Byte.parseByte(StringUtils.deleteWhitespace(name.substring(index + 1))); - return matchDefinedXMaterial(mat, data); + + try { + byte data = (byte) Integer.parseInt(StringUtils.deleteWhitespace(name.substring(index + 1))); + return matchDefinedXMaterial(mat, data); + } catch (NumberFormatException ignored) { + } } return Optional.empty(); @@ -1502,7 +1569,7 @@ public enum XMaterial { public static XMaterial matchXMaterial(@Nonnull Material material) { Objects.requireNonNull(material, "Cannot match null material"); return matchDefinedXMaterial(material.name(), (byte) -1) - .orElseThrow(() -> new IllegalArgumentException("Unsupported Material With No Bytes: " + material.name())); + .orElseThrow(() -> new IllegalArgumentException("Unsupported material with no data value: " + material.name())); } /** @@ -1522,7 +1589,7 @@ public enum XMaterial { byte data = (byte) (ISFLAT || isDamageable(material) ? 0 : item.getDurability()); return matchDefinedXMaterial(material, data) - .orElseThrow(() -> new IllegalArgumentException("Unsupported Material: " + material + " (" + data + ')')); + .orElseThrow(() -> new IllegalArgumentException("Unsupported material: " + material + " (" + data + ')')); } /** @@ -1544,11 +1611,12 @@ public enum XMaterial { // Do basic number and boolean checks before accessing more complex enum stuff. // Maybe we can simplify (ISFLAT || !duplicated) with the (!ISFLAT && duplicated) under it to save a few nanoseconds? // if (!Boolean.valueOf(Boolean.getBoolean(Boolean.TRUE.toString())).equals(Boolean.FALSE.booleanValue())) return null; - if (data <= 0 && !duplicated) { + Optional xMaterial = null; + if (data <= 0 && (ISFLAT || !duplicated)) { // Apparently the transform method is more efficient than toJavaUtil() // toJavaUtil isn't even supported in older versions. - Optional xMat = Enums.getIfPresent(XMaterial.class, name).transform(Optional::of).or(Optional.empty()); - if (xMat.isPresent()) return xMat; + xMaterial = getIfPresent(name); + if (xMaterial.isPresent()) return xMaterial; } // XMaterial Paradox (Duplication Check) @@ -1566,7 +1634,7 @@ public enum XMaterial { // A solution for XMaterial Paradox. // Manually parses the duplicated materials to find the exact material based on the server version. // If ends with "S" -> Plural Form Material - return Enums.getIfPresent(XMaterial.class, name).transform(Optional::of).or(Optional.empty()); + return xMaterial == null ? getIfPresent(name) : xMaterial; } return Optional.ofNullable(xMat); } @@ -1585,7 +1653,7 @@ public enum XMaterial { */ private static boolean isDuplicated(@Nonnull String name) { // Don't use matchXMaterial() since this method is being called from matchXMaterial() itself and will cause a StackOverflowError. - for (Map.Entry duplicated : duplicated.entrySet()) { + for (Map.Entry duplicated : DUPLICATED.entrySet()) { XMaterial material = duplicated.getKey(); if (material.name().equals(name) || material.anyMatchLegacy(name)) return true; } @@ -1614,16 +1682,41 @@ public enum XMaterial { /** * Attempts to build the string like an enum name. - * Removes all the spaces, numbers and extra non-English characters. Also removes some config/in-game based strings. + * Removes all the spaces, and extra non-English characters. Also removes some config/in-game based strings. + * While this method is hard to maintain, it's extremely efficient. It's approximately more than x5 times faster than + * the normal RegEx + String Methods approach for both formatted and unformatted material names. * * @param name the material name to modify. * @return a Material enum name. * @since 2.0.0 */ @Nonnull - private static String format(@Nonnull String name) { - return FORMAT_PATTERN.matcher( - name.trim().replace('-', '_').replace(' ', '_')).replaceAll("").toUpperCase(Locale.ENGLISH); + protected static String format(@Nonnull String name) { + int len = name.length(); + char[] chs = new char[len]; + int count = 0; + boolean appendUnderline = false; + + for (int i = 0; i < len; ++i) { + char ch = name.charAt(i); + + if (!appendUnderline && count != 0 && (ch == '-' || ch == ' ' || ch == '_') && chs[count] != '_') appendUnderline = true; + else { + boolean number = false; + // Old materials have numbers in them. + if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (number = (ch >= '0' && ch <= '9'))) { + if (appendUnderline) { + chs[count++] = '_'; + appendUnderline = false; + } + + if (number) chs[count++] = ch; + else chs[count++] = (char) (ch & 0x5f); + } + } + } + + return new String(chs, 0, count); } /** @@ -1709,8 +1802,9 @@ public enum XMaterial { */ public static boolean isDamageable(@Nonnull String name) { Objects.requireNonNull(name, "Material name cannot be null"); - for (String damageable : DAMAGEABLE) + for (String damageable : DAMAGEABLE) { if (name.contains(damageable)) return true; + } return false; } @@ -1750,32 +1844,31 @@ public enum XMaterial { *

* Want to learn RegEx? You can mess around in RegExr website. * - * @param material the base material to match other materials with. * @param materials the material names to check base material on. * @return true if one of the given material names is similar to the base material. * @since 3.1.1 */ - public static boolean isOneOf(@Nonnull Material material, @Nullable List materials) { + public boolean isOneOf(@Nullable Collection materials) { if (materials == null || materials.isEmpty()) return false; - Objects.requireNonNull(material, "Cannot match materials with a null material"); - String name = material.name(); + String name = this.name(); for (String comp : materials) { - comp = comp.toUpperCase(); - if (comp.startsWith("CONTAINS:")) { - comp = format(comp.substring(9)); + String checker = comp.toUpperCase(Locale.ENGLISH); + if (checker.startsWith("CONTAINS:")) { + comp = format(checker.substring(9)); if (name.contains(comp)) return true; continue; } - if (comp.startsWith("REGEX:")) { + if (checker.startsWith("REGEX:")) { comp = comp.substring(6); - if (name.matches(comp)) return true; + Pattern pattern = CACHED_REGEX.getUnchecked(comp); + if (pattern != null && pattern.matcher(name).matches()) return true; continue; } // Direct Object Equals - Optional mat = matchXMaterial(comp); - if (mat.isPresent() && mat.get().parseMaterial() == material) return true; + Optional xMat = matchXMaterial(comp); + if (xMat.isPresent() && xMat.get() == this) return true; } return false; } @@ -1809,27 +1902,14 @@ public enum XMaterial { @SuppressWarnings("deprecation") public ItemStack setType(@Nonnull ItemStack item) { Objects.requireNonNull(item, "Cannot set material for null ItemStack"); + Material material = this.parseMaterial(); + Objects.requireNonNull(material, "Unsupported material: " + this.name()); - item.setType(this.parseMaterial()); + item.setType(material); if (!ISFLAT && !this.isDamageable()) item.setDurability(this.data); return item; } - /** - * Checks if the list of given material names matches the given base material. - * Mostly used for configs. - * - * @param materials the material names to check base material on. - * @return true if one of the given material names is similar to the base material. - * @see #isOneOf(Material, List) - * @since 3.0.0 - */ - public boolean isOneOf(@Nullable List materials) { - Material material = this.parseMaterial(); - if (material == null) return false; - return isOneOf(material, materials); - } - /** * Checks if the given string matches any of this material's legacy material names. * All the values passed to this method will not be null or empty and are formatted correctly. @@ -1868,7 +1948,7 @@ public enum XMaterial { */ @SuppressWarnings("deprecation") public int getId() { - if (this.data != 0 || (this.legacy.length != 0 && Integer.parseInt(this.legacy[0].substring(2)) >= 13)) return -1; + if (this.data != 0 || (this.legacy.length != 0 && this.legacy[0].charAt(1) == '.' && Integer.parseInt(this.legacy[0].substring(2)) >= 13)) return -1; Material material = this.parseMaterial(); return material == null ? -1 : material.getId(); } @@ -1881,7 +1961,7 @@ public enum XMaterial { * @since 2.0.0 */ public boolean isDuplicated() { - return duplicated.containsKey(this); + return DUPLICATED.containsKey(this); } /** @@ -1984,7 +2064,8 @@ public enum XMaterial { if (mat == null) mat = requestOldMaterial(suggest); } - if (mat != null) PARSED_CACHE.put(this, Optional.ofNullable(mat)); + Optional opt = Optional.ofNullable(mat); + PARSED_CACHE.put(this, opt); return mat; }