KitPvP Plugin Help 2

Discussion in 'Plugin Development' started by ztowne13, Mar 25, 2013.

  1. Offline

    ztowne13

    OMG This code wont work! the /kitpvp wont work aswell as /sharp and the eventhandler... Any idaes why?
    Code (Text):
    1.  
    2. package me.ztowne13.zPvP2;
    3.  
    4. import java.util.Map;
    5. import java.util.logging.Logger;
    6.  
    7.  
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Material;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.enchantments.Enchantment;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.entity.PlayerDeathEvent;
    17. import org.bukkit.event.player.PlayerJoinEvent;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.meta.ItemMeta;
    20. import org.bukkit.plugin.PluginDescriptionFile;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22. import org.bukkit.potion.PotionEffect;
    23. import org.bukkit.potion.PotionEffectType;
    24.  
    25. public class Main extends JavaPlugin{
    26.     public final Logger logger = Logger.getLogger("Minecraft");
    27.    
    28.     @Override
    29.     public void onEnable(){
    30.         PluginDescriptionFile pdfFile = this.getDescription();
    31.         this.logger.info(pdfFile.getName() + " " + "Version" + pdfFile.getVersion()  + " " + "has been enabled!");
    32.         getConfig().options().copyDefaults(true);
    33.         saveConfig();
    34.     }
    35.    
    36.     @Override
    37.     public void onDisable(){
    38.         PluginDescriptionFile pdfFile = this.getDescription();
    39.         this.logger.info(pdfFile.getName() + " " + "Version" + pdfFile.getVersion() + " " + "has been disabled!");
    40.         saveConfig();
    41.     }
    42.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    43.         Player player = (Player) sender;
    44.         ItemStack helmet = new ItemStack(Material.IRON_HELMET);
    45.         ItemStack chestplate = new ItemStack(Material.IRON_CHESTPLATE);
    46.         ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS);
    47.         ItemStack boots = new ItemStack(Material.IRON_BOOTS);
    48.         String version = this.getDescription().getVersion();
    49.         if(commandLabel.equalsIgnoreCase("KitPvP")){
    50.             if(args.length == 0){
    51.                 player.sendMessage(ChatColor.BLUE + "zPvP Version: " + version + " Author: Ztowne13");
    52.             }else if(args.length == 1 && args[0] == "help"){
    53.                 player.sendMessage(ChatColor.AQUA + "----" + ChatColor.BLUE +  " PvP Help 1 " + ChatColor.AQUA + "----");
    54.                 player.sendMessage(ChatColor.BLUE + "/PvP" + ChatColor.AQUA + " | Get The PvP Kit.");
    55.                 player.sendMessage(ChatColor.BLUE + "/Archer" + ChatColor.AQUA + " | Get The Archer Kit.");
    56.                 player.sendMessage(ChatColor.BLUE + "/Refill" + ChatColor.AQUA + " | Refill You Soup.");
    57.                 player.sendMessage(ChatColor.BLUE + "/Diamond" + ChatColor.AQUA + " | Get Full Diamond.");
    58.                 player.sendMessage(ChatColor.BLUE + "/Speed" + ChatColor.AQUA + " | Get Speed II.");
    59.                 player.sendMessage(ChatColor.BLUE + "/Sharp" + ChatColor.AQUA + " | Get Sharpness On Your Sword.");
    60.                 player.sendMessage(ChatColor.BLUE + "/Power" + ChatColor.AQUA + " | Get Power On Your Bow.");
    61.                 //10
    62.                 player.sendMessage(ChatColor.AQUA + "----" + ChatColor.BLUE + " /PvP Help 2 For More " + ChatColor.AQUA + "----");
    63.             }else if(args.length == 3){
    64.                 if(args[1] == "1"){
    65.                     player.sendMessage(ChatColor.AQUA + "----" + ChatColor.BLUE +  " PvP Help 2 " + ChatColor.AQUA + "----");
    66.                 }
    67.             }else{
    68.                 player.sendMessage(ChatColor.RED + "Too Many Arguments.");
    69.             }
    70.         }
    71.         if(commandLabel.equalsIgnoreCase("PvP")){
    72.             if(player.hasPermission("KitPvP.PvP")){
    73.                 ItemStack mushroom = new ItemStack(Material.MUSHROOM_SOUP);
    74.                 ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    75.                 player.getInventory().clear();
    76.                 player.getInventory().addItem(sword);
    77.                 player.getInventory().addItem(mushroom);
    78.                 player.getInventory().addItem(mushroom);
    79.                 player.getInventory().addItem(mushroom);
    80.                 player.getInventory().addItem(mushroom);
    81.                 player.getInventory().addItem(mushroom);
    82.                 player.getInventory().addItem(mushroom);
    83.                 player.getInventory().addItem(mushroom);
    84.                 player.getInventory().addItem(mushroom);
    85.                 player.getInventory().addItem(mushroom);
    86.                 player.getInventory().addItem(mushroom);
    87.                 player.getInventory().addItem(mushroom);
    88.                 player.getInventory().addItem(mushroom);
    89.                 player.getInventory().addItem(mushroom);
    90.                 player.getInventory().addItem(mushroom);
    91.                 player.getInventory().addItem(mushroom);
    92.                 player.getInventory().addItem(mushroom);
    93.                 player.getInventory().setHelmet(helmet);
    94.                 player.getInventory().setChestplate(chestplate);
    95.                 player.getInventory().setLeggings(leggings);
    96.                 player.getInventory().setBoots(boots);
    97.                 player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 18000, 0));
    98.             }else{
    99.                 player.sendMessage(ChatColor.RED + "Insufficient Permissions To Use /PvP");
    100.             }
    101.         }
    102.         if(commandLabel.equalsIgnoreCase("Archer")){
    103.             if(player.hasPermission("KitPvP.Archer")){
    104.                 ItemStack mushroom = new ItemStack(Material.MUSHROOM_SOUP);
    105.                 ItemStack bow = new ItemStack(Material.BOW);
    106.                 ItemStack arrow = new ItemStack(Material.ARROW);
    107.                 ItemMeta meta = bow.getItemMeta();
    108.                 meta.addEnchant(Enchantment.ARROW_INFINITE, 1 , true);
    109.                 bow.setItemMeta(meta);
    110.                 player.getInventory().clear();
    111.                 player.getInventory().addItem(bow);
    112.                 player.getInventory().addItem(mushroom);
    113.                 player.getInventory().addItem(mushroom);
    114.                 player.getInventory().addItem(mushroom);
    115.                 player.getInventory().addItem(mushroom);
    116.                 player.getInventory().addItem(mushroom);
    117.                 player.getInventory().addItem(mushroom);
    118.                 player.getInventory().addItem(mushroom);
    119.                 player.getInventory().addItem(mushroom);
    120.                 player.getInventory().addItem(mushroom);
    121.                 player.getInventory().addItem(mushroom);
    122.                 player.getInventory().addItem(mushroom);
    123.                 player.getInventory().addItem(mushroom);
    124.                 player.getInventory().addItem(mushroom);
    125.                 player.getInventory().addItem(mushroom);
    126.                 player.getInventory().addItem(mushroom);
    127.                 player.getInventory().addItem(mushroom);
    128.                 player.getInventory().addItem(arrow);
    129.                 player.getInventory().setHelmet(helmet);
    130.                 player.getInventory().setChestplate(chestplate);
    131.                 player.getInventory().setLeggings(leggings);
    132.                 player.getInventory().setBoots(boots);
    133.                 player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 18000, 0));
    134.             }else{
    135.                 player.sendMessage(ChatColor.RED + "Insufficient Permissions To Use /Archer");
    136.             }
    137.         }
    138.         if(commandLabel.equalsIgnoreCase("Diamond")){
    139.             if(player.hasPermission("KitPvP.Diamond")){
    140.                 ItemStack dhelm = new ItemStack(Material.DIAMOND_HELMET);
    141.                 ItemStack dchest = new ItemStack(Material.DIAMOND_CHESTPLATE);
    142.                 ItemStack dleg = new ItemStack(Material.DIAMOND_LEGGINGS);
    143.                 ItemStack dboot = new ItemStack(Material.DIAMOND_BOOTS);
    144.                 player.getInventory().setHelmet(dhelm);
    145.                 player.getInventory().setChestplate(dchest);
    146.                 player.getInventory().setLeggings(dleg);
    147.                 player.getInventory().setBoots(dboot);
    148.             }else{
    149.                 player.sendMessage(ChatColor.RED + "Insufficient Permissions To Use /Diamond");
    150.             }
    151.         }
    152.         if(commandLabel.equalsIgnoreCase("Speed")){
    153.             if(player.hasPermission("KitPvP.Speed")){
    154.                 player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 2400, 1));
    155.             }else{
    156.                 player.sendMessage(ChatColor.RED + "Insufficient Permissions To Use /Speed");
    157.             }
    158.         }
    159.         if(commandLabel.equalsIgnoreCase("Strength")){
    160.             if(player.hasPermission("KitPvP.Strength")){
    161.                 player.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
    162.                 player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 600, 3));
    163.             }else{
    164.                 player.sendMessage(ChatColor.RED + "Insufficient Permissions To Use /Strength");
    165.             }
    166.         }
    167.         if(commandLabel.equalsIgnoreCase("Sharp")){
    168.             Map<Enchantment, Integer> Enchants = player.getInventory().getItemInHand().getEnchantments();
    169.             Integer level = Enchants.get(Enchantment.DAMAGE_ALL);
    170.             if(player.hasPermission("KitPvP.Sharp")){
    171.                 if(player.getItemInHand().getType() == Material.DIAMOND_SWORD){
    172.                     if(player.getItemInHand().containsEnchantment(Enchantment.DAMAGE_ALL)){
    173.                         if(level == 1){
    174.                             player.getItemInHand().addEnchantment(Enchantment.DAMAGE_ALL, 2);
    175.                         }else if(level == 2){
    176.                             player.getItemInHand().addEnchantment(Enchantment.DAMAGE_ALL, 3);
    177.                         }else if (level == 0){
    178.                             player.getItemInHand().addEnchantment(Enchantment.DAMAGE_ALL, 1);
    179.                         }else{
    180.                             player.sendMessage(ChatColor.RED + "This Sword Has Maximized Its Enchantments!");
    181.                         }
    182.                     }
    183.                 }else{
    184.                     player.sendMessage(ChatColor.RED + "Please Hold A Diamond Sword");
    185.                 }
    186.             }else{
    187.                 player.sendMessage(ChatColor.RED + "Insufficient Permissions To Use /Sharp");
    188.             }
    189.         }
    190.         if(commandLabel.equalsIgnoreCase("Power")){
    191.             Map<Enchantment, Integer> Enchants = player.getInventory().getItemInHand().getEnchantments();
    192.             Integer level = Enchants.get(Enchantment.ARROW_DAMAGE);
    193.             if(player.hasPermission("KitPvP.Power")){
    194.                 if(player.getItemInHand().getType() == Material.BOW){
    195.                     if(player.getItemInHand().containsEnchantment(Enchantment.ARROW_DAMAGE)){
    196.                         if(level == 1){
    197.                             player.getItemInHand().addEnchantment(Enchantment.ARROW_DAMAGE, 2);
    198.                         }else if(level == 2){
    199.                             player.getItemInHand().addEnchantment(Enchantment.ARROW_DAMAGE, 3);
    200.                         }else if(level == 0){
    201.                             player.getItemInHand().addEnchantment(Enchantment.ARROW_DAMAGE, 1);
    202.                         }else{
    203.                             player.sendMessage(ChatColor.RED + "This Bow Has Maximized Its Enchantments!");
    204.                         }
    205.                     }
    206.                 }else{
    207.                     player.sendMessage(ChatColor.RED + "Please Hold A Bow");
    208.                 }
    209.             }else{
    210.                 player.sendMessage(ChatColor.RED + "Insufficient Permissions To Use /Power");
    211.             }
    212.         }
    213.         return false;
    214.     }
    215.     @EventHandler
    216.     public void onPlayerDeath(PlayerDeathEvent e){
    217.         Player player = (Player) e;
    218.         Player killer = e.getEntity().getKiller();
    219.         e.setDroppedExp(0);
    220.         e.getDrops().clear();
    221.         killer.sendMessage(ChatColor.RED + "You Recieved 15 Credits!");
    222.         player.sendMessage(ChatColor.RED + "You Killer Recieved 15 Credits");
    223.     }
    224.     @EventHandler
    225.     public void onPlayerJoin(PlayerJoinEvent e){
    226.         Player player = (Player) e;
    227.         player.sendMessage(ChatColor.RED + "Welcome To KitPvP!");
    228.     }
    229. }
    230.  
    YES THIS IS DIFFERENT FROM MY OTHER POST...
     
  2. Offline

    skipperguy12

    Dude! Just edit your post!! Read what I said on #1, you're not getting help because :

    A) You're not giving us errors
    B) Broken means nothing
    C) You're talking about nonexistant suff, eventhandler, what evernthandler!
     
  3. Offline

    Phinary

  4. Also, do NOT assume onCommand is only sent by players. Consoles can also send commands and it will go through the onCommand, therefore do not directly cast Player to sender without checks.
     
  5. At least it is not so hard to specify what he is doing wrong:
    • If you have @EventHandlers, the class should implement Listener. In your case
      Code (Text):
      1. public class Main extends JavaPlugin implements Listener
    • You have to register the Listener you just declared
      Code (Text):
      1. Bukkit.getPluginManager().registerEvents(this, this);
    • You cannot cast PlayerEvents to Player (the '(Player) e'). To get the Player object, use e.getPlayer();
    • .getKiller() can return null, check if(killer != null) before using it
    • Don't blindly cast CommandSender to Player, it could be a ConsoleCommandSender or BlockCommandSender. This would throw a ClassCastException. So use this:
      Code (Text):
      1. if(sender instanceof Player)
      2. {
      3.     Player player = (Player) sender;
      4.     // Continue code
      5. }
    • Compare Strings using either .equals(otherString) or .equalsIgnoreCase(otherString) (latter for ignoring case, duh :p). Using == is a reference comparison, and Strings do not share objects by default (afaik)
    • You check for args.length == 3 while only using the second argument. Not an error, but might give unwanted results
    • You might want to return true on success
    • Also, the reason the sharp command does not work is because you need to have an enchant on it to work (as level 0 does not exist afaik, enchantments start at level 1)
     
  6. Offline

    ztowne13

    Thankyou for all that helped i really apreciate it and sorry for just asking "help." There were no errors or anything thats why i just didnt know why nothing happened
     
  7. For soup, make it easier on yourself by doing a loop.

    Code:java
    1.  
    2. for (int i = 0; i < 17; i++)
    3. {
    4. player.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP));
     
  8. Offline

    ztowne13

    Ok ill look into that... Thanks. I do remember learning this
     

Share This Page