Created
May 14, 2014 14:16
-
-
Save Vazkii/a8b31637388a3b1ab235 to your computer and use it in GitHub Desktop.
Watmean - a small mod to tell you how stuff works.
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 vazkii.watmean; | |
import java.awt.Desktop; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.client.gui.GuiChat; | |
import net.minecraft.client.gui.GuiScreen; | |
import net.minecraft.client.gui.GuiTextField; | |
import net.minecraft.client.gui.ScaledResolution; | |
import net.minecraft.util.MathHelper; | |
import org.lwjgl.input.Mouse; | |
import org.lwjgl.opengl.GL11; | |
import cpw.mods.fml.common.FMLCommonHandler; | |
import cpw.mods.fml.common.Mod; | |
import cpw.mods.fml.common.Mod.EventHandler; | |
import cpw.mods.fml.common.event.FMLInitializationEvent; | |
import cpw.mods.fml.common.eventhandler.SubscribeEvent; | |
import cpw.mods.fml.common.gameevent.TickEvent.Phase; | |
import cpw.mods.fml.common.gameevent.TickEvent.RenderTickEvent; | |
import cpw.mods.fml.relauncher.ReflectionHelper; | |
@Mod(modid = "watmean", name = "watmean", version = "1.0") | |
public final class Watmean { | |
private static final String[] OBF_TEXT_FIELD = { | |
"inputField", "field_146415_a", "a" | |
}; | |
private static final String DEFAULT_PROPS = "# Watmean config, add websites here. Format: (name)>(url).\n" | |
+ "# Set %text% to be the text you put in.\n" | |
+ "# Do NOT include http:// in the URL, that is added automatically.\n" | |
+ "Wikipedia>wikipedia.org/wiki/%text%\n" | |
+ "Minecraft Wiki>minecraftwiki.net/%text%\n" | |
+ "Dictionary.com>dictionary.com/browse/%text%\n" | |
+ "Thesaurus>thesaurus.com/browse/%text%\n" | |
+ "LMGTFY>lmgtfy.com/?q=%text%\n" | |
+ "Urban Dictionary>urbandictionary.com/define.php?term=%text%\n" | |
+ "MCSkinSearch>mcskinsearch.com/skin/%text%"; | |
Map<String, String> websites = new LinkedHashMap(); | |
boolean leftDown = false; | |
boolean rightDown = false; | |
@EventHandler | |
public void init(FMLInitializationEvent event) { | |
try { | |
File file = new File(".", "config/watmean.properties"); | |
if(!file.exists()) { | |
file.createNewFile(); | |
BufferedWriter writer = new BufferedWriter(new FileWriter(file)); | |
writer.write(DEFAULT_PROPS); | |
writer.close(); | |
} | |
BufferedReader reader = new BufferedReader(new FileReader(file)); | |
String line; | |
while((line = reader.readLine()) != null) { | |
if(line.startsWith("#")) | |
continue; | |
line = line.trim(); | |
String[] tokens = line.split(">"); | |
if(tokens.length == 2) | |
websites.put(tokens[0], tokens[1]); | |
} | |
reader.close(); | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} | |
FMLCommonHandler.instance().bus().register(this); | |
} | |
@SubscribeEvent | |
public void renderTick(RenderTickEvent event) { | |
if(event.phase == Phase.END) { | |
Minecraft mc = Minecraft.getMinecraft(); | |
GuiScreen gui = mc.currentScreen; | |
boolean isChat = gui instanceof GuiChat; | |
if(isChat) { | |
ScaledResolution res = new ScaledResolution(mc.gameSettings, mc.displayWidth, mc.displayHeight); | |
int mouseX = Mouse.getX() * res.getScaledWidth() / mc.displayWidth; | |
int mouseY = res.getScaledHeight() - Mouse.getY() * res.getScaledHeight() / mc.displayHeight; | |
boolean leftDown = Mouse.isButtonDown(0); | |
boolean rightDown = Mouse.isButtonDown(1); | |
final int start = 300; | |
final float tweenPixels = 200F; | |
int i = 0; | |
GL11.glEnable(GL11.GL_BLEND); | |
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); | |
for(String website : websites.keySet()) { | |
int width = mc.fontRenderer.getStringWidth(website); | |
int x = gui.width - width - 5; | |
int y = gui.height - 30 - (websites.size() * 15) + i * 15; | |
int relPos = gui.width - mouseX; | |
if(relPos > start) | |
break; | |
boolean in = mouseX >= x && mouseX - x < width && mouseY >= y && mouseY - y < 10; | |
if(in) { | |
GuiTextField field = ReflectionHelper.getPrivateValue(GuiChat.class, (GuiChat) gui, OBF_TEXT_FIELD); | |
String text = getFormattedText(field, websites.get(website)); | |
if(!MathHelper.stringNullOrLengthZero(text)) { | |
if(!this.rightDown && rightDown) | |
field.setText(text); | |
else if(!this.leftDown && leftDown) | |
try { | |
Desktop.getDesktop().browse(new URI("http://" + text)); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
float alpha = Math.max(0.02F, Math.min(1F, (float) (start - relPos) / tweenPixels)); | |
mc.fontRenderer.drawStringWithShadow(website, x, y, (in ? 0x66FF66 : 0xFFFFFF) | ((int) (alpha * 0xFF)) << 24); | |
i++; | |
} | |
GL11.glDisable(GL11.GL_BLEND); | |
this.leftDown = leftDown; | |
this.rightDown = rightDown; | |
} | |
} | |
} | |
private String getFormattedText(GuiTextField field, String format) { | |
return format.replaceAll("%text%", field.getText().replaceAll(" ", "%20")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment