Do not treat Material as an enum

This commit is contained in:
MD 2024-07-19 20:08:38 +01:00
parent 33e086ff05
commit d8244dbd80
3 changed files with 17 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package com.earth2me.essentials.perm; package com.earth2me.essentials.perm;
import com.earth2me.essentials.commands.Commandhat; import com.earth2me.essentials.commands.Commandhat;
import com.earth2me.essentials.utils.MaterialUtil;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
@ -10,16 +11,16 @@ import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
public final class PermissionsDefaults { public final class PermissionsDefaults {
private PermissionsDefaults() { private PermissionsDefaults() {
} }
public static void registerAllBackDefaults() { public static void registerAllBackDefaults() {
for (final World world : Bukkit.getWorlds()) { for (final World world : Bukkit.getWorlds()) {
registerBackDefaultFor(world); registerBackDefaultFor(world);
} }
} }
public static void registerBackDefaultFor(final World w) { public static void registerBackDefaultFor(final World w) {
final String permName = "essentials.back.into." + w.getName(); final String permName = "essentials.back.into." + w.getName();
@ -41,7 +42,7 @@ public final class PermissionsDefaults {
} }
final ImmutableMap.Builder<String, Boolean> children = ImmutableMap.builder(); final ImmutableMap.Builder<String, Boolean> children = ImmutableMap.builder();
for (final Material mat : Material.values()) { for (final Material mat : MaterialUtil.getKnownMaterials()) {
final String matPerm = Commandhat.PERM_PREFIX + mat.name().toLowerCase(); final String matPerm = Commandhat.PERM_PREFIX + mat.name().toLowerCase();
children.put(matPerm, true); children.put(matPerm, true);
pluginManager.addPermission(new Permission(matPerm, "Prevent using " + mat + " as a type of hat.", PermissionDefault.FALSE)); pluginManager.addPermission(new Permission(matPerm, "Prevent using " + mat + " as a type of hat.", PermissionDefault.FALSE));

View File

@ -14,7 +14,7 @@ import org.bukkit.inventory.ItemStack;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.EnumSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -37,15 +37,15 @@ public final class LocationUtil {
private static final Material FARMLAND = EnumUtil.getMaterial("FARMLAND"); private static final Material FARMLAND = EnumUtil.getMaterial("FARMLAND");
// The player can stand inside these materials // The player can stand inside these materials
private static final Set<Material> HOLLOW_MATERIALS = EnumSet.noneOf(Material.class); private static final Set<Material> HOLLOW_MATERIALS = new HashSet<>();
private static final Set<Material> TRANSPARENT_MATERIALS = EnumSet.noneOf(Material.class); private static final Set<Material> TRANSPARENT_MATERIALS = new HashSet<>();
static { static {
// If the server is running in a test environment, the isTransparent() method will blow up since // If the server is running in a test environment, the isTransparent() method will blow up since
// it requires the registry to be initialized. This is a workaround to prevent that from happening. // it requires the registry to be initialized. This is a workaround to prevent that from happening.
if (!Essentials.TESTING) { if (!Essentials.TESTING) {
// Materials from Material.isTransparent() // Materials from Material.isTransparent()
for (final Material mat : Material.values()) { for (final Material mat : MaterialUtil.getKnownMaterials()) {
if (mat.isTransparent()) { if (mat.isTransparent()) {
HOLLOW_MATERIALS.add(mat); HOLLOW_MATERIALS.add(mat);
} }

View File

@ -3,6 +3,7 @@ package com.earth2me.essentials.utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Registry;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.Damageable;
import org.bukkit.material.MaterialData; import org.bukkit.material.MaterialData;
@ -246,4 +247,11 @@ public final class MaterialUtil {
return DyeColor.WHITE; return DyeColor.WHITE;
} }
public static Material[] getKnownMaterials() {
if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_14_R01)) {
return Material.values();
}
return Registry.MATERIAL.stream().toArray(Material[]::new);
}
} }