Skip to content

SlotValidator

MCE626 edited this page May 4, 2019 · 4 revisions

ISlotValidator interface and the SlotValidator class is for block containers that have recipes and slots. This will only allow an item to be put into a slot if it's apart of a recipe, or if you specifically want an item in the slot.

isItemValid takes in an itemstack and returns true or false if the item is allowed in the slot.

Examples

For containers and tile entity blocks. This example is taken from LittleUtilities and has been shortened so you can get the gist of using this function. Also, you have to implement the ISlotValidator interface in your container class.

Container Class

public class ContainerTechBlock extends Container implements ISlotValidator {
	public ContainerTechBlock(InventoryPlayer playerInv, IInventory tileInv) {
		this.addSlotToContainer(new SlotValid(this, tileInv, 0, 56, 35)); // validator, inv, slotIndex, x, y
	}

	public boolean isItemValid(ItemStack input){
		return TechBlockRecipes.isRcipe(input);
	}
}

These are what makes it so only an item from the recipe list can be put in the slot. For the container part at least. The TechBlockRecipes class is below.

Recipe Class

This class is done in the same was the vanilla furnace does its recipes. Not everyone does recipes like this, and this block will act like a furnace, putting one item in, "smelting" and getting an item out.

Again shortened code so you can get the gist of the usage here.

public class TechBlockRecipes {
	private final Map<ItemStack, ItemStack> recipeMap = Maps.<ItemStack, ItemStack>newHashMap();

	public ItemStack getRecipeResult(ItemStack stack) {
		for (Entry<ItemStack, ItemStack> entry : recipeMap.entrySet()) {
			if (compareStacks(stack, entry.getKey()))
				return entry.getValue();
		}
		return ItemStack.EMPTY;
	}

	public static boolean isRecipe(ItemStack stack) {
		return instance().getRecipeResult(input) != null;
	}
}

Here there is a Map containing the input stack and the output stack for that input stack for all the recipes registered for this "TechBlock". The vanilla furnace does it this way, however you might not want it to be like this for your block.

There is a boolean method isRecipe that is used in the container to get a boolean to see if the input item is a part of the recipe, and if it is, it can be placed in that input slot in the container, the ValidSlot. isRecipe uses a method in the recipe class called getRecipeResult which takes an input itemstack. This is the stack that goes in the input slot.

This getRecipeResult method takes Map of the recipes, looks through the first entry of a recipe listing and compares it to the itemstack you passed into the method and returns an entry of the output itemstack. If they are the same, that means the item is apart of the recipe and isRecipe gets a result back of that it is not null. (Meaning there is an item being returned from getting the result of the recipe, which means it's a valid recipe and a valid item.)

I know, some advanced logic and code going on here. If you want to know more/see more of the classes, check out the vanilla stuff or LittleUtilities classes.

References