diff --git a/Bungee/build.gradle b/Bungee/build.gradle index e493e39..6b24599 100644 --- a/Bungee/build.gradle +++ b/Bungee/build.gradle @@ -27,7 +27,4 @@ dependencies { test { useJUnitPlatform() -} - -java.sourceCompatibility = JavaVersion.VERSION_1_9 -java.targetCompatibility = JavaVersion.VERSION_1_9 \ No newline at end of file +} \ No newline at end of file diff --git a/Bungee/src/main/java/tk/bluetree242/advancedplhide/bungee/AdvancedPlHideBungee.java b/Bungee/src/main/java/tk/bluetree242/advancedplhide/bungee/AdvancedPlHideBungee.java index 7695c2e..d2f496d 100644 --- a/Bungee/src/main/java/tk/bluetree242/advancedplhide/bungee/AdvancedPlHideBungee.java +++ b/Bungee/src/main/java/tk/bluetree242/advancedplhide/bungee/AdvancedPlHideBungee.java @@ -73,6 +73,7 @@ public void onEnable() { getProxy().getPluginManager().registerListener(this, new EventListener(this)); ProxyServer.getInstance().getPluginManager().registerCommand(this, new AdvancedPlHideCommand(this)); new Metrics(this, 13709); + ProxyServer.getInstance().getConsole().sendMessage(ChatColor.translateAlternateColorCodes('&', Constants.startupMessage())); performStartUpdateCheck(); } @@ -177,6 +178,11 @@ public String getVersionConfig() { throw new UncheckedIOException(e); } } + + @Override + public Type getType() { + return Type.BUNGEE; + } } } diff --git a/Bungee/src/main/java/tk/bluetree242/advancedplhide/bungee/listener/packet/PacketListener.java b/Bungee/src/main/java/tk/bluetree242/advancedplhide/bungee/listener/packet/PacketListener.java index 050cf20..996dc51 100644 --- a/Bungee/src/main/java/tk/bluetree242/advancedplhide/bungee/listener/packet/PacketListener.java +++ b/Bungee/src/main/java/tk/bluetree242/advancedplhide/bungee/listener/packet/PacketListener.java @@ -40,12 +40,12 @@ import tk.bluetree242.advancedplhide.impl.completer.SuggestionCommandCompleterList; import tk.bluetree242.advancedplhide.impl.subcompleter.SuggestionSubCommandCompleterList; import tk.bluetree242.advancedplhide.utils.Constants; -import tk.bluetree242.advancedplhide.utils.MultiMap; +import tk.bluetree242.advancedplhide.utils.UsedMap; import java.util.UUID; public class PacketListener extends AbstractPacketListener { - private final MultiMap commandsWaiting = new MultiMap<>(); + private final UsedMap commandsWaiting = new UsedMap<>(); private final AdvancedPlHideBungee core; public PacketListener(AdvancedPlHideBungee core) { @@ -79,6 +79,7 @@ public void packetSend(PacketSendEvent e) { }else if (notCompleted.contains(" ") && notCompleted.trim().startsWith("/")) { StringSubCommandCompleterList list = new StringSubCommandCompleterList(packet.getCommands(), notCompleted); CompleterModifier.handleSubCompleter(list, AdvancedPlHideBungee.getGroupForPlayer(player), player.hasPermission(Constants.SUB_WHITELIST_MODE_PERMISSION)); + if (list.isCancelled()) e.cancelled(true); } } else { if (!notCompleted.contains(" ") && notCompleted.trim().startsWith("/")) { @@ -87,6 +88,7 @@ public void packetSend(PacketSendEvent e) { }else if (notCompleted.contains(" ") && notCompleted.trim().startsWith("/")){ SuggestionSubCommandCompleterList suggestions = new SuggestionSubCommandCompleterList(e.packet().getSuggestions(), notCompleted); CompleterModifier.handleSubCompleter(suggestions, AdvancedPlHideBungee.getGroupForPlayer(player), player.hasPermission(Constants.SUB_WHITELIST_MODE_PERMISSION)); + if (suggestions.isCancelled()) e.cancelled(true); } } } diff --git a/README.md b/README.md index 4e3723b..734bc06 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,7 @@ ### Requirements -* Java 11 or higher (For Velocity) -* Java 9 or higher (Bungee and spigot) +* Java 11 or higher * ProtocolLib (Spigot) * Protocolize (Proxy) * Luckperms or some other perm plugin diff --git a/Spigot/build.gradle b/Spigot/build.gradle index 2e7f501..a02aaab 100644 --- a/Spigot/build.gradle +++ b/Spigot/build.gradle @@ -48,7 +48,3 @@ test { useJUnitPlatform() } - -java.sourceCompatibility = JavaVersion.VERSION_1_9 -java.targetCompatibility = JavaVersion.VERSION_1_9 - diff --git a/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/AdvancedPlHideSpigot.java b/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/AdvancedPlHideSpigot.java index 6756d6a..ee1b71d 100644 --- a/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/AdvancedPlHideSpigot.java +++ b/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/AdvancedPlHideSpigot.java @@ -89,6 +89,7 @@ public void onEnable() { getServer().getPluginCommand("advancedplhide").setExecutor(new AdvancedPlHideCommand(this)); getServer().getPluginCommand("advancedplhide").setTabCompleter(new AdvancedPlHideCommand.TabCompleter()); new Metrics(this, 13707); + Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', Constants.startupMessage())); performStartUpdateCheck(); } @@ -107,7 +108,8 @@ public void performStartUpdateCheck() { if (result == null) { getLogger().severe("Could not check for updates"); return; - } String msg = result.getVersionsBehind() == 0 ? + } + String msg = result.getVersionsBehind() == 0 ? ChatColor.translateAlternateColorCodes('&', Constants.DEFAULT_UP_TO_DATE) : ChatColor.translateAlternateColorCodes('&', Constants.DEFAULT_BEHIND.replace("{versions}", result.getVersionsBehind() + "") .replace("{download}", result.getUpdateUrl())); @@ -218,6 +220,11 @@ public String getVersionConfig() { throw new UncheckedIOException(e); } } + + @Override + public Type getType() { + return Type.SPIGOT; + } } diff --git a/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/listener/event/EventListener.java b/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/listener/event/EventListener.java index d12b92b..7dc06fb 100644 --- a/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/listener/event/EventListener.java +++ b/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/listener/event/EventListener.java @@ -56,7 +56,7 @@ public void onPlayerJoin(PlayerJoinEvent e) { Bukkit.getScheduler().runTask(core, () -> { UpdateCheckResult result = Platform.get().get().updateCheck(); if (result == null) return; - String msg = result.getVersionsBehind() == 0 ? null : ChatColor.translateAlternateColorCodes('&', "&e[APH-&2Velocity&e] " + Constants.DEFAULT_BEHIND.replace("{versions}", result.getVersionsBehind() + "").replace("{download}", result.getUpdateUrl())); + String msg = result.getVersionsBehind() == 0 ? null : ChatColor.translateAlternateColorCodes('&', "&e[APH-&2Spigot&e] " + Constants.DEFAULT_BEHIND.replace("{versions}", result.getVersionsBehind() + "").replace("{download}", result.getUpdateUrl())); if (result.getMessage() != null) { msg = ChatColor.translateAlternateColorCodes('&', "&e[APH-&2Spigot&e] &c" + result.getMessage()); } diff --git a/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/listener/packet/PacketListener.java b/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/listener/packet/PacketListener.java index 7a0c471..30b57e2 100644 --- a/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/listener/packet/PacketListener.java +++ b/Spigot/src/main/java/tk/bluetree242/advancedplhide/spigot/listener/packet/PacketListener.java @@ -37,13 +37,13 @@ import tk.bluetree242.advancedplhide.spigot.impl.completer.StringCommandCompleterList; import tk.bluetree242.advancedplhide.spigot.impl.subcompleter.StringSubCommandCompleterList; import tk.bluetree242.advancedplhide.utils.Constants; -import tk.bluetree242.advancedplhide.utils.MultiMap; +import tk.bluetree242.advancedplhide.utils.UsedMap; import java.util.UUID; public class PacketListener extends PacketAdapter { - private final MultiMap commandsWaiting = new MultiMap<>(); + private final UsedMap commandsWaiting = new UsedMap<>(); private AdvancedPlHideSpigot core; public PacketListener(AdvancedPlHideSpigot core) { @@ -65,6 +65,7 @@ private void onTabcomplete(PacketEvent e) { StructureModifier matchModifier = e.getPacket().getSpecificModifier(Suggestions.class); Suggestions suggestionsOrigin = matchModifier.read(0); String notCompleted = this.commandsWaiting.get(e.getPlayer().getUniqueId()); + commandsWaiting.remove(e.getPlayer().getUniqueId()); if (notCompleted == null) notCompleted = "/"; if (!notCompleted.contains(" ") && notCompleted.trim().startsWith("/")) { SuggestionCommandCompleterList suggestions = new SuggestionCommandCompleterList(suggestionsOrigin); @@ -73,6 +74,7 @@ private void onTabcomplete(PacketEvent e) { } else if (notCompleted.contains(" ") && notCompleted.trim().startsWith("/")){ SuggestionSubCommandCompleterList suggestions = new SuggestionSubCommandCompleterList(suggestionsOrigin, notCompleted); CompleterModifier.handleSubCompleter(suggestions, AdvancedPlHideSpigot.getGroupForPlayer(e.getPlayer()), e.getPlayer().hasPermission(Constants.SUB_WHITELIST_MODE_PERMISSION)); + if (suggestions.isCancelled()) e.setCancelled(true); matchModifier.write(0, suggestions.export()); } @@ -91,6 +93,7 @@ private void onTabcomplete(PacketEvent e) { } else if (notCompleted.contains(" ") && notCompleted.trim().startsWith("/")) { StringSubCommandCompleterList suggestions = new StringSubCommandCompleterList(suggestionsOrigin, notCompleted); CompleterModifier.handleSubCompleter(suggestions, AdvancedPlHideSpigot.getGroupForPlayer(e.getPlayer()), e.getPlayer().hasPermission(Constants.SUB_WHITELIST_MODE_PERMISSION)); + if (suggestions.isCancelled()) e.setCancelled(true); matchModifier.write(0, suggestions.export()); } } diff --git a/Velocity/src/main/java/tk/bluetree242/advancedplhide/velocity/AdvancedPlHideVelocity.java b/Velocity/src/main/java/tk/bluetree242/advancedplhide/velocity/AdvancedPlHideVelocity.java index d872b4c..eb6b0ce 100644 --- a/Velocity/src/main/java/tk/bluetree242/advancedplhide/velocity/AdvancedPlHideVelocity.java +++ b/Velocity/src/main/java/tk/bluetree242/advancedplhide/velocity/AdvancedPlHideVelocity.java @@ -136,6 +136,11 @@ public String getVersionConfig() { } } + @Override + public Type getType() { + return Type.VELOCITY; + } + public List getGroups() { return groups; } @@ -151,6 +156,7 @@ public void onProxyInitialization(ProxyInitializeEvent e) { server.getEventManager().register(this, new EventListener()); Protocolize.listenerProvider().registerListener(new PacketListener(this)); metricsFactory.make(this, 13708); + server.getConsoleCommandSource().sendMessage(LegacyComponentSerializer.legacy('&').deserialize(Constants.startupMessage())); performStartUpdateCheck(); } diff --git a/Velocity/src/main/java/tk/bluetree242/advancedplhide/velocity/listener/packet/PacketListener.java b/Velocity/src/main/java/tk/bluetree242/advancedplhide/velocity/listener/packet/PacketListener.java index cf1ffec..c4ecbd2 100644 --- a/Velocity/src/main/java/tk/bluetree242/advancedplhide/velocity/listener/packet/PacketListener.java +++ b/Velocity/src/main/java/tk/bluetree242/advancedplhide/velocity/listener/packet/PacketListener.java @@ -32,7 +32,7 @@ import dev.simplix.protocolize.api.listener.PacketSendEvent; import tk.bluetree242.advancedplhide.CompleterModifier; import tk.bluetree242.advancedplhide.utils.Constants; -import tk.bluetree242.advancedplhide.utils.MultiMap; +import tk.bluetree242.advancedplhide.utils.UsedMap; import tk.bluetree242.advancedplhide.velocity.AdvancedPlHideVelocity; import tk.bluetree242.advancedplhide.velocity.impl.completer.OfferCompleterList; import tk.bluetree242.advancedplhide.velocity.impl.subcompleter.OfferSubCommandCompleterList; @@ -40,7 +40,7 @@ import java.util.UUID; public class PacketListener extends AbstractPacketListener { - private final MultiMap commandsWaiting = new MultiMap<>(); + private final UsedMap commandsWaiting = new UsedMap<>(); private final AdvancedPlHideVelocity core; public PacketListener(AdvancedPlHideVelocity core) { @@ -73,6 +73,7 @@ public void packetSend(PacketSendEvent e) { }else if (notCompleted.contains(" ") && notCompleted.trim().startsWith("/")) { OfferSubCommandCompleterList list = new OfferSubCommandCompleterList(e.packet().getOffers(), notCompleted); CompleterModifier.handleSubCompleter(list, AdvancedPlHideVelocity.getGroupForPlayer(player), player.hasPermission(Constants.SUB_WHITELIST_MODE_PERMISSION)); + if (list.isCancelled()) e.cancelled(true); } } else { if ((!notCompleted.contains(" ") && notCompleted.trim().startsWith("/")) ) { @@ -81,6 +82,7 @@ public void packetSend(PacketSendEvent e) { }else if (notCompleted.contains(" ") && notCompleted.trim().startsWith("/")) { OfferSubCommandCompleterList list = new OfferSubCommandCompleterList(e.packet().getOffers(), notCompleted); CompleterModifier.handleSubCompleter(list, AdvancedPlHideVelocity.getGroupForPlayer(player), player.hasPermission(Constants.SUB_WHITELIST_MODE_PERMISSION)); + if (list.isCancelled()) e.cancelled(true); } } } diff --git a/build.gradle b/build.gradle index 7aa1861..5fff8da 100644 --- a/build.gradle +++ b/build.gradle @@ -41,6 +41,9 @@ subprojects { project.jar { archivesBaseName = 'AdvancedPlHide-' + project.name } + dependencies { + compile 'org.jetbrains:annotations:16.0.2' + } project.processResources { from(sourceSets.main.resources.srcDirs) { filter ReplaceTokens, tokens: [version : version, @@ -57,6 +60,7 @@ subprojects { shadowJar { classifier '' } + dependencies { implementation project(":Spigot") implementation project(":Velocity") diff --git a/core/build.gradle b/core/build.gradle index 6e2bd86..f5f137b 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -59,14 +59,9 @@ dependencies { implementation 'space.arim.dazzleconf:dazzleconf-ext-snakeyaml:1.2.0-M2' compileOnly 'com.mojang:brigadier:1.0.18' implementation group: 'org.json', name: 'json', version: '20210307' - implementation(platform("com.squareup.okhttp3:okhttp-bom:4.9.0")) - implementation("com.squareup.okhttp3:okhttp") } test { useJUnitPlatform() } - -java.sourceCompatibility = JavaVersion.VERSION_1_9 -java.targetCompatibility = JavaVersion.VERSION_1_9 diff --git a/core/src/main/java/tk/bluetree242/advancedplhide/Platform.java b/core/src/main/java/tk/bluetree242/advancedplhide/Platform.java index 1e05855..d2c94fb 100644 --- a/core/src/main/java/tk/bluetree242/advancedplhide/Platform.java +++ b/core/src/main/java/tk/bluetree242/advancedplhide/Platform.java @@ -22,14 +22,11 @@ package tk.bluetree242.advancedplhide; -import okhttp3.MediaType; -import okhttp3.MultipartBody; -import okhttp3.OkHttpClient; -import okhttp3.Request; import org.json.JSONObject; import tk.bluetree242.advancedplhide.config.Config; import tk.bluetree242.advancedplhide.exceptions.ConfigurationLoadException; import tk.bluetree242.advancedplhide.impl.version.UpdateCheckResult; +import tk.bluetree242.advancedplhide.utils.HttpPostMultipart; import java.util.ArrayList; import java.util.HashMap; @@ -95,20 +92,33 @@ public String getBuildDate() { public UpdateCheckResult updateCheck() { try { - OkHttpClient client = new OkHttpClient(); Map headers = new HashMap<>(); headers.put("User-Agent", "APH/Java"); - MultipartBody form = new MultipartBody.Builder().setType(MediaType.get("multipart/form-data")) - .addFormDataPart("version", getCurrentVersion()) - .addFormDataPart("buildNumber", getCurrentBuild()) - .addFormDataPart("buildDate", getCurrentBuild()) - .addFormDataPart("devUpdatechecker", getConfig().dev_updatechecker() + "").build(); - Request req = new Request.Builder().url("https://advancedplhide.ml/updatecheck").post(form).build(); - String response = client.newCall(req).execute().body().string(); + HttpPostMultipart req = new HttpPostMultipart("https://advancedplhide.ml/updatecheck", "utf-8", headers); + req.addFormField("version", getCurrentVersion()); + req.addFormField("buildNumber", getCurrentBuild()); + req.addFormField("buildDate", getBuildDate()); + req.addFormField("devUpdatechecker", getConfig().dev_updatechecker() + ""); + String response = req.finish(); JSONObject json = new JSONObject(response); - return new UpdateCheckResult(json.getInt("versions_behind"), json.isNull("versions_behind") ? null : json.isNull("message") ? null : json.getString("message"), json.isNull("type") ? "INFO" : json.getString("type"), json.getString("downloadUrl")); + return new UpdateCheckResult(json.getInt("versions_behind"), json.isNull("versions_behind") ? null : json.getString("message"), json.isNull("type") ? "INFO" : json.getString("type"), json.getString("downloadUrl")); } catch (Exception e) { return null; } } + + public abstract Type getType(); + + public enum Type { + SPIGOT("Spigot"), VELOCITY("Velocity"), BUNGEE("Bungee"); + private final String name; + + Type(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } } diff --git a/core/src/main/java/tk/bluetree242/advancedplhide/SubCommandCompleterList.java b/core/src/main/java/tk/bluetree242/advancedplhide/SubCommandCompleterList.java index 2c5e9ce..03c2415 100644 --- a/core/src/main/java/tk/bluetree242/advancedplhide/SubCommandCompleterList.java +++ b/core/src/main/java/tk/bluetree242/advancedplhide/SubCommandCompleterList.java @@ -73,6 +73,15 @@ public void removeAll() { } } + /** + * Checks if the packet (or event) should be cancelled + * @return if this should be cancelled, currently true only when list is empty + * @throws IllegalStateException if this isn't supposed to be for a cancellable event/packet + */ + public boolean isCancelled() { + return isEmpty(); + } + /** * Never use this method, most likely won't affect the list final result * @deprecated 90% won't affect the list final result diff --git a/core/src/main/java/tk/bluetree242/advancedplhide/utils/Constants.java b/core/src/main/java/tk/bluetree242/advancedplhide/utils/Constants.java index 062e9ff..6b25428 100644 --- a/core/src/main/java/tk/bluetree242/advancedplhide/utils/Constants.java +++ b/core/src/main/java/tk/bluetree242/advancedplhide/utils/Constants.java @@ -22,9 +22,25 @@ package tk.bluetree242.advancedplhide.utils; +import tk.bluetree242.advancedplhide.Platform; + public class Constants { public static final String DEFAULT_UP_TO_DATE = "&aYou are up to date"; public static final String DEFAULT_BEHIND = "&cYou are {versions} Behind. Please Update ASAP. Download from {download}"; public static final String WHITELIST_MODE_PERMISSION = "plhide.whitelist-mode"; public static final String SUB_WHITELIST_MODE_PERMISSION = "plhide.sub-whitelist-mode"; + + public static String startupMessage() { + return "" + + "\n[]=====[&2Enabling AdvancedPlHide&r]=====[]\n" + + "| &cInformation:\n&r" + + "| &cName: &rAdvancedPlHide\n&r" + + "| &cDevelopers: &rBlueTree242\n&r" + + "| &cVersion: &r" + Platform.get().getCurrentVersion() + "\n&r" + + "| &cRunning on: &r" + Platform.get().getType().getName() + "\n&r" + + "| &cSupport:\n&r" + + "| &cGithub: &rhttps://github.com/BlueTree242/AdvancedPlHide/issues\n" + + "| &cDiscord: &rhttps://advancedplhide.ml/support\n" + + "[]================================[]"; + } } diff --git a/core/src/main/java/tk/bluetree242/advancedplhide/utils/HttpPostMultipart.java b/core/src/main/java/tk/bluetree242/advancedplhide/utils/HttpPostMultipart.java new file mode 100644 index 0000000..688d5b7 --- /dev/null +++ b/core/src/main/java/tk/bluetree242/advancedplhide/utils/HttpPostMultipart.java @@ -0,0 +1,144 @@ +/* + * LICENSE + * AdvancedPlHide + * ------------- + * Copyright (C) 2021 - 2021 BlueTree242 + * ------------- + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * . + * END + */ + +package tk.bluetree242.advancedplhide.utils; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.util.Iterator; +import java.util.Map; +import java.util.UUID; + +public class HttpPostMultipart { + private static final String LINE = "\r\n"; + private final String boundary; + private HttpURLConnection httpConn; + private String charset; + private OutputStream outputStream; + private PrintWriter writer; + + /** + * This constructor initializes a new HTTP POST request with content type + * is set to multipart/form-data + * + * @param requestURL + * @param charset + * @param headers + * @throws IOException + */ + public HttpPostMultipart(String requestURL, String charset, Map headers) throws IOException { + this.charset = charset; + boundary = UUID.randomUUID().toString(); + URL url = new URL(requestURL); + httpConn = (HttpURLConnection) url.openConnection(); + httpConn.setUseCaches(false); + httpConn.setDoOutput(true); // indicates POST method + httpConn.setDoInput(true); + httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); + if (headers != null && headers.size() > 0) { + Iterator it = headers.keySet().iterator(); + while (it.hasNext()) { + String key = it.next(); + String value = headers.get(key); + httpConn.setRequestProperty(key, value); + } + } + outputStream = httpConn.getOutputStream(); + writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true); + } + + /** + * Adds a form field to the request + * + * @param name field name + * @param value field value + */ + public void addFormField(String name, String value) { + writer.append("--" + boundary).append(LINE); + writer.append("Content-Disposition: form-data; name=\"" + name + "\"").append(LINE); + writer.append("Content-Type: text/plain; charset=" + charset).append(LINE); + writer.append(LINE); + writer.append(value).append(LINE); + writer.flush(); + } + + /** + * Adds a upload file section to the request + * + * @param fieldName + * @param uploadFile + * @throws IOException + */ + public void addFilePart(String fieldName, File uploadFile) + throws IOException { + String fileName = uploadFile.getName(); + writer.append("--" + boundary).append(LINE); + writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"").append(LINE); + writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE); + writer.append("Content-Transfer-Encoding: binary").append(LINE); + writer.append(LINE); + writer.flush(); + + FileInputStream inputStream = new FileInputStream(uploadFile); + byte[] buffer = new byte[4096]; + int bytesRead = -1; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + outputStream.flush(); + inputStream.close(); + writer.append(LINE); + writer.flush(); + } + + /** + * Completes the request and receives response from the server. + * + * @return String as response in case the server returned + * status OK, otherwise an exception is thrown. + * @throws IOException + */ + public String finish() throws IOException { + String response = ""; + writer.flush(); + writer.append("--" + boundary + "--").append(LINE); + writer.close(); + + // checks server's status code first + int status = httpConn.getResponseCode(); + if (status == HttpURLConnection.HTTP_OK) { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length; + while ((length = httpConn.getInputStream().read(buffer)) != -1) { + result.write(buffer, 0, length); + } + response = result.toString(this.charset); + httpConn.disconnect(); + } else { + throw new IOException("Server returned non-OK status: " + status); + } + return response; + } +} diff --git a/core/src/main/java/tk/bluetree242/advancedplhide/utils/MultiMap.java b/core/src/main/java/tk/bluetree242/advancedplhide/utils/UsedMap.java similarity index 51% rename from core/src/main/java/tk/bluetree242/advancedplhide/utils/MultiMap.java rename to core/src/main/java/tk/bluetree242/advancedplhide/utils/UsedMap.java index 0f6c0ce..0158901 100644 --- a/core/src/main/java/tk/bluetree242/advancedplhide/utils/MultiMap.java +++ b/core/src/main/java/tk/bluetree242/advancedplhide/utils/UsedMap.java @@ -22,40 +22,8 @@ package tk.bluetree242.advancedplhide.utils; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; -import java.util.Map; -public class MultiMap { +public class UsedMap extends HashMap { - private Map> map = new HashMap<>(); - - public V get(K key) { - List list = map.get(key); - if (list == null || list.isEmpty()) return null; - return (V) list.get(0); - } - - public void put(K key, V val) { - List list = map.get(key); - if (list == null) { - list = new ArrayList(); - list.add(val); - map.put(key, list); - } else { - list.add(val); - } - } - - public void remove(K key) { - List list = map.get(key); - if (list == null) { - //nothing to remove - return; - } else { - list.remove(0); - if (list.isEmpty()) map.remove(list); - } - } } diff --git a/gradle.properties b/gradle.properties index 164a9ca..8657b41 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,5 +21,5 @@ # group=tk.bluetree242 name=AdvancedPlHide -version=2.1 +version=2.2 description=Remove Most ways of finding your plugins on your minecraft Server! \ No newline at end of file