Dateien nach "src/main/java/craftersland/games/money" hochladen
This commit is contained in:
@@ -0,0 +1,35 @@
|
|||||||
|
package net.craftersland.games.money;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class ConfigurationHandler {
|
||||||
|
|
||||||
|
private Money money;
|
||||||
|
|
||||||
|
public ConfigurationHandler(Money money) {
|
||||||
|
this.money = money;
|
||||||
|
if (!(new File("plugins"+System.getProperty("file.separator")+"Money"+System.getProperty("file.separator")+"config.yml").exists())) {
|
||||||
|
Money.log.info("No config file found! Creating new one...");
|
||||||
|
money.saveDefaultConfig();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
money.getConfig().load(new File("plugins"+System.getProperty("file.separator")+"Money"+System.getProperty("file.separator")+"config.yml"));
|
||||||
|
} catch (Exception e) {
|
||||||
|
Money.log.info("Could not load config file!");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getString(String key) {
|
||||||
|
if (!money.getConfig().contains(key)) {
|
||||||
|
money.getLogger().severe("Could not locate '"+key+"' in the config.yml inside of the Money folder! (Try generating a new one by deleting the current)");
|
||||||
|
return "errorCouldNotLocateInConfigYml:"+key;
|
||||||
|
} else {
|
||||||
|
if (key.toLowerCase().contains("color")) {
|
||||||
|
return "<EFBFBD>"+money.getConfig().getString(key);
|
||||||
|
}
|
||||||
|
return money.getConfig().getString(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
95
src/main/java/craftersland/games/money/Money.java
Normal file
95
src/main/java/craftersland/games/money/Money.java
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package net.craftersland.games.money;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import net.craftersland.games.money.database.AccountDatabaseInterface;
|
||||||
|
import net.craftersland.games.money.database.DatabaseManagerInterface;
|
||||||
|
import net.craftersland.games.money.database.DatabaseManagerMysql;
|
||||||
|
import net.craftersland.games.money.database.MoneyMysqlInterface;
|
||||||
|
import net.milkbowl.vault.economy.Economy;
|
||||||
|
|
||||||
|
import org.bukkit.plugin.PluginManager;
|
||||||
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
public final class Money extends JavaPlugin {
|
||||||
|
|
||||||
|
public static Logger log;
|
||||||
|
public static Economy econ = null;
|
||||||
|
public static ExecutorService execService = null;
|
||||||
|
|
||||||
|
private ConfigurationHandler configurationHandler;
|
||||||
|
private DatabaseManagerInterface databaseManager;
|
||||||
|
private AccountDatabaseInterface<Double> moneyDatabaseInterface;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEnable(){
|
||||||
|
log = getLogger();
|
||||||
|
log.info("Loading Money Games/Lobby "+getDescription().getVersion()+"... ");
|
||||||
|
|
||||||
|
//Create Money folder
|
||||||
|
(new File("plugins"+System.getProperty("file.separator")+"Money")).mkdir();
|
||||||
|
|
||||||
|
|
||||||
|
//Setup Vault for economy and permissions
|
||||||
|
if (!setupEconomy() ) {
|
||||||
|
log.severe(String.format("[%s] - Disabled! Vault installed? If yes Economy system installed?)", getDescription().getName()));
|
||||||
|
getServer().getPluginManager().disablePlugin(this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Load Configuration
|
||||||
|
configurationHandler = new ConfigurationHandler(this);
|
||||||
|
|
||||||
|
//Initiate Threadpool
|
||||||
|
execService = Executors.newFixedThreadPool(Integer.parseInt(configurationHandler.getString("database.maximumThreads")));
|
||||||
|
|
||||||
|
//Setup Database
|
||||||
|
//if (configurationHandler.getString("database.typeOfDatabase").equalsIgnoreCase("mysql")) {
|
||||||
|
log.info("Using MYSQL as Datasource...");
|
||||||
|
databaseManager = new DatabaseManagerMysql(this);
|
||||||
|
moneyDatabaseInterface = new MoneyMysqlInterface(this);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//Register Listeners
|
||||||
|
PluginManager pm = getServer().getPluginManager();
|
||||||
|
pm.registerEvents(new PlayerListener(this), this);
|
||||||
|
|
||||||
|
log.info("Money mysql has been successfully loaded!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisable() {
|
||||||
|
log.info("Money mysql has been disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Methods for setting up Vault
|
||||||
|
private boolean setupEconomy() {
|
||||||
|
if (getServer().getPluginManager().getPlugin("Vault") == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
|
||||||
|
if (rsp == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
econ = rsp.getProvider();
|
||||||
|
return econ != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Getter for Database Interfaces
|
||||||
|
public AccountDatabaseInterface<Double> getMoneyDatabaseInterface() {
|
||||||
|
return moneyDatabaseInterface;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfigurationHandler getConfigurationHandler() {
|
||||||
|
return configurationHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabaseManagerInterface getDatabaseManagerInterface() {
|
||||||
|
return databaseManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
70
src/main/java/craftersland/games/money/PlayerListener.java
Normal file
70
src/main/java/craftersland/games/money/PlayerListener.java
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package net.craftersland.games.money;
|
||||||
|
|
||||||
|
import net.milkbowl.vault.economy.Economy;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
|
|
||||||
|
public class PlayerListener implements Listener{
|
||||||
|
|
||||||
|
private Money money;
|
||||||
|
public static Economy econ = null;
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
private ConfigurationHandler coHa;
|
||||||
|
|
||||||
|
public PlayerListener(Money money) {
|
||||||
|
this.money = money;
|
||||||
|
this.coHa = money.getConfigurationHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
@EventHandler
|
||||||
|
public void onLogin(final PlayerJoinEvent event) {
|
||||||
|
|
||||||
|
//Check if player has mysql an account first
|
||||||
|
if (!money.getMoneyDatabaseInterface().hasAccount(event.getPlayer().getUniqueId()))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//Added a small delay to prevent the onDisconnect handler overlapping onLogin on a BungeeCord configuration when switching servers.
|
||||||
|
Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(money, new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
//Set local balance to 0 before depositing the mysql balance
|
||||||
|
if (Money.econ.getBalance(event.getPlayer()) > 0)
|
||||||
|
{
|
||||||
|
Money.econ.withdrawPlayer(event.getPlayer(), Money.econ.getBalance(event.getPlayer()));
|
||||||
|
}
|
||||||
|
|
||||||
|
//Set mysql balance to local balance
|
||||||
|
Money.econ.depositPlayer(event.getPlayer(), money.getMoneyDatabaseInterface().getBalance(event.getPlayer().getUniqueId()));
|
||||||
|
|
||||||
|
}
|
||||||
|
}, 20L);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onDisconnect(PlayerQuitEvent event) {
|
||||||
|
|
||||||
|
//Check if local balance is 0
|
||||||
|
if (Money.econ.getBalance(event.getPlayer()) == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//Check if player has account and if no create it
|
||||||
|
if (!money.getMoneyDatabaseInterface().hasAccount(event.getPlayer().getUniqueId()))
|
||||||
|
{
|
||||||
|
money.getMoneyDatabaseInterface().createAccount(event.getPlayer().getUniqueId());
|
||||||
|
}
|
||||||
|
//Set local balance on mysql balance
|
||||||
|
money.getMoneyDatabaseInterface().setBalance(event.getPlayer().getUniqueId(), Money.econ.getBalance(event.getPlayer()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user