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

135 lines
5.7 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-26 16:51:12 +02:00
package com.plotsquared.bukkit.util.bukkit;
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;
2015-07-27 19:50:04 +02:00
import org.bukkit.Chunk;
import java.util.Collection;
import java.util.HashMap;
import static com.intellectualcrafters.plot.util.ReflectionUtils.getRefClass;
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 {
2014-12-18 03:15:11 +01:00
private static final RefClass classBlock = getRefClass("{nms}.Block");
private static final RefClass classChunk = getRefClass("{nms}.Chunk");
private static final RefClass classWorld = getRefClass("{nms}.World");
2014-11-05 04:42:08 +01:00
private static final RefClass classCraftWorld = getRefClass("{cb}.CraftWorld");
2014-12-18 03:15:11 +01:00
private static RefMethod methodGetHandle;
private static RefMethod methodGetChunkAt;
private static RefMethod methodA;
private static RefMethod methodGetById;
public static 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);
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 {
SendChunk.sendChunk(chunks);
2014-12-18 03:15:11 +01:00
} catch (final Throwable e) {
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
}