Jump to content

[RESOLVED][1.8] Data from server to GUI


tancfire

Recommended Posts

Hello everyone,

 

I have a big problem. I try to get data from server, to put it in a text box, in a GUI, on client side.

 

To open the gui on the current player, it's an other player who type a command. I used a packet to do it. This part works fine. BUT, i can't send a data string from server to the GUI.

 

Here my code:

 

The Command:

public class OfferAllianceCommand extends BaseCommand {

	public OfferAllianceCommand() {
		super("offerAlliance", 2);
	}

	@Override
	public String getCommandUsage(ICommandSender sender) {
		return "/offerAlliance <kingdom> <kingdomTarget>";
	}

	@Override
	public int compareTo(Object arg0) {
		return 0;
	}

	@Override
	public boolean canCommandSenderUse(ICommandSender sender) {
		return true;
	}

	@Override
	public List addTabCompletionOptions(ICommandSender sender, String[] args,BlockPos pos) {
		return null;
	}

	@Override
	public boolean isUsernameIndex(String[] args, int index) {
		return false;
	}

	@Override
	public void onExecute(ICommandSender sender, String[] args) {
		String kingdomName = args[0];
		String kingdomTargetName = args[1];

		KingdomAgeSavedData kt = KingdomAgeSavedData.forWorld(sender.getEntityWorld());
		Kingdom kingdom = kt.getKingdomByName(kingdomName);
		Kingdom kingdomTarget = kt.getKingdomByName(kingdomTargetName);
		
		if(isKingdomExist(sender, kingdom, kingdomName) // si le royaume existe
				&& isOwnKingdom(sender, kingdom, sender.getName()) //si le joueur en est le roi
				&& isKingdomExist(sender, kingdomTarget, kingdomTargetName) //si le royaume cible existe
				&& isConnected(sender, kingdom.getOwner())){ //Et le joueur cible est connecté
			
			System.out.println("Joueur qui envoit l'offre:  "+sender.getName());
			
			EntityPlayerMP playerTarget = MinecraftServer.getServer().getConfigurationManager().getPlayerByUsername(kingdomTarget.getOwner());
			
			playerTarget.getEntityData().setString("allianceOffer", kingdomName);
			
			System.out.println("Kingdom of alliance: "+playerTarget.getEntityData().getString("allianceOffer"));
			
			KingdomAgeMod.network.sendTo(new AllianceOfferMessage(), playerTarget);
			//playerTarget.openGui(KingdomAgeMod.instance, GUIs.ALLIANCE_OFFER.ordinal(), sender.getEntityWorld(), playerTarget.getPosition().getX(), playerTarget.getPosition().getY(),playerTarget.getPosition().getZ());
			//FMLNetworkHandler.openGui(playerTarget, KingdomAgeMod.instance, GUIs.ALLIANCE_OFFER.ordinal(), playerTarget.getEntityWorld(), playerTarget.getPosition().getX(), playerTarget.getPosition().getY(),playerTarget.getPosition().getZ());
		}
	}

}

 

The packet:

 


public class AllianceOfferMessage implements IMessage{
	
	/*private int x;
	private int y;
	private int z;*/
	
	public AllianceOfferMessage()
	{
		
	}

	
	/*public AllianceOfferMessage(int x, int y, int z)
	{
		this.x = x;
		this.y = y;
		this.z = z;
	}*/
	
	
	@Override
	public void fromBytes(ByteBuf buf) {
		/*x = ByteBufUtils.readVarInt(buf, 5);
		y = ByteBufUtils.readVarInt(buf, 5);
		z = ByteBufUtils.readVarInt(buf, 5);*/
	}

	@Override
	public void toBytes(ByteBuf buf) {
		/*ByteBufUtils.writeVarInt(buf, x, 5);
		ByteBufUtils.writeVarInt(buf, y, 5);
		ByteBufUtils.writeVarInt(buf, z, 5);*/
	}
	
	
public static class Handler implements IMessageHandler<AllianceOfferMessage, IMessage> {
        
        @Override
        public IMessage onMessage(AllianceOfferMessage message, MessageContext ctx) {
        	EntityPlayerSP player = Minecraft.getMinecraft().thePlayer;
        	
        	System.out.println("Joueur qui reçoit l'offre:  "+player.getName());
        	
        	player.openGui(KingdomAgeMod.instance, GUIs.ALLIANCE_OFFER.ordinal(), player.getEntityWorld(), player.getPosition().getX(), player.getPosition().getY(),player.getPosition().getZ());
        	
        	
        	return null;
        }
}
}

 

The GUI:

 



public class GuiAllianceOffer extends GuiScreen{
	
    private int x, y, z;
    private EntityPlayer player;
    private World world;
    private int xSize, ySize;
    
    private GuiButton boutonAccept;
    private GuiButton boutonRefuse;
    
    private String kingdomName;
    
    private ResourceLocation backgroundimage = new ResourceLocation(KingdomAgeMod.MODID.toLowerCase() + ":" + "textures/client/gui/guikingdomerrename.png");

	public GuiAllianceOffer(EntityPlayer player, World world, int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.player = player;
        this.world = world;
        xSize = 185;
        ySize = 137;
        
        kingdomName = player.getEntityData().getString("allianceOffer");
	}
	
	@Override
	public void updateScreen()
	{
		super.updateScreen();
	}
	
	@Override
	protected void keyTyped(char typedChar, int keyCode) throws IOException
    {
		super.keyTyped(typedChar, keyCode);
    }
	
	
	@Override
    public void initGui()
    {
        boutonAccept = new GuiButton(1, 0, 0, 50,20,"Accept");
        boutonRefuse = new GuiButton(2,0,0,50,20, "Refuse");
        buttonList.add(boutonAccept);
        buttonList.add(boutonRefuse);
    }
	
	
    @Override
    public void drawScreen(int mouseX, int mouseY, float renderPartialTicks) {
        int x = (this.width - xSize) / 2;
        int y = (this.height - ySize) / 2;
        
        boutonRefuse.xPosition = x+xSize-55;
        boutonRefuse.yPosition = y+ySize-25;
        boutonAccept.xPosition = x+5;
        boutonAccept.yPosition = y+ySize-25;
        
        System.out.println("Kingdom of alliance2: "+kingdomName);
    	
        this.mc.getTextureManager().bindTexture(backgroundimage);
        drawTexturedModalRect(x, y, 0, 0, xSize,  ySize);
        drawCenteredString(this.fontRendererObj, "The kingdom of "+kingdomName, x+(xSize/2), y+5, -1);
        drawCenteredString(this.fontRendererObj, "offer you an alliance", x+(xSize/2), y+20, -1);
        
        boutonAccept.drawButton(mc, mouseX, mouseY);
        boutonRefuse.drawButton(mc, mouseX, mouseY);
    }
    
    
    @Override
    protected void actionPerformed(GuiButton button) throws IOException
    {
    	if(button==this.boutonAccept)
    	{
    		//KingdomAgeMod.network.sendToServer(new RenameKingdomMessage(player.getEntityData().getInteger("id_kingdom"),this.editKingdomName.getText()));
    		this.player.closeScreen(); //On ferme la fenêtre du GUI.
    	} else if (button==this.boutonRefuse)
    	{
    		//KingdomAgeMod.network.sendToServer(new DeleteKingdomMessage(player.getEntityData().getInteger("id_kingdom"),x,y,z));
    		this.player.closeScreen();
    	}
    	
    }
    

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

	

}

 

I try to put NBTag to the player on server side, and get it on the client side, but it's not working.

 

I have no idea how to do it, so a little help will be appreciate ^^

 

Also, sorry for my english, it's not my native language.

Edited by tancfire
Link to comment
Share on other sites

It works !

Thanks you very muck !

 

Now, my code is:

 

My packet:

public class AllianceOfferMessage implements IMessage{
	
	private String kingdomName;
	
	public AllianceOfferMessage()
	{
		
	}

	
	public AllianceOfferMessage(String kingdomName)
	{
		this.kingdomName = kingdomName;
	}
	
	
	@Override
	public void fromBytes(ByteBuf buf) {
		kingdomName = ByteBufUtils.readUTF8String(buf);
		
	}

	@Override
	public void toBytes(ByteBuf buf) {
		ByteBufUtils.writeUTF8String(buf, kingdomName);

	}
	
	
public static class Handler implements IMessageHandler<AllianceOfferMessage, IMessage> {
        
        @Override
        @SideOnly(Side.CLIENT)
        public IMessage onMessage(AllianceOfferMessage message, MessageContext ctx) {
        	EntityPlayerSP player = Minecraft.getMinecraft().thePlayer;
        	
        	Minecraft.getMinecraft().displayGuiScreen(new GuiAllianceOffer(player,player.getEntityWorld(),player.getPosition().getX(),player.getPosition().getY(),player.getPosition().getZ(), message.kingdomName));
        	
        	return null;
        }
}
}

Note: To use "Minecraft::displayGuiScreen", i had to put "@SideOnly(Side.CLIENT)

 

My GUI:


	public GuiAllianceOffer(EntityPlayer player, World world, int x, int y, int z, String kingdomName) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.player = player;
        this.world = world;
        xSize = 185;
        ySize = 137;
        
        this.kingdomName = kingdomName;
	}
	
	@Override
	public void updateScreen()
	{
		super.updateScreen();
	}
	
	@Override
	protected void keyTyped(char typedChar, int keyCode) throws IOException
    {
		super.keyTyped(typedChar, keyCode);
    }
	
	
	@Override
    public void initGui()
    {
        boutonAccept = new GuiButton(1, 0, 0, 50,20,"Accept");
        boutonRefuse = new GuiButton(2,0,0,50,20, "Refuse");
        buttonList.add(boutonAccept);
        buttonList.add(boutonRefuse);
    }
	
	
    @Override
    public void drawScreen(int mouseX, int mouseY, float renderPartialTicks) {
        int x = (this.width - xSize) / 2;
        int y = (this.height - ySize) / 2;
        
        boutonRefuse.xPosition = x+xSize-55;
        boutonRefuse.yPosition = y+ySize-25;
        boutonAccept.xPosition = x+5;
        boutonAccept.yPosition = y+ySize-25;
    	
        this.mc.getTextureManager().bindTexture(backgroundimage);
        drawTexturedModalRect(x, y, 0, 0, xSize,  ySize);
        drawCenteredString(this.fontRendererObj, "The kingdom of "+kingdomName, x+(xSize/2), y+5, -1);
        drawCenteredString(this.fontRendererObj, "offer you an alliance", x+(xSize/2), y+20, -1);
        
        boutonAccept.drawButton(mc, mouseX, mouseY);
        boutonRefuse.drawButton(mc, mouseX, mouseY);
    }
    
    
    @Override
    protected void actionPerformed(GuiButton button) throws IOException
    {
    	if(button==this.boutonAccept)
    	{
    		//KingdomAgeMod.network.sendToServer(new RenameKingdomMessage(player.getEntityData().getInteger("id_kingdom"),this.editKingdomName.getText()));
    		this.player.closeScreen(); //On ferme la fenêtre du GUI.
    	} else if (button==this.boutonRefuse)
    	{
    		//KingdomAgeMod.network.sendToServer(new DeleteKingdomMessage(player.getEntityData().getInteger("id_kingdom"),x,y,z));
    		this.player.closeScreen();
    	}
    	
    }
    

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

	

}

Here, I put a new parameter "kingdomName" in the gui's constructor.

 

 

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.