Skip to content

Commit

Permalink
Basic implementation of the parity command
Browse files Browse the repository at this point in the history
Issue #243
  • Loading branch information
Thorinwasher committed Dec 22, 2023
1 parent 86853e2 commit 188afeb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
79 changes: 77 additions & 2 deletions src/main/java/org/sgrewritten/stargate/command/CommandParity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,50 @@
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;

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
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}

}
Binary file added src/test/resources/TestPlugin-1.0-SNAPSHOT.jar
Binary file not shown.

0 comments on commit 188afeb

Please sign in to comment.