Divination Sigil no longer spams chat

This commit is contained in:
Nick 2015-10-30 16:54:59 -07:00
parent 09ff96fe03
commit 2bf78bc61c
9 changed files with 324 additions and 27 deletions

View file

@ -1,6 +1,7 @@
package WayofTime.alchemicalWizardry; package WayofTime.alchemicalWizardry;
import WayofTime.alchemicalWizardry.api.util.helper.LogHelper; import WayofTime.alchemicalWizardry.api.util.helper.LogHelper;
import WayofTime.alchemicalWizardry.network.AlchemicalWizardryPacketHandler;
import WayofTime.alchemicalWizardry.registry.ModBlocks; import WayofTime.alchemicalWizardry.registry.ModBlocks;
import WayofTime.alchemicalWizardry.registry.ModEntities; import WayofTime.alchemicalWizardry.registry.ModEntities;
import WayofTime.alchemicalWizardry.registry.ModItems; import WayofTime.alchemicalWizardry.registry.ModItems;
@ -67,6 +68,8 @@ public class AlchemicalWizardry {
@Mod.EventHandler @Mod.EventHandler
public void init(FMLPreInitializationEvent event) { public void init(FMLPreInitializationEvent event) {
AlchemicalWizardryPacketHandler.init();
proxy.init(); proxy.init();
} }

View file

@ -1,14 +0,0 @@
package WayofTime.alchemicalWizardry.api.util.helper;
import net.minecraft.util.StatCollector;
public class TextHelper {
public static String getFormattedText(String string) {
return string.replaceAll("&", "\u00A7");
}
public static String localize(String key, Object ... format) {
return getFormattedText(StatCollector.translateToLocalFormatted(key, format));
}
}

View file

@ -6,7 +6,7 @@ import WayofTime.alchemicalWizardry.api.NBTHolder;
import WayofTime.alchemicalWizardry.api.iface.IBindable; import WayofTime.alchemicalWizardry.api.iface.IBindable;
import WayofTime.alchemicalWizardry.api.util.helper.BindableHelper; import WayofTime.alchemicalWizardry.api.util.helper.BindableHelper;
import WayofTime.alchemicalWizardry.api.util.helper.NetworkHelper; import WayofTime.alchemicalWizardry.api.util.helper.NetworkHelper;
import WayofTime.alchemicalWizardry.api.util.helper.TextHelper; import WayofTime.alchemicalWizardry.util.helper.TextHelper;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item; import net.minecraft.item.Item;

View file

@ -3,6 +3,7 @@ package WayofTime.alchemicalWizardry.item.sigil;
import WayofTime.alchemicalWizardry.AlchemicalWizardry; import WayofTime.alchemicalWizardry.AlchemicalWizardry;
import WayofTime.alchemicalWizardry.api.NBTHolder; import WayofTime.alchemicalWizardry.api.NBTHolder;
import WayofTime.alchemicalWizardry.api.iface.ISigil; import WayofTime.alchemicalWizardry.api.iface.ISigil;
import WayofTime.alchemicalWizardry.util.helper.TextHelper;
import WayofTime.alchemicalWizardry.item.ItemBindable; import WayofTime.alchemicalWizardry.item.ItemBindable;
import lombok.Getter; import lombok.Getter;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
@ -21,6 +22,7 @@ public class ItemSigilBase extends ItemBindable implements ISigil {
private final String name; private final String name;
private boolean toggleable; private boolean toggleable;
protected final String tooltipBase;
public ItemSigilBase(String name, int energyUsed) { public ItemSigilBase(String name, int energyUsed) {
super(); super();
@ -29,6 +31,7 @@ public class ItemSigilBase extends ItemBindable implements ISigil {
setEnergyUsed(energyUsed); setEnergyUsed(energyUsed);
this.name = name; this.name = name;
this.tooltipBase = "tooltip.AlchemicalWizardry.sigil." + name + ".";
} }
public ItemSigilBase(String name) { public ItemSigilBase(String name) {
@ -54,10 +57,8 @@ public class ItemSigilBase extends ItemBindable implements ISigil {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void addInformation(ItemStack stack, EntityPlayer player, List tooltip, boolean advanced) { public void addInformation(ItemStack stack, EntityPlayer player, List tooltip, boolean advanced) {
String desc = "tooltip.sigil." + name + ".desc"; if (StatCollector.canTranslate(tooltipBase + "desc"))
tooltip.add(TextHelper.localize(tooltipBase + "desc"));
if (StatCollector.canTranslate(desc))
tooltip.add(StatCollector.translateToLocal(desc));
super.addInformation(stack, player, tooltip, advanced); super.addInformation(stack, player, tooltip, advanced);
} }

View file

@ -3,7 +3,8 @@ package WayofTime.alchemicalWizardry.item.sigil;
import WayofTime.alchemicalWizardry.api.iface.IBloodAltar; import WayofTime.alchemicalWizardry.api.iface.IBloodAltar;
import WayofTime.alchemicalWizardry.api.util.helper.BindableHelper; import WayofTime.alchemicalWizardry.api.util.helper.BindableHelper;
import WayofTime.alchemicalWizardry.api.util.helper.NetworkHelper; import WayofTime.alchemicalWizardry.api.util.helper.NetworkHelper;
import WayofTime.alchemicalWizardry.api.util.helper.TextHelper; import WayofTime.alchemicalWizardry.util.ChatUtil;
import WayofTime.alchemicalWizardry.util.helper.TextHelper;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
@ -26,14 +27,14 @@ public class ItemSigilDivination extends ItemSigilBase {
int currentEssence = NetworkHelper.getCurrentEssence(BindableHelper.getOwnerName(stack)); int currentEssence = NetworkHelper.getCurrentEssence(BindableHelper.getOwnerName(stack));
if (position == null) { if (position == null) {
player.addChatComponentMessage(new ChatComponentText(TextHelper.localize("message.divinationsigil.currentessence", currentEssence))); ChatUtil.sendNoSpam(player, new ChatComponentText(TextHelper.localize(tooltipBase + "currentEssence", currentEssence)));
return stack; return stack;
} else { } else {
if (position.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) { if (position.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
TileEntity tile = world.getTileEntity(position.getBlockPos()); TileEntity tile = world.getTileEntity(position.getBlockPos());
if (!(tile instanceof IBloodAltar)) { if (!(tile instanceof IBloodAltar)) {
player.addChatComponentMessage(new ChatComponentText(TextHelper.localize("message.divinationsigil.currentessence", currentEssence))); ChatUtil.sendNoSpam(player, new ChatComponentText(TextHelper.localize(tooltipBase + "currentEssence", currentEssence)));
return stack; return stack;
} }
} }

View file

@ -0,0 +1,31 @@
package WayofTime.alchemicalWizardry.network;
import WayofTime.alchemicalWizardry.AlchemicalWizardry;
import WayofTime.alchemicalWizardry.util.ChatUtil;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;
public class AlchemicalWizardryPacketHandler {
public static final SimpleNetworkWrapper INSTANCE = new SimpleNetworkWrapper(AlchemicalWizardry.MODID);
public static void init() {
INSTANCE.registerMessage(ChatUtil.PacketNoSpamChat.Handler.class, ChatUtil.PacketNoSpamChat.class, 0, Side.CLIENT);
}
public static void sendToAllAround(IMessage message, TileEntity te, int range) {
INSTANCE.sendToAllAround(message, new NetworkRegistry.TargetPoint(te.getWorld().provider.getDimensionId(), te.getPos().getX(), te.getPos().getY(), te.getPos().getZ(), range));
}
public static void sendToAllAround(IMessage message, TileEntity te) {
sendToAllAround(message, te, 64);
}
public static void sendTo(IMessage message, EntityPlayerMP player) {
INSTANCE.sendTo(message, player);
}
}

View file

@ -0,0 +1,233 @@
package WayofTime.alchemicalWizardry.util;
import WayofTime.alchemicalWizardry.network.AlchemicalWizardryPacketHandler;
import WayofTime.alchemicalWizardry.util.helper.TextHelper;
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.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.IChatComponent;
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 {
public static class PacketNoSpamChat implements IMessage {
private IChatComponent[] chatLines;
public PacketNoSpamChat() {
chatLines = new IChatComponent[0];
}
private PacketNoSpamChat(IChatComponent... 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 (IChatComponent c : chatLines) {
ByteBufUtils.writeUTF8String(buf, IChatComponent.Serializer.componentToJson(c));
}
}
@Override
public void fromBytes(ByteBuf buf) {
chatLines = new IChatComponent[buf.readInt()];
for (int i = 0; i < chatLines.length; i++) {
chatLines[i] = IChatComponent.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;
}
}
}
private static final int DELETION_ID = 8675309;
private static int lastAdded;
private static void sendNoSpamMessages(IChatComponent[] messages) {
GuiNewChat chat = Minecraft.getMinecraft().ingameGUI.getChatGUI();
for (int i = DELETION_ID + messages.length - 1; i <= lastAdded; i++) {
chat.deleteChatLine(i);
}
for (int i = 0; i < messages.length; i++) {
chat.printChatMessageWithOptionalDeletion(messages[i], DELETION_ID + i);
}
lastAdded = DELETION_ID + messages.length - 1;
}
/**
* Returns a standard {@link ChatComponentText} for the given {@link String}.
*
* @param s
* The string to wrap.
* @return An {@link IChatComponent} containing the string.
*/
public static IChatComponent wrap(String s) {
return new ChatComponentText(s);
}
/**
* @see #wrap(String)
*/
public static IChatComponent[] wrap(String... s) {
IChatComponent[] ret = new IChatComponent[s.length];
for (int i = 0; i < ret.length; i++) {
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
*/
public static IChatComponent wrapFormatted(String s, Object... args) {
return new ChatComponentTranslation(s, args);
}
/**
* 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
*/
public static void sendChat(EntityPlayer player, String... lines) {
sendChat(player, wrap(lines));
}
/**
* Localizes the lines before sending them.
*
* @see #sendChat(EntityPlayer, String...)
*/
public static void sendChatUnloc(EntityPlayer player, String... unlocLines) {
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 IChatComponent chat components} to send.yes
*/
public static void sendChat(EntityPlayer player, IChatComponent... lines) {
for (IChatComponent c : lines) {
player.addChatComponentMessage(c);
}
}
/**
* Localizes the strings before sending them.
*
* @see #sendNoSpamClient(String...)
*/
public static void sendNoSpamClientUnloc(String... unlocLines) {
sendNoSpamClient(TextHelper.localizeAll(unlocLines));
}
/**
* Same as {@link #sendNoSpamClient(IChatComponent...)}, but wraps the Strings
* automatically.
*
* @param lines
* The chat lines to send
* @see #wrap(String)
*/
public static void sendNoSpamClient(String... lines) {
sendNoSpamClient(wrap(lines));
}
/**
* Skips the packet sending, unsafe to call on servers.
*
* @see #sendNoSpam(EntityPlayerMP, IChatComponent...)
*/
public static void sendNoSpamClient(IChatComponent... lines) {
sendNoSpamMessages(lines);
}
/**
* Localizes the strings before sending them.
*
* @see #sendNoSpam(EntityPlayer, String...)
*/
public static void sendNoSpamUnloc(EntityPlayer player, String... unlocLines) {
sendNoSpam(player, TextHelper.localizeAll(unlocLines));
}
/**
* @see #wrap(String)
* @see #sendNoSpam(EntityPlayer, IChatComponent...)
*/
public static void sendNoSpam(EntityPlayer player, String... lines) {
sendNoSpam(player, wrap(lines));
}
/**
* First checks if the player is instanceof {@link EntityPlayerMP} before
* casting.
*
* @see #sendNoSpam(EntityPlayerMP, IChatComponent...)
*/
public static void sendNoSpam(EntityPlayer player, IChatComponent... lines) {
if (player instanceof EntityPlayerMP) {
sendNoSpam((EntityPlayerMP) player, lines);
}
}
/**
* Localizes the strings before sending them.
*
* @see #sendNoSpam(EntityPlayerMP, String...)
*/
public static void sendNoSpamUnloc(EntityPlayerMP player, String... unlocLines) {
sendNoSpam(player, TextHelper.localizeAll(unlocLines));
}
/**
* @see #wrap(String)
* @see #sendNoSpam(EntityPlayerMP, IChatComponent...)
*/
public static void sendNoSpam(EntityPlayerMP player, String... lines) {
sendNoSpam(player, wrap(lines));
}
/**
* Sends a chat message to the client, deleting past messages also sent via
* this method.
* <p>
* Credit to RWTema for the idea
*
* @param player
* The player to send the chat message to
* @param lines
* The chat lines to send.
*/
public static void sendNoSpam(EntityPlayerMP player, IChatComponent... lines) {
if (lines.length > 0)
AlchemicalWizardryPacketHandler.INSTANCE.sendTo(new PacketNoSpamChat(lines), player);
}
}

View file

@ -0,0 +1,41 @@
package WayofTime.alchemicalWizardry.util.helper;
import com.google.common.collect.Lists;
import net.minecraft.util.StatCollector;
import scala.actors.threadpool.Arrays;
import java.util.List;
public class TextHelper {
public static String getFormattedText(String string) {
return string.replaceAll("&", "\u00A7");
}
public static String localize(String key, Object ... format) {
return getFormattedText(StatCollector.translateToLocalFormatted(key, format));
}
/**
* Localizes all strings in a list, using the prefix.
*
* @param unloc
* The list of unlocalized strings.
* @return A list of localized versions of the passed strings.
*/
public static List<String> localizeAll(List<String> unloc) {
List<String> ret = Lists.newArrayList();
for (String s : unloc)
ret.add(localize(s));
return ret;
}
public static String[] localizeAll(String... unloc) {
String[] ret = new String[unloc.length];
for (int i = 0; i < ret.length; i++)
ret[i] = localize(unloc[i]);
return ret;
}
}

View file

@ -62,8 +62,9 @@ tile.AlchemicalWizardry.fluid.lifeEssence.name=Life Essence
# Tooltips # Tooltips
tooltip.AlchemicalWizardry.orb.desc=Stores raw Life Essence tooltip.AlchemicalWizardry.orb.desc=Stores raw Life Essence
tooltip.AlchemicalWizardry.orb.owner=Added by: %s tooltip.AlchemicalWizardry.orb.owner=Added by: %s
tooltip.AlchemicalWizardry.currentOwner=&oOwner: %s tooltip.AlchemicalWizardry.currentOwner=Owner: %s
tooltip.sigil.air.desc=I feel lighter already... tooltip.AlchemicalWizardry.sigil.air.desc=&oI feel lighter already...
tooltip.sigil.bloodLight.desc=I see a light! tooltip.AlchemicalWizardry.sigil.bloodLight.desc=&oI see a light!
tooltip.sigil.compression.desc=Hands of Diamonds tooltip.AlchemicalWizardry.sigil.compression.desc=&oHands of Diamonds
tooltip.sigil.divination.desc=Peer into the soul tooltip.AlchemicalWizardry.sigil.divination.desc=&oPeer into the soul
tooltip.AlchemicalWizardry.sigil.divination.currentEssence=Current Essence: %d LP