Solved Creating a blocks in the direction a player is facing.

Discussion in 'Plugin Development' started by gogobebe2, Dec 21, 2014.

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

    gogobebe2

    What the plugin should do is when you type /makecannon it should generate a bunch of blocks that look like a cannon where the player is facing (so they can set it up where they want) and be used with a redstone source to fire (eg button).

    I've been googling for so long to figure this one out. This is my first bukkit plugin and I've been working really hard to learn the different classes from the bukkit docs.

    I've tried using vectors, but they were too confusing. I had a look at getTargetBlock but that was deprecated for some reason and I've just come to the point where I'm about to give up but as a last hope create a thread here.

    This post might help more experienced bukkit developers understand vectors better to help me:
    http://bukkit.org/threads/get-the-block-in-front-of-player.61270/

    When reading you don't really need to worry about anything but the makeCannon() void method I think...
    Here is the code for the plugin not including the config.yml and plugin.yml:
    Code:
    package com.gmail.gogobebe2.Cannon;
    
    import net.milkbowl.vault.economy.*;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.*;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;
    
    public class Cannon extends JavaPlugin {
        public static Economy economy = null;
    
        @Override
        public void onEnable() {
            if(!setupEconomy()) {
                getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
            }
            getConfig().options().copyDefaults(true);
            saveConfig();
            getLogger().info("Hi! Cannon has been loaded on version " + getDescription().getVersion() + "!");
        }
    
        @Override
        public void onDisable() {
            getLogger().info("Hi! Cannon version: " + getDescription().getVersion() + "has been turned off!!");
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("makecannon")) {
                if (!sender.hasPermission("Cannon.makecannon")) {
                    sender.sendMessage(ChatColor.RED + "You are not permitted to use this command." + ChatColor.RESET);
                    return true;
                }
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.GREEN + "This command can only be run by a player dude!" + ChatColor.RESET);
                } else {
                    Player player = (Player) sender;
             
                    //If they want to use economy, do all this stuff...
                    if (getConfig().getBoolean("Use economy")) {
                        EconomyResponse r = economy.withdrawPlayer(player, getConfig().getInt("Cost of /makecannon"));
                        if (r.transactionSuccess()) {
                            player.sendMessage("[" + ChatColor.RED + "Cannon" + ChatColor.WHITE + "] " + ChatColor.GREEN + "Cannon created and $" + getConfig().getInt("Cost of /makecannon") + " has been withdrawn from your balance."+ ChatColor.RESET);
                            makeCannon(player);
                        }
                        else {
                            player.sendMessage("[" + ChatColor.RED + "Cannon" + ChatColor.WHITE + "] " + ChatColor.YELLOW + "You do not sufficent funds to create a cannon. Type " + ChatColor.AQUA + "/money" + ChatColor.RESET + ".");
                        }
                    }
                    //If they don't, carry on the command as usual...
                    else {
                        player.sendMessage("[" + ChatColor.RED + "Cannon" + ChatColor.WHITE + "] " + ChatColor.GREEN + "Cannon created." + ChatColor.RESET);
                        makeCannon(player);
                    }
             
                }
                return true;
            }
            return false;
        }
    
        private void makeCannon(Player player) {
            Location playerLoc = player.getLocation();
     
     
            //Sends player back 1 block from where he is looking.
            Vector playerVec = playerLoc.getDirection();
            Location behindLocation = playerLoc.subtract(playerVec);
            player.teleport(behindLocation);
     
            //TODO test all for air.
    
     
            //TODO generate the cannon. 
    
     
            //TODO make the fireball fire from the cannon.
     
        }
    
    
        private boolean setupEconomy()
        {
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
            if (economyProvider != null) {
                economy = economyProvider.getProvider();
            }
    
            return (economy != null);
        }
    
        private boolean isAir(Location loc) {
            if (loc.getBlock().getType().equals(Material.AIR)) {
                return true;
            }
            return false;
        }
    
        private void setBlock(Location loc, Material material) {
            loc.getBlock().setType(material);
        }
    
    }
    
    [Edit]: P.S. The bit where it makes the player go back 1 block is so that I can generate the structure without trapping the player.
    [Edit2]: Let me rephrase, how do I set the block next to where I am looking. How to find the left or right of my looking direction?
     
    Last edited: Dec 21, 2014
  2. Offline

    Skionz

  3. Offline

    ChipDev

  4. Offline

    gogobebe2

    Yeah I have, I did a whole function that tried to do that. I could easily get it infront, however when I tried to get the one 1 ahead and 1 left of the player, then it started getting harder.
    Here's what I did:
    Code:
        private void makeCannon(Player player) {
            Location playerLoc = player.getLocation();
            Vector playerVec = playerLoc.getDirection();
          
            Location behindLocation = playerLoc.subtract(playerVec);
          
            //Sends player back 1 block on the x axis.
            player.teleport(behindLocation);
          
            //TODO test all for air. Example for just bottomWoodenSlab:
            //if (isAir(bottomWoodenSlab)) {
            //      if (isAir(some other part of the cannon) {
            //        etc
            //    }
            //}
          
            //TODO generate the cannon.
          
            //1
            //bottom wooden slab
            Location bottomWoodenSlab = playerLoc.add(playerVec);
            setBlock(bottomWoodenSlab, Material.WOOD_STEP);
          
            //2
            //first wooden block
            Location firstWoodenBlock = bottomWoodenSlab.add(playerVec);
            setBlock(firstWoodenBlock, Material.WOOD);
          
            //top wooden slab
            Location topWoodenSlab = firstWoodenBlock.clone();
            topWoodenSlab.setY(topWoodenSlab.getY() + 1);
            setBlock(topWoodenSlab, Material.WOOD_STEP);
          
            //first right cobble block
            //???? wtf?
          
          
            //3
            //second wooden block
            Location secondWoodenBlock = firstWoodenBlock.add(playerVec);
            setBlock(secondWoodenBlock, Material.WOOD);
          
            //4
            //third wooden block
            Location thirdWoodenBlock = secondWoodenBlock.add(playerVec);
            setBlock(thirdWoodenBlock, Material.WOOD);
          
            //5
            //forth wooden block
            Location forthWoodenBlock = thirdWoodenBlock.add(playerVec);
            setBlock(forthWoodenBlock, Material.WOOD);
          
    
          
            //TODO make the fireball fire from the cannon.
          
        }
      
    
    In your post, I don't understand what the .multiply(4.0) does?

    This post might help more experienced bukkit developers understand vectors better to help me:
    http://bukkit.org/threads/get-the-block-in-front-of-player.61270/
    I also edited my post a couple times

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 28, 2016
  5. Offline

    ChipDev

    Oh, that is just optional. I made a snowball shoot with that velocity, or else it would be a wimpy velocity to I multiplied it :p
     
    gogobebe2 likes this.
  6. Offline

    gogobebe2

    Oh ok. That's fine. Thanks.
    So you see what I've done so far? I can create blocks which are infront of the player and modify y and how far away they are to get them there but I can't make the block next to the player.

    @ChipDev
    For some reason a moderator or someone locked that other thread....
    Anyway I tried .multiply(0.5) and added it, but it didn't put it on the side. :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 28, 2016
    ChipDev likes this.
  7. Offline

    ChipDev

    That divides the vector into 2, you want to create 2 vectors (I think) and then get the real vector, and add the X 0.5 vector to it.

    Did it work? :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 28, 2016
    gogobebe2 likes this.
Thread Status:
Not open for further replies.

Share This Page