Ninja code?

Discussion in 'Plugin Development' started by TCO_007, Mar 22, 2014.

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

    TCO_007

    Hey guys! Im almost positive the rest of my code is 100 percent correct but I wanted to ask if this was correct. Im new to coding so I apologize if it is horrible but I'm hoping I at least got close haha. Basically what I want it to do is give the player Speed 2 for 30 seconds. Im still trying to learn how to do the time but I will eventually learn. I just need to know as of right now if this is even the correct code to apply the effect. Thanks!
    Code:java
    1. @EventHandler
    2. public void onNinjaClick(PlayerInteractEvent e){
    3. Player player = e.getPlayer();
    4. if (e.getAction().equals(Action.RIGHT_CLICK_AIR) && player.getItemInHand().getType() == Material.QUARTZ){
    5. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 80, 1));
    6. player.sendMessage(ChatColor.GRAY + "You have activated Ninja speed!");
    7.  
    8.  
    9. }
    10. }
     
  2. Offline

    Scizzr

    1. Duration is in ticks, so just multiply seconds*20 to get ticks.
    2. Do you want to check if they already have the effect?
    3. Do you want to consume an item when they use it? Except for creative players?
    Code:
    private final PotionEffect PE_NINJA_SPEED = new PotionEffect(PotionEffectType.SPEED, 30*20, 0);
     
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        BukkitUtils.msgPlayer(player, "&cpie");
     
        if (event.getAction() == Action.RIGHT_CLICK_AIR && player.getItemInHand().getType() == Material.QUARTZ) {
            //check for potion already in effect and consume an item for non-creative players
            if (player.getGameMode() != GameMode.CREATIVE) {
                for (PotionEffect pe : player.getActivePotionEffects()) {
                    //type and amplifier are good enough to distinguish this
                    if (pe.getType().equals(PE_NINJA_SPEED.getType()) && pe.getAmplifier() == PE_NINJA_SPEED.getAmplifier()) {
                        int remain = (int)Math.ceil((double)pe.getDuration()/20);
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', String.format("&cYou still have Ninja Speed for &e%d &cmore seconds!", remain)));
                        return;
                    }
                }
                //remove one item if they have 2 or more, else set their held item to air
                if (player.getItemInHand().getAmount() > 1) {
                    player.getItemInHand().setAmount(player.getItemInHand().getAmount()-1);
                } else {
                    player.setItemInHand(new ItemStack(Material.AIR));
                }
            }
         
            //if everything went okay, give them the effect and let them know about it
            player.addPotionEffect(PE_NINJA_SPEED);
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7You have activated Ninja speed!"));
        }
    }
    
    Cheers!

    Edit: @EventHandler
     
  3. Offline

    TCO_007

    Thanks so much Scizzr! I do have one problem though :(
    It says BukkitUtils cannot be resolved.
    Also, just a quick question, did you import a cooldown into it already? Im still getting used to reading new code haha. And is there a way for them to keep the quartz but not be able to use it for 45 seconds because what I wanted to do was when the player right clicks the quartz, It will give them the effect for 30 seconds and once its over, you cant use it again for 45 seconds. I cant tell if you implemented that or not. Sorry, I am a bit of a noob haha.
     
  4. Offline

    Scizzr

    Whoops... Sorry about that. Just remove that line unless you want it; in which case here are the methods in that class:
    Code:
    //Generic sender
    public static void msg(CommandSender sender, String msg) {
        msg = color(msg);
        sender.sendMessage(msg);
    }
     
    public static void msg(CommandSender sender, String msg, Object... args) {
        msg = color(String.format(msg, args));
        sender.sendMessage(msg);
    }
     
    //Player
    public static void msgPlayer(Player player, String msg) {
        msg(player, msg);
    }
     
    public static void msgPlayer(Player player, String msg, Object... args) {
        msg(player, msg, args);
    }
     
    //All players
    public static void msgPlayers(Player[] players, String msg) {
        for (Player player : players) {
            msg(player, msg);
        }
    }
     
    public static void msgPlayers(Player[] players, String msg, Object... args) {
        for (Player player : players) {
            msg(player, msg, args);
        }
    }
     
    //Console
    public static void msgConsole(String msg) {
        msg(Bukkit.getConsoleSender(), msg);
    }
     
    public static void msgConsole(String msg, Object... args) {
        msg(Bukkit.getConsoleSender(), msg, args);
    }
     
    //All players and Console
    public static void msgAll(String msg) {
        msgPlayers(Bukkit.getOnlinePlayers(), msg);
        msgConsole(msg);
    }
     
    public static void msgAll(String msg, Object... args) {
        msgPlayers(Bukkit.getOnlinePlayers(), msg, args);
        msgConsole(msg, args);
    }
     
    //Color
    public static String color(String in) {
        return in.replaceAll("&([0-9a-fkl-or]{1})", "ยง$1");
    }
    
    Just a quick way to send messages with color, and optionally format a string with String.format().

    Regarding the cooldown, the only cooldown I made was so that you don't give them the effect if they already have it. You can add another HashMap to keep track of that such as HashMap<String, Integer> where key is the player's name and value is the time they have left, and then add a scheduler to tick this down every second or every tick or something. I'd prefer ticks myself, then just multiple the cooldown by 20 to get the ticks. You said you want the cooldown to start after the effect ends, so you'd need to add the time of the effect and the cooldown then multiply by 20 - if the effect lasts 30 seconds and the cooldown is 45, that's 75 seconds; 75*20 = 1500 ticks.
     
Thread Status:
Not open for further replies.

Share This Page