Зарегистрировать команды для предметов

Версия Minecraft
1.7.10
28
0
Здравствуйте. Я хочу знать, как выполнить команду, зарегистрировав команду в элементе, который вы держите, с помощью команды "/register <command>" и щелкнув ее правой кнопкой мыши.
Есть хорошая идея?
 
28
0
When executing a register command, write a line from the arguments of the command to the ItemStack NBT. After get the command from the NBT for the item in the onItemRigthClick method and execute command.
Thanks you, but I don't understand. I gave the item an NBT value of "tex" and executed it with a PlayerInteractEvent.
command:
else if(args[0].equals("register")) {
    ItemStack stack = getCommandSenderAsPlayer(sender).getHeldItem();
    NBTTagCompound nbt;
    if(stack.hasTagCompound()) {
    nbt = stack.getTagCompound();
    }
    else {
    nbt = new NBTTagCompound();
    }
    if(nbt.hasKey("tex")){
    nbt.setString("tex", nbt.getString("tex"));
    }
    else {
    nbt.setString("tex", "tex");
    }
    stack.setTagCompound(nbt);
}
PlayerInteractEvent:
@SubscribeEvent
public void onRightClick(PlayerInteractEvent event) {
    if(event.action == Action.RIGHT_CLICK_AIR) {
        ItemStack stack = event.entityPlayer.getHeldItem();
        if(stack.hasTagCompound() && stack.getTagCompound().hasKey("tex")) {
            event.entityPlayer.addChatMessage(new ChatComponentText(stack.getDisplayName()));
        }
    }
}

This gives the item are holding a value of "tex" through the register command and then displays the name of the item when player are right-clicking. The problem is how to change this tex value to command arguments. and for some reason, it seems like PlayerInteractEvent is running twice.
 

tox1cozZ

aka Agravaine
8,456
598
2,892
1579174382366.png
In command:
Java:
ItemStack stack = getCommandSenderAsPlayer(sender).getHeldItem();
if(stack != null){
    NBTTagCompound nbt = stack.getTagCompound();
    if(!stack.hasTagCompound()){
        stack.setTagCompound(nbt = new NBTTagCompound());
    }
   
    nbt.setString("command", args[1]); // args[1] - second command argument
}
In event:
Java:
ItemStack stack = event.entityPlayer.getHeldItem();
if(!event.entityPlayer.worldObj.isRemote && stack != null && stack.hasTagCompound() && stack.getTagCompound().hasKey("command")) {
    MinecraftServer.getServer().getCommandManager().executeCommand(event.entityPlayer, stack.getTagCompound().getString("command"));
}
 
I have to be inside the class of the item that I want the command to execute I currently have this in my class how do i implement it i'm something new in this topic sorry
Java:
package NarutoKF.tut.items;

import NarutoKF.tut.NarutoMod;
import NarutoKF.tut.lib.Reference;
import net.minecraft.item.ItemSword;



public class ItemNose extends ItemSword {


    public ItemNose(ToolMaterial mat){
        super(mat);
        this.setCreativeTab(NarutoMod.tutTab);
        setFull3D();
        setTextureName(Reference.MOD_ID + ":hacha2filos");
        setUnlocalizedName(Reference.MOD_ID + ".hacha2filos");
        
    }
}
 
Сверху