-
Notifications
You must be signed in to change notification settings - Fork 0
/
Region.java
144 lines (123 loc) · 4.49 KB
/
Region.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* This file is part of StickyChunk by DevOnTheRocks, licensed under GPL-3.0
*
* Copyright (C) 2017 DevOnTheRocks
*
* 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.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see http://www.gnu.org/licenses/.
*
* 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.
*
* The above notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package rocks.devonthe.stickychunk.world;
import org.spongepowered.api.Server;
import org.spongepowered.api.world.Chunk;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import rocks.devonthe.stickychunk.StickyChunk;
import rocks.devonthe.stickychunk.data.DataStore;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public class Region {
private List<Chunk> chunks = new ArrayList<>();
private Coordinate fromChunkPosition;
private Coordinate toChunkPosition;
private UUID worldId;
private int area;
private Server server = StickyChunk.getInstance().getGame().getServer();
private DataStore dataStore = StickyChunk.getInstance().getDataStore();
public Region(Coordinate from, Coordinate to, UUID world) {
this.fromChunkPosition = from;
chunks = new ArrayList<>();
this.toChunkPosition = to;
this.worldId = world;
}
public Region(Location<World> from, Location<World> to) {
fromChunkPosition = new Coordinate(from.getChunkPosition());
toChunkPosition = new Coordinate(to.getChunkPosition());
chunks = new ArrayList<>();
worldId = from.getExtent().getUniqueId();
}
public Region(Location<World> location) {
chunks = new ArrayList<>();
fromChunkPosition = toChunkPosition = new Coordinate(location.getChunkPosition());
worldId = location.getExtent().getUniqueId();
}
/***
* Get the chunks in-between the two positions used to create the region
* @return a list of chunks in the region
*/
public List<Chunk> getChunkRange() {
List<Chunk> chunks = new ArrayList<Chunk>();
int height, width, pointerX, pointerZ, lowestX, lowestZ, highestX, highestZ;
Optional<World> world = server.getWorld(getWorldId());
if (world.isPresent()) {
lowestX = Math.min(fromChunkPosition.getX(), toChunkPosition.getX());
lowestZ = Math.min(fromChunkPosition.getZ(), toChunkPosition.getZ());
highestX = Math.max(fromChunkPosition.getX(), toChunkPosition.getX());
highestZ = Math.max(fromChunkPosition.getZ(), toChunkPosition.getZ());
width = Math.abs(lowestX - highestX);
height = Math.abs(lowestZ - highestZ);
area = Math.abs((width + 1) * (height + 1));
pointerX = lowestX;
pointerZ = lowestZ;
for (int i = 0; i <= width; i++) {
for (int l = 0; l <= height; l++) {
world.get().getChunk(pointerX, 0, pointerZ).ifPresent(chunks::add);
pointerZ++;
}
pointerZ = lowestZ;
pointerX++;
}
}
return chunks;
}
/***
* Get the chunks in this region
* @return a list of chunks from within this regions area
*/
public List<Chunk> getChunks() {
if (chunks.isEmpty())
chunks = getChunkRange();
return chunks;
}
/***
* Get the position of the chunk this region starts at
* @return a Coordinate of the region start point
*/
public Coordinate getFrom() {
return fromChunkPosition;
}
/***
* Get the position of the chunk this regions ends at
* @return a Coordinate of the region end point
*/
public Coordinate getTo() {
return toChunkPosition;
}
/***
* Get the world ID this region is associated with
* @return the world ID this region uses
*/
public UUID getWorldId() {
return worldId;
}
}