mirror of https://github.com/cabaletta/baritone
overhaul memory
This commit is contained in:
parent
b0066a93ca
commit
0236cb35d9
|
@ -18,7 +18,6 @@
|
||||||
package baritone.api;
|
package baritone.api;
|
||||||
|
|
||||||
import baritone.api.behavior.ILookBehavior;
|
import baritone.api.behavior.ILookBehavior;
|
||||||
import baritone.api.behavior.IMemoryBehavior;
|
|
||||||
import baritone.api.behavior.IPathingBehavior;
|
import baritone.api.behavior.IPathingBehavior;
|
||||||
import baritone.api.cache.IWorldProvider;
|
import baritone.api.cache.IWorldProvider;
|
||||||
import baritone.api.event.listener.IEventBus;
|
import baritone.api.event.listener.IEventBus;
|
||||||
|
@ -47,12 +46,6 @@ public interface IBaritone {
|
||||||
*/
|
*/
|
||||||
ILookBehavior getLookBehavior();
|
ILookBehavior getLookBehavior();
|
||||||
|
|
||||||
/**
|
|
||||||
* @return The {@link IMemoryBehavior} instance
|
|
||||||
* @see IMemoryBehavior
|
|
||||||
*/
|
|
||||||
IMemoryBehavior getMemoryBehavior();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The {@link IMineProcess} instance
|
* @return The {@link IMineProcess} instance
|
||||||
* @see IMineProcess
|
* @see IMineProcess
|
||||||
|
|
|
@ -15,9 +15,8 @@
|
||||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package baritone.api.behavior;
|
package baritone.api.cache;
|
||||||
|
|
||||||
import baritone.api.behavior.memory.IRememberedInventory;
|
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -26,7 +25,7 @@ import java.util.Map;
|
||||||
* @author Brady
|
* @author Brady
|
||||||
* @since 9/23/2018
|
* @since 9/23/2018
|
||||||
*/
|
*/
|
||||||
public interface IMemoryBehavior extends IBehavior {
|
public interface IContainerMemory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a remembered inventory by its block position.
|
* Gets a remembered inventory by its block position.
|
|
@ -15,7 +15,7 @@
|
||||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package baritone.api.behavior.memory;
|
package baritone.api.cache;
|
||||||
|
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
|
@ -36,4 +36,10 @@ public interface IWorldData {
|
||||||
* @return The waypoint collection for this world
|
* @return The waypoint collection for this world
|
||||||
*/
|
*/
|
||||||
IWaypointCollection getWaypoints();
|
IWaypointCollection getWaypoints();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The {@link IContainerMemory} instance
|
||||||
|
* @see IContainerMemory
|
||||||
|
*/
|
||||||
|
IContainerMemory getContainerMemory();
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,11 +174,6 @@ public class Baritone implements IBaritone {
|
||||||
return this.lookBehavior;
|
return this.lookBehavior;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public MemoryBehavior getMemoryBehavior() {
|
|
||||||
return this.memoryBehavior;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MineProcess getMineProcess() {
|
public MineProcess getMineProcess() {
|
||||||
return this.mineProcess;
|
return this.mineProcess;
|
||||||
|
|
|
@ -18,17 +18,17 @@
|
||||||
package baritone.behavior;
|
package baritone.behavior;
|
||||||
|
|
||||||
import baritone.Baritone;
|
import baritone.Baritone;
|
||||||
import baritone.api.behavior.IMemoryBehavior;
|
|
||||||
import baritone.api.behavior.memory.IRememberedInventory;
|
|
||||||
import baritone.api.cache.IWorldData;
|
|
||||||
import baritone.api.event.events.BlockInteractEvent;
|
import baritone.api.event.events.BlockInteractEvent;
|
||||||
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.ContainerMemory;
|
||||||
import baritone.cache.Waypoint;
|
import baritone.cache.Waypoint;
|
||||||
|
import baritone.pathing.movement.CalculationContext;
|
||||||
import baritone.utils.BlockStateInterface;
|
import baritone.utils.BlockStateInterface;
|
||||||
|
import net.minecraft.block.Block;
|
||||||
import net.minecraft.block.BlockBed;
|
import net.minecraft.block.BlockBed;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.init.Blocks;
|
||||||
import net.minecraft.network.Packet;
|
import net.minecraft.network.Packet;
|
||||||
import net.minecraft.network.play.client.CPacketCloseWindow;
|
import net.minecraft.network.play.client.CPacketCloseWindow;
|
||||||
import net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock;
|
import net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock;
|
||||||
|
@ -36,17 +36,19 @@ import net.minecraft.network.play.server.SPacketCloseWindow;
|
||||||
import net.minecraft.network.play.server.SPacketOpenWindow;
|
import net.minecraft.network.play.server.SPacketOpenWindow;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraft.tileentity.TileEntityLockable;
|
import net.minecraft.tileentity.TileEntityLockable;
|
||||||
|
import net.minecraft.util.EnumFacing;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Brady
|
* @author Brady
|
||||||
* @since 8/6/2018
|
* @since 8/6/2018
|
||||||
*/
|
*/
|
||||||
public final class MemoryBehavior extends Behavior implements IMemoryBehavior {
|
public final class MemoryBehavior extends Behavior {
|
||||||
|
|
||||||
private final Map<IWorldData, WorldDataContainer> worldDataContainers = new HashMap<>();
|
private final List<FutureInventory> futureInventories = new ArrayList<>(); // this is per-bot
|
||||||
|
|
||||||
public MemoryBehavior(Baritone baritone) {
|
public MemoryBehavior(Baritone baritone) {
|
||||||
super(baritone);
|
super(baritone);
|
||||||
|
@ -68,14 +70,27 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior {
|
||||||
CPacketPlayerTryUseItemOnBlock packet = event.cast();
|
CPacketPlayerTryUseItemOnBlock packet = event.cast();
|
||||||
|
|
||||||
TileEntity tileEntity = ctx.world().getTileEntity(packet.getPos());
|
TileEntity tileEntity = ctx.world().getTileEntity(packet.getPos());
|
||||||
|
if (tileEntity != null) {
|
||||||
|
System.out.println(tileEntity.getPos() + " " + packet.getPos());
|
||||||
|
System.out.println(tileEntity);
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure the TileEntity is a container of some sort
|
// Ensure the TileEntity is a container of some sort
|
||||||
if (tileEntity instanceof TileEntityLockable) {
|
if (tileEntity instanceof TileEntityLockable) {
|
||||||
|
|
||||||
TileEntityLockable lockable = (TileEntityLockable) tileEntity;
|
TileEntityLockable lockable = (TileEntityLockable) tileEntity;
|
||||||
int size = lockable.getSizeInventory();
|
int size = lockable.getSizeInventory();
|
||||||
|
BlockPos position = tileEntity.getPos();
|
||||||
|
BlockPos adj = neighboringConnectedBlock(position);
|
||||||
|
System.out.println(position + " " + adj);
|
||||||
|
if (adj != null) {
|
||||||
|
size *= 2; // double chest or double trapped chest
|
||||||
|
if (adj.getX() < position.getX() || adj.getZ() < position.getZ()) {
|
||||||
|
position = adj; // standardize on the lower coordinate, regardless of which side of the large chest we right clicked
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.getCurrentContainer().futureInventories.add(new FutureInventory(System.nanoTime() / 1000000L, size, lockable.getGuiID(), tileEntity.getPos()));
|
this.futureInventories.add(new FutureInventory(System.nanoTime() / 1000000L, size, lockable.getGuiID(), position));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,22 +107,19 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior {
|
||||||
if (event.getState() == EventState.PRE) {
|
if (event.getState() == EventState.PRE) {
|
||||||
if (p instanceof SPacketOpenWindow) {
|
if (p instanceof SPacketOpenWindow) {
|
||||||
SPacketOpenWindow packet = event.cast();
|
SPacketOpenWindow packet = event.cast();
|
||||||
|
|
||||||
WorldDataContainer container = this.getCurrentContainer();
|
|
||||||
|
|
||||||
// Remove any entries that were created over a second ago, this should make up for INSANE latency
|
// 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);
|
futureInventories.removeIf(i -> System.nanoTime() / 1000000L - i.time > 1000);
|
||||||
|
|
||||||
container.futureInventories.stream()
|
System.out.println("Received packet " + packet.getGuiId() + " " + packet.getEntityId() + " " + packet.getSlotCount() + " " + packet.getWindowId());
|
||||||
|
|
||||||
|
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
|
||||||
container.futureInventories.remove(matched);
|
futureInventories.remove(matched);
|
||||||
|
|
||||||
// Setup the remembered inventory
|
// Setup the remembered inventory
|
||||||
RememberedInventory inventory = container.rememberedInventories.computeIfAbsent(matched.pos, pos -> new RememberedInventory());
|
getCurrentContainer().setup(matched.pos, packet.getWindowId(), packet.getSlotCount());
|
||||||
inventory.windowId = packet.getWindowId();
|
|
||||||
inventory.size = packet.getSlotCount();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,43 +141,28 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior {
|
||||||
baritone.getWorldProvider().getCurrentWorld().getWaypoints().addWaypoint(new Waypoint("death", Waypoint.Tag.DEATH, ctx.playerFeet()));
|
baritone.getWorldProvider().getCurrentWorld().getWaypoints().addWaypoint(new Waypoint("death", Waypoint.Tag.DEATH, ctx.playerFeet()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<RememberedInventory> getInventoryFromWindow(int windowId) {
|
|
||||||
return this.getCurrentContainer().rememberedInventories.values().stream().filter(i -> i.windowId == windowId).findFirst();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateInventory() {
|
private void updateInventory() {
|
||||||
getInventoryFromWindow(ctx.player().openContainer.windowId).ifPresent(inventory -> {
|
getCurrentContainer().getInventoryFromWindow(ctx.player().openContainer.windowId).ifPresent(inventory -> inventory.updateFromOpenWindow(ctx));
|
||||||
inventory.items.clear();
|
|
||||||
inventory.items.addAll(ctx.player().openContainer.getInventory().subList(0, inventory.size));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private WorldDataContainer getCurrentContainer() {
|
private ContainerMemory getCurrentContainer() {
|
||||||
return this.worldDataContainers.computeIfAbsent(baritone.getWorldProvider().getCurrentWorld(), data -> new WorldDataContainer());
|
return (ContainerMemory) baritone.getWorldProvider().getCurrentWorld().getContainerMemory();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private BlockPos neighboringConnectedBlock(BlockPos in) {
|
||||||
public final synchronized RememberedInventory getInventoryByPos(BlockPos pos) {
|
BlockStateInterface bsi = new CalculationContext(baritone).bsi();
|
||||||
return this.getCurrentContainer().rememberedInventories.get(pos);
|
Block block = bsi.get0(in).getBlock();
|
||||||
}
|
if (block != Blocks.TRAPPED_CHEST && block != Blocks.CHEST) {
|
||||||
|
return null; // other things that have contents, but can be placed adjacent without combining
|
||||||
@Override
|
}
|
||||||
public final synchronized Map<BlockPos, IRememberedInventory> getRememberedInventories() {
|
for (int i = 0; i < 4; i++) {
|
||||||
// make a copy since this map is modified from the packet thread
|
BlockPos adj = in.offset(EnumFacing.byHorizontalIndex(i));
|
||||||
return new HashMap<>(this.getCurrentContainer().rememberedInventories);
|
if (bsi.get0(adj).getBlock() == block) {
|
||||||
}
|
return adj;
|
||||||
|
}
|
||||||
private static final class WorldDataContainer {
|
}
|
||||||
|
return null;
|
||||||
/**
|
|
||||||
* 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<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -198,43 +195,7 @@ public final class MemoryBehavior extends Behavior implements IMemoryBehavior {
|
||||||
this.slots = slots;
|
this.slots = slots;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.pos = pos;
|
this.pos = pos;
|
||||||
}
|
System.out.println("Future inventory created " + time + " " + slots + " " + type + " " + pos);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An inventory that we are aware of.
|
|
||||||
* <p>
|
|
||||||
* Associated with a {@link BlockPos} in {@link WorldDataContainer#rememberedInventories}.
|
|
||||||
*/
|
|
||||||
public static class RememberedInventory implements IRememberedInventory {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The list of items in the inventory
|
|
||||||
*/
|
|
||||||
private final List<ItemStack> items;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The last known window ID of the inventory
|
|
||||||
*/
|
|
||||||
private int windowId;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The size of the inventory
|
|
||||||
*/
|
|
||||||
private int size;
|
|
||||||
|
|
||||||
private RememberedInventory() {
|
|
||||||
this.items = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final List<ItemStack> getContents() {
|
|
||||||
return Collections.unmodifiableList(this.items);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final int getSize() {
|
|
||||||
return this.size;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,106 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Baritone.
|
||||||
|
*
|
||||||
|
* Baritone is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Baritone is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package baritone.cache;
|
||||||
|
|
||||||
|
import baritone.api.cache.IContainerMemory;
|
||||||
|
import baritone.api.cache.IRememberedInventory;
|
||||||
|
import baritone.api.utils.IPlayerContext;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class ContainerMemory implements IContainerMemory {
|
||||||
|
public ContainerMemory(Path saveTo) {
|
||||||
|
// eventually
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current remembered inventories
|
||||||
|
*/
|
||||||
|
private final Map<BlockPos, RememberedInventory> inventories = new HashMap<>();
|
||||||
|
|
||||||
|
public synchronized void setup(BlockPos pos, int windowId, int slotCount) {
|
||||||
|
RememberedInventory inventory = inventories.computeIfAbsent(pos, x -> new RememberedInventory());
|
||||||
|
inventory.windowId = windowId;
|
||||||
|
inventory.size = slotCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized Optional<RememberedInventory> getInventoryFromWindow(int windowId) {
|
||||||
|
return inventories.values().stream().filter(i -> i.windowId == windowId).findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final synchronized RememberedInventory getInventoryByPos(BlockPos pos) {
|
||||||
|
return inventories.get(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final synchronized Map<BlockPos, IRememberedInventory> getRememberedInventories() {
|
||||||
|
// make a copy since this map is modified from the packet thread
|
||||||
|
return new HashMap<>(inventories);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An inventory that we are aware of.
|
||||||
|
* <p>
|
||||||
|
* Associated with a {@link BlockPos} in {@link WorldDataContainer#rememberedInventories}.
|
||||||
|
*/
|
||||||
|
public static class RememberedInventory implements IRememberedInventory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The list of items in the inventory
|
||||||
|
*/
|
||||||
|
private final List<ItemStack> items;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The last known window ID of the inventory
|
||||||
|
*/
|
||||||
|
private int windowId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The size of the inventory
|
||||||
|
*/
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
private RememberedInventory() {
|
||||||
|
this.items = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final List<ItemStack> getContents() {
|
||||||
|
return Collections.unmodifiableList(this.items);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final int getSize() {
|
||||||
|
return this.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWindowId() {
|
||||||
|
return this.windowId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateFromOpenWindow(IPlayerContext ctx) {
|
||||||
|
items.clear();
|
||||||
|
items.addAll(ctx.player().openContainer.getInventory().subList(0, size));
|
||||||
|
System.out.println("Saved " + items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,6 +19,7 @@ package baritone.cache;
|
||||||
|
|
||||||
import baritone.Baritone;
|
import baritone.Baritone;
|
||||||
import baritone.api.cache.ICachedWorld;
|
import baritone.api.cache.ICachedWorld;
|
||||||
|
import baritone.api.cache.IContainerMemory;
|
||||||
import baritone.api.cache.IWaypointCollection;
|
import baritone.api.cache.IWaypointCollection;
|
||||||
import baritone.api.cache.IWorldData;
|
import baritone.api.cache.IWorldData;
|
||||||
|
|
||||||
|
@ -33,6 +34,7 @@ public class WorldData implements IWorldData {
|
||||||
|
|
||||||
public final CachedWorld cache;
|
public final CachedWorld cache;
|
||||||
private final Waypoints waypoints;
|
private final Waypoints waypoints;
|
||||||
|
private final ContainerMemory containerMemory;
|
||||||
//public final MapData map;
|
//public final MapData map;
|
||||||
public final Path directory;
|
public final Path directory;
|
||||||
public final int dimension;
|
public final int dimension;
|
||||||
|
@ -41,6 +43,7 @@ public class WorldData implements IWorldData {
|
||||||
this.directory = directory;
|
this.directory = directory;
|
||||||
this.cache = new CachedWorld(directory.resolve("cache"), dimension);
|
this.cache = new CachedWorld(directory.resolve("cache"), dimension);
|
||||||
this.waypoints = new Waypoints(directory.resolve("waypoints"));
|
this.waypoints = new Waypoints(directory.resolve("waypoints"));
|
||||||
|
this.containerMemory = new ContainerMemory(directory.resolve("containers"));
|
||||||
this.dimension = dimension;
|
this.dimension = dimension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,4 +63,9 @@ public class WorldData implements IWorldData {
|
||||||
public IWaypointCollection getWaypoints() {
|
public IWaypointCollection getWaypoints() {
|
||||||
return this.waypoints;
|
return this.waypoints;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IContainerMemory getContainerMemory() {
|
||||||
|
return this.containerMemory;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue