Fixed schems

The admin command "schem" now works very like WorldEdit.
You set the pos1 and pos2, copy to clipboard and paste
You can save and load. Schems go into the schems folder.
This commit is contained in:
tastybento 2018-06-03 15:33:45 -07:00
parent 1289060bd4
commit f3472815c9
3 changed files with 88 additions and 61 deletions

View File

@ -130,6 +130,7 @@ commands:
copy-first: "&cCopy a schem first!"
no-such-file: "&cNo such file!"
could-not-load: "&cCould not load that file!"
could-not-save: "&Hmm, something went wrong saving that file: [message]"
set-pos1: "&aPosition 1 set at [vector]"
set-pos2: "&aPosition 2 set at [vector]"
need-pos1-pos2: "&cSet pos1 and pos2 first!"

View File

@ -1,7 +1,5 @@
package us.tastybento.bskyblock.commands.admin;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -10,10 +8,10 @@ import java.util.UUID;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.island.builders.Clipboard;
import us.tastybento.bskyblock.util.Util;
public class AdminSchemCommand extends CompositeCommand {
private Map<UUID, Clipboard> clipboards;
private File schemFolder;
public AdminSchemCommand(CompositeCommand parent) {
super(parent, "schem");
@ -25,10 +23,6 @@ public class AdminSchemCommand extends CompositeCommand {
setDescription("commands.admin.schem.description");
setOnlyPlayer(true);
clipboards = new HashMap<>();
schemFolder = new File(getPlugin().getDataFolder(), "schems");
if (!schemFolder.exists()) {
schemFolder.mkdirs();
}
}
public boolean execute(User user, List<String> args) {
@ -51,26 +45,15 @@ public class AdminSchemCommand extends CompositeCommand {
if (args.get(0).equalsIgnoreCase("load")) {
if (args.size() == 2) {
File file = new File(schemFolder, args.get(1));
if (file.exists()) {
try {
cb.load(file);
user.sendMessage("general.success");
if (cb.load(user, args.get(1))) {
clipboards.put(user.getUniqueId(), cb);
return true;
} catch (Exception e) {
user.sendMessage("commands.admin.schem.could-not-load");
e.printStackTrace();
return false;
}
} else {
user.sendMessage("commands.admin.schem.no-such-file");
return false;
}
} else {
showHelp(this, user);
return false;
}
return false;
}
if (args.get(0).equalsIgnoreCase("copy")) {
@ -80,15 +63,7 @@ public class AdminSchemCommand extends CompositeCommand {
if (args.get(0).equalsIgnoreCase("save")) {
if (cb.isFull()) {
if (args.size() == 2) {
File file = new File(schemFolder, args.get(1));
user.sendMessage("general.success");
try {
cb.save(file);
} catch (IOException e) {
user.sendRawMessage("Could not save!");
return false;
}
return true;
return cb.save(user, args.get(1));
} else {
showHelp(this, user);
return false;
@ -101,14 +76,14 @@ public class AdminSchemCommand extends CompositeCommand {
if (args.get(0).equalsIgnoreCase("pos1")) {
cb.setPos1(user.getLocation());
user.sendMessage("commands.admin.schem.set-pos1", "[vector]", user.getLocation().toVector().toString());
user.sendMessage("commands.admin.schem.set-pos1", "[vector]", Util.xyz(user.getLocation().toVector()));
clipboards.put(user.getUniqueId(), cb);
return true;
}
if (args.get(0).equalsIgnoreCase("pos2")) {
cb.setPos2(user.getLocation());
user.sendMessage("commands.admin.schem.set-pos2", "[vector]", user.getLocation().toVector().toString());
user.sendMessage("commands.admin.schem.set-pos2", "[vector]", Util.xyz(user.getLocation().toVector()));
clipboards.put(user.getUniqueId(), cb);
return true;
}

View File

@ -63,9 +63,15 @@ public class Clipboard {
private BSkyBlock plugin;
private boolean copied;
private File schemFolder;
public Clipboard(BSkyBlock plugin) {
super();
this.plugin = plugin;
schemFolder = new File(plugin.getDataFolder(), "schems");
if (!schemFolder.exists()) {
schemFolder.mkdirs();
}
}
/**
* @return the pos1
@ -324,24 +330,10 @@ public class Clipboard {
return blockConfig;
}
/**
* Load a file to clipboard
* @param file
* @throws IOException
* @throws InvalidConfigurationException
*/
public void load(File file) throws IOException, InvalidConfigurationException {
unzip(file.getAbsolutePath());
blockConfig = new YamlConfiguration();
blockConfig.load(file);
copied = true;
Files.delete(file.toPath());
}
private void unzip(final String zipFilePath) throws IOException {
Path path = Paths.get(zipFilePath);
if (!(path.toFile().exists())) {
return;
throw new IOException("No file exists!");
}
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath))) {
ZipEntry entry = zipInputStream.getNextEntry();
@ -371,18 +363,8 @@ public class Clipboard {
}
/**
* Save the clipboard to a file
* @param file
* @throws IOException
*/
public void save(File file) throws IOException {
getBlockConfig().save(file);
zip(file);
}
private void zip(File targetFile) throws IOException {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetFile.getAbsolutePath() + ".zip"))) {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetFile.getAbsolutePath() + ".schem"))) {
zipOutputStream.putNextEntry(new ZipEntry(targetFile.getName()));
try (FileInputStream inputStream = new FileInputStream(targetFile)) {
final byte[] buffer = new byte[1024];
@ -402,4 +384,73 @@ public class Clipboard {
public boolean isFull() {
return copied;
}
/**
* Load a file to clipboard
* @param file
* @throws IOException
* @throws InvalidConfigurationException
*/
public boolean load(User user, String string) {
File zipFile = new File(schemFolder, string + ".schem");
if (!zipFile.exists()) {
user.sendMessage("commands.admin.schem.no-such-file");
return false;
}
try {
unzip(zipFile.getAbsolutePath());
} catch (IOException e) {
user.sendMessage("commands.admin.schem.could-not-load");
plugin.logError("Could not load schems file - could not unzip : " + zipFile.getName());
return false;
}
File file = new File(schemFolder, string);
if (!file.exists()) {
user.sendMessage("commands.admin.schem.could-not-load");
plugin.logError("Could not load schems file - does not exist : " + file.getName());
return false;
}
blockConfig = new YamlConfiguration();
try {
blockConfig.load(file);
} catch (Exception e) {
user.sendMessage("commands.admin.schem.could-not-load");
plugin.logError("Could not load schems file - YAML error : " + file.getName());
return false;
}
copied = true;
try {
Files.delete(file.toPath());
} catch (IOException e) {
plugin.logError("Could not delete temporary schems file: " + file.getName());
}
user.sendMessage("general.success");
return true;
}
/**
* Save the clipboard to a file
* @param user
* @param string
* @return
*/
public boolean save(User user, String string) {
File file = new File(schemFolder, string);
try {
getBlockConfig().save(file);
} catch (IOException e) {
user.sendMessage("commands.admin.schem.could-not-save", "[message]", "Could not save temp schems file.");
plugin.logError("Could not save temporary schems file: " + file.getName());
return false;
}
try {
zip(file);
} catch (IOException e) {
user.sendMessage("commands.admin.schem.could-not-save", "[message]", "Could not zip temp schems file.");
plugin.logError("Could not zip temporary schems file: " + file.getName());
return false;
}
user.sendMessage("general.success");
return true;
}
}