001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.command; 020 021import com.google.inject.Inject; 022import com.plotsquared.core.configuration.Settings; 023import com.plotsquared.core.configuration.caption.StaticCaption; 024import com.plotsquared.core.configuration.caption.TranslatableCaption; 025import com.plotsquared.core.permissions.Permission; 026import com.plotsquared.core.player.PlotPlayer; 027import com.plotsquared.core.plot.Plot; 028import com.plotsquared.core.plot.flag.implementations.DoneFlag; 029import com.plotsquared.core.plot.world.PlotAreaManager; 030import com.plotsquared.core.util.PlotUploader; 031import com.plotsquared.core.util.SchematicHandler; 032import com.plotsquared.core.util.StringMan; 033import com.plotsquared.core.util.TabCompletions; 034import com.plotsquared.core.util.WorldUtil; 035import com.plotsquared.core.util.task.RunnableVal; 036import net.kyori.adventure.text.minimessage.Template; 037import org.checkerframework.checker.nullness.qual.NonNull; 038 039import java.net.URL; 040import java.util.Collection; 041import java.util.Collections; 042import java.util.LinkedList; 043import java.util.List; 044import java.util.stream.Collectors; 045 046@CommandDeclaration(usage = "/plot download [schem | world]", 047 command = "download", 048 aliases = {"dl"}, 049 category = CommandCategory.SCHEMATIC, 050 requiredType = RequiredType.NONE, 051 permission = "plots.download") 052public class Download extends SubCommand { 053 054 private final PlotAreaManager plotAreaManager; 055 private final PlotUploader plotUploader; 056 @NonNull 057 private final SchematicHandler schematicHandler; 058 private final WorldUtil worldUtil; 059 060 @Inject 061 public Download( 062 final @NonNull PlotAreaManager plotAreaManager, 063 final @NonNull PlotUploader plotUploader, 064 final @NonNull SchematicHandler schematicHandler, 065 final @NonNull WorldUtil worldUtil 066 ) { 067 this.plotAreaManager = plotAreaManager; 068 this.plotUploader = plotUploader; 069 this.schematicHandler = schematicHandler; 070 this.worldUtil = worldUtil; 071 } 072 073 @Override 074 public boolean onCommand(final PlotPlayer<?> player, String[] args) { 075 String world = player.getLocation().getWorldName(); 076 if (!this.plotAreaManager.hasPlotArea(world)) { 077 player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world")); 078 return false; 079 } 080 final Plot plot = player.getCurrentPlot(); 081 if (plot == null) { 082 player.sendMessage(TranslatableCaption.of("errors.not_in_plot")); 083 return false; 084 } 085 if (!plot.hasOwner()) { 086 player.sendMessage(TranslatableCaption.of("info.plot_unowned")); 087 return false; 088 } 089 if ((Settings.Done.REQUIRED_FOR_DOWNLOAD && !DoneFlag.isDone(plot)) && !player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_DOWNLOAD)) { 090 player.sendMessage(TranslatableCaption.of("done.done_not_done")); 091 return false; 092 } 093 if (!plot.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN.toString())) { 094 player.sendMessage(TranslatableCaption.of("permission.no_plot_perms")); 095 return false; 096 } 097 if (plot.getRunning() > 0) { 098 player.sendMessage(TranslatableCaption.of("errors.wait_for_timer")); 099 return false; 100 } 101 if (args.length == 0 || (args.length == 1 && StringMan 102 .isEqualIgnoreCaseToAny(args[0], "sch", "schem", "schematic"))) { 103 if (plot.getVolume() > Integer.MAX_VALUE) { 104 player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large")); 105 return false; 106 } 107 plot.addRunning(); 108 upload(player, plot); 109 } else if (args.length == 1 && StringMan 110 .isEqualIgnoreCaseToAny(args[0], "mcr", "world", "mca")) { 111 if (!player.hasPermission(Permission.PERMISSION_DOWNLOAD_WORLD)) { 112 player.sendMessage( 113 TranslatableCaption.of("permission.no_permission"), 114 Template.of("node", Permission.PERMISSION_DOWNLOAD_WORLD.toString()) 115 ); 116 return false; 117 } 118 player.sendMessage(TranslatableCaption.of("schematics.mca_file_size")); 119 plot.addRunning(); 120 this.worldUtil.saveWorld(world); 121 this.worldUtil.upload(plot, null, null, new RunnableVal<>() { 122 @Override 123 public void run(URL url) { 124 plot.removeRunning(); 125 if (url == null) { 126 player.sendMessage( 127 TranslatableCaption.of("web.generating_link_failed"), 128 Template.of("plot", plot.getId().toString()) 129 ); 130 return; 131 } 132 player.sendMessage(TranslatableCaption.of("web.generation_link_success_legacy_world"), Template.of("url", url.toString())); 133 } 134 }); 135 } else { 136 sendUsage(player); 137 return false; 138 } 139 player.sendMessage(TranslatableCaption.of("web.generating_link"), Template.of("plot", plot.getId().toString())); 140 return true; 141 } 142 143 @Override 144 public Collection<Command> tab(final PlotPlayer<?> player, final String[] args, final boolean space) { 145 if (args.length == 1) { 146 final List<String> completions = new LinkedList<>(); 147 if (player.hasPermission(Permission.PERMISSION_DOWNLOAD)) { 148 completions.add("schem"); 149 } 150 if (player.hasPermission(Permission.PERMISSION_DOWNLOAD_WORLD)) { 151 completions.add("world"); 152 } 153 final List<Command> commands = completions.stream().filter(completion -> completion 154 .toLowerCase() 155 .startsWith(args[0].toLowerCase())) 156 .map(completion -> new Command( 157 null, 158 true, 159 completion, 160 "", 161 RequiredType.NONE, 162 CommandCategory.ADMINISTRATION 163 ) { 164 }).collect(Collectors.toCollection(LinkedList::new)); 165 if (player.hasPermission(Permission.PERMISSION_DOWNLOAD) && args[0].length() > 0) { 166 commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList())); 167 } 168 return commands; 169 } 170 return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList()); 171 } 172 173 private void upload(PlotPlayer<?> player, Plot plot) { 174 if (Settings.Web.LEGACY_WEBINTERFACE) { 175 schematicHandler 176 .getCompoundTag(plot) 177 .whenComplete((compoundTag, throwable) -> { 178 schematicHandler.upload(compoundTag, null, null, new RunnableVal<>() { 179 @Override 180 public void run(URL value) { 181 plot.removeRunning(); 182 player.sendMessage( 183 TranslatableCaption.of("web.generation_link_success"), 184 Template.of("download", value.toString()), 185 Template.of("delete", "Not available") 186 ); 187 player.sendMessage(StaticCaption.of(value.toString())); 188 } 189 }); 190 }); 191 return; 192 } 193 // TODO legacy support 194 this.plotUploader.upload(plot) 195 .whenComplete((result, throwable) -> { 196 if (throwable != null || !result.isSuccess()) { 197 player.sendMessage( 198 TranslatableCaption.of("web.generating_link_failed"), 199 Template.of("plot", plot.getId().toString()) 200 ); 201 } else { 202 player.sendMessage( 203 TranslatableCaption.of("web.generation_link_success"), 204 Template.of("download", result.getDownloadUrl()), 205 Template.of("delete", result.getDeletionUrl()) 206 ); 207 } 208 }); 209 } 210 211}