Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Crafting Container
- public class ContainerAssembly extends Container {
- public InventoryCrafting craftMatrix;
- public InventoryCraftResult craftResult = new InventoryCraftResult();
- public final World world;
- public IRecipe lastRecipe;
- final int x;
- final int y;
- final int z;
- final BlockPos pos;
- private final EntityPlayer player;
- public ContainerAssembly(EntityPlayer player, World world, int x, int y, int z) {
- this(player, world, new BlockPos(x, y, z));
- }
- public ContainerAssembly(EntityPlayer player, World world, BlockPos pos) {
- this.player = player;
- this.inventorySlots.clear();
- this.inventoryItemStacks.clear();
- this.world = world;
- this.x = pos.getX();
- this.y = pos.getY();
- this.z = pos.getZ();
- this.pos = pos;
- this.craftMatrix = new InventoryCrafting(this, 5, 3);
- this.addSlotToContainer(new SlotCraftingAssembly(this, player, this.craftMatrix, this.craftResult, 0, 137, 35));
- for(int i = 0; i < 3; ++i) {
- for(int j = 0; j < 5; ++j) {
- this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 5, 18 + j * 18, 17 + i * 18));
- }
- }
- for (int k = 0; k < 3; ++k) {
- for (int i1 = 0; i1 < 9; ++i1) {
- this.addSlotToContainer(new Slot(player.inventory, i1 + k * 9 + 9, 8 + i1 * 18, 84 + k * 18));
- }
- }
- for (int l = 0; l < 9; ++l) {
- this.addSlotToContainer(new Slot(player.inventory, l, 8 + l * 18, 142));
- }
- }
- @Override
- public boolean canInteractWith(EntityPlayer playerIn) {
- if (this.world.getBlockState(this.pos).getBlock() != ExkvaBlocks.assembly) {
- return false;
- } else {
- return playerIn.getDistanceSq(x + 0.5D, y + 0.5D, z + 0.5D) <= 64.0D;
- }
- }
- @Override
- public void onContainerClosed(EntityPlayer player) {
- if (pos != BlockPos.ORIGIN) super.onContainerClosed(player);
- else {
- InventoryPlayer inv = player.inventory;
- if (!inv.getItemStack().isEmpty()) {
- player.dropItem(inv.getItemStack(), false);
- inv.setItemStack(ItemStack.EMPTY);
- }
- if (!this.world.isRemote) this.clearContainer(player, this.world, this.craftMatrix);
- }
- }
- public void onCraftMatrixChanged(IInventory inventoryIn) {
- this.slotChangedCraftingGrid(this.world, this.player, this.craftMatrix, this.craftResult);
- NonNullList<ItemStack> pattern = this.craftMatrix.stackList;
- System.out.println("detected change in craft matrix");
- for(RecipeExkvaAssembly recipe : Exkva.ASSEMBLY_RECIPES) {
- if(recipe.matches(pattern)) {
- System.out.println("attempting to send result");
- this.craftResult.setInventorySlotContents(0, recipe.getOutput());
- }
- }
- }
- }
- //Main recipe class
- public class RecipeExkvaAssembly {
- private final ItemStack output;
- private final Object input;
- private ItemStack[][] pattern = new ItemStack[3][5];
- public RecipeExkvaAssembly(Object input, ItemStack output, ItemStack[][] pattern) {
- this.input = input;
- this.output = output;
- if(pattern.length == 3 && pattern[0].length == 5) {
- for(int R = 0; R < 3; R++) {
- for(int C = 0; C < 5; C++) {
- this.pattern[R][C] = pattern[R][C];
- }
- }
- }
- }
- public Object getInput() {
- return input;
- }
- public ItemStack getOutput() {
- return output.copy();
- }
- public boolean matches(ItemStack[][] pattern) {
- for(int R = 0; R < 3; R++) {
- for(int C = 0; C < 5; C++) {
- if(pattern[R][C] != this.pattern[R][C]) {
- return false;
- }
- }
- }
- return true;
- }
- public boolean matches(NonNullList<ItemStack> pattern) {
- int stackIndex = 0;
- for(int R = 0; R < 3; R++) {
- for(int C = 0; C < 5; C++) {
- if(pattern.get(stackIndex) != this.pattern[R][C]) {
- System.out.println("recipe does not match");
- return false;
- }
- }
- }
- System.out.println("recipe match success");
- return true;
- }
- }
- //Recipe Registry
- public class ExkvaAssemblyRecipes {
- private static final ItemStack X = ItemStack.EMPTY;
- public static void init() {
- steelRod();
- }
- public static void steelRod() {
- ItemStack out = new ItemStack(ExkvaItems.steelRod, 1);
- ItemStack in = new ItemStack(ExkvaItems.steelIngot,1);
- ItemStack[][] pattern = {
- {X, X, in, X, X},
- {X, X, in, X, X},
- {X, X, in, X, X}
- };
- Exkva.registerAssemblyRecipe(in, out, pattern);
- }
- }
- //Setup in Mod class
- public static final List<RecipeExkvaAssembly> ASSEMBLY_RECIPES = new ArrayList<>();
- public static RecipeExkvaAssembly registerAssemblyRecipe(Object input, ItemStack output, ItemStack[][] pattern) {
- RecipeExkvaAssembly recipe = new RecipeExkvaAssembly(input, output, pattern);
- ASSEMBLY_RECIPES.add(recipe);
- return recipe;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement