Scan ItemStacks correctly

This commit is contained in:
Logan Darklock 2019-08-31 20:43:03 -07:00
parent 544ae24b64
commit 4bca6cc1a5
No known key found for this signature in database
GPG Key ID: B8C37CEDE1AC60EA
7 changed files with 120 additions and 67 deletions

View File

@ -0,0 +1,5 @@
package baritone.api.accessor;
public interface IItemStack {
int getBaritoneHash();
}

View File

@ -17,6 +17,7 @@
package baritone.api.utils;
import baritone.api.accessor.IItemStack;
import com.google.common.collect.ImmutableSet;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
@ -26,8 +27,9 @@ import net.minecraft.util.Rotation;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -39,17 +41,35 @@ public final class BlockOptionalMeta {
private final Block block;
private final int meta;
private final boolean noMeta;
private final ImmutableSet<Integer> stateMetas;
private final Set<IBlockState> blockstates;
private final ImmutableSet<Integer> stateHashes;
private final ImmutableSet<Integer> stackHashes;
private static final Pattern pattern = Pattern.compile("^(.+?)(?::(\\d+))?$");
private static ImmutableSet<Integer> getStateMetas(@Nonnull Block block, @Nullable Integer meta) {
List<IBlockState> blockstates = block.getBlockState().getValidStates();
private static Set<IBlockState> getStates(@Nonnull Block block, @Nullable Integer meta) {
return block.getBlockState().getValidStates().stream()
.filter(blockstate -> meta == null || block.getMetaFromState(blockstate.withRotation(Rotation.NONE)) == meta)
.collect(Collectors.toCollection(HashSet::new));
}
private static ImmutableSet<Integer> getStateHashes(Set<IBlockState> blockstates) {
return ImmutableSet.copyOf(
(ArrayList<Integer>) blockstates.stream()
.filter(blockstate -> meta == null || block.getMetaFromState(blockstate.withRotation(Rotation.NONE)) == meta)
blockstates.stream()
.map(IBlockState::hashCode)
.collect(Collectors.toCollection(ArrayList::new))
.toArray(Integer[]::new)
);
}
private static ImmutableSet<Integer> getStackHashes(Set<IBlockState> blockstates) {
//noinspection ConstantConditions
return ImmutableSet.copyOf(
blockstates.stream()
.map(state -> new ItemStack(
state.getBlock().getItemDropped(state, new Random(), 0),
state.getBlock().damageDropped(state)
))
.map(stack -> ((IItemStack) (Object) stack).getBaritoneHash())
.toArray(Integer[]::new)
);
}
@ -57,7 +77,9 @@ public final class BlockOptionalMeta {
this.block = block;
this.noMeta = isNull(meta);
this.meta = noMeta ? 0 : meta;
this.stateMetas = getStateMetas(block, meta);
this.blockstates = getStates(block, meta);
this.stateHashes = getStateHashes(blockstates);
this.stackHashes = getStackHashes(blockstates);
}
public BlockOptionalMeta(@Nonnull Block block) {
@ -82,7 +104,9 @@ public final class BlockOptionalMeta {
block = Block.REGISTRY.getObject(id);
meta = noMeta ? 0 : Integer.parseInt(matchResult.group(2));
stateMetas = getStateMetas(block, noMeta ? null : meta);
blockstates = getStates(block, noMeta ? null : meta);
stateHashes = getStateHashes(blockstates);
stackHashes = getStackHashes(blockstates);
}
public Block getBlock() {
@ -99,7 +123,12 @@ public final class BlockOptionalMeta {
public boolean matches(@Nonnull IBlockState blockstate) {
Block block = blockstate.getBlock();
return block == this.block && stateMetas.contains(blockstate.hashCode());
return block == this.block && stateHashes.contains(blockstate.hashCode());
}
public boolean matches(ItemStack stack) {
//noinspection ConstantConditions
return stackHashes.contains(((IItemStack) (Object) stack).getBaritoneHash());
}
@Override

View File

@ -2,6 +2,7 @@ package baritone.api.utils;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemStack;
import java.util.Arrays;
import java.util.List;
@ -47,6 +48,16 @@ public class BlockOptionalMetaLookup {
return false;
}
public boolean has(ItemStack stack) {
for (BlockOptionalMeta bom : boms) {
if (bom.matches(stack)) {
return true;
}
}
return false;
}
public List<BlockOptionalMeta> blocks() {
return asList(boms);
}

View File

@ -1,53 +0,0 @@
/*
* 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.api.utils.command.defaults;
import baritone.api.Settings;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import java.util.List;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
public class WaypointCommand extends Command {
public WaypointCommand() {
super(asList("name1", "name2"), "Short description");
}
@Override
protected void executed(String label, ArgConsumer args, Settings settings) {
;
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) {
return Stream.empty();
}
@Override
public List<String> getLongDesc() {
return asList(
"",
"",
"Usage:",
"> "
);
}
}

View File

@ -0,0 +1,61 @@
/*
* 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.launch.mixins;
import baritone.api.accessor.IItemStack;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ItemStack.class)
public abstract class MixinItemStack implements IItemStack {
@Shadow
@Final
private Item item;
@Shadow
private int itemDamage;
@Unique
private int baritoneHash;
private void recalculateHash() {
baritoneHash = item == null ? -1 : item.hashCode() * itemDamage;
}
@Inject(method = "<init>*", at = @At("RETURN"))
private void onInit(CallbackInfo ci) {
recalculateHash();
}
@Inject(method = "setItemDamage", at = @At("TAIL"))
private void onItemDamageSet(CallbackInfo ci) {
recalculateHash();
}
@Override
public int getBaritoneHash() {
return baritoneHash;
}
}

View File

@ -22,6 +22,7 @@
"MixinEntityRenderer",
"MixinGuiChat",
"MixinGuiScreen",
"MixinItemStack",
"MixinMinecraft",
"MixinNetHandlerPlayClient",
"MixinNetworkManager",

View File

@ -91,7 +91,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
if (desiredQuantity > 0) {
int curr = ctx.player().inventory.mainInventory.stream()
.filter(stack -> filter.has(BlockOptionalMeta.blockStateFromStack(stack)))
.filter(stack -> filter.has(stack))
.mapToInt(ItemStack::getCount).sum();
System.out.println("Currently have " + curr + " valid items");
if (curr >= desiredQuantity) {
@ -304,8 +304,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
for (Entity entity : world.loadedEntityList) {
if (entity instanceof EntityItem) {
EntityItem ei = (EntityItem) entity;
ItemStack stack = ei.getItem();
if (filter.has(BlockOptionalMeta.blockStateFromStack(stack))) {
if (filter.has(ei.getItem())) {
ret.add(new BlockPos(entity));
}
}