forked from alkarinv/BattleArena
-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix exception loading arena restoration module if WorldEdit is not in…
…stalled
- Loading branch information
Showing
2 changed files
with
62 additions
and
50 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...ration/src/main/java/org/battleplugins/arena/module/restoration/ArenaRestorationUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters