Jump to content

Problems with ChatCommands


chris140989

Recommended Posts

Hey there,

I have a problem with ChatCommands aka ServerCommands. First of all i tried creating one by myself with this guide (http://www.minecraftforge.net/wiki/Server_Command), but it seems outdated. I tried to find a solution by myself but couldn't find any working example on this.

 

I believe that my main problem is that I can't register the event since event.registerServerCommand(myCommand()); doesn't work or is not existence anymore.

 

Here is my sourcecode:

 

BaseMod:

package minecraftHamster;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

public class MinecraftHamster {
// Set the ID of the mod (Should be lower case).
public static final String MODID = "MinecraftHamster";
// Set the "Name" of the mod.
public static final String MODNAME = "MinecraftHamster";
// Set the version of the mod.
public static final String MODVER = "0.0.1";

// Tell Forge what instance to use.
@Instance(value = MinecraftHamster.MODID)
public static MinecraftHamster instance;

@EventHandler
public void preInit(FMLPreInitializationEvent event) {

}

@EventHandler
public void load(FMLInitializationEvent event) {
	event.registerServerCommand(new StartHamsterCommand());
}

@EventHandler
public void postInit(FMLPostInitializationEvent event) {

}
}

 

ChatCommandClass:

package minecraftHamster;

import java.util.List;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.IChatComponent;

public class StartHamsterCommand implements ICommand {
private String commandName = "startHamster";

@Override
public int compareTo(Object arg0) {
	// TODO Auto-generated method stub
	return 0;
}

@Override
public String getName() {
	return this.commandName;
}

@Override
public String getCommandUsage(ICommandSender sender) {
	return this.commandName;
}

@Override
public List getAliases() {
	// TODO Auto-generated method stub
	return null;
}

@Override
public void execute(ICommandSender sender, String[] input) throws CommandException {
	if (input.equals(this.commandName)) {
		sender.addChatMessage(new ChatComponentTranslation("Hamster wird gestartet.."));
	} else {
		sender.addChatMessage(new ChatComponentTranslation("This is not a command"));
		return;
	}

}

@Override
public boolean canCommandSenderUse(ICommandSender sender) {
	return true;
}

@Override
public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean isUsernameIndex(String[] args, int index) {
	// TODO Auto-generated method stub
	return false;
}

}

 

Any advice on this would be awesome..

 

Another question: Is there a documentation for Forge anywhere? Cause I have the feeling that kind of any tutorial is outdated and nearly all methods got changed..

Link to comment
Share on other sites

Oh my god.. you are right, didn't see that it is another EventType.

 

Changed it and now I can use event.registerServerCommand(new myCommand()); but when I try to use this command in Minecraft, it says that the command does not exist.

 

@Override
public String getCommandUsage(ICommandSender sender) {
	return "startHamster";
}

 

If I make the commandUsage like this, it should be possible to trigger the command with /startHamster in Minecraft right?

Link to comment
Share on other sites

I can't see the problem then..

 

Here are the current classes again.

 

BaseMod:

public class MinecraftHamster {
// Set the ID of the mod (Should be lower case).
public static final String MODID = "MinecraftHamster";
// Set the "Name" of the mod.
public static final String MODNAME = "MinecraftHamster";
// Set the version of the mod.
public static final String MODVER = "0.0.1";

// Tell Forge what instance to use.
@Instance(value = MinecraftHamster.MODID)
public static MinecraftHamster instance;

@EventHandler
public void preInit(FMLPreInitializationEvent event) {

}

@EventHandler
public void load(FMLServerStartingEvent event) {
	event.registerServerCommand(new StartHamsterCommand());
}

@EventHandler
public void postInit(FMLPostInitializationEvent event) {

}
}

 

StartHamsterCommand:

public class StartHamsterCommand implements ICommand {
private List aliases;

public StartHamsterCommand() {
	this.aliases = new ArrayList();
	this.aliases.add("startHamster");
}

@Override
public int compareTo(Object arg0) {
	// TODO Auto-generated method stub
	return 0;
}

@Override
public String getName() {
	return "startHamster";
}

@Override
public String getCommandUsage(ICommandSender sender) {
	return "startHamster";
}

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

@Override
public void execute(ICommandSender sender, String[] input) throws CommandException {
	sender.addChatMessage(new ChatComponentTranslation("Hamster wird gestartet.."));
}

@Override
public boolean canCommandSenderUse(ICommandSender sender) {
	return true;
}

@Override
public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean isUsernameIndex(String[] args, int index) {
	// TODO Auto-generated method stub
	return false;
}
}

Link to comment
Share on other sites

Alright, changed my class so it will extend CommandBase. The compareTo method i deleted and the aliases I won't need either so i deleted them aswell.

 

Found another mistake I made: I totally forgot about the @Mod annotation in the base Mod file, which obviously was the error why it didn't worked.

 

Thanks for your time and help :)

 

Thread can be closed.

Link to comment
Share on other sites

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.