-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
CuboidCopy.java
113 lines (100 loc) · 3.82 KB
/
CuboidCopy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* CraftBook Copyright (C) 2010-2018 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2018 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) Contributors
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not,
* see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.craftbook.sponge.mechanics.area.complex;
import com.sk89q.craftbook.sponge.CraftBookPlugin;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.world.DataException;
import org.spongepowered.api.world.World;
import java.io.File;
import java.io.IOException;
public abstract class CuboidCopy {
protected World world;
Vector origin;
protected Vector size;
int width;
int height;
protected int length;
CuboidCopy(Vector origin, Vector size, World world) {
this.origin = origin;
this.size = size;
this.world = world;
width = size.getBlockX();
height = size.getBlockY();
length = size.getBlockZ();
}
CuboidCopy() {
// used as constructor when file is loaded
}
/**
* Loads a cuboid copy from the given file. This acts as a factory selecting the right file type depending on the
* given File.
*
* @param file to load from
*
* @return loaded CuboidCopy
*
* @throws CuboidCopyException is thrown when loading error occured
*/
public static CuboidCopy load(File file, World world) throws CuboidCopyException {
// we need to split off the file extenstion to check what class we need to use
String extension = file.getName().substring(file.getName().lastIndexOf('.'));
CuboidCopy copy = null;
if (".schematic".equalsIgnoreCase(extension)) {
copy = new MCEditCuboidCopy(world);
}
if (copy == null) throw new CuboidCopyException("The file " + file.getAbsolutePath() + " does not exist.");
try {
copy.loadFromFile(file);
} catch (IOException | DataException e) {
CraftBookPlugin.spongeInst().getLogger().warn("Failed to load cuboid region: " + file.getAbsolutePath(), e);
throw new CuboidCopyException(e.getMessage());
}
// make sure that null is never returned but an exception is thrown instead
return copy;
}
/**
* Saves the cuboid to file.
*
* @param file to save to
*
* @throws IOException Thrown if there's an issue accessing the file
* @throws DataException Thrown if the data in the file is invalid
*/
protected abstract void save(File file) throws IOException, DataException;
/**
* Loads the cuboid from file. This method is for all sub classes.
*
* @param file to load from
*
* @throws IOException Thrown if there's an issue accessing the file
* @throws CuboidCopyException Thrown if the file is invalid
* @throws DataException Thrown if the file is invalid
*/
protected abstract void loadFromFile(File file) throws IOException, CuboidCopyException, DataException;
/**
* Pastes the cuboid copy into the world on its point of origin.
*/
public abstract void paste();
/**
* Clear the area.
*/
public abstract void clear();
/**
* Copies the cuboid from the world caching its state and blocks.
*/
public abstract void copy();
}