Close the inputstreams/connections used for head conversion

This commit is contained in:
Thinkofdeath 2014-04-09 14:54:31 +01:00
parent e95737ad2c
commit 98825a6430

View File

@ -1,4 +1,4 @@
From 127178fe81c99ba39c9bf3fb1a8d89c192bd1b41 Mon Sep 17 00:00:00 2001 From d303eb8ffc3f82badc690c1bad71782f0d5f7b57 Mon Sep 17 00:00:00 2001
From: Thinkofdeath <thethinkofdeath@gmail.com> From: Thinkofdeath <thethinkofdeath@gmail.com>
Date: Wed, 9 Apr 2014 13:29:57 +0100 Date: Wed, 9 Apr 2014 13:29:57 +0100
Subject: [PATCH] Convert player heads async Subject: [PATCH] Convert player heads async
@ -112,10 +112,10 @@ index b241cfe..925e017 100644
public Packet getUpdatePacket() { public Packet getUpdatePacket() {
diff --git a/src/main/java/org/spigotmc/HeadConverter.java b/src/main/java/org/spigotmc/HeadConverter.java diff --git a/src/main/java/org/spigotmc/HeadConverter.java b/src/main/java/org/spigotmc/HeadConverter.java
new file mode 100644 new file mode 100644
index 0000000..0f465f0 index 0000000..bc949a1
--- /dev/null --- /dev/null
+++ b/src/main/java/org/spigotmc/HeadConverter.java +++ b/src/main/java/org/spigotmc/HeadConverter.java
@@ -0,0 +1,140 @@ @@ -0,0 +1,163 @@
+package org.spigotmc; +package org.spigotmc;
+ +
+import com.google.common.base.Charsets; +import com.google.common.base.Charsets;
@ -128,7 +128,6 @@ index 0000000..0f465f0
+import net.minecraft.server.MinecraftServer; +import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.NBTTagCompound; +import net.minecraft.server.NBTTagCompound;
+import net.minecraft.server.NBTTagList; +import net.minecraft.server.NBTTagList;
+import net.minecraft.server.PacketPlayOutTileEntityData;
+import net.minecraft.server.TileEntitySkull; +import net.minecraft.server.TileEntitySkull;
+import net.minecraft.util.com.mojang.authlib.GameProfile; +import net.minecraft.util.com.mojang.authlib.GameProfile;
+import org.bukkit.Bukkit; +import org.bukkit.Bukkit;
@ -162,7 +161,8 @@ index 0000000..0f465f0
+ final String name = head.getExtraType(); + final String name = head.getExtraType();
+ final NBTTagCompound tag = new NBTTagCompound(); + final NBTTagCompound tag = new NBTTagCompound();
+ head.b( tag ); + head.b( tag );
+ if ( tag.hasKey( "Owner" ) && tag.getCompound( "Owner" ).hasKey( "Properties" ) ) { + if ( tag.hasKey( "Owner" ) && tag.getCompound( "Owner" ).hasKey( "Properties" ) )
+ {
+ return; + return;
+ } + }
+ +
@ -171,12 +171,13 @@ index 0000000..0f465f0
+ @Override + @Override
+ public void run() + public void run()
+ { + {
+ HttpURLConnection connection = null;
+ try + try
+ { + {
+ // Firstly convert name -> uuid + // Firstly convert name -> uuid
+ URL accountsAPI = new URL( "https://api.mojang.com/profiles/page/1" ); + URL accountsAPI = new URL( "https://api.mojang.com/profiles/page/1" );
+ +
+ HttpURLConnection connection = (HttpURLConnection) accountsAPI.openConnection(); + connection = (HttpURLConnection) accountsAPI.openConnection();
+ connection.setRequestProperty( "Content-Type", "application/json" ); + connection.setRequestProperty( "Content-Type", "application/json" );
+ connection.setRequestMethod( "POST" ); + connection.setRequestMethod( "POST" );
+ connection.setDoInput( true ); + connection.setDoInput( true );
@ -188,8 +189,16 @@ index 0000000..0f465f0
+ outputStream.flush(); + outputStream.flush();
+ outputStream.close(); + outputStream.close();
+ +
+ JsonObject response = new JsonParser().parse( new InputStreamReader( connection.getInputStream() ) ) + InputStreamReader inputStreamReader = new InputStreamReader( connection.getInputStream() );
+ .getAsJsonObject(); + JsonObject response;
+ try
+ {
+ response = new JsonParser().parse( inputStreamReader )
+ .getAsJsonObject();
+ } finally
+ {
+ inputStreamReader.close();
+ }
+ if ( response.get( "size" ).getAsInt() != 1 ) + if ( response.get( "size" ).getAsInt() != 1 )
+ { + {
+ return; + return;
@ -208,10 +217,18 @@ index 0000000..0f465f0
+ // Now to lookup the textures + // Now to lookup the textures
+ +
+ URL url = new URL( "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid ); + URL url = new URL( "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid );
+ connection.disconnect();
+ connection = (HttpURLConnection) url.openConnection(); + connection = (HttpURLConnection) url.openConnection();
+ connection.setDoOutput( true ); + connection.setDoOutput( true );
+ response = new JsonParser().parse( new InputStreamReader( connection.getInputStream() ) ) + inputStreamReader = new InputStreamReader( connection.getInputStream() );
+ .getAsJsonObject(); + try
+ {
+ response = new JsonParser().parse( inputStreamReader )
+ .getAsJsonObject();
+ } finally
+ {
+ inputStreamReader.close();
+ }
+ +
+ if ( !response.has( "properties" ) ) + if ( !response.has( "properties" ) )
+ { + {
@ -251,6 +268,12 @@ index 0000000..0f465f0
+ } catch ( IOException e ) + } catch ( IOException e )
+ { + {
+ Bukkit.getLogger().warning( "Error connecting to Mojang servers, cannot convert player heads" ); + Bukkit.getLogger().warning( "Error connecting to Mojang servers, cannot convert player heads" );
+ } finally
+ {
+ if ( connection != null )
+ {
+ connection.disconnect();
+ }
+ } + }
+ } + }
+ } ); + } );