Solved fixed

Discussion in 'Spigot Plugin Development' started by TwerkRekt, Jan 20, 2020.

Thread Status:
Not open for further replies.
  1. Hello, im actually creating my first plugin but i have a problem here.

    Code (myclass (Unknown Language)):

    package fr.twerkrekt.islandbank.commands.player;

    import java.text.DecimalFormat;

    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.Permission;

    import com.wasteofplastic.askyblock.Island;

    import fr.twerkrekt.islandbank.Main;
    import fr.twerkrekt.islandbank.objects.BankCommand;
    import fr.twerkrekt.islandbank.objects.Bank;

    public class Withdraw extends BankCommand {

        [USER=32110]@Override[/USER]
        public void run(CommandSender sender, String[] args) {
            Player player = (Player) sender;
            Island island = Main.getInstance().manager.getIslandForPlayer(player.getUniqueId());
            if (island == null) {
                player.sendMessage(getMessage("withdraw.island-data-incorrect"));
                return;
            }
            Bank bank = Main.getInstance().manager.getBank(island);
            if (bank == null) {
                Main.getInstance().manager.loadBank(island);
                bank = Main.getInstance().manager.getBank(island);
            }
            double amount;
            if (args[1].equalsIgnoreCase("all")) {
                amount = bank.getBankBalance();
            } else {
                try {
                    amount = Main.getInstance().getVaultHandler().round(Double.parseDouble(args[1]), 2);
                } catch (NumberFormatException ex) {
                    player.sendMessage(getMessage("withdraw.enter-valid-amount"));
                    return;
                }
            }
            if (amount > bank.getBankBalance()) {
                player.sendMessage(getMessage("withdraw.invalid-balance"));
                return;
            }
            bank.decreaseBankBalance(amount);
            Main.getInstance().getVaultHandler().increasePlayerBalance(player.getUniqueId(), amount);
            DecimalFormat formatter = new DecimalFormat("#,###.00");
            player.sendMessage(getMessage("withdraw.withdrawn").replaceAll("%amount%", "\\" + formatter.format(amount)));
            return;
            }

        [USER=32110]@Override[/USER]
        public boolean requiresPlayer() {
            return true;
        }

        [USER=32110]@Override[/USER]
        public Permission getPermissionRequired() {
            return new Permission("orthyonbank.withdraw");
        }

        [USER=32110]@Override[/USER]
        public int minimumArgs() {
            return 2;
        }

        [USER=32110]@Override[/USER]
        public int maximumArgs() {
            return 2;
        }

        [USER=32110]@Override[/USER]
        public String getUsage() {
            return getMessage("withdraw.usage");
        }

        [USER=32110]@Override[/USER]
        public boolean isAdmin() {
            return false;
        }

    }
     
    so if player dont have bank data, cancel event, but when player dont have data:
    Capture.PNG
    any idea?
     
    #1 TwerkRekt, Jan 20, 2020
    Last edited: Jan 20, 2020
  2. Please wrap your code in the [ code] - blocks for readability
     
    #2 Schottky, Jan 20, 2020
    Last edited: Jan 20, 2020
  3. Done sorry for that
     
  4. Thank you. It seems like you want to return when island == null; however that does not seem to be the case. Is that correct?
    If so, what does getIslandForPlayer() do?
     
  5. If player have island (askyblock)
    And issue come to if (bank == null)

    Look my screenshot, idk for what i got this problem (only when data of player are not created in yml file)

    Sorry for my english im french
     
  6. You are not returning at that point; instead creating a new bank. Maybe that‘s the issue?
     
  7. I have tried but same issue :/
     
  8. That means the bank is probably not null. What does getBank() do?
     
  9. getBank = get bank data from a player
     
  10. Sure, but can it actually return null?
    Did you try and print out the value that it returns?
     
  11. No i try it Tomorrow :)
     
  12. Hello so when player join server and don't have data in orthyonbanks.yml and player execute command ("/sb withdraw all") he got this message:
    Capture.PNG

    (but it receives nothing in money and it is logical, thereafter I make a / sba reload and in the orthyonbanks.yml the data are well at 0.0, I do not understand why it receives this message ...)
     
  13. Please paste code for the bank.getBankBalance() method

    Code (Java):
    if (args[1].equalsIgnoreCase("all")) {
        amount = bank.getBankBalance(); // <---- this method
    }
     
  14. Yes

    Code (Text):

    package fr.twerkrekt.islandbank.objects;

    import com.wasteofplastic.askyblock.Island;

    import fr.twerkrekt.islandbank.Main;

    public class Bank {
       
       private Island i;
       private double b;

       public Bank(Island island, double balance) {
           this.i = island;
           setBankBalance(balance);
       }
       
       public Island getIsland() {
           check();
           return i;
       }
       
       public double getBankBalance() {
           check();
           return b;
       }
       
       public void setBankBalance(double amount) {
           this.b = amount;
           Main.getInstance().getIslandBankData().setBankBalance(i, b);
           check();
       }
       
       private void check() {
           if (!Main.getInstance().getIslandBankData().hasIslandData(i)) {
               Main.getInstance().getIslandBankData().createIslandData(i, b);
           }
       }

       public void increaseBankBalance(double amount) {
           this.b += amount;
           Main.getInstance().getIslandBankData().setBankBalance(i, b);
           check();
       }
       
       public void decreaseBankBalance(double amount) {
           this.b -= amount;
           Main.getInstance().getIslandBankData().setBankBalance(i, b);
           check();
       }

    }
     
     
  15. I'm having a hard time figuring your mistake out, due to lack of code.
    Your Bank.class is being called somewhere else, and that is also where the default value is set [new Bank(Island, DefaultBalance?)], unless you're willing to provide more code and details from where the default balance is initially set, I'd recommend debugging your code and walk through it step by step to try and figure it out on yourself.

    Otherwise please return code behind this method in your Bank class:
    Code (Java):
    Main.getInstance().getIslandBankData().createIslandData(i, b);
    // and also this one
    Main.getInstance().getIslandBankData().setBankBalance(i, b);
     
  16. Salut, essaie de faire un peu de déboguage dans le genre:
    Code (Text):
    System.out.print((bank == null) + " " + amount);
    bank.decreaseBankBalance(amount);
     
    • Informative Informative x 1
  17. Oh un Québécois comme moi, quoi de mieux, je vais essayer ça, merci :)
     
    • Like Like x 1
    • Funny Funny x 1
  18. Re, je viens de faire ça et au moment de withdraw, je n'ai aucun message de débug ni rien
     
  19. Hello:
    Code (Text):

    package fr.twerkrekt.islandbank.objects;

    import java.util.Set;

    import com.wasteofplastic.askyblock.Island;

    import fr.twerkrekt.islandbank.handlers.Config;

    public class BankData extends Config {

        public BankData(String fileName) {
            super(fileName);
        }
       
        public void changeIslandID(Island island, String newID) {
            changeIslandID("bank_" + island.getOwner().toString(), newID);
            return;
        }
       
        public void changeIslandID(String oldID, String newID) {
            double balance = getBankBalance(oldID);
            if (balance == Double.MAX_VALUE) {
                balance = 0.0;
            }
            removeBank(oldID);
            createIslandData(newID, balance);
            return;
        }

        public boolean removeBank(Island island) {
            return removeBank("bank_" + island.getOwner().toString());
        }
       
        public boolean removeBank(String id) {
            if (!hasIslandData(id)) {
                return false;
            }
            getConfig().set("bank." + id, null);
            return saveConfig();
        }
       
        public String getFormattedBalance(Island island) throws NumberFormatException {
            return getFormattedBalance("bank_" + island.getOwner().toString());
        }
       
        public String getFormattedBalance(String id) {
            if (!hasIslandData(id)) {
                return null;
            }
            return formatFully(Double.parseDouble(getConfig().getString("bank." + id + ".balance")));
        }

        private String formatFully(double n) {
               if (n < 1000) return "" + n;
                int exp = (int) (Math.log(n) / Math.log(1000));
                return String.format("%.1f %c", n / Math.pow(1000, exp), "kMBTQE".charAt(exp-1));
        }

        public double getBankBalance(Island island) throws NumberFormatException {
            return getBankBalance("bank_" + island.getOwner().toString());
        }
       
        public double getBankBalance(String id) throws NumberFormatException {
            if (!hasIslandData(id)) {
                return Double.MAX_VALUE;
            }
            return Double.parseDouble(getConfig().getString("bank." + id + ".balance"));
        }
       
        public boolean setBankBalance(String id, double amount) {
            if (!hasIslandData(id)) {
                return false;
            }
            getConfig().set("bank." + id +".balance", amount);
            return saveConfig();
        }
       
        public boolean setBankBalance(Island island, double amount) {
            return setBankBalance("bank_" + island.getOwner().toString(), amount);
        }
       
       
        public boolean createIslandData(String id, double amount) {
            getConfig().set("bank." + id + ".balance", amount);
            return saveConfig();
        }
       
        public boolean createIslandData(Island island, double amount) {
            String id = "bank_" + island.getOwner().toString();
            return createIslandData(id, amount);
        }
       
        public boolean hasIslandData(Island island) {
            String id = "bank_" + island.getOwner().toString();
            return hasIslandData(id);
        }
       
        public boolean hasIslandData(String id) {
            if (!getConfig().contains("bank")) {
                return false;
            }
            Set<String> islandData = getConfig().getConfigurationSection("bank").getKeys(false);
            return islandData.contains(id);
        }

    }
     
     
  20. C’est pas vrm bien d’utiliser static pour faire des trucs, cherches sur Google “dependency injection”
     
Thread Status:
Not open for further replies.