Jump to content

[Solved]Packets...


Naiten

Recommended Posts

Hello. I've read the tutorial about packets and understand that they are used to transfer data between server and client side. There's said that packets are good for TE and GUI, but should i use them for usual entities or key bindings? If yes, tell me how, please.

If i helped you, don't forget pressing "Thank You" button. Thanks for your time.

Link to comment
Share on other sites

That tutorial only shows how to make a packet that does some some weird stuff with random numbers and i can hardly understand how to use that in my locomotive control system.

If i helped you, don't forget pressing "Thank You" button. Thanks for your time.

Link to comment
Share on other sites

uhm, what weird thing is it you don't understand?

If I recall correctly he is generating random numbers on the server side, then sending them to the client with a packet to display it on screen for the client?

 

In other words, it just shows how to transfer an int from server -> client.

It also states that you can send strings, floats etc. as well using the same procedure.

 

So what is it you are struggling with?

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

I've made a key handler that adds some motion to the locomotive the player is currently riding when key is pressed. It defines the key well and locomotive moves, but it gets teleported back after some seconds. I guess it's because of mistiming between server and client, so i have to use packets...

I send a packet in the key handler class... And where should i read it and apply changes to the entity?

If i helped you, don't forget pressing "Thank You" button. Thanks for your time.

Link to comment
Share on other sites

Yay, thanks, diesieben07. That actually helped. I'm only wodering now, am i doing it right?

 

package net.row;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.EnumSet;

import org.lwjgl.input.Keyboard;

import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.row.stock.LocoCherepanov;
import cpw.mods.fml.client.registry.KeyBindingRegistry.KeyHandler;
import cpw.mods.fml.common.TickType;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;

public class RoWKeyHandler extends KeyHandler {
    
    static KeyBinding ReverseUp = new KeyBinding("ReverseUp", Keyboard.KEY_R);

    public RoWKeyHandler() {
            super(new KeyBinding[]{ReverseUp}, new boolean[]{false});
    }
    @Override
    public String getLabel() {
            return "RoWKeyBindings";
    }
    @Override
    public void keyDown(EnumSet<TickType> types, KeyBinding kb,
                    boolean tickEnd, boolean isRepeat) {
    	if(kb.equals(ReverseUp)){
    		EntityPlayer player = Minecraft.getMinecraft().thePlayer;
    		if(player.ridingEntity instanceof LocoCherepanov){
    			LocoCherepanov loco = (LocoCherepanov) player.ridingEntity;
    			loco.reverse = -0.03F;
                
                ByteArrayOutputStream bos = new ByteArrayOutputStream(;
                DataOutputStream outputStream = new DataOutputStream(bos);
                try {
                	outputStream.writeFloat(loco.reverse);
                } catch (Exception ex) {
                        ex.printStackTrace();
                }
                
                Packet250CustomPayload packet = new Packet250CustomPayload();
                packet.channel = "RoWLocoControl";
                packet.data = bos.toByteArray();
                packet.length = bos.size();
                
                Side side = FMLCommonHandler.instance().getEffectiveSide();
                if (side == Side.CLIENT) {
                	EntityClientPlayerMP playerMP = (EntityClientPlayerMP) player;
                	playerMP.sendQueue.addToSendQueue(packet);
                }
             }
    	}
    }
    @Override
    public void keyUp(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd) {
    }
    @Override
    public EnumSet<TickType> ticks() {
            return EnumSet.of(TickType.CLIENT);
    }
}

 

package net.row;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.row.stock.LocoCherepanov;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;

public class RoWPacketHandler implements IPacketHandler {
        @Override
        public void onPacketData(INetworkManager manager,
                        Packet250CustomPayload packet, Player player) {
                
                if (packet.channel.equals("RoWLocoControl")) {
                        handleControl(packet, player);
                }
        }
        private void handleControl(Packet250CustomPayload packet, Player player) {
                DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
                float reverse;
                try {
                	reverse = inputStream.readFloat();
                	EntityPlayer entityPlayer = (EntityPlayer)player;
                	if(entityPlayer.ridingEntity instanceof LocoCherepanov){
                		LocoCherepanov loco = (LocoCherepanov) entityPlayer.ridingEntity;
                		loco.reverse = reverse;
                	}
                } catch (IOException e) {
                	e.printStackTrace();
                	return;
                }
        }

}

 

And one more question. If i perform some changes to entity motion and position variables, that are later used in default moveEntity(), in onUpdate() and locomotive movement is a bit glitchy (it does not get teleported back, but twitches), is that also a packet problem or not?

If i helped you, don't forget pressing "Thank You" button. Thanks for your time.

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.