Make MemoryBehavior store data by world

This commit is contained in:
Brady 2018-10-02 15:25:58 -05:00
parent 85e4a57c76
commit 7e7b9f4fdb
No known key found for this signature in database
GPG Key ID: 73A788379A197567
2 changed files with 45 additions and 18 deletions

View File

@ -20,6 +20,8 @@ package baritone.api.behavior;
import baritone.api.behavior.memory.IRememberedInventory; import baritone.api.behavior.memory.IRememberedInventory;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.Map;
/** /**
* @author Brady * @author Brady
* @since 9/23/2018 * @since 9/23/2018
@ -33,4 +35,11 @@ public interface IMemoryBehavior extends IBehavior {
* @return The remembered inventory * @return The remembered inventory
*/ */
IRememberedInventory getInventoryByPos(BlockPos pos); 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<BlockPos, IRememberedInventory> getRememberedInventories();
} }

View File

@ -19,9 +19,11 @@ package baritone.behavior;
import baritone.api.behavior.IMemoryBehavior; import baritone.api.behavior.IMemoryBehavior;
import baritone.api.behavior.memory.IRememberedInventory; import baritone.api.behavior.memory.IRememberedInventory;
import baritone.api.cache.IWorldData;
import baritone.api.event.events.PacketEvent; import baritone.api.event.events.PacketEvent;
import baritone.api.event.events.PlayerUpdateEvent; import baritone.api.event.events.PlayerUpdateEvent;
import baritone.api.event.events.type.EventState; import baritone.api.event.events.type.EventState;
import baritone.cache.WorldProvider;
import baritone.utils.Helper; import baritone.utils.Helper;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.network.Packet; import net.minecraft.network.Packet;
@ -43,15 +45,7 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior, H
public static MemoryBehavior INSTANCE = new MemoryBehavior(); public static MemoryBehavior INSTANCE = new MemoryBehavior();
/** private final Map<IWorldData, WorldDataContainer> worldDataContainers = new HashMap<>();
* Possible future inventories that we will be able to remember
*/
private final List<FutureInventory> futureInventories = new ArrayList<>();
/**
* The current remembered inventories
*/
private final Map<BlockPos, RememberedInventory> rememberedInventories = new HashMap<>();
private MemoryBehavior() {} private MemoryBehavior() {}
@ -78,7 +72,7 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior, H
TileEntityLockable lockable = (TileEntityLockable) tileEntity; TileEntityLockable lockable = (TileEntityLockable) tileEntity;
int size = lockable.getSizeInventory(); 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) { if (p instanceof SPacketOpenWindow) {
SPacketOpenWindow packet = event.cast(); SPacketOpenWindow packet = event.cast();
// Remove any entries that were created over a second ago, this should make up for INSANE latency WorldDataContainer container = this.getCurrentContainer();
this.futureInventories.removeIf(i -> System.nanoTime() / 1000000L - i.time > 1000);
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()) .filter(i -> i.type.equals(packet.getGuiId()) && i.slots == packet.getSlotCount())
.findFirst().ifPresent(matched -> { .findFirst().ifPresent(matched -> {
// Remove the future inventory // Remove the future inventory
this.futureInventories.remove(matched); container.futureInventories.remove(matched);
// Setup the remembered inventory // 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.windowId = packet.getWindowId();
inventory.size = packet.getSlotCount(); inventory.size = packet.getSlotCount();
}); });
@ -119,7 +115,7 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior, H
} }
private Optional<RememberedInventory> getInventoryFromWindow(int windowId) { private Optional<RememberedInventory> 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() { 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 @Override
public final RememberedInventory getInventoryByPos(BlockPos pos) { public final RememberedInventory getInventoryByPos(BlockPos pos) {
return this.rememberedInventories.get(pos); return this.getCurrentContainer().rememberedInventories.get(pos);
}
@Override
public final Map<BlockPos, IRememberedInventory> 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<FutureInventory> futureInventories = new ArrayList<>();
/**
* The current remembered inventories
*/
private final Map<BlockPos, RememberedInventory> rememberedInventories = new HashMap<>();
} }
/** /**
@ -170,7 +188,7 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior, H
/** /**
* An inventory that we are aware of. * An inventory that we are aware of.
* <p> * <p>
* Associated with a {@link BlockPos} in {@link MemoryBehavior#rememberedInventories}. * Associated with a {@link BlockPos} in {@link WorldDataContainer#rememberedInventories}.
*/ */
public static class RememberedInventory implements IRememberedInventory { public static class RememberedInventory implements IRememberedInventory {