[CODE]Good ol' guns

Discussion in 'Resources' started by Deckerz, Feb 23, 2013.

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

    Deckerz

    Hey guys,​
    today i bring you my simple gun code:​
    Features:​
    • the way it is coded allows you to make different weapons and let them do different amounts of damage
    • Also it allows you to use any items with a few changes :p
    • Also i have added a anti spam List so that you cant just keep left-clicking, to let them shoot faster just simply change the 30L
    Furture features:
    • reloading system
    • possibly scopes
    Hope you enjoy :D
    Code:java
    1.  
    2. List<String> recentlyFired = new ArrayList<String>();
    3. public void onEnable(){
    4. this.server = getServer();
    5. PluginManager pm = getServer().getPluginManager();
    6. pm.registerEvents(this, this);
    7. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    8. public void run()
    9. {
    10. recentlyFired.clear();
    11. }
    12.  
    13. }
    14. , 0L, 30L);
    15. }
    16. @EventHandler(priority = EventPriority.HIGHEST)
    17. public void onDamage(EntityDamageByEntityEvent e){
    18. if(e.getCause().equals(DamageCause.PROJECTILE)){
    19. if(e.getDamager().hasMetadata("shotfrom")){
    20. String shotFrom = e.getDamager().getMetadata("shotfrom").get(0).asString();
    21. if(shotFrom.equalsIgnoreCase("pistol")){
    22. if(e.getEntity() instanceof Player){
    23. Player player = (Player) e.getEntity();
    24. // example of using 'bullet proof vests'
    25. if(player.getEquipment().getChestplate().getType() == Material.LEATHER_CHESTPLATE){
    26. e.setDamage(5);
    27. }else if(player.getEquipment().getChestplate().getType() == Material.IRON_CHESTPLATE){
    28. e.setDamage(3);
    29. }else if(player.getEquipment().getChestplate().getType() == Material.GOLD_CHESTPLATE){
    30. e.setDamage(2);
    31. }else{
    32. e.setDamage(6);
    33. }
    34. }else{
    35. e.setDamage(6);
    36. }
    37. }
    38. }
    39. }
    40.  
    41. }
    42.  
    43. //begin interact event
    44. @EventHandler
    45. public void onInteract(final PlayerInteractEvent e){
    46. Player player = e.getPlayer();
    47. final PlayerInventory inventory = player.getInventory();
    48. if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK){
    49.  
    50. if(inventory.contains(Material.GLOWSTONE_DUST)){
    51. if(e.getPlayer().getItemInHand().getType() == Material.WOOD_HOE){
    52. if(!recentlyFired.contains(player.getName())){
    53. ItemStack item = new ItemStack(Material.GLOWSTONE_DUST, 1);
    54. ItemMeta reName = item.getItemMeta();
    55. reName.setDisplayName(ChatColor.RESET + "Ammo");
    56. List<String> lore = new ArrayList<String>();
    57. lore.add(ChatColor.RESET + "For use on:");
    58. lore.add(ChatColor.RESET + "- 9mm");
    59. reName.setLore(lore);
    60. item.setItemMeta(reName);
    61.  
    62. Arrow arrow = (Arrow)player.launchProjectile(Arrow.class);
    63. arrow.setVelocity(arrow.getVelocity().multiply(3));
    64. arrow.setShooter(player);
    65. arrow.setMetadata("shotfrom", new FixedMetadataValue(this, "pistol"));
    66. recentlyFired.add(player.getName());
    67. inventory.removeItem(item);
    68. }
    69. }
    70. }
    71.  
     
  2. Offline

    Omerrg

    The snowball launching is working but the damage doesn't change.. any idea why?
     
  3. Offline

    Deckerz

    mine shoots arrows.
     
  4. Offline

    Omerrg

    So arrows wouldn't work?
     
  5. Offline

    Deckerz

    No mine shoots ARROWS and it DOES work
     
  6. Offline

    Omerrg

    Im sorry, xD i was from iPhone and i couldn't undestand you. Thx. So i understand Arrows work, can you make one for snowballs too? i can't find out how to do those.
     
  7. Offline

    Deckerz

    just remove the check for projectile
     
  8. Why did you add at the end of getVelocity multiply(3)?
     
  9. Offline

    Deckerz

    so it will fly further
     
  10. Ok thx
     
  11. Offline

    RyGuy88228

    I really would like you to add Reloading system. Scopes would be simple, just add slowness when player left clicks with gun in hand.
     
  12. Offline

    Garris0n

    Why don't you just write a reloading system?
     
  13. Offline

    BungeeTheCookie

    Because some people do not know how to write a reloading system...
     
  14. Offline

    Garris0n

    Then some people should go learn instead of copy-pasting one from the internet.
     
    mine-care, AdamQpzm, bensku and 2 others like this.
  15. Offline

    RyGuy88228

    Umm, teach us maybe? -_-
     
  16. Offline

    Garris0n

  17. Offline

    BungeeTheCookie

    Some people probably do not want to take the time to learn how to. Maybe they are just too lazy to do it and copy and paste everything. Which means, they should not be on the Bukkit Forums. :D
     
  18. Offline

    Deckerz

    Ill work on a reloading system. havent been active recently :)
     
  19. Offline

    MineStein

    Deckerz Cool! I'm going to test this out later, got to work on plugins now.
     
  20. Offline

    Developerjohn

    People who are currently learning Java (like me) can stay. You don't have to force them to leave just because they don't know something. I do agree he should start learning instead of copying and pasting, but sometimes there are things you wont find anywhere else. So he could just ask a friend or someone on the forums for SOME help (not all). Even I used to copy and paste code from people so I could read it and message them on what it means. People learn differently, so please respect them. -Don't tell me to give you an example lol :cool:
     
  21. Offline

    xTrollxDudex

    This is the huge flaw with Resources. Some people think that it is a free for all spoon feed session (which it is - I admittedly have done it myself), but I now realize that you need to show people the way to do it, not do it for them (yes, back to class everyone, we all know this rule regarding our parents ;)) Developerjohn
     
    Garris0n and AdamQpzm like this.
  22. Offline

    BungeeTheCookie

    No, I'm just saying you should learn Java first before tackling Bukkit. If you try to learn Bukkit without knowing Java, you are going to have a very hard time learning it.
     
  23. Offline

    Deckerz

    Actually thats what i did :p

    But i now know java xD
     
  24. Offline

    TheUpdater

    use hashmaps to store both ammo and player in, and cooldown on gun as well
     
  25. Offline

    CeramicTitan

    Deckerz
    I have two problems with your bit of code.
    1. The formatting.
    2.
    Code:java
    1. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3. recentlyFired.clear();
    4. }
    5. } , 0L, 30L);
    6. }


    Repeating tasks are generally red flags unless done right. Having a repeating task like that isn't very efficient.
     
Thread Status:
Not open for further replies.

Share This Page