Add showtags subcommand to nbttools

This commit is contained in:
nossr50 2020-02-17 18:30:09 -08:00
parent f45c70b694
commit 3c382a11ed
2 changed files with 56 additions and 8 deletions

View File

@ -1,22 +1,44 @@
package com.gmail.nossr50.commands.admin; package com.gmail.nossr50.commands.admin;
import co.aikar.commands.BaseCommand; import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.*;
import co.aikar.commands.annotation.Default;
import co.aikar.commands.annotation.Dependency;
import co.aikar.commands.annotation.Description;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@CommandAlias("nbttools") @CommandAlias("nbttools")
@Description("Read or Modify values of NBT on an item in-hand") @Description("Read or Modify values of NBT on an item in-hand")
public class NBTToolsCommand extends BaseCommand { public class NBTToolsCommand extends BaseCommand {
public static final String STYLE_TEXT_1 = "//////////";
@Dependency @Dependency
private mcMMO pluginRef; private mcMMO plugin;
@Default @Default
public void onCommand(Player player) { public void onCommand(Player player) {
player.sendMessage("hi"); player.sendMessage("hi");
} }
/**
* Show the NBT tags of an item in hand
*/
@Subcommand("showtags")
public void onShowTags(Player player) {
//Show NBT tags to player
player.sendMessage(STYLE_TEXT_1 + " NBT TOOLS " + STYLE_TEXT_1);
player.sendMessage("NBT Analysis: " + player.getInventory().getItemInMainHand().getType().getKey().toString());
player.sendMessage(STYLE_TEXT_1 + STYLE_TEXT_1);
plugin.getNbtManager().printNBT(player.getInventory().getItemInMainHand(), player);
player.sendMessage(ChatColor.GRAY + "NBT Analysis completed!");
}
@Subcommand("add")
public void onAddTags(Player player) {
}
@Subcommand("remove")
public void onRemoveTags(Player player) {
}
} }

View File

@ -4,6 +4,7 @@ package com.gmail.nossr50.util.nbt;
import com.gmail.nossr50.core.nbt.NBTBase; import com.gmail.nossr50.core.nbt.NBTBase;
import net.minecraft.server.v1_14_R1.NBTList; import net.minecraft.server.v1_14_R1.NBTList;
import net.minecraft.server.v1_14_R1.NBTTagCompound; import net.minecraft.server.v1_14_R1.NBTTagCompound;
import org.bukkit.ChatColor;
import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_14_R1.util.CraftNBTTagConfigSerializer; import org.bukkit.craftbukkit.v1_14_R1.util.CraftNBTTagConfigSerializer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -166,11 +167,36 @@ public class NBTManager {
*/ */
public void printNBT(ItemStack itemStack, Player player) { public void printNBT(ItemStack itemStack, Player player) {
NBTTagCompound tagCompoundCopy = getNBTCopy(itemStack); NBTTagCompound tagCompoundCopy = getNBTCopy(itemStack);
for(String key : tagCompoundCopy.getKeys()) { printNBT(tagCompoundCopy, player);
player.sendMessage("");
player.sendMessage("NBT Key: "+key);
player.sendMessage("NBT Value: " + tagCompoundCopy.get(key).asString());
} }
private void printNBT(NBTTagCompound nbtTagCompound, Player player) {
for(String key : nbtTagCompound.getKeys()) {
player.sendMessage("");
net.minecraft.server.v1_14_R1.NBTBase targetTag = nbtTagCompound.get(key);
//Recursively print contents
if(targetTag instanceof NBTTagCompound) {
NBTTagCompound childTagCompound = nbtTagCompound.getCompound(key);
if(childTagCompound != null) {
player.sendMessage(ChatColor.BLUE + "NBT named " + ChatColor.GOLD + key + ChatColor.BLUE + " is a tag compound, printing contents...");
printNBT(childTagCompound, player);
player.sendMessage(ChatColor.BLUE + "Exiting "+ key);
continue;
}
}
player.sendMessage(ChatColor.GOLD + "Tag Key: " + ChatColor.RESET + key);
if(targetTag == null) {
player.sendMessage(ChatColor.RED + "Tag is null!");
continue;
}
player.sendMessage(ChatColor.GREEN + "Tag Value: " + ChatColor.RESET + targetTag.asString());
}
} }
public boolean hasNBT(NBTBase nbt, NBTTagCompound otherNbt) { public boolean hasNBT(NBTBase nbt, NBTTagCompound otherNbt) {