PlotSquared/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SendChunk.java

85 lines
3.1 KiB
Java
Raw Normal View History

package com.intellectualcrafters.plot.util;
2014-12-16 06:03:20 +01:00
import static com.intellectualcrafters.plot.util.ReflectionUtils.getRefClass;
2014-11-21 23:45:46 +01:00
import java.util.ArrayList;
import java.util.List;
2014-12-16 06:03:20 +01:00
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import com.intellectualcrafters.plot.util.ReflectionUtils.RefClass;
import com.intellectualcrafters.plot.util.ReflectionUtils.RefConstructor;
import com.intellectualcrafters.plot.util.ReflectionUtils.RefField;
import com.intellectualcrafters.plot.util.ReflectionUtils.RefMethod;
2014-11-21 23:45:46 +01:00
/**
* An utility that can be used to send chunks, rather than
* using bukkit code to do so (uses heavy NMS)
*
* @author Empire92
*/
public class SendChunk {
2014-11-21 23:45:46 +01:00
// Ref Class
2014-12-16 06:03:20 +01:00
private static final RefClass classWorld = getRefClass("{nms}.World");
private static final RefClass classEntityPlayer = getRefClass("{nms}.EntityPlayer");
private static final RefClass classChunkCoordIntPair = getRefClass("{nms}.ChunkCoordIntPair");
2014-12-16 06:03:20 +01:00
private static final RefClass classCraftChunk = getRefClass("{cb}.CraftChunk");
private static final RefClass classChunk = getRefClass("{nms}.Chunk");
2014-11-21 23:45:46 +01:00
// Ref Method
2014-12-16 06:03:20 +01:00
private static RefMethod methodGetHandle;
2014-11-21 23:45:46 +01:00
// Ref Field
2014-12-16 06:03:20 +01:00
private static RefField chunkCoordIntPairQueue;
private static RefField players;
private static RefField locX;
private static RefField locZ;
private static RefField world;
2014-11-21 23:45:46 +01:00
// Ref Constructor
private static RefConstructor ChunkCoordIntPairCon;
2014-11-21 23:45:46 +01:00
/**
* Constructor
*
* @throws NoSuchMethodException
*/
public SendChunk() throws NoSuchMethodException {
methodGetHandle = classCraftChunk.getMethod("getHandle");
chunkCoordIntPairQueue = classEntityPlayer.getField("chunkCoordIntPairQueue");
2014-11-21 23:45:46 +01:00
players = classWorld.getField("players");
locX = classEntityPlayer.getField("locX");
locZ = classEntityPlayer.getField("locZ");
2014-11-21 23:45:46 +01:00
world = classChunk.getField("world");
2014-11-21 23:45:46 +01:00
ChunkCoordIntPairCon = classChunkCoordIntPair.getConstructor(int.class, int.class);
}
2014-11-21 23:45:46 +01:00
2014-12-16 06:03:20 +01:00
public static void sendChunk(final ArrayList<Chunk> chunks) {
int diffx, diffz;
2014-12-16 06:03:20 +01:00
final int view = Bukkit.getServer().getViewDistance() << 4;
for (final Chunk chunk : chunks) {
final Object c = methodGetHandle.of(chunk).call();
2014-11-21 23:45:46 +01:00
final Object w = world.of(c).get();
final Object p = players.of(w).get();
2014-11-21 23:45:46 +01:00
2014-12-16 06:03:20 +01:00
for (final Object ep : (List<Object>) p) {
final int x = ((Double) locX.of(ep).get()).intValue();
final int z = ((Double) locZ.of(ep).get()).intValue();
diffx = Math.abs(x - (chunk.getX() << 4));
diffz = Math.abs(z - (chunk.getZ() << 4));
2014-12-16 06:03:20 +01:00
if ((diffx <= view) && (diffz <= view)) {
final Object pair = ChunkCoordIntPairCon.create(chunk.getX(), chunk.getZ());
final Object pq = chunkCoordIntPairQueue.of(ep).get();
((List) pq).add(pair);
}
}
}
}
}