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;
|
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;
|
2016-08-28 11:03:42 +02:00
|
|
|
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
|
2016-05-06 00:59:21 +02:00
|
|
|
import com.sk89q.worldedit.internal.Constants;
|
2016-07-15 01:53:30 +02:00
|
|
|
import java.io.File;
|
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-07-15 01:53:30 +02:00
|
|
|
import java.util.Arrays;
|
2016-04-29 23:24:59 +02:00
|
|
|
import java.util.Collection;
|
2016-09-02 05:23:58 +02:00
|
|
|
import java.util.Collections;
|
2016-06-28 18:39:07 +02:00
|
|
|
import java.util.HashMap;
|
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-07-27 00:03:51 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.Block;
|
2016-04-29 23:24:59 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.BlockPosition;
|
|
|
|
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;
|
2016-05-06 00:59:21 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.EntityTypes;
|
2016-07-15 01:53:30 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.EnumDifficulty;
|
2016-07-27 00:03:51 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.EnumSkyBlock;
|
2016-05-20 10:55:26 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.LongHashMap;
|
2016-07-15 01:53:30 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.MinecraftServer;
|
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-08-16 08:41:29 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.Packet;
|
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-07-15 01:53:30 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.ServerNBTManager;
|
2016-04-29 23:24:59 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.TileEntity;
|
2016-07-15 01:53:30 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.WorldData;
|
|
|
|
import net.minecraft.server.v1_8_R3.WorldManager;
|
2016-05-20 10:55:26 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.WorldServer;
|
2016-07-15 01:53:30 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.WorldSettings;
|
|
|
|
import net.minecraft.server.v1_8_R3.WorldType;
|
2016-04-29 23:24:59 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Chunk;
|
|
|
|
import org.bukkit.World;
|
2016-07-15 01:53:30 +02:00
|
|
|
import org.bukkit.WorldCreator;
|
2016-04-29 23:24:59 +02:00
|
|
|
import org.bukkit.craftbukkit.v1_8_R3.CraftChunk;
|
2016-07-15 01:53:30 +02:00
|
|
|
import org.bukkit.craftbukkit.v1_8_R3.CraftServer;
|
|
|
|
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
|
2016-05-06 00:59:21 +02:00
|
|
|
import org.bukkit.event.entity.CreatureSpawnEvent;
|
2016-07-15 01:53:30 +02:00
|
|
|
import org.bukkit.event.world.WorldInitEvent;
|
|
|
|
import org.bukkit.event.world.WorldLoadEvent;
|
|
|
|
import org.bukkit.generator.ChunkGenerator;
|
2016-04-29 23:24:59 +02:00
|
|
|
|
2016-07-27 00:03:51 +02:00
|
|
|
public class BukkitQueue18R3 extends BukkitQueue_0<Chunk, ChunkSection[], ChunkSection> {
|
2016-04-29 23:24:59 +02:00
|
|
|
|
2016-08-28 11:03:42 +02:00
|
|
|
public static Field isDirty;
|
|
|
|
|
2016-04-29 23:24:59 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-28 11:03:42 +02:00
|
|
|
@Override
|
|
|
|
public void setupAdapter(BukkitImplAdapter adapter) {
|
|
|
|
if (this.adapter == null) {
|
|
|
|
try {
|
|
|
|
isDirty = ChunkSection.class.getDeclaredField("isDirty");
|
|
|
|
isDirty.setAccessible(true);
|
|
|
|
Fawe.debug("isDirty found");
|
|
|
|
} catch (Throwable e) {}
|
|
|
|
}
|
|
|
|
super.setupAdapter(adapter);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-07-15 01:53:30 +02:00
|
|
|
@Override
|
|
|
|
public void setFullbright(ChunkSection[] sections) {
|
|
|
|
for (int i = 0; i < sections.length; i++) {
|
|
|
|
ChunkSection section = sections[i];
|
|
|
|
if (section != null) {
|
|
|
|
byte[] bytes = section.getSkyLightArray().a();
|
|
|
|
Arrays.fill(bytes, Byte.MAX_VALUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2016-07-27 00:03:51 +02:00
|
|
|
public int getCombinedId4Data(ChunkSection section, int x, int y, int z) {
|
|
|
|
char[] ls = section.getIdArray();
|
2016-08-20 05:33:56 +02:00
|
|
|
return ls[FaweCache.CACHE_J[y][z & 15][x & 15]];
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isChunkLoaded(World world, int x, int z) {
|
|
|
|
return world.isChunkLoaded(x, z);
|
|
|
|
}
|
|
|
|
|
2016-05-02 05:06:23 +02:00
|
|
|
@Override
|
2016-07-27 00:03:51 +02:00
|
|
|
public ChunkSection getCachedSection(ChunkSection[] chunkSections, int cy) {
|
|
|
|
return chunkSections[cy];
|
2016-05-02 05:06:23 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2016-08-20 05:33:56 +02:00
|
|
|
int i = FaweCache.CACHE_I[y][z][x];
|
2016-05-12 19:46:38 +02:00
|
|
|
char[] array = fs.getIdArray(i);
|
|
|
|
if (array == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-20 05:33:56 +02:00
|
|
|
int j = FaweCache.CACHE_J[y][z][x];
|
2016-05-12 19:46:38 +02:00
|
|
|
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();
|
2016-07-15 01:53:30 +02:00
|
|
|
nmsChunk.f(true); // Modified
|
|
|
|
nmsChunk.mustSave = true;
|
2016-04-29 23:24:59 +02:00
|
|
|
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-08-20 05:33:56 +02:00
|
|
|
if (y < 0 || y > 255 || array[FaweCache.CACHE_J[y][z][x]] != 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
|
2016-06-28 18:39:07 +02:00
|
|
|
Iterator<Map.Entry<BlockPosition, TileEntity>> iterator = tiles.entrySet().iterator();
|
|
|
|
HashMap<BlockPosition, TileEntity> toRemove = null;
|
2016-04-29 23:24:59 +02:00
|
|
|
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;
|
2016-08-20 05:33:56 +02:00
|
|
|
int j = FaweCache.CACHE_I[ly][lz][lx];
|
2016-04-29 23:24:59 +02:00
|
|
|
char[] array = fs.getIdArray(j);
|
|
|
|
if (array == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-20 05:33:56 +02:00
|
|
|
int k = FaweCache.CACHE_J[ly][lz][lx];
|
2016-04-29 23:24:59 +02:00
|
|
|
if (array[k] != 0) {
|
2016-06-28 18:39:07 +02:00
|
|
|
if (toRemove == null) {
|
|
|
|
toRemove = new HashMap<>();
|
|
|
|
}
|
|
|
|
toRemove.put(tile.getKey(), tile.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (toRemove != null) {
|
|
|
|
for (Map.Entry<BlockPosition, TileEntity> entry : toRemove.entrySet()) {
|
|
|
|
BlockPosition bp = entry.getKey();
|
|
|
|
TileEntity tile = entry.getValue();
|
|
|
|
tiles.remove(bp);
|
|
|
|
tile.y();
|
|
|
|
nmsWorld.t(bp);
|
|
|
|
tile.E();
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
2016-06-28 18:39:07 +02:00
|
|
|
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
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];
|
2016-08-28 11:03:42 +02:00
|
|
|
if (section != null && isDirty != null) {
|
|
|
|
isDirty.set(section, true);
|
|
|
|
}
|
2016-04-29 23:24:59 +02:00
|
|
|
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-08-27 05:47:09 +02:00
|
|
|
BlockPosition pos = new BlockPosition(pair.get0x() + bx, pair.get1() & 0xFF, pair.get0y() + bz); // Set pos
|
2016-05-12 19:46:38 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
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
|
2016-08-09 06:32:51 +02:00
|
|
|
public void refreshChunk(FaweChunk fc) {
|
|
|
|
BukkitChunk_1_8 fs = (BukkitChunk_1_8) fc;
|
2016-08-14 02:26:51 +02:00
|
|
|
ensureChunkLoaded(fc.getX(), fc.getZ());
|
2016-08-09 06:32:51 +02:00
|
|
|
Chunk chunk = fs.getChunk();
|
2016-04-29 23:24:59 +02:00
|
|
|
if (!chunk.isLoaded()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-20 10:55:26 +02:00
|
|
|
try {
|
|
|
|
net.minecraft.server.v1_8_R3.Chunk nmsChunk = ((CraftChunk) chunk).getHandle();
|
|
|
|
WorldServer w = (WorldServer) nmsChunk.getWorld();
|
|
|
|
PlayerChunkMap chunkMap = w.getPlayerChunkMap();
|
2016-08-09 06:32:51 +02:00
|
|
|
int x = nmsChunk.locX;
|
|
|
|
int z = nmsChunk.locZ;
|
2016-05-20 10:55:26 +02:00
|
|
|
if (!chunkMap.isChunkInUse(x, z)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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);
|
2016-08-09 06:32:51 +02:00
|
|
|
Collection<EntityPlayer> players = (Collection<EntityPlayer>) fieldPlayers.get(playerChunk);
|
|
|
|
if (players.isEmpty()) {
|
2016-05-20 10:55:26 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-08-09 06:32:51 +02:00
|
|
|
// Send chunks
|
|
|
|
int mask = fc.getBitMask();
|
|
|
|
if (mask == 65535 && hasEntities(nmsChunk)) {
|
|
|
|
PacketPlayOutMapChunk packet = new PacketPlayOutMapChunk(nmsChunk, false, 65280);
|
|
|
|
for (EntityPlayer player : players) {
|
|
|
|
player.playerConnection.sendPacket(packet);
|
2016-05-20 10:55:26 +02:00
|
|
|
}
|
2016-08-09 06:32:51 +02:00
|
|
|
mask = 255;
|
2016-05-20 10:55:26 +02:00
|
|
|
}
|
2016-08-09 06:32:51 +02:00
|
|
|
PacketPlayOutMapChunk packet = new PacketPlayOutMapChunk(nmsChunk, false, mask);
|
2016-05-20 10:55:26 +02:00
|
|
|
for (EntityPlayer player : players) {
|
|
|
|
player.playerConnection.sendPacket(packet);
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
2016-08-16 08:41:29 +02:00
|
|
|
// Send tiles
|
|
|
|
for (Map.Entry<BlockPosition, TileEntity> entry : nmsChunk.getTileEntities().entrySet()) {
|
|
|
|
TileEntity tile = entry.getValue();
|
|
|
|
Packet tilePacket = tile.getUpdatePacket();
|
|
|
|
for (EntityPlayer player : players) {
|
|
|
|
player.playerConnection.sendPacket(tilePacket);
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 10:55:26 +02:00
|
|
|
} catch (Throwable e) {
|
|
|
|
MainUtil.handleError(e);
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 06:32:51 +02:00
|
|
|
public boolean hasEntities(net.minecraft.server.v1_8_R3.Chunk nmsChunk) {
|
|
|
|
for (int i = 0; i < nmsChunk.entitySlices.length; i++) {
|
|
|
|
List<Entity> slice = nmsChunk.entitySlices[i];
|
|
|
|
if (slice != null && !slice.isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-29 23:24:59 +02:00
|
|
|
@Override
|
2016-07-15 01:53:30 +02:00
|
|
|
public boolean removeLighting(ChunkSection[] sections, RelightMode mode, boolean sky) {
|
|
|
|
if (mode == RelightMode.ALL) {
|
|
|
|
for (int i = 0; i < sections.length; i++) {
|
|
|
|
ChunkSection section = sections[i];
|
|
|
|
if (section != null) {
|
|
|
|
section.a(new NibbleArray());
|
|
|
|
if (sky) {
|
|
|
|
section.b(new NibbleArray());
|
2016-05-12 19:46:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 01:53:30 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public World createWorld(final WorldCreator creator) {
|
|
|
|
final String name = creator.name();
|
|
|
|
ChunkGenerator generator = creator.generator();
|
|
|
|
final CraftServer server = (CraftServer) Bukkit.getServer();
|
|
|
|
final MinecraftServer console = server.getServer();
|
|
|
|
final File folder = new File(server.getWorldContainer(), name);
|
|
|
|
final World world = server.getWorld(name);
|
|
|
|
final WorldType type = WorldType.getType(creator.type().getName());
|
|
|
|
final boolean generateStructures = creator.generateStructures();
|
|
|
|
if (world != null) {
|
|
|
|
return world;
|
|
|
|
}
|
|
|
|
if (folder.exists() && !folder.isDirectory()) {
|
|
|
|
throw new IllegalArgumentException("File exists with the name '" + name + "' and isn't a folder");
|
|
|
|
}
|
2016-09-02 05:23:58 +02:00
|
|
|
TaskManager.IMP.sync(new RunnableVal<Object>() {
|
|
|
|
@Override
|
|
|
|
public void run(Object value) {
|
|
|
|
try {
|
|
|
|
Field field = CraftServer.class.getDeclaredField("worlds");
|
|
|
|
field.setAccessible(true);
|
|
|
|
Map<Object, Object> existing = (Map<Object, Object>) field.get(server);
|
|
|
|
if (!existing.getClass().getName().contains("SynchronizedMap")) {
|
|
|
|
field.set(server, Collections.synchronizedMap(existing));
|
|
|
|
}
|
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-07-15 01:53:30 +02:00
|
|
|
if (generator == null) {
|
|
|
|
generator = server.getGenerator(name);
|
|
|
|
}
|
|
|
|
int dimension = 10 + console.worlds.size();
|
|
|
|
boolean used = false;
|
|
|
|
do {
|
|
|
|
for (final WorldServer ws : console.worlds) {
|
|
|
|
used = (ws.dimension == dimension);
|
|
|
|
if (used) {
|
|
|
|
++dimension;
|
|
|
|
break;
|
2016-06-06 09:04:16 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-15 01:53:30 +02:00
|
|
|
} while (used);
|
|
|
|
final boolean hardcore = false;
|
|
|
|
ServerNBTManager sdm = new ServerNBTManager(server.getWorldContainer(), name, true);
|
|
|
|
WorldData worlddata = sdm.getWorldData();
|
|
|
|
final WorldSettings worldSettings;
|
|
|
|
if (worlddata == null) {
|
|
|
|
worldSettings = new WorldSettings(creator.seed(), WorldSettings.EnumGamemode.getById(server.getDefaultGameMode().getValue()), generateStructures, hardcore, type);
|
|
|
|
worldSettings.setGeneratorSettings(creator.generatorSettings());
|
|
|
|
worlddata = new WorldData(worldSettings, name);
|
|
|
|
} else {
|
|
|
|
worldSettings = null;
|
|
|
|
}
|
|
|
|
worlddata.checkName(name);
|
|
|
|
final WorldServer internal = (WorldServer)new WorldServer(console, sdm, worlddata, dimension, console.methodProfiler, creator.environment(), generator).b();
|
|
|
|
startSet(true); // Temporarily allow async chunk load since the world isn't added yet
|
2016-09-02 06:15:28 +02:00
|
|
|
if (worldSettings != null) {
|
|
|
|
internal.a(worldSettings);
|
|
|
|
}
|
2016-07-15 01:53:30 +02:00
|
|
|
endSet(true);
|
|
|
|
internal.scoreboard = server.getScoreboardManager().getMainScoreboard().getHandle();
|
|
|
|
internal.tracker = new EntityTracker(internal);
|
|
|
|
internal.addIWorldAccess(new WorldManager(console, internal));
|
|
|
|
internal.worldData.setDifficulty(EnumDifficulty.EASY);
|
|
|
|
internal.setSpawnFlags(true, true);
|
|
|
|
if (generator != null) {
|
|
|
|
internal.getWorld().getPopulators().addAll(generator.getDefaultPopulators(internal.getWorld()));
|
|
|
|
}
|
|
|
|
// Add the world
|
|
|
|
return TaskManager.IMP.sync(new RunnableVal<World>() {
|
|
|
|
@Override
|
|
|
|
public void run(World value) {
|
|
|
|
console.worlds.add(internal);
|
|
|
|
server.getPluginManager().callEvent(new WorldInitEvent(internal.getWorld()));
|
|
|
|
server.getPluginManager().callEvent(new WorldLoadEvent(internal.getWorld()));
|
|
|
|
this.value = internal.getWorld();
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
2016-07-15 01:53:30 +02:00
|
|
|
});
|
|
|
|
}
|
2016-04-29 23:24:59 +02:00
|
|
|
|
2016-07-15 01:53:30 +02:00
|
|
|
@Override
|
|
|
|
public void relight(int x, int y, int z) {
|
|
|
|
pos.c(x, y, z);
|
|
|
|
nmsWorld.x(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
private WorldServer nmsWorld;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public World getImpWorld() {
|
|
|
|
World world = super.getImpWorld();
|
|
|
|
this.nmsWorld = ((CraftWorld) world).getHandle();
|
|
|
|
return super.getImpWorld();
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|
2016-06-21 07:53:52 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public FaweChunk getFaweChunk(int x, int z) {
|
2016-08-10 09:18:55 +02:00
|
|
|
return new BukkitChunk_1_8(this, x, z);
|
2016-06-21 07:53:52 +02:00
|
|
|
}
|
2016-07-25 00:25:07 +02:00
|
|
|
|
|
|
|
@Override
|
2016-07-27 00:03:51 +02:00
|
|
|
public void setSkyLight(ChunkSection section, int x, int y, int z, int value) {
|
|
|
|
section.a(x & 15, y & 15, z & 15, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setBlockLight(ChunkSection section, int x, int y, int z, int value) {
|
|
|
|
section.b(x & 15, y & 15, z & 15, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSkyLight(ChunkSection section, int x, int y, int z) {
|
|
|
|
return section.d(x & 15, y & 15, z & 15);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEmmittedLight(ChunkSection section, int x, int y, int z) {
|
|
|
|
return section.e(x & 15, y & 15, z & 15);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getOpacity(ChunkSection section, int x, int y, int z) {
|
|
|
|
int combined = getCombinedId4Data(section, x, y, z);
|
|
|
|
if (combined == 0) {
|
|
|
|
return 0;
|
2016-07-25 00:25:07 +02:00
|
|
|
}
|
2016-07-27 00:03:51 +02:00
|
|
|
Block block = Block.getById(FaweCache.getId(combined));
|
|
|
|
return block.p();
|
2016-07-25 00:25:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-07-27 00:03:51 +02:00
|
|
|
public int getBrightness(ChunkSection section, int x, int y, int z) {
|
|
|
|
int combined = getCombinedId4Data(section, x, y, z);
|
|
|
|
if (combined == 0) {
|
|
|
|
return 0;
|
2016-07-25 00:25:07 +02:00
|
|
|
}
|
2016-07-27 00:03:51 +02:00
|
|
|
Block block = Block.getById(FaweCache.getId(combined));
|
|
|
|
return block.r();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasBlock(ChunkSection section, int x, int y, int z) {
|
2016-08-20 05:33:56 +02:00
|
|
|
int i = FaweCache.CACHE_J[y & 15][z & 15][x & 15];
|
2016-07-27 00:03:51 +02:00
|
|
|
return section.getIdArray()[i] != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getOpacityBrightnessPair(ChunkSection section, int x, int y, int z) {
|
|
|
|
int combined = getCombinedId4Data(section, x, y, z);
|
|
|
|
if (combined == 0) {
|
|
|
|
return 0;
|
2016-07-25 00:25:07 +02:00
|
|
|
}
|
2016-07-27 00:03:51 +02:00
|
|
|
Block block = Block.getById(FaweCache.getId(combined));
|
|
|
|
return MathMan.pair16(block.p(), block.r());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void relightBlock(int x, int y, int z) {
|
|
|
|
pos.c(x, y, z);
|
|
|
|
nmsWorld.c(EnumSkyBlock.BLOCK, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void relightSky(int x, int y, int z) {
|
|
|
|
pos.c(x, y, z);
|
|
|
|
nmsWorld.c(EnumSkyBlock.SKY, pos);
|
2016-07-25 00:25:07 +02:00
|
|
|
}
|
2016-04-29 23:24:59 +02:00
|
|
|
}
|