Jump to content

Packets not working correctly [1.7.10]


Azazell

Recommended Posts

I created working packets, but by some reasons all machines updates gui of each others, when 1 machines contains 1000RF, and i placing other machine, and open it's gui it displays 1000RF, but this machine not contains 1000RF.

 

Here's my code of packets:

public class PacketProgressBar implements IMessage{

    private PartEnergyInterface sender;
    private NBTTagCompound data;

    public PacketProgressBar(){
        // AutoMate, added for minecraft base handling
    }
    public PacketProgressBar(int Storage, int x,int y,int z,ForgeDirection dir){
        this.data = new NBTTagCompound();

        data.setInteger("EnergyStored",Storage);

        Gui gui = Minecraft.getMinecraft().currentScreen;
        if(gui instanceof GuiEnergyInterface){
            GuiEnergyInterface GEI = (GuiEnergyInterface)gui;

            GEI.storage = Storage;
        }

    }
    public PacketProgressBar(PartEnergyInterface sender){
        this(sender.getEnergyStorage(sender.bar).getEnergyStored(),sender.getX(),sender.getY(),sender.getZ(),sender.getSide());
        this.sender = sender;
    }
    @Override
    public void fromBytes(ByteBuf buf) {
        data = ByteBufUtils.readTag(buf);
    }

    @Override
    public void toBytes(ByteBuf buf) {

        ByteBufUtils.writeTag(buf,data);
    }
    public static class Handler extends ClientMessageHandler<PacketProgressBar>{
        @Override
        public IMessage handleClientMessage(EntityPlayer player, PacketProgressBar message, MessageContext ctx) {
            return null;
        }
    }

}

 

Edited by Azazell
Link to comment
Share on other sites

Well first and foremost your handler does absolutely nothing with your message. So I have no idea why you are getting any results at all in the first place. Unless some other code you've not shown does something else.

public static class Handler extends ClientMessageHandler<PacketProgressBar>{
        @Override
        public IMessage handleClientMessage(EntityPlayer player, PacketProgressBar message, MessageContext ctx) {
            return null;
        }
    }

What is ClientMessageHandler btw?

 

Is this message being send from server to the client or the other way around? From your description it seems like you want a server -> client message but then this

public PacketProgressBar(int Storage, int x,int y,int z,ForgeDirection dir){
        this.data = new NBTTagCompound();

        data.setInteger("EnergyStored",Storage);

        Gui gui = Minecraft.getMinecraft().currentScreen;
        if(gui instanceof GuiEnergyInterface){
            GuiEnergyInterface GEI = (GuiEnergyInterface)gui;

            GEI.storage = Storage;
        }

    }

makes zero sense since it accesses client classes.

 

Also don't use NBT to send ONE integer. Just send that integer. 

 

Your code takes some values from the thing you've passed to it on the server and then proceeds to reach across sides to set the values on the client. It also writes some data to NBT, sends it over the network and does nothing with it. This is not how messages work. Read the docs.

Link to comment
Share on other sites

ClientMessageHandler:

public abstract class ClientMessageHandler<T extends IMessage> extends AIMessageHandler<T>{
    public final IMessage handleServerMessage(EntityPlayer player, T message, MessageContext ctx) {
        return null;
    }
}

AIMessageHandler:

public abstract class AIMessageHandler<T extends IMessage> implements IMessageHandler<T, IMessage> {
            @SideOnly(Side.CLIENT)
            public abstract IMessage handleClientMessage (EntityPlayer player, T message, MessageContext ctx);

            public abstract IMessage handleServerMessage (EntityPlayer player, T message, MessageContext ctx);

            @Override
            public IMessage onMessage (T message, MessageContext ctx){
            if (ctx.side.isClient()) {
                return handleClientMessage(AppliedIntegrations.proxy.getPlayerEntity(ctx), message, ctx);
            } else {
                return handleServerMessage(AppliedIntegrations.proxy.getPlayerEntity(ctx), message, ctx);
            }
        }
}

Sending:

NetworkHandler.sendTo(new PacketProgressBar(this), (EntityPlayerMP) listener.player);

Handler:

public class NetworkHandler {
    private static byte packetId = 0;

    private static final SimpleNetworkWrapper Handler = NetworkRegistry.INSTANCE.newSimpleChannel(AppliedIntegrations.modid);

    /**
     * Call this during pre-init or loading and register all of your packets (messages) here
     */
    public static final void registerPackets() {
        NetworkHandler.registerMessage(PacketProgressBar.Handler.class, PacketProgressBar.class, Side.CLIENT);
        NetworkHandler.registerMessage(PacketFilterChange.Handler.class,PacketFilterChange.class,Side.SERVER);
        NetworkHandler.registerMessage(PacketBarChange.Handler.class,PacketBarChange.class,Side.CLIENT);

    }

    /**
     * Registers a message and message handler
     */
    private static final void registerMessage(Class handlerClass, Class messageClass, Side side) {
        NetworkHandler.Handler.registerMessage(handlerClass, messageClass, packetId++, side);
    }

    // send packet info to player
    public static final void sendTo(IMessage message, EntityPlayerMP player) {
        NetworkHandler.Handler.sendTo(message, player);
    }

    public static final void sendToAllAround(IMessage message, NetworkRegistry.TargetPoint point) {
        NetworkHandler.Handler.sendToAllAround(message, point);
    }

    public static final void sendToAllAround(IMessage message, int dimension, double x, double y, double z,

                                             double range) {
        NetworkHandler.sendToAllAround(message, new NetworkRegistry.TargetPoint(dimension, x, y, z,

                range));
    }

    public static final void sendToAllAround(IMessage message, EntityPlayer player, double range) {
        NetworkHandler.sendToAllAround(message, player.worldObj.provider.dimensionId, player.posX,

                player.posY, player.posZ, range);
    }

    public static final void sendToDimension(IMessage message, int dimensionId) {
        NetworkHandler.Handler.sendToDimension(message, dimensionId);
    }

    public static final void sendToServer(IMessage message) {
        NetworkHandler.Handler.sendToServer(message);
    }
}
Edited by Azazell
Link to comment
Share on other sites

@Azazell 1.8.9 and below are no longer supported on this forum please update to receive support.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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