Skip to content

Commit

Permalink
handle chunk packets that aren't ground-up continuous correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
DaMatrix committed May 2, 2020
1 parent 3ef7d7a commit 97497d3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ext {
//general things
javaVersion = "1.8"
minecraftVersion = "1.12.2"
programVersion = "0.2.5"
programVersion = "0.2.6"

//dependency things
apachecommonstextVersion = "1.3"
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/daporkchop/toobeetooteebot/util/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ public static final class Debug {

public static final class Packet {
public boolean received = false;
public boolean receivedBody = false;
public boolean preSent = false;
public boolean preSentBody = false;
public boolean postSent = false;
public boolean postSentBody = false;
}

public static final class Server {
Expand All @@ -113,6 +116,7 @@ public static final class Gui {
}

public static final class Log {
public boolean printDebug = false;
public boolean storeDebug = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ public class Constants {

loadConfig();

if (CONFIG.log.printDebug) {
DEFAULT_LOG.setLogAmount(LogAmount.DEBUG);
}
if (CONFIG.log.storeDebug) {
DEFAULT_LOG.addFile(new File(logFolder, String.format("%s-debug.log", date)), LogAmount.DEBUG);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,63 @@

package net.daporkchop.toobeetooteebot.util.cache.data.chunk;

import com.github.steveice10.mc.protocol.data.game.chunk.Chunk;
import com.github.steveice10.mc.protocol.data.game.chunk.Column;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.mc.protocol.data.game.world.block.UpdatedTileType;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerChunkDataPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerUpdateTileEntityPacket;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.packetlib.packet.Packet;
import lombok.NonNull;
import net.daporkchop.lib.math.vector.i.Vec2i;
import net.daporkchop.lib.math.vector.i.Vec3i;
import net.daporkchop.toobeetooteebot.util.cache.CachedData;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.Consumer;

import static net.daporkchop.toobeetooteebot.util.Constants.*;

/**
* @author DaPorkchop_
*/
public class ChunkCache implements CachedData {
public class ChunkCache implements CachedData, BiFunction<Column, Column, Column> {
protected final Map<Vec2i, Column> cache = new ConcurrentHashMap<>();

public void add(@NonNull Column column) {
if (this.cache.put(new Vec2i(column.getX(), column.getZ()), column) != null) {
CACHE_LOG.warn("Chunk (%d, %d) is already cached! (this is probably a server issue)", column.getX(), column.getZ());
} else {
CACHE_LOG.debug("Caching chunk (%d, %d)", column.getX(), column.getZ());
this.cache.merge(new Vec2i(column.getX(), column.getZ()), column, this);
CACHE_LOG.debug("Cached chunk (%d, %d)", column.getX(), column.getZ());
}

/**
* @deprecated do not call this directly!
*/
@Override
@Deprecated
public Column apply(@NonNull Column existing, @NonNull Column add) {
CACHE_LOG.warn("Chunk (%d, %d) is already cached, merging with existing", add.getX(), add.getZ());
Chunk[] chunks = existing.getChunks().clone();
for (int chunkY = 0; chunkY < 16; chunkY++) {
Chunk addChunk = add.getChunks()[chunkY];
if (addChunk == null) {
continue;
} else if (add.hasSkylight()) {
chunks[chunkY] = addChunk;
} else {
chunks[chunkY] = new Chunk(addChunk.getBlocks(), addChunk.getBlockLight(), chunks[chunkY].getSkyLight());
}
}

return new Column(
add.getX(), add.getZ(),
chunks,
add.hasBiomeData() ? add.getBiomeData() : existing.getBiomeData(),
add.getTileEntities());
}

public Column get(int x, int z) {
public Column get(int x, int z) {
return this.cache.get(new Vec2i(x, z));
}

public void remove(int x, int z) {
public void remove(int x, int z) {
CACHE_LOG.debug("Server telling us to uncache chunk (%d, %d)", x, z);
if (this.cache.remove(new Vec2i(x, z)) == null) {
CACHE_LOG.warn("Could not remove column (%d, %d)! this is probably a server issue", x, z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class HandlerRegistry<S extends Session> {
@SuppressWarnings("unchecked")
public <P extends Packet> boolean handleInbound(@NonNull P packet, @NonNull S session) {
if (CONFIG.debug.packet.received) {
this.logger.debug("Received packet: %s", packet.getClass());
this.logger.debug("Received packet: %s@%08x", CONFIG.debug.packet.receivedBody ? packet : packet.getClass(), System.identityHashCode(packet));
}
ObjObjBoolFunction<P, S> handler = (ObjObjBoolFunction<P, S>) this.inboundHandlers.get(packet.getClass());
return handler == null || handler.apply(packet, session);
Expand All @@ -64,7 +64,7 @@ public <P extends Packet> boolean handleInbound(@NonNull P packet, @NonNull S se
@SuppressWarnings("unchecked")
public <P extends Packet> P handleOutgoing(@NonNull P packet, @NonNull S session) {
if (CONFIG.debug.packet.preSent) {
this.logger.debug("Sending packet: %s", packet.getClass());
this.logger.debug("Sending packet: %s@%08x", CONFIG.debug.packet.preSentBody ? packet : packet.getClass(), System.identityHashCode(packet));
}
BiFunction<P, S, P> handler = (BiFunction<P, S, P>) this.outboundHandlers.get(packet.getClass());
return handler == null ? packet : handler.apply(packet, session);
Expand All @@ -73,7 +73,7 @@ public <P extends Packet> P handleOutgoing(@NonNull P packet, @NonNull S session
@SuppressWarnings("unchecked")
public <P extends Packet> void handlePostOutgoing(@NonNull P packet, @NonNull S session) {
if (CONFIG.debug.packet.postSent) {
this.logger.debug("Sent packet: %s", packet.getClass());
this.logger.debug("Sent packet: %s@%08x", CONFIG.debug.packet.postSentBody ? packet : packet.getClass(), System.identityHashCode(packet));
}
PostOutgoingHandler<P, S> handler = (PostOutgoingHandler<P, S>) this.postOutboundHandlers.get(packet.getClass());
if (handler != null) {
Expand Down

0 comments on commit 97497d3

Please sign in to comment.