Solved Data is not updating in JSON

Discussion in 'Spigot Plugin Development' started by GlareMasters, Jun 8, 2017.

Thread Status:
Not open for further replies.
  1. Hello all,

    I have a current question that I've gotten stuck on while preparing an update for my plugin, a user pointed out to me that they can "promote" people in their guild but not demote them. After some research, we found out that the "promote" command is not updating the JSON like it should. The question is, how do we create this? Currently you can change your "prefix" and it will update in JSON via the following:

    Code (Text):
        public void updatePrefix(String prefix) {
            setPrefix(prefix);

            updateGuild("An error occurred while updating prefix to '%s' for guild '%s'", prefix, this.name);

            Main.getInstance().getScoreboardHandler().update();
        }
    The code executing the updatePrefix function is:

    Code (Text):
        public void execute(Player player, String[] args) {
            Guild guild = Guild.getGuild(player.getUniqueId());
            if (guild == null) {
                Message.sendMessage(player, Message.COMMAND_ERROR_NO_GUILD);
                return;
            }

            GuildRole role = GuildRole.getRole(guild.getMember(player.getUniqueId()).getRole());
            if (!role.canChangePrefix()) {
                Message.sendMessage(player, Message.COMMAND_ERROR_ROLE_NO_PERMISSION);
                return;
            }

            if (!args[0].matches(Main.getInstance().getConfig().getString("prefix.regex"))) {
                Message.sendMessage(player, Message.COMMAND_PREFIX_REQUIREMENTS);
                return;
            }

            Message.sendMessage(player, Message.COMMAND_PREFIX_SUCCESSFUL);
            guild.updatePrefix(args[0]);
        }

    This will run our updateGuild function which is:

    Code (Text):
        private void updateGuild(String errorMessage, String... params) {
            Main.getInstance().getDatabaseProvider().updateGuild(this, (result, exception) -> {
                if (!result) {
                    Main.getInstance().getLogger().log(Level.SEVERE, String.format(errorMessage, params));

                    if (exception != null) {
                        exception.printStackTrace();
                    }
                }
            });
        }
    A preview of the created JSON is the follow:

    Code (Text):
    {
      "Spigot": {
        "prefix": "Spigot",
        "members": [
          {
            "uuid": "88ce8c4b-4eab-4df4-9b45-fe866c8f9552",
            "role": 0
          }
        ],
        "allies": [],
        "invitedMembers": [],
        "pendingAllies": []
      }
    }
    The success function for promoting a member is:

    [​IMG]

    So with all of this information, how can we go about fixing it so that it will update the "role" value just like it updates the "prefix" value.

    Thank you for taking your time to reading this and for your support!

    With Regards,
    Glare Masters
     
    • Informative Informative x 1
  2. Can you show us the setPrefix method? Also, where are you calling the role change?
     
  3. Hey, thanks for the quick response! Here's the information you asked for:

    Code (Text):
        public void setPrefix(String prefix) {
            this.prefix = prefix;
        }
    And I believe this is what you asked for about roles

    Code (Text):
        public static GuildRole getRole(int level) {
            return Main.getInstance().getGuildHandler().getRoles().stream().filter(role -> role.getLevel() == level).findFirst().orElse(null);
        }
     
  4. Oh, the set prefix is inside the guild object, but i cant see anywhere where you set/change the role, there is a obvious method and call for the prefix, but not for the role, at least i dont see it
     
  5. I'm not the best but I found a little more -
    Code (Text):
    public class GuildMember {

        @Expose
        private UUID uuid;

        @Expose
        private int role;

        public GuildMember(UUID uuid, int role) {
            this.uuid = uuid;
            this.role = role;
        }

        public UUID getUniqueId() {
            return uuid;
        }

        public int getRole() {
            return role;
        }

        public void setRole(GuildRole role) {
            this.role = role.getLevel();
        }
    }
    To be totally honest, that could be the issue is that there's nothing calling the role change. If so, how would I go about adding that? The odd thing is that when you promote someone, if you do "/guild info" it will show that you were promoted, but it's not saving the data on reboot.
     
  6. I miss the image xD

    Where and when you call the update guild method?
     
Thread Status:
Not open for further replies.