Jump to content

[1.10] [DERAILED] Using A Vanilla Item


averysumner

Recommended Posts

So, I want to make slimeballs throwable. I have a class and everything for throwables that works, only problem is I only know how to turn my custom items into throwables. Can someone tell me how to turn a vanilla item into a throwable without breaking other mods?

 

-Avery

There are 10 types of people in this world. Those who know binary and those who don't.

Link to comment
Share on other sites

Subscribe to

PlayerInteractEvent.RightClickItem

and do the following:

  • Check if the item is a slimeball
  • Decrement the stack size
  • If the stack size is 0, set the player's held item to
    null

    and call

    ForgeEventFactory#onPlayerDestroyItem

    to fire

    PlayerDestroyItemEvent

    .

  • Spawn your entity
  • Cancel the event to prevent
    Item#onItemRightClick

    and other handlers from being called

 

Note that even if you cancel the event, processing will still continue to the other hand until this PR is merged and you set the result to

EnumActionResult.SUCCESS

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Still not getting it to work even with everything you said, heres the code:

package com.averysumner.randomstuff;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntitySnowball;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatList;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.world.World;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class ThrowableSlimeball {

@SubscribeEvent
public void playerRightClickSlime(PlayerInteractEvent.RightClickItem e){
	if (e.getEntityPlayer().getHeldItemMainhand().getItem() == Items.SLIME_BALL) {
        ItemStack playerHeld = e.getEntityPlayer().getHeldItemMainhand();
        
		System.out.println("Slime right clicked!");
		if (!e.getEntityPlayer().capabilities.isCreativeMode)
        {
            --e.getEntityPlayer().getHeldItemMainhand().stackSize;
        }
        
		if (e.getEntityPlayer().getHeldItemMainhand().stackSize == 0) {
			playerHeld = null;
			ForgeEventFactory.onPlayerDestroyItem(e.getEntityPlayer(), e.getEntityPlayer().getHeldItemMainhand(), e.getEntityPlayer().getActiveHand());
		}

		e.getWorld().playSound((EntityPlayer)null, e.getEntityPlayer().posX, e.getEntityPlayer().posY, e.getEntityPlayer().posZ, SoundEvents.ENTITY_SLIME_SQUISH, SoundCategory.NEUTRAL, 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));

        if (!e.getWorld().isRemote)
        {
            EntitySnowball entitysnowball = new EntitySnowball(e.getWorld(), e.getEntityPlayer());
            entitysnowball.setHeadingFromThrower(e.getEntityPlayer(), e.getEntityPlayer().rotationPitch, e.getEntityPlayer().rotationYaw, 0.0F, 1.5F, 1.0F);
            e.getWorld().spawnEntityInWorld(entitysnowball);
        }
        e.setCanceled(true);
	}

}
}

Btw, I am just spawning snowballs atm to test it.

There are 10 types of people in this world. Those who know binary and those who don't.

Link to comment
Share on other sites

I got everything working, besides rendering my slimeball entity. When I use "RenderingRegistry.registerEntityRenderingHandler" I get @Deprecated, even if I use the factory version.

There are 10 types of people in this world. Those who know binary and those who don't.

Link to comment
Share on other sites

I got everything working, besides rendering my slimeball entity. When I use "RenderingRegistry.registerEntityRenderingHandler" I get @Deprecated, even if I use the factory version.

Take a look at how my rendering is handled at

https://bitbucket.org/The_Fireplace/fires-random-things/src/master/src/main/java/the_fireplace/frt/client/ClientProxy.java

It isn't a big change from how you're doing it now, it just needs an extra class, the render factory. Unless something has changed again in 1.10, this way of doing it isn't deprecated.

If I helped please press the Thank You button.

 

Check out my mods at http://www.curse.com/users/The_Fireplace/projects

Link to comment
Share on other sites

Everything is working besides the texture. Also, if I get in front of the hitbox, it pushes me forwards as I shoot. Heres the code for my renderer, my render factory, and my entity class, can you tell me what's wrong?: Renderer

package com.averysumner.randomstuff;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.Entity;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class RenderProjectile extends Render {

    protected final Item itr;
    private final RenderItem ri;
    private ResourceLocation itemTexture;

    public RenderProjectile(RenderManager rm, Item projectile) {
        super(rm);
        this.itr = projectile;
        this.ri = Minecraft.getMinecraft().getRenderItem();
    }

    public RenderProjectile(RenderManager rm, Item projectile, ResourceLocation texture) {
        super(rm);
        this.itr = projectile;
        this.ri = Minecraft.getMinecraft().getRenderItem();
        this.itemTexture = texture;
    }

    @Override
    protected ResourceLocation getEntityTexture(Entity entity) {
        if (this.itr == Items.SLIME_BALL)
            return new ResourceLocation("minecraft:items/slimeball");
        else if (this.itemTexture != null)
            return itemTexture;
        else
            return new ResourceLocation("minecraft:items/coal");
    }

    @Override
    public void doRender(Entity entity, double x, double y, double z, float f, float partialTicks) {
        GlStateManager.pushMatrix();
        GlStateManager.translate((float) x, (float) y, (float) z);
        GlStateManager.enableRescaleNormal();
        GlStateManager.scale(0.5F, 0.5F, 0.5F);
        GlStateManager.rotate(-this.renderManager.playerViewY, 0.0F, 1.0F, 0.0F);
        GlStateManager.rotate(this.renderManager.playerViewX, 1.0F, 0.0F, 0.0F);
        this.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
        this.ri.renderItem(new ItemStack(this.itr), ItemCameraTransforms.TransformType.GROUND);
        GlStateManager.disableRescaleNormal();
        GlStateManager.popMatrix();
        super.doRender(entity, x, y, z, f, partialTicks);
    }
}

RenderFactory

package com.averysumner.randomstuff;

import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.client.registry.IRenderFactory;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class ProjectileRenderFactory implements IRenderFactory<EntitySlimeball> {

    private Item projectile;
    private ResourceLocation location;

    public ProjectileRenderFactory(Item item){
        projectile=item;
    }

    public ProjectileRenderFactory(Item item, ResourceLocation loc){
        projectile=item;
        location=loc;
    }

    @Override
    public Render<? super EntitySlimeball> createRenderFor(RenderManager manager) {
        if(location != null)
            return new RenderProjectile(manager, projectile, location);
        else
            return new RenderProjectile(manager, projectile);
    }
}

Entity

package com.averysumner.randomstuff;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.EntityBlaze;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.datafix.DataFixer;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;

public class EntitySlimeball extends EntityThrowable
{
    public EntitySlimeball(World worldIn)
    {
        super(worldIn);
    }

    public EntitySlimeball(World worldIn, EntityLivingBase throwerIn)
    {
        super(worldIn, throwerIn);
    }

    public EntitySlimeball(World worldIn, double x, double y, double z)
    {
        super(worldIn, x, y, z);
    }

    public static void func_189662_a(DataFixer p_189662_0_)
    {
        EntityThrowable.func_189661_a(p_189662_0_, "Slimeball");
    }

    /**
     * Called when this EntityThrowable hits a block or entity.
     */
    protected void onImpact(RayTraceResult result)
    {
        if (result.entityHit != null)
        {
            int i = 0;

            if (result.entityHit instanceof EntityBlaze)
            {
                i = 3;
            }

            result.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), (float)i);
        }

        for (int j = 0; j < 8; ++j)
        {
            this.worldObj.spawnParticle(EnumParticleTypes.SLIME, this.posX, this.posY, this.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
        }

        if (!this.worldObj.isRemote)
        {
            this.setDead();
        }
    }
}

There are 10 types of people in this world. Those who know binary and those who don't.

Link to comment
Share on other sites

Everything is working besides the texture. Also, if I get in front of the hitbox, it pushes me forwards as I shoot. Heres the code for my renderer, my render factory, and my entity class, can you tell me what's wrong?: Renderer

package com.averysumner.randomstuff;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.Entity;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class RenderProjectile extends Render {

    protected final Item itr;
    private final RenderItem ri;
    private ResourceLocation itemTexture;

    public RenderProjectile(RenderManager rm, Item projectile) {
        super(rm);
        this.itr = projectile;
        this.ri = Minecraft.getMinecraft().getRenderItem();
    }

    public RenderProjectile(RenderManager rm, Item projectile, ResourceLocation texture) {
        super(rm);
        this.itr = projectile;
        this.ri = Minecraft.getMinecraft().getRenderItem();
        this.itemTexture = texture;
    }

    @Override
    protected ResourceLocation getEntityTexture(Entity entity) {
        if (this.itr == Items.SLIME_BALL)
            return new ResourceLocation("minecraft:items/slimeball");
        else if (this.itemTexture != null)
            return itemTexture;
        else
            return new ResourceLocation("minecraft:items/coal");
    }

    @Override
    public void doRender(Entity entity, double x, double y, double z, float f, float partialTicks) {
        GlStateManager.pushMatrix();
        GlStateManager.translate((float) x, (float) y, (float) z);
        GlStateManager.enableRescaleNormal();
        GlStateManager.scale(0.5F, 0.5F, 0.5F);
        GlStateManager.rotate(-this.renderManager.playerViewY, 0.0F, 1.0F, 0.0F);
        GlStateManager.rotate(this.renderManager.playerViewX, 1.0F, 0.0F, 0.0F);
        this.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
        this.ri.renderItem(new ItemStack(this.itr), ItemCameraTransforms.TransformType.GROUND);
        GlStateManager.disableRescaleNormal();
        GlStateManager.popMatrix();
        super.doRender(entity, x, y, z, f, partialTicks);
    }
}

RenderFactory

package com.averysumner.randomstuff;

import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.client.registry.IRenderFactory;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class ProjectileRenderFactory implements IRenderFactory<EntitySlimeball> {

    private Item projectile;
    private ResourceLocation location;

    public ProjectileRenderFactory(Item item){
        projectile=item;
    }

    public ProjectileRenderFactory(Item item, ResourceLocation loc){
        projectile=item;
        location=loc;
    }

    @Override
    public Render<? super EntitySlimeball> createRenderFor(RenderManager manager) {
        if(location != null)
            return new RenderProjectile(manager, projectile, location);
        else
            return new RenderProjectile(manager, projectile);
    }
}

Entity

package com.averysumner.randomstuff;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.EntityBlaze;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.datafix.DataFixer;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;

public class EntitySlimeball extends EntityThrowable
{
    public EntitySlimeball(World worldIn)
    {
        super(worldIn);
    }

    public EntitySlimeball(World worldIn, EntityLivingBase throwerIn)
    {
        super(worldIn, throwerIn);
    }

    public EntitySlimeball(World worldIn, double x, double y, double z)
    {
        super(worldIn, x, y, z);
    }

    public static void func_189662_a(DataFixer p_189662_0_)
    {
        EntityThrowable.func_189661_a(p_189662_0_, "Slimeball");
    }

    /**
     * Called when this EntityThrowable hits a block or entity.
     */
    protected void onImpact(RayTraceResult result)
    {
        if (result.entityHit != null)
        {
            int i = 0;

            if (result.entityHit instanceof EntityBlaze)
            {
                i = 3;
            }

            result.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), (float)i);
        }

        for (int j = 0; j < 8; ++j)
        {
            this.worldObj.spawnParticle(EnumParticleTypes.SLIME, this.posX, this.posY, this.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
        }

        if (!this.worldObj.isRemote)
        {
            this.setDead();
        }
    }
}

I need to see the code where you register it to know what the problem is. Also, this code over complicates things a bit, I didn't expect you to make an almost exact copy of the more complex of my two projectile rendering classes. My code there is designed for multiple projectiles to use the same class.

 

Here is a simpler example, which better suits your purpose, that you can use if you want something to copy, paste, and edit, though I strongly suggest you look in to it and make sure you understand what all of it does.

My renderprojectile:

https://bitbucket.org/The_Fireplace/fires-random-things/src/master/src/main/java/the_fireplace/frt/client/renderers/RenderPigderPearl.java

My Render factory:

https://bitbucket.org/The_Fireplace/fires-random-things/src/master/src/main/java/the_fireplace/frt/client/renderers/PigderPearlRenderFactory.java

And how I registered this is on line 31 of the ClientProxy class I linked you to before.

 

Edit: as for the entity pushing you, I am too tired to look in to it right now. I suggest you start a new thread about it, we have already derailed this one enough.

If I helped please press the Thank You button.

 

Check out my mods at http://www.curse.com/users/The_Fireplace/projects

Link to comment
Share on other sites

Entity/Render Registry:

package com.averysumner.randomstuff;

import net.minecraft.client.renderer.entity.RenderSnowball;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.entity.projectile.EntitySnowball;
import net.minecraft.init.Biomes;
import net.minecraft.init.Items;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraftforge.fml.common.registry.EntityRegistry;

public class ModEntities {

public static int id = 0;
public static int index;

public static Entity brownbear;
public static Entity slimeball;

public static void registerEntities()
{
	registerModEntity(EntityBrownBear.class, "brownbear", 0x228717, 0xE2F048);
	registerModProjectile(EntitySlimeball.class, "slimeball");

}

public static void registerRenders() {
	RenderingRegistry.registerEntityRenderingHandler(EntitySlimeball.class, new ProjectileRenderFactory(Items.SLIME_BALL));
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void registerModEntity(Class entityClass, String name, int primaryColor, int secondaryColor)
{
        EntityRegistry.registerModEntity(entityClass, name, ++id, RandomStuff.instance, 80, 3, false);
        EntityRegistry.registerEgg(entityClass, primaryColor, secondaryColor);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.FOREST);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.BIRCH_FOREST);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.BIRCH_FOREST_HILLS);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.EXTREME_HILLS);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.EXTREME_HILLS_EDGE);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.EXTREME_HILLS_WITH_TREES);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.FOREST_HILLS);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.MUTATED_BIRCH_FOREST);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.MUTATED_BIRCH_FOREST_HILLS);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.MUTATED_EXTREME_HILLS);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.MUTATED_EXTREME_HILLS_WITH_TREES);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.MUTATED_FOREST);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.MUTATED_PLAINS);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.MUTATED_REDWOOD_TAIGA);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.MUTATED_REDWOOD_TAIGA_HILLS);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.MUTATED_FOREST);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.PLAINS);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.REDWOOD_TAIGA);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.REDWOOD_TAIGA_HILLS);
        EntityRegistry.addSpawn(entityClass, 6, 1, 5, EnumCreatureType.CREATURE, Biomes.ROOFED_FOREST);
        System.out.println("Registering Entity " + name + " with ID " + id);
}

@SuppressWarnings({ "rawtypes", "unchecked"})
public static void registerModProjectile(Class entityClass, String name){
	EntityRegistry.registerModEntity(entityClass, name, id, RandomStuff.instance, 64, 10, true);
        System.out.println("Registering Projectile " + name + " with ID " + id);
}
}

ClientProxy:

package com.averysumner.randomstuff;

import net.minecraft.util.datafix.DataFixer;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

public class ClientProxy extends CommonProxy {

    @Override
    public void preInit(FMLPreInitializationEvent e) {
        super.preInit(e);
        ModItems.init();
        ModEntities.registerEntities();
        ModEntities.registerRenders();
    }

    @Override
    public void init(FMLInitializationEvent e) {
    	super.init(e);
    }

    @Override
    public void postInit(FMLPostInitializationEvent e) {
        super.postInit(e);
    }
}

There are 10 types of people in this world. Those who know binary and those who don't.

Link to comment
Share on other sites

Hey fireplace, I made it a lot more simple by modifying your simpler render class, but I still get no textures. I posted my registry in an older post.

There are 10 types of people in this world. Those who know binary and those who don't.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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