diff --git a/pom.xml b/pom.xml index e0306afe1..85118da31 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 org.maxgamer QuickShop - 4.0.4.4 + 4.0.4.5 diff --git a/src/main/java/org/maxgamer/quickshop/BuildInfo.java b/src/main/java/org/maxgamer/quickshop/BuildInfo.java index 4183659b8..c4e226092 100644 --- a/src/main/java/org/maxgamer/quickshop/BuildInfo.java +++ b/src/main/java/org/maxgamer/quickshop/BuildInfo.java @@ -2,7 +2,7 @@ package org.maxgamer.quickshop; import lombok.Getter; import org.bukkit.configuration.file.YamlConfiguration; -import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.IOException; import java.io.InputStream; @@ -18,8 +18,7 @@ public class BuildInfo { private final String pomGruopId; private final String pomArtifactId; - public BuildInfo(@NotNull QuickShop plugin) { - InputStream inputStream = plugin.getResource("BUILDINFO"); + public BuildInfo(@Nullable InputStream inputStream) { if (inputStream == null) { buildId = "Unknown"; buildTag = "Unknown"; diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index 6626d4386..63b5a8f61 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -505,7 +505,7 @@ public class QuickShop extends JavaPlugin { getLogger().info("QuickShop Reremake - Early boot step - Booting up..."); //BEWARE THESE ONLY RUN ONCE instance = this; - this.buildInfo = new BuildInfo(this); + this.buildInfo = new BuildInfo(getResource("BUILDINFO")); QuickShopAPI.setupApi(this); //noinspection ResultOfMethodCallIgnored getDataFolder().mkdirs(); diff --git a/src/main/java/org/maxgamer/quickshop/economy/EconomyTransaction.java b/src/main/java/org/maxgamer/quickshop/economy/EconomyTransaction.java index 1d550446f..b954a41ab 100644 --- a/src/main/java/org/maxgamer/quickshop/economy/EconomyTransaction.java +++ b/src/main/java/org/maxgamer/quickshop/economy/EconomyTransaction.java @@ -135,6 +135,7 @@ public class EconomyTransaction { steps = TransactionSteps.TAX; if (tax > 0 && taxAccount != null && !core.deposit(taxAccount, tax)) { this.lastError = "Failed to deposit tax account: " + tax; + callback.onTaxFailed(this); //Tax never should failed. } steps = TransactionSteps.DONE; @@ -155,6 +156,9 @@ public class EconomyTransaction { if (steps == TransactionSteps.CHECK) { return rollbackSteps; //We did nothing, just checks balance } + if (steps == TransactionSteps.WITHDRAW) { + return rollbackSteps; //We did nothing, because the trade failed so no anybody money changes. + } if (steps == TransactionSteps.DEPOSIT || steps == TransactionSteps.TAX) { if (from != null && !core.deposit(from, amount)) { //Rollback withdraw if (!continueWhenFailed) { @@ -201,6 +205,10 @@ public class EconomyTransaction { Util.debugLog("Transaction failed: " + economyTransaction.getLastError() + "."); } + default void onTaxFailed(@NotNull EconomyTransaction economyTransaction) { + Util.debugLog("Tax Transaction failed: " + economyTransaction.getLastError() + "."); + } + } } diff --git a/src/main/java/org/maxgamer/quickshop/util/updater/NewUpdater.java b/src/main/java/org/maxgamer/quickshop/util/updater/NewUpdater.java deleted file mode 100644 index a1657af76..000000000 --- a/src/main/java/org/maxgamer/quickshop/util/updater/NewUpdater.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.maxgamer.quickshop.util.updater; - -public class NewUpdater { - -} diff --git a/src/main/java/org/maxgamer/quickshop/util/updater/QuickUpdater.java b/src/main/java/org/maxgamer/quickshop/util/updater/QuickUpdater.java new file mode 100644 index 000000000..04c5ab792 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/util/updater/QuickUpdater.java @@ -0,0 +1,15 @@ +package org.maxgamer.quickshop.util.updater; + +import org.jetbrains.annotations.NotNull; + +import java.io.IOException; + +public interface QuickUpdater { + @NotNull VersionType getCurrentRunning(); + + boolean isLatest(@NotNull VersionType versionType) throws IOException; + + @NotNull byte[] update(@NotNull VersionType versionType) throws IOException; + + void install(byte[] bytes) throws IOException; +} diff --git a/src/main/java/org/maxgamer/quickshop/util/updater/VersionType.java b/src/main/java/org/maxgamer/quickshop/util/updater/VersionType.java new file mode 100644 index 000000000..cc22c8cb8 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/util/updater/VersionType.java @@ -0,0 +1,7 @@ +package org.maxgamer.quickshop.util.updater; + +public enum VersionType { + STABLE, + SNAPSHOT, + LTS +} diff --git a/src/main/java/org/maxgamer/quickshop/util/updater/impl/JenkinsUpdater.java b/src/main/java/org/maxgamer/quickshop/util/updater/impl/JenkinsUpdater.java new file mode 100644 index 000000000..45699cc89 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/util/updater/impl/JenkinsUpdater.java @@ -0,0 +1,97 @@ +package org.maxgamer.quickshop.util.updater.impl; + +import lombok.AllArgsConstructor; +import org.bukkit.plugin.InvalidDescriptionException; +import org.bukkit.plugin.PluginDescriptionFile; +import org.jetbrains.annotations.NotNull; +import org.maxgamer.quickshop.BuildInfo; +import org.maxgamer.quickshop.QuickShop; +import org.maxgamer.quickshop.nonquickshopstuff.com.sk89q.worldedit.util.net.HttpRequest; +import org.maxgamer.quickshop.util.Util; +import org.maxgamer.quickshop.util.updater.QuickUpdater; +import org.maxgamer.quickshop.util.updater.VersionType; + +import java.io.*; +import java.net.URL; + +@AllArgsConstructor +public class JenkinsUpdater implements QuickUpdater { + private final BuildInfo pluginBuildInfo; + + @Override + public @NotNull VersionType getCurrentRunning() { + return VersionType.STABLE; //TODO LTS + } + + @Override + public boolean isLatest(@NotNull VersionType versionType) throws IOException { + InputStream inputStream = HttpRequest.get(new URL("https://ci.codemc.io/job/Ghost-chu/job/QuickShop-Reremake/lastSuccessfulBuild/artifact/target/BUILDINFO")) + .header("User-Agent", "QuickShop-" + QuickShop.getFork() + " " + QuickShop.getVersion()) + .execute() + .expectResponseCode(200) + .getInputStream(); + BuildInfo buildInfo = new BuildInfo(inputStream); + return buildInfo.getBuildId().equalsIgnoreCase(pluginBuildInfo.getBuildId()) || buildInfo.getGitCommit().equalsIgnoreCase(pluginBuildInfo.getGitCommit()); + } + + @Override + public byte[] update(@NotNull VersionType versionType) throws IOException { + InputStream is = HttpRequest.get(new URL("https://ci.codemc.io/job/Ghost-chu/job/QuickShop-Reremake/lastSuccessfulBuild/artifact/target/QuickShop.jar")) + .header("User-Agent", "QuickShop-" + QuickShop.getFork() + " " + QuickShop.getVersion()) + .execute() + .getInputStream(); + try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { + byte[] buff = new byte[1024]; + int len; + long downloaded = 0; + if (is == null) { + throw new IOException("Failed downloading: Cannot open connection with remote server."); + } + while ((len = is.read(buff)) != -1) { + os.write(buff, 0, len); + downloaded += len; + Util.debugLog("File Downloader: " + downloaded + " bytes."); + } + is.close(); + byte[] file = os.toByteArray(); + os.close(); + return file; + } + } + + @Override + public void install(byte[] bytes) throws IOException { + File pluginFolder = new File("plugins"); + if (!pluginFolder.exists()) { + throw new RuntimeException("Can't find the plugins folder."); + } + if (!pluginFolder.isDirectory()) { + throw new RuntimeException("Plugins not a folder."); + } + File[] plugins = pluginFolder.listFiles(); + if (plugins == null) { + throw new IOException("Can't get the files in plugins folder"); + } + File quickshop = null; + for (File plugin : plugins) { + try { + PluginDescriptionFile desc = QuickShop.getInstance().getPluginLoader().getPluginDescription(plugin); + if (!desc.getName().equals(QuickShop.getInstance().getDescription().getName())) { + continue; + } + Util.debugLog("Selected: " + plugin.getPath()); + quickshop = plugin; + break; + } catch (InvalidDescriptionException e) { // Ignore } + } + } + if (quickshop == null) { + throw new IOException("Failed to get QuickShop Jar File."); + } + + OutputStream outputStream = new FileOutputStream(quickshop, false); + outputStream.write(bytes); + outputStream.flush(); + outputStream.close(); + } +}