I have a server command that I want to change a boolean value on the server and client. This boolean can be changed on the client with built in config GUI on the client, but I didn't know a better way to change it on the server than to use a command. When the command is excecuted, I want the command to send a message to the player using new TextComponentTranslation(). Currently, when I use the "TextComponentTranslation()," instead of returning what is found in the en_us.lang file, it just returns the literal string. I made sure the en_us.lang is lowercase and the pack.mcmeta file is set to 3.
Here is my source:
CommandIgnoreMobGriefing
package oresheepmod;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.play.server.SPacketEntityStatus;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
public class CommandIgnoreMobGriefing extends CommandBase
{
public List getCommandAliases()
{
List<String> aliases = new ArrayList();
aliases.add("ignoreRuleGriefing");
aliases.add("ignoreGameRuleGriefing");
aliases.add("ignoreMobGriefing");
return aliases;
}
/**
* Gets the name of the command
*/
public String getName()
{
return "ignoreGameRuleMobGriefing";
}
/**
* Return the required permission level for this command.
*/
public int getRequiredPermissionLevel()
{
return 2;
}
/**
* Gets the usage string for the command.
*/
public String getUsage(ICommandSender sender)
{
return "commands.oresheepmod.ignoreGameRuleMobGriefing.usage";
}
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
{
if (sender instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) sender;
if (args[0].length() <= 0 && !args[0].equals("true") && !args[0].equals("false"))
{
player.sendMessage(new TextComponentString("commands.oresheepmod.ignoreGameRuleMobGriefing.invalid"));
}
else
{
player.sendMessage(new TextComponentTranslation("commands.oresheepmod.ignoreGameRuleMobGriefing.valid", args[0]));
ModConfig.IgnoreGameRuleMobGriefing = Boolean.getBoolean(args[0]);
notifyGameRuleMobGriefingChange(args[0], server);
}
}
}
public static void notifyGameRuleMobGriefingChange(String p_184898_1_, MinecraftServer server)
{
ModConfig.IgnoreGameRuleMobGriefing = !ModConfig.IgnoreGameRuleMobGriefing;
for (EntityPlayerMP entityplayermp : server.getPlayerList().getPlayers())
{
//here, the server will send a packet to each player, updating them on the change in the boolean
}
}
/**
* Get a list of options for when the user presses the TAB key
*/
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos)
{
if (args.length == 1)
{
return getListOfStringsMatchingLastWord(args, new String[] {"true", "false"});
}
return Collections.<String>emptyList();
}
}
Mod Class file
// register server commands
@EventHandler
public void serverLoad(FMLServerStartingEvent event)
{
event.registerServerCommand(new CommandIgnoreMobGriefing());
}
en_us.lang file
commands.oresheepmod.ignoreGameRuleMobGriefing.invalid = Invalate Argument. Try true or false.
commands.oresheepmod.ignoreGameRuleMobGriefing.valid = Sheep ignore mobGriefing game rule: %s
commands.oresheepmod.ignoreGameRuleMobGriefing.usage = Ignores the mobGriefing game rule if set to true.
My questions are:
1. How can I update the config file (oresheepmod.cfg) on servers? (The client uses built in GUI and changes the value in game and writes it to file.
2. If the commands system is the best option, how can I update the config file and the value in the game for all users? (I could use a good example).
3. Why doesn't the TextComponentTranslation() read my en_us.lang file?
there must not be any spaces between the key and the translation and at the beginning of the translation, otherwise they will be considered part of the key.
changing your en_us.lang to this should work:
commands.oresheepmod.ignoreGameRuleMobGriefing.invalid=Invalid Argument. Try true or false.
commands.oresheepmod.ignoreGameRuleMobGriefing.valid=Sheep ignore mobGriefing game rule: %s
commands.oresheepmod.ignoreGameRuleMobGriefing.usage=Ignores the mobGriefing game rule if set to true.
Now to my first and second questions: does the game contain code to change a config file and the in game value?
Again, thanks a bunch!
at least for the client, it does. this example shows how you can easily create a config file that will also sync with the forge-provided mod config GUI.
I know it works for the client and I got that to work. Now I need a way to change the server's config file and update the server Boolean value. Then, I will update all of the client values. I wanted to change the server config file using commands, but in the below example (found also in my code above), neither the value in memory changes nor the config file.
It works now! For some reason "Boolean.getBoolean(args[0])" always returned false, even when args[0] was "true." Not sure why, but I replaced it with the below code and it worked:
if (args[0].equals("true"))
{
ModConfig.IgnoreGameRuleMobGriefing = true;
}
else if (args[0].equals("false"))
{
ModConfig.IgnoreGameRuleMobGriefing = false;
}
Also, not sure why, but I didn't have to send a packet to each client either, it works just fine without it. Yay!
It works now! For some reason "Boolean.getBoolean(args[0])" always returned false, even when args[0] was "true." Not sure why, but I replaced it with the below code and it worked:
if (args[0].equals("true"))
{
ModConfig.IgnoreGameRuleMobGriefing = true;
}
else if (args[0].equals("false"))
{
ModConfig.IgnoreGameRuleMobGriefing = false;
}
Also, not sure why, but I didn't have to send a packet to each client either, it works just fine without it. Yay!
well that's because you don't need to sync that information to the clients, as mob griefing is only handled on the server.
Hello all!
I have a server command that I want to change a boolean value on the server and client. This boolean can be changed on the client with built in config GUI on the client, but I didn't know a better way to change it on the server than to use a command. When the command is excecuted, I want the command to send a message to the player using new TextComponentTranslation(). Currently, when I use the "TextComponentTranslation()," instead of returning what is found in the en_us.lang file, it just returns the literal string. I made sure the en_us.lang is lowercase and the pack.mcmeta file is set to 3.
Here is my source:
CommandIgnoreMobGriefing
Mod Class file
en_us.lang file
My questions are:
1. How can I update the config file (oresheepmod.cfg) on servers? (The client uses built in GUI and changes the value in game and writes it to file.
2. If the commands system is the best option, how can I update the config file and the value in the game for all users? (I could use a good example).
3. Why doesn't the TextComponentTranslation() read my en_us.lang file?
Thanks so much for any and all help!
there must not be any spaces between the key and the translation and at the beginning of the translation, otherwise they will be considered part of the key.
changing your en_us.lang to this should work:
Wow, I can't believe it was that simple. The translation works now. Thanks so much!
Now to my first and second questions: does the game contain code to change a config file and the in game value?
Again, thanks a bunch!
np ^^
at least for the client, it does.
this example shows how you can easily create a config file that will also sync with the forge-provided mod config GUI.
I know it works for the client and I got that to work. Now I need a way to change the server's config file and update the server Boolean value. Then, I will update all of the client values. I wanted to change the server config file using commands, but in the below example (found also in my code above), neither the value in memory changes nor the config file.
ModConfigFile.booleanIWantToChange = Boolean.getBoolean(args[0]);
How should I do this?
Thanks!
It works now! For some reason "Boolean.getBoolean(args[0])" always returned false, even when args[0] was "true." Not sure why, but I replaced it with the below code and it worked:
Also, not sure why, but I didn't have to send a packet to each client either, it works just fine without it. Yay!
well that's because you don't need to sync that information to the clients, as mob griefing is only handled on the server.
That makes sense. Thanks!