How to make a Block Shoot Arrows?

Discussion in 'Plugin Development' started by mattyj1231, Mar 15, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    mattyj1231

    I'm trying to make a plugin where you right click the ground with a wood-shovel and you can select to place an archer tower, and that archer tower can shoot arrows at mobs and displays its name and level. But I can't seem to figure out how to make my archer tower shoot arrows.(This could be done by attaching the blocks of the archer tower to a class I don't know)

    Main.java
    Code:
    package com.mattjuior.towerdefense;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.mattjuior.towerdefense.commands.Placer;
    import com.mattjuior.towerdefense.events.PlacerClick;
    
    public class Main extends JavaPlugin {
        public void onEnable(){
            getCommand("placer").setExecutor(new Placer());
           
            registerEvents();
        }
       
        public void registerEvents() {
            PluginManager pm = getServer().getPluginManager();
           
            pm.registerEvents(new PlacerClick(), this);
        }
       
    }
    
    Placer.java
    Code:
    package com.mattjuior.towerdefense.commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    public class Placer implements CommandExecutor{
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to enter this command!");
                return false;
            }
           
            Player player = (Player) sender;
            if (player.hasPermission("MTD.placer")) {
            player.getInventory().addItem(new ItemStack(Material.WOOD_SPADE));
            return true;
            }else {
                player.sendMessage(ChatColor.RED + "You do not have permission!");
                return false;
            }
        }
    
    }
    
    PlacerClick.java
    Code:
    package com.mattjuior.towerdefense.events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class PlacerClick implements Listener {
    
        public int xClicked;
        public int yClicked;
        public int zClicked;
        World world;
    
        public static Inventory buildTowerInv = Bukkit.createInventory(null, 9, "Build Tower");
        static {
            buildTowerInv.setItem(0, new ItemStack(Material.ARROW, 1));
        }
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            ItemStack clicked = event.getCurrentItem();
            Inventory inventory = event.getInventory();
            if (inventory.getName().equals(buildTowerInv.getName())) {
                if (clicked.getType() == Material.ARROW) {
                    event.setCancelled(true);
                    player.closeInventory();
    
                    // First Layer
                    world.getBlockAt(xClicked + 1, yClicked + 1, zClicked + 1).setTypeId(85);
                    world.getBlockAt(xClicked + 1, yClicked + 1, zClicked - 1).setTypeId(85);
                    world.getBlockAt(xClicked - 1, yClicked + 1, zClicked + 1).setTypeId(85);
                    world.getBlockAt(xClicked - 1, yClicked + 1, zClicked - 1).setTypeId(85);
                    // Second Layer
                    world.getBlockAt(xClicked + 1, yClicked + 2, zClicked + 1).setTypeId(85);
                    world.getBlockAt(xClicked + 1, yClicked + 2, zClicked - 1).setTypeId(85);
                    world.getBlockAt(xClicked - 1, yClicked + 2, zClicked + 1).setTypeId(85);
                    world.getBlockAt(xClicked - 1, yClicked + 2, zClicked - 1).setTypeId(85);
                    // Third Layer
                    world.getBlockAt(xClicked + 1, yClicked + 3, zClicked + 1).setTypeId(85);
                    world.getBlockAt(xClicked + 1, yClicked + 3, zClicked - 1).setTypeId(85);
                    world.getBlockAt(xClicked - 1, yClicked + 3, zClicked + 1).setTypeId(85);
                    world.getBlockAt(xClicked - 1, yClicked + 3, zClicked - 1).setTypeId(85);
                    // Thorth Layer
                    world.getBlockAt(xClicked, yClicked + 4, zClicked).setTypeId(5);
                    world.getBlockAt(xClicked + 1, yClicked + 4, zClicked).setTypeId(5);
                    world.getBlockAt(xClicked - 1, yClicked + 4, zClicked).setTypeId(5);
                    world.getBlockAt(xClicked, yClicked + 4, zClicked + 1).setTypeId(5);
                    world.getBlockAt(xClicked, yClicked + 4, zClicked - 1).setTypeId(5);
                    world.getBlockAt(xClicked + 1, yClicked + 4, zClicked + 1).setTypeId(5);
                    world.getBlockAt(xClicked - 1, yClicked + 4, zClicked + 1).setTypeId(5);
                    world.getBlockAt(xClicked + 1, yClicked + 4, zClicked - 1).setTypeId(5);
                    world.getBlockAt(xClicked - 1, yClicked + 4, zClicked - 1).setTypeId(5);
                    // Sixth Layer
                    world.getBlockAt(xClicked, yClicked + 6, zClicked).setTypeId(126);
                    world.getBlockAt(xClicked + 1, yClicked + 6, zClicked).setTypeId(126);
                    world.getBlockAt(xClicked - 1, yClicked + 6, zClicked).setTypeId(126);
                    world.getBlockAt(xClicked, yClicked + 6, zClicked + 1).setTypeId(126);
                    world.getBlockAt(xClicked, yClicked + 6, zClicked - 1).setTypeId(126);
                    world.getBlockAt(xClicked + 1, yClicked + 6, zClicked + 1).setTypeId(126);
                    world.getBlockAt(xClicked - 1, yClicked + 6, zClicked + 1).setTypeId(126);
                    world.getBlockAt(xClicked + 1, yClicked + 6, zClicked - 1).setTypeId(126);
                    world.getBlockAt(xClicked - 1, yClicked + 6, zClicked - 1).setTypeId(126);
                }
            }
        }
    
        @EventHandler
        public void placerClick(PlayerInteractEvent event) {
    
            Player player = event.getPlayer();
    
            if (player.getItemInHand().getType() == Material.WOOD_SPADE
                    && (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    
                world = event.getClickedBlock().getWorld();
                xClicked = event.getClickedBlock().getX();
                yClicked = event.getClickedBlock().getY();
                zClicked = event.getClickedBlock().getZ();
    
                player.openInventory(buildTowerInv);
            }
        }
    }
    
    Please Help!
     
  2. Offline

    blablubbabc

    World has a method for spawning entities at a certain location: So you can spawn arrow entities at a certain location (ex. slightly above or in front of your 'tower block'). After spawning the projectile, you can assign it a certain velocity to make it fly in the wanted direction.

    So all you need to store somewhere is the location of your 'tower blocks'.
     
  3. Offline

    mattyj1231

    Thanks Ill try that!

    Where/How would you store the location of all of the archer tower blocks?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 16, 2016
  4. @mattyj1231 You can store the locations of your tower in a config file, or in an Array or Hash map.
     
  5. Offline

    mattyj1231

    Ok I'll see how that works
     
Thread Status:
Not open for further replies.

Share This Page