You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
224 lines
9.1 KiB
Java
224 lines
9.1 KiB
Java
package com.zivilon.itemeffects.commands;
|
|
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandExecutor;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.potion.PotionEffectType;
|
|
|
|
import me.dpohvar.powernbt.api.NBTCompound;
|
|
import me.dpohvar.powernbt.api.NBTManager;
|
|
import me.dpohvar.powernbt.api.NBTList;
|
|
import me.dpohvar.powernbt.PowerNBT;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import com.zivilon.itemeffects.ItemEffects;
|
|
|
|
public class itemeffect_command implements CommandExecutor {
|
|
private final ItemEffects plugin;
|
|
|
|
public itemeffect_command(ItemEffects plugin) {
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
@Override
|
|
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
|
if (cmd.getName().equalsIgnoreCase("itemeffect")) {
|
|
if (!sender.hasPermission("admin")) {
|
|
sender.sendMessage(ChatColor.RED + "You do not have permission to use this command.");
|
|
return true;
|
|
}
|
|
|
|
if (!(sender instanceof Player)) {
|
|
sender.sendMessage("This command can only be used by players.");
|
|
return true;
|
|
}
|
|
Player player = (Player) sender;
|
|
|
|
if (args.length < 1) {
|
|
sender.sendMessage("Invalid usage. Correct usage: /itemeffect <add/remove/list>");
|
|
return true;
|
|
}
|
|
|
|
String subcommand = args[0].toLowerCase();
|
|
|
|
switch (subcommand) {
|
|
case "add":
|
|
if (args.length < 4) {
|
|
player.sendMessage("Usage: /itemeffect add <type> <effect> <level>");
|
|
return true;
|
|
}
|
|
|
|
String type = args[1].toUpperCase();
|
|
String effect = args[2].toUpperCase();
|
|
int level;
|
|
try {
|
|
level = Integer.parseInt(args[3]);
|
|
} catch (NumberFormatException e) {
|
|
player.sendMessage("Invalid level. Please enter a number.");
|
|
return true;
|
|
}
|
|
Integer radius = null; // We use Integer to allow for null checks
|
|
if (type.equals("HELD_AURA") || type.equals("INVENTORY_AURA") || type.equals("WORN_AURA")) {
|
|
if (args.length < 5) {
|
|
player.sendMessage("Aura type effects require a radius. Usage: /itemeffect add <type> <effect> <level> <radius>");
|
|
return true;
|
|
}
|
|
try {
|
|
radius = Integer.parseInt(args[4]);
|
|
} catch (NumberFormatException e) {
|
|
player.sendMessage("Invalid radius. Please enter a number.");
|
|
return true;
|
|
}
|
|
}
|
|
if (!isValidType(type)) {
|
|
player.sendMessage("Invalid type. Valid types are: HELD, WORN, INVENTORY.");
|
|
return true;
|
|
}
|
|
|
|
if (!isValidEffect(effect)) {
|
|
player.sendMessage("Invalid effect. Provide a valid potion effect.");
|
|
return true;
|
|
}
|
|
|
|
ItemStack item = player.getInventory().getItemInHand();
|
|
|
|
addEffectToItem(item, type, effect, level, radius);
|
|
player.sendMessage("Effect added to item!");
|
|
break;
|
|
case "addaura":
|
|
// Not sure why this is here, regular add should work.
|
|
// Will remove later once I work out why this is here in the first place
|
|
break;
|
|
case "remove":
|
|
if (args.length < 3) {
|
|
player.sendMessage("Usage: /itemeffect remove <type> <effect>");
|
|
return true;
|
|
}
|
|
|
|
String typeToRemove = args[1].toUpperCase();
|
|
String effectToRemove = args[2].toUpperCase();
|
|
|
|
if (removeEffectFromItem(player.getInventory().getItemInHand(), typeToRemove, effectToRemove)) {
|
|
player.sendMessage("Effect removed from item!");
|
|
} else {
|
|
player.sendMessage("Specified effect not found on item.");
|
|
}
|
|
break;
|
|
case "removeaura":
|
|
// Not sure why this is here, regular remove should work.
|
|
// Will remove later once I work out why this is here in the first place
|
|
break;
|
|
case "list":
|
|
listEffectsOnItem(player.getInventory().getItemInHand(), player);
|
|
break;
|
|
|
|
default:
|
|
sender.sendMessage("Unknown subcommand. Correct usage: /itemeffect <add/remove/list>");
|
|
break;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
private boolean isValidType(String type) {
|
|
return Arrays.asList("HELD", "WORN", "INVENTORY", "HELD_AURA", "WORN_AURA", "INVENTORY_AURA").contains(type);
|
|
}
|
|
|
|
private boolean isValidEffect(String effect) {
|
|
if (effect == null || effect.isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
PotionEffectType potionType = PotionEffectType.getByName(effect.toUpperCase());
|
|
return potionType != null;
|
|
}
|
|
|
|
|
|
private void addEffectToItem(ItemStack item, String type, String effect, int level, Integer radius) {
|
|
// Fetch current item's NBT data
|
|
NBTCompound itemData = plugin.nbtManager.read(item);
|
|
NBTCompound newEffect = new NBTCompound();
|
|
|
|
if (radius != null) {
|
|
newEffect.put("radius", radius.intValue());
|
|
}
|
|
|
|
// Get the "ItemEffects" list from the NBT data, or create one if it doesn't exist
|
|
NBTList itemEffects = itemData.getList("ItemEffects");
|
|
if (itemEffects == null) {
|
|
itemEffects = new NBTList();
|
|
itemData.put("ItemEffects", itemEffects);
|
|
|
|
plugin.nbtManager.write(item, itemData);
|
|
|
|
itemData = plugin.nbtManager.read(item);
|
|
itemEffects = itemData.getList("ItemEffects");
|
|
}
|
|
|
|
boolean effectFound = false;
|
|
for (Object obj : itemEffects) {
|
|
if (obj instanceof NBTCompound) {
|
|
NBTCompound currentEffect = (NBTCompound) obj;
|
|
String currentType = currentEffect.getString("type");
|
|
String currentEffectName = currentEffect.getString("effect");
|
|
if (type.equals(currentType) && effect.equals(currentEffectName)) {
|
|
// Found the existing effect. Overwrite its attributes.
|
|
currentEffect.put("level", level);
|
|
effectFound = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!effectFound) {
|
|
newEffect.put("type", type);
|
|
newEffect.put("effect", effect);
|
|
newEffect.put("level", level);
|
|
itemEffects.add(newEffect);
|
|
}
|
|
|
|
plugin.nbtManager.write(item, itemData);
|
|
}
|
|
private boolean removeEffectFromItem(ItemStack item, String type, String effect) {
|
|
NBTCompound itemData = plugin.nbtManager.read(item);
|
|
NBTList itemEffects = itemData.getList("ItemEffects");
|
|
if (itemEffects == null) return false;
|
|
|
|
for (Object obj : itemEffects) {
|
|
if (obj instanceof NBTCompound) {
|
|
NBTCompound currentEffect = (NBTCompound) obj;
|
|
String currentType = currentEffect.getString("type");
|
|
String currentEffectName = currentEffect.getString("effect");
|
|
if (type.equals(currentType) && effect.equals(currentEffectName)) {
|
|
itemEffects.remove(currentEffect);
|
|
plugin.nbtManager.write(item, itemData);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
private void listEffectsOnItem(ItemStack item, Player player) {
|
|
NBTCompound itemData = plugin.nbtManager.read(item);
|
|
NBTList itemEffects = itemData.getList("ItemEffects");
|
|
if (itemEffects == null || itemEffects.isEmpty()) {
|
|
player.sendMessage("No effects found on this item.");
|
|
return;
|
|
}
|
|
|
|
player.sendMessage(ChatColor.GOLD + "Item Effects:");
|
|
for (Object obj : itemEffects) {
|
|
if (obj instanceof NBTCompound) {
|
|
NBTCompound currentEffect = (NBTCompound) obj;
|
|
String currentType = currentEffect.getString("type");
|
|
String currentEffectName = currentEffect.getString("effect");
|
|
int currentLevel = currentEffect.getInt("level");
|
|
player.sendMessage(ChatColor.YELLOW + "- Type: " + currentType + ", Effect: " + currentEffectName + ", Level: " + currentLevel);
|
|
}
|
|
}
|
|
}
|
|
}
|