TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/Command_enchant.java

255 lines
7.8 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.command;
2019-02-22 23:16:08 +00:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.IntStream;
2019-02-22 23:16:08 +00:00
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.rank.Rank;
2019-02-22 23:16:08 +00:00
import org.bukkit.ChatColor;
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
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)
2019-02-22 23:16:08 +00:00
@CommandParameters(description = "Enchant items.", usage = "/<command> <list | addall | reset | add <name> [level] | remove <name>>")
public class Command_enchant extends FreedomCommand
{
@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)
{
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
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())
{
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
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"))
{
Arrays.stream(Enchantment.values()).filter(enchantment -> enchantment.canEnchantItem(item)).forEach(ench ->
{
try
{
item.addEnchantment(ench, ench.getMaxLevel());
}
catch (Exception ex)
{
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
msg("Could not add enchantment: " + ench.getName());
}
});
msg("Added all possible enchantments for this item.");
}
else if (args[0].equalsIgnoreCase("reset"))
{
item.getEnchantments().keySet().forEach(item::removeEnchantment);
msg("Removed all enchantments.");
}
else
{
if (args.length < 2)
{
return false;
}
Enchantment ench = null;
try
{
2020-04-23 11:18:03 +00:00
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"))
{
2019-02-22 23:16:08 +00:00
if (!ench.canEnchantItem(item) && !ConfigEntry.ALLOW_UNSAFE_ENCHANTMENTS.getBoolean())
{
2019-02-22 23:16:08 +00:00
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]);
2019-02-22 23:16:08 +00:00
}
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
{
2019-02-22 23:16:08 +00:00
item.addUnsafeEnchantment(ench, level);
}
2019-02-22 23:16:08 +00:00
msg("Added enchantment: " + ench.getName());
}
else if (args[0].equals("remove"))
{
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
item.removeEnchantment(ench);
msg("Removed enchantment: " + ench.getName());
}
}
return true;
}
2019-02-22 23:16:08 +00:00
@SuppressWarnings("deprecation")
2019-02-22 23:16:08 +00:00
public List<String> getAllEnchantments()
{
List<String> enchantments = new ArrayList<>();
2019-02-22 23:16:08 +00:00
for (Enchantment enchantment : Enchantment.values())
{
enchantments.add(enchantment.getName());
}
return enchantments;
}
@SuppressWarnings("deprecation")
2019-02-22 23:16:08 +00:00
public List<String> getAllEnchantments(ItemStack item)
{
List<String> enchantments = new ArrayList<>();
2019-02-22 23:16:08 +00:00
for (Enchantment enchantment : Enchantment.values())
{
if (enchantment.canEnchantItem(item))
{
enchantments.add(enchantment.getName());
}
}
return enchantments;
}
@SuppressWarnings("deprecation")
2019-02-22 23:16:08 +00:00
public List<String> getEnchantments(ItemStack item)
{
List<String> enchantments = new ArrayList<>();
2019-02-22 23:16:08 +00:00
for (Enchantment enchantment : item.getEnchantments().keySet())
{
enchantments.add(enchantment.getName());
}
return enchantments;
}
@SuppressWarnings("deprecation")
2019-02-22 23:16:08 +00:00
@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();
2019-02-22 23:16:08 +00:00
if (item.getType() == Material.AIR)
2019-02-22 23:16:08 +00:00
{
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);
}
}
2022-11-13 13:40:36 +00:00
else if (args.length == 3 && args[0].equals("add"))
2019-02-22 23:16:08 +00:00
{
2022-11-13 13:40:36 +00:00
Enchantment enchantment = Enchantment.getByName(args[1].toUpperCase());
if (enchantment != null)
2019-02-22 23:16:08 +00:00
{
2022-11-13 13:40:36 +00:00
if (!unsafe)
2019-02-22 23:16:08 +00:00
{
return IntStream.rangeClosed(1, enchantment.getMaxLevel()).mapToObj(String::valueOf).toList();
2022-11-13 13:40:36 +00:00
}
else
{
return Collections.singletonList("[level]");
2019-02-22 23:16:08 +00:00
}
}
}
return Collections.emptyList();
}
}