Solved Why is this not working?

Discussion in 'Plugin Development' started by PieMan456, Dec 1, 2013.

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

    PieMan456

    Hello Everyone,

    I am making a plugin and when I went to test it none of my events were firing. I put debug lines in but none of them showed up. I checked to see if I registered my events and I did. I even checked at other projects of mine but they looked exactly the same. I have no idea what I am doing wrong. Here is my class:
    Code:java
    1. package me.pieman.eggs;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import net.milkbowl.vault.economy.Economy;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Location;
    10. import org.bukkit.Material;
    11. import org.bukkit.block.Block;
    12. import org.bukkit.block.Sign;
    13. import org.bukkit.entity.Egg;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.entity.Projectile;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.block.Action;
    19. import org.bukkit.event.block.SignChangeEvent;
    20. import org.bukkit.event.entity.ProjectileHitEvent;
    21. import org.bukkit.event.inventory.InventoryClickEvent;
    22. import org.bukkit.event.player.PlayerInteractEvent;
    23. import org.bukkit.event.player.PlayerMoveEvent;
    24. import org.bukkit.inventory.Inventory;
    25. import org.bukkit.inventory.ItemStack;
    26. import org.bukkit.inventory.meta.ItemMeta;
    27. import org.bukkit.plugin.RegisteredServiceProvider;
    28. import org.bukkit.plugin.java.JavaPlugin;
    29.  
    30. public class Eggs extends JavaPlugin implements Listener {
    31.  
    32. public Eggs plugin;
    33.  
    34. public ArrayList<String> player = new ArrayList<String>();
    35.  
    36. public ArrayList<Location> mine = new ArrayList<Location>();
    37.  
    38. public ArrayList<Location> bomb = new ArrayList<Location>();
    39.  
    40. public static Economy econ = null;
    41.  
    42. public void onEnable(){
    43. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    44. if (!setupEconomy() ) {
    45. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    46. getServer().getPluginManager().disablePlugin(this);
    47. return;
    48. }
    49. }
    50.  
    51. private boolean setupEconomy() {
    52. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    53. return false;
    54. }
    55. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    56. if (rsp == null) {
    57. return false;
    58. }
    59. econ = rsp.getProvider();
    60. return econ != null;
    61. }
    62.  
    63. @EventHandler
    64. public void onProjectileHit(ProjectileHitEvent e){
    65. Projectile p = e.getEntity();
    66. if(!(p instanceof Egg)) return;
    67. Egg egg = (Egg) p;
    68. if(egg.hasMetadata("grenade")){
    69. double eggx = egg.getLocation().getX();
    70. double eggy = egg.getLocation().getY();
    71. double eggz = egg.getLocation().getZ();
    72. egg.getWorld().createExplosion(eggx, eggy, eggz, 4F, false, false);
    73. return;
    74. }
    75. if(egg.hasMetadata("landmine")){
    76. Player shooter = (Player) egg.getShooter();
    77. shooter.sendMessage(ChatColor.GREEN + "LandMine placed!");
    78. Block b = egg.getLocation().getBlock();
    79. mine.add(b.getLocation());
    80. return;
    81. }
    82. if(egg.hasMetadata("bomb")){
    83. final Player shooter = (Player) egg.getShooter();
    84. final Block b = egg.getLocation().getBlock();
    85. bomb.add(b.getLocation());
    86. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    87. public void run(){
    88. if(player.contains(shooter.getName())){
    89. b.getWorld().createExplosion(b.getX(), b.getY(), b.getZ(), 4F, true, true);
    90. bomb.remove(b.getLocation());
    91. Bukkit.getServer().getScheduler().cancelTasks(plugin);
    92. return;
    93. }
    94. shooter.sendMessage(ChatColor.GREEN + "Bomb is going off in 10 seconds!");
    95. player.add(shooter.getName());
    96. }
    97. }, 20 * 10, 20 * 10);
    98. }
    99. }
    100.  
    101. @EventHandler
    102. public void onPlayerMove(PlayerMoveEvent e){
    103. Player p = e.getPlayer();
    104. ArrayList<Location> mine = this.mine;
    105. for(Location mloc : mine){
    106. Location ploc = p.getLocation();
    107. if(ploc.getX() == mloc.getX() && ploc.getY() == mloc.getY() && ploc.getX() == mloc.getX()){
    108. mloc.getWorld().createExplosion(ploc.getX(), ploc.getY(), ploc.getZ(), 4F, false, false);
    109. mine.remove(mloc);
    110. }
    111. }
    112. }
    113.  
    114. @SuppressWarnings("deprecation")
    115. public void openInventory(Player p){
    116. Inventory inv = Bukkit.createInventory(null, 9, "Bomb Factory");
    117.  
    118. ItemStack grenade = new ItemStack(Material.EGG);
    119. ItemMeta gmeta = grenade.getItemMeta();
    120. ArrayList<String> g = new ArrayList<String>();
    121. g.add(ChatColor.DARK_AQUA + "Cost: " + getConfig().getDouble("grenadeprice"));
    122. gmeta.setLore(g);
    123. gmeta.setDisplayName(ChatColor.GREEN + "Grenade");
    124. grenade.setItemMeta(gmeta);
    125.  
    126. ItemStack bomb = new ItemStack(Material.EGG);
    127. ItemMeta bmeta = bomb.getItemMeta();
    128. ArrayList<String> b = new ArrayList<String>();
    129. b.add(ChatColor.DARK_AQUA + "Cost: " + getConfig().getDouble("bombprice"));
    130. bmeta.setLore(b);
    131. bmeta.setDisplayName(ChatColor.GREEN + "Bomb");
    132. bomb.setItemMeta(bmeta);
    133.  
    134. ItemStack landmine = new ItemStack(Material.EGG);
    135. ItemMeta lmeta = landmine.getItemMeta();
    136. ArrayList<String> l = new ArrayList<String>();
    137. l.add(ChatColor.DARK_AQUA + "Cost: " + getConfig().getDouble("landmineprice"));
    138. lmeta.setLore(l);
    139. lmeta.setDisplayName(ChatColor.GREEN + "LandMine");
    140. landmine.setItemMeta(lmeta);
    141.  
    142. ItemStack wirecutters = new ItemStack(Material.FLINT_AND_STEEL);
    143. ItemMeta wmeta = wirecutters.getItemMeta();
    144. ArrayList<String> w = new ArrayList<String>();
    145. w.add(ChatColor.DARK_AQUA + "Cost: " + getConfig().getDouble("wirecuttersprice"));
    146. wmeta.setLore(w);
    147. wmeta.setDisplayName(ChatColor.GREEN + "WireCutters");
    148. wirecutters.setItemMeta(wmeta);
    149.  
    150. ItemStack money = new ItemStack(Material.DIAMOND);
    151. ItemMeta mmeta = money.getItemMeta();
    152. mmeta.setDisplayName(ChatColor.DARK_PURPLE + "Balance: " + econ.getBalance(p.getName()));
    153. money.setItemMeta(mmeta);
    154.  
    155. p.openInventory(inv);
    156.  
    157. inv.clear();
    158. inv.setItem(0, new ItemStack(grenade));
    159. inv.setItem(2, new ItemStack(bomb));
    160. inv.setItem(4, new ItemStack(money));
    161. inv.setItem(6, new ItemStack(landmine));
    162. inv.setItem(8, new ItemStack(wirecutters));
    163.  
    164. p.updateInventory();
    165. }
    166.  
    167. @EventHandler
    168. public void onInventoryClick(InventoryClickEvent e){
    169. if(!e.getInventory().getName().equalsIgnoreCase("Bomb Factory")) return;
    170. if(!(e.getWhoClicked() instanceof Player)) return;
    171. Player p = (Player) e.getWhoClicked();
    172. if (p.getInventory().firstEmpty() == -1) {
    173. p.sendMessage(ChatColor.RED + "Your inventory is full!");
    174. return;
    175. }
    176. if(e.getCurrentItem().getType() == Material.DIAMOND){
    177. e.setCancelled(true);
    178. }
    179. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Grenade")){
    180. e.setCancelled(true);
    181. if(econ.getBalance(p.getName()) < getConfig().getDouble("grenadeprice")){
    182. p.sendMessage(ChatColor.RED + "You do not have enough money!");
    183. return;
    184. } else {
    185. ItemStack grenade = new ItemStack(Material.EGG);
    186. ItemMeta gmeta = grenade.getItemMeta();
    187. gmeta.setDisplayName(ChatColor.GREEN + "Grenade");
    188. grenade.setItemMeta(gmeta);
    189. p.getInventory().addItem(new ItemStack(grenade));
    190. econ.withdrawPlayer(p.getName(), getConfig().getDouble("grenadeprice"));
    191. }
    192. }
    193. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Bomb")){
    194. e.setCancelled(true);
    195. if(econ.getBalance(p.getName()) < getConfig().getDouble("bombprice")){
    196. p.sendMessage(ChatColor.RED + "You do not have enough money!");
    197. return;
    198. } else {
    199. ItemStack bomb = new ItemStack(Material.EGG);
    200. ItemMeta bmeta = bomb.getItemMeta();
    201. bmeta.setDisplayName(ChatColor.GREEN + "Bomb");
    202. bomb.setItemMeta(bmeta);
    203. p.getInventory().addItem(new ItemStack(bomb));
    204. econ.withdrawPlayer(p.getName(), getConfig().getDouble("bombprice"));
    205. }
    206. }
    207. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "LandMine")){
    208. e.setCancelled(true);
    209. if(econ.getBalance(p.getName()) < getConfig().getDouble("landmineprice")){
    210. p.sendMessage(ChatColor.RED + "You do not have enough money!");
    211. return;
    212. } else {
    213. ItemStack landmine = new ItemStack(Material.EGG);
    214. ItemMeta lmeta = landmine.getItemMeta();
    215. lmeta.setDisplayName(ChatColor.GREEN + "LandMine");
    216. landmine.setItemMeta(lmeta);
    217. p.getInventory().addItem(new ItemStack(landmine));
    218. econ.withdrawPlayer(p.getName(), getConfig().getDouble("landmineprice"));
    219. }
    220. }
    221. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "WireCutters")){
    222. e.setCancelled(true);
    223. if(econ.getBalance(p.getName()) < getConfig().getDouble("wirecuttersprice")){
    224. p.sendMessage(ChatColor.RED + "You do not have enough money!");
    225. return;
    226. } else {
    227. ItemStack wirecutters = new ItemStack(Material.FLINT_AND_STEEL);
    228. ItemMeta wmeta = wirecutters.getItemMeta();
    229. wmeta.setDisplayName(ChatColor.GREEN + "WireCutters");
    230. wirecutters.setItemMeta(wmeta);
    231. p.getInventory().addItem(new ItemStack(wirecutters));
    232. econ.withdrawPlayer(p.getName(), getConfig().getDouble("landmineprice"));
    233. }
    234. }
    235. }
    236.  
    237. @EventHandler
    238. public void onSignChangeEvent(SignChangeEvent e){
    239. if(!(e.getLines().length >= 1) && !e.getLine(0).equalsIgnoreCase("Bomb Factory")) return;
    240. e.setLine(0, ChatColor.GREEN + "Bomb Factory");
    241. }
    242.  
    243. @EventHandler
    244. public void onPlayerInteract(PlayerInteractEvent e){
    245. System.out.println("Working");
    246. Block b = e.getClickedBlock();
    247. if(e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
    248. if(bomb.contains(b.getLocation())){
    249. Player p = e.getPlayer();
    250. if(p.getItemInHand().getType() == Material.FLINT_AND_STEEL){
    251. p.sendMessage(ChatColor.GREEN + "You have successfully defused the bomb!");
    252. Bukkit.getServer().getScheduler().cancelTasks(plugin);
    253. bomb.remove(b.getLocation());
    254. return;
    255. }
    256. }
    257. if(e.getClickedBlock().getType() != Material.SIGN && e.getClickedBlock().getType() != Material.WALL_SIGN
    258. && e.getClickedBlock().getType() != Material.SIGN_POST) return;
    259. System.out.println("Still Working");
    260. Sign s = (Sign) e.getClickedBlock().getState();
    261. if(s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + "Bomb Factory")){
    262. System.out.println("Works!");
    263. Player p = (Player) e.getPlayer();
    264. openInventory(p);
    265. }
    266. }
    267. }
    268.  
     
  2. Offline

    BajanAmerican

    Did you make sure you exported the plugin right and you have a plugin.yml? I honestly don't know, everything looks fine to me..
     
  3. Offline

    PieMan456

    BajanAmerican
    Yes the plugin.yml is correct and I export it the same way it do to very one of my plugins.
     
  4. Offline

    fireblast709

    PieMan456 Is Vault enabled, did you have
    Code:
    depend: [Vault]
    in plugin.yml
     
  5. Offline

    PieMan456

    fireblast709
    No I don't have that. Let me see if it works now.
     
  6. Offline

    AoH_Ruthless

    PieMan456
    The only thing I see wrong with your code is invalid operands. For example, you use == in line 176 where you should be using .equals()

    == compares two non-primitive references,
    .equals() can be overriden.

    EDIT: There could be other instances where you used the wrong one, I only noticed the one I pointed out ;)
     
  7. Offline

    PieMan456

    AoH_Ruthless
    I pretty much just copied that small piece of code from another plugin that worked perfectly fine with it.
     
  8. Offline

    AoH_Ruthless

    PieMan456
    Really? For me using == is iffy, but maybe there was something else wrong with my code at the time :confused:

    EDIT: Wait sorry, it's the other way around. == is the right method for a material name. I was thinking of the wrong thing.
     
  9. Offline

    PieMan456

    fireblast709
    Ok so I added depend: [Vault] and I put vault in the plugins but now it says it is disabled because there is not vault when vault is in the plugins folder.

    AoH_Ruthless
    Do you know?(Look up at last post)

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

    AoH_Ruthless

    PieMan456
    I would add the logger statements in the setupEconomy() constructor, and then in your onEnable just do setupEconomy();
     
  11. When you do depend: Vault, you are saying your plugin requires vault to run. If that is so then the user would require vault to run. If you do not require Vault, but have optional implementation of it, change the line to: softDepend: Vault

    It is these lines of code that are disabling your plugin when there is no vault
    Code:
    public void onEnable(){
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    if (!setupEconomy() ) {
    getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    getServer().getPluginManager().disablePlugin(this);
    return;
    }
    }
    
    You are saying if setupEconomy returns false, Disable the plugin.
     
  12. Offline

    fireblast709

    Do you have any plugins which have an economy? (one from the 'vault currently supports' on the BukkitDev page)
     
  13. Offline

    PieMan456

    fireblast709
    I just removed them all and tried again but it still disables it.
     
  14. Offline

    fireblast709

    PieMan456 No you should have one of them for the economy to be registered (nope, didn't check Vault for this, this is purely based on guessing). So you need at least Vault, one of those plugins providing economy, and your own plugin in the /plugins folder
     
  15. Offline

    PieMan456

    fireblast709
    Haha you got it right. Thanks! It only works if you have an economy plugin as well.
     
Thread Status:
Not open for further replies.

Share This Page