use of io.xol.chunkstories.api.world.chunk.ChunkHolder in project chunkstories by Hugobros3.
the class RemotePlayerLoadingAgent method handleClientRequest.
public void handleClientRequest(PacketWorldUser packet) {
try {
lock.lock();
if (packet.getType() == Type.REGISTER_CHUNK) {
int handle = chunkHandle(packet.getX(), packet.getY(), packet.getZ());
if (usedChunksHandles.add(handle)) {
ChunkHolder holder = player.getWorld().aquireChunkHolder(player, packet.getX(), packet.getY(), packet.getZ());
// assume it not being null because it's the supposed behaviour
assert holder != null;
} else {
System.out.println("Received twin request for chunk handle " + handle);
}
} else if (packet.getType() == Type.UNREGISTER_CHUNK) {
int handle = chunkHandle(packet.getX(), packet.getY(), packet.getZ());
// If we actually owned this handle
if (usedChunksHandles.remove(handle)) {
ChunkHolder holder = player.getWorld().getRegionChunkCoordinates(packet.getX(), packet.getY(), packet.getZ()).getChunkHolder(packet.getX(), packet.getY(), packet.getZ());
// We can assert the chunk holder exists because at this point it MUST be held by this very loading agent !
assert holder != null;
holder.unregisterUser(player);
} else {
System.out.println("Client requested to unregister something he never registered in the first place.");
}
} else if (packet.getType() == Type.REGISTER_SUMMARY) {
int handle = summaryHandle(packet.getX(), packet.getZ());
int[] check = summary(handle);
if (check[0] != packet.getX() || check[1] != packet.getZ()) {
System.out.println("major fuck up with handle " + handle);
System.out.println("should have been (" + packet.getX() + ", " + packet.getZ() + ")");
System.out.println("kys");
System.exit(-1);
}
if (usedRegionHandles.add(handle)) {
Heightmap regionSummary = player.getWorld().getRegionsSummariesHolder().aquireHeightmap(player, packet.getX(), packet.getZ());
// assume it not being null because it's the supposed behaviour
assert regionSummary != null;
} else {
System.out.println("Received twin request for region summary (" + packet.getX() + ", " + packet.getZ() + ")");
}
} else if (packet.getType() == Type.UNREGISTER_SUMMARY) {
int handle = summaryHandle(packet.getX(), packet.getZ());
// If we actually owned this handle
if (usedRegionHandles.remove(handle)) {
Heightmap regionSummary = player.getWorld().getRegionsSummariesHolder().getHeightmap(packet.getX(), packet.getZ());
// We can assert the region summary exists because at this point it MUST be held by this very loading agent !
assert regionSummary != null;
regionSummary.unregisterUser(player);
} else {
System.out.println("Client requested to unregister summary (" + packet.getX() + ", " + packet.getZ() + ") that he never registered in the first place.");
}
} else {
// TODO hurt him like he hurted you
// throw new IllegalPacketException(packet);
}
} finally {
lock.unlock();
}
}
use of io.xol.chunkstories.api.world.chunk.ChunkHolder in project chunkstories by Hugobros3.
the class RemotePlayerLoadingAgent method destroy.
public void destroy() {
try {
lock.lock();
for (int handle : this.usedChunksHandles) {
int[] pos = chunk(handle);
RegionImplementation region = player.getWorld().getRegionChunkCoordinates(pos[0], pos[1], pos[2]);
// assert region != null;
if (region != null) {
ChunkHolder holder = region.getChunkHolder(pos[0], pos[1], pos[2]);
// assert holder != null; // We can assert the chunk holder exists because at this point it MUST be held by this very loading agent !
if (holder != null) {
holder.unregisterUser(player);
continue;
}
}
player.getContext().logger().error("Error while disconnecting player: " + player + ", chunkholder at [" + pos[0] + ":" + pos[1] + ":" + pos[2] + "] wasn't loaded even thought it was part of that player's subscriptions list");
}
this.usedChunksHandles.clear();
for (int handle : this.usedRegionHandles) {
int[] pos = summary(handle);
Heightmap regionSummary = player.getWorld().getRegionsSummariesHolder().getHeightmap(pos[0], pos[1]);
// assert regionSummary != null; // We can assert the region summary exists because at this point it MUST be held by this very loading agent !
if (regionSummary != null)
regionSummary.unregisterUser(player);
else {
player.getContext().logger().error("Error while disconnecting player: " + player + ", region at [" + pos[0] + " :" + pos[1] + "] wasn't loaded even thought it was part of that player's subscriptions list");
}
}
this.usedRegionHandles.clear();
destroyed = true;
} catch (Exception e) {
player.getContext().logger().error("Error while disconnecting player: " + player + ", exception thrown while freeing his held world data.");
player.getContext().logger().error(e.getMessage());
} finally {
lock.unlock();
}
}
use of io.xol.chunkstories.api.world.chunk.ChunkHolder in project chunkstories by Hugobros3.
the class LocalClientLoadingAgent method handleServerResponse.
public void handleServerResponse(PacketWorldUser packet) throws IllegalPacketException {
try {
lock.lock();
// The server refused to register us to this chunk. We gracefully accept.
if (packet.getType() == Type.UNREGISTER_CHUNK) {
ChunkHolder holder = world.getRegionChunkCoordinates(packet.getX(), packet.getY(), packet.getZ()).getChunkHolder(packet.getX(), packet.getY(), packet.getZ());
// Apparently we already figured we didn't need this anyway
if (holder == null)
return;
WorldInfo worldInfo = world.getWorldInfo();
WorldInfo.WorldSize size = worldInfo.getSize();
int filteredChunkX = holder.getChunkCoordinateX() & (size.maskForChunksCoordinates);
int filteredChunkY = Math2.clampi(holder.getChunkCoordinateY(), 0, 31);
int filteredChunkZ = holder.getChunkCoordinateZ() & (size.maskForChunksCoordinates);
int summed = ((filteredChunkX << size.bitlengthOfVerticalChunksCoordinates) | filteredChunkY) << size.bitlengthOfHorizontalChunksCoordinates | filteredChunkZ;
// We remove it from our list
fastChunksMask.remove(summed);
usedChunks.remove(holder);
// And we unsub.
holder.unregisterUser(player);
// This is the same but for region summaries
} else if (packet.getType() == Type.UNREGISTER_SUMMARY) {
Heightmap regionSummary = world.getRegionsSummariesHolder().getHeightmap(packet.getX(), packet.getZ());
if (regionSummary == null)
return;
usedRegionSummaries.remove(regionSummary);
regionSummary.unregisterUser(player);
} else
// We only expect UNREGISTER packets from the server !
throw new IllegalPacketException(packet);
} finally {
lock.unlock();
}
}
use of io.xol.chunkstories.api.world.chunk.ChunkHolder in project chunkstories by Hugobros3.
the class HashMapWorldRegionsHolder method aquireChunkHolder.
/**
* Atomically grabs or create a region and registers the asked holder
*/
public ChunkHolder aquireChunkHolder(WorldUser user, int chunkX, int chunkY, int chunkZ) {
if (chunkY < 0 || chunkY > world.getMaxHeight() / 32)
return null;
noConcurrentRegionCreationDestruction.acquireUninterruptibly();
ChunkHolder holder = this.getOrCreateRegion(chunkX / 8, chunkY / 8, chunkZ / 8).getChunkHolder(chunkX, chunkY, chunkZ);
boolean userAdded = holder.registerUser(user);
noConcurrentRegionCreationDestruction.release();
return userAdded ? holder : holder;
}
Aggregations