Here I'll list some things that are very useful, but too short for a full tutorial. This list will be expanded in the future. Have fun!
The tips and tricks were created with Minecraft Forge 188.8.131.520 for Minecraft 1.8. If some of them don't work in other versions, please contact me.
If you want to add a chat message to a single player's chat, you can use the method player.addChatComponentMessage(IChatComponent msg).
This will display a message in the player's chat.
But how do you get an instance of the IChatComponent interface?
There are several useful implementations, but we're looking at ChatComponentText here. You can instantiate this class by calling
new ChatComponentText(String text). ChatComponentText is a subclass of ChatComponentStyle, thus it can make use of several useful features, like the ability to append siblings (other
chat components that will be printed behind the first object) or to change the style (using the setChatStyle(ChatStyle style) method). If you know how to create formatted messages using the
/tellraw command, you'll soon see that you can do exactly the same using the IChatComponent instances.
To translate a message, you can use two different methods.
The first one is the usage of the class ChatComponentTranslation. This is useful if you want to translate chat messages, because then you'll need an instance of IChatComponent anyway. You can
create a translation using the Constructor
new ChatComponentTranslation(String key, Object... values).
I'll give an example here: The translation of the key "chat.type.achievement" is "%s has just earned the achievement %s".
If I use the ChatComponentTranslation ("chat.type.achievement", "_Bedrock_Miner_", "Diamond King"), this will lead to the output: "_Bedrock_Miner_ has just earned the achievement Diamond King". However, you can not only add Strings as arguments, but any object. For normal objects, the .toString() method will be called to determine what to display. Instances of IChatComponent will be displayed as they would normally.
The second possibility of translation is a bit easier, here you don't get a ChatComponent but a String. Just call StatCollector.translateToLocalFormatted(String key, Object... values)
and the key you entered will be translated with the given values as parameters. There is also another method which does exactly the same, its called I18n.format(String key, Object...
values). Both are used interchangeably in Minecraft.
As an example here, I18n.format("item.diamond_pickaxe.name") will return "Diamond Pickaxe" (If your Minecraft is set to English, of course). Names that are not found will default to english
language, if they cannot be found there either they are returned as they are.
There are no mods without errors. So it is useful to protect critical passages with try/catch structures.
But what if something goes really wrong and there's no other way than quitting Minecraft? (This can happen, especially if you're reading files that are wrongly formatted). You can, of course, throw an exception and wait for Minecraft to crash, but the big downside of that is that the user cannot understand what has happened, because he probably doesn't know anything about Modding or even Java.
The alternative is making the crash reports on your own. This way, you can add information for the user what went wrong and what he can do to prevent the error from happening again.
To create a crash report, just call the Constructor new CrashReport(String message, Throwable cause).
The message is a short information what happened. The cause is the exception that has been thrown.
You can add your own information by adding a crash report category first. Therefore call CrashReportCategory cat = crashreport.makeCategory(String title);
You can now add information to the category by calling cat.addCrashSection(String title, Object data);
Here you can tell the user what he has to do now and maybe include other important information.
The only downside is that no user ever bothers reading crash reports, therefore I designed a class named UserFriendlyCrashReport where you can provide a detailed description on what to do next with the method setUserInformation(String info).
This text is highlighted so that the user recognizes it immediately and hopefully does what it says there.
You can find the class UserFriendlyCrashReport in my Miner's Basic Library in the package minersbasic.api.crash.
Now we've created a nice Crash Report, so it's time to terminate Minecraft. You can do this by throwing the following exception:
throw new ReportedException(crashreport);