Skip to content

Commit

Permalink
Fixed colored block and plank item filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Zangl committed Dec 27, 2015
1 parent d5b1cd5 commit 1699d39
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 56 deletions.
16 changes: 11 additions & 5 deletions Minebot/src/net/famzangl/minecraft/minebot/ai/BlockItemFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 1699d39

Please sign in to comment.