matchDefinedXMaterial(String name, byte data) {
// if (!Boolean.valueOf(Boolean.getBoolean(Boolean.TRUE.toString())).equals(Boolean.FALSE.booleanValue())) return null;
Boolean duplicated = null;
boolean isAMap = name.equalsIgnoreCase("MAP");
@@ -1745,7 +1717,8 @@ public enum XMaterial {
return (data >= 0 && isAMap) ? Optional.of(FILLED_MAP) : Optional.empty();
}
- if (!Data.ISFLAT && oldXMaterial.isPlural() && (duplicated == null ? isDuplicated(name) : duplicated)) return getIfPresent(name);
+ if (!Data.ISFLAT && oldXMaterial.isPlural() && (duplicated == null ? isDuplicated(name) : duplicated))
+ return getIfPresent(name);
return Optional.of(oldXMaterial);
}
@@ -1757,11 +1730,10 @@ public enum XMaterial {
* {@code MELON, CARROT, POTATO, BEETROOT -> true}
*
* @param name the name of the material to check.
- *
* @return true if there's a duplicated material for this material, otherwise false.
* @since 2.0.0
*/
- private static boolean isDuplicated( String name) {
+ private static boolean isDuplicated(String name) {
// Don't use matchXMaterial() since this method is being called from matchXMaterial() itself and will cause a StackOverflowError.
return DUPLICATED.contains(name);
}
@@ -1772,7 +1744,6 @@ public enum XMaterial {
*
* @param id the ID (Magic value) of the material.
* @param data the data value of the material.
- *
* @return a parsed XMaterial with the same ID and data value.
* @see #matchXMaterial(ItemStack)
* @since 2.0.0
@@ -1780,7 +1751,7 @@ public enum XMaterial {
* which takes a really long time. Plugins should no longer support IDs. If you want, you can make a {@link Map} cache yourself.
* This method obviously doesn't work for 1.13+ and will not be supported. This is only here for debugging purposes.
*/
-
+
@Deprecated
public static Optional matchXMaterial(int id, byte data) {
if (id < 0 || id > MAX_ID || data < 0) return Optional.empty();
@@ -1797,12 +1768,11 @@ public enum XMaterial {
* the normal RegEx + String Methods approach for both formatted and unformatted material names.
*
* @param name the material name to modify.
- *
* @return an enum name.
* @since 2.0.0
*/
-
- protected static String format( String name) {
+
+ protected static String format(String name) {
int len = name.length();
char[] chs = new char[len];
int count = 0;
@@ -1835,7 +1805,6 @@ public enum XMaterial {
* Checks if the specified version is the same version or higher than the current server version.
*
* @param version the major version to be checked. "1." is ignored. E.g. 1.12 = 12 | 1.9 = 9
- *
* @return true of the version is equal or higher than the current version.
* @since 2.0.0
*/
@@ -1843,39 +1812,6 @@ public enum XMaterial {
return Data.VERSION >= version;
}
- /**
- * Gets the exact major version (..., 1.9, 1.10, ..., 1.14)
- * In most cases, you shouldn't be using this method.
- *
- * @param version Supports {@link Bukkit#getVersion()}, {@link Bukkit#getBukkitVersion()} and normal formats such as "1.14"
- *
- * @return the exact major version.
- * @see #supports(int)
- * @see #getVersion()
- * @see #getMaterialVersion()
- * @since 2.0.0
- */
-
- public static String getMajorVersion( String version) {
- Validate.notEmpty(version, "Cannot get major Minecraft version from null or empty string");
-
- // getVersion()
- int index = version.lastIndexOf("MC:");
- if (index != -1) {
- version = version.substring(index + 4, version.length() - 1);
- } else if (version.endsWith("SNAPSHOT")) {
- // getBukkitVersion()
- index = version.indexOf('-');
- version = version.substring(0, index);
- }
-
- // 1.13.2, 1.14.4, etc...
- int lastDot = version.lastIndexOf('.');
- if (version.indexOf('.') != lastDot) version = version.substring(0, lastDot);
-
- return version;
- }
-
public String[] getLegacy() {
return this.legacy;
}
@@ -1935,11 +1871,10 @@ public enum XMaterial {
* Want to learn RegEx? You can mess around in RegExr website.
*
* @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 boolean isOneOf( Collection materials) {
+ public boolean isOneOf(Collection materials) {
if (materials == null || materials.isEmpty()) return false;
String name = this.name();
@@ -1952,7 +1887,15 @@ public enum XMaterial {
}
if (checker.startsWith("REGEX:")) {
comp = comp.substring(6);
- Pattern pattern = CACHED_REGEX.getUnchecked(comp);
+ Pattern pattern = CACHED_REGEX.getIfPresent(comp);
+ if (pattern == null) {
+ try {
+ pattern = Pattern.compile(comp);
+ CACHED_REGEX.put(comp, pattern);
+ } catch (PatternSyntaxException ex) {
+ ex.printStackTrace();
+ }
+ }
if (pattern != null && pattern.matcher(name).matches()) return true;
continue;
}
@@ -1971,13 +1914,12 @@ public enum XMaterial {
* Use {@link #parseItem()} instead when creating new ItemStacks.
*
* @param item the item to change its type.
- *
* @see #parseItem()
* @since 3.0.0
*/
-
+
@SuppressWarnings("deprecation")
- public ItemStack setType( ItemStack item) {
+ public ItemStack setType(ItemStack item) {
Objects.requireNonNull(item, "Cannot set material for null ItemStack");
Material material = this.parseMaterial();
Objects.requireNonNull(material, () -> "Unsupported material: " + this.name());
@@ -1992,11 +1934,10 @@ public enum XMaterial {
* All the values passed to this method will not be null or empty and are formatted correctly.
*
* @param name the material name to check.
- *
* @return true if it's one of the legacy names, otherwise false.
* @since 2.0.0
*/
- private boolean anyMatchLegacy( String name) {
+ private boolean anyMatchLegacy(String name) {
for (int i = this.legacy.length - 1; i >= 0; i--) {
if (name.equals(this.legacy[i])) return true;
}
@@ -2018,7 +1959,7 @@ public enum XMaterial {
* @since 3.0.0
*/
@Override
-
+
public String toString() {
return WordUtils.capitalize(this.name().replace('_', ' ').toLowerCase(Locale.ENGLISH));
}
@@ -2026,6 +1967,8 @@ public enum XMaterial {
/**
* Gets the ID (Magic value) of the material.
* https://www.minecraftinfo.com/idlist.htm
+ *
+ * Spigot added material ID support back in 1.16+
*
* @return the ID of the material or -1 if it's not a legacy material or the server doesn't support the material.
* @see #matchXMaterial(int, byte)
@@ -2033,11 +1976,15 @@ public enum XMaterial {
*/
@SuppressWarnings("deprecation")
public int getId() {
- if (this.data != 0 || this.version >= 13) return -1;
+ // https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/diff/src/main/java/org/bukkit/Material.java?until=1cb03826ebde4ef887519ce37b0a2a341494a183
+ // Should start working again in 1.16+
Material material = this.parseMaterial();
if (material == null) return -1;
- if (Data.ISFLAT && !material.isLegacy()) return -1;
- return material.getId();
+ try {
+ return material.getId();
+ } catch (IllegalArgumentException ignored) {
+ return -1;
+ }
}
/**
@@ -2062,7 +2009,7 @@ public enum XMaterial {
* @see #setType(ItemStack)
* @since 2.0.0
*/
-
+
@SuppressWarnings("deprecation")
public ItemStack parseItem() {
Material material = this.parseMaterial();
@@ -2076,7 +2023,7 @@ public enum XMaterial {
* @return the material related to this XMaterial based on the server version.
* @since 1.0.0
*/
-
+
public Material parseMaterial() {
return this.material;
}
@@ -2085,15 +2032,14 @@ public enum XMaterial {
* Checks if an item has the same material (and data value on older versions).
*
* @param item item to check.
- *
* @return true if the material is the same as the item's material (and data value if on older versions), otherwise false.
* @since 1.0.0
*/
@SuppressWarnings("deprecation")
- public boolean isSimilar( ItemStack item) {
+ public boolean isSimilar(ItemStack item) {
Objects.requireNonNull(item, "Cannot compare with null ItemStack");
if (item.getType() != this.parseMaterial()) return false;
- return Data.ISFLAT || item.getDurability() == this.data || item.getType().getMaxDurability() <= 0;
+ return Data.ISFLAT || item.getDurability() == this.data || item.getType().getMaxDurability() > 0;
}
/**
@@ -2110,17 +2056,6 @@ public enum XMaterial {
return this.material != null;
}
- /**
- * The version this material was added in. This will return the minor number in the version scheme.
- * For example, if it was added in 1.16, it'll return 16
- *
- * @return the version number of 0 if the version was not recorded.
- * @since 7.0.0
- */
- public byte getMaterialVersion() {
- return version;
- }
-
/**
* This method is needed due to Java enum initialization limitations.
* It's really inefficient yes, but it's only used for initialization.
@@ -2162,18 +2097,27 @@ public enum XMaterial {
}
/**
- * Used for datas that need to be accessed during enum initilization.
+ * Used for data that need to be accessed during enum initialization.
*
* @since 9.0.0
*/
private static final class Data {
/**
- * The current version of the server in the a form of a major version.
+ * The current version of the server in the form of a major version.
* If the static initialization for this fails, you know something's wrong with the server software.
*
* @since 1.0.0
*/
- private static final int VERSION = Integer.parseInt(getMajorVersion(Bukkit.getVersion()).substring(2));
+ private static final int VERSION;
+
+ static { // This needs to be right below VERSION because of initialization order.
+ String version = Bukkit.getVersion();
+ Matcher matcher = Pattern.compile("MC: \\d\\.(\\d+)").matcher(version);
+
+ if (matcher.find()) VERSION = Integer.parseInt(matcher.group(1));
+ else throw new IllegalArgumentException("Failed to parse server version from: " + version);
+ }
+
/**
* Cached result if the server version is after the v1.13 flattening update.
*
diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPTabComplete.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPTabComplete.java
deleted file mode 100644
index af8c217..0000000
--- a/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPTabComplete.java
+++ /dev/null
@@ -1,100 +0,0 @@
-package nl.sbdeveloper.themeparkplus.commands;
-
-import nl.iobyte.themepark.ThemePark;
-import nl.sbdeveloper.themeparkplus.api.enums.WalkingDirection;
-import org.bukkit.Bukkit;
-import org.bukkit.Location;
-import org.bukkit.block.Block;
-import org.bukkit.command.Command;
-import org.bukkit.command.CommandSender;
-import org.bukkit.command.TabCompleter;
-import org.bukkit.entity.Player;
-import org.bukkit.util.StringUtil;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.stream.Collectors;
-
-public class TPPTabComplete implements TabCompleter {
- private static final List COMMANDS = Arrays.asList("info", "help", "opengate", "closegate", "lampon", "lampoff", "lampson", "lampsoff", "redstonetimer", "givefpticket");
-
- @Override
- public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
- //Aliases
- if (label.equalsIgnoreCase("themeparkplus") || label.equalsIgnoreCase("tpp") || label.equalsIgnoreCase("themeparkp")) {
- if (args.length == 1) {
- return StringUtil.copyPartialMatches(args[0], COMMANDS, new ArrayList<>());
- } else if (args.length == 2) {
- if (args[0].equalsIgnoreCase("opengate")
- || args[0].equalsIgnoreCase("closegate")
- || args[0].equalsIgnoreCase("lampon")
- || args[0].equalsIgnoreCase("lampoff")
- || args[0].equalsIgnoreCase("redstonetimer")) {
- if (sender instanceof Player) {
- Player p = (Player) sender;
- Location targetLoc = getTarget(p).getLocation();
- return StringUtil.copyPartialMatches(args[1], Collections.singletonList("" + targetLoc.getWorld().getName()), new ArrayList<>());
- }
- } else if (args[0].equalsIgnoreCase("givefpticket")) {
- return StringUtil.copyPartialMatches(args[1], ThemePark.getInstance().getAPI().getAttractionService().getAttractions().keySet(), new ArrayList<>());
- }
- } else if (args.length == 3) {
- if (args[0].equalsIgnoreCase("opengate")
- || args[0].equalsIgnoreCase("closegate")
- || args[0].equalsIgnoreCase("lampon")
- || args[0].equalsIgnoreCase("lampoff")
- || args[0].equalsIgnoreCase("redstonetimer")) {
- if (sender instanceof Player) {
- Player p = (Player) sender;
- Location targetLoc = getTarget(p).getLocation();
- return StringUtil.copyPartialMatches(args[2], Collections.singletonList("" + targetLoc.getBlockX()), new ArrayList<>());
- }
- } else if (args[0].equalsIgnoreCase("givefpticket")) {
- return StringUtil.copyPartialMatches(args[1], Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()), new ArrayList<>());
- }
- } else if (args.length == 4) {
- if (args[0].equalsIgnoreCase("opengate")
- || args[0].equalsIgnoreCase("closegate")
- || args[0].equalsIgnoreCase("lampon")
- || args[0].equalsIgnoreCase("lampoff")
- || args[0].equalsIgnoreCase("redstonetimer")) {
- if (sender instanceof Player) {
- Player p = (Player) sender;
- Location targetLoc = getTarget(p).getLocation();
- return StringUtil.copyPartialMatches(args[3], Collections.singletonList("" + targetLoc.getBlockY()), new ArrayList<>());
- }
- }
- } else if (args.length == 5) {
- if (args[0].equalsIgnoreCase("opengate")
- || args[0].equalsIgnoreCase("closegate")
- || args[0].equalsIgnoreCase("lampon")
- || args[0].equalsIgnoreCase("lampoff")
- || args[0].equalsIgnoreCase("redstonetimer")) {
- if (sender instanceof Player) {
- Player p = (Player) sender;
- Location targetLoc = getTarget(p).getLocation();
- return StringUtil.copyPartialMatches(args[4], Collections.singletonList("" + targetLoc.getBlockZ()), new ArrayList<>());
- }
- }
- } else if (args.length == 6) {
- if (args[0].equalsIgnoreCase("opengate")) {
- return StringUtil.copyPartialMatches(args[5], Arrays.stream(WalkingDirection.values()).map(Enum::toString).collect(Collectors.toList()), new ArrayList<>());
- }
- } else if (args.length == 7) {
- if (args[0].equalsIgnoreCase("opengate")) {
- return StringUtil.copyPartialMatches(args[6], Arrays.stream(WalkingDirection.values()).map(Enum::toString).collect(Collectors.toList()), new ArrayList<>());
- }
- }
- }
-
- return null;
- }
-
- private Block getTarget(Player p) {
- return p.getTargetBlock(null, 200);
- }
-}