aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/CoflSky.java
blob: e5be925276a6e4a7817d8b30474e91b7bd201ebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package de.torui.coflsky;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.UUID;

import com.google.gson.JsonArray;
import com.google.gson.stream.JsonReader;

import de.torui.coflsky.websocket.WSClient;
import de.torui.coflsky.websocket.WSClientWrapper;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedOutEvent;
import net.minecraftforge.fml.relauncher.Side;

@Mod(modid = CoflSky.MODID, version = CoflSky.VERSION)
public class CoflSky
{
    public static final String MODID = "CoflSky";
    public static final String VERSION = "1.0";
    public static WSClientWrapper Wrapper;
    
    @EventHandler
    public void init(FMLInitializationEvent event) throws URISyntaxException
    {
    	
    	Minecraft.getSessionInfo().forEach((a,b) -> System.out.println("Key=" + a + " value=" + b));
    	
    	//System.out.println("Loggerfactory: " + LoggerFactory.getILoggerFactory());
    //	Logger log = LoggerFactory.getLogger(CoflSky.class);
   // 	log.debug("Testing");
    	
		// some example code
        System.out.println("Initializing");
        
        //new Thread(new WSClient(new URI("ws://localhost:8080"))).start();        
        System.out.println(">>>Started");
        
        String username = Minecraft.getSessionInfo().get("X-Minecraft-Username");
        String uuid = QueryUUID(username);
        System.out.println(">>> Username= " + username + " with UUID=" + uuid ); 
        
        String URI = "https://api.mojang.com/profiles/minecraft";
        
        
        //CoflSky.Wrapper = new WSClientWrapper("wss://sky-commands.coflnet.com/modsocket?version=" + CoflSky.VERSION  + "&uuid=");
        CoflSky.Wrapper = new WSClientWrapper("ws://sky-mod.coflnet.com/modsocket?version=" + CoflSky.VERSION  + "&uuid=" + uuid);
        
        if(event.getSide() == Side.CLIENT)
        	ClientCommandHandler.instance.registerCommand(new CoflSkyCommand());
        MinecraftForge.EVENT_BUS.register(new EventRegistry());	   
    }   
    private static class UUIDHelper {
    	public String id;
    	public String name;
    }
    public static String QueryUUID(String username) {
    	
		try {
			URL url = new URL("https://api.mojang.com/profiles/minecraft");
	    	HttpURLConnection con;
			con = (HttpURLConnection) url.openConnection();
			con.setRequestMethod("POST");
			
			con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
			con.setRequestProperty("Accept", "application/json");
			con.setDoInput(true);
			con.setDoOutput(true);

			// ...

			OutputStream os = con.getOutputStream();
			byte[] bytes = ("[\"" + username + "\"]").getBytes("UTF-8");
			os.write(bytes);
			os.close();
			
			 InputStream in = new BufferedInputStream(con.getInputStream());
			 ByteArrayOutputStream result = new ByteArrayOutputStream();
			 byte[] buffer = new byte[1024];
			 for (int length; (length = in.read(buffer)) != -1; ) {
			     result.write(buffer, 0, length);
			 }
			 // StandardCharsets.UTF_8.name() > JDK 7
			 String resString =  result.toString("UTF-8");
			 
			 System.out.println("Result= " + resString);
			 UUIDHelper[] helpers = WSClient.gson.fromJson(resString, UUIDHelper[].class);
			 if(helpers.length == 1) {
				 return helpers[0].id;
			 }
			 
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return UUID.randomUUID().toString();
    }
    

   /* @EventHandler
    public void init(FMLServerStartingEvent event)
    {
    	
    	if(event.getSide() == Side.CLIENT)    	return;
    		//event.registerServerCommand(new CoflSkyCommand());
    }*/
    
}