From 1699d391f60a2035db0d10ca6e4f84c5c62e8b75 Mon Sep 17 00:00:00 2001 From: Michael Zangl Date: Mon, 28 Dec 2015 00:03:41 +0100 Subject: [PATCH] Fixed colored block and plank item filter. --- .../minecraft/minebot/ai/BlockItemFilter.java | 16 ++++--- .../minebot/ai/ColoredBlockItemFilter.java | 45 ++----------------- .../ai/task/inventory/ItemWithSubtype.java | 26 ++++++++++- .../minebot/build/block/SlabFilter.java | 8 +--- .../minebot/build/block/SlabType.java | 5 +++ 5 files changed, 44 insertions(+), 56 deletions(-) diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/BlockItemFilter.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/BlockItemFilter.java index 38209153..4b5ea94b 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/BlockItemFilter.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/BlockItemFilter.java @@ -16,8 +16,10 @@ *******************************************************************************/ package net.famzangl.minecraft.minebot.ai; +import net.famzangl.minecraft.minebot.ai.command.BlockWithData; import net.famzangl.minecraft.minebot.ai.command.BlockWithDataOrDontcare; import net.famzangl.minecraft.minebot.ai.path.world.BlockSet; +import net.famzangl.minecraft.minebot.ai.task.inventory.ItemWithSubtype; import net.minecraft.block.Block; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; @@ -33,22 +35,26 @@ public class BlockItemFilter implements HumanReadableItemFilter { private final BlockSet matched; public BlockItemFilter(Block... matched) { - this.matched = new BlockSet(matched); + this(new BlockSet(matched)); } public BlockItemFilter(BlockSet matched) { this.matched = matched; } + public BlockItemFilter(BlockWithDataOrDontcare blockWithData) { + this(blockWithData.toBlockSet()); + } + @Override public boolean matches(ItemStack itemStack) { return itemStack != null && itemStack.getItem() != null - && itemStack.getItem() instanceof ItemBlock - && matchesItem(itemStack, (ItemBlock) itemStack.getItem()); + && matchesItem(itemStack); } - protected boolean matchesItem(ItemStack itemStack, ItemBlock item) { - return matched.contains(item.block); + protected boolean matchesItem(ItemStack itemStack) { + BlockWithDataOrDontcare blockType = new ItemWithSubtype(itemStack).getBlockType(); + return blockType != null && matched.contains(blockType); } @Override diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/ColoredBlockItemFilter.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/ColoredBlockItemFilter.java index f19808c7..a13a0ca0 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/ColoredBlockItemFilter.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/ColoredBlockItemFilter.java @@ -16,6 +16,7 @@ *******************************************************************************/ package net.famzangl.minecraft.minebot.ai; +import net.famzangl.minecraft.minebot.ai.command.BlockWithData; import net.famzangl.minecraft.minebot.ai.path.world.BlockSet; import net.minecraft.block.Block; import net.minecraft.init.Blocks; @@ -33,7 +34,6 @@ public class ColoredBlockItemFilter extends BlockItemFilter { public static final BlockSet COLORABLE_BLOCKS = new BlockSet(Blocks.wool, Blocks.stained_hardened_clay, Blocks.stained_glass, Blocks.stained_glass_pane, Blocks.carpet); - private final int colorMeta; // /** // * Right names for sheep wool and most blocks. @@ -68,55 +68,16 @@ public static EnumDyeColor colorFromStringNull(String color) { } public ColoredBlockItemFilter(Block matched, EnumDyeColor color) { - super(matched); - colorMeta = color.getMetadata(); + super(new BlockWithData(matched, color.getMetadata())); if (COLORABLE_BLOCKS.contains(matched)) { throw new IllegalArgumentException(); } } - @Override - protected boolean matchesItem(ItemStack itemStack, ItemBlock item) { - return super.matchesItem(itemStack, item) - && itemStack.getItemDamage() == colorMeta; - } - @Override public String toString() { - return "ColoredBlockItemFilter [colorMeta=" + colorMeta + ", " + return "ColoredBlockItemFilter [" + super.toString() + "]"; } - @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + colorMeta; - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!super.equals(obj)) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final ColoredBlockItemFilter other = (ColoredBlockItemFilter) obj; - if (colorMeta != other.colorMeta) { - return false; - } - return true; - } - - @Override - public String getDescription() { - return EnumDyeColor.values()[colorMeta].getName() + " " - + super.getDescription(); - } - } diff --git a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/inventory/ItemWithSubtype.java b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/inventory/ItemWithSubtype.java index 90cc5ded..15c9963a 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/ai/task/inventory/ItemWithSubtype.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/ai/task/inventory/ItemWithSubtype.java @@ -16,7 +16,11 @@ *******************************************************************************/ package net.famzangl.minecraft.minebot.ai.task.inventory; +import net.famzangl.minecraft.minebot.ai.command.BlockWithData; +import net.famzangl.minecraft.minebot.ai.command.BlockWithDataOrDontcare; +import net.famzangl.minecraft.minebot.ai.command.BlockWithDontcare; import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; /** @@ -40,7 +44,7 @@ public ItemWithSubtype(ItemStack stack) { public ItemWithSubtype(int itemId, int itemDamage) { this.itemId = itemId; this.itemDamage = itemDamage; - this.hasSubtype = Item.getItemById(itemId).getHasSubtypes(); + this.hasSubtype = getItem().getHasSubtypes(); } @Override @@ -83,7 +87,6 @@ public ItemWithSubtype withSubtype(int subtype) { } public ItemWithSubtype withSubtype(String string) { - int subtype; if (string.matches("\\d{1,3}")) { subtype = Integer.parseInt(string); @@ -95,6 +98,25 @@ public ItemWithSubtype withSubtype(String string) { } return withSubtype(subtype); } + + public Item getItem() { + return Item.getItemById(itemId); + } + + public BlockWithDataOrDontcare getBlockType() { + Item item = getItem(); + if (item instanceof ItemBlock) { + ItemBlock itemBlock = (ItemBlock) item; + if (hasSubtype) { + int meta = getItem().getMetadata(itemDamage); + return new BlockWithData(itemBlock.block, meta); + } else { + return new BlockWithDontcare(itemBlock.block); + } + } else { + return null; + } + } /** * Convert a name to an item id. Always sets the subtype to 0. diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/block/SlabFilter.java b/Minebot/src/net/famzangl/minecraft/minebot/build/block/SlabFilter.java index 6f6d61f2..b5d3e8af 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/block/SlabFilter.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/block/SlabFilter.java @@ -30,16 +30,10 @@ public class SlabFilter extends BlockItemFilter { private final SlabType type; public SlabFilter(SlabType type) { - super(type.slabBlock); + super(type.getBlock()); this.type = type; } - @Override - protected boolean matchesItem(ItemStack itemStack, ItemBlock item) { - return super.matchesItem(itemStack, item) - && (itemStack.getItemDamage() & 7) == type.meta; - } - @Override public int hashCode() { final int prime = 31; diff --git a/Minebot/src/net/famzangl/minecraft/minebot/build/block/SlabType.java b/Minebot/src/net/famzangl/minecraft/minebot/build/block/SlabType.java index 14330b58..62493ee2 100644 --- a/Minebot/src/net/famzangl/minecraft/minebot/build/block/SlabType.java +++ b/Minebot/src/net/famzangl/minecraft/minebot/build/block/SlabType.java @@ -16,6 +16,7 @@ *******************************************************************************/ package net.famzangl.minecraft.minebot.build.block; +import net.famzangl.minecraft.minebot.ai.command.BlockWithData; import net.minecraft.block.Block; import net.minecraft.init.Blocks; @@ -49,4 +50,8 @@ private SlabType(Block slabBlock, Block doubleBlock, int meta) { this.doubleBlock = doubleBlock; this.meta = meta; } + + public BlockWithData getBlock() { + return new BlockWithData(slabBlock, meta); + } }