2020-04-16 04:52:39 +02:00
|
|
|
/*
|
|
|
|
* _____ _ _ _____ _
|
|
|
|
* | __ \| | | | / ____| | |
|
|
|
|
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
|
|
|
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
|
|
|
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
|
|
|
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
|
|
|
* | |
|
|
|
|
* |_|
|
|
|
|
* PlotSquared plot management system for Minecraft
|
|
|
|
* Copyright (C) 2020 IntellectualSites
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-04-15 21:26:54 +02:00
|
|
|
package com.plotsquared.bukkit.queue;
|
2016-02-10 19:59:51 +01:00
|
|
|
|
2020-04-30 12:01:52 +02:00
|
|
|
import com.google.common.base.Preconditions;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.bukkit.util.BukkitBlockUtil;
|
2020-04-11 02:19:18 +02:00
|
|
|
import com.plotsquared.bukkit.util.BukkitUtil;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.location.ChunkWrapper;
|
|
|
|
import com.plotsquared.core.location.Location;
|
2020-07-17 16:41:06 +02:00
|
|
|
import com.plotsquared.core.queue.ScopedQueueCoordinator;
|
2020-04-30 12:01:52 +02:00
|
|
|
import com.plotsquared.core.util.MainUtil;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.util.PatternUtil;
|
2018-12-26 17:24:35 +01:00
|
|
|
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
2019-11-10 18:47:37 +01:00
|
|
|
import com.sk89q.worldedit.function.pattern.Pattern;
|
|
|
|
import com.sk89q.worldedit.world.biome.BiomeType;
|
2018-12-26 17:05:37 +01:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
2019-11-04 20:58:24 +01:00
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
2019-11-10 18:47:37 +01:00
|
|
|
import com.sk89q.worldedit.world.block.BlockTypes;
|
2018-12-20 13:23:49 +01:00
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
2020-07-17 16:41:06 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2016-02-10 19:59:51 +01:00
|
|
|
import org.bukkit.Chunk;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.block.Biome;
|
|
|
|
import org.bukkit.generator.ChunkGenerator.BiomeGrid;
|
|
|
|
import org.bukkit.generator.ChunkGenerator.ChunkData;
|
|
|
|
|
2020-07-17 16:41:06 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2018-08-10 17:01:10 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
2020-07-17 16:41:06 +02:00
|
|
|
public class GenChunk extends ScopedQueueCoordinator {
|
2016-02-10 19:59:51 +01:00
|
|
|
|
2016-02-14 02:01:18 +01:00
|
|
|
public final Biome[] biomes;
|
2019-11-04 20:55:55 +01:00
|
|
|
public BlockState[][] result;
|
2019-02-15 18:50:43 +01:00
|
|
|
public BiomeGrid biomeGrid;
|
2016-06-13 06:47:50 +02:00
|
|
|
public Chunk chunk;
|
|
|
|
public String world;
|
2019-05-14 03:57:41 +02:00
|
|
|
public int chunkX;
|
|
|
|
public int chunkZ;
|
2019-05-17 20:38:57 +02:00
|
|
|
@Getter @Setter private ChunkData chunkData = null;
|
2016-06-13 06:47:50 +02:00
|
|
|
|
2019-03-26 09:27:20 +01:00
|
|
|
public GenChunk() {
|
2020-07-10 12:21:29 +02:00
|
|
|
super(null, Location.at("", 0, 0, 0), Location.at("", 15, 255, 15));
|
2016-02-10 19:59:51 +01:00
|
|
|
this.biomes = Biome.values();
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-06-13 06:47:50 +02:00
|
|
|
public Chunk getChunk() {
|
|
|
|
if (chunk == null) {
|
|
|
|
World worldObj = BukkitUtil.getWorld(world);
|
|
|
|
if (worldObj != null) {
|
2019-05-14 03:57:41 +02:00
|
|
|
this.chunk = worldObj.getChunkAt(chunkX, chunkZ);
|
2016-06-13 06:47:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
2019-04-24 00:48:22 +02:00
|
|
|
public void setChunk(Chunk chunk) {
|
|
|
|
this.chunk = chunk;
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:01:10 +02:00
|
|
|
public void setChunk(ChunkWrapper wrap) {
|
|
|
|
chunk = null;
|
|
|
|
world = wrap.world;
|
2019-05-14 03:57:41 +02:00
|
|
|
chunkX = wrap.x;
|
|
|
|
chunkZ = wrap.z;
|
2018-08-10 17:01:10 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 18:47:37 +01:00
|
|
|
@Override public void fillBiome(BiomeType biomeType) {
|
2019-02-15 18:50:43 +01:00
|
|
|
if (biomeGrid == null) {
|
2016-06-13 06:47:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-11-10 18:47:37 +01:00
|
|
|
Biome biome = BukkitAdapter.adapt(biomeType);
|
2016-06-18 05:15:50 +02:00
|
|
|
for (int x = 0; x < 16; x++) {
|
|
|
|
for (int z = 0; z < 16; z++) {
|
2019-02-15 18:50:43 +01:00
|
|
|
this.biomeGrid.setBiome(x, z, biome);
|
2016-06-13 06:47:50 +02:00
|
|
|
}
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 20:55:55 +01:00
|
|
|
@Override public void setCuboid(Location pos1, Location pos2, BlockState block) {
|
2018-12-20 13:23:49 +01:00
|
|
|
if (result != null && pos1.getX() == 0 && pos1.getZ() == 0 && pos2.getX() == 15
|
|
|
|
&& pos2.getZ() == 15) {
|
2017-09-25 08:57:42 +02:00
|
|
|
for (int y = pos1.getY(); y <= pos2.getY(); y++) {
|
|
|
|
int layer = y >> 4;
|
2019-11-04 20:55:55 +01:00
|
|
|
BlockState[] data = result[layer];
|
2017-09-25 08:57:42 +02:00
|
|
|
if (data == null) {
|
2019-11-04 20:55:55 +01:00
|
|
|
result[layer] = data = new BlockState[4096];
|
2017-09-25 08:57:42 +02:00
|
|
|
}
|
|
|
|
int start = y << 8;
|
|
|
|
int end = start + 256;
|
2018-12-20 02:23:48 +01:00
|
|
|
Arrays.fill(data, start, end, block);
|
2017-09-25 08:57:42 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-14 03:57:41 +02:00
|
|
|
int minX = Math.min(pos1.getX(), pos2.getX());
|
|
|
|
int minY = Math.min(pos1.getY(), pos2.getY());
|
|
|
|
int minZ = Math.min(pos1.getZ(), pos2.getZ());
|
|
|
|
int maxX = Math.max(pos1.getX(), pos2.getX());
|
|
|
|
int maxY = Math.max(pos1.getY(), pos2.getY());
|
|
|
|
int maxZ = Math.max(pos1.getZ(), pos2.getZ());
|
2020-04-30 12:01:52 +02:00
|
|
|
chunkData
|
|
|
|
.setRegion(minX, minY, minZ, maxX + 1, maxY + 1, maxZ + 1, BukkitAdapter.adapt(block));
|
2017-09-25 08:57:42 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 18:47:37 +01:00
|
|
|
@Override public boolean setBiome(int x, int z, BiomeType biomeType) {
|
|
|
|
return setBiome(x, z, BukkitAdapter.adapt(biomeType));
|
2016-06-13 06:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setBiome(int x, int z, Biome biome) {
|
2019-02-15 18:50:43 +01:00
|
|
|
if (this.biomeGrid != null) {
|
|
|
|
this.biomeGrid.setBiome(x, z, biome);
|
2016-06-13 06:47:50 +02:00
|
|
|
return true;
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2016-06-13 06:47:50 +02:00
|
|
|
return false;
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2020-07-14 18:49:40 +02:00
|
|
|
@Override public boolean setBlock(int x, int y, int z, @Nonnull Pattern pattern) {
|
2020-04-30 12:01:52 +02:00
|
|
|
return setBlock(x, y, z, PatternUtil
|
|
|
|
.apply(Preconditions.checkNotNull(pattern, "Pattern may not be null"), x, y, z));
|
2019-11-10 18:47:37 +01:00
|
|
|
}
|
|
|
|
|
2019-11-04 20:55:55 +01:00
|
|
|
@Override public boolean setBlock(int x, int y, int z, BlockState id) {
|
2016-03-23 02:41:37 +01:00
|
|
|
if (this.result == null) {
|
2019-11-04 20:55:55 +01:00
|
|
|
this.chunkData.setBlock(x, y, z, BukkitAdapter.adapt(id));
|
2016-06-13 06:47:50 +02:00
|
|
|
return true;
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2019-11-04 20:55:55 +01:00
|
|
|
this.chunkData.setBlock(x, y, z, BukkitAdapter.adapt(id));
|
2018-12-26 17:24:35 +01:00
|
|
|
this.storeCache(x, y, z, id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-04 20:55:55 +01:00
|
|
|
private void storeCache(final int x, final int y, final int z, final BlockState id) {
|
2016-02-10 19:59:51 +01:00
|
|
|
int i = MainUtil.CACHE_I[y][x][z];
|
2019-11-04 20:55:55 +01:00
|
|
|
BlockState[] v = this.result[i];
|
2016-02-10 19:59:51 +01:00
|
|
|
if (v == null) {
|
2019-11-04 20:55:55 +01:00
|
|
|
this.result[i] = v = new BlockState[4096];
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2016-03-02 07:42:04 +01:00
|
|
|
int j = MainUtil.CACHE_J[y][x][z];
|
2018-12-20 02:23:48 +01:00
|
|
|
v[j] = id;
|
2016-06-13 06:47:50 +02:00
|
|
|
}
|
|
|
|
|
2018-12-26 17:05:37 +01:00
|
|
|
@Override public boolean setBlock(int x, int y, int z, BaseBlock id) {
|
2018-12-26 17:24:35 +01:00
|
|
|
if (this.result == null) {
|
2019-05-17 20:38:57 +02:00
|
|
|
this.chunkData.setBlock(x, y, z, BukkitAdapter.adapt(id));
|
2018-12-26 17:24:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-17 20:38:57 +02:00
|
|
|
this.chunkData.setBlock(x, y, z, BukkitAdapter.adapt(id));
|
2019-11-10 18:47:37 +01:00
|
|
|
this.storeCache(x, y, z, id.toImmutableState());
|
2018-12-26 17:24:35 +01:00
|
|
|
return true;
|
2018-12-26 17:05:37 +01:00
|
|
|
}
|
|
|
|
|
2019-11-04 20:55:55 +01:00
|
|
|
@Override public BlockState getBlock(int x, int y, int z) {
|
2016-06-13 06:47:50 +02:00
|
|
|
int i = MainUtil.CACHE_I[y][x][z];
|
|
|
|
if (result == null) {
|
2019-11-04 20:55:55 +01:00
|
|
|
return BukkitBlockUtil.get(chunkData.getType(x, y, z));
|
2016-06-13 06:47:50 +02:00
|
|
|
}
|
2019-11-04 20:55:55 +01:00
|
|
|
BlockState[] array = result[i];
|
2016-06-13 06:47:50 +02:00
|
|
|
if (array == null) {
|
2019-11-10 18:47:37 +01:00
|
|
|
return BlockTypes.AIR.getDefaultState();
|
2016-06-13 06:47:50 +02:00
|
|
|
}
|
|
|
|
int j = MainUtil.CACHE_J[y][x][z];
|
2018-12-20 02:23:48 +01:00
|
|
|
return array[j];
|
2016-06-13 06:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getX() {
|
2019-05-14 03:57:41 +02:00
|
|
|
return chunk == null ? chunkX : chunk.getX();
|
2016-06-13 06:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getZ() {
|
2019-05-14 03:57:41 +02:00
|
|
|
return chunk == null ? chunkZ : chunk.getZ();
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2020-07-17 16:41:06 +02:00
|
|
|
@Override public com.sk89q.worldedit.world.World getWorld() {
|
|
|
|
return chunk == null ?
|
|
|
|
BukkitAdapter.adapt(Bukkit.getWorld(world)) :
|
|
|
|
BukkitAdapter.adapt(chunk.getWorld());
|
2016-06-13 06:47:50 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 17:01:10 +02:00
|
|
|
@Override public Location getMax() {
|
2020-07-17 16:41:06 +02:00
|
|
|
return Location.at(getWorld().getName(), 15 + (getX() << 4), 255, 15 + (getZ() << 4));
|
2016-06-13 06:47:50 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 17:01:10 +02:00
|
|
|
@Override public Location getMin() {
|
2020-07-17 16:41:06 +02:00
|
|
|
return Location.at(getWorld().getName(), getX() << 4, 0, getZ() << 4);
|
2016-06-13 06:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public GenChunk clone() {
|
2019-03-26 09:27:20 +01:00
|
|
|
GenChunk toReturn = new GenChunk();
|
2016-03-23 02:41:37 +01:00
|
|
|
if (this.result != null) {
|
|
|
|
for (int i = 0; i < this.result.length; i++) {
|
2019-11-04 20:55:55 +01:00
|
|
|
BlockState[] matrix = this.result[i];
|
2016-02-10 19:59:51 +01:00
|
|
|
if (matrix != null) {
|
2019-11-04 20:55:55 +01:00
|
|
|
toReturn.result[i] = new BlockState[matrix.length];
|
2016-02-10 19:59:51 +01:00
|
|
|
System.arraycopy(matrix, 0, toReturn.result[i], 0, matrix.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-17 20:38:57 +02:00
|
|
|
toReturn.chunkData = this.chunkData;
|
2016-02-10 19:59:51 +01:00
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
}
|