Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ public interface DataSource {
boolean removePlayedTutorial(UUID uuid, String id);

boolean hasPlayedTutorial(UUID uuid, String id);

String getQuitTutorial(UUID uuid);

void setQuitTutorial(UUID uuid, String id);

boolean removeQuitTutorial(UUID uuid);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,65 @@ public FlatDataSource(ServerTutorialPlus plugin){

@Override
public List<String> getPlayedTutorials(UUID uuid) {
return (List<String>) getDataFromUUID(uuid).getOrDefault("tutorials", new ArrayList<>());
}

@Override
public String getQuitTutorial(UUID uuid) {
return (String) getDataFromUUID(uuid).get("quit_tutorial");
}

private JSONObject getDataFromUUID(UUID uuid) {
File hostlocation = new File(plugin.getDataFolder() + "/data/playerdata");
hostlocation.mkdirs();

JSONObject data = new JSONObject();

File file = new File(plugin.getDataFolder() + "/data/playerdata/" + uuid + ".json");
if(file.exists()){
if(file.exists()) {
JSONParser parser = new JSONParser();
JSONObject data = null;
FileReader reader = null;

try{
try {
reader = new FileReader(file.getPath());
Object obj = parser.parse(reader);
data = (JSONObject) obj;
} catch (Exception ex){
} catch (Exception ex) {
ex.printStackTrace();

} finally {
if(reader != null){
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return (List<String>) data.get("tutorials");
}
else{
return new ArrayList<>();
}

return data;
}

@Override
public boolean addPlayedTutorial(UUID uuid, String id) {
List<String> played = getPlayedTutorials(uuid);
played.add(id);
return changeDataInFile(uuid, "tutorials", played);
}

File hostlocation = new File(plugin.getDataFolder() + "/data/playerdata/");
hostlocation.mkdirs();
@Override
public void setQuitTutorial(UUID uuid, String id) {
changeDataInFile(uuid, "quit_tutorial", id);
}

JSONObject data = new JSONObject();
data.put("tutorials", played);
public boolean changeDataInFile(UUID uuid, String key, Object value) {
JSONObject data = getDataFromUUID(uuid);
if (value == null) {
data.remove(key);
} else {
data.put(key, value);
}

File file = new File(plugin.getDataFolder() + "/data/playerdata/" + uuid + ".json");

Expand Down Expand Up @@ -96,34 +112,12 @@ public boolean addPlayedTutorial(UUID uuid, String id) {
public boolean removePlayedTutorial(UUID uuid, String id) {
List<String> played = getPlayedTutorials(uuid);
played.remove(id);
return changeDataInFile(uuid, "tutorials", played);
}

File hostlocation = new File(plugin.getDataFolder() + "/data/playerdata/");
hostlocation.mkdirs();

JSONObject data = new JSONObject();
data.put("tutorials", played);

File file = new File(plugin.getDataFolder() + "/data/playerdata/" + uuid + ".json");

FileWriter writer = null;
try{
writer = new FileWriter(file);
writer.write(data.toJSONString());
} catch (Exception ex){
ex.printStackTrace();
return false;
} finally {
if(writer != null){
try {
writer.flush();
writer.close();
} catch (Exception ex){
ex.printStackTrace();
}
}
}

return true;
@Override
public boolean removeQuitTutorial(UUID uuid) {
return changeDataInFile(uuid, "quit_tutorial", null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public boolean setup() {
return false;
}

return true;
return simpleSqlUpdate("CREATE TABLE IF NOT EXISTS Quit_Tutorials " +
"(uuid VARCHAR(64) PRIMARY KEY, " +
" tutorial VARCHAR(255) NOT NULL);");
}

public boolean simpleSqlUpdate(String sql)
Expand Down Expand Up @@ -142,16 +144,75 @@ public List<String> getPlayedTutorials(UUID uuid) {
return tutorials;
}

@Override
public String getQuitTutorial(UUID uuid) {
Connection connection = null;
Statement statement = null;
ResultSet result = null;

try{
connection = mySql.getConnection();
statement = connection.createStatement();

result = statement.executeQuery("select tutorial from Quit_Tutorials where uuid='" + uuid + "'");

if (result.next()){
return result.getString("tutorial");
}

} catch (Exception ex){
plugin.getLogger().warning("[!!!] An error occurred while to get a players played tutorials...");
ex.printStackTrace();
return null;
} finally {
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(result != null){
try {
result.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

return null;
}

@Override
public boolean addPlayedTutorial(UUID uuid, String id) {
return simpleSqlUpdate("insert into Tutorial_Players (uuid, tutorial) VALUES " + String.format("('%s', '%s')", uuid, id));
}

@Override
public void setQuitTutorial(UUID uuid, String id) {
simpleSqlUpdate("insert into Quit_Tutorials (uuid, tutorial) VALUES " + String.format("('%s', '%s')", uuid, id));
}

@Override
public boolean removePlayedTutorial(UUID uuid, String id) {
return simpleSqlUpdate("delete from tutorial_players where uuid='" + uuid + "' AND tutorial='" + id + "'");
}

@Override
public boolean removeQuitTutorial(UUID uuid) {
return simpleSqlUpdate("delete from Quit_Tutorials where uuid='" + uuid + "';");
}

@Override
public boolean hasPlayedTutorial(UUID uuid, String id) {
List<String> tutorials = getPlayedTutorials(uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public void run() {
return;
}

String quitTutorialId = plugin.getDataSource().getQuitTutorial(event.getPlayer().getUniqueId());
if (quitTutorialId != null) {
ServerTutorial serverTutorial = PluginUtils.getTutorial(plugin, quitTutorialId);
if (serverTutorial != null) {
plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
new TutorialController(plugin, event.getPlayer(), serverTutorial).start();
plugin.getDataSource().removeQuitTutorial(event.getPlayer().getUniqueId());
}, 20L);
}
}

new BukkitRunnable(){
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public void onPlayerQuit(PlayerQuitEvent event){
TutorialController tc = plugin.inTutorial.get(event.getPlayer().getUniqueId());
tc.cancel(true);

plugin.getDataSource().setQuitTutorial(event.getPlayer().getUniqueId(), tc.getTutorial().getId());

OldValuesPlayer oldValuesPlayer = tc.getOldValuesPlayer();
oldValuesPlayer.restore(event.getPlayer());
}
Expand Down