Page 1 of 1

Using emoticons in fCraft

Posted: September 10th, 2012, 4:02 pm
by Jonty800
This is a quick tutorial on how to get emoticons to work on fCraft

First things first, you need to allow bytes 1 - 31 in LineWrapper.cs (ignore byte 0, it is null)
In LineWrapper.cs, find bool IsWordChar and replace with

Code: Select all

static bool IsWordChar(byte ch)
        {
            return (ch > (byte)' ' && ch <= (byte)'~') || (ch >= (byte)1 && ch <= (byte)31);
        }
Now visit Chat.cs in the player folder. At the top of the file, add

Code: Select all

static System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Now add these functions. I will comment them so you know what is going on.

Code: Select all

#region Emotes
        public struct EmoteData //Contains all data needed for the emotes. Emote is the trigger word, 
            //example (bullet). ID is the byte value of the char
        {
            public string Emote;
            public byte ID;
        }

        //this list will be loaded with the list of emotes and their replacement values
        static List<EmoteData> EmoteTriggers = null;

        //a method to add all the emotes into EmoteTriggers. You can add your own triggers here
        static void AddEmotes()
        {
            EmoteTriggers = new List<EmoteData>();
                EmoteTriggers.Add(new EmoteData() { Emote = "(darksmile)", ID = 1 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(:))", ID = 1 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(smile)", ID = 2 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(:P)", ID = 2 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(heart)", ID = 3 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(<3)", ID = 3 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(diamond)", ID = 4 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(bullet)", ID = 7 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(hole)", ID = 8 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(male)", ID = 11 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(female)", ID = 12 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(sun)", ID = 15 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(right)", ID = 16 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(>)", ID = 16 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(left)", ID = 17 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(<)", ID = 17 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(double)", ID = 19 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(half)", ID = 22 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(uparrow)", ID = 24 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(^)", ID = 24 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(downarrow)", ID = 25 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(rightarrow)", ID = 26 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(up)", ID = 30 });
                EmoteTriggers.Add(new EmoteData() { Emote = "(down)", ID = 31 });
        }

        //this is the main method. This parses the emotes, correct glitches and makes the emotes safe to display
        public static string ParseEmotes(string rawMessage, bool SentencePart)
        {
            if (EmoteTriggers == null){ //until a message is made, the list is null, so fill it up here
                AddEmotes();
            }
            byte[] stored = new byte[1]; //this byte represents the emoticon, to be used in GetString(
                foreach(EmoteData ed in EmoteTriggers){ //scroll through the emotes, replacing each trigger word with the emote
                    string s = ed.Emote; //s is the emote trigger
                    stored[0] = (byte)ed.ID; //stored is the byte char for the emote
                    string s1 = enc.GetString(stored); //s1 is the emote
                    if (rawMessage.Contains(s)){
                        switch (ed.ID){ //this fixes glitches. Each ID appends a ' to resolve overlapping of letters
                            case 7:
                            case 12:
                            case 22:
                            case 24:
                            case 25:
                                s1 = s1 + "-";
                                break;
                            case 19:
                                s1 = s1 + "' ";
                                break;
                            default: break;
                        }
                        if (!SentencePart) //if sentence being parsed definately ends with no following text (not a displayedname, ect)
                        {
                            if (rawMessage.EndsWith(s))
                            {
                                s1 = s1 + "."; //append a period. A emoticon ONLY shows if some text follows the emoticon (excluding a blank space)
                            }
                        }
                        rawMessage = rawMessage.Replace(s, s1); //replace the triggerword with the emoticon
                    }
                }
            return rawMessage; //return the new sentence
        }

        #endregion
Now all you need to do is hook the main method onto your desired chat room. Example, if you want emoticons to work in global chat, then add

Code: Select all

rawMessage = ParseEmotes(rawMessage, false);
into void SendGlobal

Image

Re: Using emoticons in fCraft

Posted: September 11th, 2012, 8:16 pm
by boblol0909
Why do you need the lock when EmoteTriggers is never modified after you add the triggers?

Re: Using emoticons in fCraft

Posted: September 11th, 2012, 10:00 pm
by Jonty800
I don't actually know. I wrote it just in case someone was trying to read the list while another player was adding to it, even though the chance of that happening is an extreme rarity since the list is only modified once. I guess I'll remove the locks.

Re: Using emoticons in fCraft

Posted: September 15th, 2012, 2:13 am
by Jonty800
Emoticons are highly unstable.

When used in a server message, they kick everyone in the server who sees it and java throws an exception.

DO NOT USE THIS CODE.

I'll leave it here in case someone finds the safe conditions for them

Re: Using emoticons in fCraft

Posted: September 15th, 2012, 3:06 am
by Allie
I remember joining a server a while back that had proper emoticon support, and I jumped 20 feet in the air when I saw a heart in chat -- I wonder how they might've done it, and which software they were running. It would help if I remembered which server it was in the first place, I suppose.

Re: Using emoticons in fCraft

Posted: September 15th, 2012, 3:14 am
by Jonty800
They seem to work fine in the chat, but when its your displayedName and you kick someone, the entire server disconnects one by one.

Strange.

Edit: Also, (bullet) makes Hexchat / xchat ping O.o

Re: Using emoticons in fCraft

Posted: October 3rd, 2012, 7:57 pm
by fragmer
An official implementation of emotes in Minecraft is coming soon.

EDIT: And done.