From ca721c1acbd5ca703ab039bca99b0bd2438e2aa2 Mon Sep 17 00:00:00 2001 From: md678685 Date: Mon, 27 Aug 2018 21:29:30 +0100 Subject: [PATCH 1/3] Add V1_13_R2 to ReflUtil --- nms/ReflectionProvider/src/net/ess3/nms/refl/ReflUtil.java | 1 + 1 file changed, 1 insertion(+) diff --git a/nms/ReflectionProvider/src/net/ess3/nms/refl/ReflUtil.java b/nms/ReflectionProvider/src/net/ess3/nms/refl/ReflUtil.java index 5ed8aa833..fd9ffd607 100644 --- a/nms/ReflectionProvider/src/net/ess3/nms/refl/ReflUtil.java +++ b/nms/ReflectionProvider/src/net/ess3/nms/refl/ReflUtil.java @@ -24,6 +24,7 @@ public class ReflUtil { public static final NMSVersion V1_11_R1 = NMSVersion.fromString("v1_11_R1"); public static final NMSVersion V1_12_R1 = NMSVersion.fromString("v1_12_R1"); public static final NMSVersion V1_13_R1 = NMSVersion.fromString("v1_13_R1"); + public static final NMSVersion V1_13_R2 = NMSVersion.fromString("v1_13_R2"); private static NMSVersion nmsVersionObject; private static String nmsVersion; From acbc96fd55187664569b8f1bc915b73db8fa4349 Mon Sep 17 00:00:00 2001 From: md678685 Date: Sat, 1 Sep 2018 10:34:11 +0100 Subject: [PATCH 2/3] Add enum utility helpers for Statistic and Material --- .../earth2me/essentials/utils/EnumUtil.java | 35 ++++++++++++++++ .../essentials/utils/MaterialUtil.java | 41 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 Essentials/src/com/earth2me/essentials/utils/EnumUtil.java create mode 100644 Essentials/src/com/earth2me/essentials/utils/MaterialUtil.java diff --git a/Essentials/src/com/earth2me/essentials/utils/EnumUtil.java b/Essentials/src/com/earth2me/essentials/utils/EnumUtil.java new file mode 100644 index 000000000..44f233ead --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/EnumUtil.java @@ -0,0 +1,35 @@ +package com.earth2me.essentials.utils; + +import org.bukkit.Material; +import org.bukkit.Statistic; + +import java.lang.reflect.Field; + +public class EnumUtil { + + /** + * Looks up enum fields by checking multiple names. + */ + public static T valueOf(Class enumClass, String... names) { + for (String name : names) { + try { + Field enumField = enumClass.getDeclaredField(name); + + if (enumField.isEnumConstant()) { + return (T) enumField.get(null); + } + } catch (NoSuchFieldException | IllegalAccessException ignored) {} + } + + return null; + } + + public static Material getMaterial(String... names) { + return valueOf(Material.class, names); + } + + public static Statistic getStatistic(String... names) { + return valueOf(Statistic.class, names); + } + +} diff --git a/Essentials/src/com/earth2me/essentials/utils/MaterialUtil.java b/Essentials/src/com/earth2me/essentials/utils/MaterialUtil.java new file mode 100644 index 000000000..0b65ddd66 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/MaterialUtil.java @@ -0,0 +1,41 @@ +package com.earth2me.essentials.utils; + +import org.bukkit.Material; + +import java.util.HashSet; +import java.util.Set; + +import static com.earth2me.essentials.utils.EnumUtil.getMaterial; + +public class MaterialUtil { + + private static final Set BEDS = new HashSet<>(); + + static { + // Adds WHITE_BED if 1.13+, otherwise BED + BEDS.add(getMaterial("WHITE_BED", "BED")); + + // Don't keep looking up and adding BED if we're not on 1.13+ + if (BEDS.add(getMaterial("ORANGE_BED", "BED"))) { + BEDS.add(getMaterial("MAGENTA_BED", "BED")); + BEDS.add(getMaterial("LIGHT_BLUE_BED", "BED")); + BEDS.add(getMaterial("YELLOW_BED", "BED")); + BEDS.add(getMaterial("LIME_BED", "BED")); + BEDS.add(getMaterial("PINK_BED", "BED")); + BEDS.add(getMaterial("GRAY_BED", "BED")); + BEDS.add(getMaterial("LIGHT_GRAY_BED", "BED")); + BEDS.add(getMaterial("CYAN_BED", "BED")); + BEDS.add(getMaterial("PURPLE_BED", "BED")); + BEDS.add(getMaterial("BLUE_BED", "BED")); + BEDS.add(getMaterial("BROWN_BED", "BED")); + BEDS.add(getMaterial("GREEN_BED", "BED")); + BEDS.add(getMaterial("RED_BED", "BED")); + BEDS.add(getMaterial("BLACK_BED", "BED")); + } + } + + public static boolean isBed(Material material) { + return BEDS.contains(material); + } + +} From 6c9c9ad42a65091ff3dc28a7583e13e6660d8641 Mon Sep 17 00:00:00 2001 From: md678685 Date: Sat, 1 Sep 2018 10:35:08 +0100 Subject: [PATCH 3/3] Use enum methods throughout plugin for cross-version enum lookups --- .../essentials/EssentialsBlockListener.java | 8 ++--- .../essentials/EssentialsPlayerListener.java | 3 +- .../essentials/commands/Commandspawner.java | 9 ++---- .../essentials/commands/Commandwhois.java | 13 ++++---- .../essentials/utils/LocationUtil.java | 30 +++++-------------- 5 files changed, 20 insertions(+), 43 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/EssentialsBlockListener.java b/Essentials/src/com/earth2me/essentials/EssentialsBlockListener.java index dc6e954ba..7d30de172 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsBlockListener.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsBlockListener.java @@ -1,5 +1,6 @@ package com.earth2me.essentials; +import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.LocationUtil; import net.ess3.api.IEssentials; import org.bukkit.GameMode; @@ -31,12 +32,7 @@ public class EssentialsBlockListener implements Listener { if (is == null) { return; } - Material MOB_SPAWNER; - try { - MOB_SPAWNER = Material.SPAWNER; - } catch (Exception e) { - MOB_SPAWNER = Material.valueOf("MOB_SPAWNER"); - } + Material MOB_SPAWNER = EnumUtil.getMaterial("SPAWNER", "MOB_SPAWNER"); if (is.getType() == MOB_SPAWNER && event.getItemInHand() != null && event.getPlayer() != null && event.getItemInHand().getType() == MOB_SPAWNER) { final BlockState blockState = event.getBlockPlaced().getState(); diff --git a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java index ba93b60ad..03fe03ce3 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java @@ -6,6 +6,7 @@ import com.earth2me.essentials.textreader.TextInput; import com.earth2me.essentials.textreader.TextPager; import com.earth2me.essentials.utils.DateUtil; import com.earth2me.essentials.utils.LocationUtil; +import com.earth2me.essentials.utils.MaterialUtil; import net.ess3.api.IEssentials; import net.ess3.nms.refl.ReflUtil; @@ -592,7 +593,7 @@ public class EssentialsPlayerListener implements Listener { public void onPlayerInteract(final PlayerInteractEvent event) { switch (event.getAction()) { case RIGHT_CLICK_BLOCK: - if (!event.isCancelled() && event.getClickedBlock().getType() == Material.LEGACY_BED && ess.getSettings().getUpdateBedAtDaytime()) { + if (!event.isCancelled() && MaterialUtil.isBed(event.getClickedBlock().getType()) && ess.getSettings().getUpdateBedAtDaytime()) { User player = ess.getUser(event.getPlayer()); if (player.isAuthorized("essentials.sethome.bed")) { player.getBase().setBedSpawnLocation(event.getClickedBlock().getLocation()); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandspawner.java b/Essentials/src/com/earth2me/essentials/commands/Commandspawner.java index 1e24f752e..ee079d678 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandspawner.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandspawner.java @@ -3,6 +3,7 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.Mob; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.LocationUtil; import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.StringUtil; @@ -28,12 +29,8 @@ public class Commandspawner extends EssentialsCommand { } final Location target = LocationUtil.getTarget(user.getBase()); - Material MOB_SPAWNER; - try { - MOB_SPAWNER = Material.SPAWNER; - } catch (Exception e) { - MOB_SPAWNER = Material.valueOf("MOB_SPAWNER"); - } + Material MOB_SPAWNER = EnumUtil.getMaterial("SPAWNER", "MOB_SPAWNER"); + if (target == null || target.getBlock().getType() != MOB_SPAWNER) { throw new Exception(tl("mobSpawnTarget")); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandwhois.java b/Essentials/src/com/earth2me/essentials/commands/Commandwhois.java index a47c4ead9..0515d12e5 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandwhois.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandwhois.java @@ -4,6 +4,7 @@ import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.User; import com.earth2me.essentials.craftbukkit.SetExpFix; import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.NumberUtil; import org.bukkit.Server; import org.bukkit.Statistic; @@ -16,17 +17,13 @@ import static com.earth2me.essentials.I18n.tl; public class Commandwhois extends EssentialsCommand { - private Statistic playOneTick; + private final Statistic playOneTick; public Commandwhois() { super("whois"); - try { - // For some reason, in 1.13 PLAY_ONE_MINUTE = ticks played - // https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/commits/b848d8ce633871b52115247b089029749c02f579 - playOneTick = Statistic.valueOf("PLAY_ONE_MINUTE"); - } catch (IllegalArgumentException e) { - playOneTick = Statistic.valueOf("PLAY_ONE_TICK"); - } + // For some reason, in 1.13 PLAY_ONE_MINUTE = ticks played + // https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/commits/b848d8ce633871b52115247b089029749c02f579 + playOneTick = EnumUtil.getStatistic("PLAY_ONE_MINUTE", "PLAY_ONE_TICK"); } @Override diff --git a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java index ba0bd6c84..571333347 100644 --- a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java @@ -113,36 +113,22 @@ public class LocationUtil { switch (below.getType()) { case LAVA: case FIRE: - case BLACK_BED: - case BLUE_BED: - case BROWN_BED: - case CYAN_BED: - case GRAY_BED: - case GREEN_BED: - case LIGHT_BLUE_BED: - case LIGHT_GRAY_BED: - case LIME_BED: - case MAGENTA_BED: - case ORANGE_BED: - case PINK_BED: - case PURPLE_BED: - case RED_BED: - case WHITE_BED: - case YELLOW_BED: return true; } + + if (MaterialUtil.isBed(below.getType())) { + return true; + } + try { if (below.getType() == Material.valueOf("FLOWING_LAVA")) { return true; } } catch (Exception ignored) { // 1.13 LAVA uses Levelled } - Material PORTAL; - try { - PORTAL = Material.NETHER_PORTAL; - } catch (Exception ignored) { - PORTAL = Material.valueOf("PORTAL"); - } + + Material PORTAL = EnumUtil.getMaterial("NETHER_PORTAL", "PORTAL"); + if (world.getBlockAt(x, y, z).getType() == PORTAL) { return true; }