PlotSquared/src/main/java/com/plotsquared/bukkit/util/SetBlockFast.java

139 lines
5.8 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// 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, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
2015-07-30 19:24:01 +02:00
package com.plotsquared.bukkit.util;
2015-07-30 16:25:16 +02:00
import static com.intellectualcrafters.plot.util.ReflectionUtils.getRefClass;
import java.util.Collection;
import java.util.HashMap;
import org.bukkit.Chunk;
import com.intellectualcrafters.plot.object.ChunkLoc;
2015-02-20 12:23:48 +01:00
import com.intellectualcrafters.plot.util.MainUtil;
2015-01-13 17:38:15 +01:00
import com.intellectualcrafters.plot.util.ReflectionUtils.RefClass;
import com.intellectualcrafters.plot.util.ReflectionUtils.RefMethod;
import com.intellectualcrafters.plot.util.TaskManager;
2014-09-22 13:02:14 +02:00
2014-09-25 08:39:13 +02:00
/**
2014-12-18 03:15:11 +01:00
* SetBlockFast class<br> Used to do fast world editing
2014-11-20 00:00:38 +01:00
*
* @author Empire92
2014-09-25 08:39:13 +02:00
*/
2015-02-23 06:29:45 +01:00
public class SetBlockFast extends BukkitSetBlockManager {
2015-08-03 20:20:04 +02:00
private final RefClass classBlock = getRefClass("{nms}.Block");
private final RefClass classChunk = getRefClass("{nms}.Chunk");
private final RefClass classWorld = getRefClass("{nms}.World");
private final RefClass classCraftWorld = getRefClass("{cb}.CraftWorld");
private RefMethod methodGetHandle;
private RefMethod methodGetChunkAt;
private RefMethod methodA;
private RefMethod methodGetById;
private SendChunk chunksender;
2015-08-03 20:20:04 +02:00
public HashMap<ChunkLoc, Chunk> toUpdate = new HashMap<>();
2015-02-23 02:32:27 +01:00
2014-11-20 00:00:38 +01:00
/**
* Constructor
*
* @throws NoSuchMethodException
*/
2014-11-05 04:42:08 +01:00
public SetBlockFast() throws NoSuchMethodException {
methodGetHandle = classCraftWorld.getMethod("getHandle");
methodGetChunkAt = classWorld.getMethod("getChunkAt", int.class, int.class);
methodA = classChunk.getMethod("a", int.class, int.class, int.class, classBlock, int.class);
methodGetById = classBlock.getMethod("getById", int.class);
TaskManager.runTaskRepeat(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
update(toUpdate.values());
toUpdate = new HashMap<>();
}
}, 20);
2015-08-03 20:20:04 +02:00
this.chunksender = new SendChunk();
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2015-04-02 02:56:34 +02:00
private ChunkLoc lastLoc = null;
2014-11-20 00:00:38 +01:00
/**
* Set the block at the location
2014-12-16 06:03:20 +01:00
*
2014-12-18 03:15:11 +01:00
* @param world World in which the block should be set
* @param x X Coordinate
* @param y Y Coordinate
* @param z Z Coordinate
* @param blockId Block ID
* @param data Block Data Value
*
2014-11-20 00:00:38 +01:00
*/
2015-02-11 16:37:52 +01:00
@Override
2015-02-19 04:24:05 +01:00
public void set(final org.bukkit.World world, final int x, final int y, final int z, final int blockId, final byte data) {
if (blockId == -1) {
world.getBlockAt(x, y, z).setData(data, false);
return;
}
2015-04-02 02:56:34 +02:00
int X = x >> 4;
int Z = z >> 4;
ChunkLoc loc = new ChunkLoc(X, Z);
if (!loc.equals(lastLoc)) {
Chunk chunk = toUpdate.get(loc);
if (chunk == null) {
chunk = world.getChunkAt(X, Z);
toUpdate.put(loc, chunk);
}
chunk.load(false);
}
2014-11-05 04:42:08 +01:00
final Object w = methodGetHandle.of(world).call();
final Object chunk = methodGetChunkAt.of(w).call(x >> 4, z >> 4);
final Object block = methodGetById.of(null).call(blockId);
methodA.of(chunk).call(x & 0x0f, y, z & 0x0f, block, data);
}
2015-02-23 02:32:27 +01:00
2014-11-20 00:00:38 +01:00
/**
* Update chunks
2014-12-16 06:03:20 +01:00
*
2015-03-13 08:02:48 +01:00
* @param chunks list of chunks to update
2014-11-20 00:00:38 +01:00
*/
2015-02-11 16:37:52 +01:00
@Override
public void update(final Collection<Chunk> chunks) {
2015-02-11 16:37:52 +01:00
if (chunks.size() == 0) {
return;
}
2015-02-20 12:23:48 +01:00
if (!MainUtil.canSendChunk) {
2015-04-02 02:56:34 +02:00
for (Chunk chunk : chunks) {
chunk.getWorld().refreshChunk(chunk.getX(), chunk.getZ());
2015-06-09 22:00:53 +02:00
chunk.unload(true, false);
chunk.load();
}
return;
}
try {
2015-08-03 20:20:04 +02:00
chunksender.sendChunk(chunks);
2014-12-18 03:15:11 +01:00
} catch (final Throwable e) {
2015-08-23 14:04:17 +02:00
e.printStackTrace();
2015-02-20 12:23:48 +01:00
MainUtil.canSendChunk = false;
}
2014-11-05 04:42:08 +01:00
}
2014-09-22 13:02:14 +02:00
}