Use enum methods throughout plugin for cross-version enum lookups

This commit is contained in:
md678685 2018-09-01 10:35:08 +01:00
parent acbc96fd55
commit 6c9c9ad42a
5 changed files with 20 additions and 43 deletions

View File

@ -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();

View File

@ -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());

View File

@ -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"));
}

View File

@ -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

View File

@ -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;
}