Jump to content

[1.7.10] Sending RF storage to client(gui) & sending packet from gui to server


IvanTune

Recommended Posts

Hello there!

 

1. So been having a lot of trouble with tileentity syncing and packets. The first thing i I am trying to do is send the RF storage from the server to the client so that the player can see in the GUI what the storage is at. It works fine on a SP world, but when I tried on a server the RF would go up to 32k, then reset itself and stay at 0, then go up to 32k and reset itself again in the gui.

 

TileEntitySolarPanel

public void writeToNBT(NBTTagCompound nbt) {
	super.writeToNBT(nbt);
	storage.writeToNBT(nbt);
        }
public void readFromNBT(NBTTagCompound nbt) {
	super.readFromNBT(nbt);
	storage.readFromNBT(nbt);
}

public void updateEntity() {
	if (!worldObj.isRemote) {
		if(storage.getEnergyStored() < storage.getMaxEnergyStored()) {

		int i = storage.getEnergyStored() + currentRfgeneration;

		storage.setEnergyStored(i);
	}
}
}

 

ContainerSolarPanel


private TileEntitySolarPanel solarPanel;
int lastEnergyStored;

    @Override
public void addCraftingToCrafters(ICrafting icrafting) {
	super.addCraftingToCrafters(icrafting);
	icrafting.sendProgressBarUpdate(this, 0, this.solarPanel.getEnergyStored());
}

public void detectAndSendChanges() {
	super.detectAndSendChanges();

	for (int i = 0; i < this.crafters.size(); i++) {
		ICrafting icrafting = (ICrafting) this.crafters.get(i);

		if (this.lastEnergyStored != this.solarPanel.getEnergyStored()) {
			icrafting.sendProgressBarUpdate(this, 0, this.solarPanel.getEnergyStored());
		}
	}
	this.lastEnergyStored = this.solarPanel.getEnergyStored();
}

@SideOnly(Side.CLIENT)
@Override
public void updateProgressBar(int slot, int newValue) {

	if (slot == 0) {
		this.solarPanel.setEnergyStored(newValue);
	}
}

 

 

2. Also when a player does changes to stuff in GUI (ints), i want these to be sent to the server. I'm not quite sure if I am doing packets right and I am not sure what to put in the OnMessage so that the int is sent right. I also want the int to be sent back to the client so that other people can see it, is it ok to just do it through the container?

 

MessageGuiSolarTab1Height

package com.EvanTune.futuregate.network;

import com.EvanTune.futuregate.block.machine.solar_panel.tab.TestTab;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;

public class MessageGuiSolarTab1Height implements IMessage, IMessageHandler<MessageGuiSolarTab1Height, IMessage>
{
    private int tabHeight;


    public MessageGuiSolarTab1Height()
    {}

    public MessageGuiSolarTab1Height(int value)
    {
        this.tabHeight = value;
    }

    @Override
    public void toBytes(ByteBuf buffer)
    {
        buffer.writeInt(tabHeight);
    }

    @Override
    public void fromBytes(ByteBuf buffer)
    {
        this.tabHeight = buffer.readInt();
    }

    @Override
    public IMessage onMessage(MessageGuiSolarTab1Height message, MessageContext ctx)
    {

        	TestTab heighty = new TestTab(tabHeight);

        	heighty.tempHeightSetting = message.tabHeight;
        

        return null;
    }
}

 

NetworkHandler

package com.EvanTune.futuregate.network;

import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;

public class NetworkHandler
{
    public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.CHANNEL);
    private static int id = 0;

    public static void init()
    {
        INSTANCE.registerMessage(MessageGuiSolarTab1Height.class, MessageGuiSolarTab1Height.class, id++, Side.SERVER);

    }
}

 

TestTab

@Override
public void mouseReleased(GuiSolarPanel gui, int x, int y, int button) {
	NetworkHandler.INSTANCE.sendToServer(new MessageGuiSolarTab1Height(tempHeightSetting));
	isDragging = false;
}

 

Thanks in advance for any help or tips!

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.



×
×
  • Create New...

Important Information

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