Jump to content

Particles stop spawning! Server/Client desynch?


Recommended Posts

Posted

So I have my BlockCampfire, together with it's TileEntityCampfire. Which is basically a furnace. But I have added a new heat parameter to the tileentity.

As the vanilla furnace does, my campfire switches between campfireIdle and campfireBurning depending if there is fuel burning inside, with the exception that the campire should remain in the burning state as long as there is any heat left (so it emits light during dark creepy nights). And that works fine.

 

Now I only want it to generate the flame & smoke particles when something (fuel) is burning (furnaceBurnTime > 0). Here is where my problem emerges.

As soon as the burn time is up (the coal lump is used up) it stops spawning the flame particles, EVEN if I have more coal that keeps the campfire going. If I right-click and opens up the GUI it starts spawning again, only to stop again when the burn time is up.

 

The campfire works perfectly (burn, heat, and cook timers, as well as the counters). It's just the damn particles that stops spawning when the currently burning fuel source is consumed. It must be something with the server and the client that goes out of synch or something

 

In BlockCampfire I have moved the spawn particles code to the TileEntityCampfire... (for some reason that I can't remember)

    @SideOnly(Side.CLIENT)
    public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
    {
    	TileEntityCampfire tileentity = (TileEntityCampfire)par1World.getBlockTileEntity(par2, par3, par4);
    	
    	if (tileentity != null && tileentity instanceof TileEntityCampfire)
        {
            tileentity.validate();
            tileentity.generateCampfireFlames(par1World, par2, par3, par4, par5Random);
        }
    }

 

Here are the relevant methods from the TileEntityCampfire class

    public boolean isBurning()
    {
        return this.furnaceBurnTime > 0;
    }

    @SideOnly(Side.CLIENT)
    /** Generate flame animation while fuel is still burning */
    public void generateCampfireFlames(World par1World, int par2, int par3, int par4, Random par5Random)
    {
    	if (this.isBurning())
        {
            float f = (float)par2 + 0.5F;
            float f1 = (float)par3 + 0.0F + par5Random.nextFloat() * 4.0F / 16.0F;
            float f2 = (float)par4 + 0.5F;
            float f3 = par5Random.nextFloat() * 0.6F - 0.3F;
            float f4 = par5Random.nextFloat() * 0.6F - 0.3F;

            par1World.spawnParticle("smoke", (double)(f + f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
            par1World.spawnParticle("flame", (double)(f + f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D); 
        }
    }

 

    /**
     * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
     * ticks and creates a new spawn inside its implementation.
     */
    public void updateEntity()
    {
    	boolean flag1 = false;
    	boolean flag2 = this.currentHeat > 0;
        
        if (this.furnaceBurnTime > 0){
            --this.furnaceBurnTime;
            if(this.currentHeat > this.maxFuelHeat)
            	this.decreaseHeat();
            else
            	this.increaseHeat();
        }
        else{
        	this.decreaseHeat();
        }
        
        if (!this.worldObj.isRemote)
        { // *** isRemote bracket	
        	
        	if (this.furnaceBurnTime == 0 && this.canSmelt())
            {
        		this.maxFuelHeat = getMaxFuelHeat(this.furnaceItemStacks[1]);
        		this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);

                if (this.furnaceBurnTime > 0)
                {
                	flag1 = true;

                	if (this.furnaceItemStacks[1] != null)
                    {
                        --this.furnaceItemStacks[1].stackSize;

                        if (this.furnaceItemStacks[1].stackSize == 0)
                        {
                            this.furnaceItemStacks[1] = this.furnaceItemStacks[1].getItem().getContainerItemStack(furnaceItemStacks[1]);
                        }
                    }
                }
            }
        	
            if (this.canSmelt() && this.currentHeat >= this.getHeatNeeded()) //this.isBurning() && 
            {
                ++this.furnaceCookTime;

                if (this.furnaceCookTime == this.cookSpeed)
                {
                    this.furnaceCookTime = 0;
                    this.smeltItem();
                    flag1 = true;
                }
            }
            else
            {
                this.furnaceCookTime = 0;
            }

            if (flag2 != this.currentHeat > 0)
            {
                flag1 = true;
                BlockCampfire.updateFurnaceBlockState(this.currentHeat > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
            }
        } // *** isRemote ending bracket
    	
        if (flag1)
        {
            this.onInventoryChanged();
        }
        
    }

 

why WHY WHYYYYY does the particles stop spawning!?

 

/Thanks in advance for any help

Posted

Hi

 

A suggestion - try adding System.out.println("{log msg here}"); at key points in your code, eg

    @SideOnly(Side.CLIENT)

    public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)

    {

    TileEntityCampfire tileentity = (TileEntityCampfire)par1World.getBlockTileEntity(par2, par3, par4);

System.out.println("randomDisplayTick");

   

    if (tileentity != null && tileentity instanceof TileEntityCampfire)

        {

System.out.println("tileentity correct");

 

            tileentity.validate();

            tileentity.generateCampfireFlames(par1World, par2, par3, par4, par5Random);

        }

    }

 

That should quickly help you narrow down which part is not working as you expect - eg the randomDisplayTicks stop, your entity disappears, isBurning() becomes false, etc.

 

-TGG

Posted

Thanks for the tip! I'll do some research with that in mind.

 

Are you familiar with the isRemote-check? I know what it does, and why it is used. I'm just not sure where to place it.

 

Have I misplaced it in updateEntity? I've tried to place it in several locations, ending up with different results. Heat and cook timers start bugging out, the progress bars stops and stuff like that.

 

 

Posted

Hi

 

There's a bit more background about the Client-Server division here that might help

http://greyminecraftcoder.blogspot.com/2013/10/the-most-important-minecraft-classes.html

http://greyminecraftcoder.blogspot.com/2013/10/client-server-communication-using.html

http://greyminecraftcoder.blogspot.com.au/2013/10/client-side-class-linkage-map.html

http://greyminecraftcoder.blogspot.com/2013/10/server-side-class-linkage-map.html

 

To be honest I also find it difficult to be sure when I have to do something on one side only.  Some are obvious (such as rendering) but often it's not.    I use vanilla code as a guideline, i.e. I find a block or item or whatever in the vanilla code that does something similar to what I want, and see whether they are bothering with isRemote.  If I'm not sure about a particular method, I'll search for all usages of it in the code and look to see if they're called exclusively from one side or from both.

 

-TGG

 

 

Posted

Haha these charts make me even more confused :D

Updating variables in TileEntities (such as cookTim, burnTime etc) should be done server-side right?

Another thing, what happens in all functions that don't have a .isRemote statement? Is that code executed on both server and client?

Posted

Hi

 

The general rules are-

The server holds the master copy of the world, and things which affect the state of the world (blocks, changes to entities, movement, etc) are nearly always calculated on the server and the results sent to any clients which are affected (nearby).

Things like rendering are usually done on the client only, likewise user interactions.

 

In some cases, the client and the server do the same calculations in parallel to help make the game more responsive, but if there is a clash down the track (eg the position of an entity) then the server takes priority.

 

Some classes are only ever called on the server side (eg the ones in that server-side chart)

Some classes are only ever called on the client side (eg the ones in that client-side chart) - and often the methods are marked as @SideOnly(Side.CLIENT)

 

Some are called in both- if you see a vanilla method with .isRemote() in it, you can be reasonably confident that both sides might call it (otherwise there would be no point in checking it).

 

If the method you're looking at isn't one of the classes in the charts above, and none of the methods have .isRemote() in it, then you usually can't be sure which sides (or both) call it.

 

Perhaps other folks on the forum have a more reliable method of telling.  If I had more technical knowledge I'd run a coverage checker and find out, maybe I'll do that one of these days...

 

-TGG

 

 

 

 

Posted

Thanks for the clarification Ghost. I got the particles to spawn as long as the heat is > 0. And render a fire animation inside the campfire as long as something is burning.

But now the currentItemBurnTime is out of sync :/

The actual time each item is burning seems to be fine... it's just the progressbar is behaving strange. AND, I get not fire animation when I first start cooking something, I must close the GUI and open it again to get the fire to render. My oh my... this is so frustrating!

 

BlockCampfire

 

 

    @SideOnly(Side.CLIENT)
    /** A randomly called display update to be able to add particles or other items for display */
    public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
    {
    	TileEntityCampfire tileentity = (TileEntityCampfire)par1World.getBlockTileEntity(par2, par3, par4);
    	
    	if (tileentity != null && tileentity instanceof TileEntityCampfire)
        {
    		if (tileentity.isBurning() || this.isActive)
            {
                float f = (float)par2 + 0.5F;
                float f1 = (float)par3 + 0.0F + par5Random.nextFloat() * 4.0F / 16.0F;
                float f2 = (float)par4 + 0.5F;
                float f3 = par5Random.nextFloat() * 0.6F - 0.3F;
                float f4 = par5Random.nextFloat() * 0.6F - 0.3F;

                par1World.spawnParticle("smoke", (double)(f + f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
                par1World.spawnParticle("flame", (double)(f + f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D); 
                
                if (tileentity.isBurning() && par5Random.nextInt(10) == 0)
                {
                	f = (float)par2 + 0.5F;  f1 = (float)par3 + 0.0F + par5Random.nextFloat() * 4.0F / 16.0F;
                    f2 = (float)par4 + 0.5F; f3 = par5Random.nextFloat() * 0.6F - 0.2F;
                    f4 = par5Random.nextFloat() * 0.6F - 0.3F;
                    par1World.spawnParticle("largesmoke", (double)(f + f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
                   
                	if(par5Random.nextInt(2) == 0) par1World.playSound((double)((float)par2 + 0.5F), (double)((float)par3 + 0.5F), (double)((float)par4 + 0.5F), "fire.fire", 1.0F + par5Random.nextFloat(), par5Random.nextFloat() * 0.7F + 0.3F, false);
                }
            }
    		
    		tileentity.validate();
        }
    }

 

 

 

TileEntityCampfire

 

 

    public void updateEntity()
    {
    	boolean flag1 = false;
    	boolean flag2 = this.currentHeat > 0;
    	boolean flag3 = false;
        
    	this.fireFrame++;
    	if(this.fireFrame > 31) {this.fireFrame=0;}
    	
        if (this.furnaceBurnTime > 0){
            --this.furnaceBurnTime;
            if(this.currentHeat > this.maxFuelHeat)
            	this.decreaseHeat();
            else
            	this.increaseHeat();
        }
        else{
        	this.decreaseHeat();
        }
        
        if (this.furnaceBurnTime == 0 && this.canSmelt())
        {
        	this.maxFuelHeat = getMaxFuelHeat(this.furnaceItemStacks[1]);
    		this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
    		flag3=true;
        }
        
        if (!this.worldObj.isRemote)
        { // *** isRemote bracket	
        	
        	if (flag3 && this.canSmelt())
            {
                if(flag3)
                {

                	flag1 = true;

                	if (this.furnaceItemStacks[1] != null)
                    {
                        --this.furnaceItemStacks[1].stackSize;

                        if (this.furnaceItemStacks[1].stackSize == 0)
                        {
                            this.furnaceItemStacks[1] = this.furnaceItemStacks[1].getItem().getContainerItemStack(furnaceItemStacks[1]);
                        }
                    }
                }
            }
        	
            if (this.canSmelt() && this.currentHeat >= this.getHeatNeeded())
            {
                ++this.furnaceCookTime;

                if (this.furnaceCookTime == this.cookSpeed)
                {
                    this.furnaceCookTime = 0;
                    this.smeltItem();
                    flag1 = true;
                }
            }
            else
            {
                this.furnaceCookTime = 0;
            }

            if (flag2 != this.currentHeat > 0)
            {
                flag1 = true;
                BlockCampfire.updateFurnaceBlockState(this.currentHeat > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
            }
        } // *** isRemote ending bracket
    	
        if (flag1)
        {
            this.onInventoryChanged();
        }
      
    }

 

 

 

 

 

    /**
     * Returns true if the campfire is currently burning
     */
    public boolean isBurning()
    {
        return this.furnaceBurnTime > 0;
    }

 

 

 

 

 

    @SideOnly(Side.CLIENT)
    /**
     * Returns an integer between 0 and the passed value representing how much burn time is left on the current fuel
     * item, where 0 means that the item is exhausted and the passed value means that the item is fresh
     */
    public int getBurnTimeRemainingScaled(int par1)
    {
        if (this.currentItemBurnTime == 0)
        {
            this.currentItemBurnTime = this.cookSpeed; // cookSpeed = 320 always for campfires
        }

        return this.furnaceBurnTime * par1 / this.currentItemBurnTime;
    }

 

 

 

TileEntityCampfireRenderer

 

 

    @SideOnly(side.CLIENT)
    public void renderTileEntityCampfireAt(TileEntityCampfire entity, double x, double y, double z, float f)
    {
    	Tessellator tessellator = Tessellator.instance;
    	double ff;
    	
    	// Render campfire model
    	GL11.glPushMatrix();
        GL11.glTranslatef((float)x + 0.5F, (float)y + 1.5F, (float)z + 0.5F);
        GL11.glRotatef(180, 0.0F, 0.0F, 1.0F);
    
        this.func_110628_a(texCampfire); // bind texture
        
        GL11.glPushMatrix();
        	this.model.renderModel(0.0625F);
        GL11.glPopMatrix();
        
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        GL11.glPopMatrix();
        
        // Render fire animation
        if(entity.isBurning()){
        GL11.glPushMatrix();
        	
        	ff = (double)entity.getFireFrame();
	        this.func_110628_a(texFireFlame); // bind texture
	        
	        GL11.glPushMatrix();
		        tessellator.startDrawingQuads();
		    	
		        tessellator.addVertexWithUV((double)x+0.2D, (double)y     , (double)z+0.2F, 0 , (ff+1D)*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.2D, (double)y+0.7D, (double)z+0.2F, 0 , ff*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.8D, (double)y+0.7D, (double)z+0.8F, 1D, ff*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.8D, (double)y     , (double)z+0.8F, 1D, (ff+1D)*(1D/32D));
		        
		        tessellator.addVertexWithUV((double)x+0.8D, (double)y     , (double)z+0.8F, 0 , (ff+1D)*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.8D, (double)y+0.7D, (double)z+0.8F, 0 , ff*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.2D, (double)y+0.7D, (double)z+0.2F, 1D, ff*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.2D, (double)y     , (double)z+0.2F, 1D, (ff+1D)*(1D/32D));
		        
		        tessellator.addVertexWithUV((double)x+0.2D, (double)y     , (double)z+0.8F, 0 , (ff+1D)*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.2D, (double)y+0.7D, (double)z+0.8F, 0 , ff*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.8D, (double)y+0.7D, (double)z+0.2F, 1D, ff*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.8D, (double)y     , (double)z+0.2F, 1D, (ff+1D)*(1D/32D));
		        
		        tessellator.addVertexWithUV((double)x+0.8D, (double)y     , (double)z+0.2F, 0 , (ff+1D)*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.8D, (double)y+0.7D, (double)z+0.2F, 0 , ff*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.2D, (double)y+0.7D, (double)z+0.8F, 1D, ff*(1D/32D));
		        tessellator.addVertexWithUV((double)x+0.2D, (double)y     , (double)z+0.8F, 1D, (ff+1D)*(1D/32D));
		        			        
		        tessellator.draw();
	        GL11.glPopMatrix();
	        
	        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        GL11.glPopMatrix();
        }
        
    }

 

 

Posted

Hi

 

yeah it's pretty bad when it just won't work and there aren't any clues why not.  I can't see anything obvious wrong with your code (but then - I've never made a custom furnace so that's not saying much..!)

 

So your campfire is pretty much exactly like the vanilla furnace except it looks different, yeah?  And when you fixed the particles problem, the progressbar counter messed up?

What change did you make that stopped the progressbar from working properly?

 

From memory the vanilla furnace works through a GUIHandler of some sort and there is some fairly complicated synchronisation going on between the client and the server.  Also, the furnace changes its blockID depending on whether it is burning or not.  So if you get really stuck, I would suggest starting by making an exact copy of the vanilla furnace blocks, tileentity, Container, and GUI classes, and tweaking them one bit at a time to add the various effects you want.

 

Sorry I can't be of more help, I haven't fooled with the furnace at all so this is all guesswork for me

 

-TGG

Posted

Although I know there are build-in methods to synch the server with the client, The following solution uses the TileEntity packet. It gets your NBT data of the server, TileEntity#writeToNBT() (assuming you have that implemented), wraps that in a packet, and sends that to the clients which will handle that via TileEntity#readFromNBT(). So any variable you want to have synched with the client you can put in your NBT methods (fortunately these are often the same as the variables you want to save to disk with NBT).

 

Put this in your TileEntity class:

@Override
    public Packet getDescriptionPacket(){
        NBTTagCompound tag = new NBTTagCompound();
        writeToNBT(tag);
        return new Packet132TileEntityData(xCoord, yCoord, zCoord, 0, tag);
    }

@Override
    public void onDataPacket(INetworkManager net, Packet132TileEntityData pkt){
        readFromNBT(pkt.data);
    }

public void sendDescriptionPacket(){
        PacketDispatcher.sendPacketToAllAround(xCoord, yCoord, zCoord, 64D, worldObj.provider.dimensionId, getDescriptionPacket());
    }

 

Now every time you want to send the clients(!) an update, you can invoke sendDescriptionPacket(). Keypoint where you want to do this is when you burn up a new burnable item, i.e. you're setting the burn timer again.

 

Remember that you should only do this when you're sure clients will be desynched, because if you invoke sendDescriptionPacket() unnecessarily often this will affect the network. Also this will send an update packet to every player within 64 blocks of this TE, which is great in this example but may not be in others.

Author of PneumaticCraft, MineChess, Minesweeper Mod and Sokoban Mod. Visit www.minemaarten.com to take a look at them.

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

    • 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() ); } }  
    • Do you use features of inventory profiles next (ipnext) or is there a change without it?
    • Remove rubidium - you are already using embeddium, which is a fork of rubidium
  • Topics

×
×
  • Create New...

Important Information

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