Begin reworking badge SQL

This commit is contained in:
Ghoti 2020-10-29 21:36:21 -05:00
parent 11bb1e903b
commit 711ffca758
18 changed files with 546 additions and 542 deletions

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>FPChat</groupId> <groupId>com.fpghoti</groupId>
<artifactId>FPChat</artifactId> <artifactId>FPChat</artifactId>
<version>1.1.1</version> <version>1.1.2</version>
<build> <build>
<sourceDirectory>src/main/java</sourceDirectory> <sourceDirectory>src/main/java</sourceDirectory>
<resources> <resources>
@ -35,12 +35,6 @@
</executions> </executions>
<configuration> <configuration>
<source>1.7</source> <source>1.7</source>
<artifactSet>
<includes>
<include>org.apache.commons*:*</include>
</includes>
</artifactSet>
<relocations />
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
@ -63,8 +57,30 @@
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.16.2-R0.1-SNAPSHOT</version> <version>1.16.3-R0.1-SNAPSHOT</version>
<scope>compile</scope> <scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>commons-lang</artifactId>
<groupId>commons-lang</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
<exclusion>
<artifactId>gson</artifactId>
<groupId>com.google.code.gson</groupId>
</exclusion>
<exclusion>
<artifactId>bungeecord-chat</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
<exclusion>
<artifactId>snakeyaml</artifactId>
<groupId>org.yaml</groupId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.MilkBowl</groupId> <groupId>com.github.MilkBowl</groupId>

21
pom.xml
View File

@ -2,9 +2,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>FPChat</groupId> <groupId>com.fpghoti</groupId>
<artifactId>FPChat</artifactId> <artifactId>FPChat</artifactId>
<version>1.1.1</version> <version>1.1.2</version>
<repositories> <repositories>
<repository> <repository>
<id>spigot-repo</id> <id>spigot-repo</id>
@ -43,13 +43,6 @@
<version>3.0.0</version> <version>3.0.0</version>
<configuration> <configuration>
<source>1.7</source> <source>1.7</source>
<artifactSet>
<includes>
<include>org.apache.commons*:*</include>
</includes>
</artifactSet>
<relocations>
</relocations>
</configuration> </configuration>
<executions> <executions>
<execution> <execution>
@ -67,11 +60,13 @@
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>3.0</version> <version>3.0</version>
<scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.16.2-R0.1-SNAPSHOT</version> <version>1.16.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.MilkBowl</groupId> <groupId>com.github.MilkBowl</groupId>
@ -79,5 +74,11 @@
<version>1.7</version> <version>1.7</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -59,7 +59,6 @@ public class FPChat extends JavaPlugin {
private MySQLConnection sql; private MySQLConnection sql;
private static FPChat plugin; private static FPChat plugin;
private String pluginTag; private String pluginTag;
private int mysqlTimer = 1140;
private BukkitTask refresh = null; private BukkitTask refresh = null;
private PlayerListener listener = null; private PlayerListener listener = null;
private PlayerCache cache; private PlayerCache cache;
@ -235,14 +234,6 @@ public class FPChat extends JavaPlugin {
} }
} }
} }
if(config.mySQLEnabled()){ // mysql auto reconnect
if(mysqlTimer >= 1200){
sql.reconnect();
mysqlTimer = 0;
}else{
mysqlTimer++;
}
}
} }
}.runTaskTimerAsynchronously(this, 1*20, 1*20); }.runTaskTimerAsynchronously(this, 1*20, 1*20);
} }

View File

@ -2,12 +2,12 @@ package com.fpghoti.fpchatx.badge;
import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.config.BadgeConfig; import com.fpghoti.fpchatx.config.BadgeConfig;
import com.fpghoti.fpchatx.player.FPlayer;
public class Badge { public class Badge {
private static BadgeList badges; private static BadgeList badges;
private static BadgeConfig bconfig = null; private static BadgeConfig bconfig = null;
private static Badge zero = new Badge(0, "Empty", "", "", false);
public static void loadBadges() { public static void loadBadges() {
badges = new BadgeList(); badges = new BadgeList();
@ -22,6 +22,10 @@ public class Badge {
return bconfig; return bconfig;
} }
public static Badge getZero() {
return zero;
}
public static BadgeList getList() { public static BadgeList getList() {
return badges; return badges;
} }

View File

@ -0,0 +1,270 @@
package com.fpghoti.fpchatx.badge;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.player.FPlayer;
import com.fpghoti.fpchatx.util.Util;
public class BadgeData {
private FPlayer player;
private HashMap<Integer, Badge> slots;
private BadgeList badges;
public BadgeData(FPlayer player) {
this.player = player;
this.slots = new HashMap<Integer, Badge>();
badges = new BadgeList();
CompletableFuture.runAsync(() -> {
createPlayerData();
}).thenRunAsync(() -> {
getPlayerData();
savePlayerData();
});
}
public FPlayer getPlayer() {
return player;
}
public Badge getBadge(int slot) {
if(slot <= 0 || !FPChat.getPlugin().getMainConfig().mySQLEnabled() || slot > FPChat.getPlugin().getMainConfig().getMaxBadgeSlots()) {
return null;
}
return slots.get(slot);
}
public BadgeList getBadgeList() {
return this.badges;
}
public void clearLoadout() {
this.slots = new HashMap<Integer, Badge>();
savePlayerData();
}
public void clearBadgeList() {
this.badges = new BadgeList();
savePlayerData();
}
/**Will equip the badge regardless of the user's permissions.**/
public BadgeEquipResult setBadge(int slot, Badge badge) {
if(!FPChat.getPlugin().getMainConfig().mySQLEnabled()) {
return BadgeEquipResult.NO_SQL;
}
if(slot <= 0 || slot > FPChat.getPlugin().getMainConfig().getMaxBadgeSlots()) {
return BadgeEquipResult.INVALID_SLOT;
}
slots.put(slot, badge);
savePlayerData();
return BadgeEquipResult.SUCCESS;
}
/**Will not equip badge if user does not have permission
* to use the badge or slot.**/
public BadgeEquipResult equipBadge(int slot, Badge badge) {
if(!hasSlotPermission(slot)) {
return BadgeEquipResult.NO_PERMISSION_SLOT;
}
if(!player.hasPermission(badge.getPerm())) {
return BadgeEquipResult.NO_PERMISSION_BADGE;
}
return setBadge(slot, badge);
}
public String getAppearanceString() {
String appearance = "";
if(slots.isEmpty()) {
return "";
}
for(int slot = 1; slot <= FPChat.getPlugin().getMainConfig().getMaxBadgeSlots(); slot++) {
if(hasSlotPermission(slot)) {
Badge badge = slots.get(slot);
if(badge != null && badge.isEnabled() && player.hasPermission(badge.getPerm())) {
appearance = badge.getContents() + appearance;
}
}
}
return appearance;
}
public String toString() {
int max = FPChat.getPlugin().getMainConfig().getMaxBadgeSlots();
String val = "";
boolean first = true;
for(int slot = 1; slot <= max; slot++) {
Badge badge = getBadge(slot);
int id = 0;
if(badge != null) {
id = badge.getId();
}
if(first) {
first = false;
val = Integer.toString(id);
}else {
val = val + "," + Integer.toString(id);
}
}
return val;
}
private void setLoadoutFromString(String str) {
String[] array = str.split(",");
HashMap<Integer, Badge> loadout = new HashMap<Integer, Badge>();
for(int i = 0; i < array.length; i++) {
int slot = i + 1;
String item = array[i];
if(Util.isDigit(item)) {
int id = Integer.parseInt(item);
if(Badge.getList().containsId(id)) {
loadout.put(slot, Badge.getList().get(id));
}else {
loadout.put(slot, Badge.getZero());
}
}else {
loadout.put(slot, Badge.getZero());
}
}
}
private boolean hasSlotPermission(int slot) {
return player.hasPermission("fpchat.slot" + Integer.toString(slot)) || hasSlotBadge(slot);
}
private boolean hasSlotBadge(int slot) {
for(Badge b : Badge.getList().getSlotUnlockBadges(slot)) {
if(badges.containsId(b.getId())) {
return true;
}
}
return false;
}
private void createPlayerData() {
createSQLLoadoutEntry();
createSQLBadgeListEntry();
}
private void getPlayerData() {
String loadoutString = getLoadoutSQLString();
setLoadoutFromString(loadoutString);
String badgeListString = getBadgeListSQLString();
badges = BadgeList.fromString(badgeListString);
}
public void savePlayerData() {
saveNewBadgesToSQL();
setBadgeListSQLString(badges.toString());
setLoadoutSQLString(toString());
}
private void saveNewBadgesToSQL() {
for(Badge badge : Badge.getList()) {
if(player.hasPermission(badge.getPerm()) && !badges.containsId(badge.getId())) {
badges.add(badge);
}
}
}
private void createSQLLoadoutEntry(){
String uuid = player.getUniqueId().toString();
Connection connection = null;
try {
connection = FPChat.getPlugin().getMySQLConnection().getConnection();
if(!FPChat.getPlugin().getMySQLConnection().itemExists("player_uuid", uuid, FPChat.getPlugin().getMainConfig().getChatFeatureTable())){
FPChat.getPlugin().getMySQLConnection().update("INSERT INTO " + FPChat.getPlugin().getMainConfig().getChatFeatureTable() + " (player_uuid, badge_loadout) VALUES ( '" + uuid + "', '' )");
}
}catch(SQLException e) {
e.printStackTrace();
}finally{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private String getLoadoutSQLString() {
String uuid = player.getUniqueId().toString();
String value = null;
Connection connection = null;
try{
connection = FPChat.getPlugin().getMySQLConnection().getConnection();
ResultSet rs = FPChat.getPlugin().getMySQLConnection().query("SELECT * FROM " + FPChat.getPlugin().getMainConfig().getChatFeatureTable() + " WHERE player_uuid = '" + uuid + "';", connection);
if (rs.next()) {
value = (String) rs.getObject("badge_loadout");
}
}catch(SQLException e) {
FPChat.getPlugin().log(Level.SEVERE, "MySQL get error: " + e.getMessage());
e.printStackTrace();
}finally{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return value;
}
private void setLoadoutSQLString(String value) {
String uuid = player.getUniqueId().toString();
FPChat.getPlugin().getMySQLConnection().asyncUpdate("UPDATE " + FPChat.getPlugin().getMainConfig().getChatFeatureTable() + " SET badge_loadout = '" + value + "' WHERE player_uuid = '" + uuid + "';");
}
private void createSQLBadgeListEntry(){
String uuid = player.getUniqueId().toString();
Connection connection = null;
try {
connection = FPChat.getPlugin().getMySQLConnection().getConnection();
if(!FPChat.getPlugin().getMySQLConnection().itemExists("player_uuid", uuid, FPChat.getPlugin().getMainConfig().getPermSyncTable())){
FPChat.getPlugin().getMySQLConnection().update("INSERT INTO " + FPChat.getPlugin().getMainConfig().getPermSyncTable() + " (player_uuid, badges) VALUES ( '" + uuid + "', '' )");
}
}catch(SQLException e) {
e.printStackTrace();
}finally{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private String getBadgeListSQLString() {
String uuid = player.getUniqueId().toString();
String value = null;
Connection connection = null;
try{
connection = FPChat.getPlugin().getMySQLConnection().getConnection();
ResultSet rs = FPChat.getPlugin().getMySQLConnection().query("SELECT * FROM " + FPChat.getPlugin().getMainConfig().getPermSyncTable() + " WHERE player_uuid = '" + uuid + "';", connection);
if (rs.next()) {
value = (String) rs.getObject("badges");
}
}catch(SQLException e) {
FPChat.getPlugin().log(Level.SEVERE, "MySQL get error: " + e.getMessage());
e.printStackTrace();
}finally{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return value;
}
private void setBadgeListSQLString(String value) {
String uuid = player.getUniqueId().toString();
FPChat.getPlugin().getMySQLConnection().asyncUpdate("UPDATE " + FPChat.getPlugin().getMainConfig().getPermSyncTable() + " SET badges = '" + value + "' WHERE player_uuid = '" + uuid + "';");
}
}

View File

@ -0,0 +1,36 @@
package com.fpghoti.fpchatx.badge;
public enum BadgeEquipResult {
SUCCESS{
@Override
public String toString() {
return "Success";
}
},
INVALID_SLOT{
@Override
public String toString() {
return "Invalid slot";
}
},
NO_SQL{
@Override
public String toString() {
return "SQL is not enabled";
}
},
NO_PERMISSION_BADGE{
@Override
public String toString() {
return "User does not have permission for badge";
}
},
NO_PERMISSION_SLOT{
@Override
public String toString() {
return "User does not have permission for slot";
}
};
}

View File

@ -3,11 +3,27 @@ package com.fpghoti.fpchatx.badge;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import com.fpghoti.fpchatx.util.Util;
public class BadgeList implements Iterable<Badge>{ public class BadgeList implements Iterable<Badge>{
public static BadgeList fromString(String str) {
String[] array = str.split(",");
BadgeList badgeList = new BadgeList();
for(String item : array) {
if(Util.isDigit(item)) {
int id = Integer.parseInt(item);
if(Badge.getList().containsId(id) && !badgeList.containsId(id)) {
Badge badge = Badge.getList().get(id);
badgeList.add(badge);
}
}
}
return badgeList;
}
private ArrayList<Badge> list = new ArrayList<Badge>(); private ArrayList<Badge> list = new ArrayList<Badge>();
private Badge empty = new Badge(0, "Empty", "", "", false);
@Override @Override
public Iterator<Badge> iterator() { public Iterator<Badge> iterator() {
@ -24,6 +40,13 @@ public class BadgeList implements Iterable<Badge>{
list.add(badge); list.add(badge);
} }
public void add(int id) {
Badge badge = Badge.getList().get(id);
if(badge != null) {
add(badge);
}
}
public void remove(int id) { public void remove(int id) {
ArrayList<Badge> rl = new ArrayList<Badge>(list); ArrayList<Badge> rl = new ArrayList<Badge>(list);
for(Badge b : rl) { for(Badge b : rl) {
@ -46,20 +69,46 @@ public class BadgeList implements Iterable<Badge>{
return false; return false;
} }
public int getLargestId() {
int largest = 0;
for(Badge b : list) {
if(b.getId() > largest) {
largest = b.getId();
}
}
return largest;
}
public String toString() {
boolean first = true;
String val = "";
for(int id = 1; id <= getLargestId(); id++) {
if(containsId(id)) {
if(first) {
first = false;
val = Integer.toString(id);
}else {
val = val + "," + Integer.toString(id);
}
}
}
return val;
}
public boolean overwrites(Badge badge) { public boolean overwrites(Badge badge) {
return containsId(badge.getId()); return containsId(badge.getId());
} }
public Badge get(int id) { public Badge get(int id) {
if(id <= 0 || !containsId(id)) { if(id <= 0 || !containsId(id)) {
return empty; return Badge.getZero();
} }
for(Badge b : list) { for(Badge b : list) {
if(b.getId() == id) { if(b.getId() == id) {
return b; return b;
} }
} }
return empty; return Badge.getZero();
} }
public Badge getIndex(int index) { public Badge getIndex(int index) {

View File

@ -1,165 +0,0 @@
package com.fpghoti.fpchatx.badge;
import java.util.UUID;
import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.player.FPlayer;
import com.fpghoti.fpchatx.util.Util;
public class Sync {
public static void syncBadges(FPlayer p, boolean create){
UUID id = p.getUniqueId();
String uuid = id.toString();
Util.connect();
if(create && !FPChat.getPlugin().getMySQLConnection().itemExists("player_uuid", uuid, FPChat.getPlugin().getMainConfig().getPermSyncTable())){
createPlayer(p);
}
String raw = (String) FPChat.getPlugin().getMySQLConnection().get("badges", "player_uuid", "=", uuid, FPChat.getPlugin().getMainConfig().getPermSyncTable());
if(raw != null && raw.length() > 0 && raw.charAt(0) == ','){
raw = raw.substring(1);
}
if(raw != null && !raw.equals("")) {
String list[] = Util.stripLast(raw).split(",");
for(String item : list) {
if(!item.equals("")) {
Integer badgeId = Integer.parseInt(item);
p.addSyncedBadge(badgeId);
}
}
}
}
public static Boolean syncExists(FPlayer p){
Boolean check = false;
if(p == null) {
return false;
}
String uuid = p.getUniqueId().toString();
Util.connect();
if(FPChat.getPlugin().getMySQLConnection().itemExists("player_uuid", uuid, FPChat.getPlugin().getMainConfig().getPermSyncTable())){
check = true;
}
return check;
}
public static void update(FPlayer p) {
update(p, true);
}
public static void update(FPlayer p, boolean create){
String uuid = p.getUniqueId().toString();
Util.connect();
if(create && !FPChat.getPlugin().getMySQLConnection().itemExists("player_uuid", uuid, FPChat.getPlugin().getMainConfig().getPermSyncTable())){
createPlayer(p);
}
syncBadges(p, false);
String nl = "";
String nl2 = "";
String permstring = getBadgeString(p);
String[] permids = permstring.split(",");
for(String item : permids){
if(Util.isDigit(item)) {
int id = Integer.parseInt(item);
if(!p.isSynced(id)){
nl = nl + item + ",";
}
}
}
for(int id : p.syncedList()){
String item = Integer.toString(id);
nl2 = nl2 + item + ",";
}
nl = nl + nl2;
FPChat.getPlugin().getMySQLConnection().set("badges", nl, "player_uuid", "=", uuid, FPChat.getPlugin().getMainConfig().getPermSyncTable());
syncBadges(p,false);
}
public static void revoke(FPlayer p) {
revoke(p,true);
}
public static void revoke(FPlayer p, boolean create) {
String uuid = p.getUniqueId().toString();
Util.connect();
if(create && !FPChat.getPlugin().getMySQLConnection().itemExists("player_uuid", uuid, FPChat.getPlugin().getMainConfig().getPermSyncTable())){
createPlayer(p);
}
syncBadges(p, false);
String nl = "";
FPChat.getPlugin().getMySQLConnection().set("badges", nl, "player_uuid", "=", uuid, FPChat.getPlugin().getMainConfig().getPermSyncTable());
syncBadges(p, false);
}
public static void revoke(FPlayer p, int badgeId){
revoke(p, badgeId, true);
}
public static void revoke(FPlayer p, int badgeId, boolean create){
String uuid = p.getUniqueId().toString();
Util.connect();
if(create && !FPChat.getPlugin().getMySQLConnection().itemExists("player_uuid", uuid, FPChat.getPlugin().getMainConfig().getPermSyncTable())){
createPlayer(p);
}
syncBadges(p, false);
String nl = "";
String nl2 = "";
for(String item : revokeBadgeString(p, badgeId).split(",")){
if(Util.isDigit(item)) {
int id = Integer.parseInt(item);
if(p.isSynced(id)){
nl = nl + item + ",";
}
}
}
for(int id : p.syncedList()){
String item = Integer.toString(id);
if(id != badgeId){
nl2 = nl2 + item + ",";
}
}
nl = nl + nl2;
FPChat.getPlugin().getMySQLConnection().set("badges", nl, "player_uuid", "=", uuid, FPChat.getPlugin().getMainConfig().getPermSyncTable());
syncBadges(p, false);
}
public static String getBadgeString(FPlayer p){
String list = "";
for(Badge badge : Badge.getList()){
int id = badge.getId();
if(badge.isEnabled() && p.hasPermission("fpchat.badge." + badge.getPerm())){
String add = Integer.toString(id) + ",";
list = list + add;
}else if(p.getBadgeQueue().contains(id)) {
String add = Integer.toString(id) + ",";
list = list + add;
p.unqueueBadge(id);
}
}
return list;
}
public static String revokeBadgeString(FPlayer p, int badgeId){
String list = "";
for(Badge badge : Badge.getList()){
int id = badge.getId();
if(p.hasPermission("fpchat.badge." + badge.getPerm())){
if(id != badgeId){
String add = Integer.toString(id) + ",";
list = list + add;
}
}
}
return list;
}
public static void createPlayer(FPlayer p){
String uuid = p.getUniqueId().toString();
Util.connect();
if(!FPChat.getPlugin().getMySQLConnection().itemExists("player_uuid", uuid, FPChat.getPlugin().getMainConfig().getPermSyncTable())){
FPChat.getPlugin().getMySQLConnection().insertInto("player_uuid, badges", " '" + uuid + "', '' ", FPChat.getPlugin().getMainConfig().getPermSyncTable());
}
}
}

View File

@ -10,7 +10,6 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.badge.Badge;
import com.fpghoti.fpchatx.config.ChannelFile; import com.fpghoti.fpchatx.config.ChannelFile;
import com.fpghoti.fpchatx.customcodes.BubbleCode; import com.fpghoti.fpchatx.customcodes.BubbleCode;
import com.fpghoti.fpchatx.customcodes.Codify; import com.fpghoti.fpchatx.customcodes.Codify;
@ -297,35 +296,24 @@ public abstract class ChatChannel {
} }
public String format(FPlayer p, String msg) { public String format(FPlayer p, String msg) {
String finalMessage = ""; String finalMessage = "", badges = "", stf = "", filler = "";
String slot3 = "", slot2 = "", slot1 = "";
String stf = "";
if (Permission.canUseColor(p)) {
msg = msg.replaceAll("&([0-9a-fk-or])", "§$1");
} else {
msg = msg.replaceAll("§[0-9a-fk-or]", "");
}
String header = PrepareChat.swapPlaceholders(p, this, msg); String header = PrepareChat.swapPlaceholders(p, this, msg);
if(Permission.isStaff(p)){ if(Permission.isStaff(p)){
stf = FPChat.getPlugin().getMainConfig().getStaffBadge(); stf = FPChat.getPlugin().getMainConfig().getStaffBadge();
} }
if(plugin.getMainConfig().mySQLEnabled()){ if(plugin.getMainConfig().mySQLEnabled()){
Integer[] badges = p.getBadges(); badges = p.getBadgeData().getAppearanceString();
slot1 = Badge.getList().get(badges[0]).getContents();
slot2 = Badge.getList().get(badges[1]).getContents();
slot3 = Badge.getList().get(badges[2]).getContents();
} }
String filler = "";
if(plugin.getMainConfig().chatFilterEnabled()){ if(plugin.getMainConfig().chatFilterEnabled()){
filler = "word "; filler = "word ";
} }
if(Permission.canUseColor(p)){ if(Permission.canUseColor(p)){
String last = ChatFilter.filter(filler + msg); String last = ChatFilter.filter(filler + msg);
last = BubbleCode.bubblecode(Permission.canBubbleCode(p), Codify.changeFormatSign(last)); last = BubbleCode.bubblecode(Permission.canBubbleCode(p), Codify.changeFormatSign(last));
finalMessage = stf + slot3 + slot2 + slot1 + header + last; finalMessage = ChatColor.translateAlternateColorCodes('&', stf + badges + header + last);
}else{ }else{
String newmsg = ChatColor.stripColor(ChatColor.translateAlternateColorCodes('§', ChatFilter.filter(filler + msg))); String newmsg = ChatColor.stripColor(ChatColor.translateAlternateColorCodes('§', ChatFilter.filter(filler + msg)));
finalMessage = stf + slot3 + slot2 + slot1 + Codify.removeBubbles(header + newmsg); finalMessage = stf + badges + Codify.removeBubbles(header + newmsg);
} }
return finalMessage; return finalMessage;
} }

View File

@ -38,12 +38,7 @@ public class BadgeClearCommand extends Commands {
} }
FPlayer p = FPlayer.getPlayer((Player)sender); FPlayer p = FPlayer.getPlayer((Player)sender);
p.setBadge(1, 0); p.getBadgeData().clearLoadout();
p.updateBadges(1, 0);
p.setBadge(2, 0);
p.updateBadges(2, 0);
p.setBadge(3, 0);
p.updateBadges(3, 0);
p.sendMessage(FPChat.logo() + ChatColor.YELLOW + " Unequipped all badges!"); p.sendMessage(FPChat.logo() + ChatColor.YELLOW + " Unequipped all badges!");
} }

View File

@ -6,6 +6,7 @@ import org.bukkit.entity.Player;
import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.badge.Badge; import com.fpghoti.fpchatx.badge.Badge;
import com.fpghoti.fpchatx.badge.BadgeEquipResult;
import com.fpghoti.fpchatx.command.Commands; import com.fpghoti.fpchatx.command.Commands;
import com.fpghoti.fpchatx.player.FPlayer; import com.fpghoti.fpchatx.player.FPlayer;
import com.fpghoti.fpchatx.util.Util; import com.fpghoti.fpchatx.util.Util;
@ -42,16 +43,24 @@ public class BadgeEquipCommand extends Commands {
if(Util.isDigit(args[0]) && Util.isDigit(args[1])){ if(Util.isDigit(args[0]) && Util.isDigit(args[1])){
Integer slot = Integer.parseInt(args[0]), badgeId = Integer.parseInt(args[1]); Integer slot = Integer.parseInt(args[0]), badgeId = Integer.parseInt(args[1]);
if(p.canUseSlot(slot)){
if(badgeId == 0 || (Badge.getList().containsId(badgeId) && Badge.getList().get(badgeId).isEnabled() && p.hasBadge(badgeId))){ if(!Badge.getList().containsId(badgeId)) {
p.setBadge(slot, badgeId); p.sendMessage(FPChat.logo() + ChatColor.RED + " Invalid badge!");
p.updateBadges(slot, badgeId); return;
p.sendMessage( FPChat.logo() + ChatColor.YELLOW + " You have equipped a badge!");
}else{
p.sendMessage(FPChat.logo() + ChatColor.YELLOW + " You do not have permission to equip this badge!");
} }
}else{
p.sendMessage(FPChat.logo() + ChatColor.YELLOW + " You do not have permission to equip a badge in this slot!"); Badge badge = Badge.getList().get(badgeId);
BadgeEquipResult result = p.getBadgeData().equipBadge(slot, badge);
switch(result) {
case SUCCESS:
p.sendMessage(FPChat.logo() + ChatColor.YELLOW + " You successfully equiped a badge!");
break;
default:
p.sendMessage(FPChat.logo() + ChatColor.RED + " Could not equip badge: " + result.toString());
} }
} }
} }

View File

@ -49,7 +49,7 @@ public class BadgeListCommand extends Commands {
} }
} }
BadgeList list = p.getSyncedBadgeList(); BadgeList list = p.getBadgeData().getBadgeList();
int pageCount = (int) Math.ceil((double) list.getListSize() / 8); int pageCount = (int) Math.ceil((double) list.getListSize() / 8);
if (pg > pageCount) { if (pg > pageCount) {

View File

@ -6,7 +6,6 @@ import org.bukkit.entity.Player;
import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.badge.Badge; import com.fpghoti.fpchatx.badge.Badge;
import com.fpghoti.fpchatx.badge.Sync;
import com.fpghoti.fpchatx.command.Commands; import com.fpghoti.fpchatx.command.Commands;
import com.fpghoti.fpchatx.permission.Permission; import com.fpghoti.fpchatx.permission.Permission;
import com.fpghoti.fpchatx.player.FPlayer; import com.fpghoti.fpchatx.player.FPlayer;
@ -47,9 +46,10 @@ public class GiveBadgeCommand extends Commands {
} }
if(Util.isDigit(args[1]) && Badge.getList().containsId(Integer.parseInt(args[1]))) { if(Util.isDigit(args[1]) && Badge.getList().containsId(Integer.parseInt(args[1]))) {
int id = Integer.parseInt(args[1]); int id = Integer.parseInt(args[1]);
toGive.queueBadgeAdd(id);
Sync.update(toGive); toGive.getBadgeData().getBadgeList().add(id);
toGive.addSyncedBadge(id); toGive.getBadgeData().savePlayerData();
FPlayer.goodMsg(p, "Badge granted."); FPlayer.goodMsg(p, "Badge granted.");
}else { }else {
FPlayer.errMsg(p,"Wrong command usage."); FPlayer.errMsg(p,"Wrong command usage.");

View File

@ -10,7 +10,6 @@ import org.bukkit.entity.Player;
import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.badge.Badge; import com.fpghoti.fpchatx.badge.Badge;
import com.fpghoti.fpchatx.badge.Sync;
import com.fpghoti.fpchatx.command.Commands; import com.fpghoti.fpchatx.command.Commands;
import com.fpghoti.fpchatx.permission.Permission; import com.fpghoti.fpchatx.permission.Permission;
import com.fpghoti.fpchatx.player.FPlayer; import com.fpghoti.fpchatx.player.FPlayer;
@ -49,18 +48,19 @@ public class RevokeBadgeCommand extends Commands {
OfflinePlayer o = Bukkit.getOfflinePlayer(u); OfflinePlayer o = Bukkit.getOfflinePlayer(u);
if(o != null) { if(o != null) {
FPlayer toRevoke = FPlayer.getPlayer(o,true); FPlayer toRevoke = FPlayer.getPlayer(o,true);
if(Sync.syncExists(toRevoke) && (Util.isDigit(args[1]) || args[1].equals("*") )){ if(Util.isDigit(args[1]) || args[1].equals("*")){
if(args[1].equals("*")) { if(args[1].equals("*")) {
Sync.revoke(toRevoke); toRevoke.getBadgeData().clearBadgeList();
FPlayer.goodMsg(p, "All badges revoked from the specified player."); FPlayer.goodMsg(p, "All badges revoked from the specified player.");
}else { }else {
if(Badge.getList().containsId(Integer.parseInt(args[1]))) { if(Badge.getList().containsId(Integer.parseInt(args[1]))) {
int id = Integer.parseInt(args[1]); int id = Integer.parseInt(args[1]);
Sync.revoke(toRevoke, id);
toRevoke.removeSyncedBadge(id); toRevoke.getBadgeData().getBadgeList().remove(id);
toRevoke.clearUnownedBadges(); toRevoke.getBadgeData().savePlayerData();
FPlayer.goodMsg(p, "Badge revoked from the specified player."); FPlayer.goodMsg(p, "Badge revoked from the specified player.");
}else { }else {
FPlayer.errMsg(p, "Invalid badge."); FPlayer.errMsg(p, "Invalid badge.");

View File

@ -9,6 +9,7 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.util.Util;
public class MainConfig { public class MainConfig {
@ -121,6 +122,10 @@ public class MainConfig {
config.createSection("PluginTag"); config.createSection("PluginTag");
config.set("PluginTag", "&a&lFPChat&r"); config.set("PluginTag", "&a&lFPChat&r");
} }
if (config.get("MaxBadgeSlots")==null){
config.createSection("MaxBadgeSlots");
config.set("MaxBadgeSlots", "3");
}
try { try {
config.save(configFile); config.save(configFile);
} catch (IOException e) { } catch (IOException e) {
@ -263,4 +268,12 @@ public class MainConfig {
return ChatColor.translateAlternateColorCodes('&',config.getString("PluginTag")); return ChatColor.translateAlternateColorCodes('&',config.getString("PluginTag"));
} }
public int getMaxBadgeSlots() {
String val = config.getString("MaxBadgeSlots");
if(Util.isDigit(val)) {
return Integer.parseInt(val);
}
return 0;
}
} }

View File

@ -1,16 +1,15 @@
package com.fpghoti.fpchatx.mysql; package com.fpghoti.fpchatx.mysql;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.scheduler.BukkitRunnable;
import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.config.MainConfig; import com.fpghoti.fpchatx.config.MainConfig;
import com.zaxxer.hikari.HikariDataSource;
public class MySQLConnection{ public class MySQLConnection{
@ -18,6 +17,7 @@ public class MySQLConnection{
private FPChat plugin; private FPChat plugin;
private String host, user, password, database, port; private String host, user, password, database, port;
private MainConfig config; private MainConfig config;
private HikariDataSource hikari;
public MySQLConnection(FPChat plugin) { public MySQLConnection(FPChat plugin) {
this.plugin = plugin; this.plugin = plugin;
@ -27,28 +27,39 @@ public class MySQLConnection{
password = config.getPassword(); password = config.getPassword();
database = config.getDatabase(); database = config.getDatabase();
port = config.getPort(); port = config.getPort();
} hikari = new HikariDataSource();
hikari.setMaximumPoolSize(10);
private Connection connection; hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
hikari.addDataSourceProperty("serverName", host);
public Connection getConnection(){ hikari.addDataSourceProperty("user", user);
return connection; hikari.addDataSourceProperty("password", password);
hikari.addDataSourceProperty("databaseName", database);
hikari.addDataSourceProperty("port", port);
} }
public void generate() { public void generate() {
if(config.mySQLEnabled()){ if(config.mySQLEnabled()){
plugin.log(Level.INFO, "Connecting to MySQL..."); plugin.log(Level.INFO, "Connecting to MySQL...");
connect(); Connection connection = null;
if(!tableExists(config.getChatFeatureTable())){ try {
connection = hikari.getConnection();
if(!tableExists(config.getChatFeatureTable(), connection)){
plugin.log(Level.INFO, "FPChat table not found. Creating new table..."); plugin.log(Level.INFO, "FPChat table not found. Creating new table...");
//createTable(config.getChatFeatureTable(), "player_uuid VARCHAR (36), badge_slot1 INT (11), badge_slot2 INT (11), badge_slot3 INT (11)"); update("CREATE TABLE " + config.getChatFeatureTable() + " (player_uuid VARCHAR (36), badge_loadout TEXT, PRIMARY KEY(player_uuid))");
update("CREATE TABLE " + config.getChatFeatureTable() + " (player_uuid VARCHAR (36), badge_slot1 INT (11), badge_slot2 INT (11), badge_slot3 INT (11), PRIMARY KEY(player_uuid))");
plugin.log(Level.INFO, "FPChat table created!"); plugin.log(Level.INFO, "FPChat table created!");
} }
if(!tableExists(config.getPermSyncTable())){ if(!tableExists(config.getPermSyncTable(), connection)){
//createTable(config.getPermSyncTable(), "player_uuid VARCHAR (36), badges TEXT");
update("CREATE TABLE " + config.getPermSyncTable() + " (player_uuid VARCHAR (36), badges TEXT, PRIMARY KEY(player_uuid))"); update("CREATE TABLE " + config.getPermSyncTable() + " (player_uuid VARCHAR (36), badges TEXT, PRIMARY KEY(player_uuid))");
} }
}catch(SQLException e) {
e.printStackTrace();
}finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
plugin.log(Level.INFO, "FPChat successfully connected to MySQL!"); plugin.log(Level.INFO, "FPChat successfully connected to MySQL!");
plugin.log(Level.INFO, "Badges have been enabled!"); plugin.log(Level.INFO, "Badges have been enabled!");
}else{ }else{
@ -56,66 +67,15 @@ public class MySQLConnection{
} }
} }
public void connect(){ public ResultSet query(String query, Connection connection){
if (host.equalsIgnoreCase("") || host == null) {
plugin.log(Level.SEVERE, "You have not specified a host in the FPChatX config!");
} else if (user.equalsIgnoreCase("") || user == null) {
plugin.log(Level.SEVERE, "You have not specified a user in the FPChatX config!");
} else if (password.equalsIgnoreCase("") || password == null) {
plugin.log(Level.SEVERE, "You have not specified a password in the FPChatX config!");
} else if (database.equalsIgnoreCase("") || database == null) {
plugin.log(Level.SEVERE, "You have not specified a database in the FPChatX config!");
} else {
login();
}
}
public void disconnect(){
try{
if (getConnection() != null){
connection.close();
}
else{
plugin.log(Level.SEVERE, "There was an issue with MySQL: FPChatX is not currently connected to a database.");
}
}catch(SQLException e){
plugin.log(Level.SEVERE, "There was an issue with MySQL: " + e.getMessage());
e.printStackTrace();
}
connection = null;
}
public void reconnect(){
disconnect();
connect();
}
public void login(){
try{
if (getConnection() != null){
connection.close();
}
}
catch (Exception e){}
connection = null;
try{
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);
}catch(SQLException e){
plugin.log(Level.SEVERE, "There was an issue with MySQL: " + e.getMessage());
e.printStackTrace();
}
}
public ResultSet query(String query){
if (query == null) { if (query == null) {
return null; return null;
} }
connect();
ResultSet results = null; ResultSet results = null;
try{ try{
Statement statement = getConnection().createStatement(); Statement statement = connection.createStatement();
results = statement.executeQuery(query); results = statement.executeQuery(query);
}catch(SQLException e){ }catch(SQLException e){
plugin.log(Level.SEVERE, "There has been an error:" + e.getMessage()); plugin.log(Level.SEVERE, "There has been an error:" + e.getMessage());
plugin.log(Level.SEVERE,"Failed Query in MySQL using the following query input:"); plugin.log(Level.SEVERE,"Failed Query in MySQL using the following query input:");
@ -129,34 +89,46 @@ public class MySQLConnection{
if (input == null){ if (input == null){
return; return;
} }
connect(); Statement statement;
try{ Connection connection = null;
Statement statement = getConnection().createStatement(); try {
connection = hikari.getConnection();
statement = connection.createStatement();
statement.executeUpdate(input); statement.executeUpdate(input);
statement.close(); statement.close();
}catch(SQLException e){ } catch (SQLException e) {
plugin.log(Level.SEVERE, "There has been an error:" + e.getMessage()); e.printStackTrace();
plugin.log(Level.SEVERE,"Failed to update MySQL using the following update input:"); }finally {
plugin.log(Level.SEVERE, input); try {
connection.close();
} catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
public boolean tableExists(String tablename){ public void asyncUpdate(String input){
CompletableFuture.runAsync(() -> {
update(input);
});
}
public boolean tableExists(String tablename, Connection connection){
if (tablename == null) { if (tablename == null) {
return false; return false;
} }
try{ try{
if (getConnection() == null) { if (connection == null) {
return false; return false;
} }
if (getConnection().getMetaData() == null) { if (connection.getMetaData() == null) {
return false; return false;
} }
ResultSet results = getConnection().getMetaData().getTables(null, null, tablename, null); ResultSet results = connection.getMetaData().getTables(null, null, tablename, null);
if (results.next()) { if (results.next()) {
return true; return true;
} }
}catch(SQLException e) { }catch(SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -167,67 +139,53 @@ public class MySQLConnection{
if (data != null) { if (data != null) {
data = "'" + data + "'"; data = "'" + data + "'";
} }
Connection connection = null;
try{ try{
ResultSet results = query("SELECT * FROM " + table + " WHERE " + column + "=" + data); connection = hikari.getConnection();
ResultSet results = query("SELECT * FROM " + table + " WHERE " + column + "=" + data, connection);
while (results.next()) { while (results.next()) {
if (results.getString(column) != null) { if (results.getString(column) != null) {
return true; return true;
} }
} }
}catch(SQLException e) { }catch(SQLException e) {
plugin.log(Level.SEVERE, "MYSQL itemExists error: " + e.getMessage()); plugin.log(Level.SEVERE, "MYSQL itemExists error: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
}finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
return false; return false;
} }
public void createTable(String table, String columns){ public void createTable(String table, String columns){
if (!tableExists(table)) { Connection connection = null;
try {
connection = hikari.getConnection();
if (!tableExists(table, connection)) {
update("CREATE TABLE " + table + " (" + columns + ")"); update("CREATE TABLE " + table + " (" + columns + ")");
} }
}
public void insertInto(final String columns, final String values, final String table){
new BukkitRunnable() {
@Override
public void run() {
update("INSERT INTO " + table + " (" + columns + ") VALUES (" + values + ")");
}
}.runTaskAsynchronously(plugin);
}
public void set(final String selected, final Object object, final String column, final String equality, final String data, final String table){
new BukkitRunnable() {
@Override
public void run() {
Object gobject = object;
String gdata = data;
if (gobject != null) {
gobject = "'" + gobject + "'";
}
if (gdata != null) {
gdata = "'" + gdata + "'";
}
update("UPDATE " + table + " SET " + selected + "=" + gobject + " WHERE " + column + equality + gdata + ";");
}
}.runTaskAsynchronously(plugin);
}
public Object get(String selected, String column, String equality, String data, String table){
if (data != null) {
data = "'" + data + "'";
}
try{
ResultSet rs = query("SELECT * FROM " + table + " WHERE " + column + equality + data);
if (rs.next()) {
return rs.getObject(selected);
}
}catch(SQLException e) { }catch(SQLException e) {
plugin.log(Level.SEVERE, "MySQL get error: " + e.getMessage()); e.printStackTrace();
}finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null;
} }
}
public Connection getConnection() throws SQLException {
return hikari.getConnection();
}
public void disconnect() {
hikari.close();
}
} }

View File

@ -2,7 +2,6 @@ package com.fpghoti.fpchatx.player;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.UUID; import java.util.UUID;
@ -16,9 +15,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.badge.Badge; import com.fpghoti.fpchatx.badge.BadgeData;
import com.fpghoti.fpchatx.badge.BadgeList;
import com.fpghoti.fpchatx.badge.Sync;
import com.fpghoti.fpchatx.chat.ChatChannel; import com.fpghoti.fpchatx.chat.ChatChannel;
import com.fpghoti.fpchatx.chat.ChatFilter; import com.fpghoti.fpchatx.chat.ChatFilter;
import com.fpghoti.fpchatx.chat.PrepareChat; import com.fpghoti.fpchatx.chat.PrepareChat;
@ -118,22 +115,19 @@ public class FPlayer {
private String primaryChannel; private String primaryChannel;
private String primaryTempChannel; private String primaryTempChannel;
private int shoutCooldown; private int shoutCooldown;
private Integer[] badges;
private UUID lastMsg; private UUID lastMsg;
private boolean spy; private boolean spy;
private boolean shoutVisible; private boolean shoutVisible;
private boolean hushed; private boolean hushed;
private ArrayList<Integer> synced;
private boolean toShout; private boolean toShout;
private boolean toTalk; private boolean toTalk;
private ChatChannel talkChannel; private ChatChannel talkChannel;
private ArrayList<Integer> giveBadgeQueue; private BadgeData badgeData;
private FPlayer(OfflinePlayer p) { private FPlayer(OfflinePlayer p) {
this.toShout = false; this.toShout = false;
this.toTalk = false; this.toTalk = false;
this.talkChannel = null; this.talkChannel = null;
this.synced = new ArrayList<Integer>();
this.offlinePlayer = p; this.offlinePlayer = p;
this.name = p.getName(); this.name = p.getName();
this.uuid = p.getUniqueId(); this.uuid = p.getUniqueId();
@ -144,7 +138,6 @@ public class FPlayer {
this.spy = pfile.isSpy(); this.spy = pfile.isSpy();
this.shoutVisible = pfile.shoutVisible(); this.shoutVisible = pfile.shoutVisible();
this.hushed = pfile.isHushed(); this.hushed = pfile.isHushed();
this.giveBadgeQueue = new ArrayList<Integer>();
String rawignore = pfile.getIgnore(); String rawignore = pfile.getIgnore();
if(!rawignore.equals("")) { if(!rawignore.equals("")) {
for(String s : rawignore.split(",")) { for(String s : rawignore.split(",")) {
@ -173,12 +166,7 @@ public class FPlayer {
} }
} }
if(FPChat.getPlugin().getMainConfig().mySQLEnabled()) { if(FPChat.getPlugin().getMainConfig().mySQLEnabled()) {
Util.connect(); this.badgeData = new BadgeData(this);
Sync.update(this);
this.badges = getSQLBadges();
}else {
Integer[] empt = {0,0,0};
this.badges = empt;
} }
} }
@ -190,127 +178,6 @@ public class FPlayer {
return this.name; return this.name;
} }
public void updateBadges(int slot, int id) {
if(!Badge.getList().get(id).isEnabled()) {
badges[slot-1] = 0;
return;
}
badges[slot-1] = id;
}
public void clearUnownedBadges() {
if(!hasBadge(badges[0]) || !Badge.getList().get(badges[0]).isEnabled()) {
setBadge(1, 0);
updateBadges(1,0);
}
if(!hasBadge(badges[1])|| !Badge.getList().get(badges[1]).isEnabled()) {
setBadge(2, 0);
updateBadges(2,0);
}
if(!hasBadge(badges[2])|| !Badge.getList().get(badges[2]).isEnabled()) {
setBadge(3, 0);
updateBadges(3,0);
}
}
public Integer[] getSQLBadges(){
UUID id = uuid;
String uuid = id.toString();
Integer badge1 = 0, badge2 = 0, badge3 = 0;
Util.connect();
if(!FPChat.getPlugin().getMySQLConnection().itemExists("player_uuid", uuid, FPChat.getPlugin().getMainConfig().getChatFeatureTable())){
createPlayer();
}
badge1 = (Integer) FPChat.getPlugin().getMySQLConnection().get("badge_slot1", "player_uuid", "=", uuid, FPChat.getPlugin().getMainConfig().getChatFeatureTable());
badge2 = (Integer)FPChat.getPlugin().getMySQLConnection().get("badge_slot2", "player_uuid", "=", uuid, FPChat.getPlugin().getMainConfig().getChatFeatureTable());
badge3 = (Integer)FPChat.getPlugin().getMySQLConnection().get("badge_slot3", "player_uuid", "=", uuid, FPChat.getPlugin().getMainConfig().getChatFeatureTable());
Integer[] badges = {badge1, badge2, badge3};
return badges;
}
public Boolean hasBadge(int id){
if(id == 0) {
return true;
}
return hasPermission("fpchat.badge." + Badge.getList().get(id).getPerm()) || isSynced(id);
}
public void setBadge(int slot, int badgeId){
if(!Badge.getList().get(badgeId).isEnabled() && badgeId != 0) {
return;
}
if(slot > 3){
slot = 3;
}else if(slot < 1){
slot = 1;
}
Util.connect();
if(!FPChat.getPlugin().getMySQLConnection().itemExists("player_uuid", uuid.toString(), FPChat.getPlugin().getMainConfig().getChatFeatureTable())){
createPlayer();
}
FPChat.getPlugin().getMySQLConnection().set("badge_slot" + String.valueOf(slot), badgeId, "player_uuid", "=", uuid.toString(), FPChat.getPlugin().getMainConfig().getChatFeatureTable());
getSQLBadges();
}
public void createPlayer(){
Util.connect();
if(!FPChat.getPlugin().getMySQLConnection().itemExists("player_uuid", uuid.toString(), FPChat.getPlugin().getMainConfig().getChatFeatureTable())){
FPChat.getPlugin().getMySQLConnection().insertInto("player_uuid, badge_slot1, badge_slot2, badge_slot3", " '" + uuid + "', '0', '0', '0' ", FPChat.getPlugin().getMainConfig().getChatFeatureTable());
}
}
public Boolean canUseSlot(int slotid){
if(slotid == 1){
return hasPermission("fpchat.slot1") || hasSlotBadge(1);
}else if(slotid == 2){
return hasPermission("fpchat.slot2") || hasSlotBadge(2);
}else if(slotid == 3){
return hasPermission("fpchat.slot3") || hasSlotBadge(3);
}
return false;
}
private boolean hasSlotBadge(int slot) {
for(Badge b : Badge.getList().getSlotUnlockBadges(slot)) {
if(hasBadge(b.getId())) {
return true;
}
}
return false;
}
public void addSyncedBadge(Integer id) {
if(!synced.contains(id)) {
this.synced.add(id);
}
}
public void removeSyncedBadge(Integer id) {
this.synced.remove(id);
}
public boolean isSynced(Integer id) {
return this.synced.contains(id);
}
public ArrayList<Integer> syncedList(){
return this.synced;
}
public BadgeList getSyncedBadgeList() {
BadgeList list = new BadgeList();
ArrayList<Integer> sc = new ArrayList<Integer>(synced);
Collections.sort(sc);
for(Integer i : sc) {
Badge badge = Badge.getList().get(i);
if(badge.isEnabled()) {
list.add(badge);
}
}
return list;
}
public int getShoutCooldown() { public int getShoutCooldown() {
return this.shoutCooldown; return this.shoutCooldown;
} }
@ -359,6 +226,10 @@ public class FPlayer {
this.shoutVisible = false; this.shoutVisible = false;
} }
public BadgeData getBadgeData() {
return this.badgeData;
}
public boolean setPrefix(String prefix) { public boolean setPrefix(String prefix) {
if(isOnline() && getPlayer() != null) { if(isOnline() && getPlayer() != null) {
World world = null; World world = null;
@ -388,13 +259,6 @@ public class FPlayer {
this.hushed = false; this.hushed = false;
} }
public Integer[] getBadges() {
if(badges[0] == null || badges[1] == null || badges[2] == null) {
badges = getSQLBadges();
}
return this.badges;
}
public FPlayer getLastMessage() { public FPlayer getLastMessage() {
if(this.lastMsg == null) { if(this.lastMsg == null) {
return null; return null;
@ -741,23 +605,6 @@ public class FPlayer {
return false; return false;
} }
public void queueBadgeAdd(int id) {
if(!Badge.getList().get(id).isEnabled()) {
return;
}
if(!hasBadge( id) && !giveBadgeQueue.contains(id)) {
giveBadgeQueue.add(id);
}
}
public void unqueueBadge(Integer id) {
giveBadgeQueue.remove(id);
}
public ArrayList<Integer> getBadgeQueue(){
return this.giveBadgeQueue;
}
public void cleanup() { public void cleanup() {
String ignorelist = ""; String ignorelist = "";
for(UUID id : ignored) { for(UUID id : ignored) {

View File

@ -5,16 +5,8 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.fpghoti.fpchatx.FPChat;
public class Util { public class Util {
public static void connect(){
if(FPChat.getPlugin().getMySQLConnection().getConnection() == null){
FPChat.getPlugin().getMySQLConnection().reconnect();
}
}
public Player playerGet(String uuid){ public Player playerGet(String uuid){
UUID id = UUID.fromString(uuid); UUID id = UUID.fromString(uuid);
Player p = Bukkit.getPlayer(id); Player p = Bukkit.getPlayer(id);