Jump to content

[1.10.2] Gui help


Kokkie

Recommended Posts

So, I have made a gui that will, after some texts, trade xp - cookies but I need a player instance for me to check the xp, give a cookie and take xp, I have something that works, but it won't work if multiple people use it at the same time, this is what I have come up with: The gui class:

public class CookieGui extends GuiScreen {

public static GuiSlider cookies;
public boolean slider = false;
public EntityPlayer player = Minecraft.getMinecraft().thePlayer;
public static boolean cookiesC = false;
public static boolean cookiesS = false;
@Override
public void initGui() {
	slider = false;
	cookiesC = false;
	cookiesS = false;
	buttonList.add(new GuiButtonExt(1, width / 2 - 100, height / 4 + 42 + -16, 200, 20, "Goed geopend!"));
	cookies = new GuiSlider(8, width / 2 - 100, height / 4 + 42 + -16, 200, 20, "Ik wil ", " koekjes", 1, 64, 1, true, true);
}

@Override
protected void actionPerformed(GuiButton button) throws IOException {
	switch(button.id) {
	case 1 :
		buttonList.removeAll(buttonList);
		buttonList.add(new GuiButtonExt(2, width / 2 - 100, height / 4 + 42 + -16, 200, 20, "Stop!"));
		break;
	case 2 :
		buttonList.removeAll(buttonList);
		buttonList.add(new GuiButtonExt(3, width / 2 - 100, height / 4 + 42 + -16, 200, 20, "Ik zei STOP!"));
		break;
	case 3 :
		buttonList.removeAll(buttonList);
		buttonList.add(new GuiButtonExt(4, width / 2 - 100, height / 4 + 42 + -16, 200, 20, "Nooit luisteren he"));
		break;
	case 4 :
		buttonList.removeAll(buttonList);
		buttonList.add(new GuiButtonExt(5, width / 2 - 100, height / 4 + 42 + -16, 200, 20, "Wil je een koekje?"));
		buttonList.add(new GuiButtonExt(6, width / 2 - 100, height / 4 + 72 + -16, 75, 20, "Ja"));
		buttonList.add(new GuiButtonExt(7, width / 2 + 25, height / 4 + 72 + -16, 75, 20, "Nee"));
		break;
	case 5 :
		buttonList.removeAll(buttonList);
		buttonList.add(new GuiButtonExt(8, width / 2 - 100, height / 4 + 42 + -16, 200, 20, "Mahn, niet op klikken"));
		buttonList.add(new GuiButtonExt(9, width / 2 - 100, height / 4 + 72 + -16, 200, 20, "Terug naar koekjes"));
		break;
	case 6 :
		buttonList.removeAll(buttonList);
		buttonList.add(cookies);
		buttonList.add(new GuiButtonExt(10, width / 2 - 100, height / 4 + 72 + -16, 200, 20, "Ok"));
		slider = true;
		break;
	case 7 :
		buttonList.removeAll(buttonList);
		this.mc.displayGuiScreen((GuiScreen)null);
		break;
	case 8 :
		break;
	case 9 :
		buttonList.removeAll(buttonList);
		buttonList.add(new GuiButtonExt(5, width / 2 - 100, height / 4 + 42 + -16, 200, 20, "Wil je een koekje?"));
		buttonList.add(new GuiButtonExt(6, width / 2 - 100, height / 4 + 72 + -16, 75, 20, "Ja"));
		buttonList.add(new GuiButtonExt(7, width / 2 + 25, height / 4 + 72 + -16, 75, 20, "Nee"));
		break;
	case 10 :
		buttonList.removeAll(buttonList);
		slider = false;
		if(!player.isCreative()) {
			if(player.experienceLevel >= 3 * cookies.getValueInt()) {
				cookiesS = true;
				buttonList.add(new GuiButtonExt(11, width / 2 - 100, height / 4 + 42 + -16, 200, 20, "Alsjeblieft"));
			} else {
				buttonList.add(new GuiButtonExt(7, width / 2 - 100, height / 4 + 42 + -16, 200, 20, "Je hebt niet genoeg xp"));
			}
		} else {
			cookiesC = true;
			buttonList.add(new GuiButtonExt(11, width / 2 - 100, height / 4 + 42 + -16, 200, 20, "Alsjeblieft"));
		}
		break;
	case 11 :
		CheeseCookieBlock.remove();
		cookiesC = false;
		cookiesS = false;
		this.mc.displayGuiScreen((GuiScreen)null);
		break;
	}
}

@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
	if(slider) {
		drawCenteredString(fontRendererObj, "Je krijgt " + String.valueOf(cookies.getValueInt()) + " koekjes, dat is " + String.valueOf(cookies.getValueInt() * 3) + " XP levels" , width / 2, height / 4 + 102 + -16, 0xFFFFFF);
	}
	super.drawScreen(mouseX, mouseY, partialTicks);
}
}

The block class:

public class CookieBlock extends Block {
public static EntityPlayer player;

public CookieBlock () {
	super(Material.ROCK);
}

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
	player = playerIn;
	playerIn.openGui(MainClass.Instance, GuiHandler.COOKIEGUIID, worldIn, pos.getX(), pos.getY(), pos.getZ());
	return super.onBlockActivated(worldIn, pos, state, playerIn, hand, heldItem, side, hitX, hitY, hitZ);
}

public static void remove() {
	if(CookieGui.cookiesC) {
		player.inventory.addItemStackToInventory(new ItemStack(Items.COOKIE, CookieGui.cookies.getValueInt()));
		player.addStat(KokkieAchievements.COOKIE);
	} else if(CookieGui.cookiesS) {
		player.inventory.addItemStackToInventory(new ItemStack(Items.COOKIE, CookieGui.cookies.getValueInt()));
		player.removeExperienceLevel(3 * CookieGui.cookies.getValueInt());
		player.addStat(KokkieAchievements.COOKIE);
	}
}

}

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

The client that sent the packet is the player that sent the packet.  Duh.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Kokkie,

 

You are missing some fundamental ideas about how the client and server work. In most multiplayer games, including Minecraft, the server is the ultimate determiner of what happens in the game and the client(s) just allows interaction with the user. So the client primarily is responsible for taking in input, rendering graphics and playing sounds.

 

However, it isn't quite so simple because if that was all the client did then it would look really glitchy because the network would not be able to smoothly transmit all the data needed. So most gaming clients also provide some "tweening" (smoothing out the gameplay between syncs with the server). This means that the client also runs some game code so that it will mostly be correct until the server syncs up again.

 

But despite the client running some of the game code, the server must always get the information from the client via networking before it can take action on it.

 

When you play single-player it is actually still running a client and a server. However, in Minecraft it is a bit "goofy" because both run in the same Java instance. So it looks like only one program is running which can hide the fact that your Java programming won't work when the client and server are actually separate. For example, as diesieben07 pointed out in single player you could have a static field that could be accessed by both client and server (because both are running in the same instance) without any network activity to sync it. But that is still bad because it will break as soon as you run actual mult-player.

 

To fix this, you need to make sure a packet is sent to sync up the information. Any time user input happens the client needs to send it to the server, and any time the server updates something (like the position of an entity) it needs to send a packet from server to client. Now there are many cases where the packet will automatically be sent for you. For example, if you place a block on the server there is already packets that get sent to update the client. However, if you're doing something custom you probably need a custom packet.

 

So in your case, you need to set up a networking system (see any good tutorial) and whenever the condition in the GUI happens where you want to award the XP Cookie, you should send a custom packet to the server. The server will know which player it is because it knows which client the packet came from.

 

I've got a tutorial on networking here if you're interested: http://jabelarminecraft.blogspot.com/p/minecraft-forge.html

 

This is an important concept and takes every modder a while to really understand. But it is worth it to understand or you'll constantly be getting "weird" behavior in your mods due to lack of synchronization.

 

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

It now crashes when I register them, my code: Message and message handler

public class MyMessage implements IMessage {

public MyMessage(){
}

private boolean creative;
private int amount;

public MyMessage(boolean isCreative, int cookieAmount) {
	this.amount = cookieAmount;
	this.creative = isCreative;
}

@Override
public void toBytes(ByteBuf buf) {
	buf.writeBoolean(creative);
	buf.writeInt(amount);
}

@Override 
public void fromBytes(ByteBuf buf) {
	creative = buf.readBoolean();
	amount = buf.readInt();
}

public int getAmount() {
	return amount;
}

public boolean isCreative() {
	return creative;
}

public class MyMessageHandler implements IMessageHandler<MyMessage, IMessage> {
	@Override
        public IMessage onMessage(final MyMessage message, final MessageContext ctx) {
            final EntityPlayerMP player = ctx.getServerHandler().playerEntity;
            IThreadListener mainThread = (WorldServer) player.worldObj;
            mainThread.addScheduledTask(new Runnable() {
                @Override
                public void run() {
                    System.out.println(String.format("Received %s and %s from %s", message.getAmount(), message.isCreative(), player.getDisplayName()));
                	player.worldObj.spawnEntityInWorld(new EntityItem(player.worldObj, player.posX, player.posY, player.posZ, new ItemStack(Items.COOKIE, message.getAmount())));
                	player.addStat(Achievements.COOKIE);
                    if(!message.isCreative()) {
                    	player.removeExperienceLevel(message.getAmount() * 3);
                    }
                }
            });
            return null;
        }
}

}

PacketHandler class

public class PacketHandler {

public static SimpleNetworkWrapper INSTANCE;
public static int ID = 0;

public void registerPackets() {
	INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel("myChannel");
	INSTANCE.registerMessage(MyMessage.MyMessageHandler.class, MyMessage.class, ID++, Side.CLIENT);
}
}

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

1) You didn't post the crash, but....

 

2) Do not put the IMessageHandler in the same class as the IMessage. You'll only make it harder to separate out which bit belongs to which class.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

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