· 6 years ago · Sep 25, 2019, 02:50 PM
1package extensions.VoteEngine.auto;
2
3import java.io.BufferedReader;
4import java.io.InputStreamReader;
5import java.net.HttpURLConnection;
6import java.net.URL;
7import java.util.logging.Logger;
8
9import net.sf.l2j.Config;
10
11/**
12 * Usage if (VoteRewardHotzone.check(player.getClient().getConnection().getInetAddress().toString()) == true) means player has voted
13 * @author Nightwolf
14 */
15public class VoteRewardHotzone
16{
17 private static Logger _log = Logger.getLogger(VoteRewardHotzone.class.getName());
18
19 // Configurations.
20 private static boolean debug = true;
21 private static String player_ip;
22 private static String server_id;
23 private static String server_ip;
24 private static String server_api;
25
26 public static boolean check(String ip)
27 {
28 if (debug)
29 _log.info("Server VOTE CHECK START");
30 // set configs
31 player_ip = ip;// value from player's IP [player.getClient().getConnection().getInetAddress().toString()]
32 server_id = Config.HOTZONE_SERVER_ID;// value from config (server id)
33 server_ip = Config.HOTZONE_SERVER_IP;// value from config (server IP)
34 server_api = Config.HOTZONE_SERVER_API;// value from config (server API)
35
36 return HotCheck();
37 }
38
39 private final static boolean HotCheck()
40 {
41 String _status = null;
42 String _error_code = null;
43 String _server_id = null;
44 String _server_ip = null;
45 String _server_api = null;
46 String _server_ipv = null;
47 Boolean _voted = false;
48
49 try
50 {
51 final URL connection = new URL("https://l2hotzone.com/api.php?API_KEY=" + server_api + "&SERVER_ID=" + server_id + "&IP=" + server_ip + "&VOTER_IP=" + player_ip);
52
53 final HttpURLConnection con = (HttpURLConnection) connection.openConnection();
54 con.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36");
55
56 final int responseCode = con.getResponseCode();
57 if (debug)
58 _log.info("Server RESPONSE CODE: " + responseCode);
59 if (responseCode == 200) // OK
60 {
61 // JsonObject json = new JsonObject(new URL("https://l2hotzone.com/api.php?API_KEY=" + server_api + "&SERVER_ID=" + server_id + "&IP=" + server_ip + "&VOTER_IP=" + ip));
62
63 try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())))
64 {
65 String line = null;
66 while ((line = br.readLine()) != null)
67 {
68 // remove characters "{" "}" "\" "result:" and "spaces"
69 line = line.replace("{", "");
70 line = line.replace("}", "");
71 line = line.replace("\"", "");
72 line = line.replace("result:", "");
73 line = line.replace(" ", "");
74 if (debug)
75 _log.info("Server clear string: " + line);
76
77 // split key:value, key:value, ...
78 String[] keySplit = line.split(",");
79
80 int i = 0;
81 while (keySplit != null)
82 {
83 String value = keySplit[i];
84 // split key and value
85 String[] valSplit = value.split(":");
86 String key = valSplit[0];// get key
87 String val = valSplit[1];// get val
88 // set values
89 if (key.equals("status"))
90 {
91 _status = val;
92 }
93 if (key.equals("error_code"))
94 {
95 _error_code = val;
96 }
97 if (key.equals("ServerId"))
98 {
99 _server_id = val;
100 }
101 if (key.equals("ipaddress"))
102 {
103 _server_ip = val;
104 }
105 if (key.equals("Api-Key"))
106 {
107 _server_api = val;
108 }
109 if (key.equals("Ipversion"))
110 {
111 _server_ipv = val;
112 }
113 if (key.equals("voted"))
114 {
115 _voted = Boolean.valueOf(val);
116 }
117
118 if (debug)
119 _log.info("Server READER: key(" + key + ") val(" + val + ")");
120
121 i++;
122 // exit while
123 if (i == 7)
124 break;
125
126 }
127 // validate the response messages
128 // check status and if not valid show error code message
129 if (_status == null || _server_id == null || _server_ip == null || _server_api == null || _server_ipv == null || _voted == null)
130 {
131 _log.info("Vote Error: null values:" + _status);
132 _log.info("Vote Error: null values:" + _server_id);
133 _log.info("Vote Error: null values:" + _server_ip);
134 _log.info("Vote Error: null values:" + _server_api);
135 _log.info("Vote Error: null values:" + _server_ipv);
136 _log.info("Vote Error: null values:" + _voted);
137 return false;
138 }
139 // check status
140 if (!_status.equals("valid"))
141 {
142 _log.info("Vote Error: " + _error_code);
143 return false;
144 }
145 // check server id
146 if (!_server_id.equals(server_id))
147 {
148 _log.info("Vote Error: server ID was not the same: " + _server_id + " should be: " + server_id);
149 return false;
150 }
151 if (!_server_ip.equals(server_ip))
152 {
153 _log.info("Vote Error: server IP was not the same: " + _server_ip + " should be: " + server_ip);
154 return false;
155 }
156 if (!_server_api.equals(server_api))
157 {
158 _log.info("Vote Error: server API was not the same: " + _server_api + " should be: " + server_api);
159 return false;
160 }
161 if (!_server_ipv.equals("IPversion4"))
162 {
163 _log.info("Vote Error: user IP Version was not 4: " + _server_ipv + " should be: IPversion4");
164 return false;
165 }
166
167 if (debug)
168 _log.info("Server Voted: " + _voted);
169
170 return _voted;
171 }
172 }
173 }
174 }
175 catch (Exception e)
176 {
177 e.printStackTrace();
178 }
179 return false;
180 }
181}