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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
    • Update your drivers: https://www.amd.com/en/support/graphics/amd-radeon-r9-series/amd-radeon-r9-200-series/amd-radeon-r9-280x
  • Topics

×
×
  • Create New...

Important Information

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