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"?>
<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>
<groupId>FPChat</groupId>
<groupId>com.fpghoti</groupId>
<artifactId>FPChat</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
@ -35,12 +35,6 @@
</executions>
<configuration>
<source>1.7</source>
<artifactSet>
<includes>
<include>org.apache.commons*:*</include>
</includes>
</artifactSet>
<relocations />
</configuration>
</plugin>
</plugins>
@ -63,8 +57,30 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.2-R0.1-SNAPSHOT</version>
<scope>compile</scope>
<version>1.16.3-R0.1-SNAPSHOT</version>
<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>
<groupId>com.github.MilkBowl</groupId>

21
pom.xml
View File

@ -2,9 +2,9 @@
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">
<modelVersion>4.0.0</modelVersion>
<groupId>FPChat</groupId>
<groupId>com.fpghoti</groupId>
<artifactId>FPChat</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
<repositories>
<repository>
<id>spigot-repo</id>
@ -43,13 +43,6 @@
<version>3.0.0</version>
<configuration>
<source>1.7</source>
<artifactSet>
<includes>
<include>org.apache.commons*:*</include>
</includes>
</artifactSet>
<relocations>
</relocations>
</configuration>
<executions>
<execution>
@ -67,11 +60,13 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<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>
<groupId>com.github.MilkBowl</groupId>
@ -79,5 +74,11 @@
<version>1.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -59,7 +59,6 @@ public class FPChat extends JavaPlugin {
private MySQLConnection sql;
private static FPChat plugin;
private String pluginTag;
private int mysqlTimer = 1140;
private BukkitTask refresh = null;
private PlayerListener listener = null;
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);
}

View File

@ -2,12 +2,12 @@ package com.fpghoti.fpchatx.badge;
import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.config.BadgeConfig;
import com.fpghoti.fpchatx.player.FPlayer;
public class Badge {
private static BadgeList badges;
private static BadgeConfig bconfig = null;
private static Badge zero = new Badge(0, "Empty", "", "", false);
public static void loadBadges() {
badges = new BadgeList();
@ -22,6 +22,10 @@ public class Badge {
return bconfig;
}
public static Badge getZero() {
return zero;
}
public static BadgeList getList() {
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.Iterator;
import com.fpghoti.fpchatx.util.Util;
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 Badge empty = new Badge(0, "Empty", "", "", false);
@Override
public Iterator<Badge> iterator() {
@ -24,6 +40,13 @@ public class BadgeList implements Iterable<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) {
ArrayList<Badge> rl = new ArrayList<Badge>(list);
for(Badge b : rl) {
@ -46,20 +69,46 @@ public class BadgeList implements Iterable<Badge>{
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) {
return containsId(badge.getId());
}
public Badge get(int id) {
if(id <= 0 || !containsId(id)) {
return empty;
return Badge.getZero();
}
for(Badge b : list) {
if(b.getId() == id) {
return b;
}
}
return empty;
return Badge.getZero();
}
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 com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.badge.Badge;
import com.fpghoti.fpchatx.config.ChannelFile;
import com.fpghoti.fpchatx.customcodes.BubbleCode;
import com.fpghoti.fpchatx.customcodes.Codify;
@ -297,35 +296,24 @@ public abstract class ChatChannel {
}
public String format(FPlayer p, String msg) {
String finalMessage = "";
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 finalMessage = "", badges = "", stf = "", filler = "";
String header = PrepareChat.swapPlaceholders(p, this, msg);
if(Permission.isStaff(p)){
stf = FPChat.getPlugin().getMainConfig().getStaffBadge();
}
if(plugin.getMainConfig().mySQLEnabled()){
Integer[] badges = p.getBadges();
slot1 = Badge.getList().get(badges[0]).getContents();
slot2 = Badge.getList().get(badges[1]).getContents();
slot3 = Badge.getList().get(badges[2]).getContents();
badges = p.getBadgeData().getAppearanceString();
}
String filler = "";
if(plugin.getMainConfig().chatFilterEnabled()){
filler = "word ";
}
if(Permission.canUseColor(p)){
String last = ChatFilter.filter(filler + msg);
last = BubbleCode.bubblecode(Permission.canBubbleCode(p), Codify.changeFormatSign(last));
finalMessage = stf + slot3 + slot2 + slot1 + header + last;
finalMessage = ChatColor.translateAlternateColorCodes('&', stf + badges + header + last);
}else{
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;
}

View File

@ -38,12 +38,7 @@ public class BadgeClearCommand extends Commands {
}
FPlayer p = FPlayer.getPlayer((Player)sender);
p.setBadge(1, 0);
p.updateBadges(1, 0);
p.setBadge(2, 0);
p.updateBadges(2, 0);
p.setBadge(3, 0);
p.updateBadges(3, 0);
p.getBadgeData().clearLoadout();
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.badge.Badge;
import com.fpghoti.fpchatx.badge.BadgeEquipResult;
import com.fpghoti.fpchatx.command.Commands;
import com.fpghoti.fpchatx.player.FPlayer;
import com.fpghoti.fpchatx.util.Util;
@ -42,16 +43,24 @@ public class BadgeEquipCommand extends Commands {
if(Util.isDigit(args[0]) && Util.isDigit(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))){
p.setBadge(slot, badgeId);
p.updateBadges(slot, badgeId);
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!");
if(!Badge.getList().containsId(badgeId)) {
p.sendMessage(FPChat.logo() + ChatColor.RED + " Invalid badge!");
return;
}
}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);
if (pg > pageCount) {

View File

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

View File

@ -9,6 +9,7 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.util.Util;
public class MainConfig {
@ -121,6 +122,10 @@ public class MainConfig {
config.createSection("PluginTag");
config.set("PluginTag", "&a&lFPChat&r");
}
if (config.get("MaxBadgeSlots")==null){
config.createSection("MaxBadgeSlots");
config.set("MaxBadgeSlots", "3");
}
try {
config.save(configFile);
} catch (IOException e) {
@ -263,4 +268,12 @@ public class MainConfig {
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;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import org.bukkit.scheduler.BukkitRunnable;
import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.config.MainConfig;
import com.zaxxer.hikari.HikariDataSource;
public class MySQLConnection{
@ -18,6 +17,7 @@ public class MySQLConnection{
private FPChat plugin;
private String host, user, password, database, port;
private MainConfig config;
private HikariDataSource hikari;
public MySQLConnection(FPChat plugin) {
this.plugin = plugin;
@ -27,28 +27,39 @@ public class MySQLConnection{
password = config.getPassword();
database = config.getDatabase();
port = config.getPort();
}
private Connection connection;
public Connection getConnection(){
return connection;
hikari = new HikariDataSource();
hikari.setMaximumPoolSize(10);
hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
hikari.addDataSourceProperty("serverName", host);
hikari.addDataSourceProperty("user", user);
hikari.addDataSourceProperty("password", password);
hikari.addDataSourceProperty("databaseName", database);
hikari.addDataSourceProperty("port", port);
}
public void generate() {
if(config.mySQLEnabled()){
plugin.log(Level.INFO, "Connecting to MySQL...");
connect();
if(!tableExists(config.getChatFeatureTable())){
Connection connection = null;
try {
connection = hikari.getConnection();
if(!tableExists(config.getChatFeatureTable(), connection)){
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_slot1 INT (11), badge_slot2 INT (11), badge_slot3 INT (11), PRIMARY KEY(player_uuid))");
update("CREATE TABLE " + config.getChatFeatureTable() + " (player_uuid VARCHAR (36), badge_loadout TEXT, PRIMARY KEY(player_uuid))");
plugin.log(Level.INFO, "FPChat table created!");
}
if(!tableExists(config.getPermSyncTable())){
//createTable(config.getPermSyncTable(), "player_uuid VARCHAR (36), badges TEXT");
if(!tableExists(config.getPermSyncTable(), connection)){
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, "Badges have been enabled!");
}else{
@ -56,66 +67,15 @@ public class MySQLConnection{
}
}
public void connect(){
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){
public ResultSet query(String query, Connection connection){
if (query == null) {
return null;
}
connect();
ResultSet results = null;
try{
Statement statement = getConnection().createStatement();
Statement statement = connection.createStatement();
results = statement.executeQuery(query);
}catch(SQLException e){
plugin.log(Level.SEVERE, "There has been an error:" + e.getMessage());
plugin.log(Level.SEVERE,"Failed Query in MySQL using the following query input:");
@ -129,34 +89,46 @@ public class MySQLConnection{
if (input == null){
return;
}
connect();
Statement statement;
Connection connection = null;
try {
Statement statement = getConnection().createStatement();
connection = hikari.getConnection();
statement = connection.createStatement();
statement.executeUpdate(input);
statement.close();
} catch (SQLException e) {
plugin.log(Level.SEVERE, "There has been an error:" + e.getMessage());
plugin.log(Level.SEVERE,"Failed to update MySQL using the following update input:");
plugin.log(Level.SEVERE, input);
e.printStackTrace();
}finally {
try {
connection.close();
} catch (SQLException e) {
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) {
return false;
}
try{
if (getConnection() == null) {
if (connection == null) {
return false;
}
if (getConnection().getMetaData() == null) {
if (connection.getMetaData() == null) {
return false;
}
ResultSet results = getConnection().getMetaData().getTables(null, null, tablename, null);
ResultSet results = connection.getMetaData().getTables(null, null, tablename, null);
if (results.next()) {
return true;
}
}catch(SQLException e) {
e.printStackTrace();
}
@ -167,67 +139,53 @@ public class MySQLConnection{
if (data != null) {
data = "'" + data + "'";
}
Connection connection = null;
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()) {
if (results.getString(column) != null) {
return true;
}
}
}catch(SQLException e) {
plugin.log(Level.SEVERE, "MYSQL itemExists error: " + e.getMessage());
e.printStackTrace();
}finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
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 + ")");
}
}
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) {
plugin.log(Level.SEVERE, "MySQL get error: " + e.getMessage());
e.printStackTrace();
}finally {
try {
connection.close();
} catch (SQLException e) {
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.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;
@ -16,9 +15,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import com.fpghoti.fpchatx.FPChat;
import com.fpghoti.fpchatx.badge.Badge;
import com.fpghoti.fpchatx.badge.BadgeList;
import com.fpghoti.fpchatx.badge.Sync;
import com.fpghoti.fpchatx.badge.BadgeData;
import com.fpghoti.fpchatx.chat.ChatChannel;
import com.fpghoti.fpchatx.chat.ChatFilter;
import com.fpghoti.fpchatx.chat.PrepareChat;
@ -118,22 +115,19 @@ public class FPlayer {
private String primaryChannel;
private String primaryTempChannel;
private int shoutCooldown;
private Integer[] badges;
private UUID lastMsg;
private boolean spy;
private boolean shoutVisible;
private boolean hushed;
private ArrayList<Integer> synced;
private boolean toShout;
private boolean toTalk;
private ChatChannel talkChannel;
private ArrayList<Integer> giveBadgeQueue;
private BadgeData badgeData;
private FPlayer(OfflinePlayer p) {
this.toShout = false;
this.toTalk = false;
this.talkChannel = null;
this.synced = new ArrayList<Integer>();
this.offlinePlayer = p;
this.name = p.getName();
this.uuid = p.getUniqueId();
@ -144,7 +138,6 @@ public class FPlayer {
this.spy = pfile.isSpy();
this.shoutVisible = pfile.shoutVisible();
this.hushed = pfile.isHushed();
this.giveBadgeQueue = new ArrayList<Integer>();
String rawignore = pfile.getIgnore();
if(!rawignore.equals("")) {
for(String s : rawignore.split(",")) {
@ -173,12 +166,7 @@ public class FPlayer {
}
}
if(FPChat.getPlugin().getMainConfig().mySQLEnabled()) {
Util.connect();
Sync.update(this);
this.badges = getSQLBadges();
}else {
Integer[] empt = {0,0,0};
this.badges = empt;
this.badgeData = new BadgeData(this);
}
}
@ -190,127 +178,6 @@ public class FPlayer {
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() {
return this.shoutCooldown;
}
@ -359,6 +226,10 @@ public class FPlayer {
this.shoutVisible = false;
}
public BadgeData getBadgeData() {
return this.badgeData;
}
public boolean setPrefix(String prefix) {
if(isOnline() && getPlayer() != null) {
World world = null;
@ -388,13 +259,6 @@ public class FPlayer {
this.hushed = false;
}
public Integer[] getBadges() {
if(badges[0] == null || badges[1] == null || badges[2] == null) {
badges = getSQLBadges();
}
return this.badges;
}
public FPlayer getLastMessage() {
if(this.lastMsg == null) {
return null;
@ -741,23 +605,6 @@ public class FPlayer {
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() {
String ignorelist = "";
for(UUID id : ignored) {

View File

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