Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -780,6 +781,24 @@ private void registerCommands() {
return Bukkit.getWorldContainer();
}

@Override
public @NonNull Path getWorldContainer(final String namespace) {
Path root = Bukkit.getWorldContainer().toPath();
if (MinecraftVersion.current().isOlderOrEqualThan(MinecraftVersion.TINY_TAKEOVER)) {
return root.resolve("world").resolve("dimensions").resolve(namespace);
}
return root;
}

@Override
public @NonNull Path getWorldPath(final String namespace, final String world) {
Path root = Bukkit.getWorldContainer().toPath();
if (MinecraftVersion.current().isOlderOrEqualThan(MinecraftVersion.TINY_TAKEOVER)) {
return root.resolve("world").resolve("dimensions").resolve(namespace).resolve(world);
}
return root.resolve(world);
}

@SuppressWarnings("deprecation")
private void runEntityTask() {
TaskManager.runTaskRepeat(() -> this.plotAreaManager.forEachPlotArea(plotArea -> {
Expand Down
37 changes: 36 additions & 1 deletion Core/src/main/java/com/plotsquared/core/PlotPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.File;
import java.nio.file.Path;

/**
* PlotSquared main utility class
Expand All @@ -67,10 +68,44 @@ public interface PlotPlatform<P> extends LocaleHolder {

/**
* Gets the folder where all world data is stored.
* For legacy versions this is the server root, for 26.1+ this is {@code ./world}
*
* @return the world folder
*/
@NonNull File worldContainer();
@NonNull
File worldContainer();

/**
* Gets the path to the dimension directory for the given namespace, e.g. {@code ./world/dimension/<namespace>}.
* <p>
* Legacy versions ignore the namespace and return the default path (by default the server root).
*
* @param namespace the dimension namespace
* @return a path to the dimension directory. may not exist.
*/
@NonNull
Path getWorldContainer(String namespace);

/**
* See {@link #getWorldPath(String, String)}. Uses the default {@code minecraft} namespace.
*
*/
@NonNull
default Path getWorldPath(String world) {
return getWorldPath("minecraft", world);
}

/**
* Gets the path to the world directory for the given namespace, e.g. {@code ./world/dimension/<namespace>/<world>}.
* <p>
* Legacy versions ignore the namespace (by default {@code ./<world>})
*
* @param namespace the dimension namespace
* @param world the name of the world in the given dimension
* @return a path to the world directory. may not exist.
*/
@NonNull
Path getWorldPath(String namespace, String world);

/**
* Completely shuts down the plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -61,6 +66,8 @@
usage = "/plot database [area] <sqlite | mysql | import>")
public class DatabaseCommand extends SubCommand {

private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + DatabaseCommand.class.getSimpleName());

private final PlotAreaManager plotAreaManager;
private final EventDispatcher eventDispatcher;
private final PlotListener plotListener;
Expand Down Expand Up @@ -93,7 +100,7 @@ public static void insertPlots(
});
} catch (Exception e) {
player.sendMessage(TranslatableCaption.of("database.conversion_failed"));
e.printStackTrace();
LOGGER.error("Database conversion failed", e);
}
});
}
Expand Down Expand Up @@ -171,18 +178,14 @@ public boolean onCommand(final PlotPlayer<?> player, String[] args) {
if (newPlot != null) {
PlotId newId = newPlot.getId();
PlotId id = plot.getId();
File worldFile =
new File(
PlotSquared.platform().worldContainer(),
id.toCommaSeparatedString()
);
if (worldFile.exists()) {
File newFile =
new File(
PlotSquared.platform().worldContainer(),
newId.toCommaSeparatedString()
);
worldFile.renameTo(newFile);
Path worldPath = PlotSquared.platform().getWorldPath(id.toCommaSeparatedString());
if (Files.exists(worldPath)) {
Path newPath = PlotSquared.platform().getWorldPath(newId.toCommaSeparatedString());
try {
Files.move(worldPath, newPath);
} catch (IOException e) {
LOGGER.error("Failed to rename world entry", e);
}
}
plot.setId(newId);
plot.setArea(pa);
Expand Down Expand Up @@ -257,15 +260,15 @@ public boolean onCommand(final PlotPlayer<?> player, String[] args) {
} catch (ClassNotFoundException | SQLException e) {
player.sendMessage(TranslatableCaption.of("database.failed_to_save_plots"));
player.sendMessage(TranslatableCaption.of("errors.stacktrace_begin"));
e.printStackTrace();
LOGGER.error("Inserting plots failed", e);
player.sendMessage(TranslatableCaption.of("errors.stacktrace_end"));
player.sendMessage(TranslatableCaption.of("database.invalid_args"));
return false;
}
} catch (ClassNotFoundException | SQLException e) {
player.sendMessage(TranslatableCaption.of("database.failed_to_open"));
player.sendMessage(TranslatableCaption.of("errors.stacktrace_begin"));
e.printStackTrace();
LOGGER.error("Opening database connection failed", e);
player.sendMessage(TranslatableCaption.of("errors.stacktrace_end"));
player.sendMessage(TranslatableCaption.of("database.invalid_args"));
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,38 @@
import com.google.common.base.Charsets;
import com.google.inject.Inject;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.caption.StaticCaption;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.PlotId;
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.plot.world.SinglePlotArea;
import com.plotsquared.core.plot.world.SinglePlotAreaManager;
import com.plotsquared.core.util.WorldUtil;
import com.plotsquared.core.util.task.RunnableVal2;
import com.plotsquared.core.util.task.RunnableVal3;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.stream.Stream;

@CommandDeclaration(command = "debugimportworlds",
permission = "plots.admin",
requiredType = RequiredType.CONSOLE,
category = CommandCategory.TELEPORT)
public class DebugImportWorlds extends Command {

private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + DebugImportWorlds.class.getSimpleName());

private final PlotAreaManager plotAreaManager;
private final WorldUtil worldUtil;

Expand All @@ -67,37 +78,65 @@ public CompletableFuture<Boolean> execute(
return CompletableFuture.completedFuture(false);
}
SinglePlotArea area = ((SinglePlotAreaManager) this.plotAreaManager).getArea();
PlotId id = PlotId.of(0, 0);
File container = PlotSquared.platform().worldContainer();
if (container.equals(new File("."))) {
Path container = PlotSquared.platform().getWorldContainer("minecraft");
if (container.equals(Path.of("."))) {
player.sendMessage(TranslatableCaption.of("debugimportworlds.world_container"));
return CompletableFuture.completedFuture(false);
}
for (File folder : container.listFiles()) {
String name = folder.getName();
if (!this.worldUtil.isWorld(name) && PlotId.fromStringOrNull(name) == null) {
UUID uuid;
if (name.length() > 16) {
uuid = UUID.fromString(name);
} else {
player.sendMessage(TranslatableCaption.of("players.fetching_player"));
uuid = PlotSquared.get().getImpromptuUUIDPipeline().getSingle(name, 60000L);
}
if (uuid == null) {
uuid =
UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
}
while (new File(container, id.toCommaSeparatedString()).exists()) {
id = id.getNextId();
}
File newDir = new File(container, id.toCommaSeparatedString());
if (folder.renameTo(newDir)) {
area.getPlot(id).setOwner(uuid);
}
}
try (Stream<Path> stream = Files.walk(container, 1)) {
stream.map(path -> new PathWithName(path, path.getFileName().toString()))
.filter(p -> !this.worldUtil.isWorld(p.name()))
.filter(p -> PlotId.fromStringOrNull(p.name()) == null)
.forEach(new ImportAction(player, area, container));
} catch (IOException e) {
LOGGER.error("Failed to import world", e);
throw new CommandException(StaticCaption.of("<red>World import failed. Check console.</red>"));
}
player.sendMessage(TranslatableCaption.of("players.done"));
return CompletableFuture.completedFuture(true);
}

private record PathWithName(Path path, String name) {

}

private static class ImportAction implements Consumer<PathWithName> {

private final PlotPlayer<?> player;
private final PlotArea area;
private final Path container;

private PlotId id = PlotId.of(0, 0);

private ImportAction(final PlotPlayer<?> player, final PlotArea area, final Path container) {
this.player = player;
this.area = area;
this.container = container;
}

@Override
public void accept(final PathWithName p) {
UUID uuid;
if (p.name().length() > 16) {
uuid = UUID.fromString(p.name());
} else {
this.player.sendMessage(TranslatableCaption.of("players.fetching_player"));
uuid = PlotSquared.get().getImpromptuUUIDPipeline().getSingle(p.name(), 60000L);
}
if (uuid == null) {
uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + p.name()).getBytes(Charsets.UTF_8));
}
Path target;
if (Files.exists(target = this.container.resolve(this.id.toCommaSeparatedString()))) {
this.id = this.id.getNextId();
}
try {
Files.move(p.path(), target);
Objects.requireNonNull(this.area.getPlot(this.id)).setOwner(uuid);
} catch (IOException ignored) {
}
}

}

}
Loading
Loading