Jump to content

Recommended Posts

Posted (edited)

I am new to coding a minecraft mod, so apologize if I miss some knowledge. 

 

Currently, I am trying to create a mod that takes items from the inventory of the player, puts them into a workbench and takes the crafted result.

So far everything seems to work fine, I take the item put it in the workbench. So for every item, but when I try to take the craft result or click on of the golden nuggets everything gets moved back to the original slot. 

 

Any help for me out there? Thanks in advance!

 

Here is my code:

private boolean turn;
private boolean gold;

protected Minecraft mc = Minecraft.getMinecraft();
public TimeHelper time = new TimeHelper();

public void onUpdate(EventUpdate event) {
		if (time.hasReached(1000L)) {
			if (mc.thePlayer.openContainer != null && mc.thePlayer.openContainer instanceof ContainerWorkbench) {
				ContainerWorkbench workbench = (ContainerWorkbench) mc.thePlayer.openContainer;
				if (!gold)
					gold = hasEnoughGold();
				takeItemFromInv(workbench);
				craft();
			}
			time.reset();
		}
	}

	private void takeItemFromInv(ContainerWorkbench workbench) {
		int nuggetSlot = 404;
		int workbenchSlot = 404;
		if (gold) {
			if (nuggetSlot == 404) {
				nuggetSlot = getNuggetSlot();
			}
			if (workbenchSlot == 404)
				workbenchSlot = getWorkbenchSlot(workbench);

			if (nuggetSlot != 404 && workbenchSlot != 404) {
				if (!turn) {
					mc.playerController.windowClick(0, nuggetSlot, 0, 0, mc.thePlayer);
					turn = true;
				} else {
					mc.playerController.windowClick(workbench.windowId, workbenchSlot, 0, 0, mc.thePlayer);
					turn = false;
				}
			}
		}
	}

	private boolean hasEnoughGold() {
		int goldAmount = 0;
		for (int i = 0; i < mc.thePlayer.inventory.getSizeInventory(); i++) {
			if (mc.thePlayer.inventory.getStackInSlot(i) != null) {
				if (hasNugget(i)) {
					goldAmount++;
				}
			}
		}
		if (goldAmount >= 9)
			return true;
		else
			return false;
	}

	private boolean hasNugget(int i) {
		return mc.thePlayer.inventory.getStackInSlot(i).getDisplayName().equals("Gold Nugget")
				&& mc.thePlayer.inventory.getStackInSlot(i).stackSize == 64;
	}

	private int getNuggetSlot() {
		for (int i = 0; i < mc.thePlayer.inventory.getSizeInventory(); i++) {
			if (i < 9) {
				continue;
			}
			if (mc.thePlayer.inventory.getStackInSlot(i) != null) {
				if (mc.thePlayer.inventory.getStackInSlot(i).getDisplayName().equals("Gold Nugget")
						&& mc.thePlayer.inventory.getStackInSlot(i).stackSize == 64) {
					return i + 1;
				}
			}
		}
		return 404;
	}

	private int getWorkbenchSlot(ContainerWorkbench workbench) {
		for (int i = 0; i < workbench.craftMatrix.getSizeInventory(); i++) {
			if (workbench.craftMatrix.getStackInSlot(i) != null) {
				continue;
			} else {
				return i + 1;
			}
		}
		return 404;
	}

	private void craft() {
		ContainerWorkbench workbench = (ContainerWorkbench) mc.thePlayer.openContainer;
		if (workbench.getSlot(0).getStack() != null) {
			if (workbench.getSlot(0).getStack().getDisplayName().equals("Gold Ingot")) {
				mc.playerController.windowClick(workbench.windowId, 0, 0, 1, mc.thePlayer);
			}
		}
	}

 

It is minecraft version 1.8.8

Edited by Kargo
Forgot that sorry
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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