2016-02-23 05:11:28 +01:00
|
|
|
package com.plotsquared.bukkit.util;
|
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
import static com.intellectualcrafters.plot.util.ReflectionUtils.getRefClass;
|
|
|
|
|
2016-02-23 05:11:28 +01:00
|
|
|
import com.intellectualcrafters.plot.PS;
|
|
|
|
import com.intellectualcrafters.plot.object.ChunkLoc;
|
|
|
|
import com.intellectualcrafters.plot.object.Location;
|
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
|
|
|
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;
|
|
|
|
import com.intellectualcrafters.plot.util.TaskManager;
|
|
|
|
import com.intellectualcrafters.plot.util.UUIDHandler;
|
|
|
|
import com.plotsquared.bukkit.object.BukkitPlayer;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Chunk;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
2016-02-23 05:11:28 +01:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
/**
|
2016-06-27 16:10:55 +02:00
|
|
|
* An utility that can be used to send chunks, rather than using bukkit code
|
|
|
|
* to do so (uses heavy NMS).
|
2016-02-23 05:11:28 +01:00
|
|
|
*/
|
|
|
|
public class SendChunk {
|
|
|
|
|
|
|
|
private final RefMethod methodGetHandlePlayer;
|
|
|
|
private final RefMethod methodGetHandleChunk;
|
2016-03-23 02:41:37 +01:00
|
|
|
private final RefConstructor mapChunk;
|
2016-02-23 05:11:28 +01:00
|
|
|
private final RefField connection;
|
|
|
|
private final RefMethod send;
|
|
|
|
private final RefMethod methodInitLighting;
|
|
|
|
|
|
|
|
/**
|
2016-06-27 16:10:55 +02:00
|
|
|
* Constructor.
|
2016-02-23 05:11:28 +01:00
|
|
|
*/
|
2016-04-30 00:14:12 +02:00
|
|
|
public SendChunk() throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException {
|
2016-05-29 04:37:56 +02:00
|
|
|
RefConstructor tempMapChunk;
|
2016-03-23 02:41:37 +01:00
|
|
|
RefClass classCraftPlayer = getRefClass("{cb}.entity.CraftPlayer");
|
|
|
|
this.methodGetHandlePlayer = classCraftPlayer.getMethod("getHandle");
|
|
|
|
RefClass classCraftChunk = getRefClass("{cb}.CraftChunk");
|
|
|
|
this.methodGetHandleChunk = classCraftChunk.getMethod("getHandle");
|
|
|
|
RefClass classChunk = getRefClass("{nms}.Chunk");
|
|
|
|
this.methodInitLighting = classChunk.getMethod("initLighting");
|
|
|
|
RefClass classMapChunk = getRefClass("{nms}.PacketPlayOutMapChunk");
|
2016-09-14 18:16:22 +02:00
|
|
|
if (PS.get().checkVersion(PS.get().IMP.getServerVersion(), BukkitVersion.v1_9_4)) {
|
2016-06-10 19:23:21 +02:00
|
|
|
//this works for 1.9.4 and 1.10
|
2016-05-29 04:37:56 +02:00
|
|
|
tempMapChunk = classMapChunk.getConstructor(classChunk.getRealClass(),int.class);
|
2016-05-11 17:16:38 +02:00
|
|
|
} else {
|
2016-05-29 04:37:56 +02:00
|
|
|
try {
|
|
|
|
tempMapChunk = classMapChunk.getConstructor(classChunk.getRealClass(), boolean.class, int.class);
|
|
|
|
} catch (NoSuchMethodException ignored) {
|
|
|
|
tempMapChunk = classMapChunk.getConstructor(classChunk.getRealClass(),boolean.class, int.class, int.class);
|
|
|
|
}
|
2016-05-11 17:16:38 +02:00
|
|
|
}
|
2016-05-29 04:37:56 +02:00
|
|
|
this.mapChunk = tempMapChunk;
|
2016-03-23 02:41:37 +01:00
|
|
|
RefClass classEntityPlayer = getRefClass("{nms}.EntityPlayer");
|
|
|
|
this.connection = classEntityPlayer.getField("playerConnection");
|
|
|
|
RefClass classPacket = getRefClass("{nms}.Packet");
|
|
|
|
RefClass classConnection = getRefClass("{nms}.PlayerConnection");
|
|
|
|
this.send = classConnection.getMethod("sendPacket", classPacket.getRealClass());
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public void sendChunk(Collection<Chunk> input) {
|
2016-05-12 23:09:35 +02:00
|
|
|
HashSet<Chunk> chunks = new HashSet<>(input);
|
2016-03-23 02:41:37 +01:00
|
|
|
HashMap<String, ArrayList<Chunk>> map = new HashMap<>();
|
|
|
|
int view = Bukkit.getServer().getViewDistance();
|
|
|
|
for (Chunk chunk : chunks) {
|
|
|
|
String world = chunk.getWorld().getName();
|
2016-02-23 05:11:28 +01:00
|
|
|
ArrayList<Chunk> list = map.get(world);
|
|
|
|
if (list == null) {
|
|
|
|
list = new ArrayList<>();
|
|
|
|
map.put(world, list);
|
|
|
|
}
|
|
|
|
list.add(chunk);
|
2016-03-23 02:41:37 +01:00
|
|
|
Object c = this.methodGetHandleChunk.of(chunk).call();
|
|
|
|
this.methodInitLighting.of(c).call();
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
for (Entry<String, PlotPlayer> entry : UUIDHandler.getPlayers().entrySet()) {
|
|
|
|
PlotPlayer pp = entry.getValue();
|
2016-03-23 02:41:37 +01:00
|
|
|
Plot plot = pp.getCurrentPlot();
|
2016-06-02 17:38:47 +02:00
|
|
|
Location location = null;
|
2016-02-23 05:11:28 +01:00
|
|
|
String world;
|
|
|
|
if (plot != null) {
|
2017-03-23 01:10:29 +01:00
|
|
|
world = plot.getWorldName();
|
2016-02-23 05:11:28 +01:00
|
|
|
} else {
|
2016-06-02 17:38:47 +02:00
|
|
|
location = pp.getLocation();
|
|
|
|
world = location.getWorld();
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
ArrayList<Chunk> list = map.get(world);
|
2016-02-23 05:11:28 +01:00
|
|
|
if (list == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-02 17:38:47 +02:00
|
|
|
if (location == null) {
|
|
|
|
location = pp.getLocation();
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
2016-06-02 17:38:47 +02:00
|
|
|
int cx = location.getX() >> 4;
|
|
|
|
int cz = location.getZ() >> 4;
|
2016-03-23 02:41:37 +01:00
|
|
|
Player player = ((BukkitPlayer) pp).player;
|
|
|
|
Object entity = this.methodGetHandlePlayer.of(player).call();
|
2016-02-23 05:11:28 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
for (Chunk chunk : list) {
|
|
|
|
int dx = Math.abs(cx - chunk.getX());
|
|
|
|
int dz = Math.abs(cz - chunk.getZ());
|
2016-02-23 05:11:28 +01:00
|
|
|
if ((dx > view) || (dz > view)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
Object c = this.methodGetHandleChunk.of(chunk).call();
|
2016-02-23 05:11:28 +01:00
|
|
|
chunks.remove(chunk);
|
2016-03-23 02:41:37 +01:00
|
|
|
Object con = this.connection.of(entity).get();
|
2016-05-29 04:37:56 +02:00
|
|
|
Object packet = null;
|
2016-09-14 18:16:22 +02:00
|
|
|
if (PS.get().checkVersion(PS.get().IMP.getServerVersion(), BukkitVersion.v1_9_4)) {
|
2016-05-29 04:37:56 +02:00
|
|
|
try {
|
|
|
|
packet = this.mapChunk.create(c,65535);
|
|
|
|
} catch (Exception ignored) {}
|
2016-05-11 17:16:38 +02:00
|
|
|
} else {
|
2016-05-29 04:37:56 +02:00
|
|
|
try {
|
|
|
|
packet = this.mapChunk.create(c, true, 65535);
|
|
|
|
} catch (ReflectiveOperationException | IllegalArgumentException e) {
|
|
|
|
try {
|
|
|
|
packet = this.mapChunk.create(c, true, 65535, 5);
|
|
|
|
} catch (ReflectiveOperationException | IllegalArgumentException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (packet == null) {
|
|
|
|
PS.debug("Error with PacketPlayOutMapChunk reflection.");
|
2016-05-11 17:16:38 +02:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
this.send.of(con).call(packet);
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (final Chunk chunk : chunks) {
|
|
|
|
TaskManager.runTask(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
chunk.unload(true, false);
|
2016-05-12 23:09:35 +02:00
|
|
|
} catch (Throwable ignored) {
|
2016-03-29 21:47:59 +02:00
|
|
|
String worldName = chunk.getWorld().getName();
|
2016-05-12 23:09:35 +02:00
|
|
|
PS.debug("$4Could not save chunk: " + worldName + ';' + chunk.getX() + ";" + chunk.getZ());
|
2016-02-23 05:11:28 +01:00
|
|
|
PS.debug("$3 - $4File may be open in another process (e.g. MCEdit)");
|
2016-03-29 21:47:59 +02:00
|
|
|
PS.debug("$3 - $4" + worldName + "/level.dat or " + worldName
|
2016-02-23 05:11:28 +01:00
|
|
|
+ "/level_old.dat may be corrupt (try repairing or removing these)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 21:47:59 +02:00
|
|
|
public void sendChunk(String worldName, Collection<ChunkLoc> chunkLocations) {
|
|
|
|
World myWorld = Bukkit.getWorld(worldName);
|
2016-03-23 02:41:37 +01:00
|
|
|
ArrayList<Chunk> chunks = new ArrayList<>();
|
2016-03-29 21:47:59 +02:00
|
|
|
for (ChunkLoc loc : chunkLocations) {
|
|
|
|
if (myWorld.isChunkLoaded(loc.x, loc.z)) {
|
|
|
|
chunks.add(myWorld.getChunkAt(loc.x, loc.z));
|
2016-02-25 15:17:07 +01:00
|
|
|
}
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
sendChunk(chunks);
|
|
|
|
}
|
|
|
|
}
|