Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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..

  • Author

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?

  • Author

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

  • Author

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.

I have some problems with that Tutorial also.

 

It does work, but for some reason it wont let me load already generated worlds. I just hangs.

I have some problems with that Tutorial also.

 

It does work, but for some reason it wont let me load already generated worlds. I just hangs.

- Don't hijack threads.

- Post your code.

 

What you talking about? I didn't hijack anything...

 

I just letting the Original Poster know that the Tutorial is working.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.