Solved Not sure what to call this thread.

Discussion in 'Plugin Development' started by DomThePotato, Apr 6, 2015.

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

    DomThePotato

    Hello,
    Ok so basically I am making a rankup command for a prison server. It was all going fine and stuff - Implemented PeX, Essentials & Vault - until I put it out for it's first round of testing. It's not like there were any errors it's just you could rankup to B multiple times even though I had told the code if they are in B do other things. So this is what happens when you it's A -> B:

    Code:
    if(PermissionsEx.getUser(p).inGroup("RankA")){
    
                            try {
                                if(Economy.hasEnough(p.getName(), 50000.0)){
                                Main.econ.withdrawPlayer(p, 50000.0);
                                PermissionsEx.getUser(p).addGroup("RankB");
                                PermissionsEx.getUser(p).removeGroup("RankA");
                                Bukkit.broadcastMessage(Main.pr+" §6"+p.getName()+" §ahas just ranked up to Rank §6B§A!");
                                }else{
                                    p.sendMessage(Main.pr+" §4You do not have enough money! You need §6$50,000 §4to rank up to §6B§4! And you only have §6"+Main.econ.getBalance(p)+"$§4!");
                                }
                                } catch (UserDoesNotExistException e) {
                                    e.printStackTrace();
                                }
                    }
    And it does work. But then once that happens it puts the player in B and everything but then as if the other peice of code wasn't there when you do /rankup again it will tell you (If you don't have enough moneys) 'You don't have enough money to rank up to B' etc.

    Here is the code for B -> which apprently doesn't work.

    Code:
    else if(PermissionsEx.getUser(p).inGroup("RankB")){
                        try {
                            if(Economy.hasEnough(p.getName(), 75000.00)){
                            Main.econ.withdrawPlayer(p, 75000.0);
                            PermissionsEx.getUser(p).addGroup("RankC");
                            PermissionsEx.getUser(p).removeGroup("RankB");
                            Bukkit.broadcastMessage(Main.pr+" §6"+p.getName()+" §ahas just ranked up to Rank §6C§A!");        
                            }else{
                                p.sendMessage(Main.pr+" §4You do not have enough money! You need §6$75,000 §4to rank up to §6C§4! And you only have §6"+Main.econ.getBalance(p)+"$§4!");
                            }
                       
                    }catch (UserDoesNotExistException e) {
                        e.printStackTrace();
                    }
                        }
    Sorry if it's a bit of a mouthful. If you want to see anymore of the code or I have not explained it correctly/enough, please leave a comment :D. Thanks for reading this. :D

    ~Dominic :D
     
  2. @DomThePotato I wouldn't use Pex for checking groups, I personally would use Vault because then it will support all permission plugins.
     
  3. Offline

    DomThePotato

    @bwfcwalshy That is a problem :/ Hence not sure how to do that.
     
  4. Offline

    Monkey_Swag

  5. Offline

    DomThePotato

    @Monkey_Swag
    Is that for Vault or PermissionsEx? Either way I cannot see anything that implies that.
     
  6. @DomThePotato Vault. Look here for the API and then you can use #getPrimaryGroup(Player player);
     
  7. Offline

    DomThePotato

    @bwfcwalshy Is that by implementing Vault? Because if it is I have already implemented it but I cannot see how to get it.
     
  8. @DomThePotato Look at the API, if your code is like that then please post your updated code.
     
  9. Offline

    DomThePotato

    @bwfcwalshy I feel like such an annoying idiot right now, but what am I supposed to be looking at?:oops:

    ---------------------------------------Edit-----------------------------------

    Ok sorry I have been an idiot. I did some more research and I have found the method #getPrimaryGroups(Player player). Sorry. So will this resolve the problems?

    Also the reason I couldn't see it beforehand is because it was importing Bukkit's permissions instead of Vaults.

    @bwfcwalshy I'm not sure what I do next as the getPrimaryGroup() is a String method therefore I cannot set it as that is what I wanted to do. And this has gone completely of topic to what the original problem was. :/
     
    Last edited by a moderator: Apr 6, 2015
  10. Offline

    dlange

    @DomThePotato i haven't used VaultAPI but i believe that String is the name of the group's name, RankA/RankB etc.
     
  11. Offline

    DomThePotato

    @dlange Okay, thanks. But still I don't see how this helps the original situation at all :/
     
  12. Offline

    TehHypnoz

    This example from the VaultAPI GitHub page might help:

    Code:
    package com.example.plugin;
    
    import java.util.logging.Logger;
    
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import net.milkbowl.vault.permission.Permission;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ExamplePlugin extends JavaPlugin {
    
    private static final Logger log = Logger.getLogger("Minecraft");
    public static Economy econ = null;
    public static Permission perms = null;
    public static Chat chat = null;
    
    @Override
    public void onDisable() {
    log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion()));
    }
    
    @Override
    public void onEnable() {
    if (!setupEconomy() ) {
    log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    getServer().getPluginManager().disablePlugin(this);
    return;
    }
    setupPermissions();
    setupChat();
    }
    
    private boolean setupEconomy() {
    if (getServer().getPluginManager().getPlugin("Vault") == null) {
    return false;
    }
    RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    if (rsp == null) {
    return false;
    }
    econ = rsp.getProvider();
    return econ != null;
    }
    
    private boolean setupChat() {
    RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
    chat = rsp.getProvider();
    return chat != null;
    }
    
    private boolean setupPermissions() {
    RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
    perms = rsp.getProvider();
    return perms != null;
    }
    
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    if(!(sender instanceof Player)) {
    log.info("Only players are supported for this Example Plugin, but you should not do this!!!");
    return true;
    }
    
    Player player = (Player) sender;
    
    if(command.getLabel().equals("test-economy")) {
    // Lets give the player 1.05 currency (note that SOME economic plugins require rounding!)
    sender.sendMessage(String.format("You have %s", econ.format(econ.getBalance(player.getName()))));
    EconomyResponse r = econ.depositPlayer(player, 1.05);
    if(r.transactionSuccess()) {
    sender.sendMessage(String.format("You were given %s and now have %s", econ.format(r.amount), econ.format(r.balance)));
    } else {
    sender.sendMessage(String.format("An error occured: %s", r.errorMessage));
    }
    return true;
    } else if(command.getLabel().equals("test-permission")) {
    // Lets test if user has the node "example.plugin.awesome" to determine if they are awesome or just suck
    if(perms.has(player, "example.plugin.awesome")) {
    sender.sendMessage("You are awesome!");
    } else {
    sender.sendMessage("You suck!");
    }
    return true;
    } else {
    return false;
    }
    }
    }
    
     
  13. Offline

    DomThePotato

    @TehHypnoz Okay thanks. Although I've seen this many times and it hasn't helped me apart from with the Economy part.

    Please relate to my original question. :)
     
  14. Offline

    TehHypnoz

    The permissions part might be useful, I think you can use perms.getPrimaryGroup(Player p).
     
  15. Offline

    DomThePotato

    @TehHypnoz @dlange @bwfcwalshy Okay. I will restate my problem. The permissions, they're working fine. But for some reason the code is not acknowledging the fact that when they rank up they go to the rank above. So if you were in RankA and you ranked up it would work fine. But if you tried to rankup from there it wouldn't work, it would just say you didn't have enough money to rankup to B even though you were B.

    On a side note: How do you set Player groups with Vault?

    Thanks. :)
     
  16. Offline

    TehHypnoz

    I have only used vault for economy and adding permissions, but looking at the code from their permission class might help, I think you're looking for playerAddGroup.
     
  17. Offline

    DomThePotato

    @TehHypnoz Wow. Thanks! That really did help :D. It is all going fine again now :D. Although for some reason it adds them to a group that is per-world. :/ So the default rank is RankA. But then when they rankup it sets them to the next rank but they still have RankA in their group list. So the rank is only set per-world. :/. Do you know how to set it as a global group?
     
  18. Offline

    TehHypnoz

    I actually found 2 methods, one which requires a world, player and group and one that only requires a player and a group. Maybe if you use the last one it would work, but the javadocs say that 'This may return odd results if the permission system being used on the server does not support world-specific groups, or if the group being added to is a global group.'. If that doesn't work, you can always just set their rank for all worlds.
     
  19. Offline

    DomThePotato

    @TehHypnoz Yeah. I use PeX and that supports per-world and global. I was also using the last method. But I want the players to be added to the group globally so they are the rank in every world because with the per-world it is a bit weird and it messes up the hierarchy.
     
  20. Offline

    TehHypnoz

    Can't really help you with that, sorry. But if it's just a private plugin it might actually be better to use the pex api.
     
  21. Offline

    DomThePotato

    @TehHypnoz That is what I used originally. But it ceased to work.
     
Thread Status:
Not open for further replies.

Share This Page