Jump to content

Recommended Posts

Posted

I created some custom leaves for a tree in my mod. They are working fine, but they are rendering strangely. All my code for the leaves was based directly off the vanilla code. I even went through and changed all my variables to the vanilla equivalents after noticing the issues.  I switched over to vanilla textures as well. I have no clue what is causing this, and am hoping that one of you has experienced and been able to solve this issue already.

For some reason you are able to see through the leaves, kinda like an old school tnt x-ray machine. On the right are normal Oak leaves, the left are my leaves. The opaque textures for fast aren't working as well.

Fast:

gA0SOVM.png

Fancy:

7jRk11a.png

I add the block like this in my main blocks class,

	public static Block WDLeaves = new BlockWDLeaves().setBlockTextureName("wdleaves").setBlockName("wdleaves");
GameRegistry.registerBlock(WDLeaves, ItemWDLeaves.class, Reference.MODID + "_" + WDLeaves.getUnlocalizedName().substring(5));

All of the associated classes.

GenericWDLeaves:

 

package worlddomination.blocks;

 

import java.util.ArrayList;

import java.util.Random;

 

import net.minecraft.block.Block;

import net.minecraft.block.BlockLeavesBase;

import net.minecraft.block.material.Material;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.init.Blocks;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.util.IIcon;

import net.minecraft.world.ColorizerFoliage;

import net.minecraft.world.IBlockAccess;

import net.minecraft.world.World;

import net.minecraftforge.common.IShearable;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

public abstract class GenericWDLeaves extends BlockWDLeavesBase implements IShearable

{

    int[] field_150128_a;

    @SideOnly(Side.CLIENT)

    protected int field_150127_b;

    protected IIcon[][] field_150129_M = new IIcon[2][];

    private static final String __OBFID = "CL_00000263";

 

    public GenericWDLeaves()

    {

        super(Material.leaves, false);

        this.setTickRandomly(true);

        this.setCreativeTab(CreativeTabs.tabDecorations);

        this.setHardness(0.2F);

        this.setLightOpacity(1);

        this.setStepSound(soundTypeGrass);

    }

 

    @SideOnly(Side.CLIENT)

    public int getBlockColor()

    {

        return ColorizerFoliage.getFoliageColorBasic();

    }

 

    /**

    * Returns the color this block should be rendered. Used by leaves.

    */

    @SideOnly(Side.CLIENT)

    public int getRenderColor(int p_149741_1_)

    {

        return ColorizerFoliage.getFoliageColorBasic();

    }

 

    /**

    * Returns a integer with hex for 0xrrggbb with this color multiplied against the blocks color. Note only called

    * when first determining what to render.

    */

    @SideOnly(Side.CLIENT)

    public int colorMultiplier(IBlockAccess p_149720_1_, int p_149720_2_, int p_149720_3_, int p_149720_4_)

    {

        int l = 0;

        int i1 = 0;

        int j1 = 0;

 

        for (int k1 = -1; k1 <= 1; ++k1)

        {

            for (int l1 = -1; l1 <= 1; ++l1)

            {

                int i2 = p_149720_1_.getBiomeGenForCoords(p_149720_2_ + l1, p_149720_4_ + k1).getBiomeFoliageColor(p_149720_2_ + l1, p_149720_3_, p_149720_4_ + k1);

                l += (i2 & 16711680) >> 16;

                i1 += (i2 & 65280) >> 8;

                j1 += i2 & 255;

            }

        }

 

        return (l / 9 & 255) << 16 | (i1 / 9 & 255) << 8 | j1 / 9 & 255;

    }

 

    public void breakBlock(World p_149749_1_, int p_149749_2_, int p_149749_3_, int p_149749_4_, Block p_149749_5_, int p_149749_6_)

    {

        byte b0 = 1;

        int i1 = b0 + 1;

 

        if (p_149749_1_.checkChunksExist(p_149749_2_ - i1, p_149749_3_ - i1, p_149749_4_ - i1, p_149749_2_ + i1, p_149749_3_ + i1, p_149749_4_ + i1))

        {

            for (int j1 = -b0; j1 <= b0; ++j1)

            {

                for (int k1 = -b0; k1 <= b0; ++k1)

                {

                    for (int l1 = -b0; l1 <= b0; ++l1)

                    {

                        Block block = p_149749_1_.getBlock(p_149749_2_ + j1, p_149749_3_ + k1, p_149749_4_ + l1);

                        if (block.isLeaves(p_149749_1_, p_149749_2_ + j1, p_149749_3_ + k1, p_149749_4_ + l1))

                        {

                            block.beginLeavesDecay(p_149749_1_, p_149749_2_ + j1, p_149749_3_ + k1, p_149749_4_ + l1);

                        }

                    }

                }

            }

        }

    }

 

    /**

    * Ticks the block if it's been scheduled

    */

    public void updateTick(World p_149674_1_, int p_149674_2_, int p_149674_3_, int p_149674_4_, Random p_149674_5_)

    {

        if (!p_149674_1_.isRemote)

        {

            int l = p_149674_1_.getBlockMetadata(p_149674_2_, p_149674_3_, p_149674_4_);

 

            if ((l & 8) != 0 && (l & 4) == 0)

            {

                byte b0 = 4;

                int i1 = b0 + 1;

                byte b1 = 32;

                int j1 = b1 * b1;

                int k1 = b1 / 2;

 

                if (this.field_150128_a == null)

                {

                    this.field_150128_a = new int[b1 * b1 * b1];

                }

 

                int l1;

 

                if (p_149674_1_.checkChunksExist(p_149674_2_ - i1, p_149674_3_ - i1, p_149674_4_ - i1, p_149674_2_ + i1, p_149674_3_ + i1, p_149674_4_ + i1))

                {

                    int i2;

                    int j2;

 

                    for (l1 = -b0; l1 <= b0; ++l1)

                    {

                        for (i2 = -b0; i2 <= b0; ++i2)

                        {

                            for (j2 = -b0; j2 <= b0; ++j2)

                            {

                                Block block = p_149674_1_.getBlock(p_149674_2_ + l1, p_149674_3_ + i2, p_149674_4_ + j2);

 

                                if (!block.canSustainLeaves(p_149674_1_, p_149674_2_ + l1, p_149674_3_ + i2, p_149674_4_ + j2))

                                {

                                    if (block.isLeaves(p_149674_1_, p_149674_2_ + l1, p_149674_3_ + i2, p_149674_4_ + j2))

                                    {

                                        this.field_150128_a[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = -2;

                                    }

                                    else

                                    {

                                        this.field_150128_a[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = -1;

                                    }

                                }

                                else

                                {

                                    this.field_150128_a[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = 0;

                                }

                            }

                        }

                    }

 

                    for (l1 = 1; l1 <= 4; ++l1)

                    {

                        for (i2 = -b0; i2 <= b0; ++i2)

                        {

                            for (j2 = -b0; j2 <= b0; ++j2)

                            {

                                for (int k2 = -b0; k2 <= b0; ++k2)

                                {

                                    if (this.field_150128_a[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1] == l1 - 1)

                                    {

                                        if (this.field_150128_a[(i2 + k1 - 1) * j1 + (j2 + k1) * b1 + k2 + k1] == -2)

                                        {

                                            this.field_150128_a[(i2 + k1 - 1) * j1 + (j2 + k1) * b1 + k2 + k1] = l1;

                                        }

 

                                        if (this.field_150128_a[(i2 + k1 + 1) * j1 + (j2 + k1) * b1 + k2 + k1] == -2)

                                        {

                                            this.field_150128_a[(i2 + k1 + 1) * j1 + (j2 + k1) * b1 + k2 + k1] = l1;

                                        }

 

                                        if (this.field_150128_a[(i2 + k1) * j1 + (j2 + k1 - 1) * b1 + k2 + k1] == -2)

                                        {

                                            this.field_150128_a[(i2 + k1) * j1 + (j2 + k1 - 1) * b1 + k2 + k1] = l1;

                                        }

 

                                        if (this.field_150128_a[(i2 + k1) * j1 + (j2 + k1 + 1) * b1 + k2 + k1] == -2)

                                        {

                                            this.field_150128_a[(i2 + k1) * j1 + (j2 + k1 + 1) * b1 + k2 + k1] = l1;

                                        }

 

                                        if (this.field_150128_a[(i2 + k1) * j1 + (j2 + k1) * b1 + (k2 + k1 - 1)] == -2)

                                        {

                                            this.field_150128_a[(i2 + k1) * j1 + (j2 + k1) * b1 + (k2 + k1 - 1)] = l1;

                                        }

 

                                        if (this.field_150128_a[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1 + 1] == -2)

                                        {

                                            this.field_150128_a[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1 + 1] = l1;

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

 

                l1 = this.field_150128_a[k1 * j1 + k1 * b1 + k1];

 

                if (l1 >= 0)

                {

                    p_149674_1_.setBlockMetadataWithNotify(p_149674_2_, p_149674_3_, p_149674_4_, l & -9, 4);

                }

                else

                {

                    this.removeLeaves(p_149674_1_, p_149674_2_, p_149674_3_, p_149674_4_);

                }

            }

        }

    }

 

    /**

    * A randomly called display update to be able to add particles or other items for display

    */

    @SideOnly(Side.CLIENT)

    public void randomDisplayTick(World p_149734_1_, int p_149734_2_, int p_149734_3_, int p_149734_4_, Random p_149734_5_)

    {

        if (p_149734_1_.canLightningStrikeAt(p_149734_2_, p_149734_3_ + 1, p_149734_4_) && !World.doesBlockHaveSolidTopSurface(p_149734_1_, p_149734_2_, p_149734_3_ - 1, p_149734_4_) && p_149734_5_.nextInt(15) == 1)

        {

            double d0 = (double)((float)p_149734_2_ + p_149734_5_.nextFloat());

            double d1 = (double)p_149734_3_ - 0.05D;

            double d2 = (double)((float)p_149734_4_ + p_149734_5_.nextFloat());

            p_149734_1_.spawnParticle("dripWater", d0, d1, d2, 0.0D, 0.0D, 0.0D);

        }

    }

 

    private void removeLeaves(World p_150126_1_, int p_150126_2_, int p_150126_3_, int p_150126_4_)

    {

        this.dropBlockAsItem(p_150126_1_, p_150126_2_, p_150126_3_, p_150126_4_, p_150126_1_.getBlockMetadata(p_150126_2_, p_150126_3_, p_150126_4_), 0);

        p_150126_1_.setBlockToAir(p_150126_2_, p_150126_3_, p_150126_4_);

    }

 

    /**

    * Returns the quantity of items to drop on block destruction.

    */

    public int quantityDropped(Random p_149745_1_)

    {

        return p_149745_1_.nextInt(20) == 0 ? 1 : 0;

    }

 

    public Item getItemDropped(int p_149650_1_, Random p_149650_2_, int p_149650_3_)

    {

        return Item.getItemFromBlock(Blocks.sapling);

    }

 

    /**

    * Drops the block items with a specified chance of dropping the specified items

    */

    public void dropBlockAsItemWithChance(World p_149690_1_, int p_149690_2_, int p_149690_3_, int p_149690_4_, int p_149690_5_, float p_149690_6_, int p_149690_7_)

    {

        if (!p_149690_1_.isRemote)

        {

            int j1 = this.func_150123_b(p_149690_5_);

 

            if (p_149690_7_ > 0)

            {

                j1 -= 2 << p_149690_7_;

 

                if (j1 < 10)

                {

                    j1 = 10;

                }

            }

 

            if (p_149690_1_.rand.nextInt(j1) == 0)

            {

                Item item = this.getItemDropped(p_149690_5_, p_149690_1_.rand, p_149690_7_);

                this.dropBlockAsItem(p_149690_1_, p_149690_2_, p_149690_3_, p_149690_4_, new ItemStack(item, 1, this.damageDropped(p_149690_5_)));

            }

 

            j1 = 200;

 

            if (p_149690_7_ > 0)

            {

                j1 -= 10 << p_149690_7_;

 

                if (j1 < 40)

                {

                    j1 = 40;

                }

            }

 

            this.func_150124_c(p_149690_1_, p_149690_2_, p_149690_3_, p_149690_4_, p_149690_5_, j1);

        }

    }

 

    protected void func_150124_c(World p_150124_1_, int p_150124_2_, int p_150124_3_, int p_150124_4_, int p_150124_5_, int p_150124_6_) {}

 

    protected int func_150123_b(int p_150123_1_)

    {

        return 20;

    }

 

    /**

    * Called when the player destroys a block with an item that can harvest it. (i, j, k) are the coordinates of the

    * block and l is the block's subtype/damage .

    */

    public void harvestBlock(World p_149636_1_, EntityPlayer p_149636_2_, int p_149636_3_, int p_149636_4_, int p_149636_5_, int p_149636_6_)

    {

        {

            super.harvestBlock(p_149636_1_, p_149636_2_, p_149636_3_, p_149636_4_, p_149636_5_, p_149636_6_);

        }

    }

 

    /**

    * Determines the damage on the item the block drops. Used in cloth and wood.

    */

    public int damageDropped(int p_149692_1_)

    {

        return p_149692_1_ & 3;

    }

 

    /**

    * 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.field_150121_P;

    }

 

    /**

    * Gets the block's texture. Args: side, meta

    */

    @SideOnly(Side.CLIENT)

    public abstract IIcon getIcon(int var1, int var2);

 

    /**

    * Pass true to draw this block using fancy graphics, or false for fast graphics.

    */

    @SideOnly(Side.CLIENT)

    public void setGraphicsLevel(boolean p_150122_1_)

    {

        this.field_150121_P = p_150122_1_;

        this.field_150127_b = p_150122_1_ ? 0 : 1;

    }

 

    /**

    * Returns an item stack containing a single instance of the current block type. 'i' is the block's subtype/damage

    * and is ignored for blocks which do not support subtypes. Blocks which cannot be harvested should return null.

    */

    protected ItemStack createStackedBlock(int p_149644_1_)

    {

        return new ItemStack(Item.getItemFromBlock(this), 1, p_149644_1_ & 3);

    }

 

    public abstract String[] func_150125_e();

 

 

    @Override

    public boolean isShearable(ItemStack item, IBlockAccess world, int x, int y, int z)

    {

        return true;

    }

 

    @Override

    public ArrayList<ItemStack> onSheared(ItemStack item, IBlockAccess world, int x, int y, int z, int fortune)

    {

        ArrayList<ItemStack> ret = new ArrayList<ItemStack>();

        ret.add(new ItemStack(this, 1, world.getBlockMetadata(x, y, z) & 3));

        return ret;

    }

 

    @Override

    public void beginLeavesDecay(World world, int x, int y, int z)

    {

 

        int i2 = world.getBlockMetadata(x, y, z);

 

        if ((i2 & 8) == 0)

        {

            world.setBlockMetadataWithNotify(x, y, z, i2 | 8, 4);

        }

        world.setBlockMetadataWithNotify(x, y, z, world.getBlockMetadata(x, y, z) | 8, 4);

    }

 

    @Override

    public boolean isLeaves(IBlockAccess world, int x, int y, int z)

    {

        return true;

    }

}

 

BlockWDLeaves:

 

package worlddomination.blocks;

 

import java.util.List;

 

import worlddomination.core.WorldDominationItem;

import worlddomination.util.Reference;

import net.minecraft.block.BlockLeaves;

import net.minecraft.client.renderer.texture.IIconRegister;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.init.Items;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.util.IIcon;

import net.minecraft.world.World;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

public class BlockWDLeaves extends BlockLeaves

{

    public static final String[][] field_150132_N = new String[][] {{"leaves_rubber"}, {"leaves_rubber_opaque"}};

    public static final String[] field_150133_O = new String[] {"rubber"};

    private static final String __OBFID = "CL_00000276";

 

    protected void func_150124_c(World p_150124_1_, int p_150124_2_, int p_150124_3_, int p_150124_4_, int p_150124_5_, int p_150124_6_)

    {

        if ((p_150124_5_ & 3) == 0 && p_150124_1_.rand.nextInt(p_150124_6_) == 0)

        {

            this.dropBlockAsItem(p_150124_1_, p_150124_2_, p_150124_3_, p_150124_4_, new ItemStack(WorldDominationItem.Sap, 1, 0));

        }

    }

 

    /**

    * Determines the damage on the item the block drops. Used in cloth and wood.

    */

    public int damageDropped(int p_149692_1_)

    {

        return super.damageDropped(p_149692_1_) + 4;

    }

 

    /**

    * Get the block's damage value (for use with pick block).

    */

    public int getDamageValue(World p_149643_1_, int p_149643_2_, int p_149643_3_, int p_149643_4_)

    {

        return p_149643_1_.getBlockMetadata(p_149643_2_, p_149643_3_, p_149643_4_) & 3;

    }

 

    /**

    * Gets the block's texture. Args: side, meta

    */

    @SideOnly(Side.CLIENT)

    public IIcon getIcon(int p_149691_1_, int p_149691_2_)

    {

        return (p_149691_2_ & 3) == 1 ? this.field_150129_M[this.field_150127_b][1] : this.field_150129_M[this.field_150127_b][0];

    }

 

    /**

    * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)

    */

    @SideOnly(Side.CLIENT)

    public void getSubBlocks(Item p_149666_1_, CreativeTabs p_149666_2_, List p_149666_3_)

    {

        p_149666_3_.add(new ItemStack(p_149666_1_, 1, 0));

    }

 

    @SideOnly(Side.CLIENT)

    public void registerBlockIcons(IIconRegister p_149651_1_)

    {

        for (int i = 0; i < field_150132_N.length; ++i)

        {

            this.field_150129_M = new IIcon[field_150132_N.length];

 

            for (int j = 0; j < field_150132_N.length; ++j)

            {

                this.field_150129_M[j] = p_149651_1_.registerIcon(Reference.MODID + ":" + field_150132_N[j]);

            }

        }

    }

 

    public String[] func_150125_e()

    {

        return field_150133_O;

    }

}

 

BlockWDLeavesBase:

 

package worlddomination.blocks;

 

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.block.Block;

import net.minecraft.block.material.Material;

import net.minecraft.world.IBlockAccess;

 

public class BlockWDLeavesBase extends Block

{

    protected boolean field_150121_P;

    private static final String __OBFID = "CL_00000326";

 

    protected BlockWDLeavesBase(Material p_i45433_1_, boolean p_i45433_2_)

    {

        super(p_i45433_1_);

        this.field_150121_P = p_i45433_2_;

    }

 

    /**

    * 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 false;

    }

 

    /**

    * Returns true if the given side of this block type should be rendered, if the adjacent block is at the given

    * coordinates.  Args: blockAccess, x, y, z, side

    */

    @SideOnly(Side.CLIENT)

    public boolean shouldSideBeRendered(IBlockAccess p_149646_1_, int p_149646_2_, int p_149646_3_, int p_149646_4_, int p_149646_5_)

    {

        Block block = p_149646_1_.getBlock(p_149646_2_, p_149646_3_, p_149646_4_);

        return !this.field_150121_P && block == this ? false : super.shouldSideBeRendered(p_149646_1_, p_149646_2_, p_149646_3_, p_149646_4_, p_149646_5_);

    }

}

 

 

ItemWDLeaves

 

package worlddomination.items;

 

import net.minecraft.block.Block;

import net.minecraft.item.ItemBlock;

import net.minecraft.item.ItemStack;

import net.minecraft.util.IIcon;

import worlddomination.blocks.BlockWDLeaves;

import worlddomination.core.WorldDominationBlock;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

public class ItemWDLeaves extends ItemBlock {

 

    private final Block field_150940_b;

    private static final String __OBFID = "CL_00000046";

   

public ItemWDLeaves(Block p_i45344_1_) {

super(p_i45344_1_);

this.field_150940_b = p_i45344_1_;

        this.setMaxDamage(0);

        this.setHasSubtypes(true);

}

    /**

    * Returns the metadata of the block which this Item (ItemBlock) can place

    */

    public int getMetadata(int par1)

    {

        return par1 | 4;

    }

 

    /**

    * Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have

    * different names based on their damage or NBT.

    */

    public String getUnlocalizedName(ItemStack par1ItemStack)

    {

        int i = par1ItemStack.getItemDamage();

 

        if (i < 0 || i >= ((BlockWDLeaves) this.field_150940_b).func_150125_e().length)

        {

            i = 0;

        }

 

        return super.getUnlocalizedName() + "." + ((BlockWDLeaves) this.field_150940_b).func_150125_e();

    }

 

    /**

    * Gets an icon index based on an item's damage value

    */

    @SideOnly(Side.CLIENT)

    public IIcon getIconFromDamage(int par1)

    {

        return this.field_150940_b.getIcon(0, par1);

    }

 

    @SideOnly(Side.CLIENT)

    public int getColorFromItemStack(ItemStack par1ItemStack, int par2)

    {

        return this.field_150940_b.getRenderColor(par1ItemStack.getItemDamage());

    }

}

 

 

Thanks for any help, as always.

Posted

Thanks for the help, i must have forgot to override it. I am however, still having problems trying to get the opaque textures to show when i have the graphics on fast.

Posted

there is a method in the leaves class called setGraphicsLevel(boolean p_150122_1_) that is used to check if fancy graphics in enabled

Ok, so i need to call this function and assign it a value? I tried what minecraft does in there Global Renderer, but im getting all kinds of static issues. How am i supposed to use this function?

Posted

there is a method in the leaves class called setGraphicsLevel(boolean p_150122_1_) that is used to check if fancy graphics in enabled

Hi

 

There were a couple of recent posts in this forum (1-2 weeks ago?) with exactly the same symptoms, you could try a search for those

 

eg

 

http://www.minecraftforge.net/forum/index.php/topic,18775.msg94918.html#msg94918

 

-TGG

Thanks everyone, i was able to solve this issue. if anyone is interested the solution is as easy as putting this.setGraphicsLevel(Minecraft.getMinecraft().gameSettings.fancyGraphics); in your getIcon Method right before you return the icon.

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.