From 188afeb2d3ccede9e52f836c1431df8a025ec25f Mon Sep 17 00:00:00 2001 From: Thorinwasher Date: Fri, 22 Dec 2023 13:34:27 +0100 Subject: [PATCH] Basic implementation of the parity command Issue #243 --- .../stargate/command/CommandParity.java | 79 +++++++++++++++++- .../stargate/command/CommandStargate.java | 4 +- .../stargate/command/CommandParityTest.java | 13 ++- .../resources/TestPlugin-1.0-SNAPSHOT.jar | Bin 0 -> 2519 bytes 4 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 src/test/resources/TestPlugin-1.0-SNAPSHOT.jar diff --git a/src/main/java/org/sgrewritten/stargate/command/CommandParity.java b/src/main/java/org/sgrewritten/stargate/command/CommandParity.java index 9bc84d78d..1eb11961b 100644 --- a/src/main/java/org/sgrewritten/stargate/command/CommandParity.java +++ b/src/main/java/org/sgrewritten/stargate/command/CommandParity.java @@ -9,6 +9,12 @@ import org.sgrewritten.stargate.database.property.StoredPropertiesAPI; import org.sgrewritten.stargate.database.property.StoredProperty; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Objects; import java.util.logging.Level; @@ -16,10 +22,37 @@ public class CommandParity implements CommandExecutor { private final @NotNull StoredPropertiesAPI properties; private final boolean doParityUpgrades; + private static final String MECHANICS_URL = ""; + private static final String MECHANICS_NAME = "StargateMechanics"; + private static final String INTERFACES_URL = ""; + private static final String INTERFACES_NAME = "StargateInterfaces"; + private static final String CUSTOMIZATIONS_URL = ""; + private static final String CUSTOMIZATIONS_NAME = "StargateCustomizations"; + private static final String MAPPER_URL = ""; + private static final String MAPPER_NAME = "StargateMapper"; + private final File pluginFolder; + private URL mapper; + private URL mechanics; + private URL interfaces; + private URL customizations; - CommandParity(@NotNull StoredPropertiesAPI properties, boolean doParityUpgrades) { + CommandParity(@NotNull StoredPropertiesAPI properties, boolean doParityUpgrades, File pluginFolder) { + this(properties,doParityUpgrades,pluginFolder,MECHANICS_URL,INTERFACES_URL,CUSTOMIZATIONS_URL,MAPPER_URL); + } + + CommandParity(@NotNull StoredPropertiesAPI properties, boolean doParityUpgrades, File pluginFolder, String mechanics, String interfaces + , String customizations, String mapper) { this.properties = Objects.requireNonNull(properties); this.doParityUpgrades = doParityUpgrades; + this.pluginFolder = pluginFolder; + try { + this.mechanics = new URL(mechanics); + this.interfaces = new URL(interfaces); + this.customizations = new URL(customizations); + this.mapper = new URL(mapper); + } catch (MalformedURLException e) { + Stargate.log(e); + } } @Override @@ -34,8 +67,50 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command Stargate.log(Level.INFO, " Rejected parity upgrades."); return true; } - + File pluginsFolder = pluginFolder.getParentFile(); + File mechanicsFile = determineDestinationJarName(mechanics,MECHANICS_NAME,pluginsFolder); + File interfacesFile = determineDestinationJarName(interfaces,INTERFACES_NAME,pluginsFolder); + File customizationsFile = determineDestinationJarName(customizations,CUSTOMIZATIONS_NAME,pluginsFolder); + File mapperFile = determineDestinationJarName(mapper,MAPPER_NAME,pluginsFolder); + try { + downloadPlugin(mechanics, mechanicsFile); + downloadPlugin(interfaces, interfacesFile); + downloadPlugin(customizations, customizationsFile); + downloadPlugin(mapper, mapperFile); + } catch (IOException e) { + Stargate.log(e); + } return true; } + private void downloadPlugin(URL source, File destination) throws IOException { + if(destination.exists()){ + return; + } + if(!destination.createNewFile()){ + throw new IOException("Could not create new file: " + destination); + } + try (InputStream inputStream = source.openStream()){ + try(FileOutputStream outputStream = new FileOutputStream(destination)){ + inputStream.transferTo(outputStream); + } + } + } + + private File determineDestinationJarName(URL url, String pluginName, File pluginsFolder){ + String urlPath = url.getPath(); + String[] urlPathSplit = urlPath.split("-",0); + String fileName; + int urlPathSplitLength = urlPathSplit.length; + if(urlPathSplitLength > 2){ + fileName = pluginName + "-" + urlPathSplit[urlPathSplitLength-2] + "-" + urlPathSplit[urlPathSplitLength-1]; + } else { + fileName = pluginName + ".jar"; + } + if(!fileName.endsWith(".jar")){ + fileName = fileName + ".jar"; + } + return new File(pluginsFolder, fileName); + } + } diff --git a/src/main/java/org/sgrewritten/stargate/command/CommandStargate.java b/src/main/java/org/sgrewritten/stargate/command/CommandStargate.java index 99a560f47..28165f31f 100644 --- a/src/main/java/org/sgrewritten/stargate/command/CommandStargate.java +++ b/src/main/java/org/sgrewritten/stargate/command/CommandStargate.java @@ -40,9 +40,9 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command case "version": break; case "parityconfirm": - return new CommandParity(stargate.getStoredPropertiesAPI(), true).onCommand(commandSender, command, s, args); + return new CommandParity(stargate.getStoredPropertiesAPI(), true, stargate.getDataFolder()).onCommand(commandSender, command, s, args); case "parityreject": - return new CommandParity(stargate.getStoredPropertiesAPI(), false).onCommand(commandSender, command, s, args); + return new CommandParity(stargate.getStoredPropertiesAPI(), false, stargate.getDataFolder()).onCommand(commandSender, command, s, args); default: return false; } diff --git a/src/test/java/org/sgrewritten/stargate/command/CommandParityTest.java b/src/test/java/org/sgrewritten/stargate/command/CommandParityTest.java index 7bbe1d335..812db6482 100644 --- a/src/test/java/org/sgrewritten/stargate/command/CommandParityTest.java +++ b/src/test/java/org/sgrewritten/stargate/command/CommandParityTest.java @@ -25,15 +25,18 @@ class CommandParityTest { private PropertiesDatabase properties; private @NotNull ConsoleCommandSenderMock console; private final Command fakeCommand = new VersionCommand("fake"); + private MockPlugin plugin; @BeforeEach void setUp() throws IOException { @NotNull ServerMock server = MockBukkit.mock(); - @NotNull MockPlugin plugin = MockBukkit.createMockPlugin(); + this.plugin = MockBukkit.createMockPlugin(); console = server.getConsoleSender(); player = server.addPlayer(); properties = new PropertiesDatabase(new File(plugin.getDataFolder(), "test.properties")); - command = new CommandParity(properties, true); + File testPluginFile = new File(new File("").getAbsolutePath(),"/src/test/resources/TestPlugin-1.0-SNAPSHOT.jar"); + String testPluginPath = "file://" + testPluginFile.toURI().toURL().getFile(); + command = new CommandParity(properties, true, plugin.getDataFolder(), testPluginPath, testPluginPath, testPluginPath, testPluginPath); } @AfterEach @@ -55,12 +58,18 @@ void onCommandParityNotSet() { void onCommandParityFalse() { properties.setProperty(StoredProperty.PARITY_UPGRADES_AVAILABLE, "false"); Assertions.assertFalse(command.onCommand(console, fakeCommand, "", new String[]{""})); + } @Test void onCommandParityTrue() { properties.setProperty(StoredProperty.PARITY_UPGRADES_AVAILABLE, "true"); Assertions.assertTrue(command.onCommand(console, fakeCommand, "", new String[]{""})); + File pluginsFolder = plugin.getDataFolder().getParentFile(); + Assertions.assertTrue(new File(pluginsFolder,"StargateMechanics-1.0-SNAPSHOT.jar").exists()); + Assertions.assertTrue(new File(pluginsFolder,"StargateInterfaces-1.0-SNAPSHOT.jar").exists()); + Assertions.assertTrue(new File(pluginsFolder,"StargateCustomizations-1.0-SNAPSHOT.jar").exists()); + Assertions.assertTrue(new File(pluginsFolder,"StargateMapper-1.0-SNAPSHOT.jar").exists()); } } diff --git a/src/test/resources/TestPlugin-1.0-SNAPSHOT.jar b/src/test/resources/TestPlugin-1.0-SNAPSHOT.jar new file mode 100644 index 0000000000000000000000000000000000000000..f960738b995589714f21bc51a30be62af6bd4a4c GIT binary patch literal 2519 zcmWIWW@h1H0D<<{Y2jc7l;8x?zOEsTx}JV+`T>#~8;O+YMytjyQZ z&(qB{I7H9a?X&Nh(>~t1dKY=Ub+yi&Ilno`;EM5sr$stm=T7K&>pF3;KUoqsMNIpE zO2y7O-9f_MLBS<+wbqKra0^W~4ic>qJ{|N~r01*77oN(UbG(d;y$)842i>B$cc@_$%-F4L!K99}f>FMsq){8Yaz z(AV{Uf2_cz_FbjM`wZvW{+FFwE@|MTl*oB|(wrqK8y@r-h*}s}R5S1Oy6mhb!EShL zUBWiQ1pdm1`~2&g^I_o)38My=k_TzPuxSQjP)aI*gh^#?&S~#6zFUJhJazRrPiXp{ zKlAwMDc>_^HV2*1_qy)A*5i!s3D2v}({#b1Z%^TWU>V zn5re>a8+&Ew3H{}jK1uXW>!A!JSw@;)!|~3PwWoOwyHKUJh9IYbPYTO=O&h==E0*E z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