Jump to content

[1.7.2] Open GUI on mob interact


Casual Dutchman

Recommended Posts

I made a mob and a GUI.

When I right-click the mob, it will open the GUI, just as I wanted.

There is a button on the gui, when I click the event is called, just as I wanted.

But when I quit the game and open the world again, The event is cancelled.

 

lets say a block would appear above its head, when the button is clicked.

It will happen and it will stay there until the session is ended.

When I quit and open the world again, the block is gone.

 

What is the solution?

 

Here is the interact code I use:

public boolean interact(EntityPlayer par1EntityPlayer)
{
	if(par1EntityPlayer.isSneaking() && par1EntityPlayer.getCommandSenderName().equalsIgnoreCase(this.getOwnerName()) && this.isTamed() && par1EntityPlayer.inventory.getCurrentItem() == null)
	{
		Minecraft mc = Minecraft.getMinecraft();
		mc.displayGuiScreen(new Gui(this));
	}
	else{

	}
	return super.interact(par1EntityPlayer);
}

 

This is the GUI code:

private static final ResourceLocation tex = new ResourceLocation(Core.getModID(), "textures/gui/hireable.png");

public final int xSizeOfTexture = 128;
public final int ySizeOfTexture = 166;

EntityHireable hireable;

public GuiHireable(EntityHireable entity)
{
	hireable = entity;
}

@Override
public void drawScreen(int x, int y, float f)
{
	if(hireable.worldObj.isRemote){
		drawDefaultBackground();

		GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
		this.mc.renderEngine.bindTexture(tex);

		int posX = (this.width - xSizeOfTexture) / 2;
		int posY = (this.height - ySizeOfTexture) / 2;

		this.drawTexturedModalRect(posX, posY, 0, 0, this.xSizeOfTexture, this.ySizeOfTexture);

		String s = this.hireable.hasCustomNameTag() ? this.hireable.getCustomNameTag() : I18n.format("entity." + Core.getModID() + "." + hireable.getName() + ".name", new Object[0]);
		this.fontRendererObj.drawString(s + " " + I18n.format("entity.gui.stat", new Object[0]), (this.width / 2) - this.fontRendererObj.getStringWidth(s + " " + I18n.format("entity.gui.stat", new Object[0])) / 2, posY + 7, 4210752);
		this.fontRendererObj.drawString(I18n.format("entity.gui.level", new Object[0]) + " = " + hireable.getLevel(), posX + 6, posY + 7 + 9, 4210752);
		this.fontRendererObj.drawString(I18n.format("entity.gui.health", new Object[0]) + " = " + hireable.getHealth() + " / " + hireable.getMaxHealth(), posX + 6, posY + 7 + (9 * 2), 4210752);

		super.drawScreen(x, y, f);
	}
}

public void initGui()
{
		this.buttonList.clear();

		int posX = (this.width - xSizeOfTexture) / 2;
		int posY = (this.height - ySizeOfTexture) / 2;

		this.buttonList.add(new GuiButton(0, posX+ 6, posY + 10 + (9 * 11), 100, 20, "level up"));
}

public void actionPerformed(GuiButton button)
{
	int i = hireable.getLevel();

		switch(button.id)
		{
		case 0: hireable.setLevel(i + 1);
		break;
		default:
	}
}

private String EquipmentDamage(int i){
	if(hireable.getEquipmentInSlot(i) != null){
		return "" + (hireable.getEquipmentInSlot(i).getMaxDamage() - hireable.getEquipmentInSlot(i).getItemDamage());
	}else{
		return I18n.format("entity.gui.none", new Object[0]);
	}
}

}

Coding, Testing, Smiling, Publishing!

Link to comment
Share on other sites

Okay, I got things running now.

I'm using a methode that will detect a mob in front of you.

When the mob is in front of you, it will open the gui.

 

EntityPlayer.opengui(more stuff);

 

It is working, Im also using a Container, it looks great.

The only downside -> the container does not do its job.

 

When I place a ItemStack in a Slot and I quit and open the world again, all the changes I made are gone.

 

I have Guihandler and I Know that the server part for the container is called. just like the client side, GUI.

 

Why is it still not behaving properly?

 

code:

 

Gui:

private static final ResourceLocation tex = new ResourceLocation(Core.getModID(), "textures/gui/hireable.png");

EntityHireable hireable;

public GuiHireable(EntityPlayer player, EntityHireable entity)
{
	super(new ContainerHireable(player, entity));
	hireable = entity;
}	

protected void drawGuiContainerForegroundLayer(int p_146979_1_, int p_146979_2_)
{
                //TODO
}

@Override
protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_)
    {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(tex);
        int k = (this.width - this.xSize) / 2;
        int l = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
        
    }

 

Container:

extends Container{

EntityHireable Hire;

public ContainerHireable(EntityPlayer player, EntityHireable entity){
	Hire = entity;

	int i;

	this.addSlotToContainer(new slotInv(entity, 0, 80, 62)
	{
		private static final String __OBFID = "CL_00001755";
		public int getSlotStackLimit()
		{
			return 1;
		}

		public boolean isItemValid(ItemStack par1ItemStack)
		{
			if (par1ItemStack == null) return false;
			return par1ItemStack.getItem() instanceof ItemSword;
		}
	});

	for (i = 0; i < 4; ++i)
	{
		final int k = i;
		this.addSlotToContainer(new slotInv(entity, 4 - i, 8, 8 + i * 18)
		{
			private static final String __OBFID = "CL_00001755";
			public int getSlotStackLimit()
			{
				return 1;
			}

			public boolean isItemValid(ItemStack par1ItemStack)
			{
				if (par1ItemStack == null) return false;
				return par1ItemStack.getItem().isValidArmor(par1ItemStack, k, Hire);
			}

			@SideOnly(Side.CLIENT)
			public IIcon getBackgroundIconIndex()
			{
				return ItemArmor.func_94602_b(k);
			}
		});
	}


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

	for (i = 0; i < 9; ++i)
	{
		this.addSlotToContainer(new Slot(player.inventory, i, 8 + i * 18, 142));
	}
}


@Override
public boolean canInteractWith(EntityPlayer par1EntityPlayer)
{
	return this.Hire.isEntityAlive() && this.Hire.getDistanceToEntity(par1EntityPlayer) < 8.0F;
}

//TODO
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
{
	ItemStack itemstack = null;
	Slot slot = (Slot)this.inventorySlots.get(par2);

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

		if (par2 == 0)
		{
			if (!this.mergeItemStack(itemstack1, 0, 39, true))
			{
				return null;
			}

			slot.onSlotChange(itemstack1, itemstack);
		}
		else if (par2 != 1 && par2 != 0)
		{
			if (par2 >= 3 && par2 < 30)
			{
				if (!this.mergeItemStack(itemstack1, 30, 39, false))
				{
					return null;
				}
			}
			else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(itemstack1, 3, 30, false))
			{
				return null;
			}
		}
		else if (!this.mergeItemStack(itemstack1, 3, 39, false))
		{
			return null;
		}

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

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

		slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
	}

	return itemstack;
}
}

 

Interact part of the EntityHireable:

public boolean interact(EntityPlayer par1EntityPlayer)
{
	EntityHireable hire = GuiHandler.getUnitMouseOver(5, 0, par1EntityPlayer);
	if(hire != null && hire.isTamed() && !hire.isAngry() && par1EntityPlayer.getCommandSenderName() == hire.getOwnerName()) {
		System.out.println("wohoo");
		par1EntityPlayer.openGui(Core.instance, 5, par1EntityPlayer.worldObj, 0, 0, 0);
	}
	return super.interact(par1EntityPlayer);
}

 

GuiHandler.getUnitMouseOver is a methode that will retun a EntityHireable in this case.

I can use this in the GuiHandler class to get the entity of which I want to get information

Trust me the MouseOver methode works.

 

@Override
public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) { 
	switch(id){
	case 5:
		EntityHireable hire = getUnitMouseOver(5, 0, player);
		System.out.println("calling Container");
		return new ContainerHireable(player, hire);
	}

	return null;
}

@Override
public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {
	switch(id){
	case 5:
		EntityHireable hire = getUnitMouseOver(5, 0, player);
		System.out.println("calling Gui");
		return new GuiHireable(player, hire);
	}

	return null;

}

Coding, Testing, Smiling, Publishing!

Link to comment
Share on other sites

Let me be clear with the different slot I created,

It basicly is the normal slot, but then modified to change the EntityHireable's equipment.

It works fine, but it does not save it to the entity.

When I quit en open the world again, the entity has not changed equipment.

 

Code here:

package mefa.client.Gui;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import mefa.Entities.EntityHireable;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;

public class slotInv extends Slot
{
    /** The index of the slot in the inventory. */
    private final int slotIndex;
    /** The inventory we want to extract a slot from. */
    public final IInventory inventory;
    public final EntityHireable hireable;
    /** the id of the slot(also the index in the inventory arraylist) */
    public int slotNumber;
    /** display position of the inventory slot on the screen x axis */
    public int xDisplayPosition;
    /** display position of the inventory slot on the screen y axis */
    public int yDisplayPosition;
    private static final String __OBFID = "CL_00001762";

    /** Position within background texture file, normally -1 which causes no background to be drawn. */
    protected IIcon backgroundIcon = null;

    /** Background texture file assigned to this slot, if any. Vanilla "/gui/items.png" is used if this is null. */
    @SideOnly(Side.CLIENT)
    protected ResourceLocation texture;

    public slotInv(IInventory par1IInventory, int par2, int par3, int par4)
    {
    	super(par1IInventory, par2, par3, par4);
        this.inventory = par1IInventory;
        this.hireable = null;
        this.slotIndex = par2;
        this.xDisplayPosition = par3;
        this.yDisplayPosition = par4;
    }
    
    public slotInv(EntityHireable par1IInventory, int par2, int par3, int par4)
    {
    	super(null, par2, par3, par4);
        this.hireable = par1IInventory;
        this.inventory = null;
        this.slotIndex = par2;
        this.xDisplayPosition = par3;
        this.yDisplayPosition = par4;
    }

    public void onSlotChange(ItemStack par1ItemStack, ItemStack par2ItemStack)
    {
        if (par1ItemStack != null && par2ItemStack != null)
        {
            if (par1ItemStack.getItem() == par2ItemStack.getItem())
            {
                int i = par2ItemStack.stackSize - par1ItemStack.stackSize;

                if (i > 0)
                {
                    this.onCrafting(par1ItemStack, i);
                }
            }
        }
    }

    protected void onCrafting(ItemStack par1ItemStack, int par2) {}

    protected void onCrafting(ItemStack par1ItemStack) {}

    public void onPickupFromSlot(EntityPlayer par1EntityPlayer, ItemStack par2ItemStack)
    {
        this.onSlotChanged();
    }

    public boolean isItemValid(ItemStack par1ItemStack)
    {
        return true;
    }

    public ItemStack getStack()
    {
    	if(this.inventory != null){
    		return this.inventory.getStackInSlot(this.slotIndex);
    	}else{
    		return this.hireable.getEquipmentInSlot(this.slotIndex);
    	}
    }

    public boolean getHasStack()
    {
        return this.getStack() != null;
    }

    public void putStack(ItemStack par1ItemStack)
    {
    	if(this.inventory != null){
    		this.inventory.setInventorySlotContents(slotIndex, par1ItemStack);
    	}else{
    		this.hireable.setCurrentItemOrArmor(slotIndex, par1ItemStack);
    	}
        this.onSlotChanged();
    }

    public void onSlotChanged()
    {
        if(this.inventory != null){
        	this.inventory.markDirty();;
    	}
    }

    public int getSlotStackLimit()
    {
    	if(this.inventory != null){
    		return this.inventory.getInventoryStackLimit();
    	}else{
    		return 5;
    	}
    }

    public ItemStack decrStackSize(int par1)
    {
    	int i = this.hireable.getEquipmentInSlot(this.slotIndex).stackSize;
    	ItemStack item = this.hireable.getEquipmentInSlot(this.slotIndex);
    	boolean Size = item.stackSize - par1 > 0;
    	ItemStack item2 = new ItemStack(item.getItem(), item.getItemDamage(), item.stackSize - par1);
    	
    	if(this.inventory != null){
    		return this.inventory.decrStackSize(this.slotIndex, par1);
    	}else{
    		return Size ? item2 : (ItemStack)null;
    	}
    }

    public boolean isSlotInInventory(IInventory par1IInventory, int par2)
    {
        return par1IInventory == this.inventory && par2 == this.slotIndex;
    }

    public boolean canTakeStack(EntityPlayer par1EntityPlayer)
    {
        return true;
    }

    @SideOnly(Side.CLIENT)
    public IIcon getBackgroundIconIndex()
    {
        return backgroundIcon;
    }

    @SideOnly(Side.CLIENT)
    public boolean func_111238_b()
    {
        return true;
    }

    @SideOnly(Side.CLIENT)
    public ResourceLocation getBackgroundIconTexture()
    {
        return (texture == null ? TextureMap.locationItemsTexture : texture);
    }

    public void setBackgroundIcon(IIcon icon)
    {
        backgroundIcon = icon;
    }

    @SideOnly(Side.CLIENT)
    public void setBackgroundIconTexture(ResourceLocation texture)
    {
        this.texture = texture;
    }

    public int getSlotIndex()
    {
        /** The index of the slot in the inventory. */
        return slotIndex;
    }
}[code]

Coding, Testing, Smiling, Publishing!

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.