why will this plugin not work? o.O

Discussion in 'Plugin Development' started by tommycake50, Sep 3, 2012.

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

    tommycake50

    i hope you can help me because i cant see any bugs in my code but when i try to use it and when i use the command it just does nothing doesn't even say command not found or anything like that nor does it do what its supposed to do in the code it enables properly it disables properly but it just does nothing.
    here is the code
    Code:
    package me.tommycake50.enchant4cash;
     
    import java.util.logging.Logger;
     
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Enchant4cash /* makes it recognisable to bukkit as a plugin */ extends JavaPlugin{
    /* gets the minecraft logger */
    public final Logger logger = Logger.getLogger("Minecraft");
    /* declares the variable econ for further use */
    public Economy econ;
    /* does the stuff in the body when the plugin gets enabled */
    @Override
    public void onEnable() {
           
            PluginDescriptionFile pdffile = getDescription();
            logger.info("[" + pdffile.getName()+ "]" + " version: " + pdffile.getVersion() + " has been enabled!");
            
            loadConfig();
     
    setupEconomy();
    }
     
    public void loadConfig() {
    saveDefaultConfig();
    this.reloadConfig();
    }
     
    public boolean setupEconomy()
    {
       RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
       if(rsp == null)
       {
           return false;
       }
       econ = rsp.getProvider();
       return econ != null;
    }
    @Override
    public void onDisable(){
    PluginDescriptionFile pdffile = getDescription();
    logger.info("[" + pdffile.getName() + "]" + " Has been disabled");
    }
    public boolean OnCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) {
     
    /* gets the player name */
    Player player = (Player) sender;
    /* gets the name of the player */
    String playername = player.getDisplayName();
    /* sets wht  you type in after the slash to activate this plugins features */
    if(args.length == 1 && player.hasPermission("enchant.use") && CommandLabel.equalsIgnoreCase("enchant4cash")){
    player.getInventory();
    /* enchans the item that the player is holding after checking what enchantment he wants */
    switch(args[1]){
    case "unbreaking":
    /* withdraws money from the sender's account */
    EconomyResponse r = econ.withdrawPlayer(playername, getConfig().getDouble("priceunbreaking"));
    if(r.transactionSuccess()){
    /* enchants the item */
    player.getInventory().getItemInHand().addEnchantment(Enchantment.DURABILITY, getConfig().getInt("enchantlevel"));
    /* sends a notifying message */
    player.sendMessage(ChatColor.GREEN + "your item has been enchanted for the sum of" + getConfig().getDouble("priceunbreaking"));
    }else{
    player.sendMessage(ChatColor.RED + "The transaction wasnt successful");
    }
    break;
    case "efficiency":
    EconomyResponse r1 = econ.withdrawPlayer(playername, getConfig().getDouble("priceefficiency"));
    if(r1.transactionSuccess()){
    player.getInventory().getItemInHand().addEnchantment(Enchantment.DIG_SPEED, getConfig().getInt("enchantlevel"));
     
    player.sendMessage(ChatColor.GREEN + "your item has been enchanted for the sum of" + getConfig().getDouble("priceefficiency"));
    }else{
    player.sendMessage(ChatColor.RED + "The transaction wasnt successful");
    }
    break;
    case "silktouch":
     
    EconomyResponse r2 = econ.withdrawPlayer(playername, getConfig().getDouble("pricesilktouch"));
    if(r2.transactionSuccess()){
    player.getInventory().getItemInHand().addEnchantment(Enchantment.SILK_TOUCH, getConfig().getInt("enchantlevel"));
     
    player.sendMessage(ChatColor.GREEN + "your item has been enchanted for the sum of" + getConfig().getDouble("pricesilktouch"));
    }else{
    player.sendMessage(ChatColor.RED + "The transaction wasnt successful");
    }
     
    break;
    case "fireaspect":
    EconomyResponse r3 =  econ.withdrawPlayer(playername, getConfig().getDouble("pricefireaspect"));
    if(r3.transactionSuccess()){
     
                        player.getInventory().getItemInHand().addEnchantment(Enchantment.FIRE_ASPECT, getConfig().getInt("enchantlevel"));
     
    player.sendMessage(ChatColor.GREEN + "your item has been enchanted for the sum of" + getConfig().getDouble("pricefireaspect"));
    }else{
    player.sendMessage(ChatColor.RED + "The transaction wasnt successful");
    }
    break;
    case "knockback":
    EconomyResponse r4 = econ.withdrawPlayer(playername, getConfig().getDouble("priceknockback"));
    if(r4.transactionSuccess()){
                        player.getInventory().getItemInHand().addEnchantment(Enchantment.KNOCKBACK, getConfig().getInt("enchantlevel"));
     
    player.sendMessage(ChatColor.GREEN + "your item has been enchanted for the sum of" + getConfig().getDouble("priceknockback"));
    }else{
    player.sendMessage(ChatColor.RED + "The transaction wasnt successful");
    }
    break;
    case "smite":
    EconomyResponse r5 = econ.withdrawPlayer(playername, getConfig().getDouble("pricesmite"));
    if(r5.transactionSuccess()){
     
                        player.getInventory().getItemInHand().addEnchantment(Enchantment.DAMAGE_ALL, getConfig().getInt("enchantlevel"));
     
    player.sendMessage(ChatColor.GREEN + "your item has been enchanted for the sum of" + getConfig().getDouble("pricesmite"));
    }else{
    player.sendMessage(ChatColor.RED + "The transaction wasnt successful");
    }
    break;
    }
      
    /* sends a message to the player if they dont have the correct perms */
    }else if (!player.hasPermission("enchant.use")){
    player.sendMessage(ChatColor.RED + "You do not have permission to enchant items this way sorry." );
    }else{
    player.sendMessage(ChatColor.RED + "wrong number of args");
    }
     
     
    return true;
     
    }
     
    }
    
    no errors/stacktrace's so it must be buggy code can anyone shed some light on where i failed?
     
  2. you know you need to register commands at the plugin.yml?
     
  3. Offline

    tommycake50

    yes i know
    my plugin.yml
    Code:
    name: Enchant4Cash
    main: me.tommycake50.enchant4cash.Enchant4cash
    version: 1.0
    depend: [Vault]
    description: A plugin to enchant your items for a set amount of cash
    commands:
      enchant4cash:
        permission: enchant.use
        permission-message: You do not have permission to enchant items this way sorry.
    as i said im not good with yaml files.
     
  4. can you put the following line on top of the onCommand:

    sender.sendMessage("DEBUG: Command was been excuted"); // TODO remove this debug message at production code
     
  5. Offline

    tommycake50

    no apparently the command wasn't actually executed. which seems wierd.

    Bump!

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

    Doggyroc

    Can you post your code on Pastebin or Pastie? This code is messed up.
     
  7. Offline

    tommycake50

  8. Offline

    StevasaurousREX

    Dont know if you fixed this yet or not, but you ask for 1 arg (args.length == 1) then you try to use information for a second. (args[1]) change that to (args[0])

    also you put the debug message in the wrong part you should have put it inside the command itself, in the pastebin below I removed it.

    In java '0' counts as a number too, so the first 'args' is 'args[0]'

    Here is what I meant using your code, I also changed how people get error message if it doesnt have the correct 'args' that way if you add more commands later you dont have to change your code, I would also do the same for the permission but I didnt see that so much as a problem, just a personal preference
    http://pastebin.com/sdGMkDNb
    and I changed the plugin.yml some
    http://pastebin.com/zYVDLYJc
     
  9. Offline

    tommycake50

    thanks a bunch but the plugin.yml is invalid
    Code:
    15:17:46 [SEVERE] Permission node 'permission' in plugin description file for Enchant4Cash v1.0 is invalid
    java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
            at org.bukkit.permissions.Permission.loadPermissions(Permission.java:218)
            at org.bukkit.plugin.PluginDescriptionFile.getPermissions(PluginDescriptionFile.java:167)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:267)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:247)
            at net.minecraft.server.MinecraftServer.i(MinecraftServer.java:296)
            at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:275)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:225)
            at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:140)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:380)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    15:17:46 [SEVERE] Permission node 'permission-message' in plugin description file for Enchant4Cash v1.0 is invalid
    java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
            at org.bukkit.permissions.Permission.loadPermissions(Permission.java:218)
            at org.bukkit.plugin.PluginDescriptionFile.getPermissions(PluginDescriptionFile.java:167)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:267)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:247)
            at net.minecraft.server.MinecraftServer.i(MinecraftServer.java:296)
            at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:275)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:225)
            at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:140)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:380)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    15:17:46 [SEVERE] Permission node 'Default' in plugin description file for Enchant4Cash v1.0 is invalid
    java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.util.Map
            at org.bukkit.permissions.Permission.loadPermissions(Permission.java:218)
            at org.bukkit.plugin.PluginDescriptionFile.getPermissions(PluginDescriptionFile.java:167)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:267)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:247)
            at net.minecraft.server.MinecraftServer.i(MinecraftServer.java:296)
            at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:275)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:225)
            at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:140)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:380)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    
     
  10. The problem is you wrote OnCommand instead of onCommand which are two diferent methods, that's why it doesn't trigger.

    You should use ctrl+space whenever you want to get an existing method to override, it's faster and safer.
     
  11. Offline

    tommycake50

    thanks a lot for the help.
     
  12. Offline

    StevasaurousREX

    Sorry had some extra whitespace on the permission: line that messed up the rest
    should work now and didnt notice the capital 'O' on onCommand lol sorry

    Code:
    name: Enchant4Cash
    main: me.tommycake50.enchant4cash.Enchant4cash
    version: 1.0
    depend: [Vault]
    description: A plugin to enchant your items for a set amount of cash
    commands:
      enchant4cash:
        description: Buy enchants.
        
    permissions:
      permission: enchant.use
        permission-message: You do not have permission to enchant items this way sorry.
        Default: false
    
     
  13. Offline

    tommycake50

    thanks a lot the only problem i have now is that the method WithDrawPlayer()
    in vault keeps on not working and the "transaction" iis never completed :/

    also i noticed that the players' name when printed to thee console has wierd symbols around it :S
    for instance
    21:53:10 [INFO] [Enchant4Cash]User§e§ctommy2ndacc§f§fHas executed the command /enchant4cash efficiency

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

    Craftiii4

    Are you changing the colour?
     
  15. Offline

    StevasaurousREX

    Those look like color codes to me, probably cause you used this
    Code:
    String playername = player.getDisplayName();
    change that to
    Code:
    String playername = player.getName();
    and it should output correctly and i think this should fix your vault issue too. Ill read up on vault haven't used it in a while
     
  16. Offline

    tommycake50

    oh i think its because when you op someone essentials make their name red.

    wow thanks soo much i couldn't have finished it without you.

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

    StevasaurousREX

    No problem, tbh I'm new to this too, and have problems doing things all the time. Make sure you use the right method though like Digi said OnCommand is a different method than onCommand. I noticed in your post you put WithDrawPlayer() but I checked your code and you have it as 'withdrawPlayer()' which is correct way to use it
     
Thread Status:
Not open for further replies.

Share This Page