Money API
This commit is contained in:
@@ -272,6 +272,9 @@ public class AnuraCore extends JavaPlugin implements PluginMessageListener {
|
||||
{
|
||||
return;
|
||||
}
|
||||
for (Player P : getServer().getOnlinePlayers()) {
|
||||
Money.saveMoney(P);
|
||||
}
|
||||
File configFile = new File("plugins/Core/","config.yml");
|
||||
File dbFile = new File("plugins/Core","database.yml");
|
||||
try
|
||||
@@ -981,7 +984,6 @@ public class AnuraCore extends JavaPlugin implements PluginMessageListener {
|
||||
}
|
||||
else if(cmd.getName().equalsIgnoreCase("money"))
|
||||
{
|
||||
try {
|
||||
if(args.length == 0)
|
||||
{
|
||||
if(P == null)
|
||||
@@ -989,17 +991,8 @@ public class AnuraCore extends JavaPlugin implements PluginMessageListener {
|
||||
tools.sendStatusMsg(sender,"only_player_cmd",false);
|
||||
return true;
|
||||
}
|
||||
ResultSet rs = sql.querySelect("SELECT * FROM coreStats WHERE player = (SELECT id FROM players WHERE uuid = '"+P.getUniqueId().toString()+"')");
|
||||
rs.last();
|
||||
if(rs.getRow() == 1)
|
||||
{
|
||||
Integer money = rs.getInt("money");
|
||||
tools.sendStatusMsg(P,API.getl("you_have", P)+" "+money+" "+ API.getl("coins", P),true);
|
||||
}
|
||||
else
|
||||
{
|
||||
API.statusMsg(P,"unknown_error",true);
|
||||
}
|
||||
Integer money = Money.getMoney(P);
|
||||
tools.sendStatusMsg(P,API.getl("you_have", P)+" "+money+" "+ API.getl("coins", P),true);
|
||||
return true;
|
||||
}
|
||||
else if((args[0].equalsIgnoreCase("pay")) && (args.length == 3))
|
||||
@@ -1035,25 +1028,16 @@ public class AnuraCore extends JavaPlugin implements PluginMessageListener {
|
||||
API.statusMsg(P,"number_must_positive",false);
|
||||
return true;
|
||||
}
|
||||
ResultSet rs = sql.querySelect("SELECT * FROM coreStats WHERE player = (SELECT id FROM players WHERE uuid LIKE '"+P.getUniqueId().toString()+"')");
|
||||
rs.last();
|
||||
if(rs.getRow() == 1)
|
||||
int currentMoney = Money.getMoney(P);
|
||||
if(currentMoney < money)
|
||||
{
|
||||
if(rs.getInt("money") < money)
|
||||
{
|
||||
API.statusMsg(P,"not_enough_money",false);
|
||||
canPay = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
geld = rs.getInt("money");
|
||||
canPay = true;
|
||||
}
|
||||
API.statusMsg(P,"not_enough_money",false);
|
||||
canPay = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
API.statusMsg(P,"unknown_error",false);
|
||||
return true;
|
||||
geld = currentMoney;
|
||||
canPay = true;
|
||||
}
|
||||
}
|
||||
if(canPay)
|
||||
@@ -1064,15 +1048,14 @@ public class AnuraCore extends JavaPlugin implements PluginMessageListener {
|
||||
API.statusMsg(P, "never_seen_player", false);
|
||||
return true;
|
||||
}
|
||||
ResultSet rs = sql.querySelect("SELECT * FROM coreStats WHERE player = (SELECT id FROM players WHERE uuid LIKE '"+oP.getUniqueId().toString()+"')");
|
||||
rs.last();
|
||||
if(rs.getRow() == 1)
|
||||
int currentMoney2 = Money.getMoney(oP);
|
||||
if(currentMoney2 != -1)
|
||||
{
|
||||
sql.queryUpdate("UPDATE coreStats SET money = '"+(rs.getInt("money")+money)+"' WHERE player = (SELECT id FROM players WHERE uuid = '"+oP.getUniqueId().toString()+"')");
|
||||
Money.payMoney(oP, money);
|
||||
if(P == null){}
|
||||
else if(!P.hasPermission("core.money.endless"))
|
||||
{
|
||||
sql.queryUpdate("UPDATE coreStats SET money = '"+(geld-rs.getInt("money"))+"' WHERE player = (SELECT id FROM players WHERE uuid = '"+P.getUniqueId().toString()+"')");
|
||||
Money.payMoney(P, -money);
|
||||
}
|
||||
tools.sendStatusMsg(sender,API.getl("money_payed_1", sender)+" "+args[1]+" "+API.getl("money_payed_2", sender)+" "+args[2]+" "+API.getl("money_payed_3", sender),true);
|
||||
if(oP.isOnline())
|
||||
@@ -1093,11 +1076,6 @@ public class AnuraCore extends JavaPlugin implements PluginMessageListener {
|
||||
}
|
||||
|
||||
}
|
||||
} catch(SQLException e)
|
||||
{
|
||||
API.statusMsg(sender,"unknown_error",true);
|
||||
System.out.println("SQL-Exception!");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ public class FlowerPots {
|
||||
ResultSet rs = AnuraCore.getSql().querySelect("SELECT url, name, money FROM corePots WHERE id = '"+pot+"'");
|
||||
try {
|
||||
rs.first();
|
||||
AnuraCore.getSql().queryUpdate("UPDATE players SET money = money + '"+rs.getInt("money")+"' WHERE uuid = '"+P.getUniqueId().toString()+"'");
|
||||
Money.payMoney(P, rs.getInt("money"));
|
||||
P.sendMessage(ChatColor.RED + "---------- "+ChatColor.YELLOW + "Achievement"+ChatColor.RED+ " ----------");
|
||||
P.sendMessage(ChatColor.BLUE + rs.getString("name"));
|
||||
P.sendMessage("" + ChatColor.GOLD + ChatColor.UNDERLINE + rs.getString("url"));
|
||||
|
||||
46
src/de/anura/core/Money.java
Normal file
46
src/de/anura/core/Money.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package de.anura.core;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
public class Money {
|
||||
private static final HashMap<OfflinePlayer, Integer> playerMoney = new HashMap<>();
|
||||
|
||||
public static void payMoney(OfflinePlayer P, int count) {
|
||||
if (!playerMoney.containsKey(P)) {
|
||||
if (!loadMoney(P)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
playerMoney.put(P, playerMoney.get(P) + count);
|
||||
}
|
||||
|
||||
public static void saveMoney(OfflinePlayer P) {
|
||||
if (!playerMoney.containsKey(P)) return;
|
||||
API.getMySql().queryUpdate("UPDATE coreStats SET money = '" + playerMoney.get(P) + "' WHERE player = (SELECT id FROM players WHERE uuid = '"+P.getUniqueId().toString()+"')");
|
||||
playerMoney.remove(P);
|
||||
}
|
||||
|
||||
public static boolean loadMoney(OfflinePlayer P) {
|
||||
ResultSet rs = API.getMySql().querySelect("SELECT money FROM coreStats WHERE player = (SELECT id FROM players WHERE uuid = '"+P.getUniqueId().toString()+"')");
|
||||
try {
|
||||
rs.first();
|
||||
playerMoney.put(P, rs.getInt("money"));
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Could not load money for player "+P.getName());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static int getMoney(OfflinePlayer P) {
|
||||
if (!playerMoney.containsKey(P)) {
|
||||
if (!loadMoney(P)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return playerMoney.get(P);
|
||||
}
|
||||
}
|
||||
@@ -6,16 +6,17 @@ import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
public class PlayerQuit implements Listener
|
||||
{
|
||||
public PlayerQuit(AnuraCore plugin)
|
||||
{
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event)
|
||||
{
|
||||
event.setQuitMessage(null);
|
||||
AnuraCore.getInstance().endSitting(event.getPlayer());
|
||||
AnuraCore.getInstance().disableCommandsAdventure.remove(event.getPlayer());
|
||||
}
|
||||
public PlayerQuit(AnuraCore plugin)
|
||||
{
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event)
|
||||
{
|
||||
event.setQuitMessage(null);
|
||||
AnuraCore.getInstance().endSitting(event.getPlayer());
|
||||
AnuraCore.getInstance().disableCommandsAdventure.remove(event.getPlayer());
|
||||
Money.saveMoney(event.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user