Created
May 30, 2016 09:01
-
-
Save lucko/46976180a017bdc45399efa685a4cd71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.lucko.chatinject; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.command.CommandBase; | |
import net.minecraft.command.CommandException; | |
import net.minecraft.command.ICommandSender; | |
import net.minecraft.util.ChatComponentText; | |
import net.minecraftforge.client.ClientCommandHandler; | |
import net.minecraftforge.fml.common.Mod; | |
import net.minecraftforge.fml.common.Mod.EventHandler; | |
import net.minecraftforge.fml.common.event.FMLInitializationEvent; | |
import org.apache.commons.lang3.StringUtils; | |
@Mod(modid = ChatInject.MODID, name = ChatInject.MODNAME, version = ChatInject.VERSION) | |
public class ChatInject { | |
public static final String MODID = "chatinject"; | |
public static final String MODNAME = "ChatInject"; | |
public static final String VERSION = "1.0"; | |
@EventHandler | |
public void init(FMLInitializationEvent event) { | |
ClientCommandHandler.instance.registerCommand(new CommandBase() { | |
@Override | |
public String getCommandName() { | |
return "inject"; | |
} | |
@Override | |
public String getCommandUsage(ICommandSender sender) { | |
return "/inject <message>"; | |
} | |
@Override | |
public void processCommand(ICommandSender sender, String[] args) throws CommandException { | |
if (args.length != 0) { | |
final String s = StringUtils.join(args, " "); | |
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(translateColorCodes('&', s))); | |
} else { | |
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("Usage: /inject <message>")); | |
} | |
} | |
@Override | |
public int getRequiredPermissionLevel() { | |
return 0; | |
} | |
private String translateColorCodes(char altColorChar, String textToTranslate) { | |
char[] b = textToTranslate.toCharArray(); | |
for(int i = 0; i < b.length - 1; ++i) { | |
if(b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i + 1]) > -1) { | |
b[i] = 167; | |
b[i + 1] = Character.toLowerCase(b[i + 1]); | |
} | |
} | |
return new String(b); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment