Skip to content
Snippets Groups Projects
Commit f46451bb authored by insert's avatar insert
Browse files

Improve message builder + HTTP(S) wrapper

parent 4daa694c
Branches
Tags
No related merge requests found
package uk.insrt.minecraft.inslib.helpers;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HTTP {
private final static String USER_AGENT = "inslib/1.0.4 HTTP(S) Wrapper";
public static String GET(String URL) throws Exception {
// Redirect to SECURE class if URL is using HTTPS protocol
if (URL.toLowerCase().startsWith("https")) return SECURE.GET(URL);
// Create URL
URL url = new URL(URL);
// Connect
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
// Check if we are fine in reading or not
int responseCode = con.getResponseCode();
if (responseCode != 200) {
throw new Exception("Response code was not 200!");
}
// Read response
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
String fullInput = "";
while ((inputLine = in.readLine()) != null)
fullInput += inputLine;
in.close();
// Return response
return fullInput;
}
public static String POST(String URL, String PARAMETERS) throws Exception {
// Redirect to SECURE class if URL is using HTTPS protocol
if (URL.toLowerCase().startsWith("https")) return SECURE.POST(URL, PARAMETERS);
// Create URL
URL url = new URL(URL);
// Connect
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
// Flush parameters
con.setDoOutput(true);
DataOutputStream write = new DataOutputStream(con.getOutputStream());
write.writeBytes(PARAMETERS);
write.flush();
write.close();
// Check if we are fine in reading or not
int responseCode = con.getResponseCode();
if (responseCode != 200) {
throw new Exception("Response code was not 200!");
}
// Read response
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
String fullInput = "";
while ((inputLine = in.readLine()) != null)
fullInput += inputLine;
in.close();
// Return response
return fullInput;
}
public static class SECURE {
public static String GET(String URL) throws Exception {
// Create URL
URL url = new URL(URL);
// Connect
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
// Check if we are fine in reading or not
int responseCode = con.getResponseCode();
if (responseCode != 200) {
throw new Exception("Response code was not 200!");
}
// Read response
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
String fullInput = "";
while ((inputLine = in.readLine()) != null)
fullInput += inputLine;
in.close();
// Return response
return fullInput;
}
public static String POST(String URL, String PARAMETERS) throws Exception {
// Create URL
URL url = new URL(URL);
// Connect
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
// Flush parameters
con.setDoOutput(true);
DataOutputStream write = new DataOutputStream(con.getOutputStream());
write.writeBytes(PARAMETERS);
write.flush();
write.close();
// Check if we are fine in reading or not
int responseCode = con.getResponseCode();
if (responseCode != 200) {
throw new Exception("Response code was not 200!");
}
// Read response
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
String fullInput = "";
while ((inputLine = in.readLine()) != null)
fullInput += inputLine;
in.close();
// Return response
return fullInput;
}
}
}
package uk.insrt.minecraft.inslib.helpers;
import net.minecraft.client.Minecraft;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
......@@ -8,10 +9,22 @@ import net.minecraft.util.text.TextFormatting;
public class MessageBuilder {
private String message = "";
public MessageBuilder() {
}
public MessageBuilder(String message) {
this.message = message;
}
public MessageBuilder(String message, String modifier) {
add(message, modifier);
}
public MessageBuilder(String message, String[] modifiers) {
add(message, modifiers);
}
public MessageBuilder add(String text) {
this.message += TextFormatting.RESET + text;
return this;
......@@ -45,6 +58,10 @@ public class MessageBuilder {
return new TextComponentString(message);
}
public void send(ICommandSender sender) {
sender.sendMessage(getComponent());
}
public void send(EntityPlayer player) {
player.sendMessage(getComponent());
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment