Jump to content

Recommended Posts

Posted

I was trying to add some randomization so my entities would not act in sync.

 

So in the constructor I set some fields to random values using the worldObj.rand from the entity.

 

The entities were not random, so I tested by putting following statement into the constructor:

System.out.println("Rand ="+worldObj.rand.nextInt(6));

 

And as multiple entities were created (from a save game, so all are being constructed in same tick):

[22:10:38] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:initSyncDataCompound:87]: Rand =3
[22:10:38] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:initSyncDataCompound:87]: Rand =3
[22:10:38] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:initSyncDataCompound:87]: Rand =3

 

Basically it looks like the worldObj.rand.nextInt(5) is giving the same value. If I stop and reload the save game, it has same value, and also if I exit completely and run again.

 

However, I get a different value for any I construct later. In other words, all the ones being constructed during loading a game have same value, but ones that get constructed later in the game all have different values.

 

Note that I'm running from Eclipse -- I know some dev environments will fix the seed value to make debugging easier. Is that happening here?

 

Or when a save game is loading, maybe the world's random number generator isn't set up yet?

 

Any other explanation?

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

To analyze why random class gives same value there, it is needed that when and where the Random#nexInt() is called.

So please post the code.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted

That does seem odd, but at the same time it is very common for random number generators to generate the same number multiple times in a row (up to 7 times in a row, actually, according to some article I read somewhere...) - it's a common misconception that random means every number is different.

 

Especially with such a small range (0-4), you are bound to end up with lots of repeats, and Random will always output the same sequence of numbers given the same seed.

 

Whether that means you don't have an issue or not, I don't know, but it is very much within the realm of randomness to get numbers that don't seem quite random.

Posted

I already posted the code.  It is in the entity constructor.

 

it is not just the number happening to be the same due to random chance.  It returns same number no matter how many times it runs.  I've got 5 entities in the save game and they all print same value, over multiple save then load cycles,  including exiting fully then restarting.

 

I'm pretty familiar with random generation in other languages and even with fixed seed I think next int method should retuRN different numbers on each subsequent call.

 

The code can produce different numbers because entities constructed later (with spawn egg) have different values. Only those constructed during loading of saved game have same value.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

I already posted the code.  It is in the entity constructor.

 

it is not just the number happening to be the same due to random chance.  It returns same number no matter how many times it runs.  I've got 5 entities in the save game and they all print same value, over multiple save then load cycles,  including exiting fully then restarting.

 

I'm pretty familiar with random generation in other languages and even with fixed seed I think next int method should retuRN different numbers on each subsequent call.

 

The code can produce different numbers because entities constructed later (with spawn egg) have different values. Only those constructed during loading of saved game have same value.

The whole code in the entity constructor is needed.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted

The world random object has a seed. Which makes it predictable in behaviour.

 

Thats why maps with a seed spawn the same stuff on different computers.

 

If you want more randomness construct your own Random object and use that. That way it operates without the seed constraints.

 

How much wood could a woodchuck chuck if a wood chuck could chuck wood - Guybrush Treepwood

 

I wrote my own mod ish... still a few bugs to fix. http://thaumcraft.duckdns.org/downloads/MagicCookies-1.0.6.4.jar

Posted

The world random object has a seed. Which makes it predictable in behaviour.

 

Thats why maps with a seed spawn the same stuff on different computers.

 

If you want more randomness construct your own Random object and use that. That way it operates without the seed constraints.

You missed the point of this issue.

Randoms with same seed gives same values in same time, but in this case it gives same values for different times!

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted

Is it possible that the entity code is in a different thread?

That way it could be possible they are asking the same random state at the same moment which would result in the same nextInt to be returned in the randomness step.

 

how rand works is

 

Rand(seed) will always return the same next values in order

Nextint 2

Nextint 4

Nextint 7

 

Is it possible that the stepvalue gets reset in the world object or that it gets cloned for the tick event? As not to mess up worldgen?

 

That would also explain the same values because step will always be the same.

 

Future steps will be different because they might happen in seperate ticks.

 

There are multiple possibilities why the random is the same.

How much wood could a woodchuck chuck if a wood chuck could chuck wood - Guybrush Treepwood

 

I wrote my own mod ish... still a few bugs to fix. http://thaumcraft.duckdns.org/downloads/MagicCookies-1.0.6.4.jar

Posted

Is it possible that the entity code is in a different thread?

 

I agree that that is what it seems to act like -- like multiple rands are starting with same seed. But I'm pretty sure Minecraft isn't multi-threaded like that.

 

I don't know if anyone else can replicate the issue. It is pretty simple -- take a custom entity and add line of code to print out random int in constructor, then create multiple entities and save and load the game and see what prints out.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

The whole code in the entity constructor is needed.

 

It is really simply this (other variations yield similar results).

 

public EntityBirdOfPrey(parWorld)
{
    super(parWorld);
    System.out.println("Rand ="+parWorld.rand.nextInt(6));
}

Oh, and how did you created the multiple instances of the EntityBirdOfPrey?

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted

World#rand has NOTHING to with worldgen. The random used for worldgen is in ChunkProviderGenerate and has indeed a fixed seed.

 

Any ideas about my problem though? Even with a fixed seed, my results don't make sense (unless the rand instance is somehow cloned or reset to the seed at the time of each constructor).

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Can you print out the seed before you call nextInt?

 

There is no getSeed() function but I think toString() shows the seed so I printed that out.

 

Here is what my constructor looks like currently:

    public EntityBirdOfPrey(World parWorld)
    {
        super(parWorld);
        
        // DEBUG
        System.out.println("EntityBirdOfPrey constructor(), "+"on Client="
                +parWorld.isRemote+", EntityID = "+getEntityId()+", ModEntityID = "+entityUniqueID);

        setSize(2.0F, 3.0F);
        // DEBUG
        System.out.println("World random seed = "+worldObj.rand.toString());
        randFactor = worldObj.rand.nextInt(10);
        // DEBUG
        System.out.println("randFactor = "+randFactor);
        initSyncDataCompound();
        setupAI();
     }

 

Here is console output for the entities constructed from loading the save game:

[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=false, EntityID = 26, ModEntityID = 9b21931b-67ff-4ae4-be5c-6503abe1c41d
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@5905e924
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 1
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=false, EntityID = 27, ModEntityID = 691ba2b6-afe7-4370-9bd4-3a64a01a861b
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@5905e924
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 1
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=false, EntityID = 39, ModEntityID = cf32a9a0-af33-4e24-a920-15dd1926bfb2
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@5905e924
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 1
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=false, EntityID = 40, ModEntityID = 64fbcf89-c902-4820-af27-9779c9c1dd98
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@5905e924
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 1

 

Possibly relevant is console ouptut for entities construction on the client side (it seems to be properly random):

[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=true, EntityID = 519, ModEntityID = e2af7ddc-47fa-47c9-b345-cad610ed0b2e
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@19d4ee95
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 0
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=true, EntityID = 523, ModEntityID = b5a59dc1-f32e-4235-9171-30d6396a9c82
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@19d4ee95
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 5
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=true, EntityID = 526, ModEntityID = 948012db-6716-41fe-9e91-c120f708f8c4
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@19d4ee95
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 2

 

In both cases (server and client) the seed is constant but only on client does nextInt seem to give random number...

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

That's not the seed, that's the default hashcode for the object ;)

Make a breakpoint and look at the object.

 

Okay, that makes sense that it is the hashcode. Although I think a constant hashcode would indicate that the seed is constant, right?

 

With breakpoint on the line that does next int, the seed for the world rand went like this as it went through the server constructors (these four are being loaded from save game):

 

93667606441033

93667606441033

93667606441033

93667606441033

 

then in the client constructors:

 

139379158755069

195327285708415

89798151390929

154026912586867

 

Then if I create the entity later, on the server seed is:

182302737784538 (now it is different!)

 

And on client:

182192877199158

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Okay, that makes sense that it is the hashcode. Although I think a constant hashcode would indicate that the seed is constant, right?
No, it just means that the object is constant. But objects are mutable.

 

The only place I see that sets the seed for the world Random is some stuff in the structure generation. That shouldn't cause the seed to be constant...

As a workaround you could use the rand field in Entity instead.

 

I ended up just using a new Random since it is a one-off type of thing, but using entity rand is good idea -- I checked and it works properly.

 

I mostly wanted to understand the issue because I feel like it is fairly natural to want to do something random in entity constructor.

 

The last thing that surprises me though is that I thought a set seed makes the sequence predictable but I thought there is still a sequence -- doesn't nextInt() generate different values even if seed is constant? Calling nextInt() several times on the same Random should provide different values even if seed is constant, right? It is really acting like the world rand instance is being re-created with a set seed each time.

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

That's just... mind-boggling :D Do you have a git repo so I could check it out?

 

Sure. https://github.com/jabelar/WildAnimalsPlus-1.7.10

 

The EntityBirdsOfPrey class is the one of interest. I have the code now using the entity rand (i.e. it works), but to see what I was complaining about, change it to worldObj.rand instead.

 

Then you have to create several of the entities in game. There is a spawn egg for "eagle" in the creative tab. The randomness will be okay on initial creation, so then you need to save and load the game. When they are reconstructed that's when the problem occurs.

 

Thanks for looking in to it.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Just tried it. Spawned 3 eagles, quite the game, got back in. randFactor was 2, 2, 4. So... it's random for me.

 

Hmmm, wonder if it is a JVM thing or an Eclipse setting thing? I've got to go out for rest of today, but I'll take a video to show people that I'm not crazy...

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
    • Okay, but does the modpack works with 1.12 or just with 1.12.2, because I need the Forge client specifically for Minecraft 1.12, not 1.12.2
    • Version 1.19 - Forge 41.0.63 I want to create a wolf entity that I can ride, so far it seems to be working, but the problem is that when I get on the wolf, I can’t control it. I then discovered that the issue is that the server doesn’t detect that I’m riding the wolf, so I’m struggling with synchronization. However, it seems to not be working properly. As I understand it, the server receives the packet but doesn’t register it correctly. I’m a bit new to Java, and I’ll try to provide all the relevant code and prints *The comments and prints are translated by chatgpt since they were originally in Spanish* Thank you very much in advance No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. MountableWolfEntity package com.vals.valscraft.entity; import com.vals.valscraft.network.MountSyncPacket; import com.vals.valscraft.network.NetworkHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.animal.Wolf; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.network.PacketDistributor; public class MountableWolfEntity extends Wolf { private boolean hasSaddle; private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(MountableWolfEntity.class, EntityDataSerializers.BYTE); public MountableWolfEntity(EntityType<? extends Wolf> type, Level level) { super(type, level); this.hasSaddle = false; } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_FLAGS, (byte)0); } public static AttributeSupplier.Builder createAttributes() { return Wolf.createAttributes() .add(Attributes.MAX_HEALTH, 20.0) .add(Attributes.MOVEMENT_SPEED, 0.3); } @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { ItemStack itemstack = player.getItemInHand(hand); if (itemstack.getItem() == Items.SADDLE && !this.hasSaddle()) { if (!player.isCreative()) { itemstack.shrink(1); } this.setSaddle(true); return InteractionResult.SUCCESS; } else if (!level.isClientSide && this.hasSaddle()) { player.startRiding(this); MountSyncPacket packet = new MountSyncPacket(true); // 'true' means the player is mounted NetworkHandler.CHANNEL.sendToServer(packet); // Ensure the server handles the packet return InteractionResult.SUCCESS; } return InteractionResult.PASS; } @Override public void travel(Vec3 travelVector) { if (this.isVehicle() && this.getControllingPassenger() instanceof Player) { System.out.println("The wolf has a passenger."); System.out.println("The passenger is a player."); Player player = (Player) this.getControllingPassenger(); // Ensure the player is the controller this.setYRot(player.getYRot()); this.yRotO = this.getYRot(); this.setXRot(player.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = this.getYRot(); this.yHeadRot = this.yBodyRot; float forward = player.zza; float strafe = player.xxa; if (forward <= 0.0F) { forward *= 0.25F; } this.flyingSpeed = this.getSpeed() * 0.1F; this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.5F); this.setDeltaMovement(new Vec3(strafe, travelVector.y, forward).scale(this.getSpeed())); this.calculateEntityAnimation(this, false); } else { // The wolf does not have a passenger or the passenger is not a player System.out.println("No player is mounted, or the passenger is not a player."); super.travel(travelVector); } } public boolean hasSaddle() { return this.hasSaddle; } public void setSaddle(boolean hasSaddle) { this.hasSaddle = hasSaddle; } @Override protected void dropEquipment() { super.dropEquipment(); if (this.hasSaddle()) { this.spawnAtLocation(Items.SADDLE); this.setSaddle(false); } } @SubscribeEvent public static void onServerTick(TickEvent.ServerTickEvent event) { if (event.phase == TickEvent.Phase.START) { MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(); if (server != null) { for (ServerPlayer player : server.getPlayerList().getPlayers()) { if (player.isPassenger() && player.getVehicle() instanceof MountableWolfEntity) { MountableWolfEntity wolf = (MountableWolfEntity) player.getVehicle(); System.out.println("Tick: " + player.getName().getString() + " is correctly mounted on " + wolf); } } } } } private boolean lastMountedState = false; @Override public void tick() { super.tick(); if (!this.level.isClientSide) { // Only on the server boolean isMounted = this.isVehicle() && this.getControllingPassenger() instanceof Player; // Only print if the state changed if (isMounted != lastMountedState) { if (isMounted) { Player player = (Player) this.getControllingPassenger(); // Verify the passenger is a player System.out.println("Server: Player " + player.getName().getString() + " is now mounted."); } else { System.out.println("Server: The wolf no longer has a passenger."); } lastMountedState = isMounted; } } } @Override public void addPassenger(Entity passenger) { super.addPassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(true)); } } } @Override public void removePassenger(Entity passenger) { super.removePassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is no longer mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(false)); } } } @Override public boolean isControlledByLocalInstance() { Entity entity = this.getControllingPassenger(); return entity instanceof Player; } @Override public void positionRider(Entity passenger) { if (this.hasPassenger(passenger)) { double xOffset = Math.cos(Math.toRadians(this.getYRot() + 90)) * 0.4; double zOffset = Math.sin(Math.toRadians(this.getYRot() + 90)) * 0.4; passenger.setPos(this.getX() + xOffset, this.getY() + this.getPassengersRidingOffset() + passenger.getMyRidingOffset(), this.getZ() + zOffset); } } } MountSyncPacket package com.vals.valscraft.network; import com.vals.valscraft.entity.MountableWolfEntity; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class MountSyncPacket { private final boolean isMounted; public MountSyncPacket(boolean isMounted) { this.isMounted = isMounted; } public void encode(FriendlyByteBuf buffer) { buffer.writeBoolean(isMounted); } public static MountSyncPacket decode(FriendlyByteBuf buffer) { return new MountSyncPacket(buffer.readBoolean()); } public void handle(NetworkEvent.Context context) { context.enqueueWork(() -> { ServerPlayer player = context.getSender(); // Get the player from the context if (player != null) { // Verifies if the player has dismounted if (!isMounted) { Entity vehicle = player.getVehicle(); if (vehicle instanceof MountableWolfEntity wolf) { // Logic to remove the player as a passenger wolf.removePassenger(player); System.out.println("Server: Player " + player.getName().getString() + " is no longer mounted."); } } } }); context.setPacketHandled(true); // Marks the packet as handled } } networkHandler package com.vals.valscraft.network; import com.vals.valscraft.valscraft; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class NetworkHandler { private static final String PROTOCOL_VERSION = "1"; public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel( new ResourceLocation(valscraft.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); public static void init() { int packetId = 0; // Register the mount synchronization packet CHANNEL.registerMessage( packetId++, MountSyncPacket.class, MountSyncPacket::encode, MountSyncPacket::decode, (msg, context) -> msg.handle(context.get()) // Get the context with context.get() ); } }  
  • Topics

×
×
  • Create New...

Important Information

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