Jump to content

[1.6.4] Custom GUI with dynamic mapping *help*


hugo_the_dwarf

Recommended Posts

The title makes it sound a bit confusing, I'm trying to make a special block for my Mod that when acquired and placed will allow the player to "build" a base.

I'm still learning and have a slim grasp on TileEntities, Containers, and NBT so I'm more or less mucking around hoping things work.

 

Anyways What I want is that when you right-click the block you can set "locations" that will be 'filled' when run (and enough resources)

 

right now I have the TileEntity, GUI, Container and the GUI opening. But after you set the locations for blocks to be placed you can't see it ever again (also I'm having trouble figuring out how to save it via NBT)

 

So what works:

  • Gui opens
  • Gui buttons work
  • Gui text displays information
  • Locations can be set with Gui
  • Locations can be cleared (all of them) with Gui
  • TileEntity can place blocks using Locations

 

What I want to work (or get working/do)

  • Gui shows list/map of locations that can be clicked for removal/editing
  • Block information can be stored with a location, and picked by player (use wood planks for 123,45,-200. But use cobble at 122,44,-200)
  • Needs to have blocks in storage to use them and decrease the stacks (I can probably figure this one out, if I can get it to use it's inventory)
  • Saves list and loads it so persists through client/server close, reopen
  • Turns into an Entity with same Gui so it can be attacked(bit out my league but if know how or tutorials would be appreciated)

 

But the main thing is I want a map that is basically a cut out matrix based on the Y value. That can be clicked to make changes to the locations.

 

Code:

public class GuiBaseBuilder extends GuiContainer
{
public static final ResourceLocation texture = 
		new ResourceLocation(Main.MODID.toLowerCase(), "textures/gui/blankGui.png");

private TileEntityBaseBuilder bb;
private int cw = 15; //control Width
private int ch = 15; //control Height
private int x = 0;
private int y = 0;
private int z = 0;

public GuiBaseBuilder(Container par1Container) 
{
	super(par1Container);
}

public GuiBaseBuilder(InventoryPlayer invPlayer, TileEntityBaseBuilder entity) 
{
	super(new ContainerBaseBuilder(invPlayer, entity));
	xSize = 176;
	ySize = 165;
	bb = entity;
}	

@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) 
{
	int posX = (this.width) / 2;//middle of the GUI image
	int posY = (this.height) / 2;//middle of the GUI image
	int xBuffer = 29;//just my own margin for drawing buttons

	GL11.glColor4f(1F, 1F, 1F, 1F);
	Minecraft.getMinecraft().renderEngine.bindTexture(texture);
	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

	this.buttonList.clear();
	//Start with main control buttons		
	this.buttonList.add(new GuiButton(0, posX + xBuffer - 60, posY - 80, 60, 20, "Start/Stop")); //right now does nothing, as it was hit and miss
	this.buttonList.add(new GuiButton(1, posX + xBuffer, posY - 80, 25, 20, "Add")); //Adds the location based on x,y,z
	this.buttonList.add(new GuiButton(8, posX + xBuffer + 25, posY - 80, 25, 20, "Clear")); //Clears all the locations

	//Location controls
	this.buttonList.add(new GuiButton(2, posX + xBuffer, posY - 50, cw, ch, "<"));//X left
	this.buttonList.add(new GuiButton(3, posX + xBuffer + cw, posY - 50, cw, ch, ">"));//X right

	this.buttonList.add(new GuiButton(6, posX + xBuffer, posY - 35, cw, ch, "Y+"));//Y up
	this.buttonList.add(new GuiButton(7, posX + xBuffer + cw, posY - 35, cw, ch, "Y-"));//Y down

	this.buttonList.add(new GuiButton(4, posX + xBuffer, posY - 20, cw, ch, "^"));//Z up confusing should be 'forward'
	this.buttonList.add(new GuiButton(5, posX + xBuffer + cw, posY - 20, cw, ch, "v"));//Z down confusing should be 'back'

	//Visual information on location
	this.drawString(fontRenderer, "X: "+(x+bb.xCoord)+" offSet: "+x, posX + xBuffer - 90, posY - 38, 0xFFFFFF);
	this.drawString(fontRenderer, "Y: "+(y+bb.yCoord)+" offSet: "+y, posX + xBuffer - 90, posY - 28, 0xFFFFFF);
	this.drawString(fontRenderer, "Z: "+(z+bb.zCoord)+" offSet: "+z, posX + xBuffer - 90, posY - 18, 0xFFFFFF);
}

...snip...

}

Custom methods and update are at the bottom

public class TileEntityBaseBuilder extends TileEntity implements IInventory
{

private ItemStack[] inventory = new ItemStack[9];
private ArrayList<Vector3f> locations = new ArrayList<Vector3f>();
private int CD_TIME = 35;
private int cd = CD_TIME;
private boolean onOff = false;

@Override
public void closeChest(){}

@Override
public void writeToNBT(NBTTagCompound par1nbtTagCompound) 
{
	super.writeToNBT(par1nbtTagCompound);

	NBTTagList list = new NBTTagList();

	for(int i = 0; i < getSizeInventory(); i++) 
	{
		  ItemStack itemstack = getStackInSlot(i);
		  if(itemstack != null) 
		  {
			  NBTTagCompound item = new NBTTagCompound();
			  
			  item.setByte("SlotBaseBuild", (byte) i);
			  itemstack.writeToNBT(item);
			  
			  list.appendTag(item);
		  }
		}
	par1nbtTagCompound.setTag("ItemsBaseBuild", list);
}

@Override
public void readFromNBT(NBTTagCompound par1nbtTagCompound) 
{
	super.readFromNBT(par1nbtTagCompound);

	NBTTagList list = par1nbtTagCompound.getTagList("ItemsBaseBuild");

	  for(int i = 0; i < list.tagCount(); i++) {
	        NBTTagCompound item = (NBTTagCompound) list.tagAt(i);
	        int slot = item.getByte("SlotBaseBuild");

	        if(slot >= 0 && slot < getSizeInventory()) {
	          setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
	        }
	  }
}

@Override
public ItemStack decrStackSize(int slot, int count) 
{
	ItemStack itemstack = getStackInSlot(slot);

	if(itemstack != null) 
	{
		if(itemstack.stackSize <= count) 
		{
			setInventorySlotContents(slot, null);
		} 
		else 
		{
			itemstack = itemstack.splitStack(count);
			onInventoryChanged();
		}
	}
	return itemstack;
}

@Override
public String getInvName() 
{
	return "BaseBuilder";
}

@Override
public int getInventoryStackLimit() 
{
	return 64;
}

@Override
public int getSizeInventory() 
{
	return inventory.length;
}

@Override
public ItemStack getStackInSlot(int i) 
{
	return inventory[i];
}

@Override
public ItemStack getStackInSlotOnClosing(int slot) 
{
	ItemStack itemstack = getStackInSlot(slot);
	setInventorySlotContents(slot, null);
	return itemstack;
}

@Override
public boolean isInvNameLocalized() 
{
	return true;
}

@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) 
{
	return true;
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) 
{
	return entityplayer.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <= 64;
}

@Override
public void openChest(){}

@Override
public void setInventorySlotContents(int i, ItemStack itemstack) 
{
	inventory[i] = itemstack;

	if(itemstack != null && itemstack.stackSize > getInventoryStackLimit()) 
	{
		itemstack.stackSize = getInventoryStackLimit();
	}
	onInventoryChanged();
}

@Override
public boolean canUpdate() 
{
	return true;
}

@Override
public void updateEntity() 
{
	if (cd == 0 /*&& onOff*/)
	{
		cd = CD_TIME;
		for(Vector3f v : locations)			
		{			
			getWorldObj().setBlock((int)v.x, (int)v.y, (int)v.z, Block.blockClay.blockID);				
		}
		//switchOnOff();
	}
	else cd--;
}

public void switchOnOff()
{
	onOff = !onOff;
	if(onOff)System.out.println("on");
	else System.out.println("off");
	System.out.println(onOff);
}

public void clearList()
{
	locations.clear();
}

public void addLocation(float i, float j, float k)
{
	if (i + xCoord == xCoord && j + yCoord == yCoord && k + zCoord == zCoord)return;
	Vector3f location = new Vector3f(i + xCoord, j + yCoord, k + zCoord);
	if (locations.size() > 0)
	{
		for (int l = 0;l < locations.size();l++)
		{
			if (locations.get(l).equals(location))
			{
				System.out.println("Same Location Found, replacing");
				locations.set(l, location);
				System.out.println("replaced: "+ location);
				return;
			}				
		}			
	}
	locations.add(location);
	System.out.println("Location added");
	System.out.println(location);
}

Link to comment
Share on other sites

OK so in your writeToNBT() and readFromNBT() you aren't reading or writing the locations so that will not be saved if they are unloaded.

 

Secondly GUIs are client side only so you need a packet send a packet to the client saying what the locations are so you can show then on the GUI.

 

Also, I can't see the rest of your GUI class, but from what I can see you are doing the TileEntity stuff client side. If you could add the rest of your GuiBaseBuilder it would help.

Link to comment
Share on other sites

Ok, so I guess I can try to muck around with NBTTagCompunds and see if I can mash a Vector3f in them, might have to make my own Class thinking on it if I want to store a Block ID:Meta with a location aswell (so if I make a new class for that what would I have to Extend/Implement?)

 

Oh? Packets are something that are still a mystery to me I had *assumed* since the GUI class gets passed and instance of the TileEntity that it was being altered in realtime, I know my tests allow me to use commands and make changes in the TileEntity from the GUI so somehow my wrong way is partially working (hit and miss)

 

And here is the entire GUI class, the stuff I snipped out before was two methods, I don't even know if I need the Mouse Clicked method, I was looking at someones GUI tutorial trying to find how to add buttons and use them.

 

 

All of the GuiClass excluding imports*

public class GuiBaseBuilder extends GuiContainer
{
public static final ResourceLocation texture = 
		new ResourceLocation(Main.MODID.toLowerCase(), "textures/gui/blankGui.png");

private TileEntityBaseBuilder bb;
private int cw = 15; //control Width
private int ch = 15; //control Height
private int x = 0;
private int y = 0;
private int z = 0;

public GuiBaseBuilder(Container par1Container) 
{
	super(par1Container);
}

public GuiBaseBuilder(InventoryPlayer invPlayer, TileEntityBaseBuilder entity) 
{
	super(new ContainerBaseBuilder(invPlayer, entity));
	xSize = 176;
	ySize = 165;
	bb = entity;
}	

@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) 
{
	int posX = (this.width) / 2;//middle of the GUI image
	int posY = (this.height) / 2;//middle of the GUI image
	int xBuffer = 29;//just my own margin for drawing buttons

	GL11.glColor4f(1F, 1F, 1F, 1F);
	Minecraft.getMinecraft().renderEngine.bindTexture(texture);
	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

	this.buttonList.clear();
	//Start with main control buttons		
	this.buttonList.add(new GuiButton(0, posX + xBuffer - 60, posY - 80, 60, 20, "Start/Stop")); //right now does nothing, as it was hit and miss
	this.buttonList.add(new GuiButton(1, posX + xBuffer, posY - 80, 25, 20, "Add")); //Adds the location based on x,y,z
	this.buttonList.add(new GuiButton(8, posX + xBuffer + 25, posY - 80, 25, 20, "Clear")); //Clears all the locations

	//Location controls
	this.buttonList.add(new GuiButton(2, posX + xBuffer, posY - 50, cw, ch, "<"));//X left
	this.buttonList.add(new GuiButton(3, posX + xBuffer + cw, posY - 50, cw, ch, ">"));//X right

	this.buttonList.add(new GuiButton(6, posX + xBuffer, posY - 35, cw, ch, "Y+"));//Y up
	this.buttonList.add(new GuiButton(7, posX + xBuffer + cw, posY - 35, cw, ch, "Y-"));//Y down

	this.buttonList.add(new GuiButton(4, posX + xBuffer, posY - 20, cw, ch, "^"));//Z up confusing should be 'forward'
	this.buttonList.add(new GuiButton(5, posX + xBuffer + cw, posY - 20, cw, ch, "v"));//Z down confusing should be 'back'

	//Visual information on location
	this.drawString(fontRenderer, "X: "+(x+bb.xCoord)+" offSet: "+x, posX + xBuffer - 90, posY - 38, 0xFFFFFF);
	this.drawString(fontRenderer, "Y: "+(y+bb.yCoord)+" offSet: "+y, posX + xBuffer - 90, posY - 28, 0xFFFFFF);
	this.drawString(fontRenderer, "Z: "+(z+bb.zCoord)+" offSet: "+z, posX + xBuffer - 90, posY - 18, 0xFFFFFF);
}

@Override
protected void mouseClicked(int par1, int par2, int par3) 
{
	super.mouseClicked(par1, par2, par3);
}

@Override
protected void actionPerformed(GuiButton button) 
{		
	switch (button.id)
	{
	case 0:
		bb.switchOnOff();
		break;
	case 1:
		bb.addLocation(x, y, z);
		break;
	case 2:
		x--;
		break;
	case 3:
		x++;
		break;
	case 4:
		z--;
		break;
	case 5:
		z++;
		break;
	case 6:
		y++;
		break;
	case 7:
		y--;
		break;
	case 8:
		bb.clearList();
		break;
	}
}

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.