Update forge packet sending

This commit is contained in:
Jesse Boyd 2016-05-21 16:50:25 +10:00
parent 408f690d80
commit f6592f5101
2 changed files with 154 additions and 53 deletions

View File

@ -4,16 +4,15 @@ import com.boydti.fawe.Fawe;
import com.boydti.fawe.FaweCache; import com.boydti.fawe.FaweCache;
import com.boydti.fawe.example.CharFaweChunk; import com.boydti.fawe.example.CharFaweChunk;
import com.boydti.fawe.example.NMSMappedFaweQueue; import com.boydti.fawe.example.NMSMappedFaweQueue;
import com.boydti.fawe.forge.ForgePlayer;
import com.boydti.fawe.object.BytePair; import com.boydti.fawe.object.BytePair;
import com.boydti.fawe.object.FaweChunk; import com.boydti.fawe.object.FaweChunk;
import com.boydti.fawe.object.FawePlayer;
import com.boydti.fawe.object.IntegerPair; import com.boydti.fawe.object.IntegerPair;
import com.boydti.fawe.object.PseudoRandom; import com.boydti.fawe.object.PseudoRandom;
import com.boydti.fawe.object.RunnableVal; import com.boydti.fawe.object.RunnableVal;
import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.MainUtil;
import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.MathMan;
import com.boydti.fawe.util.ReflectionUtils; import com.boydti.fawe.util.ReflectionUtils;
import com.boydti.fawe.util.TaskManager;
import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.ListTag; import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.StringTag; import com.sk89q.jnbt.StringTag;
@ -31,15 +30,18 @@ import java.util.UUID;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList; import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityTracker;
import net.minecraft.entity.EntityTrackerEntry;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetHandlerPlayServer; import net.minecraft.network.play.server.S13PacketDestroyEntities;
import net.minecraft.network.play.server.S21PacketChunkData; import net.minecraft.network.play.server.S21PacketChunkData;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.PlayerManager;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChunkCoordinates; import net.minecraft.util.IntHashMap;
import net.minecraft.util.LongHashMap; import net.minecraft.util.LongHashMap;
import net.minecraft.world.ChunkCoordIntPair; import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.ChunkPosition; import net.minecraft.world.ChunkPosition;
@ -220,33 +222,81 @@ public class ForgeQueue_All extends NMSMappedFaweQueue<World, Chunk, ExtendedBlo
}; };
@Override @Override
public void refreshChunk(World world, Chunk chunk) { public void refreshChunk(World world, net.minecraft.world.chunk.Chunk nmsChunk) {
if (!chunk.isChunkLoaded) { if (!nmsChunk.isChunkLoaded) {
return; return;
} }
ChunkCoordIntPair pos = chunk.getChunkCoordIntPair(); try {
int cx = pos.chunkXPos; ChunkCoordIntPair pos = nmsChunk.getChunkCoordIntPair();
int cz = pos.chunkZPos; WorldServer w = (WorldServer) nmsChunk.worldObj;
for (FawePlayer fp : Fawe.get().getCachedPlayers()) { PlayerManager chunkMap = w.getPlayerManager();
ForgePlayer forgePlayer = (ForgePlayer) fp; int x = pos.chunkXPos;
EntityPlayerMP player = forgePlayer.parent; int z = pos.chunkZPos;
if (!player.worldObj.equals(world)) { if (!chunkMap.func_152621_a(x, z)) {
continue; return;
} }
int view = MinecraftServer.getServer().getConfigurationManager().getViewDistance(); EntityTracker tracker = w.getEntityTracker();
EntityPlayerMP nmsPlayer = (EntityPlayerMP) player; final HashSet<EntityPlayerMP> players = new HashSet<>();
ChunkCoordinates loc = player.getPlayerCoordinates(); for (EntityPlayer player : (List<EntityPlayer>) w.playerEntities) {
int px = loc.posX >> 4; if (player instanceof EntityPlayerMP) {
int pz = loc.posZ >> 4; if (chunkMap.isPlayerWatchingChunk((EntityPlayerMP) player, x, z)) {
int dx = Math.abs(cx - (loc.posX >> 4)); players.add((EntityPlayerMP) player);
int dz = Math.abs(cz - (loc.posZ >> 4)); }
if ((dx > view) || (dz > view)) { }
continue;
} }
NetHandlerPlayServer con = nmsPlayer.playerNetServerHandler; if (players.size() == 0) {
con.sendPacket(new S21PacketChunkData(chunk, false, 65535)); return;
// Try sending true, 0 first }
// Try bulk chunk packet HashSet<EntityTrackerEntry> entities = new HashSet<>();
Collection<Entity>[] entitieSlices = nmsChunk.entityLists;
IntHashMap entries = null;
for (Field field : tracker.getClass().getDeclaredFields()) {
if (field.getType() == IntHashMap.class) {
field.setAccessible(true);
entries = (IntHashMap) field.get(tracker);
}
}
for (Collection<Entity> slice : entitieSlices) {
if (slice == null) {
continue;
}
for (Entity ent : slice) {
EntityTrackerEntry entry = entries != null ? (EntityTrackerEntry) entries.lookup(ent.getEntityId()) : null;
if (entry == null) {
continue;
}
entities.add(entry);
S13PacketDestroyEntities packet = new S13PacketDestroyEntities(ent.getEntityId());
for (EntityPlayerMP player : players) {
player.playerNetServerHandler.sendPacket(packet);
}
}
}
// Send chunks
S21PacketChunkData packet = new S21PacketChunkData(nmsChunk, false, 65535);
for (EntityPlayerMP player : players) {
player.playerNetServerHandler.sendPacket(packet);
}
// send ents
for (final EntityTrackerEntry entry : entities) {
try {
TaskManager.IMP.later(new Runnable() {
@Override
public void run() {
for (EntityPlayerMP player : players) {
boolean result = entry.trackingPlayers.remove(player);
if (result && entry.myEntity != player) {
entry.tryStartWachingThis(player);
}
}
}
}, 2);
} catch (Throwable e) {
MainUtil.handleError(e);
}
}
} catch (Throwable e) {
MainUtil.handleError(e);
} }
} }

View File

@ -4,15 +4,14 @@ import com.boydti.fawe.Fawe;
import com.boydti.fawe.FaweCache; import com.boydti.fawe.FaweCache;
import com.boydti.fawe.example.CharFaweChunk; import com.boydti.fawe.example.CharFaweChunk;
import com.boydti.fawe.example.NMSMappedFaweQueue; import com.boydti.fawe.example.NMSMappedFaweQueue;
import com.boydti.fawe.forge.ForgePlayer;
import com.boydti.fawe.object.BytePair; import com.boydti.fawe.object.BytePair;
import com.boydti.fawe.object.FaweChunk; import com.boydti.fawe.object.FaweChunk;
import com.boydti.fawe.object.FawePlayer;
import com.boydti.fawe.object.PseudoRandom; import com.boydti.fawe.object.PseudoRandom;
import com.boydti.fawe.object.RunnableVal; import com.boydti.fawe.object.RunnableVal;
import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.MainUtil;
import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.MathMan;
import com.boydti.fawe.util.ReflectionUtils; import com.boydti.fawe.util.ReflectionUtils;
import com.boydti.fawe.util.TaskManager;
import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.ListTag; import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.StringTag; import com.sk89q.jnbt.StringTag;
@ -29,16 +28,20 @@ import java.util.UUID;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList; import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityTracker;
import net.minecraft.entity.EntityTrackerEntry;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetHandlerPlayServer; import net.minecraft.network.play.server.S13PacketDestroyEntities;
import net.minecraft.network.play.server.S21PacketChunkData; import net.minecraft.network.play.server.S21PacketChunkData;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.PlayerManager;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos; import net.minecraft.util.BlockPos;
import net.minecraft.util.ClassInheritanceMultiMap; import net.minecraft.util.ClassInheritanceMultiMap;
import net.minecraft.util.IntHashMap;
import net.minecraft.world.ChunkCoordIntPair; import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.WorldServer; import net.minecraft.world.WorldServer;
@ -556,33 +559,81 @@ public class ForgeQueue_All extends NMSMappedFaweQueue<World, Chunk, ExtendedBlo
} }
@Override @Override
public void refreshChunk(World world, Chunk chunk) { public void refreshChunk(World world, net.minecraft.world.chunk.Chunk nmsChunk) {
if (!chunk.isLoaded()) { if (!nmsChunk.isLoaded()) {
return; return;
} }
ChunkCoordIntPair pos = chunk.getChunkCoordIntPair(); try {
int cx = pos.chunkXPos; ChunkCoordIntPair pos = nmsChunk.getChunkCoordIntPair();
int cz = pos.chunkZPos; WorldServer w = (WorldServer) nmsChunk.getWorld();
for (FawePlayer fp : Fawe.get().getCachedPlayers()) { PlayerManager chunkMap = w.getPlayerManager();
ForgePlayer forgePlayer = (ForgePlayer) fp; int x = pos.chunkXPos;
EntityPlayerMP player = forgePlayer.parent; int z = pos.chunkZPos;
if (!player.worldObj.equals(world)) { if (!chunkMap.hasPlayerInstance(x, z)) {
continue; return;
} }
int view = MinecraftServer.getServer().getConfigurationManager().getViewDistance(); EntityTracker tracker = w.getEntityTracker();
EntityPlayerMP nmsPlayer = (EntityPlayerMP) player; HashSet<EntityPlayerMP> players = new HashSet<>();
BlockPos loc = player.getPosition(); for (EntityPlayer player : w.playerEntities) {
int px = loc.getX() >> 4; if (player instanceof EntityPlayerMP) {
int pz = loc.getZ() >> 4; if (chunkMap.isPlayerWatchingChunk((EntityPlayerMP) player, x, z)) {
int dx = Math.abs(cx - (loc.getX() >> 4)); players.add((EntityPlayerMP) player);
int dz = Math.abs(cz - (loc.getZ() >> 4)); }
if ((dx > view) || (dz > view)) { }
continue;
} }
NetHandlerPlayServer con = nmsPlayer.playerNetServerHandler; if (players.size() == 0) {
con.sendPacket(new S21PacketChunkData(chunk, false, 65535)); return;
// Try sending true, 0 first }
// Try bulk chunk packet HashSet<EntityTrackerEntry> entities = new HashSet<>();
ClassInheritanceMultiMap<Entity>[] entitieSlices = nmsChunk.getEntityLists();
IntHashMap<EntityTrackerEntry> entries = null;
for (Field field : tracker.getClass().getDeclaredFields()) {
if (field.getType() == IntHashMap.class) {
field.setAccessible(true);
entries = (IntHashMap<EntityTrackerEntry>) field.get(tracker);
}
}
for (ClassInheritanceMultiMap<Entity> slice : entitieSlices) {
if (slice == null) {
continue;
}
for (Entity ent : slice) {
EntityTrackerEntry entry = entries != null ? entries.lookup(ent.getEntityId()) : null;
if (entry == null) {
continue;
}
entities.add(entry);
S13PacketDestroyEntities packet = new S13PacketDestroyEntities(ent.getEntityId());
for (EntityPlayerMP player : players) {
player.playerNetServerHandler.sendPacket(packet);
}
}
}
// Send chunks
S21PacketChunkData packet = new S21PacketChunkData(nmsChunk, false, 65535);
for (EntityPlayerMP player : players) {
player.playerNetServerHandler.sendPacket(packet);
}
// send ents
for (EntityTrackerEntry entry : entities) {
try {
TaskManager.IMP.later(new Runnable() {
@Override
public void run() {
for (EntityPlayerMP player : players) {
boolean result = entry.trackingPlayers.remove(player);
if (result && entry.trackedEntity != player) {
entry.updatePlayerEntity(player);
}
}
}
}, 2);
} catch (Throwable e) {
MainUtil.handleError(e);
}
}
} catch (Throwable e) {
MainUtil.handleError(e);
} }
} }