Server Command

From Minecraft Forge
Jump to: navigation, search

How-To Icon.png

This is a How-To guide or Tutorial detailing a practice or process for Minecraft Forge or related software.

Contents

Prerequisites

Getting started

I will assume you know how to add files to your project and get them to build. The source code below is entirely complete - you need not have any code from a previous mod except for the proxies (which are trivial from Basic Modding).

Overview

This mod will demonstrate how to create a simple server command called "sample" which will take a single argument and chat the argument back to the player. It will also have an alias "sam" which does the same thing. Once running, the player can type "/sample text" and see "Sample: [text]".

Event Registration

Server commands may be registered by handling the FMLServerStartingEvent, which is similar to the other FML events your mod normally handles, like FMLInitializationEvent. Like those other events, you event handler is registered simply by annotating a method with the correct signature. In our main mod file which handles the FMLInitializationEvent, we will add the following:

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

The FMLServerStartingEvent contains a method we can use to register commands. So we just new up our command implementation and pass it to the registerServerCommand method.

Command Implementation

Server commands are classes which implement the ICommand interface. Our sample command implementation looks as follows:

package stargazer.minecraft.samples.servercommand;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;

public class SampleCommand implements ICommand
{
  private List aliases;
  public SampleCommand()
  {
    this.aliases = new ArrayList();
    this.aliases.add("sample");
    this.aliases.add("sam");
  }

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

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

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

  @Override
  public void processCommand(ICommandSender icommandsender, String[] astring)
  {
    if(astring.length == 0)
    {
      icommandsender.sendChatToPlayer("Invalid arguments");
      return;
    }
   
    icommandsender.sendChatToPlayer("Sample: [" + astring[0] + "]");
   
  }

  @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;
  }
}

In our constructor, we initialize a list containing the aliases for our command. This list will be returned by the getCommandAliases() method.

Everything else is an implementation of a method from the interface. These methods need to do the following:

getCommandName():

This method should return the name of the command, like "sample".

getCommandUsage(ICommandSender):

This method should return a string describing the command format. The ICommandSender argument lets the command get information about the player, translate strings for other languages, and send chat messages to the player.

getCommandAliases():

This method should return a list of all the aliases for the command. Aliases are alternate names and are usually shorter.

processCommand(ICommandSender, String[]):

This method executes the command. In our sample implementation, we first check the arguments passed to the command by the second parameter. If we didn't get valid arguments, we spit out a message to the player using the ICommandSender passed in as the first argument to this method. Once we have validated the arguments, then we finish the command, which for our sample simply sends a chat back to the player. This method has no return value.

If the Player sent the command you can use the following code to get the player object from the ICommandSender.

                EntityPlayer player;
               
                if(icommandsender instanceof EntityPlayer){
                        player = (EntityPlayer)icommandsender;
                }
                else {
                        icommandsender.sendChatToPlayer("Player Only Command");
                        return;
                }

canCommandSenderUseCommand(ICommandSender):

This method is used to check whether the player (or in the case of a CommandBlock, the block itself) can actually use the command. Return true if the command is usable by the sender, false otherwise.

addTabCompletionOptions(ICommandSender, String[]):

TODO: This needs to be filled in.

isUsernameIndex(String[], int):

This method returns whether the command String at the specified index (int) is supposed to represent the username of a Minecraft Player. The String array is the arguments to your command, and the command String you should be checking is String[int]. Return true if it's a username, false otherwise.

compareTo(Object):

TODO: This needs to be filled in.

Mod Implementation

For the sake of completeness, here is the mod file itself. There is nothing special here other than the handling of the FMLServerStartingEvent as described above.

package stargazer.minecraft.samples.servercommand;

import stargazer.minecraft.samples.common.CommonProxy;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.network.NetworkMod;

//
// This is a minimal (but complete) mod entry point.
//
@Mod(modid = SampleMod.ID, name = SampleMod.NAME, version = SampleMod.VERSION)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class SampleMod
{
  public final static String ID = "Sample";
  public final static String NAME = "Sample";
  public final static String VERSION = "1.0";
   
  // The instance of your mod that Forge uses.
  @Instance(ID)
  public static SampleMod  instance;

  // Says where the client and server 'proxy' code is loaded.
  @SidedProxy(clientSide = "stargazer.minecraft.samples.common.ClientProxy", serverSide = "stargazer.minecraft.samples.common.CommonProxy")
  public static CommonProxy proxy;
 
  //
  // Mod entry points
  //  
  @EventHandler
  public void load(FMLInitializationEvent event)
  {
    proxy.registerRenderers();
  }
 
  @EventHandler
  public void serverLoad(FMLServerStartingEvent event)
  {
    event.registerServerCommand(new SampleCommand());
  }
}
Personal tools
Namespaces
Variants
Actions
Navigation
tutorials
Toolbox