bug in my code that i cant see xD

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

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

    tommycake50

    well in a plugin im developing for a server almost none of the enchantments work
    and i found the first bug which was wrong enchant level but now most dnt work which is wierd :confused:
    and i rly dont get why s if someone could look at the code and tell me if i am doing anything stupidly wrong i would much appreciate it
    also it says that it as been enchanted but when you hover over the item the enchant ain't there.
    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 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() {
    this.getConfig();
    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.getName();
    PluginDescriptionFile pdffile = this.getDescription();
     
    /* sets what you type in after the slash to activate this plugins features */
    if(player.hasPermission("enchant.use") && CommandLabel.equalsIgnoreCase("enchant4cash")){
    if (args.length == 1) {
    logger.info(" [" + pdffile.getName() + "] " + "User" + playername + " Has executed the command /enchant4cash " + args[0]);
    player.getInventory();
    /* enchans the item that the player is holding after checking what enchantment he wants */
    switch(args[0]){
    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, 3);
    /* 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("levelsilk"));
     
    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("levelfireaspect"));
     
    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("levelknockback"));
     
    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("levelsmite"));
     
    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;
    case "respiration":
    EconomyResponse r6 = econ.withdrawPlayer(playername, getConfig().getDouble("priceaquaaffinity"));
    if(r6.transactionSuccess()){
    player.getItemInHand().addEnchantment(Enchantment.WATER_WORKER, getConfig().getInt("levelaffinity"));
    player.sendMessage(ChatColor.GREEN + "your item has been enchanted for the sum of" + getConfig().getDouble("priceaquaaffinity"));
    }else{
    player.sendMessage(ChatColor.RED + "the transaction wasnt successful");
    }
    break;
    case "aquaaffnity":
    EconomyResponse r7 = econ.withdrawPlayer(playername, getConfig().getDouble("pricerespiration"));
    if(r7.transactionSuccess()){
    player.getItemInHand().addEnchantment(Enchantment.OXYGEN, getConfig().getInt("leveloxygen"));
    player.sendMessage(ChatColor.GREEN + "your item has been enchanted for the price of" + getConfig().getDouble("pricerespiration"));
    }else{
    player.sendMessage(ChatColor.RED + "the transaction wasnt successful");
    }
    break;
    case "projectileprotection":
    EconomyResponse r8 = econ.withdrawPlayer(playername, getConfig().getDouble("priceprojectileprotection"));
    if(r8.transactionSuccess()){
    player.getItemInHand().addEnchantment(Enchantment.PROTECTION_PROJECTILE, getConfig().getInt("levelprojectileprotection"));
    player.sendMessage(ChatColor.GREEN + "your item has been enchanted for the sum of" + getConfig().getDouble("priceprojectileprotection"));
    }else{
    player.sendMessage(ChatColor.RED + "the transaction wasnt successful");
    }
    break;
    case "fireprotection":
    EconomyResponse r9 = econ.withdrawPlayer(playername, getConfig().getDouble("pricefireprotection"));
    if(r9.transactionSuccess()){
    player.getItemInHand().addEnchantment(Enchantment.PROTECTION_FIRE, getConfig().getInt("enchantlevelfireprot"));
    player.sendMessage(ChatColor.GREEN + "your item was enchanted for the prie of" + getConfig().getDouble("pricefireprotection"));
    }else{
    player.sendMessage(ChatColor.RED + "the transaction wasnt succesful");
    }
    break;
    case "infinity":
    EconomyResponse r10 = econ.withdrawPlayer(playername, 1);
    if(r10.transactionSuccess()){
    player.getItemInHand().addEnchantment(Enchantment.ARROW_INFINITE, getConfig().getInt("levelinfinity"));
    player.sendMessage(ChatColor.GREEN + "your item was enchanted for the sum of" + getConfig().getDouble("priceinfinity"));
    }else{
    player.sendMessage(ChatColor.RED + "the transaction wasnt successful");
    }
    break;
    case "power":
    EconomyResponse r11 = econ.withdrawPlayer(playername, getConfig().getDouble("pricepower"));
    if(r11.transactionSuccess()){
    player.getItemInHand().addEnchantment(Enchantment.ARROW_DAMAGE, getConfig().getInt("enchantlevelpower"));
    player.sendMessage(ChatColor.GREEN + "your item has been enchanted for" + getConfig().getDouble("pricepower"));
    }else{
    player.sendMessage(ChatColor.RED + "the transaction wasnt successful");
    }
    }
    }else if(args.length != 1){
    player.sendMessage(ChatColor.RED + " possible problems: wrong number of args");
    }else{
    /* sends a message to the player if they dont have the correct perms */
    }if (!player.hasPermission("enchant.use")){
    player.sendMessage(ChatColor.RED + "You do not have permission to enchant items this way sorry." );
    }else{
    }
    }
    return true;
    }
    }
    
    erm OK i wasn't asking to be spoon-fed LOL

    i know xD i think i may try to debug it myself but that may fail LOL.

    it doesn't stop working LOL it tells the player it enchants but it hasn't plus it sends the message after the enchant so your wrong.

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

    Tirelessly

    Don't get things from the config like that, load from the config on startup then get from the loaded values.
     
  3. Offline

    tommycake50

    erm ok but my way works :confused:
     
  4. Offline

    Assult

    Are you talking to yourself? O.0
     
  5. Offline

    Tirelessly

    • Not acceptable conduct
    Erm ok but it's laggy when used often.. I'm giving you a better solution, you wanna be a cunt?
     
  6. Offline

    Assult

    Copied from the forum rule page:
    2. No racism, discrimination, threats or personal attacks of any kind are permitted.

    This is a friendly community and as such you are expected to be courteous and follow general posting etiquette. There are times when you may not agree with someone, or you've had a bad day and that's fine, we all have those days. But please, for your own benefit, leave your problems off Bukkit.org. Doing so will ensure that the Bukkit Community is a welcoming, friendly one.
     
    afistofirony likes this.
  7. Offline

    McLuke500

    Wow you have serious issues yes your way may be better(I use for my playerlogger plugin) but for most plugins the way he is using is fine it's the same used in the tutorials for configs on bukkit. His way works he didn't ask for help on that so just stay away from this thread.
     
  8. Offline

    tommycake50

    erm i believe that may be against the rules. i can see how this may be laggy as it would load the file every time so thank you for the advice i may implement it in the future. as of now im fine thankyou.
     
  9. Offline

    Tirelessly

    It's used in the config tutorials because the point of those tutorials is to show how to get from a config...
     
Thread Status:
Not open for further replies.

Share This Page