Jump to content

[1.11.2] 4x4 crafting table


Kokkie

Recommended Posts

Hello, how can I create a 4x4x4 crafting table and create recipes for it?

 

How do you plan to display a three dimensional crafting grid?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I now have this, but it has 3x3 and it deletes the item in my first slot

public class ContainerCheeseWorkbench extends Container {
/** The crafting matrix inventory (3x3). */
public InventoryCrafting craftMatrix = new InventoryCrafting(this, 4, 4);
public IInventory craftResult = new InventoryCraftResult();
private final World worldObj;
/** Position of the workbench */
private final BlockPos pos;

public ContainerCheeseWorkbench(InventoryPlayer playerInventory, World worldIn, BlockPos posIn) {
	this.worldObj = worldIn;
	this.pos = posIn;
	this.addSlotToContainer(
			new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 124, 35));

	for (int i = 0; i < 4; ++i) {
		for (int j = 0; j < 4; ++j) {
			this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 3, 30 + j * 18, 17 + i * 18));
		}
	}

	for (int k = 0; k < 3; ++k) {
		for (int i1 = 0; i1 < 9; ++i1) {
			this.addSlotToContainer(new Slot(playerInventory, i1 + k * 9 + 9, 8 + i1 * 18, 84 + k * 18));
		}
	}

	for (int l = 0; l < 9; ++l) {
		this.addSlotToContainer(new Slot(playerInventory, l, 8 + l * 18, 142));
	}

	this.onCraftMatrixChanged(this.craftMatrix);
}

/**
 * Callback for when the crafting matrix is changed.
 */
public void onCraftMatrixChanged(IInventory inventoryIn) {
	this.craftResult.setInventorySlotContents(0,
			CheeseCraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
}

/**
 * Called when the container is closed.
 */
public void onContainerClosed(EntityPlayer playerIn) {
	super.onContainerClosed(playerIn);

	if (!this.worldObj.isRemote) {
		for (int i = 0; i < 16; ++i) {
			ItemStack itemstack = this.craftMatrix.removeStackFromSlot(i);

			if (!itemstack.isEmpty()) {
				playerIn.dropItem(itemstack, false);
			}
		}
	}
}

/**
 * Determines whether supplied player can use this container
 */
public boolean canInteractWith(EntityPlayer playerIn) {
	return this.worldObj.getBlockState(this.pos).getBlock() != CheeseBlocks.CHEESE_CRAFTING_TABLE ? false
			: playerIn.getDistanceSq((double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D,
					(double) this.pos.getZ() + 0.5D) <= 64.0D;
}

/**
 * Take a stack from the specified inventory slot.
 */
public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
	ItemStack itemstack = ItemStack.EMPTY;
	Slot slot = (Slot) this.inventorySlots.get(index);

	if (slot != null && slot.getHasStack()) {
		ItemStack itemstack1 = slot.getStack();
		itemstack = itemstack1.copy();

		if (index == 0) {
			itemstack1.getItem().onCreated(itemstack1, this.worldObj, playerIn);

			if (!this.mergeItemStack(itemstack1, 10, 46, true)) {
				return ItemStack.EMPTY;
			}

			slot.onSlotChange(itemstack1, itemstack);
		} else if (index >= 10 && index < 37) {
			if (!this.mergeItemStack(itemstack1, 37, 46, false)) {
				return ItemStack.EMPTY;
			}
		} else if (index >= 37 && index < 46) {
			if (!this.mergeItemStack(itemstack1, 10, 37, false)) {
				return ItemStack.EMPTY;
			}
		} else if (!this.mergeItemStack(itemstack1, 10, 46, false)) {
			return ItemStack.EMPTY;
		}

		if (itemstack1.isEmpty()) {
			slot.putStack(ItemStack.EMPTY);
		} else {
			slot.onSlotChanged();
		}

		if (itemstack1.getCount() == itemstack.getCount()) {
			return ItemStack.EMPTY;
		}

		ItemStack itemstack2 = slot.onTake(playerIn, itemstack1);

		if (index == 0) {
			playerIn.dropItem(itemstack2, false);
		}
	}

	return itemstack;
}

/**
 * Called to determine if the current slot is valid for the stack merging
 * (double-click) code. The stack passed in is null for the initial slot
 * that was double-clicked.
 */
public boolean canMergeSlot(ItemStack stack, Slot slotIn) {
	return slotIn.inventory != this.craftResult && super.canMergeSlot(stack, slotIn);
}
}

public class CheeseCraftingManager {
/** The static instance of this class */
private static final CheeseCraftingManager INSTANCE = new CheeseCraftingManager();
private final List<IRecipe> recipes = Lists.<IRecipe>newArrayList();

/**
 * Returns the static instance of this class
 */
public static CheeseCraftingManager getInstance() {
	/** The static instance of this class */
	return INSTANCE;
}

private CheeseCraftingManager() {
	this.addRecipe(new ItemStack(CheeseItems.CHEESE_WAND), new Object[]{"  C "," CDC"," SC ","S   ",'C',CheeseItems.CHEESE_INGOT,'S',Items.STICK});
	Collections.sort(this.recipes, new Comparator<IRecipe>() {
		public int compare(IRecipe p_compare_1_, IRecipe p_compare_2_) {
			return p_compare_1_ instanceof ShapelessRecipes && p_compare_2_ instanceof ShapedRecipes ? 1
					: (p_compare_2_ instanceof ShapelessRecipes && p_compare_1_ instanceof ShapedRecipes ? -1
							: (p_compare_2_.getRecipeSize() < p_compare_1_.getRecipeSize() ? -1
									: (p_compare_2_.getRecipeSize() > p_compare_1_.getRecipeSize() ? 1 : 0)));
		}
	});
}

/**
 * Adds a shaped recipe to the games recipe list.
 */
public ShapedRecipes addRecipe(ItemStack stack, Object... recipeComponents) {
	String s = "";
	int i = 0;
	int j = 0;
	int k = 0;

	if (recipeComponents[i] instanceof String[]) {
		String[] astring = (String[]) ((String[]) recipeComponents[i++]);

		for (String s2 : astring) {
			++k;
			j = s2.length();
			s = s + s2;
		}
	} else {
		while (recipeComponents[i] instanceof String) {
			String s1 = (String) recipeComponents[i++];
			++k;
			j = s1.length();
			s = s + s1;
		}
	}

	Map<Character, ItemStack> map;

	for (map = Maps.<Character, ItemStack>newHashMap(); i < recipeComponents.length; i += 2) {
		Character character = (Character) recipeComponents[i];
		ItemStack itemstack = ItemStack.EMPTY;

		if (recipeComponents[i + 1] instanceof Item) {
			itemstack = new ItemStack((Item) recipeComponents[i + 1]);
		} else if (recipeComponents[i + 1] instanceof Block) {
			itemstack = new ItemStack((Block) recipeComponents[i + 1], 1, 32767);
		} else if (recipeComponents[i + 1] instanceof ItemStack) {
			itemstack = (ItemStack) recipeComponents[i + 1];
		}

		map.put(character, itemstack);
	}

	ItemStack[] aitemstack = new ItemStack[j * k];

	for (int l = 0; l < j * k; ++l) {
		char c0 = s.charAt(l);

		if (map.containsKey(Character.valueOf(c0))) {
			aitemstack[l] = ((ItemStack) map.get(Character.valueOf(c0))).copy();
		} else {
			aitemstack[l] = ItemStack.EMPTY;
		}
	}

	ShapedRecipes shapedrecipes = new ShapedRecipes(j, k, aitemstack, stack);
	this.recipes.add(shapedrecipes);
	return shapedrecipes;
}

/**
 * Adds a shapeless crafting recipe to the the game.
 */
public void addShapelessRecipe(ItemStack stack, Object... recipeComponents) {
	List<ItemStack> list = Lists.<ItemStack>newArrayList();

	for (Object object : recipeComponents) {
		if (object instanceof ItemStack) {
			list.add(((ItemStack) object).copy());
		} else if (object instanceof Item) {
			list.add(new ItemStack((Item) object));
		} else {
			if (!(object instanceof Block)) {
				throw new IllegalArgumentException(
						"Invalid shapeless recipe: unknown type " + object.getClass().getName() + "!");
			}

			list.add(new ItemStack((Block) object));
		}
	}

	this.recipes.add(new ShapelessRecipes(stack, list));
}

/**
 * Adds an IRecipe to the list of crafting recipes.
 */
public void addRecipe(IRecipe recipe) {
	this.recipes.add(recipe);
}

/**
 * Retrieves an ItemStack that has multiple recipes for it.
 */
public ItemStack findMatchingRecipe(InventoryCrafting craftMatrix, World worldIn) {
	for (IRecipe irecipe : this.recipes) {
		if (irecipe.matches(craftMatrix, worldIn)) {
			return irecipe.getCraftingResult(craftMatrix);
		}
	}

	return ItemStack.EMPTY;
}

public NonNullList<ItemStack> getRemainingItems(InventoryCrafting craftMatrix, World worldIn) {
	for (IRecipe irecipe : this.recipes) {
		if (irecipe.matches(craftMatrix, worldIn)) {
			return irecipe.getRemainingItems(craftMatrix);
		}
	}

	NonNullList<ItemStack> nonnulllist = NonNullList.<ItemStack>withSize(craftMatrix.getSizeInventory(),
			ItemStack.EMPTY);

	for (int i = 0; i < nonnulllist.size(); ++i) {
		nonnulllist.set(i, craftMatrix.getStackInSlot(i));
	}

	return nonnulllist;
}

public List<IRecipe> getRecipeList() {
	return this.recipes;
}
}

public class CheeseWorkbench extends Block {
public CheeseWorkbench() {
	super(Material.WOOD);
	this.setCreativeTab(CreativeTabs.DECORATIONS);
}

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
		EnumHand hand, EnumFacing heldItem, float side, float hitX, float hitY) {
	if (worldIn.isRemote) {
		return false;
	} else {
		playerIn.displayGui(new CheeseWorkbench.InterfaceCheeseCraftingTable(worldIn, pos));
		return true;
	}
}

public static class InterfaceCheeseCraftingTable implements IInteractionObject {
	private final World world;
	private final BlockPos position;

	public InterfaceCheeseCraftingTable(World worldIn, BlockPos pos) {
		this.world = worldIn;
		this.position = pos;
	}

	/**
	 * Get the name of this object. For players this returns their username
	 */
	public String getName() {
		return "crafting_table";
	}

	/**
	 * Returns true if this thing is named
	 */
	public boolean hasCustomName() {
		return false;
	}

	/**
	 * Get the formatted ChatComponent that will be used for the sender's
	 * username in chat
	 */
	public ITextComponent getDisplayName() {
		return new TextComponentTranslation(CheeseBlocks.CHEESE_CRAFTING_TABLE.getUnlocalizedName() + ".name", new Object[0]);
	}

	public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
		return new ContainerCheeseWorkbench(playerInventory, this.world, this.position);
	}

	public String getGuiID() {
		return "minecraft:crafting_table";
	}
}
}

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

I don't understand what you mean when you say it deletes the item in your first slot. But I did notice this line:

 

this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 3, 30 + j * 18, 17 + i * 18));

 

I believe you need to change the 3 to a 4 so your crafting grid slots will have correct IDs. Maybe that will help?

Link to comment
Share on other sites

Doesn't work, but I get an exception:

[16:52:56] [Client thread/FATAL]: Error executing task
java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException: Index: 46, Size: 46
at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_111]
at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_111]
at net.minecraft.util.Util.runTask(Util.java:27) [util.class:?]
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1109) [Minecraft.class:?]
at net.minecraft.client.Minecraft.run(Minecraft.java:407) [Minecraft.class:?]
at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]
at GradleStart.main(GradleStart.java:26) [start/:?]
Caused by: java.lang.IndexOutOfBoundsException: Index: 46, Size: 46
at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_111]
at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_111]
at net.minecraft.inventory.Container.getSlot(Container.java:128) ~[Container.class:?]
at net.minecraft.inventory.Container.setAll(Container.java:547) ~[Container.class:?]
at net.minecraft.client.network.NetHandlerPlayClient.handleWindowItems(NetHandlerPlayClient.java:1305) ~[NetHandlerPlayClient.class:?]
at net.minecraft.network.play.server.SPacketWindowItems.processPacket(SPacketWindowItems.java:72) ~[sPacketWindowItems.class:?]
at net.minecraft.network.play.server.SPacketWindowItems.processPacket(SPacketWindowItems.java:13) ~[sPacketWindowItems.class:?]
at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:15) ~[PacketThreadUtil$1.class:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_111]
at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_111]
at net.minecraft.util.Util.runTask(Util.java:26) ~[util.class:?]
... 15 more

I think I'll first make a chest.. Looks a little bit easier

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.