Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Model Renderer from JSON [1.16.5]


Recommended Posts

Posted (edited)

Hi!
I want to render an entity with a json file, but when i spawn the entity it is like invisible.

The Main File: https://pastebin.com/Asa5Tymq

The Renderer Classes: https://pastebin.com/Vag0YmDZhttps://pastebin.com/iD14HzvHhttps://pastebin.com/beV8gxCc

Where i spawn The Entity:

@Override
    public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
        if (world instanceof ServerWorld) {
            int ammo = this.getAmmo(player);
            if (ammo > 0) {
                //this.removeAmmo(player);
                BulletEntity bullet = new BulletEntity(world, player);
                bullet.setBaseDamage(this.getDamage());
                bullet.setOwner(player);
                bullet.setPos(player.getX(), player.getY(), player.getZ());
                bullet.setDeltaMovement(1, 1, 1);
                world.addFreshEntity(bullet);
                return ActionResult.consume(player.inventory.getSelected());
            }
        }
        return super.use(world, player, hand);
    }

The Item associated with the entity (the bullet) has no particular code, and the model shows fine.

Edited by Yurim64
Link to post
Share on other sites
Posted (edited)

Ok so, after a couple of hours of debugging, i found out that the IBakedModel of the Bullet is null.

This is what i do for adding the model:

@SubscribeEvent
    public static void onModelRegister(ModelRegistryEvent event) {
        System.out.println("Register model");
        ModelLoader.addSpecialModel(new ResourceLocation(GunMod.MODID, "item/bullet"));
    }

The method is called correctly, and there is no error when i add the Special Model. but then it shows to be null.
Anyone can help me?

 

I also changed the Registry Type of my item in this way:
 

private void setupClient(FMLClientSetupEvent event) {
        RenderingRegistry.registerEntityRenderingHandler(ModEntities.BULLET.get(), manager -> new SpriteRenderer<>(manager, Minecraft.getInstance().getItemRenderer()));
    }

I don't know if this is the correct way

Edited by Yurim64
Link to post
Share on other sites

So, why are you trying to make the bullet json separate from the item itself? You seem to be under the assumption that #addSpecialModel attaches the model to your item. It just attaches the model to general ModelManager. However, this seems completely unnecessary as you could just specify this as the item model.

Link to post
Share on other sites

The bullet is like an Arrow.
I have a BulletEntity and a BulletItem
The Bullet item shows perfectly, and has no problem to load the model from the json.
But the BulletEntity is invisible, I don't know how to associate the item model with the entity.

I tried to use the SpriteRenderer, but the Entity is still insisible as it has no model.

What can i do?

Link to post
Share on other sites
32 minutes ago, Yurim64 said:

But the BulletEntity is invisible, I don't know how to associate the item model with the entity.

I just told you there is no association. The item model manager has no concept of any models that are not part of an item. If you want to add a special model, you have to pull it directly from the ModelManager and not from the ItemModelMesher.

33 minutes ago, Yurim64 said:

The bullet is like an Arrow.

This doesn't explain why it can't be part of the item. You could just use the gui model for the gui transform type and specify the other json mdoel as the first person right/left hand using the perspective model json. The notion of having a completely separate model from the item being thrown imo is outdated and should be respectively attached to the item render itself.

Link to post
Share on other sites
Posted (edited)

The only thing I can tell you is that I don't understand why if I do this:

private void setupClient(FMLClientSetupEvent event) {
        RenderingRegistry.registerEntityRenderingHandler(ModEntities.BULLET.get(), manager -> new BulletRender<>(manager));
    }

And then I generate my entity, It does not call the render method inside the BulletRenderer class which extends EntityRenderer.
Why? Is there another way? What I miss??

Otherwise the entity works.
I can use it, I can kill the mobs as I have programmed it, but it is as if it did not have a registered renderer. I don't really understand ...

Edited by Yurim64
Link to post
Share on other sites
1 hour ago, Yurim64 said:

Ok, so how can i do that?

Look at a reference of the perspectives test.

58 minutes ago, Yurim64 said:

And then I generate my entity, It does not call the render method inside the BulletRenderer class which extends EntityRenderer.
Why? Is there another way? What I miss??

If that's the case, you're probably not overriding the render method. Put an override annotation on the method. If it fails, find the correct method signature and adjust.

Link to post
Share on other sites

This is the Renderer Class:

public class BulletRender<T extends BulletEntity> extends EntityRenderer<T> {

    public BulletRender(EntityRendererManager manager) {
        super(manager);
    }

    @Override
    public void render(T entity, float f1, float f2, MatrixStack matrix, IRenderTypeBuffer buffer, int light) {
        super.render(entity, f1, f2, matrix, buffer, light);
        System.out.println("Render");
        RenderUtil.renderColoredModel(SpecialModels.BULLET.getModel(), ItemCameraTransforms.TransformType.NONE, false, matrix, buffer, light, OverlayTexture.NO_OVERLAY);
    }

    @Override
    public boolean shouldRender(T p_225626_1_, ClippingHelper p_225626_2_, double p_225626_3_, double p_225626_5_, double p_225626_7_) {
        return true;
    }

    @Override
    public ResourceLocation getTextureLocation(T p_110775_1_) {
        return null;
    }
}

The print is not shown on the console, as if the method is never invoked.

 

Now i try with the perspective, thanks

Link to post
Share on other sites

Ok so, i tried to use the perspctive, but it works only for the item bullet.

Again, the BulletEntity is not rendered, And I don't know what to do or how to do it anymore.

Link to post
Share on other sites

The Item:

public class Bullet extends Item {

    public Bullet() {
        super(new Item.Properties().tab(ItemGroup.TAB_COMBAT));
    }

}

The Entity:

public class BulletEntity extends DamagingProjectileEntity implements IRendersAsItem {

    private static final ItemStack STACK = new ItemStack(ModItems.BULLET.get());

    private int damage;

    public BulletEntity(World world) {
        super(ModEntities.BULLET.get(), world);
    }

    @Override
    public ItemStack getItem() {
        return STACK;
    }

    @Override
    public void setOwner(@Nullable Entity owner) {
        super.setOwner(owner);
    }

    @Override
    protected void onHitBlock(BlockRayTraceResult p_230299_1_) {
        super.onHitBlock(p_230299_1_);
        if (!this.level.isClientSide) {
            this.remove();
        }
    }

    @Override
    protected void onHitEntity(EntityRayTraceResult entityRayTraceResult) {
        super.onHitEntity(entityRayTraceResult);
        if (!this.level.isClientSide) {
            Entity entity = entityRayTraceResult.getEntity();
            DamageSource source = new DamageSource("bullet");
            source = source.setProjectile();
            entity.hurt(source, this.damage);
            this.remove();
        }
    }

    @Override
    public void tick() {
        super.tick();
    }

    @Override
    public void addAdditionalSaveData(CompoundNBT nbt) {
        super.addAdditionalSaveData(nbt);
        nbt.putInt("bulletDamage", this.damage);
    }

    @Override
    public void readAdditionalSaveData(CompoundNBT nbt) {
        super.readAdditionalSaveData(nbt);
        if (nbt.contains("bulletDamage")) {
            this.damage = nbt.getInt("bulletDamage");
        }
    }

    public void setDamage(int damage) {
        this.damage = damage;
    }

    @Override
    public IPacket<?> getAddEntityPacket() {
        return super.getAddEntityPacket();
    }
}

The Entity Renderer:

public class BulletRender<T extends BulletEntity> extends EntityRenderer<T> {

    public BulletRender(EntityRendererManager manager) {
        super(manager);
    }

    @Override
    public void render(T entity, float f1, float f2, MatrixStack matrix, IRenderTypeBuffer buffer, int light) {
        super.render(entity, f1, f2, matrix, buffer, light);
        System.out.println("Render");
        RenderUtil.renderColoredModel(SpecialModels.BULLET.getModel(), ItemCameraTransforms.TransformType.NONE, false, matrix, buffer, light, OverlayTexture.NO_OVERLAY);
    }

    @Override
    public boolean shouldRender(T p_225626_1_, ClippingHelper p_225626_2_, double p_225626_3_, double p_225626_5_, double p_225626_7_) {
        return true;
    }

    @Override
    public ResourceLocation getTextureLocation(T p_110775_1_) {
        return null;
    }
}

Tell me if you need something else.

Link to post
Share on other sites
1 hour ago, Yurim64 said:

@Override public IPacket<?> getAddEntityPacket() { return super.getAddEntityPacket(); }

Aha, this need to return NetworkHooks#getEntitySpawningPacket. Vanilla has hardcoded entries for non living entity types. As such, you need to set it to the custom packet so that the entity will spawn on the client and be rendered.

 

Link to post
Share on other sites
Posted (edited)

I will try to explain it a little bit more verbosely. Basically, your entity is first created on the server and then a message with SSpawnObjectPacket, which is created by Entity#createSpawnPacket, is sent to the client, then magical client-side Mojang algorithms(look at ClientPlayNetHandler#handleSpawnObject), that are hard-coded, just throw your entity away since it's not of vanilla EntityType and don't add it to the ClientWorld lists. So, in effect, you have a strange situation when the entity is operating perfectly fine on the server and client doesn't even know of its existance. The funniest thing about all this is that I was making exactly the same BulletEntity and I spent 2 hours before I found out that the entity is not in the ClientWorld lists.

Edited by BudRunBun
Link to post
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.

Guest
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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.