Is my code correct?

Discussion in 'Plugin Development' started by Zazzony, Jul 24, 2013.

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

    Zazzony

    The code for my main class:
    Code:java
    1. import java.awt.List;
    2. import java.util.ArrayList;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Entity;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.Event;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.entity.EntityDamageEvent;
    11. import org.bukkit.event.entity.EntityEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16.  
    17. public class TNTsword extends JavaPlugin {
    18.  
    19.  
    20. public static Object TNTsword;
    21.  
    22. @Override
    23. public void onEnable() {
    24. getLogger().info("The TnT Sword PLugin has been enabled!");
    25.  
    26. TNTsword = null;
    27.  
    28. //in your onEnable
    29. //your usual stuff, and register a listener
    30. getServer().getPluginManager().registerEvents(new TNTListener(), this);
    31. //initialize TNTsword
    32. ItemStack TNTsword = new ItemStack(Material.IRON_SWORD, 1);
    33.  
    34.  
    35. ItemMeta tntmeta = TNTsword.getItemMeta();
    36. tntmeta.setDisplayName(ChatColor.RED + "TNT " + ChatColor.WHITE + "Sword");
    37.  
    38. ArrayList<String> lore = new ArrayList<String>();
    39. lore.add("Explodes on hit!");
    40. tntmeta.setLore(lore);
    41.  
    42. TNTsword.setItemMeta(tntmeta);
    43.  
    44.  
    45. Player sender = null;
    46. if(sender instanceof Player){
    47.  
    48. ((Player) sender).getInventory().addItem(TNTsword);
    49. return;
    50.  
    51. if(Event.getEntity() instanceof Player && Event.getCause() instanceof Player){
    52. if(((Player) Event.getCause()).getItemInHand().getType().equals(Material.IRON_SWORD)){
    53. ((Player) Event.getCause()).setItemInHand(new ItemStack(Material.AIR)); {}}}}
    54.  
    55. }
    56.  
    57. public void onDisable() {} {
    58. getLogger().info("The TnT Sword PLugin has been disabled!");
    59.  
    60.  
    61.  
    62.  
    63. }
    64.  
    65.  
    66.  
    67.  
    68. public Entity getEntity() {
    69. // TODO Auto-generated method stub
    70. return null;
    71. }}
    72.  


    This is my code for my TNTListener class:
    Code:java
    1. import org.bukkit.entity.Entity;
    2. import org.bukkit.entity.Player;
    3. import org.bukkit.event.Event;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    7. import org.bukkit.event.entity.EntityDamageEvent;
    8. import org.bukkit.event.entity.EntityEvent;
    9.  
    10.  
    11. public class TNTListener implements Listener {
    12.  
    13. private static final EntityDamageByEntityEvent e = null;
    14. @EventHandler
    15. public void onDamage(EntityDamageByEntityEvent e){
    16. if(e.getDamager() instanceof Player); }
    17. Player p = (Player) e.getDamager(); {
    18. //check if the item in hand is the sword from TNTClass
    19. if(p.getItemInHand().equals(TNTsword.TNTsword)); {
    20. Object e;
    21. //create explosion
    22. ((TNTsword) e).getEntity().getWorld().createExplosion(((TNTsword) e).getEntity().getLocation(), 0f);
    23.  
    24. Entity sender;
    25. Player target = sender.getWorld().getPlayer(args[0]);
    26. target.setHealth(0); }}}

    I know I have a few errors but I need your help to find them and fix them. Whats the point of this plugin you may ask? A person can create a TnT Sword (didn't add crafting yet) so when a player gets hit with the sword there becomes an explosion that is the power of 2 TnT. Thank you.
     
  2. Offline

    collielimabean

    Given the code as is, have you tested it? If it didn't work in the test server, what exceptions, if any, were raised/thrown? [i.e. if you got an exception, post its stack trace - it tells you where the problem is!]
     
  3. Offline

    Pawnguy7

    Well, to start, if I looked at that right, you have a sender argument in the onEnable method (?).

    Also, like... look at this:

    Code:java
    1.  
    2. private static final EntityDamageByEntityEvent e = null;
    3.  


    Why do you have this?
     
  4. Offline

    wreed12345

    Pawnguy7 that made me laugh because its final and null hahaah
     
    MuisYa likes this.
  5. Offline

    MuisYa

    For your information:

    Code:
    private static final EntityDamageByEntityEvent e = null;
    Is being laughed at, because:

    If you put final in front of your variable, you can't assign another value to this variable, this is handy in case you got a variable you don't want to be changed.
    You are assigning 'null' to the variable which is nothing, and you won't be able to change this anymore.
    That makes that you got a variable, that is null, that you can't change, which is pretty pointless...
     
  6. Offline

    Zazzony

    Yeah I messed up a bit. I understand that. That's why I need your help to fix the errors I have made. Sorry if I made a few stupid mistakes but people learn from trial and error. Please help me.

    Thank you.

    And again, it was a stupid accident.
     
Thread Status:
Not open for further replies.

Share This Page