Jump to content

[1.7.10] New Anvil damages hammer


EmperorZelos

Recommended Posts

Hello, I am trying to make an anvil that utilizes a hammer to craft items, I have managed so far making it only appear if the hammer is put into the correct slot but I am currently struggling with finding a way to damage the hammer when an item is being crafted.

 

I have looked into this and sure enough it reacts when I make something

public void onCrafting(PlayerEvent.ItemCraftedEvent e) {

	System.out.println(e.crafting.getDisplayName());
}

which is all fine but I cannot think of a way to go from there ensuring it is only the anvil crafting it reacts to, I cannot find something I can use to have it say "yes it is an anvil I am crafting from", and the tileentity x,y,z, if I could get that the rest oughta work, is there some way from here to get it so? Or is there a better way to approch this?

Link to comment
Share on other sites

The issue is mostly I have no idea what function to use such that when someone grabs the item they craft that it damages the hammer that is placed in the item slot for the hammer (that I have fixed). Just want a guidance on what function would be suitible or if there is a way to call forth which tileentity from there that is being used.

Link to comment
Share on other sites

Okey, I am not understanding you perfeclty...or at all for that manner. Sorry if I am a bit ignorant here

 

package aerosteam.container;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import aerosteam.crafting.AnvilSmithManager;
import aerosteam.items.ItemsAS;
import aerosteam.tileentity.TileEntityAnvil;
import aerosteam.tileentity.TileEntitySmelter;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.inventory.SlotFurnace;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.world.World;

public class ContainerAnvil extends Container {
private TileEntityAnvil anvil;
private World worldObj;
private int posX;
private int posY;
private int posZ;
public InventoryCrafting craftMatrix;
public IInventory craftResult;

public ContainerAnvil(InventoryPlayer inventory,TileEntityAnvil tileentity, World world, int x, int y, int z){
	this.anvil=tileentity;
	craftMatrix = new InventoryCrafting(this, 2,3);
	craftResult = new InventoryCraftResult();
	worldObj=world;
	this.posX=x;
	this.posY=y;
	this.posZ=z;

	//Hammer slot
	this.addSlotToContainer(new Slot(tileentity,0,116,13));

	for (int i = 0; i<2;i++){ 
		for (int j = 0; j<3;j++){
			this.addSlotToContainer(new Slot(craftMatrix,(i*3+j),21+18*j,33+18*i));				
		}
	}		
	this.addSlotToContainer(new SlotCrafting(inventory.player, craftMatrix,craftResult, 1,136,42));

	for(int i=0; i<3;i++){
		for(int j=0; j<9;j++){
			this.addSlotToContainer(new Slot(inventory,j+i*9+9,7+18*j+1,83+18*i+1	));
		}
	}
	for(int i=0;i<9;i++){
		this.addSlotToContainer(new Slot(inventory,i,8+18*i,142));
	}

	onCraftMatrixChanged(craftMatrix);
}
public void onCraftMatrixChanged(IInventory inventory){
	//System.out.println("Craft Change");
        Slot slot = (Slot)this.inventorySlots.get(0);
        if (slot != null && slot.getHasStack()){
            Item item = slot.getStack().getItem();
            if (slot.getStack().getItem() != null){
            	if (slot.getStack().getItem() == ItemsAS.hammerBronze){
            		//System.out.println("has hammer");
            		craftResult.setInventorySlotContents(7, AnvilSmithManager.getInstance().findMatchingRecipe(craftMatrix,worldObj));
            	}
            }        	
        }
	//System.out.println("Done matching");
}


@Override
public boolean canInteractWith(EntityPlayer player) {
	return player.getDistanceSq((double)posX +0.5D, (double)posY+0.5D, (double)posZ+0.5D)<64.0D;
}
public void addCraftingToCrafter (ICrafting icraft){
	super.addCraftingToCrafters(icraft);
	//icraft.sendProgressBarUpdate(this, 0, this.anvil.cookTime);
}
public void detectAndSendChanges (){
	super.detectAndSendChanges();
	for (int i=0; i<this.crafters.size();i++){
		ICrafting icraft = (ICrafting) this.crafters.get(i);

		//if(this.lastCookTime != this.anvil.cookTime){
			//icraft.sendProgressBarUpdate(this, 0, this.anvil.burnTime);
		//}
	}

	//this.lastCookTime = this.smelter.cookTime;
}
@SideOnly(Side.CLIENT)
public void updateProgressBar (int slot, int newValue)	{
	//if (slot==0) this.anvil.burnTime=newValue;
}

  public ItemStack transferStackInSlot(EntityPlayer player, int i)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(i);

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

            if (i == 3)
            {
                if (!this.mergeItemStack(itemstack1, 4, 39, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (i != 1 && i != 0 && i != 2)
            {
                if (FurnaceRecipes.smelting().getSmeltingResult(itemstack1) != null)
                {
                    if (!this.mergeItemStack(itemstack1, 1, 2, false))
                    {
                        return null;
                    }
                }
                else if (TileEntityFurnace.isItemFuel(itemstack1))
                {
                    if (!this.mergeItemStack(itemstack1, 0, 3, false))
                    {
                        return null;
                    }
                }
                else if (i >= 3 && i < 30)
                {
                    if (!this.mergeItemStack(itemstack1, 30, 39, false))
                    {
                        return null;
                    }
                }
                else if (i >= 30 && i < 39 && !this.mergeItemStack(itemstack1, 4, 30, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 4, 39, false))
            {
                return null;
            }

            if (itemstack1.stackSize == 0)
            {
                slot.putStack((ItemStack)null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.stackSize == itemstack.stackSize)
            {
                return null;
            }

            slot.onPickupFromSlot(player, itemstack1);
        }

        return itemstack;
    }
  
    public void onContainerClosed(EntityPlayer p_75134_1_)
    {
        super.onContainerClosed(p_75134_1_);

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

                if (itemstack != null)
                {
                    p_75134_1_.dropPlayerItemWithRandomChoice(itemstack, false);
                }
            }
        }
    }

}

 

This is the code for the conatiner, let's try getting this in a manner so I can understand, which parts of these needs to be repalced by what exacly?

Link to comment
Share on other sites

So that diesieben07 doesn't lose patience... here is some more info

 

To make a crafting table (or anvil, inventory, chest, etc.) you will have a custom Container class associated with your custom Block.  In that container, you should be using Slots.  So you have Slot instances associated with your container.

 

If you want custom behavior in the slot, like in this case updating damage on item removed from the slot, then you'll need a custom Slot class.

 

So make a custom Slot that extends vanilla Slot and change the method mentioned for pickup item.  Then in your container instance use the custom slots.

 

So custom block should use custom container that uses custom slots. 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

public class AnvilSlots extends SlotCrafting{


public AnvilSlots(EntityPlayer p_i1823_1_, IInventory p_i1823_2_,
		IInventory p_i1823_3_, int p_i1823_4_, int p_i1823_5_,
		int p_i1823_6_) {
	super(p_i1823_1_, p_i1823_2_, p_i1823_3_, p_i1823_4_, p_i1823_5_, p_i1823_6_);
	// TODO Auto-generated constructor stub
}

@Override
public void onPickupFromSlot(EntityPlayer player, ItemStack p_82870_2_)
    {
	System.out.println("slot changed");
        this.onSlotChanged();
    }
}

I tried this and I get it to work to the extent it reacts whenever something happens to the slot in question, the crafting slot one where the result appears, works marveously. But....I am uncertain how to get it to act on the other slots.

 

public ContainerAnvil(InventoryPlayer inventory,TileEntityAnvil tileentity, World world, int x, int y, int z){
	this.anvil=tileentity;
	craftMatrix = new InventoryCrafting(this, 2,3);
	craftResult = new InventoryCraftResult();
	worldObj=world;
	this.posX=x;
	this.posY=y;
	this.posZ=z;

	//Hammer slot
	this.addSlotToContainer(new Slot(tileentity,0,116,13));

	for (int i = 0; i<2;i++){ 
		for (int j = 0; j<3;j++){
			this.addSlotToContainer(new Slot(craftMatrix,(i*3+j),21+18*j,33+18*i));				
		}
	}		
	this.addSlotToContainer(new AnvilSlots(inventory.player, craftMatrix,craftResult, 1,136,42));

	for(int i=0; i<3;i++){
		for(int j=0; j<9;j++){
			this.addSlotToContainer(new Slot(inventory,j+i*9+9,7+18*j+1,83+18*i+1	));
		}
	}
	for(int i=0;i<9;i++){
		this.addSlotToContainer(new Slot(inventory,i,8+18*i,142));
	}

	onCraftMatrixChanged(craftMatrix);
}
public void onCraftMatrixChanged(IInventory inventory){
	//System.out.println("Craft Change");
        Slot slot = (Slot)this.inventorySlots.get(0);
        if (slot != null && slot.getHasStack()){
            Item item = slot.getStack().getItem();
            if (slot.getStack().getItem() != null){
            	if (slot.getStack().getItem() == ItemsAS.hammerBronze){
            		//System.out.println("has hammer");
            		craftResult.setInventorySlotContents(7, AnvilSmithManager.getInstance().findMatchingRecipe(craftMatrix,worldObj));
            	}
            }        	
        }
	//System.out.println("Done matching");
}

The part I have changed is the one where the result is shown, perhaps I need to change all of them? Either how this is a great leap forward for me, what I want it to affect when the slot changes is slot 0 that is first generated, how would I access it from the slothchanged function?

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.