Created
October 20, 2012 23:08
-
-
Save Vazkii/3925155 to your computer and use it in GitHub Desktop.
Chat Spell Checker
This file contains 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 springcraft.spellchecker; | |
import java.net.MalformedURLException; | |
import net.minecraft.client.Minecraft; | |
import com.inet.jortho.SpellChecker; | |
import cpw.mods.fml.common.Mod; | |
import cpw.mods.fml.common.Mod.Init; | |
import cpw.mods.fml.common.Side; | |
import cpw.mods.fml.common.event.FMLInitializationEvent; | |
import cpw.mods.fml.common.registry.TickRegistry; | |
@Mod(modid = "sc_Spellchecker", version = "[1.0] for 1.3.2") | |
public class mod_Spellchecker { | |
@Init | |
public void init(FMLInitializationEvent event) { | |
try { | |
SpellChecker.registerDictionaries(Minecraft.getAppDir("minecraft/dict").toURI().toURL(), "en"); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} | |
TickRegistry.registerTickHandler(TickHandler.INSTANCE, Side.CLIENT); | |
} | |
} |
This file contains 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 springcraft.spellchecker; | |
import java.awt.Point; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Vector; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.src.FontRenderer; | |
import net.minecraft.src.GuiTextField; | |
import com.inet.jortho.SpellChecker; | |
public class SpellcheckingTextbox extends GuiTextField { | |
int x; | |
int y; | |
public SpellcheckingTextbox(FontRenderer par1FontRenderer, int par2, int par3, int par4, int par5) { | |
super(par1FontRenderer, par2, par3, par4, par5); | |
x = par2; | |
y = par3; | |
} | |
public void renderSpellchecking() { | |
FontRenderer renderer = Minecraft.getMinecraft().fontRenderer; | |
StringAndPoint[] snps = spellCheck(getText(), renderer, y); | |
for (StringAndPoint snp : snps) | |
renderer.drawStringWithShadow(snp.string, snp.point.x + x, snp.point.y, 0xFFFFFF); | |
} | |
@Override | |
public void drawTextBox() { | |
super.drawTextBox(); | |
renderSpellchecking(); | |
} | |
public StringAndPoint[] spellCheck(String str, FontRenderer renderer, int height) { | |
Vector<StringAndPoint> strings = new Vector(); | |
String str1 = str.toLowerCase(); | |
int i = 0; | |
int lastSpace = 0; | |
for (char c : str1.toCharArray()) { | |
if (c == ' ' || i == str1.length() - 1) { | |
if (i == str1.length() - 1) ++i; | |
String allBefore = str.substring(0, lastSpace); | |
strings.add(new StringAndPoint(str.substring(lastSpace, i), new Point(renderer.getStringWidth(allBefore), height))); | |
lastSpace = i + 1; | |
} | |
++i; | |
} | |
int j = 0; | |
for (StringAndPoint s : strings) { | |
String parsed = parseToBeRead(s.string); | |
if (parsed.length() > 1 && !SpellChecker.getCurrentDictionary().exist(parsed)) strings.set(j, new StringAndPoint("§c" + s.string, s.point)); | |
++j; | |
} | |
return strings.toArray(new StringAndPoint[strings.size()]); | |
} | |
String validValues = "qwertyuiopasdfghjklzxcvbnm-'"; | |
public String parseToBeRead(String s) { | |
List<Character> valid = new ArrayList(); | |
for (char c : validValues.toCharArray()) | |
valid.add(Character.valueOf(c)); | |
StringBuffer buffer = new StringBuffer(); | |
new Vector(); | |
for (char c : s.toCharArray()) | |
if (valid.contains(c)) buffer.append(Character.toLowerCase(c)); | |
return buffer.toString(); | |
} | |
public class StringAndPoint { | |
public final String string; | |
public final Point point; | |
public StringAndPoint(String string, Point point) { | |
this.string = string; | |
this.point = point; | |
} | |
} | |
} |
This file contains 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 springcraft.spellchecker; | |
import java.util.EnumSet; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.src.GuiChat; | |
import net.minecraft.src.GuiScreen; | |
import cpw.mods.fml.common.ITickHandler; | |
import cpw.mods.fml.common.TickType; | |
import cpw.mods.fml.relauncher.ReflectionHelper; | |
public class TickHandler implements ITickHandler { | |
public static final TickHandler INSTANCE = new TickHandler(); | |
volatile static boolean ticked = false; | |
private static final int TEXTBOX_INDEX = 7; | |
// Make consctructor not be visible. | |
private TickHandler() { | |
}; | |
@Override | |
public void tickStart(EnumSet<TickType> type, Object... tickData) { | |
} | |
@Override | |
public void tickEnd(EnumSet<TickType> type, Object... tickData) { | |
Minecraft mc = Minecraft.getMinecraft(); | |
GuiScreen screen = mc.currentScreen; | |
if (screen != null && screen instanceof GuiChat) { | |
if (!ticked) { | |
ticked = true; | |
GuiChat chatGui = (GuiChat) screen; | |
SpellcheckingTextbox box = new SpellcheckingTextbox(mc.fontRenderer, 4, chatGui.height - 12, chatGui.width - 4, 12); | |
box.setMaxStringLength(100); | |
box.setEnableBackgroundDrawing(false); | |
box.setFocused(true); | |
box.setText(""); | |
box.setCanLoseFocus(false); | |
ReflectionHelper.setPrivateValue(GuiChat.class, chatGui, box, TEXTBOX_INDEX); | |
} | |
} else ticked = false; | |
} | |
@Override | |
public EnumSet<TickType> ticks() { | |
return EnumSet.of(TickType.CLIENT); | |
} | |
@Override | |
public String getLabel() { | |
return "Spellchecker"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you email me bevanjecks@rocketmail.com