BloodMagic/src/main/java/WayofTime/bloodmagic/util/ChatUtil.java

271 lines
7.5 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.util;
2015-10-30 16:54:59 -07:00
import WayofTime.bloodmagic.network.BloodMagicPacketHandler;
import WayofTime.bloodmagic.util.helper.TextHelper;
2015-10-30 16:54:59 -07:00
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiNewChat;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
2015-10-30 16:54:59 -07:00
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
public class ChatUtil
{
2015-10-30 17:02:10 -07:00
private static final int DELETION_ID = 2525277;
2015-10-30 16:54:59 -07:00
private static int lastAdded;
private static void sendNoSpamMessages(ITextComponent[] messages)
{
2015-10-30 16:54:59 -07:00
GuiNewChat chat = Minecraft.getMinecraft().ingameGUI.getChatGUI();
for (int i = DELETION_ID + messages.length - 1; i <= lastAdded; i++)
{
2015-10-30 16:54:59 -07:00
chat.deleteChatLine(i);
}
for (int i = 0; i < messages.length; i++)
{
2015-10-30 16:54:59 -07:00
chat.printChatMessageWithOptionalDeletion(messages[i], DELETION_ID + i);
}
lastAdded = DELETION_ID + messages.length - 1;
}
/**
2016-04-05 16:16:17 -04:00
* Returns a standard {@link TextComponentString} for the given
* {@link String} .
*
* @param s
* The string to wrap.
*
* @return An {@link ITextComponent} containing the string.
2015-10-30 16:54:59 -07:00
*/
public static ITextComponent wrap(String s)
{
return new TextComponentString(s);
2015-10-30 16:54:59 -07:00
}
/**
* @see #wrap(String)
*/
public static ITextComponent[] wrap(String... s)
{
ITextComponent[] ret = new ITextComponent[s.length];
for (int i = 0; i < ret.length; i++)
{
2015-10-30 16:54:59 -07:00
ret[i] = wrap(s[i]);
}
return ret;
}
/**
* Returns a translatable chat component for the given string and format
* args.
*
* @param s
* The string to format
* @param args
* The args to apply to the format
2015-10-30 16:54:59 -07:00
*/
public static ITextComponent wrapFormatted(String s, Object... args)
{
return new TextComponentTranslation(s, args);
2015-10-30 16:54:59 -07:00
}
/**
* Simply sends the passed lines to the player in a chat message.
*
* @param player
* The player to send the chat to
* @param lines
* The lines to send
2015-10-30 16:54:59 -07:00
*/
public static void sendChat(EntityPlayer player, String... lines)
{
2015-10-30 16:54:59 -07:00
sendChat(player, wrap(lines));
}
/**
* Localizes the lines before sending them.
*
2015-10-30 16:54:59 -07:00
* @see #sendChat(EntityPlayer, String...)
*/
public static void sendChatUnloc(EntityPlayer player, String... unlocLines)
{
2015-10-30 16:54:59 -07:00
sendChat(player, TextHelper.localizeAll(unlocLines));
}
/**
* Sends all passed chat components to the player.
*
* @param player
* The player to send the chat lines to.
* @param lines
* The {@link ITextComponent chat components} to send.yes
2015-10-30 16:54:59 -07:00
*/
public static void sendChat(EntityPlayer player, ITextComponent... lines)
{
for (ITextComponent c : lines)
{
2015-10-30 16:54:59 -07:00
player.addChatComponentMessage(c);
}
}
/**
* Localizes the strings before sending them.
*
2015-10-30 16:54:59 -07:00
* @see #sendNoSpamClient(String...)
*/
public static void sendNoSpamClientUnloc(String... unlocLines)
{
2015-10-30 16:54:59 -07:00
sendNoSpamClient(TextHelper.localizeAll(unlocLines));
}
/**
* Same as {@link #sendNoSpamClient(ITextComponent...)}, but wraps the
* Strings automatically.
*
* @param lines
* The chat lines to send
*
2015-10-30 16:54:59 -07:00
* @see #wrap(String)
*/
public static void sendNoSpamClient(String... lines)
{
2015-10-30 16:54:59 -07:00
sendNoSpamClient(wrap(lines));
}
/**
* Skips the packet sending, unsafe to call on servers.
*
* @see #sendNoSpam(EntityPlayerMP, ITextComponent...)
2015-10-30 16:54:59 -07:00
*/
public static void sendNoSpamClient(ITextComponent... lines)
{
2015-10-30 16:54:59 -07:00
sendNoSpamMessages(lines);
}
/**
* Localizes the strings before sending them.
*
2015-10-30 16:54:59 -07:00
* @see #sendNoSpam(EntityPlayer, String...)
*/
public static void sendNoSpamUnloc(EntityPlayer player, String... unlocLines)
{
2015-10-30 16:54:59 -07:00
sendNoSpam(player, TextHelper.localizeAll(unlocLines));
}
/**
* @see #wrap(String)
* @see #sendNoSpam(EntityPlayer, ITextComponent...)
2015-10-30 16:54:59 -07:00
*/
public static void sendNoSpam(EntityPlayer player, String... lines)
{
2015-10-30 16:54:59 -07:00
sendNoSpam(player, wrap(lines));
}
/**
* First checks if the player is instanceof {@link EntityPlayerMP} before
* casting.
*
* @see #sendNoSpam(EntityPlayerMP, ITextComponent...)
2015-10-30 16:54:59 -07:00
*/
public static void sendNoSpam(EntityPlayer player, ITextComponent... lines)
{
if (player instanceof EntityPlayerMP)
{
2015-10-30 16:54:59 -07:00
sendNoSpam((EntityPlayerMP) player, lines);
}
}
/**
* Localizes the strings before sending them.
*
2015-10-30 16:54:59 -07:00
* @see #sendNoSpam(EntityPlayerMP, String...)
*/
public static void sendNoSpamUnloc(EntityPlayerMP player, String... unlocLines)
{
2015-10-30 16:54:59 -07:00
sendNoSpam(player, TextHelper.localizeAll(unlocLines));
}
/**
* @see #wrap(String)
* @see #sendNoSpam(EntityPlayerMP, ITextComponent...)
2015-10-30 16:54:59 -07:00
*/
public static void sendNoSpam(EntityPlayerMP player, String... lines)
{
2015-10-30 16:54:59 -07:00
sendNoSpam(player, wrap(lines));
}
/**
* Sends a chat message to the client, deleting past messages also sent via
* this method.
*
2015-10-30 16:54:59 -07:00
* Credit to RWTema for the idea
*
* @param player
* The player to send the chat message to
* @param lines
* The chat lines to send.
2015-10-30 16:54:59 -07:00
*/
public static void sendNoSpam(EntityPlayerMP player, ITextComponent... lines)
{
2015-10-30 16:54:59 -07:00
if (lines.length > 0)
2015-11-27 20:15:19 -05:00
BloodMagicPacketHandler.INSTANCE.sendTo(new PacketNoSpamChat(lines), player);
2015-10-30 16:54:59 -07:00
}
/**
* @author tterrag1098
*
* Ripped from EnderCore (and slightly altered)
*/
public static class PacketNoSpamChat implements IMessage
{
private ITextComponent[] chatLines;
public PacketNoSpamChat()
{
chatLines = new ITextComponent[0];
}
private PacketNoSpamChat(ITextComponent... lines)
{
// this is guaranteed to be >1 length by accessing methods
this.chatLines = lines;
}
@Override
public void toBytes(ByteBuf buf)
{
buf.writeInt(chatLines.length);
for (ITextComponent c : chatLines)
{
ByteBufUtils.writeUTF8String(buf, ITextComponent.Serializer.componentToJson(c));
}
}
@Override
public void fromBytes(ByteBuf buf)
{
chatLines = new ITextComponent[buf.readInt()];
for (int i = 0; i < chatLines.length; i++)
{
chatLines[i] = ITextComponent.Serializer.jsonToComponent(ByteBufUtils.readUTF8String(buf));
}
}
public static class Handler implements IMessageHandler<PacketNoSpamChat, IMessage>
{
@Override
public IMessage onMessage(PacketNoSpamChat message, MessageContext ctx)
{
sendNoSpamMessages(message.chatLines);
return null;
}
}
}
2015-10-30 16:54:59 -07:00
}