Jump to content

Recommended Posts

Posted

Hi, I made a new block using this tutorial http://www.minecraftforge.net/wiki/Rendering_a_Techne_Model_as_a_Block .The model was a 2x2x2 block. Everything was ok until I placed the block, I can walk throw the block... The bounds are all ok but I don't know why the only part y can't walk throw is the right corner

 

BigBoxBlock

package deantonious.blocks;

import java.util.List;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;


public class BigBoxBlock extends BlockContainer {

        //Treat it like a normal block here. The Block Bounds are a good idea - the first three are X Y and Z of the botton-left corner,
        //And the second three are the top-right corner.
        public BigBoxBlock(int id) {
                super(id, Material.iron);
                this.setCreativeTab(CreativeTabs.tabBlock);
                this.setBlockBounds(0.0F, 0.0F, 0.0F, 2.0F, 2.0F, 2.0F);               
        }
               

        //Make sure you set this as your TileEntity class relevant for the block!
        @Override
        public TileEntity createNewTileEntity(World world) {
                return new BigBoxEntity();
        }
        
        //You don't want the normal render type, or it wont render properly.
        @Override
        public int getRenderType() {
                return -1;
        }
        
        //It's not an opaque cube, so you need this.
        @Override
        public boolean isOpaqueCube() {
                return false;
        }
        
        //It's not a normal block, so you need this too.
        public boolean renderAsNormalBlock() {
                return false;
        }
        
        //This is the icon to use for showing the block in your hand.
        public void registerIcons(IconRegister icon) {
                this.blockIcon = icon.registerIcon("bigbox");
        }

}

 

BigBoxEntity

package deantonious.blocks;

import net.minecraft.tileentity.TileEntity;

public class BigBoxEntity extends TileEntity {

}

 

BigBoxModel

package deantonious.blocks;

import net.minecraft.client.model.ModelBase;
import net.minecraft.client.model.ModelRenderer;
import net.minecraft.entity.Entity;

public class BigBoxModel extends ModelBase
{
  //fields
    ModelRenderer Shape1;
  
  public BigBoxModel()
  {
    textureWidth = 128;
    textureHeight = 128;
    
      Shape1 = new ModelRenderer(this, 0, 0);
      Shape1.addBox(0F, 0F, 0F, 32, 32, 32);
      Shape1.setRotationPoint(-24F, -8F, -7F);
      Shape1.setTextureSize(128, 128);
      Shape1.mirror = true;
      setRotation(Shape1, 0F, 0F, 0F);
  }
  
  public void render(Entity entity, float f, float f1, float f2, float f3, float f4, float f5)
  {
    super.render(entity, f, f1, f2, f3, f4, f5);
    setRotationAngles(f, f1, f2, f3, f4, f5, entity);
    Shape1.render(f5);
  }
  
  private void setRotation(ModelRenderer model, float x, float y, float z)
  {
    model.rotateAngleX = x;
    model.rotateAngleY = y;
    model.rotateAngleZ = z;
  }
  
  public void setRotationAngles(float f, float f1, float f2, float f3, float f4, float f5, Entity entity)
  {
    super.setRotationAngles(f, f1, f2, f3, f4, f5, entity);
  }

}

 

BigBoxRenderer

package deantonious.blocks;

import org.lwjgl.opengl.GL11;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;

public class BigBoxRenderer extends TileEntitySpecialRenderer {
        
        //The model of your block
        private final BigBoxModel model;
        
        public BigBoxRenderer() {
                this.model = new BigBoxModel();
        }
        
        private void adjustRotatePivotViaMeta(World world, int x, int y, int z) {
                int meta = world.getBlockMetadata(x, y, z);
                GL11.glPushMatrix();
                GL11.glRotatef(meta * (-90), 0.0F, 0.0F, 1.0F);
                GL11.glPopMatrix();
        }
        
        @Override
        public void renderTileEntityAt(TileEntity te, double x, double y, double z, float scale) {
        //The PushMatrix tells the renderer to "start" doing something.
                GL11.glPushMatrix();
        //This is setting the initial location.
                GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F);
        //This is the texture of your block. It's pathed to be the same place as your other blocks here.
       //Use in 1.6.2  this
        bindTexture(new ResourceLocation("textures/blocks/bigboxtext.png")); 
        //the ':' is very important

        //This rotation part is very important! Without it, your model will render upside-down! And for some reason you DO need PushMatrix again!                       
                GL11.glPushMatrix();
                GL11.glRotatef(180F, 0.0F, 0.0F, 1.0F);
        //A reference to your Model file. Again, very important.
                this.model.render((Entity)null, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);
        //Tell it to stop rendering for both the PushMatrix's
                GL11.glPopMatrix();
                GL11.glPopMatrix();
        }

	//Set the lighting stuff, so it changes it's brightness properly.       
        private void adjustLightFixture(World world, int i, int j, int k, Block block) {
                Tessellator tess = Tessellator.instance;
                float brightness = block.getBlockBrightness(world, i, j, k);
                int skyLight = world.getLightBrightnessForSkyBlocks(i, j, k, 0);
                int modulousModifier = skyLight % 65536;
                int divModifier = skyLight / 65536;
                tess.setColorOpaque_F(brightness, brightness, brightness);
                OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit,  (float) modulousModifier,  divModifier);
        }
}

 

Sorry for my english and thanks for any help

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted
    /**
     * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been
     * cleared to be reused)
     */
    public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
    {
    }

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

    /**
     * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been
     * cleared to be reused)
     */
    public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
    {
    }

 

I know that, the problem was how to use it... (Thanks for the reply)

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

Return a new AABB that is the right size.  The collission box, the boundary size, and the MovingObjectPosition collisionRayTrace box are all used for different purposes.

 

You'll need to set all three of them to being your 2x2x2 box.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

AxisAlignedBB.getAABBPool().getAABB(double, double, double, double, double, double)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

The problem is the getCollisionBoundingBoxFromPool() is only called and used when the entity is within the block. You can verify this by adding a print in the getCollisionBoundingBoxFromPool()  method, or placing a breakpoint. So the problem is that the entity isn't realizing that it's colliding with the block when it's outside the normal block bounds. My solution would be to add 'ghost blocks'. Invisible blocks that have a collision box.

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

Posted

The problem is the getCollisionBoundingBoxFromPool() is only called and used when the entity is within the block. You can verify this by adding a print in the getCollisionBoundingBoxFromPool()  method, or placing a breakpoint. So the problem is that the entity isn't realizing that it's colliding with the block when it's outside the normal block bounds. My solution would be to add 'ghost blocks'. Invisible blocks that have a collision box.

I know what you mean, but I don't know how to make it... I've looked for it, but no result

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

Well, let me help you then.

 

I think you should know how to create a block that's invisible. So I'm assuming you now have a block called GhostBigBoxBlock. When you place down a BigBoxBlock, you want to place down 7 GhostBigBoxBlocks as well. So in your BigBoxBlock you can override onBlockAdded() :

public void onBlockAdded(World world, int x, int y, int z)
    {
     world.setBlock(x + 1, y      , z      , GhostBigBoxBlock.blockID, 0, 3);
     world.setBlock(x      , y      , z + 1, GhostBigBoxBlock.blockID, 1, 3);
     world.setBlock(x + 1, y      , z + 1, GhostBigBoxBlock.blockID, 2, 3);
     world.setBlock(x      , y + 1, z      , GhostBigBoxBlock.blockID, 3, 3);
     world.setBlock(x + 1, y + 1, z      , GhostBigBoxBlock.blockID, 4, 3);
     world.setBlock(x      , y + 1, z + 1, GhostBigBoxBlock.blockID, 5, 3);
     world.setBlock(x + 1, y + 1, z + 1, GhostBigBoxBlock.blockID, 6, 3);
}

As you can see, for every one of the ghost blocks added, I set a different metadata. This we can use in the next part. The next part is the breaking of the block. When you break any of the ghost blocks (or BigBoxBlock) you need to remove every other ghost block as well. And we use the metadata here for the GhostBigBoxBlocks to let them know in which position they are in relation to the BigBoxBlock. And if one of the ghost blocks get broken, we remove the BigBoxBlock which then will remove every GhostBigBoxBlock on his part. To do this override breakBlock() of the GhostBigBoxBlock :

public void breakBlock(World world, int x, int y, int z, int blockID, int metadata)
    {
         super.breakBlock(world, x, y, z, blockID, metadata);
         switch(metadata){
             case 0:
                  world.setBlockToAir(x - 1, y, z);
                  break;
             case 1:
                  world.setBlockToAir(x, y, z - 1);
                  break;
             //TODO do this for every of the 7 cases
         }
    }

So this will break the BigBoxBlock when one of the Ghost Blocks get broken. Now to break every Ghost block override breakBlock in BigBoxBlock:

public void breakBlock(World world, int x, int y, int z, int blockID, int metadata)
    {
         super.breakBlock(world, x, y, z, blockID, metadata);
         for(int i = x; i < x + 2; i++) for(int j = y; j < y + 2; j++) for(int k = z; k < z + 2; k++) world.setBlockToAir(i, j, k);
    }

 

That's how I would do it, if others have better solutions I'm happy to hear them :).

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

Posted

Well, let me help you then.

 

I think you should know how to create a block that's invisible. So I'm assuming you now have a block called GhostBigBoxBlock. When you place down a BigBoxBlock, you want to place down 7 GhostBigBoxBlocks as well. So in your BigBoxBlock you can override onBlockAdded() :

public void onBlockAdded(World world, int x, int y, int z)
    {
     world.setBlock(x + 1, y      , z      , GhostBigBoxBlock.blockID, 0, 3);
     world.setBlock(x      , y      , z + 1, GhostBigBoxBlock.blockID, 1, 3);
     world.setBlock(x + 1, y      , z + 1, GhostBigBoxBlock.blockID, 2, 3);
     world.setBlock(x      , y + 1, z      , GhostBigBoxBlock.blockID, 3, 3);
     world.setBlock(x + 1, y + 1, z      , GhostBigBoxBlock.blockID, 4, 3);
     world.setBlock(x      , y + 1, z + 1, GhostBigBoxBlock.blockID, 5, 3);
     world.setBlock(x + 1, y + 1, z + 1, GhostBigBoxBlock.blockID, 6, 3);
}

As you can see, for every one of the ghost blocks added, I set a different metadata. This we can use in the next part. The next part is the breaking of the block. When you break any of the ghost blocks (or BigBoxBlock) you need to remove every other ghost block as well. And we use the metadata here for the GhostBigBoxBlocks to let them know in which position they are in relation to the BigBoxBlock. And if one of the ghost blocks get broken, we remove the BigBoxBlock which then will remove every GhostBigBoxBlock on his part. To do this override breakBlock() of the GhostBigBoxBlock :

public void breakBlock(World world, int x, int y, int z, int blockID, int metadata)
    {
         super.breakBlock(world, x, y, z, blockID, metadata);
         switch(metadata){
             case 0:
                  world.setBlockToAir(x - 1, y, z);
                  break;
             case 1:
                  world.setBlockToAir(x, y, z - 1);
                  break;
             //TODO do this for every of the 7 cases
         }
    }

So this will break the BigBoxBlock when one of the Ghost Blocks get broken. Now to break every Ghost block override breakBlock in BigBoxBlock:

public void breakBlock(World world, int x, int y, int z, int blockID, int metadata)
    {
         super.breakBlock(world, x, y, z, blockID, metadata);
         for(int i = x; i < x + 2; i++) for(int j = y; j < y + 2; j++) for(int k = z; k < z + 2; k++) world.setBlockToAir(i, j, k);
    }

 

That's how I would do it, if others have better solutions I'm happy to hear them :).

 

Thanks, it really helped me. The only thing I don't understand is the way of removing the hole block with one click. I don't know how to make the other cases...

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

The way I would do it is to look at BlockHalfSlab, and use the methods that make it's shape smaller. I think its


public BlockHalfSlab(int par1, boolean par2, Material par3Material)
    {
        super(par1, par3Material);
        this.isDoubleSlab = par2;


        if (par2)
        {
            opaqueCubeLookup[par1] = true;
        }
        else
        {
            this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.5F, 1.0F);
        }


        this.setLightOpacity(255);
    }


    /**
     * Updates the blocks bounds based on its current state. Args: world, x, y, z
     */
    public void setBlockBoundsBasedOnState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
    {
        if (this.isDoubleSlab)
        {
            this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
        }
        else
        {
            boolean flag = (par1IBlockAccess.getBlockMetadata(par2, par3, par4) & != 0;


            if (flag)
            {
                this.setBlockBounds(0.0F, 0.5F, 0.0F, 1.0F, 1.0F, 1.0F);
            }
            else
            {
                this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.5F, 1.0F);
            }
        }
    }

/**
     * Sets the block's bounds for rendering it as an item
     */
    public void setBlockBoundsForItemRender()
    {
        if (this.isDoubleSlab)
        {
            this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
        }
        else
        {
            this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.5F, 1.0F);
        }
    }


    /**
     * Adds all intersecting collision boxes to a list. (Be sure to only add boxes to the list if they intersect the
     * mask.) Parameters: World, X, Y, Z, mask, list, colliding entity
     */
    public void addCollisionBoxesToList(World par1World, int par2, int par3, int par4, AxisAlignedBB par5AxisAlignedBB, List par6List, Entity par7Entity)
    {
        this.setBlockBoundsBasedOnState(par1World, par2, par3, par4);
        super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    }


    /**
     * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
     * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
     */
    public boolean isOpaqueCube()
    {
        return this.isDoubleSlab;
    }

My mod was a while ago, so I don't exactly remember, but playing around with this and looking at the methods and their arguments will yield results. Anyway, that's what I used for my mod, to make a block a little smaller than a half slab. You could also look at the enchantment table, it's less that a full block. Or BlockSkull, since it's both lower and smaller than a normal block. Good luck with it! Hope I was some help.

Posted

Problem here is that his block is bigger than a block. And then you have that problem that the entity doesn't even check if it's colliding a block if it isn't in the block.

 

I won't just give the other cases of the switch, as I want you to understand my thinking. What I'm doing here is that I'm using metadata to let the ghost block know in what position the ghost block is in relation to the base block. If you look at what happens when the metadata is 0:

 world.setBlock(x + 1, y      , z      , GhostBigBoxBlock.blockID, 0, 3);//The block is placed 1 further on the x-axis than the base block

So the ghost block now 'knows' that if it has metadata 0 it is placed 1 block further on the x-axis than the base block. So the base block is at x - 1, y, z. So when we break the ghost block, we break the base block as well by doing:

world.setBlockToAir(x - 1, y, z);

I hope you understand now.

 

This solution isn't finished yet. You now could replace blocks in the world by placing a BigBoxBlock next to it. You can prevent this from happening, and you could look at BlockCactus.java how it's done for the cactus. Look at the canPlaceBlockAt() method.

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

Posted

I fixed it! Code for checking if there is any block near the block:

 public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
        {
            return !super.canPlaceBlockAt(par1World, par2, par3, par4) ? false : this.canBlockStay(par1World, par2, par3, par4);
        }

        public boolean canBlockStay(World par1World, int par2, int par3, int par4)
        {
           
            if (par1World.getBlockMaterial(par2 + 1, par3, par4).isSolid())
            {
                return false;
            }
            
            else if (par1World.getBlockMaterial(par2, par3, par4 + 1).isSolid())
            {
                return false;
            }
            
            else if (par1World.getBlockMaterial(par2 +1 , par3, par4 + 1).isSolid())
            {
                return false;
            }
            else
            {
                int l = par1World.getBlockId(par2, par3 - 1, par4);
                return true;
            }
        }

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

You're nearly there yes!:) You've only covered 3/7 cases though! You forgot to check all the positions on y+1 ;).

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

Posted

You're nearly there yes!:) You've only covered 3/7 cases though! You forgot to check all the positions on y+1 ;).

 

This was the one of my 2x2x1 block xD. The right one is this:

public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
        {
            return !super.canPlaceBlockAt(par1World, par2, par3, par4) ? false : this.canBlockStay(par1World, par2, par3, par4);
        }

        public boolean canBlockStay(World par1World, int par2, int par3, int par4)
        {
           
            if (par1World.getBlockMaterial(par2 + 1, par3, par4).isSolid())
            {
                return false;
            }
            
            else if (par1World.getBlockMaterial(par2, par3, par4 + 1).isSolid())
            {
                return false;
            }
            
            else if (par1World.getBlockMaterial(par2 +1 , par3, par4 + 1).isSolid())
            {
                return false;
            }
            
            else if (par1World.getBlockMaterial(par2 + 1, par3 + 1, par4).isSolid())
            {
                return false;
            }
            
            else if (par1World.getBlockMaterial(par2, par3 + 1, par4 + 1).isSolid())
            {
                return false;
            }
            
            else if (par1World.getBlockMaterial(par2 + 1, par3 + 1, par4 + 1).isSolid())
            {
                return false;
            }
            
            else if (par1World.getBlockMaterial(par2, par3 + 1, par4).isSolid())
            {
                return false;
            }
            
            else
            {
                int l = par1World.getBlockId(par2, par3 - 1, par4);
                return true;
            }
        }

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

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.