Update several commands with cross-version enum lookups

This commit is contained in:
md678685 2018-12-31 12:53:23 +00:00
parent 5a14a64b6c
commit 0114b5e4f6
5 changed files with 28 additions and 13 deletions

View File

@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import com.earth2me.essentials.utils.EnumUtil;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Server; import org.bukkit.Server;
@ -15,6 +16,9 @@ import static com.earth2me.essentials.I18n.tl;
public class Commandbook extends EssentialsCommand { public class Commandbook extends EssentialsCommand {
private static final Material WRITABLE_BOOK = EnumUtil.getMaterial("WRITABLE_BOOK", "BOOK_AND_QUILL");
public Commandbook() { public Commandbook() {
super("book"); super("book");
} }
@ -44,7 +48,7 @@ public class Commandbook extends EssentialsCommand {
} }
} else { } else {
if (isAuthor(bmeta, player) || user.isAuthorized("essentials.book.others")) { if (isAuthor(bmeta, player) || user.isAuthorized("essentials.book.others")) {
ItemStack newItem = new ItemStack(Material.WRITABLE_BOOK, item.getAmount()); ItemStack newItem = new ItemStack(WRITABLE_BOOK, item.getAmount());
newItem.setItemMeta(bmeta); newItem.setItemMeta(bmeta);
InventoryWorkaround.setItemInMainHand(user.getBase(), newItem); InventoryWorkaround.setItemInMainHand(user.getBase(), newItem);
user.sendMessage(tl("editBookContents")); user.sendMessage(tl("editBookContents"));
@ -52,7 +56,7 @@ public class Commandbook extends EssentialsCommand {
throw new Exception(tl("denyBookEdit")); throw new Exception(tl("denyBookEdit"));
} }
} }
} else if (item.getType() == Material.WRITABLE_BOOK) { } else if (item.getType() == WRITABLE_BOOK) {
BookMeta bmeta = (BookMeta) item.getItemMeta(); BookMeta bmeta = (BookMeta) item.getItemMeta();
if (!user.isAuthorized("essentials.book.author")) { if (!user.isAuthorized("essentials.book.author")) {
bmeta.setAuthor(player); bmeta.setAuthor(player);

View File

@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.MetaItemStack; import com.earth2me.essentials.MetaItemStack;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.MaterialUtil;
import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.NumberUtil;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
@ -44,7 +45,7 @@ public class Commandfirework extends EssentialsCommand {
@Override @Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
final ItemStack stack = user.getBase().getInventory().getItemInMainHand(); final ItemStack stack = user.getBase().getInventory().getItemInMainHand();
if (stack.getType() == Material.FIREWORK_ROCKET || stack.getType() == Material.FIREWORK_STAR) { if (MaterialUtil.isFirework(stack.getType())) {
if (args.length > 0) { if (args.length > 0) {
if (args[0].equalsIgnoreCase("clear")) { if (args[0].equalsIgnoreCase("clear")) {
FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta(); FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();

View File

@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.EnumUtil;
import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.NumberUtil;
import com.earth2me.essentials.utils.VersionUtil; import com.earth2me.essentials.utils.VersionUtil;
import org.bukkit.Material; import org.bukkit.Material;
@ -20,6 +21,11 @@ import net.ess3.nms.refl.ReflUtil;
public class Commandrecipe extends EssentialsCommand { public class Commandrecipe extends EssentialsCommand {
private static final Material FIREWORK_ROCKET = EnumUtil.getMaterial("FIREWORK_ROCKET", "FIREWORK");
private static final Material FIREWORK_STAR = EnumUtil.getMaterial("FIREWORK_STAR", "FIREWORK_CHARGE");
private static final Material GUNPOWDER = EnumUtil.getMaterial("GUNPOWDER", "SULPHUR");
public Commandrecipe() { public Commandrecipe() {
super("recipe"); super("recipe");
} }
@ -67,11 +73,11 @@ public class Commandrecipe extends EssentialsCommand {
} else if (selectedRecipe instanceof ShapedRecipe) { } else if (selectedRecipe instanceof ShapedRecipe) {
shapedRecipe(sender, (ShapedRecipe) selectedRecipe, sender.isPlayer()); shapedRecipe(sender, (ShapedRecipe) selectedRecipe, sender.isPlayer());
} else if (selectedRecipe instanceof ShapelessRecipe) { } else if (selectedRecipe instanceof ShapelessRecipe) {
if (recipesOfType.size() == 1 && (itemType.getType() == Material.FIREWORK_ROCKET || itemType.getType() == Material.FIREWORK_STAR)) { if (recipesOfType.size() == 1 && (itemType.getType() == FIREWORK_ROCKET)) {
ShapelessRecipe shapelessRecipe = new ShapelessRecipe(itemType); ShapelessRecipe shapelessRecipe = new ShapelessRecipe(itemType);
shapelessRecipe.addIngredient(Material.LEGACY_SULPHUR); shapelessRecipe.addIngredient(GUNPOWDER);
shapelessRecipe.addIngredient(Material.PAPER); shapelessRecipe.addIngredient(Material.PAPER);
shapelessRecipe.addIngredient(Material.FIREWORK_ROCKET); shapelessRecipe.addIngredient(FIREWORK_STAR);
shapelessRecipe(sender, shapelessRecipe, sender.isPlayer()); shapelessRecipe(sender, shapelessRecipe, sender.isPlayer());
} else { } else {
shapelessRecipe(sender, (ShapelessRecipe) selectedRecipe, sender.isPlayer()); shapelessRecipe(sender, (ShapelessRecipe) selectedRecipe, sender.isPlayer());

View File

@ -2,6 +2,8 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import com.earth2me.essentials.utils.EnumUtil;
import com.earth2me.essentials.utils.MaterialUtil;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Server; import org.bukkit.Server;
@ -14,6 +16,9 @@ import java.util.List;
import static com.earth2me.essentials.I18n.tl; import static com.earth2me.essentials.I18n.tl;
public class Commandskull extends EssentialsCommand { public class Commandskull extends EssentialsCommand {
private static final Material SKULL_ITEM = EnumUtil.getMaterial("PLAYER_HEAD", "SKULL_ITEM");
public Commandskull() { public Commandskull() {
super("skull"); super("skull");
} }
@ -35,10 +40,10 @@ public class Commandskull extends EssentialsCommand {
SkullMeta metaSkull = null; SkullMeta metaSkull = null;
boolean spawn = false; boolean spawn = false;
if (itemSkull != null && itemSkull.getType() == Material.LEGACY_SKULL_ITEM && itemSkull.getDurability() == 3) { if (itemSkull != null && MaterialUtil.isPlayerHead(itemSkull.getType(), itemSkull.getDurability())) {
metaSkull = (SkullMeta) itemSkull.getItemMeta(); metaSkull = (SkullMeta) itemSkull.getItemMeta();
} else if (user.isAuthorized("essentials.skull.spawn")) { } else if (user.isAuthorized("essentials.skull.spawn")) {
itemSkull = new ItemStack(Material.LEGACY_SKULL_ITEM, 1, (byte) 3); itemSkull = new ItemStack(SKULL_ITEM, 1, (byte) 3);
metaSkull = (SkullMeta) itemSkull.getItemMeta(); metaSkull = (SkullMeta) itemSkull.getItemMeta();
spawn = true; spawn = true;
} else { } else {

View File

@ -17,13 +17,12 @@ import static com.earth2me.essentials.I18n.tl;
public class Commandwhois extends EssentialsCommand { public class Commandwhois extends EssentialsCommand {
private final Statistic playOneTick; // For some reason, in 1.13 PLAY_ONE_MINUTE = ticks played = what used to be PLAY_ONE_TICK
// https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/commits/b848d8ce633871b52115247b089029749c02f579
private static final Statistic PLAY_ONE_TICK = EnumUtil.getStatistic("PLAY_ONE_MINUTE", "PLAY_ONE_TICK");
public Commandwhois() { public Commandwhois() {
super("whois"); super("whois");
// 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 @Override
@ -42,7 +41,7 @@ public class Commandwhois extends EssentialsCommand {
sender.sendMessage(tl("whoisHunger", user.getBase().getFoodLevel(), user.getBase().getSaturation())); sender.sendMessage(tl("whoisHunger", user.getBase().getFoodLevel(), user.getBase().getSaturation()));
sender.sendMessage(tl("whoisExp", SetExpFix.getTotalExperience(user.getBase()), user.getBase().getLevel())); sender.sendMessage(tl("whoisExp", SetExpFix.getTotalExperience(user.getBase()), user.getBase().getLevel()));
sender.sendMessage(tl("whoisLocation", user.getLocation().getWorld().getName(), user.getLocation().getBlockX(), user.getLocation().getBlockY(), user.getLocation().getBlockZ())); sender.sendMessage(tl("whoisLocation", user.getLocation().getWorld().getName(), user.getLocation().getBlockX(), user.getLocation().getBlockY(), user.getLocation().getBlockZ()));
long playtimeMs = System.currentTimeMillis() - (user.getBase().getStatistic(playOneTick) * 50); long playtimeMs = System.currentTimeMillis() - (user.getBase().getStatistic(PLAY_ONE_TICK) * 50);
sender.sendMessage(tl("whoisPlaytime", DateUtil.formatDateDiff(playtimeMs))); sender.sendMessage(tl("whoisPlaytime", DateUtil.formatDateDiff(playtimeMs)));
if (!ess.getSettings().isEcoDisabled()) { if (!ess.getSettings().isEcoDisabled()) {
sender.sendMessage(tl("whoisMoney", NumberUtil.displayCurrency(user.getMoney(), ess))); sender.sendMessage(tl("whoisMoney", NumberUtil.displayCurrency(user.getMoney(), ess)));