Created
October 13, 2013 13:43
-
-
Save NeatMonster/bfcb0c0cb171c8c8b74e to your computer and use it in GitHub Desktop.
Greeting v1.0 - http://www.bukkit.fr/?showtopic=6554
This file contains hidden or 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 fr.neatmonster.greeting; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.client.gui.FontRenderer; | |
import net.minecraft.client.gui.ScaledResolution; | |
import net.minecraft.util.StringUtils; | |
import net.minecraftforge.client.event.ClientChatReceivedEvent; | |
import net.minecraftforge.client.event.RenderGameOverlayEvent; | |
import net.minecraftforge.common.MinecraftForge; | |
import net.minecraftforge.event.EventPriority; | |
import net.minecraftforge.event.ForgeSubscribe; | |
import org.lwjgl.opengl.GL11; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParser; | |
import cpw.mods.fml.common.Mod; | |
import cpw.mods.fml.common.Mod.EventHandler; | |
import cpw.mods.fml.common.Mod.Instance; | |
import cpw.mods.fml.common.event.FMLPostInitializationEvent; | |
@Mod(modid = "greeting", name = "Greeting", version = "1.0") | |
public class Greeting { | |
@Instance(value = "greeting") | |
public static Greeting instance; | |
private long ending = 0L; | |
private String message = ""; | |
@EventHandler | |
public void postInit(final FMLPostInitializationEvent event) { | |
MinecraftForge.EVENT_BUS.register(this); | |
} | |
@ForgeSubscribe(priority = EventPriority.NORMAL) | |
public void onRenderGameOverlay(final RenderGameOverlayEvent.Post event) { | |
if (event.type == RenderGameOverlayEvent.ElementType.TEXT | |
&& !message.isEmpty() && ending > System.currentTimeMillis()) { | |
final FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer; | |
GL11.glEnable(GL11.GL_BLEND); | |
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); | |
final int transparency = Math.round((ending - System.currentTimeMillis()) / 12f); | |
fontRenderer.drawStringWithShadow(message, (event.resolution.getScaledWidth() - fontRenderer.getStringWidth(message)) / 2, | |
Math.round(0.3f * event.resolution.getScaledHeight()), 16777215 + (Math.max(5, transparency) << 24)); | |
GL11.glDisable(GL11.GL_BLEND); | |
} | |
} | |
@ForgeSubscribe(priority = EventPriority.NORMAL) | |
public void onChatMessage(final ClientChatReceivedEvent event) { | |
try { | |
final String message = ((JsonObject) new JsonParser() | |
.parse(event.message)).get("text").getAsString(); | |
if (StringUtils.stripControlCodes(message).startsWith("** ")) { | |
ending = System.currentTimeMillis() + 3000L; | |
this.message = message.substring(3); | |
event.setCanceled(true); | |
} | |
} catch (final Exception e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment