Jump to content

Recommended Posts

Posted
29 minutes ago, Leomelonseeds said:

 

Huh??

 

Just do what you did befire.

its just an empty string, i am not worried about the centering so much as the output. what i did before does not refresh like it does on static value unfortunantly. but if i use a static value each time i exit and enter the game it starts the value over rather then reading from nbt

  • Replies 58
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted
4 hours ago, clowcadia said:

public static Basic instance;

This line is meaningless because 1. You never initialise or use it, and 2. Your entity is not a singleton class. Every time one of your entity spawns in the world (however that happens), a new instance of the class is created - you can't use static fields or instances for this. It'd be like trying to apply damage to a particular zombie in the world by saying "Give all zombies 1/2 health". Anything that involves your entity needs to relate to the particular instance that is involved - in this case, the particular instance that the player clicked on to open the GUI.

 

4 hours ago, clowcadia said:

if(ID==BASIC){
	Basic basic = new Basic(world);
	return new ContainerBasic(player.inventory,basic);
}

 

Creating a new Basic is not at all what you need to do - you need the specific Basic instance that the player clicked on. You were on the right track before when you were using getEntityByID.

4 hours ago, clowcadia said:

this.mc.fontRendererObj.drawString(basic.getStomach()+"", this.xSize / 2 - this.mc.fontRendererObj.getStringWidth(stomachDisplay) / 2, 20, 4210752);

This is on the right track, too. Once you get your basic field pointing to the correct instance of the entity, this line should work.

 

If your entity data is being reset when the world is saved/loaded, that's not a problem with GUIs and Containers - it's to do with the entity itself (probably something to do with NBT). If that's still a problem, it's probably best to post a new thread about that specific topic to get more help.

Posted
8 hours ago, Jay Avery said:

If your entity data is being reset when the world is saved/loaded, that's not a problem with GUIs and Containers - it's to do with the entity itself (probably something to do with NBT). If that's still a problem, it's probably best to post a new thread about that specific topic to get more help.

The data in NBT seams to reset only when it is on static where the value of stomach variable actually adjust in gui properly. Static shows correct gui change but resets the value always on restart thats the issue. other wise NBT runs correctly but the value does not change with in gui or just shows the initial value of creation of entity 0, weather the getStomach method is return stomach; or return this.stomach;

Posted (edited)
29 minutes ago, clowcadia said:

The data in NBT seams to reset only when it is on static where the value of stomach variable actually adjust in gui properly. Static shows correct gui change but resets the value always on restart thats the issue. other wise NBT runs correctly but the value does not change with in gui or just shows the initial value of creation of entity 0, weather the getStomach method is return stomach; or return this.stomach;

Whatever is happening when you make stomach static, it is not going to be what you want to happen - even if it seems that way at first glance. Post your most up-to-date code (with non-static stomach) and we'll see if we can fix the GUI issue.

Edited by Jay Avery
Posted

Ok thank you.

Basic

package com.clowcadia.test.npc;

import com.clowcadia.test.GuiHandler;
import com.clowcadia.test.TestModHandler;
import com.clowcadia.test.utils.Utils;

import net.minecraft.block.ITileEntityProvider;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import scala.reflect.internal.Trees.This;

public class Basic extends EntityLiving implements  ICapabilityProvider{
	
	public static Basic instance;
	
	private final ItemStackHandler handler;
	private String tagStomach = "Stomach";
	private int stomach;	
	private int stomachCap = 800;
	
	public Basic(World world) 
	{			
		super(world);		
		this.handler = new ItemStackHandler(9); 
		this.stomach = 0;
		//Utils.getLogger().info("basic constructor" +this.stomach);
	}
	public int getStomach(){
		return stomach;
	}
	@Override
	public void readEntityFromNBT(NBTTagCompound compound) {
		this.handler.deserializeNBT(compound.getCompoundTag("ItemStackHandler"));		
		this.stomach = compound.getInteger("Stomach");
		if(compound.hasKey(tagStomach)) Utils.getLogger().info("Reading Stomach");
		super.readEntityFromNBT(compound);
	}
	
	@Override
	public void writeEntityToNBT(NBTTagCompound compound) {
		super.writeEntityToNBT(compound);
		
		compound.setTag("ItemStackHandler", this.handler.serializeNBT());
		compound.setInteger("Stomach", this.stomach);
		if(compound.hasKey(tagStomach)) Utils.getLogger().info("Writing Stomach");
		
	}	
	
	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
		if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return (T) this.handler;		
		return super.getCapability(capability, facing);
	}
	
	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
		if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return true;
		return super.hasCapability(capability, facing);
	}
	
	@Override
	public void onEntityUpdate() {
		ItemStack foodStack = handler.getStackInSlot(0);
		if (this.world != null){
			if(!this.world.isRemote){
				Utils.getLogger().info("Stomach: "+stomach);
				if(foodStack.getUnlocalizedName().equals(Items.APPLE.getUnlocalizedName()) && stomach != stomachCap){
					
					foodStack.splitStack(1);
					stomach += 40;
				}
				
			}				
		}
			
			
		super.onEntityUpdate();
	}
	
	@Override
	public boolean processInteract(EntityPlayer player, EnumHand hand)
    {
		
		if (!this.world.isRemote)
		{			
			int basicID = this.getEntityId(); //for inputing into x/y/z in the opne gui to pass the entety id
			System.out.println("Player has interacted with the mob");	
			player.openGui(TestModHandler.instance, GuiHandler.BASIC, this.world, basicID,0, 0);	
		}
		return true;
    }




}

Gui Handler

package com.clowcadia.test;


import java.security.PublicKey;


import com.clowcadia.test.containers.ContainerBasic;
import com.clowcadia.test.gui.GuiBasic;
import com.clowcadia.test.npc.Basic;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.datafix.fixes.EntityId;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;

public class GuiHandler implements IGuiHandler{
	
	
	public static final int BASIC = 0;	

	@Override
	public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
		if(ID==BASIC){
			Entity basic = world.getEntityByID(x);
			
			return new ContainerBasic(player.inventory,basic);
		}
		return null;
	}

	@Override
	public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
		if(ID==BASIC){
			Entity basic = world.getEntityByID(x);
			return new GuiBasic(player.inventory, basic);
		}
		return null;
	}

}

GuiBasic

package com.clowcadia.test.gui;

import java.util.ArrayList;
import java.util.List;

import com.clowcadia.test.TestModHandler;
import com.clowcadia.test.containers.ContainerBasic;
import com.clowcadia.test.npc.Basic;
import com.clowcadia.tutorial3.Reference;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.Entity;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.items.CapabilityItemHandler;
import scala.reflect.internal.Trees.This;

public class GuiBasic extends GuiContainer{
	
	private IInventory playerInv;
	private Entity entity;
	private String stomachDisplay;

	public GuiBasic(IInventory playerInv, Entity entity) {
		super(new ContainerBasic(playerInv, entity));
		
		
		this.xSize=176;
		this.ySize=166;
		
		this.playerInv = playerInv;
		this.entity = entity;
	}

	@Override
	protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
		GlStateManager.color(1.0F, 1.0F, 1.0F,1.0F);
		this.mc.getTextureManager().bindTexture(new ResourceLocation(TestModHandler.modId, "textures/gui/container/basic.png"));
		this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize,this.ySize);
		
	}
	
	/**
	 * Draws the text that is an overlay, i.e where it says Block Breaker in the gui on the top
	 */
	@Override
	protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
		String s = I18n.format("container.basic"); //Gets the formatted name for the block breaker from the language file
		this.mc.fontRendererObj.drawString(s, this.xSize / 2 - this.mc.fontRendererObj.getStringWidth(s) / 2, 6, 4210752); //Draws the block breaker name in the center on the top of the gui
		this.mc.fontRendererObj.drawString(this.playerInv.getDisplayName().getFormattedText(), 8, 72, 4210752); //The player's inventory name
		this.mc.fontRendererObj.drawString(((Basic) entity).getStomach()+"", this.xSize / 2 - this.mc.fontRendererObj.getStringWidth(stomachDisplay) / 2, 20, 4210752);
		int actualMouseX = mouseX - ((this.width - this.xSize) / 2);
		int actualMouseY = mouseY - ((this.height - this.ySize) / 2);
		
	}
	

}

 

Posted (edited)

That code all looks fine and like it should be working.

 

The problem is likely to do with server/client syncing - you are updating your entity data on the server side (where !world.isRemote), but the GUI is drawn on the client side, where the data isn't changed. I think you'll need to use packets to send the updated stomach value to the client side when it gets changed on the server, so that it can be used in the GUI.

Edited by Jay Avery
Posted

I've never used packets with an Entity. I assume the process is more or less the same as for TIleEntities and suchlike, but you should probably ask someone who has direct experience if you need more help!

Posted

You can use Container#detectAndSendChanges() to send pakcet and Container#updateProgressBar(int, int)(clinet only) to update data.

in detectAndSendChanges(), compare values you want to update and call IContainerListener#sendProgressBarUpdate(Container, int, int) for every listener in Container#listeners.

in updateProgressBar(int, int), update values according to ids.

Look into the ContainerFurnace and GuiFurnace (container updats values in TE and GUI draws from TE's values)

  • Guest locked this topic
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.