MinecraftScripterAI
Discord ↗
Link Copied to Clipboard
×
"do a stick that throwing a projectile that projectile if touch a mob mob will explode"
/* To create a custom item that launches a projectile that explodes when it touches a mob, follow these steps: */ // Step 1: Create a custom item class that extends Item public class ExplodingStickItem extends Item { public ExplodingStickItem(Properties properties) { super(properties); } // Step 2: Override onItemRightClick to launch the projectile @Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { ItemStack itemStack = player.getItemInHand(hand); // Step 3: Create and spawn the projectile entity ExplodingProjectileEntity projectile = new ExplodingProjectileEntity(world, player); projectile.shootFromRotation(player, player.xRot, player.yRot, 0.0F, 1.5F, 1.0F); world.addFreshEntity(projectile); // Step 4: Consume durability or item stack if (!player.abilities.instabuild) { itemStack.hurtAndBreak(1, player, (p) -> p.broadcastBreakEvent(hand)); } return ActionResult.success(itemStack); } } // Step 5: Create a custom projectile entity class that extends ThrowableEntity public class ExplodingProjectileEntity extends ThrowableEntity { public ExplodingProjectileEntity(EntityType<? extends ThrowableEntity> entityType, World world) { super(entityType, world); } public ExplodingProjectileEntity(World world, LivingEntity thrower) { super(/* Replace with your EntityType */, thrower, world); } // Step 6: Override onHitEntity to explode when it touches a mob @Override protected void onHitEntity(EntityRayTraceResult result) { super.onHitEntity(result); Entity entity = result.getEntity(); // Step 7: Create the explosion if (entity instanceof LivingEntity && !(entity instanceof PlayerEntity)) { this.level.explode(this, this.getX(), this.getY(), this.getZ(), 3.0F, Explosion.Mode.BREAK); this.remove(); } } } /* Finally, register your custom item and projectile entity in your mod's registry. */