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.common.base.Charsets; 022import com.google.inject.Inject; 023import com.plotsquared.core.PlotSquared; 024import com.plotsquared.core.configuration.caption.StaticCaption; 025import com.plotsquared.core.configuration.caption.TranslatableCaption; 026import com.plotsquared.core.player.PlotPlayer; 027import com.plotsquared.core.plot.PlotArea; 028import com.plotsquared.core.plot.PlotId; 029import com.plotsquared.core.plot.world.PlotAreaManager; 030import com.plotsquared.core.plot.world.SinglePlotArea; 031import com.plotsquared.core.plot.world.SinglePlotAreaManager; 032import com.plotsquared.core.util.WorldUtil; 033import com.plotsquared.core.util.task.RunnableVal2; 034import com.plotsquared.core.util.task.RunnableVal3; 035import org.apache.logging.log4j.LogManager; 036import org.apache.logging.log4j.Logger; 037import org.checkerframework.checker.nullness.qual.NonNull; 038 039import java.io.IOException; 040import java.nio.file.Files; 041import java.nio.file.Path; 042import java.util.Objects; 043import java.util.UUID; 044import java.util.concurrent.CompletableFuture; 045import java.util.function.Consumer; 046import java.util.function.Predicate; 047import java.util.stream.Stream; 048 049@CommandDeclaration(command = "debugimportworlds", 050 permission = "plots.admin", 051 requiredType = RequiredType.CONSOLE, 052 category = CommandCategory.TELEPORT) 053public class DebugImportWorlds extends Command { 054 055 private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + DebugImportWorlds.class.getSimpleName()); 056 057 private final PlotAreaManager plotAreaManager; 058 private final WorldUtil worldUtil; 059 060 @Inject 061 public DebugImportWorlds( 062 final @NonNull PlotAreaManager plotAreaManager, 063 final @NonNull WorldUtil worldUtil 064 ) { 065 super(MainCommand.getInstance(), true); 066 this.plotAreaManager = plotAreaManager; 067 this.worldUtil = worldUtil; 068 } 069 070 @Override 071 public CompletableFuture<Boolean> execute( 072 PlotPlayer<?> player, String[] args, 073 RunnableVal3<Command, Runnable, Runnable> confirm, 074 RunnableVal2<Command, CommandResult> whenDone 075 ) throws CommandException { 076 // UUID.nameUUIDFromBytes(("OfflinePlayer:" + player.getName()).getBytes(Charsets.UTF_8)) 077 if (!(this.plotAreaManager instanceof SinglePlotAreaManager)) { 078 player.sendMessage(TranslatableCaption.of("debugimportworlds.single_plot_area")); 079 return CompletableFuture.completedFuture(false); 080 } 081 SinglePlotArea area = ((SinglePlotAreaManager) this.plotAreaManager).getArea(); 082 Path container = PlotSquared.platform().worldContainer().toPath(); 083 if (WorldUtil.isModernServerLevelStructure()) { 084 container = container.resolve("dimensions").resolve("minecraft"); 085 } 086 if (container.equals(Path.of("."))) { 087 player.sendMessage(TranslatableCaption.of("debugimportworlds.world_container")); 088 return CompletableFuture.completedFuture(false); 089 } 090 final Path finalContainer = container; 091 try (Stream<Path> stream = Files.walk(finalContainer, 1)) { 092 stream.filter(Predicate.not(p -> p.equals(finalContainer))) // skip container / root 093 .map(path -> new PathWithName(path, path.getFileName().toString())) 094 .filter(p -> !this.worldUtil.isWorld(p.name())) 095 .filter(p -> PlotId.fromStringOrNull(p.name()) == null) 096 .forEach(new ImportAction(player, area, finalContainer)); 097 } catch (IOException e) { 098 LOGGER.error("Failed to import world", e); 099 throw new CommandException(StaticCaption.of("<red>World import failed. Check console.</red>")); 100 } 101 player.sendMessage(TranslatableCaption.of("players.done")); 102 return CompletableFuture.completedFuture(true); 103 } 104 105 /** 106 * A directory path paired with its file name, used while filtering candidate world folders for import. 107 */ 108 private record PathWithName(Path path, String name) { 109 110 } 111 112 private static class ImportAction implements Consumer<PathWithName> { 113 114 private final PlotPlayer<?> player; 115 private final PlotArea area; 116 private final Path container; 117 118 private PlotId id = PlotId.of(0, 0); 119 120 private ImportAction(final PlotPlayer<?> player, final PlotArea area, final Path container) { 121 this.player = player; 122 this.area = area; 123 this.container = container; 124 } 125 126 @Override 127 public void accept(final PathWithName p) { 128 UUID uuid; 129 if (p.name().length() > 16) { 130 uuid = UUID.fromString(p.name()); 131 } else { 132 this.player.sendMessage(TranslatableCaption.of("players.fetching_player")); 133 uuid = PlotSquared.get().getImpromptuUUIDPipeline().getSingle(p.name(), 60000L); 134 } 135 if (uuid == null) { 136 uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + p.name()).getBytes(Charsets.UTF_8)); 137 } 138 Path target; 139 while (Files.exists(target = this.container.resolve(this.id.toCommaSeparatedString()))) { 140 this.id = this.id.getNextId(); 141 } 142 try { 143 Files.move(p.path(), target); 144 Objects.requireNonNull(this.area.getPlot(this.id)).setOwner(uuid); 145 } catch (IOException e) { 146 LOGGER.error("Failed to import world: Failed to move world", e); 147 } 148 } 149 150 } 151 152}