From 7e7b9f4fdb3d98aff15dc492131f2a127b082bdd Mon Sep 17 00:00:00 2001 From: Brady Date: Tue, 2 Oct 2018 15:25:58 -0500 Subject: [PATCH] Make MemoryBehavior store data by world --- .../api/behavior/IMemoryBehavior.java | 9 ++++ .../baritone/behavior/MemoryBehavior.java | 54 ++++++++++++------- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/api/java/baritone/api/behavior/IMemoryBehavior.java b/src/api/java/baritone/api/behavior/IMemoryBehavior.java index ea5e9007..6b6df1dd 100644 --- a/src/api/java/baritone/api/behavior/IMemoryBehavior.java +++ b/src/api/java/baritone/api/behavior/IMemoryBehavior.java @@ -20,6 +20,8 @@ package baritone.api.behavior; import baritone.api.behavior.memory.IRememberedInventory; import net.minecraft.util.math.BlockPos; +import java.util.Map; + /** * @author Brady * @since 9/23/2018 @@ -33,4 +35,11 @@ public interface IMemoryBehavior extends IBehavior { * @return The remembered inventory */ IRememberedInventory getInventoryByPos(BlockPos pos); + + /** + * Gets the map of all block positions to their remembered inventories. + * + * @return Map of block positions to their respective remembered inventories + */ + Map getRememberedInventories(); } diff --git a/src/main/java/baritone/behavior/MemoryBehavior.java b/src/main/java/baritone/behavior/MemoryBehavior.java index b0980322..6570cbc2 100644 --- a/src/main/java/baritone/behavior/MemoryBehavior.java +++ b/src/main/java/baritone/behavior/MemoryBehavior.java @@ -19,9 +19,11 @@ package baritone.behavior; import baritone.api.behavior.IMemoryBehavior; import baritone.api.behavior.memory.IRememberedInventory; +import baritone.api.cache.IWorldData; import baritone.api.event.events.PacketEvent; import baritone.api.event.events.PlayerUpdateEvent; import baritone.api.event.events.type.EventState; +import baritone.cache.WorldProvider; import baritone.utils.Helper; import net.minecraft.item.ItemStack; import net.minecraft.network.Packet; @@ -43,15 +45,7 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior, H public static MemoryBehavior INSTANCE = new MemoryBehavior(); - /** - * Possible future inventories that we will be able to remember - */ - private final List futureInventories = new ArrayList<>(); - - /** - * The current remembered inventories - */ - private final Map rememberedInventories = new HashMap<>(); + private final Map worldDataContainers = new HashMap<>(); private MemoryBehavior() {} @@ -78,7 +72,7 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior, H TileEntityLockable lockable = (TileEntityLockable) tileEntity; int size = lockable.getSizeInventory(); - this.futureInventories.add(new FutureInventory(System.nanoTime() / 1000000L, size, lockable.getGuiID(), tileEntity.getPos())); + this.getCurrentContainer().futureInventories.add(new FutureInventory(System.nanoTime() / 1000000L, size, lockable.getGuiID(), tileEntity.getPos())); } } @@ -96,17 +90,19 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior, H if (p instanceof SPacketOpenWindow) { SPacketOpenWindow packet = event.cast(); - // Remove any entries that were created over a second ago, this should make up for INSANE latency - this.futureInventories.removeIf(i -> System.nanoTime() / 1000000L - i.time > 1000); + WorldDataContainer container = this.getCurrentContainer(); - this.futureInventories.stream() + // Remove any entries that were created over a second ago, this should make up for INSANE latency + container.futureInventories.removeIf(i -> System.nanoTime() / 1000000L - i.time > 1000); + + container.futureInventories.stream() .filter(i -> i.type.equals(packet.getGuiId()) && i.slots == packet.getSlotCount()) .findFirst().ifPresent(matched -> { // Remove the future inventory - this.futureInventories.remove(matched); + container.futureInventories.remove(matched); // Setup the remembered inventory - RememberedInventory inventory = this.rememberedInventories.computeIfAbsent(matched.pos, pos -> new RememberedInventory()); + RememberedInventory inventory = container.rememberedInventories.computeIfAbsent(matched.pos, pos -> new RememberedInventory()); inventory.windowId = packet.getWindowId(); inventory.size = packet.getSlotCount(); }); @@ -119,7 +115,7 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior, H } private Optional getInventoryFromWindow(int windowId) { - return this.rememberedInventories.values().stream().filter(i -> i.windowId == windowId).findFirst(); + return this.getCurrentContainer().rememberedInventories.values().stream().filter(i -> i.windowId == windowId).findFirst(); } private void updateInventory() { @@ -129,9 +125,31 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior, H }); } + private WorldDataContainer getCurrentContainer() { + return this.worldDataContainers.computeIfAbsent(WorldProvider.INSTANCE.getCurrentWorld(), data -> new WorldDataContainer()); + } + @Override public final RememberedInventory getInventoryByPos(BlockPos pos) { - return this.rememberedInventories.get(pos); + return this.getCurrentContainer().rememberedInventories.get(pos); + } + + @Override + public final Map getRememberedInventories() { + return Collections.unmodifiableMap(this.getCurrentContainer().rememberedInventories); + } + + private static final class WorldDataContainer { + + /** + * Possible future inventories that we will be able to remember + */ + private final List futureInventories = new ArrayList<>(); + + /** + * The current remembered inventories + */ + private final Map rememberedInventories = new HashMap<>(); } /** @@ -170,7 +188,7 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior, H /** * An inventory that we are aware of. *

- * Associated with a {@link BlockPos} in {@link MemoryBehavior#rememberedInventories}. + * Associated with a {@link BlockPos} in {@link WorldDataContainer#rememberedInventories}. */ public static class RememberedInventory implements IRememberedInventory {