Jump to content

Recommended Posts

Posted

im trying to fix a few things for a custome toilet whit running water
in this moment i alredy have keybinds and custome packages to be sended to the server side and thas working 

now im trying to send a package from the server side to the client side

soo ijust copy paste the server package code and change in the declaration NetworkDirection.PLAY_TO_SERVER to NetworkDirection.PLAY_TO_CLIENT
seems it dont trow errors but don't works  still the messages gets in the console so its running 
i notice in this line the player is null 

	ServerPlayer player = context.getSender();
System.out.print( ((player != null)? player.getDisplayName() : "NullPlayer" ) ); //this returns nullPlayer 
	

##############################

i need a guide on how to send a package form server to client side 
or 
a way to get the player entity form the local world i remember in 1.8 you have to do something like 

Minecraft mc = new Minecraft()

Player pe = mc.getThePlayer();

 

....

half working code i made just guessing 

Spoiler

				
			package mercblk.networking;
		
		import com.mojang.authlib.minecraft.client.MinecraftClient;
		import mercblk.entity.running_water_ghost;
		import net.minecraft.ChatFormatting;
		import net.minecraft.client.Minecraft;
		import net.minecraft.client.main.GameConfig;
		import net.minecraft.nbt.CompoundTag;
		import net.minecraft.network.FriendlyByteBuf;
		import net.minecraft.network.chat.Style;
		import net.minecraft.server.level.ServerLevel;
		import net.minecraft.server.level.ServerPlayer;
		import net.minecraft.world.entity.Pose;
		import net.minecraft.world.entity.player.Player;
		import net.minecraft.world.level.Level;
		import net.minecraftforge.network.NetworkEvent;
		
		import java.util.function.Supplier;
		
		public class set_crawl_locally {
		
		boolean crawl = false;
		
		
		public set_crawl_locally(boolean crawl) {
		this.crawl = crawl;
		}
		
		public set_crawl_locally(FriendlyByteBuf buf) {
		CompoundTag nbt = buf.readNbt();
		this.crawl = nbt.getBoolean("crawl");
		}
		
		public void toBytes(FriendlyByteBuf buf) {
		CompoundTag nbt = new CompoundTag();
		nbt.putBoolean("crawl", this.crawl);
		buf.writeNbt(nbt);
		}
		
		public boolean handle(Supplier<NetworkEvent.Context> supplier) {
		NetworkEvent.Context context = supplier.get();
		context.enqueueWork(() -> {
		// HERE WE ARE ON THE LOCAL!
		
		System.out.println("HERE WE ARE ON THE LOCAL!" + this.crawl ) ;
		
		Minecraft mf = new Minecraft((GameConfig)null);
		
		//Text.translable().fillStyle(Style.EMPTY.withColor(ChatFormatting.RED ))
		
		
		ServerPlayer player = context.getSender();
		//ServerLevel warudo = player.getLevel();
		System.out.print( ((player != null)? player.getDisplayName() : "NullPlayer" ) );
		//Here the player is null
		//but the rest of code dont display errors just dont work
		
		
		Pose pose = player.getPose();
		System.out.print( "Pose = " + ((pose != null)? pose.toString() : "Null ") );
		
		if( this.crawl ){
		if( pose != Pose.SWIMMING ) {
		player.setForcedPose(Pose.SWIMMING);
		}
		}
		if( !this.crawl )
		{
		if( pose != null ) {
		player.setForcedPose(null);
		}
		}
		
		pose = player.getPose();
		System.out.print( "Pose = " + ((pose != null)? pose.toString() : "Null ") );
		
		});
		return true;
		}
		
		}				
			

 

 

 

thanks for your time

 

 

Posted

There are literally hundreds of threads in this forum where this is discussed because people do it wrong.

Use Minecraft.getInstance() on the client then get what state you want.

But you should put all client code in a separate class otherwise you are very likely going to crash the game on a dedicated server.

https://docs.minecraftforge.net/en/latest/networking/simpleimpl/#handling-packets

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted (edited)
3 hours ago, warjort said:

There are literally hundreds of threads in this forum where this is discussed because people do it wrong.

Use Minecraft.getInstance() on the client then get what state you want.

But you should put all client code in a separate class otherwise you are very likely going to crash the game on a dedicated server.

https://docs.minecraftforge.net/en/latest/networking/simpleimpl/#handling-packets

Quote

No i dont get how to make it work 
public static void handle(MyClientMessage msg, Supplier<NetworkEvent.Context> ctx) {}

this costume class MyClientMessage  what it is what requires,  Kapnejoe tutorial just create a variable class <MSG> but it i make it this way it just red lines in intellij 

public static <MSG> void sendToPlayer(MSG message, ServerPlayer pe) {} <-msg is a generic class


also there's no example on how to the the player instance in the local word 

i need and instance of player to get from it the level and do the stuff 

is there a working example specifically made for 1.19.4 i can use to guide myself ??

Captura-de-pantalla-de-2023-05-28-07-03-

 

 

 

Quote



 

 

Edited by perromercenary00

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



×
×
  • Create New...

Important Information

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