Solved gun, change shooting distance and add sight(left_click) like mcwar

Discussion in 'Plugin Development' started by BeastCraft3, Aug 6, 2014.

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

    BeastCraft3

    Hello, If you have played on mcwar/z, then you have probraly discovered that if you left click while holding a gun you zoom in. Help please. Another thing is how do I make an axe shoot a snowball that goes 250 blocks? Please help. my code so far:

    Code:
    package CrankPhilsWarPlugin;
     
    import java.util.ArrayList;
     
    import org.bukkit.Bukkit;
    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.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
     
    public class Dragunov implements Listener, CommandExecutor {
     
        private Main main;
       
        public Dragunov(Main main) {
            this.main = main;
            main.getServer().getPluginManager().registerEvents(this, main);
        }
       
        ArrayList<Player> cooldown = new ArrayList<Player>();
       
        ItemStack Dragunov = new ItemStack(Material.WOOD_AXE);{
            ItemMeta Dragunovmeta = Dragunov.getItemMeta();
            ArrayList<String> cc = new ArrayList<String>();
            Dragunovmeta.setDisplayName(ChatColor.RED + "Dragunov");
            cc.add(ChatColor.GREEN + "Sniper");
            Dragunovmeta.setLore(cc);
            Dragunov.setItemMeta(Dragunovmeta);
        }
           
           
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent e) {
                final Player player2 = e.getPlayer();
                if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
               
                if (!(e.getItem().getType() == Material.WOOD_AXE)) return;
               
                if(checkForItems(e.getPlayer(), 1, Material.CLAY_BALL) == false) return;
               
                if (cooldown.contains(player2)) {
                    player2.sendMessage("§aYou have to wait 3 seconds!");
                } else {
                    e.getPlayer().launchProjectile(Snowball.class);
                   
                    subtractItems(e.getPlayer(), 1, Material.CLAY_BALL);
                }
               
                cooldown.add(player2);
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                public void run() {
                cooldown.remove(player2);
                }
                }, 30);
                return;
               
                }
           
            @EventHandler
                public void onEntityDamage(EntityDamageByEntityEvent e) {
                        if (e.getDamager() instanceof Snowball) {
                            Snowball f = (Snowball) e.getDamager();
                                if (f.getShooter() instanceof Player) {
                                        Player shooter = (Player) f.getShooter();
                                        if (shooter.getItemInHand().getType() == Material.WOOD_AXE) {
                                                e.setDamage(1.0);
                                        }
                                }
                        }
                }
            public boolean checkForItems(Player p, int z, Material m){
                    Player player = p;
                    boolean check = false;
                    int count = 0;
                    ItemStack[] playInv = player.getInventory().getContents();
                    for(int i = 0; i < playInv.length; i++){
                        if(playInv[i] != null){
                            if(playInv[i].getType().equals(m)){
                                count += playInv[i].getAmount();
                            }
                        }
                    }
                    if(count >= z) check = true;
                 
                        return check;
                }
            public void subtractItems(Player p, int z, Material m){
                    Player player = p;
                    ItemStack[] playInv = player.getInventory().getContents();
                    for(int i = 0; i < playInv.length; i++){
                        if(playInv[i] != null){
                            if(playInv[i].getType().equals(m)){
                                if(playInv[i].getAmount() == z){
                                    player.getInventory().setItem(i,  null);
                                    return;
                                }
                                else if(playInv[i].getAmount() > z){
                                    playInv[i].setAmount(playInv[i].getAmount() - z);
                                    return;
                                }
                                else{
                                    z -= playInv[i].getAmount();
                                    player.getInventory().setItem(i,  null);
                                }
                            }
                        }
                    }
                 
                }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            if(label.equalsIgnoreCase("dragunov")) {
                Player p = (Player) sender;
                if(p.hasPermission("gun.dragunov")){
                    p.getInventory().addItem(new ItemStack(Dragunov));
               
                }
            }
           
            return false;
        }
     
    }
    
     
  2. Offline

    Garris0n

    First of all, don't use "== false".
    Second, fix your variable/class/anything else names. If you don't understand what's wrong, click here.
    Third, that is not how you do cooldowns. You should save a map of player UUIDs and longs, the long being the timestamp of the last time they did the action. Then you check against that.
    Fourth, don't save collections of players.
    Fifth, your code doesn't seem to have anything to do with your question. What have you tried?
     
  3. Offline

    BeastCraft3

    Garris0n
    I dont have any idea how to add that sight stuff or the "getting the snowball to shoot 250 blocks" Help me maybe?
     
  4. Offline

    Garris0n

    Fix the aforementioned issues first.
     
  5. Offline

    BeastCraft3

    Garris0n
    Really? They work perfecly fine for me. I get any issues out of them.
     
  6. Offline

    Garris0n

    The things I listed are extremely bad practice and it's best to stop doing them now before you end up with bigger issues in the future. Some of them also lead to unnecessary strain on the server (player lists = memory leaks, cooldowns should not be implemented with schedulers). I assure you, you should stop doing these things immediately or you will regret it down the road.
     
  7. Offline

    BeastCraft3

    Garris0n
    Ok, can you help me change the cooldown to the other thing? I've never done it before and you sound like you know what you are talking about
     
  8. Offline

    Garris0n

    You can get the current timestamp in milliseconds with System.currentTimeMillis(). You would save this in a Map with player UUIDs as keys. Whenever they "do" the action, put the player's UUID (player.getUniqueId()) in the map along with the current time in milliseconds. When you're checking if they're still on cooldown, check if the difference of the current time and the time in the map is greater than your cooldown time. If so, enough time has passed and they are no longer on cooldown.

    If you at any point need to convert between different units of time, use the TimeUnit class.
     
  9. Offline

    BeastCraft3

    Garris0n
    I tried and I've come this far, I know i've done something wrong. Can you please help me? What should I change and where should I really write it, and how do I make it wait 3 seconds before using the Dragunov again?
    Code:
    package CrankPhilsWarPlugin;
     
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.UUID;
    import java.util.concurrent.TimeUnit;
     
    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.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
     
    public class Dragunov implements Listener, CommandExecutor {
       
        public Dragunov(Main main) {
            main.getServer().getPluginManager().registerEvents(this, main);
        }
       
        HashMap<UUID, Long> lastUsed = new HashMap<UUID, Long>();
       
        ItemStack Dragunov = new ItemStack(Material.WOOD_AXE);{
            ItemMeta Dragunovmeta = Dragunov.getItemMeta();
            ArrayList<String> cc = new ArrayList<String>();
            Dragunovmeta.setDisplayName(ChatColor.RED + "Dragunov");
            cc.add(ChatColor.GREEN + "Sniper");
            Dragunovmeta.setLore(cc);
            Dragunov.setItemMeta(Dragunovmeta);
        }
           
           
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent e) {
                Player player = e.getPlayer();
                if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
               
                if (!(e.getItem().getType() == Material.WOOD_AXE)) return;
               
                if(checkForItems(e.getPlayer(), 1, Material.CLAY_BALL) == false) return;
               
                lastUsed.put(player.getUniqueId(), System.currentTimeMillis());
               
                    e.getPlayer().launchProjectile(Snowball.class);
                   
                    subtractItems(e.getPlayer(), 1, Material.CLAY_BALL);
               
                }
           
            @EventHandler
                public void onEntityDamage(EntityDamageByEntityEvent e) {
                        if (e.getDamager() instanceof Snowball) {
                            Snowball f = (Snowball) e.getDamager();
                                if (f.getShooter() instanceof Player) {
                                        Player shooter = (Player) f.getShooter();
                                        if (shooter.getItemInHand().getType() == Material.WOOD_AXE) {
                                                e.setDamage(1.0);
                                        }
                                }
                        }
                }
           
            private static final int COOLDOWN_TIME_IN_SECONDS = 10;
           
                public boolean canUseAgain(Player player){
                    long lastTimeUsed = lastUsed.get(player.getUniqueId());
                    long timeToWait = TimeUnit.SECONDS.toMillis(COOLDOWN_TIME_IN_SECONDS);
                    return System.currentTimeMillis() - lastTimeUsed > timeToWait;
                }
           
            public boolean checkForItems(Player p, int z, Material m){
                    Player player = p;
                    boolean check = false;
                    int count = 0;
                    ItemStack[] playInv = player.getInventory().getContents();
                    for(int i = 0; i < playInv.length; i++){
                        if(playInv[i] != null){
                            if(playInv[i].getType().equals(m)){
                                count += playInv[i].getAmount();
                            }
                        }
                    }
                    if(count >= z) check = true;
                 
                        return check;
                }
            public void subtractItems(Player p, int z, Material m){
                    Player player = p;
                    ItemStack[] playInv = player.getInventory().getContents();
                    for(int i = 0; i < playInv.length; i++){
                        if(playInv[i] != null){
                            if(playInv[i].getType().equals(m)){
                                if(playInv[i].getAmount() == z){
                                    player.getInventory().setItem(i,  null);
                                    return;
                                }
                                else if(playInv[i].getAmount() > z){
                                    playInv[i].setAmount(playInv[i].getAmount() - z);
                                    return;
                                }
                                else{
                                    z -= playInv[i].getAmount();
                                    player.getInventory().setItem(i,  null);
                                }
                            }
                        }
                    }
                 
                }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            if(label.equalsIgnoreCase("dragunov")) {
                Player p = (Player) sender;
                if(p.hasPermission("gun.dragunov")){
                    p.getInventory().addItem(new ItemStack(Dragunov));
               
                }
            }
           
            return false;
        }
     
    }
    
     
  10. Offline

    Garris0n

    First of all, you should really initialize that ItemStack in your onEnable.
    Second, you need to fix your variable names (specifically in that ItemStack bit). Again, here.
    Third, you do not actually use the method you made to check if the player is on cooldown.
     
  11. Offline

    BeastCraft3

    Garris0n
    Please help me with the "Cooldown" I need this plugin finished within 1 and a half hour. Where should I put the different methods
     
  12. Offline

    Garris0n

    It's not my plugin. I've told you everything you need to do in pretty good detail.

    I'm also having a hard time believing that you yourself wrote that code, seeing as you have a variable named "COOLDOWN_TIME_IN_SECONDS" and yet asked how to change the cooldown time.
     
  13. Offline

    BeastCraft3

    Garris0n
    I told you, I didnt write the code my self, Thats the reason why I need help. I tried the way its now and I failed. I found some methods that I hoped would work, I didnt find any tutorials on how to do this. Its a reason why im asking.
    1) Im new to java
    2) I've never used this type of "cooldown"
    3) Tutorials on youtube are often bad, witch gave me an idea to ask for help here. Sorry if im stupid or dont got so much knowledge about this, but you got to understand that I'm trying my best to learn and understand every post you have written to me. Im thankful for every post, but I just dont understand this type of "cooldown" than the others. So Im begging you, Could you help me by writing methods and I can try to paste them in my code so next time I do this kinds of plugins I actually know where to start and end.
     
  14. Offline

    mazentheamazin

    Let me give you some quick simple and direct steps to fix your problem:
    1. Stop what you're doing and learn Java (for obvious reasons);
    2. Go back to your code and you should have the knowledge to fix your problem.

    If you have learned the language properly members of the Bukkit Forums will be happy to assist you with your problem, otherwise we will direct you in the right way; a teacher will not teach you calculus without knowledge of basic math.
     
    Garris0n likes this.
  15. Offline

    Necrodoom

  16. BeastCraft3 And I wouldn't recommend YouTube, I'd recommend a book or, failing that, oracle's online tutorial
     
    mazentheamazin and Garris0n like this.
  17. Offline

    BeastCraft3

    Necrodoom
    I wrote the code, I just didnt write the UUID methods. mazentheamazin I know the basic of java, I just dont know that much about UUID's. :3
     
  18. Offline

    Garris0n

    If you're new to Java, then don't start messing with Bukkit until you have a basic understanding of it. There's not going to be a tutorial for everything, you can't just think in terms of assembling other people's code and calling it your own.

    If your teacher assigns you an essay to write about, say, dogs, perhaps you could go copy-paste paragraphs from other people's essays on dogs. That would both be taking credit for others work and proving that you're not capable of writing an essay. Sure, you could keep doing that, but what happens when your teacher asks you to write a paper on, say, your experience with your pet dog? Are you going to search the internet for papers other people wrote about you and your dog? No, you won't find anything. You'll need to learn to write your own.

    It's the same thing here. There are not tutorials for everything you can possibly imagine, and at some point you're going to actually have to do something yourself. If you don't understand how to do that, go back to Java until you know the basics. Then start using the Bukkit API.
     
    mazentheamazin and AdamQpzm like this.
  19. Offline

    mazentheamazin

    BeastCraft3
    If you did know Java you'd know that UUID is an object which represents a unique ID and has nothing to do with adding players to a map/list. Many other members have suggested and explained why you should learn the language itself before going into any API. Even if you actually learned the language, you weren't taught properly due to that you use bad practices and not following general naming conventions.
     
  20. Offline

    BeastCraft3

    Garris0n
    I guess I'll use the cooldown for now, until I learn more about UUID. Thanks anyway. I'll slove the thread cause I didnt get the help I was really looking for. And garrions, I wrote the code my self. I didnt take any credit over it. I got told that I could use bukkit for help, not to be throwing shit at my face. I made the entire code my self without the UUID. Bye.
     
  21. Offline

    mazentheamazin

    BeastCraft3
    You are aware we are not trying to insult you, correct? We are actually giving you advice and not just doing the work for you. If you want somebody to program a plugin for you; you've come to the wrong place. On the Bukkit Forums we actually want to make sure that you know how, and why your code is working in the way it does; not "my code works and I don't know why" or "my code doesn't work and I don't know why". Furthermore, I would recommend that you should take our advice to heart and apply them accordingly.
     
  22. Offline

    BeastCraft3

  23. Offline

    Dubehh

Thread Status:
Not open for further replies.

Share This Page