[Tutorial] - Create a Kit Plugin

Discussion in 'Resources' started by Retherz_, Sep 3, 2013.

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

    Retherz_

    #1 Soup
    Code:java
    1.  
    2.  
    3. @EventHandler
    4. public void onPlayerInteract(PlayerInteractEvent event) {
    5. if (event.getAction() == Action.RIGHT_CLICK_AIR
    6. || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    7. Player p = event.getPlayer();
    8. if(p.getHealth() != 20){
    9. player.setHealth((player.getHealth() + 7 > player.getMaxHealth() ? player.getMaxHealth() : player.getHealth() + 7))
    10. event.getPlayer().getItemInHand().setType(Material.BOWL);
    11.  
    12. }
    13. }
    14. }
    15.  
    16.  
    17. }

    #2 Clearing Inventories & Removing potion effect
    Code:java
    1.  
    2.  
    3. public static void InventoryClear(Player p) {
    4. p.getInventory().setArmorContents(null));
    5. p.getInventory().clear();
    6. for (PotionEffect effect : p.getActivePotionEffects())
    7. p.removePotionEffect(effect.getType());
    8. }
    9. }

    #3 Giving Soup
    Code:java
    1.  
    2.  
    3. public static void SoupFill(Player p) {
    4. for (int i = 0; i < 32; i++){
    5. p.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP);
    6. }
    7. }
    8. }

    #4 Deaths and Kills
    Code:java
    1.  
    2.  
    3.  
    4. @EventHandler
    5. public void playerDeathKill(PlayerDeathEvent event) {
    6. event.setDeathMessage("");
    7. event.getDrops().clear();
    8. event.setDroppedExp(0);
    9. Player killer = (Player) event.getEntity().getKiller();
    10. final Player p = (Player) event.getEntity();
    11. p.setLevel(0);
    12. ClassBoard.resetScore(p);
    13.  
    14. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    15. public void run() {
    16. Packet205ClientCommand packet = new Packet205ClientCommand();
    17. packet.a = 1;
    18. ((CraftPlayer) p).getHandle().playerConnection.a(packet);
    19. }
    20. }, 5L);
    21. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,
    22. new Runnable() {
    23. public void run() {
    24. p.teleport(Bukkit.getWorld("<worldname>, x, y, z");
    25.  
    26. }
    27. }, 7L);
    28. }
    29. }
    30. }
    31. //Giving players money, requieres Vault
    32. if (event.getEntity().getKiller() instanceof Player) {
    33.  
    34. Player killer = event.getEntity().getKiller();
    35. ClassBoard.killstreakAdd(killer);
    36. p.sendMessage(Main.tag + "§4 You were slain by "
    37. + killer.getDisplayName());
    38. killer.sendMessage(Main.tag + "§a You have slain §e"
    39. + p.getDisplayName());
    40. killer.playSound(p.getLocation(), Sound.LEVEL_UP, 5F, 2F);
    41.  
    42. if (p != killer) {
    43. if (killer.hasPermission("mkit.donator")) {
    44. Main.economy.depositPlayer(killer.getName(), 15);
    45. killer.sendMessage(ChatColor.GREEN
    46. + "You have been awarded §6$15 §afor killing "
    47. + ChatColor.RED + p.getName()
    48. + "§a. Your balance is now §6$"
    49. + Main.economy.getBalance(killer.getName()));
    50.  
    51. } else {
    52. Main.economy.depositPlayer(killer.getName(), 10);
    53. killer.sendMessage(ChatColor.GREEN
    54. + "You have been awarded §6$10 §afor killing "
    55. + ChatColor.RED + p.getName()
    56. + "§a. Your balance is now §6$"
    57. + Main.economy.getBalance(killer.getName()));
    58.  
    59. }
    60. } else {
    61. Main.economy.withdrawPlayer(killer.getName(), 30);
    62. killer.sendMessage(ChatColor.RED
    63. + "You have lost §6$30 §cfor suicide "
    64. + "§a. Your balance is now §6$"
    65. + Main.economy.getBalance(killer.getName()));
    66. }
    67.  
    68. } else {
    69. return;
    70. }
    71.  
    72. }

    #5 Signs & Kits
    Code:java
    1.  
    2. @EventHandler
    3. public void onSignJoin(PlayerInteractEvent e) {
    4. final Player p = e.getPlayer();
    5.  
    6. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    7.  
    8. if (e.getClickedBlock().getState() instanceof Sign) {
    9.  
    10. Sign s = (Sign) e.getClickedBlock().getState();
    11.  
    12. s.update();
    13.  
    14. if (s.getLine(0).contains("[Kit]")) {
    15.  
    16. // Archer
    17. if (s.getLine(1).contains("Archer")) {
    18. InventoryClear(p);
    19.  
    20. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,
    21. new Runnable() {
    22. public void run() {
    23. archer(p);
    24. SoupFill(p);
    25. p.sendMessage(Main.tag
    26. + " §eYou are now an Archer!");
    27.  
    28. }
    29. }, 10L);
    30. }
    31.  
    32. // Knight
    33.  
    34. if (s.getLine(1).contains("Knight")) {
    35. InventoryClear(p);
    36.  
    37. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,
    38. new Runnable() {
    39. public void run() {
    40. knight(p);
    41. SoupFill(p);
    42. p.sendMessage(Main.tag
    43. + " §eYou are now a Knight!");
    44.  
    45. }
    46. }, 10L);
    47.  
    48. }
    49.  
    50. }
    51.  
    52. }
    53. }
    54.  
    55. public void knight(Player p) {
    56.  
    57. // Armor
    58.  
    59. // Boots
    60. ItemStack boots = new ItemStack(309, 1);
    61. // Leggings
    62. ItemStack leggings = new ItemStack(308, 1);
    63. // Chestplate
    64. ItemStack chestplate = new ItemStack(307, 1);
    65. // Helmet
    66. ItemStack helmet = new ItemStack(306, 1);
    67.  
    68. // Weps
    69.  
    70. // Sword
    71. ItemStack sword = new ItemStack(276, 1);
    72. sword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
    73.  
    74. p.getInventory().setBoots(boots);
    75. p.getInventory().setLeggings(leggings);
    76. p.getInventory().setChestplate(chestplate);
    77. p.getInventory().setHelmet(helmet);
    78.  
    79. p.getInventory().addItem(sword);
    80.  
    81. }
    82.  
    83. public void archer(Player p) {
    84.  
    85. // Armor
    86.  
    87. // Boots
    88. ItemStack boots = new ItemStack(305, 1);
    89. // Leggings
    90. ItemStack leggings = new ItemStack(304, 1);
    91. // Chestplate
    92. ItemStack chestplate = new ItemStack(303, 1);
    93. // Helmet
    94. ItemStack helmet = new ItemStack(302, 1);
    95.  
    96. // Bow
    97. ItemStack bow = new ItemStack(261, 1);
    98. bow.addEnchantment(Enchantment.ARROW_INFINITE, 1);
    99. bow.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 4);
    100.  
    101. p.getInventory().setBoots(boots);
    102. p.getInventory().setLeggings(leggings);
    103. p.getInventory().setChestplate(chestplate);
    104. p.getInventory().setHelmet(helmet);
    105.  
    106. p.getInventory().addItem(bow);
    107.  
    108. p.getInventory().addItem(new ItemStack(262, 1));
    109.  
    110. }
    111.  
     
    KaptanAykin likes this.
  2. Offline

    chasechocolate

    For #1 you can use: player.setHealth((player.getHealth() + 7 > player.getMaxHealth() ? player.getMaxHealth() : player.getHealth() + 7)). For #3, just use a for-loop.
     
    hawkfalcon likes this.
  3. Offline

    viper_monster

    You could use this:
    Code:java
    1. p.getInventory().setArmorContents(null);
    2. p.getInventory().clear();
    instead of this:
    Code:java
    1. p.getInventory().setBoots(new ItemStack(0, 0));
    2. p.getInventory().setLeggings(new ItemStack(0, 0));
    3. p.getInventory().setChestplate(new ItemStack(0, 0));
    4. p.getInventory().setHelmet(new ItemStack(0, 0));
    5. p.getInventory().clear();


    and the "giving soup" part you can replace with :
    Code:java
    1. for (int i = 0; i < 32; i++) {
    2. ItemStack stew = new ItemStack(Material.MUSHROOM_SOUP);
    3. p.getInventory().addItem(stew);
    4. }


    MercilessPvP, forgot to "Tahg" you :p

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

    Retherz_

  5. If you do use .setHealth() higher than the max health, I'm pretty sure it automatically limits it to the max health? In my KingKits soup feature, I just player.setHealth(player.getHealth() + 6) and once it reaches 20, it stops.
     
  6. Offline

    ZeusAllMighty11

    KingFaris11

    It will throw an error if you set the health higher than max health.

    With @chasechocolate's statement, it's basically using a ternary operator for simple compact code. A ternary operator is basically a single liner if/else statement

    so
    ((player.getHealth() + 7 > player.getMaxHealth() ? player.getMaxHealth() : player.getHealth() + 7))

    if player.getHealth() + 7 is greater than the max health ... set health to max health.. else set health to health + 7
     
  7. Sorry, I know about that bit I just never knew it throws an error if it goes higher than the max health.

    Oh I know why, I have it in a try and catch method, and I never print the exception. It probably throws loads of errors.

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

Share This Page