1
0
mirror of https://github.com/cabaletta/baritone synced 2024-12-17 20:55:09 +00:00

Move ItemInteractionHelper into its own file

This commit is contained in:
Brady 2023-07-05 21:58:04 -07:00
parent bc18f0eabd
commit dbf5058066
No known key found for this signature in database
GPG Key ID: 73A788379A197567
5 changed files with 112 additions and 75 deletions

View File

@ -75,7 +75,8 @@ public final class Settings {
/**
* Allow Baritone to automatically put useful items (such as tools and throwaway blocks) on the hotbar while
* pathing. Requires {@link #allowInventory}.
* pathing. This can reduce delays when retrieving items due settings like {@link #ticksBetweenInventoryMoves} and
* {@link #inventoryMoveOnlyIfStationary}. Requires {@link #allowInventory}.
*/
public final Setting<Boolean> allowHotbarManagement = new Setting<>(false);

View File

@ -17,6 +17,8 @@
package baritone;
import baritone.pathing.movement.CalculationContext;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@ -24,7 +26,8 @@ import java.lang.annotation.Target;
/**
* Annotation that should be used on methods which are performance critical (i.e. called millions of times per second
* by the pathfinder) and should be modified with care.
* by the pathfinder) and should be modified with care. Particularly useful for methods for which this fact is not
* obvious, such as those which don't have a {@link CalculationContext} parameter.
*
* @author Brady
*/

View File

@ -22,22 +22,19 @@ import baritone.api.event.events.TickEvent;
import baritone.api.utils.Helper;
import baritone.api.utils.InventorySlot;
import baritone.api.utils.Pair;
import baritone.utils.ItemInteractionHelper;
import baritone.utils.ToolSet;
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
import it.unimi.dsi.fastutil.objects.Reference2BooleanOpenHashMap;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.ClickType;
import net.minecraft.item.*;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.util.EnumFacing;
import javax.annotation.Nonnull;
import java.lang.reflect.Method;
import java.util.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.OptionalInt;
import java.util.Random;
import java.util.function.IntPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;
@ -327,66 +324,4 @@ public final class InventoryBehavior extends Behavior implements Helper {
public enum SelectionType {
IMMEDIATE, ENQUEUED
}
private static final class ItemInteractionHelper {
private static final Reference2BooleanMap<Class<? extends Item>> CACHE = new Reference2BooleanOpenHashMap<>();
public static boolean couldInteract(final ItemStack stack) {
if (stack.isEmpty()) {
return false;
}
return CACHE.computeIfAbsent(stack.getItem().getClass(), itemClass -> {
try {
final Method onItemUse = itemClass.getMethod(Helper1.name, Helper1.parameters);
final Method onItemRightClick = itemClass.getMethod(Helper2.name, Helper2.parameters);
// If the declaring class isn't Item, then the method is overridden
return onItemUse.getDeclaringClass() != Item.class
|| onItemRightClick.getDeclaringClass() != Item.class;
} catch (NoSuchMethodException ignored) {
// this shouldn't happen
return true;
}
});
}
private static final class Helper1 extends Item {
public static final String name;
public static final Class<?>[] parameters;
static {
final Method method = Helper1.class.getDeclaredMethods()[0];
name = method.getName();
parameters = method.getParameterTypes();
}
@Nonnull
@Override
public EnumActionResult onItemUse(@Nonnull EntityPlayer player, @Nonnull World worldIn,
@Nonnull BlockPos pos, @Nonnull EnumHand hand,
@Nonnull EnumFacing facing, float hitX, float hitY, float hitZ) {
return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
}
}
private static final class Helper2 extends Item {
public static final String name;
public static final Class<?>[] parameters;
static {
final Method method = Helper2.class.getDeclaredMethods()[0];
name = method.getName();
parameters = method.getParameterTypes();
}
@Nonnull
@Override
public ActionResult<ItemStack> onItemRightClick(@Nonnull World worldIn, @Nonnull EntityPlayer playerIn,
@Nonnull EnumHand handIn) {
return super.onItemRightClick(worldIn, playerIn, handIn);
}
}
}
}

View File

@ -0,0 +1,100 @@
/*
* 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.utils;
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
import it.unimi.dsi.fastutil.objects.Reference2BooleanOpenHashMap;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import java.lang.reflect.Method;
/**
* @author Brady
*/
public final class ItemInteractionHelper {
private ItemInteractionHelper() {}
private static final Reference2BooleanMap<Class<? extends Item>> CACHE = new Reference2BooleanOpenHashMap<>();
public static boolean couldInteract(final ItemStack stack) {
if (stack.isEmpty()) {
return false;
}
return CACHE.computeIfAbsent(stack.getItem().getClass(), itemClass -> {
try {
final Method onItemUse = itemClass.getMethod(Helper1.name, Helper1.parameters);
final Method onItemRightClick = itemClass.getMethod(Helper2.name, Helper2.parameters);
// If the declaring class isn't Item, then the method is overridden
return onItemUse.getDeclaringClass() != Item.class
|| onItemRightClick.getDeclaringClass() != Item.class;
} catch (NoSuchMethodException ignored) {
// this shouldn't happen
return true;
}
});
}
private static final class Helper1 extends Item {
public static final String name;
public static final Class<?>[] parameters;
static {
final Method method = Helper1.class.getDeclaredMethods()[0];
name = method.getName();
parameters = method.getParameterTypes();
}
@Nonnull
@Override
public EnumActionResult onItemUse(@Nonnull EntityPlayer player, @Nonnull World worldIn,
@Nonnull BlockPos pos, @Nonnull EnumHand hand,
@Nonnull EnumFacing facing, float hitX, float hitY, float hitZ) {
return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
}
}
private static final class Helper2 extends Item {
public static final String name;
public static final Class<?>[] parameters;
static {
final Method method = Helper2.class.getDeclaredMethods()[0];
name = method.getName();
parameters = method.getParameterTypes();
}
@Nonnull
@Override
public ActionResult<ItemStack> onItemRightClick(@Nonnull World worldIn, @Nonnull EntityPlayer playerIn,
@Nonnull EnumHand handIn) {
return super.onItemRightClick(worldIn, playerIn, handIn);
}
}
}

View File

@ -28,11 +28,9 @@ import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.init.Enchantments;
import net.minecraft.init.Items;
import net.minecraft.init.MobEffects;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;
import java.util.Comparator;
import java.util.function.ToDoubleFunction;