Skip to content

Commit

Permalink
Fixed bug where the todo mark would affect other sections. Made the j…
Browse files Browse the repository at this point in the history
…son file save on clear.

Syncronized the json file save.
Made it save every time is received a messageId update.
  • Loading branch information
DV8FromTheWorld committed Jun 19, 2016
1 parent 614a25e commit 0987f39
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/main/java/net/dv8tion/discord/commands/KanzeTodoCommand.java
Expand Up @@ -110,7 +110,7 @@ private boolean markTodo(String arg, MessageReceivedEvent e)
e.getTextChannel().sendMessageAsync(e.getAuthorNick() + ": No number was provided to mark! Example: `!!todo -3`", null);
return false;
}
int index ;
int index;
try
{
index = Integer.valueOf(arg);
Expand All @@ -128,6 +128,8 @@ private boolean markTodo(String arg, MessageReceivedEvent e)
}

int searchIndex = 1;

listLoop:
for (int i = 0; i < todoList.length(); i++)
{
JSONObject section = todoList.getJSONObject(i);
Expand All @@ -139,7 +141,7 @@ private boolean markTodo(String arg, MessageReceivedEvent e)
JSONObject entry = entries.getJSONObject(j);
entry.put("marked", !entry.getBoolean("marked"));
sendSectionToDiscord(section, index - j);
break;
break listLoop;
}
searchIndex++;
}
Expand Down Expand Up @@ -181,6 +183,7 @@ private boolean clearTodo()
sendSectionToDiscord(section, index);
index += section.getJSONArray("entries").length();
}
updateSaveFile();
return true;
}

Expand Down Expand Up @@ -256,13 +259,16 @@ private JSONObject createNewEntry(String content, boolean marked)

private void updateSaveFile()
{
try
{
Files.write(jsonFile.toPath(), todoList.toString(4).getBytes());
}
catch (IOException e)
synchronized (todoList)
{
e.printStackTrace();
try
{
Files.write(jsonFile.toPath(), todoList.toString(4).getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

Expand Down Expand Up @@ -299,6 +305,7 @@ private void sendSectionToDiscord(JSONObject section, int beginIndex)
api.getTextChannelById(TODO_CHANNEL_ID).sendMessageAsync(sb.toString(), msg ->
{
section.put("messageId", msg.getId());
updateSaveFile();
});
}
else
Expand Down

0 comments on commit 0987f39

Please sign in to comment.