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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • version used : minecraft forge 1.20.1 Logs : https://bytebin.lucko.me/rtG4EA0vfb hi! so when i try to create a new minecraft world in single player, it just crashes?? does anyone has an idea what could be causing this ?
    • Hey today I was trying to play in 1.21 modpack that I made on fabric (my version of fabric is in 0.16.0) with this list of mods : - Entity Model Features (by traben_0) - Entity Texture Features (by traben_0) - Advanced Chat (by Wesley8081) - Ambient Environment (by Jaredlll08) - Ambient Souds 6 (by CreativeMD) - Animatica (by FoundationGames) - Armor Statues (by Fuzs) - Auto HUD (by Crendgrim) - Bad Packets (by badasintended) - Balm (by BlayTheNinth) - Better Beds (by Motschen) - Better Chunk Loading (by someaddon) - Better Compatibility Checker (by Gaz-) - Better FPS [Render Distance] (by someaddon) - Better Ping Display (by Quintinity) - Better Statistics Screen (by TheCSDev) - Better Armor HUD (by efekos) - Better F3 ( by cominixo) - Blur + (by Motschen) - Bobby (by Johni0702) - Cat Jam (by shmove) - Catalogue (by MrCrayfish) - Chat Heads (by dzwdz) - Cherished Worlds (by TheIllusiveC4) - Chunk Sending (by someaddon) - Chunks Fade In (by kerudion) - Chunky (by pop4959) - Cloth Config (by shedaniel) - Clumps (by Jaredlll08) - Cobweb (CrystalSpider) - Collective (by Serilum) - Configured (by MrCrayfish) - Connectivity (by someaddon) - Continuity (by Pepper_Bell) - Creative Core (by CreativeMD) - Cupboard (by someaddon) - Cut Through (by Fuzs) - Dark Loading Screen (by Neecko5b84) - Debugify (by XanderIsDev) - Double Doors (by Serilum) - Dynamic FPS (by juliand665) - Dynamic Lights (by Tschipcraft) - Easy Anvils (by Fuzs) - Easy Magic (by Fuzs) - Easy Shulker Boxes (by Fuzs) - Eating Animation (by theone-ss) - Enhanced Attack Indicator (by Mineash) - Enhanced Block Entities (by FoundationGames) - Enhanced Visuals (by CreativeMD) - Entity Culling (by tr7zw) - Environmental Water (by AgentMindStorm) - Essential (by SparkUniverse_) - Extra Thicc Packets (by JaselUmena) - Fabric (by modmuss50) - Fabric Language Kotlin (by modmuss50) - Falling Leaves (by RandomMcSomethin) - Fancy Block Particles (by Hantonik) - Fancy Health Bar (by efekos) - Farsight (by someaddon) - Fast IP Ping (by Fallen_Breath) - Ferrite Core (by  malte0811) - Fish On The Line (by Serilum) - Fix GPU Memory Leak  (by someaddon) - Forge Config API Port (by Fuzs) - FPS Reducer (by bre2el) - Freecam (by hashalite) - Fzzy Config (by fzzyhmstrs) - Get It Together (by bl4ckscor3) - Global Packs (by JTK222) - Harvest With Ease (by CrystalSpider) - Ichuntil Util (by iChun) - Immediately Fast (by RaphilMC) - Immersive UI (by SSKirillSS) - Indium (by comp500) - Inventory HUD+ (by dmitrylovin) - Iris Shaders (by coderbot) - Item Physic Full (by CreativeMD) - Jade (by Snownee) - Journey Map (by techbrew) - Just Enough Breeding (by Christofmeg) - Just Enough Items (by mezz) - Just Enough Professions (by Mrbysco) - Language Reload (by Jerozgen) - Leaves Be Gone (by Fuzs) - Let Me Despawn (by frikinjay) - Let Sleeping Dogs Lie (by iChun) - Lithium (by JellySquid) - Loading Backgrounds (by Lupin) - Mineblock's Repeated Utilities (by itsmineblock11) - Menulogue (by MrCrayfish) - Mob Plaques (by Fuzs) - Mod Menu (by Prospector) - Model Gap Fix (by MehVahdJukaar) - Mouse Tweaks (by YaLTeR) - Nether Portal Fix (by BlayTheNinth) - Nicer Skies (by ztereohype) - Noisium (by Steveplays28) - Not Enough Animations (by tr7zw) - Opti GUI (by opekope2) - Packet Fixer (by TonimatasDEV) - Particle Core (by fzzyhmstrs) - Passive Shields (by Serilum) - Pick Up Notifier (by Fuzs) - Ping Wheel (by LukenSkyne) - Puzzle (by Motschen) - Puzzles LIB (by Fuzs) - Reese's Sodium Options (by FlashyReese) - Resource Pack Overrides (by Fuzs) - Server Country Flags (by khajiitos) - Server Performance (by someaddon) - Skin Layer 3D (by tr7zw) - Sodium (by JellySquid) - Sodium Extra (by FlashyReese) - Sodium Shadowy Path Blocks (by Rynnavinx) - Sound Physics Remastered (by henkelmax) - Sounds (by itsmineblock11) - Spark (by lucko) - Stack Refill (by Serilum) - Structure Essentials (by someaddon) - TCD Commons API (by TheCSDev) - Text Placeholder API (by Patbox) - Villager Names (by Serilum) - Visual Overhaul (by Motschen) - Visual Workbench (by Fuzs) - Visuality (by PinkGoosik) - Wakes (by Goby56) - Wavey Capes (by tr7zw) - YDM'S Weapon Master  (by YourDailyModderx) - Yet Another Config LIB (by XanderIsDev) - Zoomify (by XanderIsDev) And When I load it it work, I can create a world and even let him open forever at least I didn't move cause if so the game just crash and tell me this message :   Encountered an unexpected exception net.minecraft.class_148: Ticking entity at net.minecraft.server.MinecraftServer.method_3813(MinecraftServer.java:1025) at net.minecraft.server.MinecraftServer.method_3748(MinecraftServer.java:912) at net.minecraft.class_1132.method_3748(class_1132.java:114) at net.minecraft.server.MinecraftServer.method_29741(MinecraftServer.java:697) at net.minecraft.server.MinecraftServer.method_29739(MinecraftServer.java:281) at java.base/java.lang.Thread.run(Thread.java:1583) Caused by: java.lang.NullPointerException: Cannot invoke "net.minecraft.class_4706.isEmpty()" because "tickets" is null at net.minecraft.class_3204.canNoneExpire(class_3204.java:1034) at net.minecraft.class_3204.handler$dln000$lithium$unregisterExpiringTicket(class_3204.java:1075) at net.minecraft.class_3204.method_17645(class_3204.java:172) at net.minecraft.class_3204.method_17292(class_3204.java:194) at net.minecraft.class_3215.method_17300(class_3215.java:443) at com.betterchunkloading.chunk.PlayerChunkData.checkPrediction(PlayerChunkData.java:406) at com.betterchunkloading.chunk.PlayerChunkData.checkDirection(PlayerChunkData.java:353) at com.betterchunkloading.chunk.PlayerChunkData.trackPlayerMovement(PlayerChunkData.java:177) at com.betterchunkloading.chunk.PlayerChunkData.onChunkChanged(PlayerChunkData.java:130) at com.betterchunkloading.event.EventHandler.onPlayerTick(EventHandler.java:200) at net.minecraft.class_3222.handler$zed000$betterchunkloading$onPlayerTick(class_3222.java:2618) at net.minecraft.class_3222.method_5773(class_3222.java:564) at net.minecraft.class_3218.mixinextras$bridge$method_5773$202(class_3218.java) at net.minecraft.class_3218.wrapOperation$eei000$puzzleslib$tickNonPassenger(class_3218.java:7797) at net.minecraft.class_3218.method_18762(class_3218.java:770) at net.minecraft.class_1937.method_18472(class_1937.java:498) at net.minecraft.class_3218.method_31420(class_3218.java:408) at net.minecraft.class_5574.method_31791(class_5574.java:54) at net.minecraft.class_3218.method_18765(class_3218.java:372) at net.minecraft.server.MinecraftServer.method_3813(MinecraftServer.java:1021) ... 5 more Sorry guys if it obvious but actually I'm an old player of Minecraft but I'm new in modding so yup. But Thanks everyone by advance to help me with this.
    • These forums are for Minecraft Forge, not Fabric. You would probably be better served posting this question wherever fabric support is given, most likely their discord server.
    • Add the new latest.log (logs-folder) with sites like https://mclo.gs/ and paste the link to it here
    • Update: I tried to reproduce the bug and found out that if I place a flare on a block and then dismantle it with the pickarang (Quark), the flare gets no NBT Tag after getting assigned an empty one through the offhand.
  • Topics

×
×
  • Create New...

Important Information

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