2016-04-29 23:24:59 +02:00
|
|
|
package com.boydti.fawe.bukkit.v1_8;
|
|
|
|
|
|
|
|
import com.boydti.fawe.Fawe;
|
|
|
|
import com.boydti.fawe.FaweCache;
|
|
|
|
import com.boydti.fawe.bukkit.v0.BukkitQueue_0;
|
|
|
|
import com.boydti.fawe.example.CharFaweChunk;
|
2016-05-06 00:59:21 +02:00
|
|
|
import com.boydti.fawe.object.BytePair;
|
2016-04-29 23:24:59 +02:00
|
|
|
import com.boydti.fawe.object.FaweChunk;
|
|
|
|
import com.boydti.fawe.object.PseudoRandom;
|
2016-05-06 00:59:21 +02:00
|
|
|
import com.boydti.fawe.object.RunnableVal;
|
2016-05-19 09:41:55 +02:00
|
|
|
import com.boydti.fawe.util.MainUtil;
|
2016-05-06 00:59:21 +02:00
|
|
|
import com.boydti.fawe.util.MathMan;
|
|
|
|
import com.boydti.fawe.util.ReflectionUtils;
|
2016-05-20 10:55:26 +02:00
|
|
|
import com.boydti.fawe.util.TaskManager;
|
2016-05-06 00:59:21 +02:00
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
|
|
|
import com.sk89q.jnbt.ListTag;
|
|
|
|
import com.sk89q.jnbt.StringTag;
|
|
|
|
import com.sk89q.jnbt.Tag;
|
|
|
|
import com.sk89q.worldedit.internal.Constants;
|
2016-04-29 23:24:59 +02:00
|
|
|
import java.lang.reflect.Field;
|
2016-05-06 00:59:21 +02:00
|
|
|
import java.util.ArrayList;
|
2016-04-29 23:24:59 +02:00
|
|
|
import java.util.Collection;
|
2016-05-12 19:46:38 +02:00
|
|
|
import java.util.HashSet;
|
2016-04-29 23:24:59 +02:00
|
|
|
import java.util.Iterator;
|
2016-05-20 10:55:26 +02:00
|
|
|
import java.util.List;
|
2016-04-29 23:24:59 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2016-05-12 19:46:38 +02:00
|
|
|
import java.util.UUID;
|
2016-04-29 23:24:59 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.BlockPosition;
|
|
|
|
import net.minecraft.server.v1_8_R3.ChunkCoordIntPair;
|
|
|
|
import net.minecraft.server.v1_8_R3.ChunkSection;
|
2016-05-06 00:59:21 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.Entity;
|
2016-05-12 19:46:38 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.EntityPlayer;
|
2016-05-20 10:55:26 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.EntityTracker;
|
|
|
|
import net.minecraft.server.v1_8_R3.EntityTrackerEntry;
|
2016-05-06 00:59:21 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.EntityTypes;
|
2016-05-20 10:55:26 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.LongHashMap;
|
2016-05-06 00:59:21 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.NBTTagCompound;
|
2016-05-12 19:46:38 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.NibbleArray;
|
2016-05-20 10:55:26 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy;
|
2016-04-29 23:24:59 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.PacketPlayOutMapChunk;
|
2016-05-20 10:55:26 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.PlayerChunkMap;
|
2016-04-29 23:24:59 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.TileEntity;
|
2016-05-20 10:55:26 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.WorldServer;
|
2016-04-29 23:24:59 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Chunk;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.craftbukkit.v1_8_R3.CraftChunk;
|
2016-05-06 00:59:21 +02:00
|
|
|
import org.bukkit.event.entity.CreatureSpawnEvent;
|
2016-04-29 23:24:59 +02:00
|
|
|
|
|
|
|
public class BukkitQueue18R3 extends BukkitQueue_0<Chunk, ChunkSection[], char[]> {
|
|
|
|
|
|
|
|
public BukkitQueue18R3(final String world) {
|
|
|
|
super(world);
|
2016-05-13 10:19:04 +02:00
|
|
|
checkVersion("v1_8_R3");
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isChunkLoaded(int x, int z) {
|
|
|
|
return getWorld().isChunkLoaded(x, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
public World getWorld(String world) {
|
|
|
|
return Bukkit.getWorld(world);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean regenerateChunk(World world, int x, int z) {
|
|
|
|
return world.regenerateChunk(x, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean loadChunk(World world, int x, int z, boolean generate) {
|
2016-05-19 09:41:55 +02:00
|
|
|
return getCachedSections(world, x, z) != null;
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-19 09:41:55 +02:00
|
|
|
public ChunkSection[] getCachedSections(World world, int x, int z) {
|
2016-04-29 23:24:59 +02:00
|
|
|
Chunk chunk = world.getChunkAt(x, z);
|
|
|
|
if (chunk == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!chunk.isLoaded()) {
|
|
|
|
chunk.load(true);
|
|
|
|
}
|
|
|
|
return ((CraftChunk) chunk).getHandle().getSections();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCombinedId4Data(char[] ls, int x, int y, int z) {
|
|
|
|
return ls[FaweCache.CACHE_J[y][x & 15][z & 15]];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isChunkLoaded(World world, int x, int z) {
|
|
|
|
return world.isChunkLoaded(x, z);
|
|
|
|
}
|
|
|
|
|
2016-05-02 05:06:23 +02:00
|
|
|
@Override
|
|
|
|
public char[] getCachedSection(ChunkSection[] chunkSections, int cy) {
|
|
|
|
ChunkSection section = chunkSections[cy];
|
|
|
|
return section == null ? null : section.getIdArray();
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:46:38 +02:00
|
|
|
@Override
|
|
|
|
public CharFaweChunk getPrevious(CharFaweChunk fs, ChunkSection[] sections, Map<?, ?> tilesGeneric, Collection<?>[] entitiesGeneric, Set<UUID> createdEntities, boolean all) throws Exception {
|
|
|
|
Map<BlockPosition, TileEntity> tiles = (Map<BlockPosition, TileEntity>) tilesGeneric;
|
|
|
|
Collection<Entity>[] entities = (Collection<Entity>[]) entitiesGeneric;
|
2016-05-19 09:41:55 +02:00
|
|
|
CharFaweChunk previous = (CharFaweChunk) getFaweChunk(fs.getX(), fs.getZ());
|
2016-05-12 19:46:38 +02:00
|
|
|
char[][] idPrevious = new char[16][];
|
|
|
|
for (int layer = 0; layer < sections.length; layer++) {
|
|
|
|
if (fs.getCount(layer) != 0 || all) {
|
|
|
|
ChunkSection section = sections[layer];
|
|
|
|
if (section != null) {
|
|
|
|
idPrevious[layer] = section.getIdArray().clone();
|
|
|
|
short solid = 0;
|
|
|
|
for (int combined : idPrevious[layer]) {
|
|
|
|
if (combined > 1) {
|
|
|
|
solid++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
previous.count[layer] = solid;
|
|
|
|
previous.air[layer] = (short) (4096 - solid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
previous.ids = idPrevious;
|
|
|
|
if (tiles != null) {
|
|
|
|
for (Map.Entry<BlockPosition, TileEntity> entry : tiles.entrySet()) {
|
|
|
|
TileEntity tile = entry.getValue();
|
|
|
|
NBTTagCompound tag = new NBTTagCompound();
|
|
|
|
BlockPosition pos = entry.getKey();
|
2016-05-19 09:41:55 +02:00
|
|
|
CompoundTag nativeTag = getTag(tile);
|
|
|
|
previous.setTile(pos.getX() & 15, pos.getY(), pos.getZ() & 15, nativeTag);
|
2016-05-12 19:46:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (entities != null) {
|
|
|
|
for (Collection<Entity> entityList : entities) {
|
|
|
|
for (Entity ent : entityList) {
|
2016-05-19 09:41:55 +02:00
|
|
|
if (ent instanceof EntityPlayer || (!createdEntities.isEmpty() && createdEntities.contains(ent.getUniqueID()))) {
|
2016-05-12 19:46:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int x = ((int) Math.round(ent.locX) & 15);
|
|
|
|
int z = ((int) Math.round(ent.locZ) & 15);
|
|
|
|
int y = (int) Math.round(ent.locY);
|
|
|
|
int i = FaweCache.CACHE_I[y][x][z];
|
|
|
|
char[] array = fs.getIdArray(i);
|
|
|
|
if (array == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int j = FaweCache.CACHE_J[y][x][z];
|
|
|
|
if (array[j] != 0) {
|
|
|
|
String id = EntityTypes.b(ent);
|
|
|
|
if (id != null) {
|
|
|
|
NBTTagCompound tag = new NBTTagCompound();
|
|
|
|
ent.e(tag); // readEntityIntoTag
|
|
|
|
CompoundTag nativeTag = (CompoundTag) methodToNative.invoke(adapter, tag);
|
|
|
|
Map<String, Tag> map = ReflectionUtils.getMap(nativeTag.getValue());
|
|
|
|
map.put("Id", new StringTag(id));
|
|
|
|
previous.setEntity(nativeTag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return previous;
|
|
|
|
}
|
|
|
|
|
2016-05-19 09:41:55 +02:00
|
|
|
public CompoundTag getTag(TileEntity tile) {
|
|
|
|
try {
|
|
|
|
NBTTagCompound tag = new NBTTagCompound();
|
|
|
|
tile.b(tag); // readTagIntoEntity
|
|
|
|
return (CompoundTag) methodToNative.invoke(adapter, tag);
|
|
|
|
} catch (Exception e) {
|
|
|
|
MainUtil.handleError(e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private BlockPosition.MutableBlockPosition pos = new BlockPosition.MutableBlockPosition(0, 0, 0);
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CompoundTag getTileEntity(Chunk chunk, int x, int y, int z) {
|
|
|
|
Map<BlockPosition, TileEntity> tiles = ((CraftChunk) chunk).getHandle().getTileEntities();
|
|
|
|
pos.c(x, y, z);
|
|
|
|
TileEntity tile = tiles.get(pos);
|
|
|
|
return tile != null ? getTag(tile) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Chunk getChunk(World world, int x, int z) {
|
|
|
|
return world.getChunkAt(x, z);
|
|
|
|
}
|
|
|
|
|
2016-04-29 23:24:59 +02:00
|
|
|
@Override
|
2016-05-06 00:59:21 +02:00
|
|
|
public boolean setComponents(FaweChunk fc, RunnableVal<FaweChunk> changeTask) {
|
2016-04-29 23:24:59 +02:00
|
|
|
CharFaweChunk<Chunk> fs = (CharFaweChunk<Chunk>) fc;
|
|
|
|
CraftChunk chunk = (CraftChunk) fs.getChunk();
|
|
|
|
net.minecraft.server.v1_8_R3.Chunk nmsChunk = chunk.getHandle();
|
|
|
|
net.minecraft.server.v1_8_R3.World nmsWorld = nmsChunk.getWorld();
|
|
|
|
try {
|
|
|
|
final boolean flag = getWorld().getEnvironment() == World.Environment.NORMAL;
|
|
|
|
// Sections
|
|
|
|
ChunkSection[] sections = nmsChunk.getSections();
|
|
|
|
Map<BlockPosition, TileEntity> tiles = nmsChunk.getTileEntities();
|
|
|
|
Collection<net.minecraft.server.v1_8_R3.Entity>[] entities = nmsChunk.getEntitySlices();
|
2016-05-06 00:59:21 +02:00
|
|
|
|
2016-05-12 19:46:38 +02:00
|
|
|
// Remove entities
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
int count = fs.getCount(i);
|
|
|
|
if (count == 0) {
|
|
|
|
continue;
|
|
|
|
} else if (count >= 4096) {
|
|
|
|
entities[i].clear();
|
|
|
|
} else {
|
|
|
|
char[] array = fs.getIdArray(i);
|
|
|
|
Collection<Entity> ents = new ArrayList<>(entities[i]);
|
|
|
|
for (Entity entity : ents) {
|
|
|
|
if (entity instanceof EntityPlayer) {
|
|
|
|
continue;
|
2016-05-06 00:59:21 +02:00
|
|
|
}
|
2016-05-12 19:46:38 +02:00
|
|
|
int x = ((int) Math.round(entity.locX) & 15);
|
|
|
|
int z = ((int) Math.round(entity.locZ) & 15);
|
|
|
|
int y = (int) Math.round(entity.locY);
|
2016-05-06 00:59:21 +02:00
|
|
|
if (array == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-25 21:29:55 +02:00
|
|
|
if (y < 0 || y > 255 || array[FaweCache.CACHE_J[y][x][z]] != 0) {
|
2016-05-12 19:46:38 +02:00
|
|
|
nmsWorld.removeEntity(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set entities
|
|
|
|
Set<UUID> createdEntities = new HashSet<>();
|
|
|
|
Set<CompoundTag> entitiesToSpawn = fs.getEntities();
|
|
|
|
for (CompoundTag nativeTag : entitiesToSpawn) {
|
|
|
|
Map<String, Tag> entityTagMap = nativeTag.getValue();
|
|
|
|
StringTag idTag = (StringTag) entityTagMap.get("Id");
|
|
|
|
ListTag posTag = (ListTag) entityTagMap.get("Pos");
|
|
|
|
ListTag rotTag = (ListTag) entityTagMap.get("Rotation");
|
|
|
|
if (idTag == null || posTag == null || rotTag == null) {
|
|
|
|
Fawe.debug("Unknown entity tag: " + nativeTag);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double x = posTag.getDouble(0);
|
|
|
|
double y = posTag.getDouble(1);
|
|
|
|
double z = posTag.getDouble(2);
|
|
|
|
float yaw = rotTag.getFloat(0);
|
|
|
|
float pitch = rotTag.getFloat(1);
|
|
|
|
String id = idTag.getValue();
|
|
|
|
Entity entity = EntityTypes.createEntityByName(id, nmsWorld);
|
|
|
|
if (entity != null) {
|
|
|
|
if (nativeTag != null) {
|
|
|
|
NBTTagCompound tag = (NBTTagCompound)methodFromNative.invoke(adapter, nativeTag);
|
|
|
|
for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
|
|
|
|
tag.remove(name);
|
2016-05-06 00:59:21 +02:00
|
|
|
}
|
2016-05-12 19:46:38 +02:00
|
|
|
entity.f(tag);
|
2016-05-06 00:59:21 +02:00
|
|
|
}
|
2016-05-12 19:46:38 +02:00
|
|
|
entity.setLocation(x, y, z, yaw, pitch);
|
|
|
|
nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM);
|
|
|
|
createdEntities.add(entity.getUniqueID());
|
2016-05-06 00:59:21 +02:00
|
|
|
}
|
2016-05-12 19:46:38 +02:00
|
|
|
}
|
|
|
|
// Run change task if applicable
|
|
|
|
if (changeTask != null) {
|
|
|
|
CharFaweChunk previous = getPrevious(fs, sections, tiles, entities, createdEntities, false);
|
2016-05-06 00:59:21 +02:00
|
|
|
changeTask.run(previous);
|
|
|
|
}
|
2016-04-29 23:24:59 +02:00
|
|
|
// Trim tiles
|
|
|
|
Set<Map.Entry<BlockPosition, TileEntity>> entryset = tiles.entrySet();
|
|
|
|
Iterator<Map.Entry<BlockPosition, TileEntity>> iterator = entryset.iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
Map.Entry<BlockPosition, TileEntity> tile = iterator.next();
|
|
|
|
BlockPosition pos = tile.getKey();
|
|
|
|
int lx = pos.getX() & 15;
|
|
|
|
int ly = pos.getY();
|
|
|
|
int lz = pos.getZ() & 15;
|
|
|
|
int j = FaweCache.CACHE_I[ly][lx][lz];
|
|
|
|
char[] array = fs.getIdArray(j);
|
|
|
|
if (array == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-06 00:59:21 +02:00
|
|
|
int k = FaweCache.CACHE_J[ly][lx][lz];
|
2016-04-29 23:24:59 +02:00
|
|
|
if (array[k] != 0) {
|
2016-05-13 10:19:04 +02:00
|
|
|
tile.getValue().E();
|
2016-04-29 23:24:59 +02:00
|
|
|
iterator.remove();
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 19:46:38 +02:00
|
|
|
HashSet<UUID> entsToRemove = fs.getEntityRemoves();
|
|
|
|
if (entsToRemove.size() > 0) {
|
|
|
|
for (int i = 0; i < entities.length; i++) {
|
2016-05-06 00:59:21 +02:00
|
|
|
Collection<Entity> ents = new ArrayList<>(entities[i]);
|
|
|
|
for (Entity entity : ents) {
|
2016-05-12 19:46:38 +02:00
|
|
|
if (entsToRemove.contains(entity.getUniqueID())) {
|
2016-05-06 00:59:21 +02:00
|
|
|
nmsWorld.removeEntity(entity);
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-06 00:59:21 +02:00
|
|
|
// Set blocks
|
2016-04-29 23:24:59 +02:00
|
|
|
for (int j = 0; j < sections.length; j++) {
|
|
|
|
if (fs.getCount(j) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char[] newArray = fs.getIdArray(j);
|
|
|
|
if (newArray == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ChunkSection section = sections[j];
|
|
|
|
if ((section == null) || (fs.getCount(j) >= 4096)) {
|
|
|
|
section = new ChunkSection(j << 4, flag, newArray);
|
|
|
|
sections[j] = section;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char[] currentArray = section.getIdArray();
|
|
|
|
int solid = 0;
|
|
|
|
for (int k = 0; k < newArray.length; k++) {
|
|
|
|
char n = newArray[k];
|
|
|
|
switch (n) {
|
|
|
|
case 0:
|
|
|
|
continue;
|
|
|
|
case 1:
|
|
|
|
if (currentArray[k] > 1) {
|
|
|
|
solid++;
|
2016-05-06 00:59:21 +02:00
|
|
|
currentArray[k] = 0;
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
solid++;
|
|
|
|
currentArray[k] = n;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setCount(0, solid, section);
|
|
|
|
}
|
2016-05-06 00:59:21 +02:00
|
|
|
|
|
|
|
// Set biomes
|
|
|
|
int[][] biomes = fs.biomes;
|
|
|
|
if (biomes != null) {
|
|
|
|
for (int x = 0; x < 16; x++) {
|
|
|
|
int[] array = biomes[x];
|
|
|
|
if (array == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int z = 0; z < 16; z++) {
|
|
|
|
int biome = array[z];
|
|
|
|
if (biome == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nmsChunk.getBiomeIndex()[((z & 0xF) << 4 | x & 0xF)] = (byte) biome;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set tiles
|
|
|
|
Map<BytePair, CompoundTag> tilesToSpawn = fs.getTiles();
|
|
|
|
int bx = fs.getX() << 4;
|
|
|
|
int bz = fs.getZ() << 4;
|
|
|
|
|
|
|
|
for (Map.Entry<BytePair, CompoundTag> entry : tilesToSpawn.entrySet()) {
|
|
|
|
CompoundTag nativeTag = entry.getValue();
|
|
|
|
BytePair pair = entry.getKey();
|
2016-05-12 19:46:38 +02:00
|
|
|
BlockPosition pos = new BlockPosition(MathMan.unpair16x(pair.pair[0]) + bx, pair.pair[1] & 0xFF, MathMan.unpair16y(pair.pair[0]) + bz); // Set pos
|
|
|
|
TileEntity tileEntity = nmsWorld.getTileEntity(pos);
|
2016-05-06 00:59:21 +02:00
|
|
|
if (tileEntity != null) {
|
|
|
|
NBTTagCompound tag = (NBTTagCompound) methodFromNative.invoke(adapter, nativeTag);
|
|
|
|
tileEntity.a(tag); // ReadTagIntoTile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable e) {
|
2016-05-19 09:41:55 +02:00
|
|
|
MainUtil.handleError(e);
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
2016-05-13 10:19:04 +02:00
|
|
|
sendChunk(fc, null);
|
2016-04-29 23:24:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCount(int tickingBlockCount, int nonEmptyBlockCount, ChunkSection section) throws NoSuchFieldException, IllegalAccessException {
|
|
|
|
Class<? extends ChunkSection> clazz = section.getClass();
|
|
|
|
Field fieldTickingBlockCount = clazz.getDeclaredField("tickingBlockCount");
|
|
|
|
Field fieldNonEmptyBlockCount = clazz.getDeclaredField("nonEmptyBlockCount");
|
|
|
|
fieldTickingBlockCount.setAccessible(true);
|
|
|
|
fieldNonEmptyBlockCount.setAccessible(true);
|
|
|
|
fieldTickingBlockCount.set(section, tickingBlockCount);
|
|
|
|
fieldNonEmptyBlockCount.set(section, nonEmptyBlockCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void refreshChunk(World world, Chunk chunk) {
|
|
|
|
if (!chunk.isLoaded()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-20 10:55:26 +02:00
|
|
|
try {
|
|
|
|
net.minecraft.server.v1_8_R3.Chunk nmsChunk = ((CraftChunk) chunk).getHandle();
|
|
|
|
ChunkCoordIntPair pos = nmsChunk.j(); // getPosition()
|
|
|
|
WorldServer w = (WorldServer) nmsChunk.getWorld();
|
|
|
|
PlayerChunkMap chunkMap = w.getPlayerChunkMap();
|
|
|
|
int x = pos.x;
|
|
|
|
int z = pos.z;
|
|
|
|
if (!chunkMap.isChunkInUse(x, z)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
HashSet<EntityPlayer> set = new HashSet<EntityPlayer>();
|
|
|
|
EntityTracker tracker = w.getTracker();
|
|
|
|
// Get players
|
|
|
|
|
|
|
|
Field fieldChunkMap = chunkMap.getClass().getDeclaredField("d");
|
|
|
|
fieldChunkMap.setAccessible(true);
|
|
|
|
LongHashMap<Object> map = (LongHashMap<Object>) fieldChunkMap.get(chunkMap);
|
|
|
|
long pair = (long) x + 2147483647L | (long) z + 2147483647L << 32;
|
|
|
|
Object playerChunk = map.getEntry(pair);
|
|
|
|
Field fieldPlayers = playerChunk.getClass().getDeclaredField("b");
|
|
|
|
fieldPlayers.setAccessible(true);
|
|
|
|
HashSet<EntityPlayer> players = new HashSet<>((Collection<EntityPlayer>)fieldPlayers.get(playerChunk));
|
|
|
|
if (players.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
HashSet<EntityTrackerEntry> entities = new HashSet<>();
|
|
|
|
List<Entity>[] entitieSlices = nmsChunk.getEntitySlices();
|
|
|
|
for (List<Entity> slice : entitieSlices) {
|
|
|
|
if (slice == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (Entity ent : slice) {
|
|
|
|
EntityTrackerEntry entry = tracker.trackedEntities.get(ent.getId());
|
|
|
|
if (entry == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
entities.add(entry);
|
|
|
|
PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(ent.getId());
|
|
|
|
for (EntityPlayer player : players) {
|
|
|
|
player.playerConnection.sendPacket(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Send chunks
|
|
|
|
PacketPlayOutMapChunk packet = new PacketPlayOutMapChunk(nmsChunk, false, 65535);
|
|
|
|
for (EntityPlayer player : players) {
|
|
|
|
player.playerConnection.sendPacket(packet);
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
2016-05-20 10:55:26 +02:00
|
|
|
// send ents
|
2016-05-20 18:05:16 +02:00
|
|
|
for (EntityTrackerEntry entry : entities) {
|
|
|
|
try {
|
|
|
|
TaskManager.IMP.later(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (EntityPlayer player : players) {
|
|
|
|
boolean result = entry.trackedPlayers.remove(player);
|
|
|
|
if (result && entry.tracker != player) {
|
|
|
|
entry.updatePlayer(player);
|
2016-05-20 10:55:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-20 18:05:16 +02:00
|
|
|
}
|
|
|
|
}, 2);
|
|
|
|
} catch (Throwable e) {
|
|
|
|
MainUtil.handleError(e);
|
2016-05-20 10:55:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable e) {
|
|
|
|
MainUtil.handleError(e);
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-12 19:46:38 +02:00
|
|
|
public boolean fixLighting(FaweChunk chunk, RelightMode mode) {
|
2016-05-13 10:19:04 +02:00
|
|
|
if (mode == RelightMode.NONE) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-29 23:24:59 +02:00
|
|
|
try {
|
|
|
|
CharFaweChunk<Chunk> fc = (CharFaweChunk<Chunk>) chunk;
|
|
|
|
CraftChunk craftChunk = (CraftChunk) fc.getChunk();
|
|
|
|
net.minecraft.server.v1_8_R3.Chunk nmsChunk = craftChunk.getHandle();
|
|
|
|
if (!craftChunk.isLoaded()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-12 19:46:38 +02:00
|
|
|
ChunkSection[] sections = nmsChunk.getSections();
|
2016-05-13 10:19:04 +02:00
|
|
|
if (mode == RelightMode.ALL) {
|
2016-05-12 19:46:38 +02:00
|
|
|
for (int i = 0; i < sections.length; i++) {
|
|
|
|
ChunkSection section = sections[i];
|
|
|
|
if (section != null) {
|
2016-05-13 10:19:04 +02:00
|
|
|
section.a(new NibbleArray());
|
2016-05-12 19:46:38 +02:00
|
|
|
section.b(new NibbleArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 23:24:59 +02:00
|
|
|
nmsChunk.initLighting();
|
2016-05-12 19:46:38 +02:00
|
|
|
if (fc.getTotalRelight() == 0 && mode == RelightMode.MINIMAL) {
|
2016-04-29 23:24:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
net.minecraft.server.v1_8_R3.World nmsWorld = nmsChunk.getWorld();
|
|
|
|
|
|
|
|
int X = fc.getX() << 4;
|
|
|
|
int Z = fc.getZ() << 4;
|
|
|
|
|
|
|
|
for (int j = 0; j < sections.length; j++) {
|
|
|
|
ChunkSection section = sections[j];
|
|
|
|
if (section == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-13 10:19:04 +02:00
|
|
|
if (((fc.getRelight(j) == 0) && mode == RelightMode.MINIMAL) || (fc.getCount(j) == 0 && mode != RelightMode.ALL) || ((fc.getCount(j) >= 4096) && (fc.getAir(j) == 0)) || fc.getAir(j) == 4096) {
|
2016-04-29 23:24:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char[] array = section.getIdArray();
|
2016-05-13 10:19:04 +02:00
|
|
|
if (mode == RelightMode.ALL) {
|
|
|
|
for (int k = array.length - 1; k >= 0; k--) {
|
|
|
|
final int x = FaweCache.CACHE_X[j][k];
|
|
|
|
final int y = FaweCache.CACHE_Y[j][k];
|
|
|
|
final int z = FaweCache.CACHE_Z[j][k];
|
|
|
|
if (isSurrounded(sections, x, y, z)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pos.c(X + x, y, Z + z);
|
|
|
|
nmsWorld.x(pos);
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
2016-05-13 10:19:04 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int k = array.length - 1; k >= 0; k--) {
|
|
|
|
final int i = array[k];
|
|
|
|
final short id = (short) (i >> 4);
|
2016-04-29 23:24:59 +02:00
|
|
|
switch (id) { // Lighting
|
2016-05-13 10:19:04 +02:00
|
|
|
case 0:
|
|
|
|
continue;
|
2016-04-29 23:24:59 +02:00
|
|
|
default:
|
2016-05-12 19:46:38 +02:00
|
|
|
if (mode == RelightMode.MINIMAL) {
|
2016-04-29 23:24:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-13 10:19:04 +02:00
|
|
|
if (PseudoRandom.random.random(3) != 0) {
|
2016-04-29 23:24:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case 10:
|
|
|
|
case 11:
|
|
|
|
case 39:
|
|
|
|
case 40:
|
|
|
|
case 50:
|
|
|
|
case 51:
|
|
|
|
case 62:
|
|
|
|
case 74:
|
|
|
|
case 76:
|
|
|
|
case 89:
|
|
|
|
case 122:
|
|
|
|
case 124:
|
|
|
|
case 130:
|
|
|
|
case 138:
|
|
|
|
case 169:
|
2016-05-13 10:19:04 +02:00
|
|
|
final int x = FaweCache.CACHE_X[j][k];
|
|
|
|
final int y = FaweCache.CACHE_Y[j][k];
|
|
|
|
final int z = FaweCache.CACHE_Z[j][k];
|
2016-04-29 23:24:59 +02:00
|
|
|
if (isSurrounded(sections, x, y, z)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pos.c(X + x, y, Z + z);
|
|
|
|
nmsWorld.x(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (Throwable e) {
|
|
|
|
if (Thread.currentThread() == Fawe.get().getMainThread()) {
|
2016-05-19 09:41:55 +02:00
|
|
|
MainUtil.handleError(e);
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSurrounded(ChunkSection[] sections, int x, int y, int z) {
|
|
|
|
return isSolid(getId(sections, x, y + 1, z))
|
|
|
|
&& isSolid(getId(sections, x + 1, y - 1, z))
|
|
|
|
&& isSolid(getId(sections, x - 1, y, z))
|
|
|
|
&& isSolid(getId(sections, x, y, z + 1))
|
|
|
|
&& isSolid(getId(sections, x, y, z - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSolid(int i) {
|
|
|
|
return Material.getMaterial(i).isOccluding();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getId(ChunkSection[] sections, int x, int y, int z) {
|
|
|
|
if (x < 0 || x > 15 || z < 0 || z > 15) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (y < 0 || y > 255) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int i = FaweCache.CACHE_I[y][x][z];
|
|
|
|
ChunkSection section = sections[i];
|
|
|
|
if (section == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
char[] array = section.getIdArray();
|
|
|
|
int j = FaweCache.CACHE_J[y][x][z];
|
|
|
|
return array[j] >> 4;
|
|
|
|
}
|
|
|
|
}
|