Jump to content

Recommended Posts

Posted (edited)

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
Posted

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.

 

 

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • "I want to understand how complex mods with ASM transformation and coremods work, such as Xray or AntiXray. Why do they break when you simply rename packages? What features of their architecture make refactoring difficult? And what techniques are used to protect these mods? I am interested in technical aspects in order to better understand the bytecode and Forge loader system."
    • I can't figure out if you're looking for help trying to steal someone elses work, or cheat at the game....
    • Title: Why Is It So Hard to Rename and Restructure Mods Like Xray or AntiXray? 🤔 Post text: Hey everyone! I’ve been digging into Minecraft modding for a while and have one big question that I can’t figure out on my own. Maybe someone with more experience could help or give me some advice. Here’s the issue: When I take a “normal” Minecraft mod — for example, one that just adds some blocks or new items — I can easily change its structure, package names, or even rebrand it entirely. It’s straightforward. But as soon as I try this with cheat-type mods like XrayMod or AntiXray, everything falls apart. Even if I just rename the classes, refactor the packages, or hide its identity somehow, the mod either breaks or stops working properly. XrayMod in particular is proving to be a nightmare to modify without losing its core function. So my question is — why is this so much harder with cheat mods like Xray? Is there something fundamentally different about how they’re coded, loaded, or protected that prevents simple renaming or restructuring? And if so, how can I actually learn to understand someone else’s cheat mod enough to safely refactor it without breaking the core features? I’ve already been spending over two months trying to figure this out and haven’t gotten anywhere. It feels like there must be some trick or knowledge I’m missing. Would really appreciate any thoughts, tips, or references — maybe there are guides or techniques for understanding cheat-mod internals? Or if you’ve successfully “disguised” a cheat mod like Xray before, I’d love to hear how you did it. Thanks in advance for any help or discussion. ✌️
    • just started making cinamatic contect check it out on my channel or check out my facebook page    Humbug City Minecraft Youtube https://www.youtube.com/watch?v=v2N6OveKwno https://www.facebook.com/profile.php?id=61575866982337  
    • Where did you get the schematic? Source/Link? And do use an own modpack or a pre-configured from curseforge? If yes, which one On a later time, I can make some tests on my own - but I need the schematic and the modpack name
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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