Skip to content

Commit

Permalink
Make inventory code a lot more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Apr 8, 2024
1 parent 721e6a3 commit d53a604
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 383 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,13 @@ public enum ArmorType {
ArmorType(ItemType... itemTypes) {
this.itemTypes = Arrays.asList(itemTypes);
}

public EquipmentSlot toEquipmentSlot() {
return switch (this) {
case HELMET -> EquipmentSlot.HEAD;
case CHESTPLATE -> EquipmentSlot.CHEST;
case LEGGINGS -> EquipmentSlot.LEGS;
case BOOTS -> EquipmentSlot.FEET;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import com.soulfiremc.server.pathfinding.Costs;
import com.soulfiremc.server.pathfinding.SFVec3i;
import com.soulfiremc.server.protocol.BotConnection;
import com.soulfiremc.server.protocol.bot.container.SFItemStack;
import com.soulfiremc.server.util.BlockTypeHelper;
import com.soulfiremc.server.util.TimeUtil;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cloudburstmc.math.vector.Vector3d;
Expand All @@ -38,9 +35,7 @@ public final class BlockBreakAction implements WorldAction {
private final SideHint sideHint;
boolean finishedDigging = false;
private boolean didLook = false;
private boolean putOnHotbar = false;
private boolean calculatedBestItemStack = false;
private SFItemStack bestItemStack = null;
private boolean putInHand = false;
private int remainingTicks = -1;

@Override
Expand Down Expand Up @@ -72,105 +67,12 @@ public void tick(BotConnection connection) {
}
}

if (!calculatedBestItemStack) {
SFItemStack itemStack = null;
var bestCost = Integer.MAX_VALUE;
var sawEmpty = false;
for (var slot : playerInventory.storage()) {
var item = slot.item();
if (item == null) {
if (sawEmpty) {
continue;
}

sawEmpty = true;
}

var optionalBlockType = level.getBlockStateAt(blockPosition).blockType();
if (optionalBlockType == BlockType.VOID_AIR) {
log.warn("Block at {} is not in view range!", blockPosition);
return;
}

var cost =
Costs.getRequiredMiningTicks(
sessionDataManager.tagsState(),
sessionDataManager.clientEntity(),
sessionDataManager.inventoryManager(),
clientEntity.onGround(),
item,
optionalBlockType)
.ticks();

if (cost < bestCost || (item == null && cost == bestCost)) {
bestCost = cost;
itemStack = item;
}
if (!putInHand) {
if (ItemPlaceHelper.placeBestToolInHand(sessionDataManager, blockPosition)) {
putInHand = true;
}

bestItemStack = itemStack;
calculatedBestItemStack = true;
}

if (!putOnHotbar && bestItemStack != null) {
var heldSlot = playerInventory.getHeldItem();
if (heldSlot.item() != null) {
var item = heldSlot.item();
if (item.equalsShape(bestItemStack)) {
putOnHotbar = true;
return;
}
}

for (var hotbarSlot : playerInventory.hotbar()) {
if (hotbarSlot.item() == null) {
continue;
}

var item = hotbarSlot.item();
if (!item.equalsShape(bestItemStack)) {
continue;
}

inventoryManager.heldItemSlot(playerInventory.toHotbarIndex(hotbarSlot));
inventoryManager.sendHeldItemChange();
putOnHotbar = true;
return;
}

for (var slot : playerInventory.mainInventory()) {
if (slot.item() == null) {
continue;
}

var item = slot.item();
if (!item.equalsShape(bestItemStack)) {
continue;
}

if (!inventoryManager.tryInventoryControl()) {
return;
}

try {
inventoryManager.leftClickSlot(slot.slot());
TimeUtil.waitTime(50, TimeUnit.MILLISECONDS);
inventoryManager.leftClickSlot(playerInventory.getHeldItem().slot());
TimeUtil.waitTime(50, TimeUnit.MILLISECONDS);

if (inventoryManager.cursorItem() != null) {
inventoryManager.leftClickSlot(slot.slot());
TimeUtil.waitTime(50, TimeUnit.MILLISECONDS);
}
} finally {
inventoryManager.unlockInventoryControl();
}

putOnHotbar = true;
return;
}

throw new IllegalStateException("Failed to find item stack");
return;
}

if (finishedDigging) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@
package com.soulfiremc.server.pathfinding.execution;

import com.github.steveice10.mc.protocol.data.game.entity.player.Hand;
import com.soulfiremc.server.data.BlockItems;
import com.soulfiremc.server.pathfinding.SFVec3i;
import com.soulfiremc.server.protocol.BotConnection;
import com.soulfiremc.server.protocol.bot.BotActionManager;
import com.soulfiremc.server.protocol.bot.container.SFItemStack;
import com.soulfiremc.server.util.BlockTypeHelper;
import com.soulfiremc.server.util.ItemTypeHelper;
import com.soulfiremc.server.util.TimeUtil;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -51,87 +46,11 @@ public void tick(BotConnection connection) {
sessionDataManager.controlState().resetAll();

if (!putOnHotbar) {
var inventoryManager = sessionDataManager.inventoryManager();
var playerInventory = inventoryManager.playerInventory();

SFItemStack leastHardItem = null;
var leastDestroyTime = 0F;
for (var slot : playerInventory.storage()) {
if (slot.item() == null) {
continue;
}

var item = slot.item();
var blockType = BlockItems.getBlockType(item.type());
if (blockType.isEmpty()) {
continue;
}

var destroyTime = blockType.get().destroyTime();
if (leastHardItem == null || destroyTime < leastDestroyTime) {
leastHardItem = item;
leastDestroyTime = destroyTime;
}
}

var heldSlot = playerInventory.getHeldItem();
if (heldSlot.item() != null) {
var item = heldSlot.item();
if (ItemTypeHelper.isSafeFullBlockItem(item.type())) {
putOnHotbar = true;
return;
}
}

for (var hotbarSlot : playerInventory.hotbar()) {
if (hotbarSlot.item() == null) {
continue;
}

var item = hotbarSlot.item();
if (!ItemTypeHelper.isSafeFullBlockItem(item.type())) {
continue;
}

inventoryManager.heldItemSlot(playerInventory.toHotbarIndex(hotbarSlot));
inventoryManager.sendHeldItemChange();
if (ItemPlaceHelper.placeBestBlockInHand(sessionDataManager)) {
putOnHotbar = true;
return;
}

for (var slot : playerInventory.mainInventory()) {
if (slot.item() == null) {
continue;
}

var item = slot.item();
if (!ItemTypeHelper.isSafeFullBlockItem(item.type())) {
continue;
}

if (!inventoryManager.tryInventoryControl()) {
return;
}

try {
inventoryManager.leftClickSlot(slot.slot());
TimeUtil.waitTime(50, TimeUnit.MILLISECONDS);
inventoryManager.leftClickSlot(playerInventory.getHeldItem().slot());
TimeUtil.waitTime(50, TimeUnit.MILLISECONDS);

if (inventoryManager.cursorItem() != null) {
inventoryManager.leftClickSlot(slot.slot());
TimeUtil.waitTime(50, TimeUnit.MILLISECONDS);
}
} finally {
inventoryManager.unlockInventoryControl();
}

putOnHotbar = true;
return;
}

throw new IllegalStateException("Failed to find item stack");
return;
}

if (finishedPlacing) {
Expand Down
Loading

0 comments on commit d53a604

Please sign in to comment.