From e42c19bfec7f4769f53421481cc6eae8810ce9b2 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sat, 27 Jun 2020 22:25:47 -0700 Subject: [PATCH 1/4] thebes uwu --- src/api/java/baritone/api/Settings.java | 11 ++++++++++- src/main/java/baritone/process/BuilderProcess.java | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index 1bbde3b2b..283e4e0a6 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -30,8 +30,8 @@ import java.awt.*; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.util.*; import java.util.List; +import java.util.*; import java.util.function.Consumer; /** @@ -193,6 +193,15 @@ public final class Settings { ))); + /** + * A list of blocks to become air + *

+ * If a schematic asks for a block on this list, only air will be accepted at that location (and nothing on buildIgnoreBlocks) + */ + public final Setting> okIfAir = new Setting<>(new ArrayList<>(Arrays.asList( + + ))); + /** * If this is true, the builder will treat all non-air blocks as correct. It will only place new blocks. */ diff --git a/src/main/java/baritone/process/BuilderProcess.java b/src/main/java/baritone/process/BuilderProcess.java index 17a8f5f94..b8f77ce24 100644 --- a/src/main/java/baritone/process/BuilderProcess.java +++ b/src/main/java/baritone/process/BuilderProcess.java @@ -206,6 +206,7 @@ public final class BuilderProcess extends BaritoneProcessHelper implements IBuil continue; // irrelevant } IBlockState curr = bcc.bsi.get0(x, y, z); + Blocks.ICE; if (curr.getBlock() != Blocks.AIR && !(curr.getBlock() instanceof BlockLiquid) && !valid(curr, desired, false)) { BetterBlockPos pos = new BetterBlockPos(x, y, z); Optional rot = RotationUtils.reachable(ctx.player(), pos, ctx.playerController().getBlockReachDistance()); @@ -773,10 +774,12 @@ public final class BuilderProcess extends BaritoneProcessHelper implements IBuil if (desired == null) { return true; } - // TODO more complicated comparison logic I guess if (current.getBlock() instanceof BlockLiquid && Baritone.settings().okIfWater.value) { return true; } + if (current.getBlock() instanceof BlockAir && Baritone.settings().okIfAir.value.contains(desired.getBlock())) { + return true; + } if (desired.getBlock() instanceof BlockAir && Baritone.settings().buildIgnoreBlocks.value.contains(current.getBlock())) { return true; } From e6ba4ef309bf356dfd92e571bc9e55801c0c86b3 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Mon, 17 Aug 2020 15:00:57 -0700 Subject: [PATCH 2/4] we don't compile here --- src/main/java/baritone/process/BuilderProcess.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/baritone/process/BuilderProcess.java b/src/main/java/baritone/process/BuilderProcess.java index b45a7f3b3..ecb40a224 100644 --- a/src/main/java/baritone/process/BuilderProcess.java +++ b/src/main/java/baritone/process/BuilderProcess.java @@ -206,7 +206,6 @@ public final class BuilderProcess extends BaritoneProcessHelper implements IBuil continue; // irrelevant } IBlockState curr = bcc.bsi.get0(x, y, z); - Blocks.ICE; if (curr.getBlock() != Blocks.AIR && !(curr.getBlock() instanceof BlockLiquid) && !valid(curr, desired, false)) { BetterBlockPos pos = new BetterBlockPos(x, y, z); Optional rot = RotationUtils.reachable(ctx.player(), pos, ctx.playerController().getBlockReachDistance()); From 38d047dbd1de4fc48619a59f3b06da381ac311b4 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Mon, 17 Aug 2020 15:19:11 -0700 Subject: [PATCH 3/4] make the packer queue super cute and deduplicated --- src/main/java/baritone/cache/CachedWorld.java | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/main/java/baritone/cache/CachedWorld.java b/src/main/java/baritone/cache/CachedWorld.java index 23d7ca8c8..1d1132465 100644 --- a/src/main/java/baritone/cache/CachedWorld.java +++ b/src/main/java/baritone/cache/CachedWorld.java @@ -26,6 +26,7 @@ import baritone.api.utils.Helper; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.ChunkPos; import net.minecraft.world.chunk.Chunk; import java.io.IOException; @@ -33,6 +34,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.LinkedBlockingQueue; /** @@ -56,7 +59,17 @@ public final class CachedWorld implements ICachedWorld, Helper { */ private final String directory; - private final LinkedBlockingQueue toPack = new LinkedBlockingQueue<>(); + /** + * Queue of positions to pack. Refers to the toPackMap, in that every element of this queue will be a + * key in that map. + */ + private final LinkedBlockingQueue toPackQueue = new LinkedBlockingQueue<>(); + + /** + * All chunk positions pending packing. This map will be updated in-place if a new update to the chunk occurs + * while waiting in the queue for the packer thread to get to it. + */ + private final Map toPackMap = new ConcurrentHashMap<>(); private final int dimension; @@ -89,10 +102,8 @@ public final class CachedWorld implements ICachedWorld, Helper { @Override public final void queueForPacking(Chunk chunk) { - try { - toPack.put(chunk); - } catch (InterruptedException e) { - e.printStackTrace(); + if (toPackMap.put(chunk.getPos(), chunk) == null) { + toPackQueue.add(chunk.getPos()); } } @@ -293,13 +304,9 @@ public final class CachedWorld implements ICachedWorld, Helper { public void run() { while (true) { - // TODO: Add CachedWorld unloading to remove the redundancy of having this - LinkedBlockingQueue queue = toPack; - if (queue == null) { - break; - } try { - Chunk chunk = queue.take(); + ChunkPos pos = toPackQueue.take(); + Chunk chunk = toPackMap.remove(pos); CachedChunk cached = ChunkPacker.pack(chunk); CachedWorld.this.updateCachedChunk(cached); //System.out.println("Processed chunk at " + chunk.x + "," + chunk.z); From f3561cab47693799c01b6a59ee9fbaf2b7c0842c Mon Sep 17 00:00:00 2001 From: Leijurv Date: Mon, 17 Aug 2020 15:54:42 -0700 Subject: [PATCH 4/4] SUPER cute code for repackOnAnyBlockChange --- src/api/java/baritone/api/Settings.java | 5 ++ .../mixins/MixinNetHandlerPlayClient.java | 67 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index 4e622dc2a..4b44d3e57 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -439,6 +439,11 @@ public final class Settings { */ public final Setting simplifyUnloadedYCoord = new Setting<>(true); + /** + * Whenever a block changes, repack the whole chunk that it's in + */ + public final Setting repackOnAnyBlockChange = new Setting<>(true); + /** * If a movement takes this many ticks more than its initial cost estimate, cancel it */ diff --git a/src/launch/java/baritone/launch/mixins/MixinNetHandlerPlayClient.java b/src/launch/java/baritone/launch/mixins/MixinNetHandlerPlayClient.java index 96e9cc29d..f1c1f7972 100644 --- a/src/launch/java/baritone/launch/mixins/MixinNetHandlerPlayClient.java +++ b/src/launch/java/baritone/launch/mixins/MixinNetHandlerPlayClient.java @@ -17,14 +17,19 @@ package baritone.launch.mixins; +import baritone.Baritone; import baritone.api.BaritoneAPI; import baritone.api.IBaritone; import baritone.api.event.events.ChunkEvent; import baritone.api.event.events.type.EventState; +import baritone.cache.CachedChunk; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.network.NetHandlerPlayClient; +import net.minecraft.network.play.server.SPacketBlockChange; import net.minecraft.network.play.server.SPacketChunkData; import net.minecraft.network.play.server.SPacketCombatEvent; +import net.minecraft.network.play.server.SPacketMultiBlockChange; +import net.minecraft.util.math.ChunkPos; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -80,6 +85,68 @@ public class MixinNetHandlerPlayClient { } } + @Inject( + method = "handleBlockChange", + at = @At("RETURN") + ) + private void postHandleBlockChange(SPacketBlockChange packetIn, CallbackInfo ci) { + if (!Baritone.settings().repackOnAnyBlockChange.value) { + return; + } + if (!CachedChunk.BLOCKS_TO_KEEP_TRACK_OF.contains(packetIn.getBlockState().getBlock())) { + return; + } + for (IBaritone ibaritone : BaritoneAPI.getProvider().getAllBaritones()) { + EntityPlayerSP player = ibaritone.getPlayerContext().player(); + if (player != null && player.connection == (NetHandlerPlayClient) (Object) this) { + ibaritone.getGameEventHandler().onChunkEvent( + new ChunkEvent( + EventState.POST, + ChunkEvent.Type.POPULATE_FULL, + packetIn.getBlockPosition().getX() >> 4, + packetIn.getBlockPosition().getZ() >> 4 + ) + ); + } + } + } + + @Inject( + method = "handleMultiBlockChange", + at = @At("RETURN") + ) + private void postHandleMultiBlockChange(SPacketMultiBlockChange packetIn, CallbackInfo ci) { + if (!Baritone.settings().repackOnAnyBlockChange.value) { + return; + } + if (packetIn.getChangedBlocks().length == 0) { + return; + } + https://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.15 + { + for (SPacketMultiBlockChange.BlockUpdateData update : packetIn.getChangedBlocks()) { + if (CachedChunk.BLOCKS_TO_KEEP_TRACK_OF.contains(update.getBlockState().getBlock())) { + break https; + } + } + return; + } + ChunkPos pos = new ChunkPos(packetIn.getChangedBlocks()[0].getPos()); + for (IBaritone ibaritone : BaritoneAPI.getProvider().getAllBaritones()) { + EntityPlayerSP player = ibaritone.getPlayerContext().player(); + if (player != null && player.connection == (NetHandlerPlayClient) (Object) this) { + ibaritone.getGameEventHandler().onChunkEvent( + new ChunkEvent( + EventState.POST, + ChunkEvent.Type.POPULATE_FULL, + pos.x, + pos.z + ) + ); + } + } + } + @Inject( method = "handleCombatEvent", at = @At(