use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.
the class WorldUtil method upload.
public void upload(@NonNull final Plot plot, @Nullable final UUID uuid, @Nullable final String file, @NonNull final RunnableVal<URL> whenDone) {
plot.getHome(home -> SchematicHandler.upload(uuid, file, "zip", new RunnableVal<>() {
@Override
public void run(OutputStream output) {
try (final ZipOutputStream zos = new ZipOutputStream(output)) {
File dat = getDat(plot.getWorldName());
Location spawn = getSpawn(plot.getWorldName());
if (dat != null) {
ZipEntry ze = new ZipEntry("world" + File.separator + dat.getName());
zos.putNextEntry(ze);
try (NBTInputStream nis = new NBTInputStream(new GZIPInputStream(new FileInputStream(dat)))) {
Map<String, Tag> tag = ((CompoundTag) nis.readNamedTag().getTag()).getValue();
Map<String, Tag> newMap = new HashMap<>();
for (Map.Entry<String, Tag> entry : tag.entrySet()) {
if (!entry.getKey().equals("Data")) {
newMap.put(entry.getKey(), entry.getValue());
continue;
}
Map<String, Tag> data = new HashMap<>(((CompoundTag) entry.getValue()).getValue());
data.put("SpawnX", new IntTag(home.getX()));
data.put("SpawnY", new IntTag(home.getY()));
data.put("SpawnZ", new IntTag(home.getZ()));
newMap.put("Data", new CompoundTag(data));
}
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
try (NBTOutputStream out = new NBTOutputStream(new GZIPOutputStream(baos, true))) {
// TODO Find what this should be called
out.writeNamedTag("Schematic????", new CompoundTag(newMap));
}
zos.write(baos.toByteArray());
}
}
}
setSpawn(spawn);
byte[] buffer = new byte[1024];
Set<BlockVector2> added = new HashSet<>();
for (Plot current : plot.getConnectedPlots()) {
Location bot = current.getBottomAbs();
Location top = current.getTopAbs();
int brx = bot.getX() >> 9;
int brz = bot.getZ() >> 9;
int trx = top.getX() >> 9;
int trz = top.getZ() >> 9;
Set<BlockVector2> files = getChunkChunks(bot.getWorldName());
for (BlockVector2 mca : files) {
if (mca.getX() >= brx && mca.getX() <= trx && mca.getZ() >= brz && mca.getZ() <= trz && !added.contains(mca)) {
final File file = getMcr(plot.getWorldName(), mca.getX(), mca.getZ());
if (file != null) {
// final String name = "r." + (x - cx) + "." + (z - cz) + ".mca";
String name = file.getName();
final ZipEntry ze = new ZipEntry("world" + File.separator + "region" + File.separator + name);
zos.putNextEntry(ze);
added.add(mca);
try (FileInputStream in = new FileInputStream(file)) {
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
}
zos.closeEntry();
}
}
}
}
zos.closeEntry();
zos.flush();
zos.finish();
} catch (IOException e) {
e.printStackTrace();
}
}
}, whenDone));
}
use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.
the class Save method onCommand.
@Override
public boolean onCommand(final PlotPlayer<?> player, final String[] args) {
final String world = player.getLocation().getWorldName();
if (!this.plotAreaManager.hasPlotArea(world)) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
return false;
}
final Plot plot = player.getCurrentPlot();
if (plot == null) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
return false;
}
if (!plot.hasOwner()) {
player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
return false;
}
if (plot.getVolume() > Integer.MAX_VALUE) {
player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
return false;
}
if (!plot.isOwner(player.getUUID()) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMMAND_SAVE)) {
player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
return false;
}
if (plot.getRunning() > 0) {
player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
return false;
}
plot.addRunning();
this.schematicHandler.getCompoundTag(plot).whenComplete((compoundTag, throwable) -> {
TaskManager.runTaskAsync(() -> {
String time = (System.currentTimeMillis() / 1000) + "";
Location[] corners = plot.getCorners();
corners[0] = corners[0].withY(plot.getArea().getMinBuildHeight());
corners[1] = corners[1].withY(plot.getArea().getMaxBuildHeight());
int size = (corners[1].getX() - corners[0].getX()) + 1;
PlotId id = plot.getId();
String world1 = plot.getArea().toString().replaceAll(";", "-").replaceAll("[^A-Za-z0-9]", "");
final String file = time + '_' + world1 + '_' + id.getX() + '_' + id.getY() + '_' + size;
UUID uuid = player.getUUID();
schematicHandler.upload(compoundTag, uuid, file, new RunnableVal<>() {
@Override
public void run(URL url) {
plot.removeRunning();
if (url == null) {
player.sendMessage(TranslatableCaption.of("backups.backup_save_failed"));
return;
}
player.sendMessage(TranslatableCaption.of("web.save_success"));
player.sendMessage(TranslatableCaption.of("errors.deprecated_commands"), Template.of("replacement", "/plot download"));
try (final MetaDataAccess<List<String>> schematicAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_SCHEMATICS)) {
schematicAccess.get().ifPresent(schematics -> schematics.add(file + ".schem"));
}
}
});
});
});
return true;
}
use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.
the class Download method onCommand.
@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
String world = player.getLocation().getWorldName();
if (!this.plotAreaManager.hasPlotArea(world)) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
return false;
}
final Plot plot = player.getCurrentPlot();
if (plot == null) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
return false;
}
if (!plot.hasOwner()) {
player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
return false;
}
if ((Settings.Done.REQUIRED_FOR_DOWNLOAD && !DoneFlag.isDone(plot)) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMMAND_DOWNLOAD)) {
player.sendMessage(TranslatableCaption.of("done.done_not_done"));
return false;
}
if (!plot.isOwner(player.getUUID()) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN.toString())) {
player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
return false;
}
if (plot.getRunning() > 0) {
player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
return false;
}
if (args.length == 0 || (args.length == 1 && StringMan.isEqualIgnoreCaseToAny(args[0], "sch", "schem", "schematic"))) {
if (plot.getVolume() > Integer.MAX_VALUE) {
player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
return false;
}
plot.addRunning();
upload(player, plot);
} else if (args.length == 1 && StringMan.isEqualIgnoreCaseToAny(args[0], "mcr", "world", "mca")) {
if (!Permissions.hasPermission(player, Permission.PERMISSION_DOWNLOAD_WORLD)) {
player.sendMessage(TranslatableCaption.of("permission.no_permission"), Template.of("node", Permission.PERMISSION_DOWNLOAD_WORLD.toString()));
return false;
}
player.sendMessage(TranslatableCaption.of("schematics.mca_file_size"));
plot.addRunning();
this.worldUtil.saveWorld(world);
this.worldUtil.upload(plot, null, null, new RunnableVal<>() {
@Override
public void run(URL url) {
plot.removeRunning();
if (url == null) {
player.sendMessage(TranslatableCaption.of("web.generating_link_failed"), Template.of("plot", plot.getId().toString()));
return;
}
player.sendMessage(TranslatableCaption.of("web.generation_link_success_legacy_world"), Template.of("url", url.toString()));
}
});
} else {
sendUsage(player);
return false;
}
player.sendMessage(TranslatableCaption.of("web.generating_link"), Template.of("plot", plot.getId().toString()));
return true;
}
use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.
the class Inbox method onCommand.
@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
final Plot plot = player.getCurrentPlot();
if (plot == null) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
return false;
}
if (!plot.hasOwner()) {
player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
return false;
}
if (args.length == 0) {
sendUsage(player);
for (final CommentInbox inbox : CommentManager.inboxes.values()) {
if (inbox.canRead(plot, player)) {
if (!inbox.getComments(plot, new RunnableVal<>() {
@Override
public void run(List<PlotComment> value) {
if (value != null) {
int total = 0;
int unread = 0;
for (PlotComment comment : value) {
total++;
if (comment.timestamp > CommentManager.getTimestamp(player, inbox.toString())) {
unread++;
}
}
if (total != 0) {
player.sendMessage(TranslatableCaption.of("comment.inbox_item"), Template.of("value", inbox + " (" + total + '/' + unread + ')'));
return;
}
}
player.sendMessage(TranslatableCaption.of("comment.inbox_item"), Template.of("value", inbox.toString()));
}
})) {
player.sendMessage(TranslatableCaption.of("comment.inbox_item"), Template.of("value", inbox.toString()));
}
}
}
return false;
}
final CommentInbox inbox = CommentManager.inboxes.get(args[0].toLowerCase());
if (inbox == null) {
player.sendMessage(TranslatableCaption.of("comment.invalid_inbox"), Template.of("list", StringMan.join(CommentManager.inboxes.keySet(), ", ")));
return false;
}
final MetaDataKey<Long> metaDataKey = MetaDataKey.of(String.format("inbox:%s", inbox), new TypeLiteral<>() {
});
try (final MetaDataAccess<Long> metaDataAccess = player.accessTemporaryMetaData(metaDataKey)) {
metaDataAccess.set(System.currentTimeMillis());
}
final int page;
if (args.length > 1) {
switch(args[1].toLowerCase()) {
case "delete":
if (!inbox.canModify(plot, player)) {
player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox_modify"));
return false;
}
if (args.length != 3) {
player.sendMessage(TranslatableCaption.of("commandconfig.command_syntax"), Template.of("value", "/plot inbox " + inbox + " delete <index>"));
return true;
}
final int index;
try {
index = Integer.parseInt(args[2]);
if (index < 1) {
player.sendMessage(TranslatableCaption.of("comment.not_valid_inbox_index"), Templates.of("number", index));
return false;
}
} catch (NumberFormatException ignored) {
player.sendMessage(TranslatableCaption.of("commandconfig.command_syntax"), Template.of("value", "/plot inbox " + inbox + " delete <index>"));
return false;
}
if (!inbox.getComments(plot, new RunnableVal<>() {
@Override
public void run(List<PlotComment> value) {
if (index > value.size()) {
player.sendMessage(TranslatableCaption.of("comment.not_valid_inbox_index"), Templates.of("number", index));
return;
}
PlotComment comment = value.get(index - 1);
inbox.removeComment(plot, comment);
boolean success = plot.getPlotCommentContainer().removeComment(comment);
if (success) {
player.sendMessage(TranslatableCaption.of("comment.comment_removed_success"), Template.of("value", comment.comment));
} else {
player.sendMessage(TranslatableCaption.of("comment.comment_removed_failure"));
}
}
})) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
return false;
}
return true;
case "clear":
if (!inbox.canModify(plot, player)) {
player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox_modify"));
}
inbox.clearInbox(plot);
List<PlotComment> comments = plot.getPlotCommentContainer().getComments(inbox.toString());
if (!comments.isEmpty()) {
player.sendMessage(TranslatableCaption.of("comment.comment_removed_success"), Template.of("value", String.valueOf(comments)));
plot.getPlotCommentContainer().removeComments(comments);
}
return true;
default:
try {
page = Integer.parseInt(args[1]);
} catch (NumberFormatException ignored) {
sendUsage(player);
return false;
}
}
} else {
page = 1;
}
if (!inbox.canRead(plot, player)) {
player.sendMessage(TranslatableCaption.of("comment.no_perm_inbox"));
return false;
}
if (!inbox.getComments(plot, new RunnableVal<>() {
@Override
public void run(List<PlotComment> value) {
displayComments(player, value, page);
}
})) {
player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
return false;
}
return true;
}
use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.
the class Auto method claimSingle.
private void claimSingle(@NonNull final PlotPlayer<?> player, @NonNull final Plot plot, @NonNull final PlotArea plotArea, @Nullable final String schematic) {
try (final MetaDataAccess<Boolean> metaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_AUTO)) {
metaDataAccess.set(true);
}
plot.setOwnerAbs(player.getUUID());
final RunnableVal<Plot> runnableVal = new RunnableVal<>() {
{
this.value = plot;
}
@Override
public void run(final Plot plot) {
try {
TaskManager.getPlatformImplementation().sync(new AutoClaimFinishTask(player, plot, plotArea, schematic, PlotSquared.get().getEventDispatcher()));
} catch (final Exception e) {
e.printStackTrace();
}
}
};
DBFunc.createPlotSafe(plot, runnableVal, () -> claimSingle(player, plot, plotArea, schematic));
}
Aggregations