[TUTORIAL] How to add a ranged shotgun in game (deluxe edition)

Started by TiberiumTree on

Topic category: User side tutorials

Last seen on 10:02, 8. Aug 2023
Joined May 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[TUTORIAL] How to add a ranged shotgun in game (deluxe edition)
Sat, 03/30/2024 - 09:59 (edited)

shotgun in-game

This tutorial shows how to make a shotgun with ranged bullets,gravity-free ballistics, simple recoil, and 3 ammo strategies. (4 steps total)

 

 

1. Gun and bullets

gun

Gun can be food, item or tool, we only need its trigger.

bullet
Bullets should be ranged items.

 

2. Firing procedure

gun's firing procedure

Part 1 is the part that tells the gun which ammo strategy is activated by the number NBT tag. This is easy to do with the GUI or Key binding.

 

Part 3 are the details after the shooting.

Particle command is set to display a particle as gun smoke.

execute at @s anchored eyes run particle minecraft:smoke ^-0.45 ^-0.38 ^1 ~ ~ 0 0 0 force
execute at @s anchored eyes run particle minecraft:smoke ^0.45 ^-0.38 ^1 ~ ~ 0 0 0 force

Teleport command plays a simple recoil effect. The last parameter controls vertical rotation, and the penultimate parameter controls horizontal rotation.

teleport @s ~ ~ ~ ~ ~

 

 

3*Part 2 are the core parts of this procedure.

execute anchored eyes as @e[type=vinheim_primary_school:entitybullethunter_blunderbuss_ammo_a,tag=!vps_bullet_initialized,distance=..3,limit=1,sort=nearest] at @s run function vinheim_primary_school:entitybullethunter_blunderbuss_ammo_a_init

This command will select the last bullet player shot, to run the corresponding function. The first part2 repeats 7 times to make a buckshot effect.

 

type name

Among 3*part 2, type names are different from each other, and relative to the ranged item being shot. 
Check code in that ranged item or /summon command in-game, to make sure the type name correct.

A damage box is added here to limit the number of shots. If that not fit in your mod, bullet items or ammo cases with NBTtag can be alternatives.

 

 

3. Init function and custom command.

ammo_a

tag @s add vps_bullet_initialized
tag @s add vps_is_bullet
tag @s add vps_has_trail_smoke
data merge entity @s {NoGravity:1}
vpscmd_despawn 5
vpscmd_bulletdispersion 350

 ammo_b

tag @s add vps_bullet_initialized
tag @s add vps_is_bullet
tag @s add vps_has_trail_smoke
data merge entity @s {NoGravity:1}
data merge entity @s {PierceLevel:10}
vpscmd_despawn 10

 ammo_c

tag @s add vps_bullet_initialized
tag @s add vps_is_bullet
tag @s add vps_has_trail_sys
tag @s add vps_trail_sys_1
data merge entity @s {NoGravity:1}
data merge entity @s {PierceLevel:7}
vpscmd_despawn 17

Tags vps_bullet_initialized is added to avoid a second init, as there is [tag=!vps_bullet_initialized] in @e selection in firing procedure part2.
Tags vps_is_bullet, vps_has_trail_smoke,  vps_trail_sys_1 are added for global tick event about bullet trail. I use 1*1 transparent texture for bullets and need to trail it to spread area_effect_cloud, so these bullets need trails, otherwise, these tags are optional.

 

{NoGravity:1} gives bullets straight ballistics, making bullets act not look like arrows. 
{PierceLevel:7} allows bullets to pass-through entities in 7 ticks.

 

vpscmd_despawn and vpscmd_bulletdispersion are custom commands.

vpscmd_despawn
vpscmd_despawn para0 will kill the executor after para0  ticks, in other words, it caps the bullet's shotrange. 
(Vanilla /schedule performs poorly somehow.)

dispersion

vpscmd_bulletdispersion para0  will modify the executor's velocity with a random space vector of length proportional to para0.

In its Part1, a random 3D unit vector created, but use a certain amount of arithmetic (sin+cos+acos=6).

cube

So, if you intend to use the cube dispersion vector, this or the plugin are good alternatives.


In Part2, as the unit of para0 is one thousand times the original velocity unit, its unit needs to be converted back.
(This adds two boxes, but is necessary because the variable set from the cmd para is int)

In Part3, velocity is overridden.

 

 

4. Global tick function (Optional)

 

on world tick update

execute as @e[tag=vps_is_bullet] at @s run function vinheim_primary_school:vpsbullettick

While bullet flying tick is a trigger a allows multiple procedures to run simultaneously and based on the player's XYZ, in that On world tick update is the proper trigger to link a global tick function.

execute as @s[tag=vps_has_trail_smoke] run particle minecraft:mycelium ~ ~ ~ ~ ~ 0 0 0 normal
execute as @s[tag=vps_has_trail_sys,tag=vps_trail_sys_1] run vpscmd_trailsys 1

In this function, bullet has tag vps_has_trail_smoke spawn particle as its trail. 

And bullet has vps_has_trail_sys and vps_trail_sys_1 at the same time run a custom command with para4=1.

trail_sys

summon minecraft:area_effect_cloud ^ ^0.4 ^ {Duration:60,Radius:1.5,RadiusPerTick:-0.01,Particle:"vinheim_primary_school:bluecloud_3_particle",ReapplicationDelay:60,DurationOnUse:10,Potion:"vinheim_primary_school:quicksilver_acute_poison"}
summon minecraft:area_effect_cloud ^ ^ ^ {Duration:60,Radius:1.5,RadiusPerTick:-0.01,Particle:"vinheim_primary_school:bluecloud_3_particle",ReapplicationDelay:60,DurationOnUse:10,Potion:"vinheim_primary_school:quicksilver_acute_poison"}
summon minecraft:area_effect_cloud ^ ^-0.4 ^ {Duration:60,Radius:1.5,RadiusPerTick:-0.01,Particle:"vinheim_primary_school:bluecloud_3_particle",ReapplicationDelay:60,DurationOnUse:10,Potion:"vinheim_primary_school:quicksilver_acute_poison"}

Enable bottles

Wait for 3 ticks to avoid damage from the potion trail. 

Bullet damage is 0, but this trail system spreads area_effect_cloud that deal a special form of damage. falling_block is also a choice.

summon minecraft:falling_block ~ ~ ~ {BlockState:{Name:"minecraft:iron_block"},Time:1,HurtEntities:1,FallHurtAmount:3,FallHurtMax:100,Motion:[0.0d, 2d, 0.0d]} 

However, I have no idea how to generate area_effect_cloud that have the correct owner NBT, as for falling_block, they are ownerless.

Besides, area_effect_cloud potion has a fixed effect duration, unless it is a vanilla potion:

summon minecraft:area_effect_cloud ^ ^ ^ {Duration:60,Radius:1.4,RadiusPerTick:-0.01,ReapplicationDelay:60,DurationOnUse:10,Particle:"vinheim_primary_school:bluecloud_3_particle",Effects:[{Id:1,Amplifier:0,Duration:320,Ambient:0,ShowParticles:0}]}

So the instant effect is recommended, or use this effect as a springboard to give the player another effect.

 

 

 

 

Edited by TiberiumTree on Sat, 03/30/2024 - 09:59
Last seen on 14:18, 16. Apr 2024
Joined Apr 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Cool tutorial
Sun, 06/13/2021 - 23:08

Cool tutorial

Last seen on 09:42, 5. Sep 2023
Joined May 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Your recoil effect is…
Sat, 07/24/2021 - 18:47

Your recoil effect is outstanding. What a clever way to use teleport.