Shoot ranged item in direction procedure

Status
Migrated
Product/platform
Not applicable
MCreator version
Priority
Minor
Issue description

I know it has been wonderful since Mcreator add shoot custom ranged items and Summon mobs with vx/vy/vz. But, can we shoot ranged items with different directions, not just in front of us? Or if this is not possible, why cant we summon custom ranged item bullets with velocity? I mean we can summon many entities, include arrow but I can find custom ranged items' bullet ?

I made a post but no response so I made this.

Issue comments

You could try to create an entity and set its movement according to whatever you want so it will act as projectile

Hello! I am having a relatable problem here, but with, like, a ton of extra difficulty.

You see, i actually managed to make a "Shotgun" Procedure, that shoots, along with the main bullet of the gun, EntitySnowballs. 

Have a look at it:

    Entity entity = (Entity) dependencies.get("entity");
        ItemStack itemstack = (ItemStack) dependencies.get("itemstack");
        double x = dependencies.get("x") instanceof Integer ? (int) dependencies.get("x") : (double) dependencies.get("x");
        double y = dependencies.get("y") instanceof Integer ? (int) dependencies.get("y") : (double) dependencies.get("y");
        double z = dependencies.get("z") instanceof Integer ? (int) dependencies.get("z") : (double) dependencies.get("z");
        IWorld world = (IWorld) dependencies.get("world");
        double ranpitch = 0;
        double ranyaw = 0;
        for (int index0 = 0; index0 < (int) (8); index0++) {
            ranpitch = (double) (Math.round(((Math.random() * 200) - 100)) / 10);
            ranyaw = (double) (Math.round(((Math.random() * 200) - 100)) / 10);
            if (world instanceof World && !world.getWorld().isRemote) {
                Entity entityToSpawn = new SnowballEntity(EntityType.SNOWBALL, world.getWorld());
                entityToSpawn.setLocationAndAngles(x, (y + 1), z, (float) (entity.rotationYaw), (float) (entity.rotationPitch));
                entityToSpawn.setRenderYawOffset((float) (entity.rotationYaw));
                entityToSpawn.setMotion(
                        (1.42 * (Math.cos(Math.toRadians((90 + ((entity.rotationYaw) + (ranyaw)))))
                                * Math.cos(Math.toRadians(Math.abs(((entity.rotationPitch) + (ranpitch))))))),
                        (1.42 * Math.sin(Math.toRadians(((-1) * ((entity.rotationPitch) + (ranpitch)))))),
                        (1.42 * (Math.sin(Math.toRadians((90 + ((entity.rotationYaw) + (ranyaw)))))
                                * Math.cos(Math.toRadians(Math.abs(((entity.rotationPitch) + (ranpitch))))))));
                if (entityToSpawn instanceof MobEntity)
                    ((MobEntity) entityToSpawn).onInitialSpawn(world, world.getDifficultyForLocation(new BlockPos(entityToSpawn)),
                            SpawnReason.MOB_SUMMONED, (ILivingEntityData) null, (CompoundNBT) null);
                world.addEntity(entityToSpawn);
            }
        }
But spawn snowballs, and that it is.

I cannot edit its textures or its damage. I **NEED** a way to customize or create a sort of entity like that, like the bullets that i fire from a normal ranged item, but with the "Spawn Entity on x,y,z with some vx, vz and vz". And i cannot. 

Can you help please? Seriously, this took a **FRICK TON OF WORK** to pull off, and i am giving it for you guys to modify, study and implement it for making multishot-like ranged guns. And i am just asking for this: If i can customize an entity like a snowball or arrow to make it work like a bullet, dealing damage and actually disappearing after hitting something.

I suggest you make a ranged item and shoot that custom ranged item, and then look for customizations of that ranged item entity as this will be much more possible to modify than existing snowball entity

I kinda understand. That is the idea, actually, as this procedure is part of a Ranged Item. Well, of the "RangedItemUsed" procedure. So, ¿How do i implement it into the Ranged Item code so it can actually shoot more of the original projectiles?

package net.mcreator.ancient_guns.item;

import net.minecraftforge.registries.ObjectHolder;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.fml.network.FMLPlayMessages;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.api.distmarker.Dist;

import net.minecraft.world.World;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Hand;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.ActionResult;
import net.minecraft.network.IPacket;
import net.minecraft.item.UseAction;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Item;
import net.minecraft.entity.projectile.AbstractArrowEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.IRendersAsItem;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.EntityClassification;
import net.minecraft.entity.Entity;
import net.minecraft.client.renderer.entity.SpriteRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.block.Blocks;

import net.mcreator.ancient_guns.procedures.MatchlockBlunderbussRangedItemUsedProcedure;
import net.mcreator.ancient_guns.itemgroup.FlintlockGunstabItemGroup;
import net.mcreator.ancient_guns.AncientGunsModElements;

import java.util.Random;
import java.util.Map;
import java.util.HashMap;

@AncientGunsModElements.ModElement.Tag
public class MatchlockBlunderbussItem extends AncientGunsModElements.ModElement {
    @ObjectHolder("ancient_guns:matchlock_blunderbuss")
    public static final Item block = null;
    @ObjectHolder("ancient_guns:entitybulletmatchlock_blunderbuss")
    public static final EntityType arrow = null;
    public MatchlockBlunderbussItem(AncientGunsModElements instance) {
        super(instance, 294);
    }

    @Override
    public void initElements() {
        elements.items.add(() -> new ItemRanged());
        elements.entities.add(() -> (EntityType.Builder.<ArrowCustomEntity>create(ArrowCustomEntity::new, EntityClassification.MISC)
                .setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).setCustomClientFactory(ArrowCustomEntity::new)
                .size(0.5f, 0.5f)).build("entitybulletmatchlock_blunderbuss").setRegistryName("entitybulletmatchlock_blunderbuss"));
    }

    @Override
    @OnlyIn(Dist.CLIENT)
    public void init(FMLCommonSetupEvent event) {
        RenderingRegistry.registerEntityRenderingHandler(ArrowCustomEntity.class,
                renderManager -> new SpriteRenderer(renderManager, Minecraft.getInstance().getItemRenderer()));
    }
    public static class ItemRanged extends Item {
        public ItemRanged() {
            super(new Item.Properties().group(FlintlockGunstabItemGroup.tab).maxDamage(1));
            setRegistryName("matchlock_blunderbuss");
        }

        @Override
        public UseAction getUseAction(ItemStack itemstack) {
            return UseAction.BOW;
        }

        @Override
        public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity entity, Hand hand) {
            entity.setActiveHand(hand);
            return new ActionResult(ActionResultType.SUCCESS, entity.getHeldItem(hand));
        }

        @Override
        public int getUseDuration(ItemStack itemstack) {
            return 72000;
        }

        @Override
        public void onPlayerStoppedUsing(ItemStack itemstack, World world, LivingEntity entityLiving, int timeLeft) {  
            if (!world.isRemote && entityLiving instanceof ServerPlayerEntity) {
                ServerPlayerEntity entity = (ServerPlayerEntity) entityLiving;
                double x = entity.posX;
                double y = entity.posY;
                double z = entity.posZ;
                double ranpitch = 0;
                double ranyaw = 0;
                int disp = 150;
                for (int index0 = 0; index0 < (int) (8); index0++) {
                    ranpitch = (double) (Math.round(((Math.random() * 2*disp) - disp)) / 10);
                    ranyaw = (double) (Math.round(((Math.random() * 2*disp) - disp)) / 10);
                    ArrowCustomEntity entityarrow = shoot(world, entity, random, 1f, 1.5, 0, ranpitch, ranyaw);
                    itemstack.damageItem(1, entity, e -> e.sendBreakAnimation(entity.getActiveHand()));
                    entityarrow.pickupStatus = AbstractArrowEntity.PickupStatus.DISALLOWED;
                }
                {
                    Map<String, Object> $_dependencies = new HashMap<>();
                    $_dependencies.put("entity", entity);
                    $_dependencies.put("itemstack", itemstack);
                    $_dependencies.put("x", x);
                    $_dependencies.put("y", y);
                    $_dependencies.put("z", z);
                    $_dependencies.put("world", world);
                    MatchlockBlunderbussRangedItemUsedProcedure.executeProcedure($_dependencies);
                }
            }     
        }
    }

    @OnlyIn(value = Dist.CLIENT, _interface = IRendersAsItem.class)
    public static class ArrowCustomEntity extends AbstractArrowEntity implements IRendersAsItem {
        public ArrowCustomEntity(FMLPlayMessages.SpawnEntity packet, World world) {
            super(arrow, world);
        }

        public ArrowCustomEntity(EntityType<? extends ArrowCustomEntity> type, World world) {
            super(type, world);
        }

        public ArrowCustomEntity(EntityType<? extends ArrowCustomEntity> type, double x, double y, double z, World world) {
            super(type, x, y, z, world);
        }

        public ArrowCustomEntity(EntityType<? extends ArrowCustomEntity> type, LivingEntity entity, World world) {
            super(type, entity, world);
        }

        @Override
        public IPacket<?> createSpawnPacket() {
            return NetworkHooks.getEntitySpawningPacket(this);
        }

        @Override
        @OnlyIn(Dist.CLIENT)
        public ItemStack getItem() {
            return new ItemStack(Blocks.STONE_BUTTON, (int) (1));
        }

        @Override
        protected ItemStack getArrowStack() {
            return null;
        }

        @Override
        protected void arrowHit(LivingEntity entity) {
            super.arrowHit(entity);
            entity.setArrowCountInEntity(entity.getArrowCountInEntity() - 1);
            entity.hurtResistantTime = 0;
        }

        @Override
        public void tick() {
            super.tick();
            double x = this.posX;
            double y = this.posY;
            double z = this.posZ;
            World world = this.world;
            Entity entity = this.getShooter();
            if (this.inGround) {
                this.remove();
            }
        }
    }
    public static ArrowCustomEntity shoot(World world, LivingEntity entity, Random random, float power, double damage, int knockback, double ranpitch, double ranyaw) {
        ArrowCustomEntity entityarrow = new ArrowCustomEntity(arrow, entity, world);
        entityarrow.shoot(entity.getLookVec().x + (0.79 * (Math.cos(Math.toRadians((90 + ((entity.rotationYaw) + (ranyaw)))))
                                    * Math.cos(Math.toRadians(Math.abs(((entity.rotationPitch) + (ranpitch))))))),
                                    entity.getLookVec().y + (0.79 * Math.sin(Math.toRadians(((-1) * ((entity.rotationPitch) + (ranpitch)))))),
                                    entity.getLookVec().z + (0.79 * (Math.sin(Math.toRadians((90 + ((entity.rotationYaw) + (ranyaw)))))
                                    * Math.cos(Math.toRadians(Math.abs(((entity.rotationPitch) + (ranpitch))))))), power * 2, 0);
        entityarrow.setSilent(true);
        entityarrow.setIsCritical(false);
        entityarrow.setDamage(damage);
        entityarrow.setKnockbackStrength(knockback);
        world.addEntity(entityarrow);
        double x = entity.posX;
        double y = entity.posY;
        double z = entity.posZ;
        world.playSound((PlayerEntity) null, (double) x, (double) y, (double) z,
                (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.generic.explode")),
                SoundCategory.PLAYERS, 1, 1f / (random.nextFloat() * 0.5f + 1) + (power / 2));
        return entityarrow;
    }

    public static ArrowCustomEntity shoot(LivingEntity entity, LivingEntity target) {
        ArrowCustomEntity entityarrow = new ArrowCustomEntity(arrow, entity, entity.world);
        double d0 = target.posY + (double) target.getEyeHeight() - 1.1;
        double d1 = target.posX - entity.posX;
        double d3 = target.posZ - entity.posZ;
        entityarrow.shoot(d1, d0 - entityarrow.posY + (double) MathHelper.sqrt(d1 * d1 + d3 * d3) * 0.2F, d3, 0.5f * 2, 12.0F);
        entityarrow.setSilent(true);
        entityarrow.setDamage(2.5);
        entityarrow.setKnockbackStrength(0);
        entityarrow.setIsCritical(false);
        entity.world.addEntity(entityarrow);
        double x = entity.posX;
        double y = entity.posY;
        double z = entity.posZ;
        entity.world.playSound((PlayerEntity) null, (double) x, (double) y, (double) z,
                (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.generic.explode")),
                SoundCategory.PLAYERS, 1, 1f / (new Random().nextFloat() * 0.5f + 1));
        return entityarrow;
    }
}
 

 

 

I did it. Finally. Thanks. Take the code and implement it for making shotguns via the MCreator Interface.



Donate to MCreator

By donating to developers you can speed up development, as with more resources, we can dedicate more time to MCreator. It is a free project made by developers working on it in their free time.