Jump to content

[1.11.2] Entity doesn't spawn on client


kedlub

Recommended Posts

Hi
I'm making a block physics mod and my Physics Block entity doesn't spawn on the client when I spawn it using world.spawnEntity(), but when I tried it using /summon it spawned
I don't know what is wrong, searching on Google didn't help me

I spawn it using this:
 

Spoiler

@SubscribeEvent
    public void onExplosion(ExplosionEvent.Detonate e) {
        PhysicsMod.ready += 5;
        List<BlockPos> pos = e.getAffectedBlocks();
        World worldObj = e.getWorld();

        for(Iterator< BlockPos> i = pos.iterator(); i.hasNext();){
            BlockPos block = i.next();
            IBlockState blockState = worldObj.getBlockState(block);

            if(blockState.isFullBlock() && !worldObj.isRemote) {
                EntityPhysicsBlock pb = new EntityPhysicsBlock(worldObj, (float) block.getX(), (float) block.getY(), (float) block.getZ(), 0, new Random().nextInt(30), 0, blockState);
                pb.forceSpawn = true;
                worldObj.spawnEntity(pb);
            }
        }

        System.out.println("Rigidbody count: " + PhysicsMod.blocks.size());
    }
 
Spoiler

 

 

And this is my entity class:
 

Spoiler

package com.github.kedlub.physics.entity;

import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.collision.shapes.SphereShape;
import com.bulletphysics.demos.applet.Sphere;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.MotionState;
import com.bulletphysics.linearmath.Transform;
import com.github.kedlub.physics.PhysicsMod;
import com.github.kedlub.physics.model.ModelCube;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.BlockStateBase;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityFallingBlock;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Rotations;
import net.minecraft.world.World;
import net.minecraftforge.fml.client.registry.IRenderFactory;
import net.minecraftforge.fml.common.Optional;
import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Quaternion;

import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;

import static com.github.kedlub.physics.PhysicsMod.blocks;

/**
 * Created by Kubik on 08.06.2017.
 */
public class EntityPhysicsBlock extends Entity implements IEntityAdditionalSpawnData {

    public static final Block DEFAULT_BLOCK = Blocks.STONE;

    //private static final DataParameter<BlockPos> VELOCITY = EntityDataManager.createKey(EntityPhysicsBlock.class, DataSerializers.BLOCK_POS);
    protected static final DataParameter<BlockPos> ORIGIN = EntityDataManager.<BlockPos>createKey(EntityPhysicsBlock.class, DataSerializers.BLOCK_POS);
    protected static final DataParameter<Rotations> ROTATION = EntityDataManager.createKey(EntityPhysicsBlock.class, DataSerializers.ROTATIONS);
    protected static final DataParameter<Float> ROTATION_W = EntityDataManager.createKey(EntityPhysicsBlock.class, DataSerializers.FLOAT);
    protected static final DataParameter<Float> BLOCKID = EntityDataManager.createKey(EntityPhysicsBlock.class, DataSerializers.FLOAT);
    public RigidBody rigidBody;
    public int number;
    public Transform xform = new Transform();
    public ModelCube model;
    public Vector3f velocity = new Vector3f();
    public IBlockState block = DEFAULT_BLOCK.getDefaultState();
    float defaultX, defaultY, defaultZ;
    float defaultVelX, defaultVelY, defaultVelZ;


    public EntityPhysicsBlock(World worldIn) {
        super(worldIn);
    }

    public EntityPhysicsBlock(World worldIn, float x, float y, float z, float velX, float velY, float velZ, IBlockState block1) {
        super(worldIn);
        block = block1;
        defaultX = x;
        defaultY = y;
        defaultZ = z;
        defaultVelX = velX;
        defaultVelY = velY;
        defaultVelZ = velZ;
    }

    @SideOnly(Side.CLIENT)
    public World getWorldObj()
    {
        return this.world;
    }

    public void setOrigin(BlockPos p_184530_1_)
    {
        this.dataManager.set(ORIGIN, p_184530_1_);
    }

    @SideOnly(Side.CLIENT)
    public BlockPos getOrigin()
    {
        return (BlockPos)this.dataManager.get(ORIGIN);
    }

    public void setRotation(Quaternion quat)
    {
        Rotations rot = new Rotations(quat.x,quat.y,quat.z);
        this.dataManager.set(ROTATION, rot);
        this.dataManager.set(ROTATION_W, quat.w);
    }

    @SideOnly(Side.CLIENT)
    public Quaternion getRotation() {
        Rotations rot = this.dataManager.get(ROTATION);
        float rot_w = this.dataManager.get(ROTATION_W);
        Quaternion quat = new Quaternion(rot.getX(),rot.getY(),rot.getZ(),rot_w);

        return quat;
    }

    /**
     * Returns true if other Entities should be prevented from moving through this Entity.
     */
    public boolean canBeCollidedWith()
    {
        return !this.isDead;
    }

    int spawnTick = 0;

    @Override
    public void onUpdate() {
        super.onUpdate();
        if(world.isRemote) { //I don't know if onUpdate() gets called only server-side, so I check for this here
            if (PhysicsMod.ready != 0 || PhysicsMod.paused) return;
            //System.out.println("onUpdate() ");

            if (this.block == null || this.block.getMaterial() == Material.AIR) {
                //System.out.println("block is null! for entity " + entityUniqueID);
                if (spawnTick < 100) {
                    spawnTick += 1;
                } else {
                    this.setDead();
                }
                return;
            }
            //System.out.println("Previous position:" + posX + " " + posY + " " + posZ);
            if(rigidBody != null && xform != null) {
                this.rigidBody.getCenterOfMassTransform(this.xform);
                //this.posX = xform.origin.x;
                //this.posY = xform.origin.y;
                //this.posZ = xform.origin.z;
                this.setPosition(xform.origin.x, xform.origin.y, xform.origin.z);
                this.dataManager.set(BLOCKID, (float) Block.getStateId(block));

                this.rigidBody.getLinearVelocity(this.velocity);

                Quat4f quat4f = new Quat4f();
                if (xform != null) {
                    this.xform.getRotation(quat4f);
                }

                setRotation(new Quaternion(quat4f.x, quat4f.y, quat4f.z, quat4f.w));
            }
        }
        else {
            block = Block.getStateById((int)((float)this.dataManager.get(BLOCKID)));
        }



        //System.out.println("New position:" + posX + " " + posY + " " + posZ);
    }


    protected void entityInit()
    {
        if(!world.isRemote) return;
        BoxShape localBoxShape = PhysicsMod.cubeSize;
        Vector3f localVector3f = new Vector3f(0.5f,0.5f,0.5f);
        localBoxShape.calculateLocalInertia(20,localVector3f);

        this.xform = new Transform();
        this.xform.setIdentity();

        MotionState motion = new DefaultMotionState(this.xform);
        RigidBodyConstructionInfo rbinfo = new RigidBodyConstructionInfo(20.0F, motion, localBoxShape, localVector3f);
        rigidBody = new RigidBody(rbinfo);
        this.rigidBody.setRestitution(0.01F);
        this.rigidBody.setFriction(0.8F);
        this.rigidBody.setDamping(0.4F, 0.4F);

        PhysicsMod.instance.dynamicsWorld.addRigidBody(this.rigidBody);

        blocks.add(this);


        this.setOrigin(new BlockPos(this));

        this.velocity = new Vector3f(0.0F, 0.0F, 0.0F);

        this.xform.origin.set(defaultX,defaultY,defaultZ);

        this.velocity.x = ((float)defaultVelX);
        this.velocity.y = ((float)defaultVelY);
        this.velocity.z = ((float)defaultVelZ);

        this.rigidBody.setCenterOfMassTransform(xform);
        this.rigidBody.setLinearVelocity(this.velocity);
        this.rigidBody.activate();

        this.dataManager.register(ORIGIN, BlockPos.ORIGIN);
        Quat4f quat4f = new Quat4f();
        if(xform != null) {
            this.xform.getRotation(quat4f);
        }
        this.dataManager.register(ROTATION, new Rotations(quat4f.x,quat4f.y,quat4f.z));
        this.dataManager.register(ROTATION_W, quat4f.w);
        this.dataManager.register(BLOCKID, 1f);
        this.setSize(0.98F, 0.98F);
        //this.dataManager.register(VELOCITY, new BlockPos(velocity.x,velocity.y,velocity.z));
    }

    @Override
    protected void kill() {
        this.setDead();
    }

    @Override
    public void setDead() {
        if(this.rigidBody != null) {
            PhysicsMod.instance.dynamicsWorld.removeRigidBody(this.rigidBody);
        }
        if(blocks.contains(this)) {
            blocks.remove(this);
        }
        this.isDead = true;
    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    public void writeEntityToNBT(NBTTagCompound compound)
    {
      
    }

    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    public void readEntityFromNBT(NBTTagCompound compound)
    {
      
    }

    // It also seems that writeSpawnData doesn't get called
    @Override
    public void writeSpawnData(ByteBuf buffer) {
        if(this.block != null) {
            buffer.writeInt(Block.getStateId(this.block));
        }
        System.out.println("Sending block spawnData");
    }

    // And readSpawnData doesn't get called too
    @Override
    public void readSpawnData(ByteBuf additionalData) {
        System.out.println("Received block spawnData"); //line gets printed only with /summon
        block = Block.getStateById(additionalData.readInt());
        System.out.println("Block is " + block.getBlock().getLocalizedName());
        this.xform.origin.set((float)posX,(float)posY,(float)posZ);
        this.setSize(0.98F, 0.98F);
    }
}
 

 


Github repository: https://github.com/Kedlub/minecraft-physics

Edited by kedlub
Link to comment
Share on other sites

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

    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
  • Topics

×
×
  • Create New...

Important Information

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