Hello! This is a tutorial on how to add guns to spigot. In particular, we will be looking at launching projectiles, the PlayerInteractEvent and the ProjectileHitEvent. 1: Starting I assume you know how to create a basic spigot plugin. We're going to do that, and add event handlers for PlayerInteractEvent and ProjectileHitEvent. Code (Text): package me.konsolas.guns; import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.ProjectileHitEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.plugin.java.JavaPlugin; public class Guns extends JavaPlugin implements Listener{ private List<Integer> bullets = new ArrayList<>(); public void onEnable() { Bukkit.getServer().getPluginManager().registerEvents(this, this); } @EventHandler public void onPlayerInteract(PlayerInteractEvent e) { } @EventHandler public void onProjectileHit(ProjectileHitEvent e) { } } The list 'bullets' will be used later. 2: PlayerInteractEvent In our case, the 'Gun' will be diamond horse armor. We will be using the PlayerInteractEvent to get when the player right-clicks with the gun. Code (Text): @EventHandler public void onPlayerInteract(PlayerInteractEvent e) { // If the player right clicks if(e.getAction().equals(Action.RIGHT_CLICK_AIR)) { // If the player was holding diamond horse armour if(e.getPlayer().getItemInHand().getType().equals(Material.DIAMOND_BARDING)) { Player player = e.getPlayer(); // Launch the snowball where the player is looking Snowball bullet = player.launchProjectile(Snowball.class, player.getLocation().getDirection()); // Save the launched snowball's id bullets.add(bullet.getEntityId()); } } } As you can see, we check if the player right clicks, and if he was holding a piece of diamond horse armor. We launch the projectile and add it's entity id to the bullets list. This is so we can refer back to it when the projectile hits something. 3: The hit. We're using the ProjectileHitEvent to detect when a snowball hits something. We will check if the projectile is a snowball, and if it's id is in our 'bullets' list. If so, we will create an explosion at it's location. Code (Text): @EventHandler public void onProjectileHit(ProjectileHitEvent e) { // If the projectile was a snowball if(e.getEntity() instanceof Snowball) { int entityId = e.getEntity().getEntityId(); // If this snowball is a bullet if(bullets.contains(entityId)) { // Clear this entity id bullets.remove((Integer) entityId); // Create an explosion where the snowball hit e.getEntity().getWorld().createExplosion(e.getEntity().getLocation(), 2F); } } } So that was a small tutorial on managing projectiles and when they hit. You could implement different guns by adding seperate lists for each one. e.g: save bullets made with gold horse armor in an 'incendiaryBullets' list, and setting the entity on fire when the projectile hits as opposed to blowing things up
the remove method is overloaded with remove(int) and remove(Object). I have to cast the int to an integer to call the second version of remove, which removes an object based on its value and not its index.
While this tutorial looks good on explaining how to make guns, one thing kind of promotes bad practice: When your plugin is enabled and disabled, it already sends a console message telling the plugin has been enabled / disabled. There are good reasons to send messages onEnable and onDisable but you shouldn't do it just to say it has been enabled. Aside from that it looks decent!
I don't typically do that, but I seem to find it in every plugin I decompile. Thanks for the advice, removed.