diff --git a/capes.txt b/capes.txt new file mode 100644 index 000000000..c547bafa9 --- /dev/null +++ b/capes.txt @@ -0,0 +1,4 @@ +Adubbz:https://raw.github.com/Glitchfiend/BiomesOPlenty/master/capes/dev_cape.png +Amnet:https://raw.github.com/Glitchfiend/BiomesOPlenty/master/capes/dev_cape.png +Forstride:https://raw.github.com/Glitchfiend/BiomesOPlenty/master/capes/dev_cape.png +ted80:https://raw.github.com/Glitchfiend/BiomesOPlenty/master/capes/dev_cape.png \ No newline at end of file diff --git a/capes/dev_cape.png b/capes/dev_cape.png new file mode 100644 index 000000000..4b011f5d4 Binary files /dev/null and b/capes/dev_cape.png differ diff --git a/common/biomesoplenty/BiomesOPlenty.java b/common/biomesoplenty/BiomesOPlenty.java index a6c73c7c4..33fc24c87 100644 --- a/common/biomesoplenty/BiomesOPlenty.java +++ b/common/biomesoplenty/BiomesOPlenty.java @@ -20,6 +20,7 @@ import biomesoplenty.configuration.configfile.BOPConfigurationIDs; import biomesoplenty.configuration.configfile.BOPConfigurationMisc; import biomesoplenty.eventhandlers.BonemealEventHandler; import biomesoplenty.eventhandlers.BreakSpeedEventHandler; +import biomesoplenty.eventhandlers.CapeEventHandler; import biomesoplenty.eventhandlers.EntityEventHandler; import biomesoplenty.eventhandlers.FlipperMovementEventHandler; import biomesoplenty.eventhandlers.FluidEventHandler; @@ -113,6 +114,10 @@ public class BiomesOPlenty MinecraftForge.EVENT_BUS.register(new BreakSpeedEventHandler()); MinecraftForge.EVENT_BUS.register(new MovementHandler()); MinecraftForge.EVENT_BUS.register(new FlipperMovementEventHandler()); + if (event.getSide() == Side.CLIENT) + { + MinecraftForge.EVENT_BUS.register(new CapeEventHandler()); + } proxy.registerRenderers(); diff --git a/common/biomesoplenty/eventhandlers/CapeEventHandler.java b/common/biomesoplenty/eventhandlers/CapeEventHandler.java new file mode 100644 index 000000000..17f94ef5e --- /dev/null +++ b/common/biomesoplenty/eventhandlers/CapeEventHandler.java @@ -0,0 +1,179 @@ +package biomesoplenty.eventhandlers; + +import java.awt.Graphics; +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.HashMap; + +import javax.swing.ImageIcon; + +import net.minecraft.client.entity.AbstractClientPlayer; +import net.minecraft.client.renderer.ThreadDownloadImageData; +import net.minecraftforge.client.event.RenderPlayerEvent; +import net.minecraftforge.event.ForgeSubscribe; +import cpw.mods.fml.common.Loader; +import cpw.mods.fml.relauncher.ReflectionHelper; + +public class CapeEventHandler +{ + private final String serverLocation = "https://raw.github.com/Glitchfiend/BiomesOPlenty/master/capes.txt"; + private final int timeout = 1000; + + private static final Graphics TEST_GRAPHICS = new BufferedImage(128, 128, + BufferedImage.TYPE_INT_RGB).getGraphics(); + private HashMap cloaks = new HashMap(); + private ArrayList capePlayers = new ArrayList(); + + public static CapeEventHandler instance; + + public CapeEventHandler() + { + buildCloakURLDatabase(); + instance = this; + } + + @ForgeSubscribe + public void onPreRenderSpecials(RenderPlayerEvent.Specials.Pre event) + { + if (Loader.isModLoaded("shadersmod")) + { + return; + } + if (event.entityPlayer instanceof AbstractClientPlayer) + { + AbstractClientPlayer abstractClientPlayer = (AbstractClientPlayer) event.entityPlayer; + + if (!capePlayers.contains(abstractClientPlayer)) + { + String cloakURL = cloaks.get(event.entityPlayer.username); + + if (cloakURL == null) + { + return; + } + + capePlayers.add(abstractClientPlayer); + + ReflectionHelper.setPrivateValue(ThreadDownloadImageData.class, abstractClientPlayer.getTextureCape(), false, new String[] { "textureUploaded", "g" }); + + new Thread(new CloakThread(abstractClientPlayer, cloakURL)).start(); + event.renderCape = true; + } + } + } + + public void buildCloakURLDatabase() + { + URL url; + try + { + url = new URL(serverLocation); + URLConnection con = url.openConnection(); + con.setConnectTimeout(timeout); + con.setReadTimeout(timeout); + InputStream io = con.getInputStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(io)); + + String str; + int linetracker = 1; + while ((str = br.readLine()) != null) + { + if (!str.startsWith("--")) + { + if (str.contains(":")) + { + String nick = str.substring(0, str.indexOf(":")); + String link = str.substring(str.indexOf(":") + 1); + new Thread(new CloakPreload(link)).start(); + cloaks.put(nick, link); + } + else + { + System.err.println("[BiomesOPlenty] [skins.txt] Syntax error on line " + linetracker + ": " + str); + } + } + linetracker++; + } + + br.close(); + } + catch (MalformedURLException e) + { + e.printStackTrace(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + private class CloakThread implements Runnable + { + AbstractClientPlayer abstractClientPlayer; + String cloakURL; + + public CloakThread(AbstractClientPlayer player, String cloak) + { + abstractClientPlayer = player; + cloakURL = cloak; + } + + @Override + public void run() + { + try + { + Image cape = new ImageIcon(new URL(cloakURL)).getImage(); + BufferedImage bo = new BufferedImage(cape.getWidth(null), + cape.getHeight(null), BufferedImage.TYPE_INT_ARGB); + bo.getGraphics().drawImage(cape, 0, 0, null); + + ReflectionHelper.setPrivateValue(ThreadDownloadImageData.class, abstractClientPlayer.getTextureCape(), bo, new String[] { "bufferedImage", "d" }); + } + catch (MalformedURLException e) + { + e.printStackTrace(); + } + } + } + + private class CloakPreload implements Runnable + { + String cloakURL; + + public CloakPreload(String link) + { + cloakURL = link; + } + + @Override + public void run() + { + try + { + TEST_GRAPHICS + .drawImage(new ImageIcon(new URL(cloakURL)).getImage(), + 0, 0, null); + } + catch (MalformedURLException e) + { + e.printStackTrace(); + } + } + } + + public void refreshCapes() + { + cloaks.clear(); + capePlayers.clear(); + buildCloakURLDatabase(); + } +}