Skip to content

Commit

Permalink
Fix exception loading arena restoration module if WorldEdit is not in…
Browse files Browse the repository at this point in the history
…stalled
  • Loading branch information
Redned235 committed Dec 8, 2024
1 parent 53a7235 commit cca76a9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.battleplugins.arena.module.restoration;

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.session.ClipboardHolder;
import org.battleplugins.arena.Arena;
import org.battleplugins.arena.competition.LiveCompetition;
import org.battleplugins.arena.competition.map.options.Bounds;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

class ArenaRestorationUtil {

public static void restoreArena(ArenaRestoration module, Arena arena, LiveCompetition<?> competition, Bounds bounds) {
Path path = module.getSchematicPath(arena, competition);
if (Files.notExists(path)) {
// No schematic found
arena.getPlugin().warn("Could not restore map {} for arena {} as no schematic was found!", competition.getMap().getName(), arena.getName());
return;
}

// Restore the arena
Clipboard clipboard;
ClipboardFormat format = ClipboardFormats.findByFile(path.toFile());
if (format == null) {
// Invalid format
arena.getPlugin().warn("Could not restore map {} for arena {} as the schematic format is invalid!", competition.getMap().getName(), arena.getName());
return;
}

try (ClipboardReader reader = format.getReader(Files.newInputStream(path))) {
clipboard = reader.read();
} catch (IOException e) {
// Error reading schematic
arena.getPlugin().error("Failed to restore map {} for arena {} due to an error reading the schematic!", competition.getMap().getName(), arena.getName(), e);
return;
}

try (EditSession session = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(competition.getMap().getWorld()))) {
Operation operation = new ClipboardHolder(clipboard).createPaste(session)
.to(BlockVector3.at(bounds.getMinX(), bounds.getMinY(), bounds.getMinZ()))
.build();

Operations.complete(operation);
} catch (WorldEditException e) {
// Error restoring schematic
arena.getPlugin().error("Failed to restore map {} for arena {} due to an error restoring the schematic!", competition.getMap().getName(), arena.getName(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
package org.battleplugins.arena.module.restoration;

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.session.ClipboardHolder;
import org.battleplugins.arena.Arena;
import org.battleplugins.arena.ArenaPlayer;
import org.battleplugins.arena.competition.Competition;
Expand All @@ -20,9 +8,6 @@
import org.battleplugins.arena.event.action.EventAction;
import org.battleplugins.arena.resolver.Resolvable;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -58,41 +43,7 @@ public void postProcess(Arena arena, Competition<?> competition, Resolvable reso
return;
}

ArenaRestoration module = moduleOpt.get();
Path path = module.getSchematicPath(arena, competition);
if (Files.notExists(path)) {
// No schematic found
arena.getPlugin().warn("Could not restore map {} for arena {} as no schematic was found!", competition.getMap().getName(), arena.getName());
return;
}

// Restore the arena
Clipboard clipboard;
ClipboardFormat format = ClipboardFormats.findByFile(path.toFile());
if (format == null) {
// Invalid format
arena.getPlugin().warn("Could not restore map {} for arena {} as the schematic format is invalid!", competition.getMap().getName(), arena.getName());
return;
}

try (ClipboardReader reader = format.getReader(Files.newInputStream(path))) {
clipboard = reader.read();
} catch (IOException e) {
// Error reading schematic
arena.getPlugin().error("Failed to restore map {} for arena {} due to an error reading the schematic!", competition.getMap().getName(), arena.getName(), e);
return;
}

try (EditSession session = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(liveCompetition.getMap().getWorld()))) {
Operation operation = new ClipboardHolder(clipboard).createPaste(session)
.to(BlockVector3.at(bounds.getMinX(), bounds.getMinY(), bounds.getMinZ()))
.build();

Operations.complete(operation);
} catch (WorldEditException e) {
// Error restoring schematic
arena.getPlugin().error("Failed to restore map {} for arena {} due to an error restoring the schematic!", competition.getMap().getName(), arena.getName(), e);
}
ArenaRestorationUtil.restoreArena(moduleOpt.get(), arena, liveCompetition, bounds);
}

@Override
Expand Down

0 comments on commit cca76a9

Please sign in to comment.