Jump to content

[1.8] Help creating Custom Commands


CoalOres

Recommended Posts

So recently my friend got me into modding, and I've been trying to implement a basic command that returns the message "Hello" in chat when you do /coal. This is simply as a test so I can gain an understanding of how everything works.

 

This is my main class:

public class TutorialMain
{
    // The instance of your mod that Forge uses, in my case tutorial.
    @Instance("testmod")
    public static TutorialMain instance;
    
    @EventHandler
    public void serverLoad(FMLServerStartingEvent event)
    {
        // register server commands

    event.registerServerCommand(new SampleCommand());
    }

    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        // Stub Method
    }

    @EventHandler
    public void init(FMLInitializationEvent event)
    {

    }

    @EventHandler
    public void postInit(FMLPostInitializationEvent event)
    {
        // Stub Method
    }
    

 

It is named TutorialMain because I was following the advice of a tutorial.

 

Here is the Sample Command class:

 

package com.testmod;

import java.util.ArrayList;
import java.util.List;
import eu.copybara.barra.util.*;

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

public class SampleCommand implements ICommand {

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

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

@Override
public boolean canCommandSenderUse(ICommandSender arg0) {
	// TODO Auto-generated method stub
	return true;
}

@Override
public void execute(ICommandSender arg0, String[] arg1) throws CommandException {
	// TODO Auto-generated method stub
	new Messages("Hello").send();

}

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

@Override
public String getCommandUsage(ICommandSender arg0) {
	// TODO Auto-generated method stub
	return "coal";
}

@Override
public String getName() {
	// TODO Auto-generated method stub
	return "coal";
}

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

 

Note: The Copybara.util is a package my friend sent me for sending chat messages, which I have tested and it worked before.

 

However when I run the mod and try to do /coal it informs me that there is no command like that.

 

 

I'm probably being really stupid and missing something but you must understand I have been following tutorials several years old.

 

Any help would be appreciated, thanks in advance!

 

Link to comment
Share on other sites

  • Replies 50
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

- For a start, do not implement ICommand. Extend CommandBase instead and override only the methods you need.

- Show the Messages class please.

- Why are you still on 1.8? Update.

 

1. I'm not entirely sure how the whole override thing works, could I have more detail? I'm assuming it is as simple as this:

package com.testmod;

import java.util.ArrayList;
import java.util.List;
import eu.copybara.barra.util.*;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.BlockPos;

public class SampleCommand extends CommandBase {

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


public List addTabCompletionOptions(ICommandSender arg0, String[] arg1, BlockPos arg2) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean canCommandSenderUse(ICommandSender arg0) {
	// TODO Auto-generated method stub
	return true;
}

@Override
public void execute(ICommandSender arg0, String[] arg1) throws CommandException {
	// TODO Auto-generated method stub
	new Messages("Hello").send();

}


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

@Override
public String getCommandUsage(ICommandSender arg0) {
	// TODO Auto-generated method stub
	return "coal";
}

@Override
public String getName() {
	// TODO Auto-generated method stub
	return "coal";
}


public boolean isUsernameIndex(String[] arg0, int arg1) {
	// TODO Auto-generated method stub
	return false;
}}

 

2. Messages class:

 

package eu.copybara.barra.util;

import net.minecraft.event.ClickEvent;

import net.minecraft.event.HoverEvent;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.fml.client.FMLClientHandler;

import java.util.ArrayList;

public class Messages {
private static IChatComponent CHAT_PREFIX;
public static String SEPARATION_MESSAGE = "\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC\u25AC";
private IChatComponent chatComponent;
private ArrayList<Messages> appendedMessages;

public Messages(String text) {
  this.chatComponent = new ChatComponentText(text);
}

public Messages(String text, EnumChatFormatting color) {
  this(text);
  this.addFormatting(color);
}

public Messages addFormatting(EnumChatFormatting formatting) {
  ChatStyle style = this.chatComponent.getChatStyle();
  switch (formatting) {
  case ITALIC:
   style.setItalic(true);
   break;
  case BOLD:
   style.setBold(true);
   break;
  case UNDERLINE:
   style.setUnderlined(true);
   break;
  case OBFUSCATED:
   style.setObfuscated(true);
   break;
  case STRIKETHROUGH:
   style.setStrikethrough(true);
   break;
  default:
   style.setColor(formatting);
   break;
  }
  this.chatComponent.setChatStyle(style);

  return this;
}

public Messages appendMessage(Messages message) {
  if (this.appendedMessages == null) {
   this.appendedMessages = new ArrayList<Messages>();
  }
  this.appendedMessages.add(message);
  if (message.appendedMessages != null) {
   this.appendedMessages.addAll(message.appendedMessages);
  }

  return this;
}

public Messages makeClickable(ClickEvent.Action action, String execute, Messages description) {
  ChatStyle style = this.chatComponent.getChatStyle();

  style.setChatClickEvent(new ClickEvent(action, execute));
  style.setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, description.assembleMessage(false)));

  this.chatComponent.setChatStyle(style);

  return this;
}

public Messages makeLink(String url) {
  Messages description = new Messages("Click to visit ", EnumChatFormatting.GRAY);
  description
    .appendMessage(new Messages(url, EnumChatFormatting.AQUA).addFormatting(EnumChatFormatting.UNDERLINE));
  this.makeClickable(ClickEvent.Action.OPEN_URL, url, description);

  return this;
}

public Messages makeHover(Messages text) {
  ChatStyle style = this.chatComponent.getChatStyle();

  style.setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, text.assembleMessage(false)));

  this.chatComponent.setChatStyle(style);

  return this;
}

public void send() {
  this.send(false);
}

public void send(boolean prefix) {
  try {
   FMLClientHandler.instance().getClientPlayerEntity().addChatMessage(this.assembleMessage(prefix));
  } catch (Exception e) {
   e.printStackTrace();
  }
}

protected IChatComponent assembleMessage(boolean prefix) {
  IChatComponent result;
  if (prefix) {
   result = CHAT_PREFIX.createCopy();
  } else if (this.appendedMessages == null) {
   return this.chatComponent;
  } else {
   result = new ChatComponentText("");
  }

  result.appendSibling(this.chatComponent);
  if (this.appendedMessages != null) {
   for (Messages m : this.appendedMessages) {
    result.appendSibling(m.chatComponent);
   }
  }

  return result;
}

public static void printSeparationMessage(EnumChatFormatting color) {
  new Messages(SEPARATION_MESSAGE, color).addFormatting(EnumChatFormatting.BOLD).send(false);
}
}

 

3. I'm more familiar with 1.8 and my goal is to create a special mod which is based around Hypixel, which supports 1.8. 1.9 and 1.10 is new territory for me.

 

Link to comment
Share on other sites

@Override

 

take a monster for example it extends EntityMob

 

I really hope you know what "extends" means

 

anyway. In EntityMob, or rather down the line at Entity, there is a method called onUpdate()

 

Lets say we want to still use onUpdate() but make it do something else

 

you do

 

@Override
public void onUpdate(){

//do stuff

}

 

in this way it overrides to parent method and substitutes whatever you want.

 

Now if you want the parent method to still run and your own stuff to run as well you add the super in

 

@Override
public void onUpdate(){
super.onUpdate();
//do stuff

}

 

I probably butchered that explanation, and may get scolded for not telling you to "Go learn java". But I am bored at work.

 

That actually made sense, thank you!

 

The

send

method of the Messages class is absolutely broken. It will crash on servers and will not work reliably in single player.

 

Is there a better one you recommend?

Link to comment
Share on other sites

ICommandSender::addChatMessage

(every entity is an

ICommandSender

).

So this is my refined code:

 

package com.testmod;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;

public class SampleCommand extends CommandBase {

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


public List addTabCompletionOptions(ICommandSender icommandsender, String[] arg1, BlockPos arg2) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean canCommandSenderUse(ICommandSender icommandsender) {
	// TODO Auto-generated method stub
	return true;
}

@Override
public void execute(ICommandSender icommandsender, String[] arg1) throws CommandException {
	// TODO Auto-generated method stub
	icommandsender.addChatMessage(new ChatComponentText("Hello"));

}


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

@Override
public String getCommandUsage(ICommandSender icommandsender) {
	// TODO Auto-generated method stub
	return "coal";
}

@Override
public String getName() {
	// TODO Auto-generated method stub
	return "coal";
}

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

 

Yet still nothing happens when I do /coal.

 

I apologise if I'm missing something obvious and am sounding like an idiot.

 

PS: I'm also crashing when genning a new world and attempting to join it. Minecraft just freezes and I have to close it.

 

 

Link to comment
Share on other sites

You are still overriding all those methods. Why?

 

How exactly are you running the mod?

You told me to override all the methods I need, so I thought I needed to remove the "@Override" for all the methods that returned null. And I'm running it through eclipse with the run function, logged into my Minecraft account.

 

Edit: Just removed the overrides, still nothing.

Link to comment
Share on other sites

Post updated code.

Also the @Override annotation is not what overrides a method. Overriding a method means creating a method with the same name and parameter types as a method in a superclass. @Override is just marker that produces an error if you are not overriding.

 

How exactly are you running the mod?

package com.testmod;

import java.util.ArrayList;
import java.util.List;
import eu.copybara.barra.util.*;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;

public class SampleCommand extends CommandBase {


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


public List addTabCompletionOptions(ICommandSender icommandsender, String[] arg1, BlockPos arg2) {
	// TODO Auto-generated method stub
	return null;
}


public boolean canCommandSenderUse(ICommandSender icommandsender) {
	// TODO Auto-generated method stub
	return true;
}


public void execute(ICommandSender icommandsender, String[] arg1) throws CommandException {
	// TODO Auto-generated method stub
	icommandsender.addChatMessage(new ChatComponentText("Hello"));

}


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


public String getCommandUsage(ICommandSender icommandsender) {
	// TODO Auto-generated method stub
	return "coal";
}


public String getName() {
	// TODO Auto-generated method stub
	return "coal";
}


public boolean isUsernameIndex(String[] arg0, int arg1) {
	// TODO Auto-generated method stub
	return false;
}}

 

Main:

package com.testmod;

import com.testmod.SampleCommand;

import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.fml.common.Mod;
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;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;

@Mod(modid="testmod", name="Test Mod", version="1.0")

public class TutorialMain
{
    // The instance of your mod that Forge uses, in my case tutorial.
    @Instance("testmod")
    public static TutorialMain instance;
    
    @EventHandler
    public void serverLoad(FMLServerStartingEvent event)
    {
        // register server commands

    event.registerServerCommand(new SampleCommand());
    }

    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        // Stub Method
    }

    @EventHandler
    public void init(FMLInitializationEvent event)
    {

    }

    @EventHandler
    public void postInit(FMLPostInitializationEvent event)
    {
        // Stub Method
    }
    

   

}[code]


As I said, I click the "Run" button on eclipse, it opens Minecraft, I go onto a server (have to because single player crashes it for some reason) and do /coal.

 

Note: When it crashes on singleplayer it returns this on the console:

The state engine was in incorrect state ERRORED and forced into state SERVER_STOPPED. Errors may have been discarded.

The state engine was in incorrect state ERRORED and forced into state AVAILABLE. Errors may have been discarded.

Link to comment
Share on other sites

Again:

You are still overriding all those methods. Why?

 

As I said, I click the "Run" button on eclipse, it opens Minecraft, I go onto a server
What kind of server? Does it have your mod installed? If so: How?

 

have to because single player crashes it for some reason
Post the full crash.

 

1. I really don't understand what I'm overriding, how I'm doing it and what effect it is having, this is one of my first times modding.

 

2. It's Hypixel, so of course it doesn't have my mod installed.

 

3. Crash:

Caught exception from testmod

java.lang.NullPointerException

at net.minecraft.command.CommandHandler.registerCommand(CommandHandler.java:138) ~[forgeBin-1.8-11.14.1.1334.jar:?]

at net.minecraftforge.fml.common.event.FMLServerStartingEvent.registerServerCommand(FMLServerStartingEvent.java:44) ~[forgeBin-1.8-11.14.1.1334.jar:?]

at com.testmod.TutorialMain.serverLoad(TutorialMain.java:27) ~[bin/:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]

at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]

at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:518) ~[forgeBin-1.8-11.14.1.1334.jar:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]

at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]

at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) ~[guava-17.0.jar:?]

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.post(EventBus.java:275) ~[guava-17.0.jar:?]

at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:208) ~[forgeBin-1.8-11.14.1.1334.jar:?]

at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:187) ~[forgeBin-1.8-11.14.1.1334.jar:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]

at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]

at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) ~[guava-17.0.jar:?]

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.post(EventBus.java:275) ~[guava-17.0.jar:?]

at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:118) [LoadController.class:?]

at net.minecraftforge.fml.common.Loader.serverStarting(Loader.java:738) [Loader.class:?]

at net.minecraftforge.fml.common.FMLCommonHandler.handleServerStarting(FMLCommonHandler.java:319) [FMLCommonHandler.class:?]

at net.minecraft.server.integrated.IntegratedServer.startServer(IntegratedServer.java:128) [integratedServer.class:?]

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:438) [MinecraftServer.class:?]

at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]

[21:00:10] [server thread/ERROR]: A fatal exception occurred during the server starting event

java.lang.NullPointerException

at net.minecraft.command.CommandHandler.registerCommand(CommandHandler.java:138) ~[forgeBin-1.8-11.14.1.1334.jar:?]

at net.minecraftforge.fml.common.event.FMLServerStartingEvent.registerServerCommand(FMLServerStartingEvent.java:44) ~[forgeBin-1.8-11.14.1.1334.jar:?]

at com.testmod.TutorialMain.serverLoad(TutorialMain.java:27) ~[bin/:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]

at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]

at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:518) ~[forgeBin-1.8-11.14.1.1334.jar:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]

at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]

at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) ~[guava-17.0.jar:?]

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.post(EventBus.java:275) ~[guava-17.0.jar:?]

at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:208) ~[LoadController.class:?]

at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:187) ~[LoadController.class:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]

at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]

at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) ~[guava-17.0.jar:?]

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.post(EventBus.java:275) ~[guava-17.0.jar:?]

at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:118) ~[LoadController.class:?]

at net.minecraftforge.fml.common.Loader.serverStarting(Loader.java:738) [Loader.class:?]

at net.minecraftforge.fml.common.FMLCommonHandler.handleServerStarting(FMLCommonHandler.java:319) [FMLCommonHandler.class:?]

at net.minecraft.server.integrated.IntegratedServer.startServer(IntegratedServer.java:128) [integratedServer.class:?]

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:438) [MinecraftServer.class:?]

at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]

[21:00:10] [server thread/INFO]: Applying holder lookups

Link to comment
Share on other sites

You need to learn Java then. Before modding.

Like I said, overriding a method means having a method with the same name and signature (i.e. parameter types) as a superclass. As an example the method

addTabCompletionOptions

from your command class overrides the

addTabCompletionOptions

method in CommandBase, because it has the same name and signature. Like I also already said,

@Override

has nothing to do with whether you override or not. @Override simply produces an error if you are not overriding.

 

 

It's Hypixel, so of course it doesn't have my mod installed.

How do you expect a server-side command that you added to work on a server that is not yours?

 

3. Crash:

[snip]

This crash happens because you are still overriding all those methods to perform nonsense instead of their actual intended function.

 

 

1. So I need to rename these things to something else? Anything of my choosing? And how am I supposed to change the parameters? Won't that screw stuff up?

 

 

2. I'm attempting to make a client side mod, eventually for convenience, for example a /afk command which will /reply to anybody who messages you after you have done /afk.

 

 

 

 

Link to comment
Share on other sites

You are trying  to create a mod that needs to have access to the server....

That would have been nice to know before hand. How does one create a client side mod then? Because all of the tutorials I have seen followed this exact procedure.

Link to comment
Share on other sites

What are you trying to do with /AFK?

The syntax would be:

/afk <reason>

 

And whenever somebody messaged you (could detect this by the pink "From" in chat) it would send "/r I am currently afk" + reason to the chat.

 

This was a simple test to see if I could have it print something to the chat upon a command.

Link to comment
Share on other sites

Sending chat messages is handled server side on servers, because it is the only way for other players to the the message though you could probably have your command run the other command to possibly fix that.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Sending chat messages is handled server side on servers, because it is the only way for other players to the the message though you could probably have your command run the other command to possibly fix that.

That's what I mean, I want it to send THROUGH MY PLAYER "/r" which is the /reply command.

Link to comment
Share on other sites

So if I understand correctly what you want to do is...

1. Run /AFK command

2. If the player that ran the command is AFK and receives a whisper/msg then send a reply back saying This player is AFK

Am I right?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I don't think you can detect if the player has received a chat message but if you could it would be down with an Event provided by forge.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I don't think you can detect if the player has received a chat message but if you could it would be down with an Event provided by forge.

This feature exists in other mods and works, I'm fairly sure that all you do is detect a "From" string in the chat, messages appear like this:

"From Player117: Want to play a game?" and the "From" part is pink, so you could detect it I think.

Link to comment
Share on other sites

So I need to rename these things to something else?
No, you should remove those methods. Why are they there? Don't just have stuff in your code that you do not understand.

 

I'm attempting to make a client side mod, eventually for convenience, for example a /afk command which will /reply to anybody who messages you after you have done /afk.

First of all, you will need a client-side command, not a server command like you are registering currently. Client commands are registered using
ClientCommandHandler

.

To detect the receiving string you need to use

ClientChatReceivedEvent

.

 

Alright, so this is my new code:

 

Main:

package com.testmod;

import com.testmod.SampleCommand;

import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.fml.common.Mod;
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;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;

@Mod(modid="testmod", name="Test Mod", version="1.0")

public class TutorialMain
{
    // The instance of your mod that Forge uses, in my case tutorial.
    @Instance("testmod")
    public static TutorialMain instance;
    
    @EventHandler
    public void clientLoad(ClientCommandHandler clientcommandhandler)
    {
        // register server commands

    clientcommandhandler.instance.registerCommand(new SampleCommand());
    }

    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        // Stub Method
    }

    @EventHandler
    public void init(FMLInitializationEvent event)
    {

    }

    @EventHandler
    public void postInit(FMLPostInitializationEvent event)
    {
        // Stub Method
    }
    

   

}

 

SampleCommand:

package com.testmod;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;

public class SampleCommand extends CommandBase {

@Override
public int compareTo(Object o) {
	// TODO Auto-generated method stub
	return 0;
}
@Override
public boolean canCommandSenderUse(ICommandSender icommandsender) {
	// TODO Auto-generated method stub
	return true;
}
@Override
public void execute(ICommandSender icommandsender, String[] arg1) throws CommandException {
	// TODO Auto-generated method stub
	icommandsender.addChatMessage(new ChatComponentText("Hello"));

}
@Override
public String getCommandUsage(ICommandSender icommandsender) {
	// TODO Auto-generated method stub
	return "coal";
}

@Override
public String getName() {
	// TODO Auto-generated method stub
	return "coal";
}
@Override
public boolean isUsernameIndex(String[] arg0, int arg1) {
	// TODO Auto-generated method stub
	return false;
}}

 

 

I removed all the methods that returned null, and used a syntax I found online for ClientCommandHandler.

 

 

 

Edit: I added the register command to the init and it worked!

Link to comment
Share on other sites

One more issue, bit more advanced now:

 

 

Main:

package com.testmod;

import com.testmod.SampleCommand;

import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod;
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;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;

@Mod(modid="testmod", name="Test Mod", version="1.0")

public class TutorialMain
{

    // The instance of your mod that Forge uses, in my case tutorial.
    @Instance("testmod")
    public static TutorialMain instance;
    

    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        // Stub Method
    	MinecraftForge.EVENT_BUS.register(new ChatListener());
    	
    }

    @EventHandler
    public void init(FMLInitializationEvent event)
    {
    	ClientCommandHandler.instance.registerCommand(new SampleCommand());
    	
    	

    }

    @EventHandler
    public void postInit(FMLPostInitializationEvent event)
    {
        // Stub Method
    }
    

   

}

 

ChatListener:

package com.testmod;

import net.minecraft.client.Minecraft;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class ChatListener {
public static boolean isAFK = false;

@SubscribeEvent
public void onChat(ClientChatReceivedEvent event) {
	String message = event.message.getUnformattedText();
	if (message.contains("From") && isAFK) {
		Minecraft.getMinecraft().thePlayer.sendChatMessage("/r I am currently AFK. Reason: ");
	}




	}
}



 

SampleCommand:

package com.testmod;

import java.util.ArrayList;
import java.util.List;
import eu.copybara.barra.util.*;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;

public class SampleCommand extends CommandBase {


@Override
public int compareTo(Object o) {
	// TODO Auto-generated method stub
	return 0;
}
@Override
public boolean canCommandSenderUse(ICommandSender icommandsender) {
	// TODO Auto-generated method stub
	return true;
}
@Override
public void execute(ICommandSender icommandsender, String[] arg1) throws CommandException {
	// TODO Auto-generated method stub
	icommandsender.addChatMessage(new ChatComponentText("Hello"));
	if (ChatListener.isAFK = false) {
	ChatListener.isAFK = true;
	icommandsender.addChatMessage(new ChatComponentText("You are now AFK!"));
	}
	else if (ChatListener.isAFK = true) {
		ChatListener.isAFK = false;
		icommandsender.addChatMessage(new ChatComponentText("You are no longer AFK!"));
		}

}
@Override
public String getCommandUsage(ICommandSender icommandsender) {
	// TODO Auto-generated method stub
	return "afk <reason>";
}

@Override
public String getName() {
	// TODO Auto-generated method stub
	return "afk";
}
@Override
public boolean isUsernameIndex(String[] arg0, int arg1) {
	// TODO Auto-generated method stub
	return false;
}}

 

 

When I do /afk it returns "You are no longer AFK!" constantly, never does the /r and also never says the "You are now AFK!".

Link to comment
Share on other sites

if (false) return;

There are two possible places for this, which are you referring to?

Here

                if (ChatListener.isAFK = false) {
	    ChatListener.isAFK = true;
	    icommandsender.addChatMessage(new ChatComponentText("You are now AFK!"));
	    return;
                }

If this doesn't work it is not being called.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements




  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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