Core/src/main/java/eu/univento/core/api/player/Friends.java

109 lines
2.4 KiB
Java

package eu.univento.core.api.player;
import eu.univento.core.Core;
import eu.univento.core.api.database.MySQL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
* api for friends
* @author joethei
* @version 1.1
*/
public class Friends {
/**
* player object
*/
CustomPlayer player;
/**
* creates the friends object
* @param player CustomPlayer
*/
public Friends(CustomPlayer player) {
this.player = player;
}
/**
* gets database IDs of friends
* @return ArrayList<Integer>
*/
public ArrayList<Integer> getFriends() {
MySQL sql = Core.returnSQL();
try {
sql.openConnection();
ArrayList<Integer> list = new ArrayList<Integer>();
ResultSet rs = sql.getConnection().createStatement().executeQuery("SELECT * FROM Friends WHERE Player_ID = '" + player.getID());
while(rs.next()) {
list.add(rs.getInt("Friend_ID"));
}
sql.closeConnection();
return list;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return null;
}
}
/**
* add player to friends
* @param p CustomPlayer
*/
public void addFriend(CustomPlayer p) {
MySQL sql = Core.returnSQL();
try {
sql.openConnection();
PreparedStatement ps = sql.getConnection().prepareStatement("INSERT INTO Friends ('Player_ID', 'Friend_ID') VALUES ('" + player.getID() + "', '" + p.getID() + "');");
ps.execute();
sql.closeConnection();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
/**
* removes player from friends
* @param p CustomPlayer
*/
public void removeFriend(CustomPlayer p) {
MySQL sql = Core.returnSQL();
try {
sql.openConnection();
PreparedStatement ps = sql.getConnection().prepareStatement("DELETE FROM Friends WHERE Player_ID='" + player.getID() + "' AND Friend_ID='" + p.getID() + "');");
ps.execute();
sql.closeConnection();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
/**
* checks if player is friend
* @param p CustomPlayer
* @return true/false
*/
public boolean isFriend(CustomPlayer p) {
return getFriends().contains(p.getID());
}
/**
* counts friends
* @return int
*/
public int countFriends() {
return getFriends().size();
}
/**
* checks if player has friends
* @return true/false
*/
public boolean hasFriends() {
return getFriends().size() >= 0;
}
}