Jump to content

[1.7.10] Touble with gui buttons


kaaz

Recommended Posts

Hello all,

 

A few days ago I started playing around with Forge, and I'm having fun with it for the most part, but I'm stuck on getting the gui buttons to upgrade my item.

 

 

What I want:

Rightclick an item -> open interface -> options/buttons to upgrade item in question -> actually upgrade the item [dmg/speed/etc.]

 

I've searched for a while and followed tutorials such as http://www.minecraftforge.net/wiki/Containers_and_GUIs

 

 

I know I'm supposed to do something with a Container in order to sync the client with the server but I'm not sure how.

I could really use a push in the right direction.

 

 

 

What I'm doing currently:

 

In my Pickaxe<Extends ItemTool>'s onItemRightClick I'm calling player.openGui()

 

@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {
    if (world.isRemote) {//client
        player.openGui(PassiveEnchanting.instance, PassiveEnchanting.GUI_ITEM_UPGRADE, world, (int) player.posX, (int) player.posY, (int) player.posZ);
    }
    return super.onItemRightClick(itemStack, world, player);
}

 

In my mod's preinit I've registered a gui handler which goes like

@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event) {
    NetworkRegistry.INSTANCE.registerGuiHandler(instance, new GuiProxy());
}

 

The guiproxy creates the gui object for the client.

public class GuiProxy implements IGuiHandler {
    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        return null;
    }

    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
if (ID == PassiveEnchanting.GUI_ITEM_UPGRADE) {
    return new ItemUpgradeGui(player);
        }
        return null;
    }
}

 

I think the problem lies in the ItemUpgradeGui.actionPerformed method. At the moment I'm just sending the player's equiped itemstack to the Item class.

 

 

public class ItemUpgradeGui extends GuiScreen {
public static final int GUI_ID = PassiveEnchanting.GUI_ITEM_UPGRADE;
private EntityPlayer player;
private ItemStack equipedItem;
private boolean pickaxe = false;
private int startX = 75, startY = 50;
private String owner = "";
private int level = 0, xp = 0, points = 0;
private String[] statNames;
private int[] statValues;

public ItemUpgradeGui(EntityPlayer player) {
	this.player = player;
	equipedItem = player.getCurrentEquippedItem();
	pickaxe = equipedItem.getUnlocalizedName().equals(Names.Items.getFullName(Names.Items.LIVING_PICKAXE));
	loadItem(equipedItem);
}

public void loadItem(ItemStack itemStack) {
	if (pickaxe) {
		statNames = ((ItemLivingPickaxe) PEItems.pickaxe).getSpecialStats();
		statValues = new int[statNames.length];
		if (itemStack.getTagCompound() != null) {
			owner = itemStack.stackTagCompound.getString("owner");
			level = itemStack.stackTagCompound.getInteger("level");
			xp = itemStack.stackTagCompound.getInteger("xp");
			points = itemStack.stackTagCompound.getInteger("points");
			for (int i = 0; i < statNames.length; i++) {
				statValues[i] = itemStack.stackTagCompound.getInteger("stat_" + statValues[i]);
			}
		}
	}
}

@Override
public void initGui() {//id,x,y,w,h,display
	buttonList.clear();
	//drawString are: string, x, y, color
	for (int i = 0; i < statNames.length; i++) {
		buttonList.add(new GuiButton(i, startX, startY + (i * 20), 100, 20, String.format("%s [%s]", statNames[i], statValues[i])));
	}
}

@Override
public boolean doesGuiPauseGame() {
	return false;
}

@Override
public void drawScreen(int i, int j, float f) {
	drawDefaultBackground();
	super.drawScreen(i, j, f);
	fontRendererObj.drawStringWithShadow("Pickaxe upgrade menu", startX, startY - 20, 0xffFFFFFF);
	for (int ii = 0; i < statNames.length; i++) {
		fontRendererObj.drawString("Current: " + statValues[i], startX + 120, startY + 5 + (ii * 20), 0xffffffff);
	}
	if (points > 0) {
		fontRendererObj.drawString("You have " + points + " point(s) left!", startX, startY + 105, 0xffffffff);
	}
}

@Override
public void actionPerformed(GuiButton button) {
	System.out.println(String.format("Clicked on %s", statNames[button.id]));
	if (pickaxe) {
		((ItemLivingPickaxe) PEItems.pickaxe).upgradeStat(equipedItem, statNames[button.id]);
		loadItem(equipedItem);
	}
}
}

 

 

 

 

The complete project can be found at https://bitbucket.org/novaz/passive-enchants/src

 

Link to comment
Share on other sites

what does your upgradeStat method look like?

Thats the method where I store the upgrades

 

public void upgradeStat(ItemStack itemStack, String statName) {
	if (itemStack.stackTagCompound != null && Arrays.asList(stats).contains(statName)) {
		int points = itemStack.stackTagCompound.getInteger("points");
		if (points > 0) {
			itemStack.stackTagCompound.setInteger("stats_" + statName, itemStack.stackTagCompound.getInteger("stats_" + statName) + 1);
			itemStack.stackTagCompound.setInteger("points", points - 1);
			System.out.println("upgraded " + statName);
		}
	}
}

 

 

You will have to send packets. You cannot change data about Items on the client, and a Gui is always client-only.

 

Could I use something like http://www.minecraftforge.net/wiki/Advanced_Packet_Handling ? Or is it outdated

 

Link to comment
Share on other sites

Thanks I think I understand.

If I wanted to add more variables to the message I can just put it below the other ByteBufUtils.write.. and I just need to make sure I read it in the same sequence?

 

But how do I go from there to the players equiped item, can I just use the MessageContext.playerEntity.getCurrentEquippedItem() or is it a bad way to do it like that since the equipment slot could potentially have changed?

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.