Hello everybody!
I have a problem with figuring out how to use Gui classes to modify specific variables of a custom entity. I searched for tutorials or code examples (even looked in vanilla) about GUI, but they all seem to be about Inventory, Containers and TileEntitties. What about entities?
Here I have a custom entity that on processInteract should open a Gui to change its state:
/* Package and imports */
public class CameraEntity extends EntityLiving
{
/* Here are the variables I want to modify */
public float speed = 0.4F;
public float accelerationRate = 0.02F;
public float accelerationMax = 1.5f;
public boolean canFly = true;
protected float acceleration = 0.0F;
/* Constructor and other method that modify entity's state */
/**
* Processes player's right clicking on the entity
*
* If the player holds camera configuration item, then GUI with camera
* configuration properties should pop up, otherwise start riding
*/
@Override
public boolean processInteract(EntityPlayer player, EnumHand p_184645_2_, ItemStack stack)
{
if (!worldObj.isRemote)
{
ItemStack item = player.getHeldItemMainhand();
if (item != null && item.getItem() instanceof CameraConfigItem)
{
player.openGui(MyMod.instance, 0, worldObj, (int) posX, (int) posY, (int) posZ);
return true;
}
if (!isBeingRidden())
{
return player.startRiding(this);
}
}
return false;
}
/* Riding logic (moveEntityWithHeading, canBeSteered, getControllingPassenger) */
}
Basically I want to modify first four public variables (I know that they won't be saved). There's my GUI code:
/* Package and imports */
public class GuiCamera extends GuiScreen
{
/* static validator */
protected GuiTextField speed;
protected GuiTextField accelerationRate;
protected GuiTextField accelerationMax;
protected GuiButton canFly;
protected GuiButton done;
public GuiCamera()
{
/* How can I pass entity here? */
}
@Override
public void initGui()
{
int x = width/2 - 150;
speed = new GuiTextField(0, fontRendererObj, x, 50, 300, 20);
// speed.setText(Double.toString(camera.speed));
speed.setValidator(validator);
accelerationRate = new GuiTextField(1, fontRendererObj, x, 90, 300, 20);
// accelerationRate.setText(Double.toString(camera.accelerationRate));
accelerationRate.setValidator(validator);
accelerationMax = new GuiTextField(2, fontRendererObj, x, 130, 300, 20);
// accelerationMax.setText(Double.toString(camera.accelerationMax));
accelerationMax.setValidator(validator);
buttonList.clear();
buttonList.add(canFly = new GuiButton(3, x, 180, 160, 20, "Can fly"));
buttonList.add(done = new GuiButton(4, x + 180, 160, 140, 20, "Done"));
// canFly.displayString = camera.canFly ? "Can fly" : "Can't fly";
}
@Override
protected void actionPerformed(GuiButton button) throws IOException
{
switch (button.id)
{
case 3:
updateFlyButton();
break;
case 4:
mc.displayGuiScreen(null);
break;
}
}
private void updateFlyButton()
{
if (canFly.displayString == "Can fly")
{
canFly.displayString = "Can't fly";
}
else
{
canFly.displayString = "Can fly";
}
}
/* Mouse clicked, key typed and draw screen methods are here */
}
There's few issues I'm having here:
1. I can't pass entity to the GuiCamera, I tried using world.getEntitiesWithinAABB, but it throws and error saying "Error executing task"
2. I need Container in order to open the GUI, but I don't know how to pass entity in it and I don't know how to use this class to communicate between client and server or how to use it to change state of the entity
So now is the question: how can I use IGuiHandler to modify state of the custom entity?
Thank you for your attention
P.S.: I don't ask for ready to use code, I ask for guidance. Thanks.
P.S.S.: when I'm pressing "Done" button in GUI, for some reason game starts to glitch a little bit, for example when I use /give command given item/block doesn't appear in the inventory, but after a while it does appear. It looks like game goes out of sync. Is there any other code I should execute before closing the GUI with mc.displayGuiScreen(null);?