Jump to content

Recommended Posts

Posted

How to register commands and where is the class, that register vanilla commands in ServerCommandManager, and I should register them in init or preinit?

Posted

You should register them during FMLServerStartingEvent. Example:

 

public class MyMainModClass {

 

    // Additional code ...

 

    @EventHandler

    public void serverStarting(FMLServerStartingEvent event) {

        event.registerServerCommand(new MyCustomCommand()); // MyCustomCommand must inherit the CommandBase class

    }

}

if (user.hasKnowledgeOfJava) {

    if (user.question.hasCode) {

        return interpetHelpfulResponse(user.getQuestion());

    } else {

        return "Could you post your code please?";

    }

} else {

    return "Learn some freaking Java!";

}

Posted

MrArcane111, I followed this http://www.minecraftforge.net/wiki/Server_Command and it worked perfectly fine for me, and I am using 1.7.2.

 

I don't see what else to discuss here even, follow the tutorial, the end.

But since I am in good mood,  I will share my sample command code with you.

package com.zetaworx.proboblyawesome.handler;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;

public class CommandHandle implements ICommand
{
private List<String> aliases;
public CommandHandle()
{
	this.aliases = new ArrayList<String>();
	this.aliases.add("probobly");
	this.aliases.add("awesome");
	this.aliases.add("proboblyawesome");
}

@Override
public String getCommandName()
{
	return "pa";
}

@Override
public String getCommandUsage(ICommandSender icommandsender)
{
	return "pa <text/help>";
}

@Override
public List<String> getCommandAliases()
{
	return this.aliases;
}

@Override
public void processCommand(ICommandSender icommandsender, String[] astring)
{
	if(astring.length == 0)
	{
		icommandsender.addChatMessage(new ChatComponentText("Invalid Arguments. Usage: " + this.getCommandUsage(icommandsender)));
		return;
	}
	if (astring[0] == "help")
	{
		icommandsender.addChatMessage(new ChatComponentText("Usage: " + this.getCommandUsage(icommandsender)));
		return;
	} else {
	ChatComponentText msg = new ChatComponentText("Output: [");
	for (int i = 0;i < astring.length; ++i)
	{
		msg.appendText(" " + astring[i]);
	}
	msg.appendText(" ]");
	icommandsender.addChatMessage(msg);	
	}
}

@Override
public boolean canCommandSenderUseCommand(ICommandSender icommandsender)
{
return true;
}

@Override
public List<?> addTabCompletionOptions(ICommandSender icommandsender,
String[] astring)
{
return null;
}

@Override
public boolean isUsernameIndex(String[] astring, int i)
{
return false;
}

@Override
public int compareTo(Object o)
{
return 0;
}
}

 

and this in your mod file

    @EventHandler
    public void serverLoad(FMLServerStartingEvent event)
    {
    	event.registerServerCommand(new CommandHandle());
    }

 

my command is using ICommand interface which is what CommandBase is extending from, but for time being I didn't need CommandBase functions so I didn't use it.

 

Ps. my command also shows how to send user a chat message.

~I was here~

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.