TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/Command_enchant.java
Paldiu 5c0f77c7c5 Removal of Lombok
Lombok implementation removal.

I have also gone through and replaced things with inline methods and variables, lambdas, and simplified loops down, removed unnecessary guard clauses, and overall cleaned up every single class. This took a long time, please do remember to follow proper naming conventions, don't include unnecessary guard clauses, follow exception rules and comment rules, and please PLEASE remember to use the DIAMOND OPERATOR rather than just inferring RAW TYPES!!!

Thank you!!
2020-12-25 14:46:43 -05:00

275 lines
8.1 KiB
Java

package me.totalfreedom.totalfreedommod.command;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.rank.Rank;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@CommandPermissions(level = Rank.OP, source = SourceType.ONLY_IN_GAME)
@CommandParameters(description = "Enchant items.", usage = "/<command> <list | addall | reset | add <name> [level] | remove <name>>")
public class Command_enchant extends FreedomCommand
{
public static List<String> stringNumberRange(int min, int max)
{
List<String> range = new ArrayList<>();
for (int i = min; i <= max; i++)
{
range.add(String.valueOf(i));
}
return range;
}
@SuppressWarnings("deprecation")
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
if (args.length < 1)
{
return false;
}
ItemStack item = Objects.requireNonNull(playerSender.getEquipment()).getItemInMainHand();
if (item.getType() == Material.AIR)
{
msg("You have to hold an item to enchant it");
return true;
}
if (args[0].equalsIgnoreCase("list"))
{
boolean has_enchantments = false;
StringBuilder possible_ench = new StringBuilder("Possible enchantments for held item: ");
for (Enchantment ench : Enchantment.values())
{
if (ench.canEnchantItem(item))
{
has_enchantments = true;
possible_ench.append(ench.getName()).append(", ");
}
}
if (has_enchantments)
{
msg(possible_ench.toString());
}
else
{
msg("The held item has no enchantments.");
}
}
else if (args[0].equalsIgnoreCase("addall"))
{
for (Enchantment ench : Enchantment.values())
{
try
{
if (ench.canEnchantItem(item))
{
item.addEnchantment(ench, ench.getMaxLevel());
}
}
catch (Exception ex)
{
msg("Could not add enchantment: " + ench.getName());
}
}
msg("Added all possible enchantments for this item.");
}
else if (args[0].equalsIgnoreCase("reset"))
{
for (Enchantment ench : item.getEnchantments().keySet())
{
item.removeEnchantment(ench);
}
msg("Removed all enchantments.");
}
else
{
if (args.length < 2)
{
return false;
}
Enchantment ench = null;
try
{
ench = Enchantment.getByName(args[1].toUpperCase());
}
catch (Exception ignored)
{
}
if (ench == null)
{
msg(args[1] + " is an invalid enchantment for the held item. Type \"/enchant list\" for valid enchantments for this item.");
return true;
}
if (args[0].equalsIgnoreCase("add"))
{
if (!ench.canEnchantItem(item) && !ConfigEntry.ALLOW_UNSAFE_ENCHANTMENTS.getBoolean())
{
msg("Can't use this enchantment on held item.");
return true;
}
int level = ench.getMaxLevel();
if (args.length > 2)
{
try
{
if (ConfigEntry.ALLOW_UNSAFE_ENCHANTMENTS.getBoolean())
{
level = Integer.parseInt(args[2]);
}
else
{
level = Math.max(1, Math.min(ench.getMaxLevel(), Integer.parseInt(args[2])));
}
}
catch (NumberFormatException ex)
{
msg("\"" + args[2] + "\" is not a valid number", ChatColor.RED);
return true;
}
}
if (!ConfigEntry.ALLOW_UNSAFE_ENCHANTMENTS.getBoolean())
{
item.addEnchantment(ench, level);
}
else
{
item.addUnsafeEnchantment(ench, level);
}
msg("Added enchantment: " + ench.getName());
}
else if (args[0].equals("remove"))
{
item.removeEnchantment(ench);
msg("Removed enchantment: " + ench.getName());
}
}
return true;
}
@SuppressWarnings("deprecation")
public List<String> getAllEnchantments()
{
List<String> enchantments = new ArrayList<>();
for (Enchantment enchantment : Enchantment.values())
{
enchantments.add(enchantment.getName());
}
return enchantments;
}
@SuppressWarnings("deprecation")
public List<String> getAllEnchantments(ItemStack item)
{
List<String> enchantments = new ArrayList<>();
for (Enchantment enchantment : Enchantment.values())
{
if (enchantment.canEnchantItem(item))
{
enchantments.add(enchantment.getName());
}
}
return enchantments;
}
@SuppressWarnings("deprecation")
public List<String> getEnchantments(ItemStack item)
{
List<String> enchantments = new ArrayList<>();
for (Enchantment enchantment : item.getEnchantments().keySet())
{
enchantments.add(enchantment.getName());
}
return enchantments;
}
@SuppressWarnings("deprecation")
@Override
public List<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args)
{
Player player;
if (sender instanceof Player)
{
player = (Player)sender;
}
else
{
return Collections.emptyList();
}
ItemStack item = Objects.requireNonNull(player.getEquipment()).getItemInMainHand();
if (item.getType() == Material.AIR)
{
return Collections.emptyList();
}
boolean unsafe = ConfigEntry.ALLOW_UNSAFE_ENCHANTMENTS.getBoolean();
if (args.length == 1)
{
return Arrays.asList("list", "addall", "reset", "add", "remove");
}
else if (args.length == 2)
{
if (args[0].equals("add"))
{
if (unsafe)
{
return getAllEnchantments();
}
else
{
return getAllEnchantments(item);
}
}
else if (args[0].equals("remove"))
{
return getEnchantments(item);
}
}
else if (args.length == 3)
{
if (args[0].equals("add"))
{
Enchantment enchantment = Enchantment.getByName(args[1].toUpperCase());
if (enchantment != null)
{
if (!unsafe)
{
return stringNumberRange(1, enchantment.getMaxLevel());
}
else
{
return Collections.singletonList("[level]");
}
}
}
}
return Collections.emptyList();
}
}