Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.palmergames.bukkit.towny.db;
- import java.io.File;
- import java.io.IOException;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import org.bukkit.World;
- import com.palmergames.bukkit.towny.*;
- import com.palmergames.bukkit.towny.object.*;
- public class TownyDataSource
- {
- public static int ENEMY = 0;
- public static int ALLY = 1;
- Connection conn = null;
- protected TownyUniverse universe;
- protected Towny plugin;
- protected boolean firstRun = false;
- public void initialize(Towny plugin, TownyUniverse universe)
- {
- this.universe = universe;
- this.plugin = plugin;
- try
- {
- Towny.displayDebugMsg("Connection String: jdbc:mysql://" + TownySettings.getMySQLServer() + "/" + TownySettings.getMySQLDatabase() + "?user=" + TownySettings.getMySQLUsername() + "&password=" + TownySettings.getMySQLPassword());
- conn = DriverManager.getConnection("jdbc:mysql://" + TownySettings.getMySQLServer() + "/" + TownySettings.getMySQLDatabase() + "?user=" + TownySettings.getMySQLUsername() + "&password=" + TownySettings.getMySQLPassword());
- query("SELECT COUNT(*) FROM worlds", true);
- //
- // query("ALTER TABLE `minecraft`.`nations` DROP FOREIGN KEY `nations_capital`");
- //
- // query("ALTER TABLE `minecraft`.`nations` CHANGE COLUMN `capital` `capital` VARCHAR(45) NULL DEFAULT NULL, ADD CONSTRAINT `nations_capital` FOREIGN KEY (`capital` ) REFERENCES `minecraft`.`towns` (`name` ) ON DELETE NO ACTION ON UPDATE NO ACTION");
- // query("DROP TABLE IF EXISTS `plot_management_delete_ids`");
- // query("DROP TABLE IF EXISTS `plot_management_ignore_ids`");
- // query("DROP TABLE IF EXISTS `plot_management_mayor_delete`");
- // query("DROP TABLE IF EXISTS `unclaimed_zone_ignore_ids`");
- // query("UPDATE players SET town_name=null");
- // query("UPDATE towns SET nation_name=null AND home_block_world=null AND home_block_x=null AND home_block_z=null");
- // query("DROP TABLE IF EXISTS `political_relationships`");
- // query("ALTER TABLE `towns` DROP FOREIGN KEY `towns_nation` , DROP FOREIGN KEY `home_block` , DROP FOREIGN KEY `towns_mayor` , DROP FOREIGN KEY `towns_world`");
- // query("DROP TABLE IF EXISTS `friends`");
- // query("ALTER TABLE `blocks` DROP FOREIGN KEY `blocks_world` , DROP FOREIGN KEY `blocks_town` , DROP FOREIGN KEY `blocks_owner`");
- // query("DROP TABLE IF EXISTS `plot_data`");
- // query("DROP TABLE IF EXISTS `blocks`");
- // query("DROP TABLE IF EXISTS `players`");
- // query("DROP TABLE IF EXISTS `nations`");
- // query("DROP TABLE IF EXISTS `towns`");
- // query("DROP TABLE IF EXISTS `worlds`");
- }
- catch (SQLException ex)
- {
- // handle any errors
- Towny.displayError("SQLException: " + ex.getMessage());
- Towny.displayError("SQLState: " + ex.getSQLState());
- Towny.displayError("VendorError: " + ex.getErrorCode());
- }
- }
- private void setup()
- {
- query("CREATE TABLE `worlds` ("
- + "`name` varchar(45) NOT NULL,"
- + "`claimable` tinyint(1) DEFAULT '0',"
- + "`pvp` tinyint(1) DEFAULT '0',"
- + "`force_pvp` tinyint(1) DEFAULT '0',"
- + "`force_town_mobs` tinyint(1) DEFAULT '0',"
- + "`world_mobs` tinyint(1) DEFAULT '0',"
- + "`fire_spread` tinyint(1) DEFAULT '0',"
- + "`force_fire_spread` tinyint(1) DEFAULT '0',"
- + "`explosions` tinyint(1) DEFAULT '0',"
- + "`force_explosions` tinyint(1) DEFAULT '0',"
- + "`enderman_protect` tinyint(1) DEFAULT '0',"
- + "`disable_player_trample` tinyint(1) DEFAULT '0',"
- + "`disable_creature_trample` tinyint(1) DEFAULT '0',"
- + "`unclaimed_zone_build` tinyint(1) DEFAULT '0',"
- + "`unclaimed_zone_destroy` tinyint(1) DEFAULT '0',"
- + "`unclaimed_zone_switch` tinyint(1) DEFAULT '0',"
- + "`unclaimed_zone_item_use` tinyint(1) DEFAULT '0',"
- + "`unclaimed_zone_name` varchar(45) DEFAULT NULL,"
- + "`unclaimed_zone_ignore_ids` tinyint(1) DEFAULT '0',"
- + "`using_plot_management_delete` tinyint(1) DEFAULT '0',"
- + "`plot_management_delete_ids` tinyint(1) DEFAULT '0',"
- + "`using_plot_management_mayor_delete` tinyint(1) DEFAULT '0',"
- + "`using_management_mayor_delete` tinyint(1) DEFAULT '0',"
- + "`using_plot_management_revert` tinyint(1) DEFAULT '0',"
- + "`using_plot_management_revert_speed` tinyint(1) DEFAULT '0',"
- + "`plot_management_ignore_ids` tinyint(1) DEFAULT '0',"
- + "`using_plot_management_wild_regen` tinyint(1) DEFAULT '0',"
- + "`using_plot_management_wild_regen_delay` tinyint(1) DEFAULT '0',"
- + "`global_chat_channel_format` text,"
- + "`town_chat_channel_format` text,"
- + "`nation_chat_channel_format` text,"
- + "`default_chat_channel_format` text,"
- + "`using_towny` tinyint(1) DEFAULT '0',"
- + "PRIMARY KEY (`name`)"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("CREATE TABLE `plot_management_delete_ids` ("
- + "`world_name` varchar(45) NOT NULL,"
- + "`id` int(11) NOT NULL,"
- + "PRIMARY KEY (`world_name`),"
- + "KEY `pmdi_world_name` (`world_name`) USING BTREE,"
- + "CONSTRAINT `pmdi_world_name` FOREIGN KEY (`world_name`) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("CREATE TABLE `plot_management_ignore_ids` ("
- + "`world_name` varchar(45) NOT NULL,"
- + "`id` int(11) NOT NULL,"
- + "PRIMARY KEY (`world_name`),"
- + "KEY `pmii_world_name` (`world_name`) USING BTREE,"
- + "CONSTRAINT `pmii_world_name` FOREIGN KEY (`world_name`) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("CREATE TABLE `plot_management_mayor_delete` ("
- + "`world_name` varchar(45) NOT NULL,"
- + "`material` varchar(45) NOT NULL,"
- + "PRIMARY KEY (`world_name`),"
- + "KEY `pmmd_world_name` (`world_name`) USING BTREE,"
- + "CONSTRAINT `pmmd_world_name` FOREIGN KEY (`world_name`) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("CREATE TABLE `unclaimed_zone_ignore_ids` ("
- + "`world_name` varchar(45) NOT NULL,"
- + "`id` int(11) NOT NULL,"
- + "PRIMARY KEY (`world_name`),"
- + "KEY `uzii_world_name` (`world_name`) USING BTREE,"
- + "CONSTRAINT `uzii_world_name` FOREIGN KEY (`world_name`) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("CREATE TABLE `towns` ("
- + "`name` varchar(45) NOT NULL,"
- + "`mayor_name` varchar(45) DEFAULT NULL,"
- + "`town_board` varchar(45) DEFAULT NULL,"
- + "`tag` varchar(100) DEFAULT NULL,"
- + "`plot_price` double DEFAULT '0',"
- + "`has_upkeep` tinyint(1) DEFAULT '0',"
- + "`tax_percent` tinyint(1) DEFAULT '0',"
- + "`taxes` double DEFAULT '0',"
- + "`plot_tax` double DEFAULT '0',"
- + "`commercial_plot_price` double DEFAULT '0',"
- + "`commercial_plot_tax` double DEFAULT '0',"
- + "`embassy_plot_price` double DEFAULT '0',"
- + "`embassy_plot_tax` double DEFAULT '0',"
- + "`public` tinyint(1) DEFAULT '1',"
- + "`home_block_world` varchar(45) DEFAULT NULL,"
- + "`home_block_x` int(11) DEFAULT NULL,"
- + "`home_block_z` int(11) DEFAULT NULL,"
- + "`spawn_world` varchar(45) DEFAULT NULL,"
- + "`spawn_x` double DEFAULT NULL,"
- + "`spawn_y` double DEFAULT NULL,"
- + "`spawn_z` double DEFAULT NULL,"
- + "`spawn_pitch` float DEFAULT NULL,"
- + "`spawn_yawn` float DEFAULT NULL,"
- + "`nation_name` varchar(45) DEFAULT NULL,"
- + "`world_name` varchar(45) NOT NULL,"
- + "`bonus_blocks` int(11) DEFAULT '0',"
- + "`purchased_blocks` int(11) DEFAULT '0',"
- + "`denyAll` tinyint(1) DEFAULT '0',"
- + "`residentBuild` tinyint(1) DEFAULT '0',"
- + "`residentDestroy` tinyint(1) DEFAULT '0',"
- + "`residentSwitch` tinyint(1) DEFAULT '0',"
- + "`residentItemUse` tinyint(1) DEFAULT '0',"
- + "`outsiderBuild` tinyint(1) DEFAULT '0',"
- + "`outsiderDestroy` tinyint(1) DEFAULT '0',"
- + "`outsiderSwitch` tinyint(1) DEFAULT '0',"
- + "`outsiderItemUse` tinyint(1) DEFAULT '0',"
- + "`allyBuild` tinyint(1) DEFAULT '0',"
- + "`allyDestroy` tinyint(1) DEFAULT '0',"
- + "`allySwitch` tinyint(1) DEFAULT '0',"
- + "`allyItemUse` tinyint(1) DEFAULT '0',"
- + "`pvp` tinyint(1) DEFAULT '0',"
- + "`fire` tinyint(1) DEFAULT '0',"
- + "`explosion` tinyint(1) DEFAULT '0',"
- + "`mobs` tinyint(1) DEFAULT '0',"
- + "PRIMARY KEY (`name`),"
- + "KEY `towns_nation` (`nation_name`) USING HASH,"
- + "KEY `towns_world` (`world_name`) USING HASH,"
- + "KEY `mayors` (`mayor_name`) USING HASH,"
- + "KEY `home_block` (`home_block_world`,`home_block_x`,`home_block_z`) USING HASH"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("CREATE TABLE `nations` ("
- + "`name` varchar(45) NOT NULL,"
- + "`capital` varchar(45) DEFAULT NULL,"
- + "`tag` varchar(45) DEFAULT NULL,"
- + "`taxes` double DEFAULT '0',"
- + "`neutral` tinyint(1) DEFAULT '0',"
- + "PRIMARY KEY (`name`),"
- + "KEY `nations_capital` (`capital`) USING HASH"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("CREATE TABLE `players` ("
- + "`name` varchar(45) NOT NULL,"
- + "`last_online` bigint(20) DEFAULT '0',"
- + "`town_name` varchar(45) DEFAULT NULL,"
- + "`registered` bigint(20) DEFAULT '0',"
- + "`is_npc` tinyint(1) DEFAULT '0',"
- + "`title` varchar(45) DEFAULT NULL,"
- + "`surname` varchar(45) DEFAULT NULL,"
- + "`town_assistant` tinyint(1) DEFAULT '0',"
- + "`nation_assistant` tinyint(1) DEFAULT '0',"
- + "`denyAll` tinyint(1) DEFAULT NULL,"
- + "`residentBuild` tinyint(1) DEFAULT NULL,"
- + "`residentDestroy` tinyint(1) DEFAULT NULL,"
- + "`residentSwitch` tinyint(1) DEFAULT NULL,"
- + "`residentItemUse` tinyint(1) DEFAULT NULL,"
- + "`outsiderBuild` tinyint(1) DEFAULT NULL,"
- + "`outsiderDestroy` tinyint(1) DEFAULT NULL,"
- + "`outsiderSwitch` tinyint(1) DEFAULT NULL,"
- + "`outsiderItemUse` tinyint(1) DEFAULT NULL,"
- + "`allyBuild` tinyint(1) DEFAULT NULL,"
- + "`allyDestroy` tinyint(1) DEFAULT NULL,"
- + "`allySwitch` tinyint(1) DEFAULT NULL,"
- + "`allyItemUse` tinyint(1) DEFAULT NULL,"
- + "`pvp` tinyint(1) DEFAULT NULL,"
- + "`fire` tinyint(1) DEFAULT NULL,"
- + "`explosion` tinyint(1) DEFAULT NULL,"
- + "`mobs` tinyint(1) DEFAULT NULL,"
- + "`global_chat` tinyint(1) DEFAULT '1',"
- + "`town_chat` tinyint(1) DEFAULT '1',"
- + "`nation_chat` tinyint(1) DEFAULT '1',"
- + "PRIMARY KEY (`name`),"
- + "KEY `players_town_name` (`town_name`) USING BTREE,"
- + "CONSTRAINT `players_town_name` FOREIGN KEY (`town_name`) REFERENCES `towns` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("CREATE TABLE `blocks` ("
- + "`world_name` varchar(45) NOT NULL,"
- + "`x` int(11) NOT NULL,"
- + "`z` int(11) NOT NULL,"
- + "`town_name` varchar(45) DEFAULT NULL,"
- + "`type` tinyint(4) DEFAULT NULL,"
- + "`price` double DEFAULT NULL,"
- + "`changed` tinyint(1) DEFAULT '0',"
- + "`regen` tinyint(1) DEFAULT '0',"
- + "`player_owner` varchar(45) DEFAULT NULL,"
- + "`denyAll` varchar(45) DEFAULT NULL,"
- + "`residentBuild` varchar(45) DEFAULT NULL,"
- + "`residentDestroy` varchar(45) DEFAULT NULL,"
- + "`residentSwitch` varchar(45) DEFAULT NULL,"
- + "`residentItemUse` varchar(45) DEFAULT NULL,"
- + "`outsiderBuild` varchar(45) DEFAULT NULL,"
- + "`outsiderDestroy` varchar(45) DEFAULT NULL,"
- + "`outsiderSwitch` varchar(45) DEFAULT NULL,"
- + "`outsiderItemUse` varchar(45) DEFAULT NULL,"
- + "`allyBuild` varchar(45) DEFAULT NULL,"
- + "`allyDestroy` varchar(45) DEFAULT NULL,"
- + "`allySwitch` varchar(45) DEFAULT NULL,"
- + "`allyItemUse` varchar(45) DEFAULT NULL,"
- + "`pvp` varchar(45) DEFAULT NULL,"
- + "`fire` varchar(45) DEFAULT NULL,"
- + "`explosion` varchar(45) DEFAULT NULL,"
- + "`mobs` varchar(45) DEFAULT NULL,"
- + "PRIMARY KEY (`world_name`,`x`,`z`),"
- + "KEY `blocks_world` (`world_name`) USING BTREE,"
- + "KEY `blocks_town` (`town_name`) USING BTREE,"
- + "KEY `blocks_owner` (`player_owner`) USING HASH,"
- + "CONSTRAINT `blocks_owner` FOREIGN KEY (`player_owner`) REFERENCES `players` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION,"
- + "CONSTRAINT `blocks_town` FOREIGN KEY (`town_name`) REFERENCES `towns` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION,"
- + "CONSTRAINT `blocks_world` FOREIGN KEY (`world_name`) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("CREATE TABLE `friends` ("
- + "`player_1` varchar(45) NOT NULL,"
- + "`player_2` varchar(45) NOT NULL,"
- + "PRIMARY KEY (`player_1`,`player_2`),"
- + "KEY `friends_player_2` (`player_2`),"
- + "KEY `friends_player_1` (`player_1`) USING BTREE,"
- + "CONSTRAINT `friends_player_1` FOREIGN KEY (`player_1`) REFERENCES `players` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION,"
- + "CONSTRAINT `friends_player_2` FOREIGN KEY (`player_2`) REFERENCES `players` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("CREATE TABLE `political_relationships` ("
- + "`nation_name_1` varchar(45) NOT NULL,"
- + "`nation_name_2` varchar(45) NOT NULL,"
- + "`relation_type` int(11) NOT NULL,"
- + "PRIMARY KEY (`nation_name_1`,`nation_name_2`),"
- + "KEY `pr_nations_2` (`nation_name_2`),"
- + "KEY `pr_nations_1` (`nation_name_1`) USING BTREE,"
- + "CONSTRAINT `pr_nations_1` FOREIGN KEY (`nation_name_1`) REFERENCES `nations` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION,"
- + "CONSTRAINT `pr_nations_2` FOREIGN KEY (`nation_name_2`) REFERENCES `nations` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("CREATE TABLE `plot_data` ("
- + "`world_name` varchar(45) NOT NULL,"
- + "`x` int(11) NOT NULL,"
- + "`z` int(11) NOT NULL,"
- + "`block_x` int(11) NOT NULL,"
- + "`block_y` int(11) NOT NULL,"
- + "`block_z` int(11) NOT NULL,"
- + "`block_type` int(11) NOT NULL,"
- + "PRIMARY KEY (`world_name`,`x`,`z`,`block_x`,`block_y`,`block_z`),"
- + "KEY `pd_block` (`world_name`,`x`,`z`) USING BTREE,"
- + "CONSTRAINT `pd_block` FOREIGN KEY (`world_name`, `x`, `z`) REFERENCES `blocks` (`world_name`, `x`, `z`) ON DELETE NO ACTION ON UPDATE NO ACTION"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- query("ALTER TABLE `towns` "
- + "ADD CONSTRAINT `towns_world` FOREIGN KEY (`world_name` ) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION, "
- + "ADD CONSTRAINT `towns_mayor` FOREIGN KEY (`mayor_name` ) REFERENCES `players` (`name` ) ON DELETE NO ACTION ON UPDATE NO ACTION, "
- + "ADD CONSTRAINT `home_block` FOREIGN KEY (`home_block_world` , `home_block_x` , `home_block_z` ) REFERENCES `blocks` (`world_name` , `x` , `z` ) ON DELETE NO ACTION ON UPDATE NO ACTION, "
- + "ADD CONSTRAINT `towns_nation` FOREIGN KEY (`nation_name` ) REFERENCES `nations` (`name` ) ON DELETE NO ACTION ON UPDATE NO ACTION");
- }
- public void deleteFile(String fileName)
- {
- File file = new File(fileName);
- if (file.exists())
- {
- file.delete();
- }
- }
- public ResultSet query(String queryStr)
- {
- return query(queryStr, false, false);
- }
- public ResultSet query(String queryStr, boolean first)
- {
- return query(queryStr, false, false);
- }
- public ResultSet query(String queryStr, boolean first, boolean suppressErrorMsg)
- {
- Statement stmt = null;
- ResultSet rs = null;
- Towny.displayDebugMsg("Query: " + queryStr);
- try
- {
- stmt = conn.createStatement();
- if (queryStr.startsWith("SELECT"))
- {
- stmt.executeQuery(queryStr);
- rs = stmt.getResultSet();
- }
- else
- {
- rs = null;
- stmt.execute(queryStr);
- }
- }
- catch (SQLException ex)
- {
- if (first)
- {
- setup();
- }
- else
- {
- if (!suppressErrorMsg)
- {
- Towny.displayError("Query: " + queryStr);
- Towny.displayError("SQLException: " + ex.getMessage());
- Towny.displayError("SQLState: " + ex.getSQLState());
- Towny.displayError("VendorError: " + ex.getErrorCode());
- ex.printStackTrace();
- }
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return rs;
- }
- public void backup() throws IOException
- {
- }
- public void cleanupBackups()
- {
- }
- public boolean confirmContinuation(String msg)
- {
- Boolean choice = null;
- String input = null;
- while (choice == null)
- {
- System.out.println(msg);
- System.out.print(" Continue (y/n): ");
- Scanner in = new Scanner(System.in);
- input = in.next();
- input = input.toLowerCase();
- if (input.equals("y") || input.equals("yes"))
- {
- in.close();
- return true;
- }
- else if (input.equals("n") || input.equals("no"))
- {
- in.close();
- return false;
- }
- }
- System.out.println("[Towny] Error recieving input, exiting.");
- return false;
- }
- public void sendDebugMsg(String msg)
- {
- if (plugin != null)
- {
- TownyMessaging.sendDebugMsg(msg);
- }
- else
- {
- System.out.println("[Towny] Debug: " + msg);
- }
- }
- public boolean loadAll()
- {
- TownyUniverse.locked = true;
- boolean result = loadWorldList() && loadResidentList() && loadNationList() && loadTownList() && loadWorlds() && loadResidents() && loadNations() && loadRegenList();
- TownyUniverse.locked = false;
- return result;
- }
- public boolean saveAll()
- {
- return loadRegenList();
- }
- public List<TownyWorld> getWorlds()
- {
- return universe.getWorlds();
- }
- public Resident getResident(String name)
- {
- try
- {
- return universe.getResident(name);
- }
- catch (NotOnlineException e)
- {
- return null;
- }
- catch (NotRegisteredException e)
- {
- return null;
- }
- }
- public Town getTown(String name)
- {
- try
- {
- return universe.getTown(name);
- }
- catch (NotRegisteredException e)
- {
- return null;
- }
- }
- public Nation getNation(String name)
- {
- try
- {
- return universe.getNation(name);
- }
- catch (NotRegisteredException e)
- {
- return null;
- }
- }
- public boolean loadResidents()
- {
- sendDebugMsg("Loading Residents");
- for (Resident resident : universe.getResidents())
- {
- if (!resident.load())
- {
- System.out.println("[Towny] Loading Error: Could not read resident data '" + resident.getName() + "'.");
- return false;
- }
- else
- {
- // Push all Towns belonging to this world
- }
- }
- return true;
- }
- public boolean loadNations()
- {
- sendDebugMsg("Loading Nations");
- for (Nation nation : universe.getNations())
- {
- if (!nation.load())
- {
- System.out.println("[Towny] Loading Error: Could not read resident data '" + nation.getName() + "'.");
- return false;
- }
- else
- {
- // Push all Towns belonging to this world
- }
- }
- return true;
- }
- public boolean loadWorlds()
- {
- sendDebugMsg("Loading Worlds");
- for (TownyWorld world : universe.getWorlds())
- {
- if (!world.load())
- {
- System.out.println("[Towny] Loading Error: Could not read world data '" + world.getName() + "'.");
- return false;
- }
- else
- {
- // Push all Towns belonging to this world
- }
- }
- return true;
- }
- public boolean loadResidentList()
- {
- Towny.displayDebugMsg("Loading Resident List");
- ResultSet rs = query("SELECT name FROM players");
- try
- {
- while (rs != null && rs.next())
- {
- universe.newResident(rs.getString("name"));
- }
- }
- catch (AlreadyRegisteredException e)
- {
- e.printStackTrace();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public boolean loadTownList()
- {
- Towny.displayDebugMsg("Loading Town List");
- ResultSet rs = query("SELECT name, world_name FROM towns");
- try
- {
- while (rs != null && rs.next())
- {
- // Towny.displayMsg("rs.getString(\"world_name\"): " + rs.getString("world_name"));
- // Towny.displayMsg("TownyUniverse.getTownWorld(" + rs.getString("world_name") + "): " + TownyUniverse.getWorld(rs.getString("world_name")));
- universe.newTown(rs.getString("name"), TownyUniverse.getWorld(rs.getString("world_name")));
- }
- }
- catch (AlreadyRegisteredException e)
- {
- e.printStackTrace();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public boolean loadNationList()
- {
- Towny.displayDebugMsg("Loading Nation List");
- ResultSet rs = query("SELECT name, capital FROM nations");
- try
- {
- while (rs != null && rs.next())
- {
- universe.newNation(rs.getString("name"));
- }
- }
- catch (AlreadyRegisteredException e)
- {
- e.printStackTrace();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public boolean loadWorldList()
- {
- Towny.displayDebugMsg("Loading World List");
- if (plugin != null)
- {
- for (World world : plugin.getServer().getWorlds())
- {
- try
- {
- universe.newWorld(world.getName());
- }
- catch (AlreadyRegisteredException e)
- {
- e.printStackTrace();
- }
- catch (NotRegisteredException e)
- {
- e.printStackTrace();
- }
- }
- }
- else
- {
- Towny.displayDebugMsg("Loading World List Failure ... plugin == null?");
- return false;
- }
- Towny.displayDebugMsg("Loading World List Sucessful");
- return true;
- }
- public boolean loadRegenList()
- {
- Towny.displayDebugMsg("Loading Regen List");
- ResultSet rs = query("SELECT world_name, x, z FROM blocks WHERE regen='1'");
- PlotBlockData plotData;
- try
- {
- while (rs != null && rs.next())
- {
- plotData = loadPlotData(rs.getString("world_name"), rs.getInt("x"), rs.getInt("z"));
- if (plotData != null)
- {
- TownyRegenAPI.addPlotChunk(plotData, false);
- }
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- // TODO Hook up to add friend cmd
- public boolean saveFriend(Resident resident, Resident friend)
- {
- try
- {
- ResultSet rs = query("SELECT player_2 FROM friends WHERE player_1='" + resident.getName() + "' AND player_2='" + friend.getName() + "'");
- if (rs == null || !rs.next())
- {
- query("INSERT INTO friends VALUES ('" + resident.getName() + "', '" + friend.getName() + "')");
- }
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- return false;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public boolean savePlotData(PlotBlockData plotChunk)
- {
- return true;
- }
- public PlotBlockData loadPlotData(String worldName, int x, int z)
- {
- return null;
- }
- public PlotBlockData loadPlotData(TownBlock townBlock)
- {
- return null;
- }
- public void deletePlotData(PlotBlockData plotChunk)
- {
- }
- public void deleteResident(Resident resident)
- {
- query("DELETE FROM players WHERE name='" + resident.getName() + "'");
- }
- public void deleteTownHomeBlock(Town town)
- {
- query("UPDATE towns SET home_block_world=null, home_block_x=null, home_block_z=null WHERE name='" + town.getName() + "'");
- }
- public void deleteTown(Town town)
- {
- deleteTownHomeBlock(town);
- query("DELETE FROM blocks WHERE town_name='" + town.getName() + "'");
- query("DELETE FROM towns WHERE name='" + town.getName() + "'");
- }
- public void deleteNation(Nation nation)
- {
- query("UPDATE towns SET nation_name=null WHERE nation_name='" + nation.getName() + "'");
- query("DELETE FROM political_relationships WHERE nation_name_1='" + nation.getName() + "' OR nation_name_2='" + nation.getName() + "'");
- query("DELETE FROM nations WHERE name='" + nation.getName() + "'");
- }
- public void deleteWorld(TownyWorld world)
- {
- query("DELETE FROM worlds WHERE name='" + world.getName() + "'");
- }
- public void deleteTownBlock(TownBlock townBlock)
- {
- query("DELETE FROM blocks WHERE world_name='" + townBlock.getWorld().getName() + "' AND x='" + townBlock.getX() + "' AND z='" + townBlock.getZ() + "'");
- }
- public void setResidentTown(Town town, String player)
- {
- query("UPDATE blocks SET player_owner=null WHERE player_owner='" + player + "'");
- if (town == null)
- {
- query("UPDATE players SET town_name=null WHERE name='" + player + "'");
- }
- else
- {
- query("UPDATE players SET town_name='" + town.getName() + "' WHERE name='" + player + "'");
- }
- }
- public void removeTownFromResidents(Town town)
- {
- query("UPDATE players SET town_name=null WHERE town_name='" + town.getName() + "'");
- }
- public void isResidentInATown(String playerName) throws AlreadyRegisteredException
- {
- ResultSet rs = query("SELECT town_name FROM players WHERE name='" + playerName + "'");
- try
- {
- if (rs != null && rs.next())
- {
- if (rs.getString("town_name") != null)
- {
- throw new AlreadyRegisteredException();
- }
- }
- }
- catch (SQLException e)
- {
- e.printStackTrace(Towny.pw);
- e.printStackTrace();
- }
- }
- public void setBlockPlayerOwner(TownBlock townBlock, String name)
- {
- query("UPDATE blocks SET player_owner='" + name + "' WHERE world_name='" + townBlock.getWorld().getName() + "' AND x='" + townBlock.getX() + "' AND z='" + townBlock.getZ() + "'");
- }
- public void deleteBlockPlayerOwner(TownBlock townBlock)
- {
- query("UPDATE blocks SET player_owner=null WHERE world_name='" + townBlock.getWorld().getName() + "' AND x='" + townBlock.getX() + "' AND z='" + townBlock.getZ() + "'");
- }
- public boolean hasFriend(String player1, String player2)
- {
- ResultSet rs = query("SELECT player_2 FROM friends WHERE player_1='" + player1 + "' AND player_2='" + player2 + "'");
- try
- {
- if (rs != null && rs.next())
- {
- if (rs.getString("player_2") != null)
- {
- return true;
- }
- }
- }
- catch (SQLException e)
- {
- e.printStackTrace(Towny.pw);
- e.printStackTrace();
- }
- return false;
- }
- public Town getResidentTown(String name)
- {
- ResultSet rs = query("SELECT town_name FROM players WHERE name='" + name + "'");
- try
- {
- if (rs != null && rs.next())
- {
- String townName = rs.getString("town_name");
- if (townName != null)
- {
- return getTown(townName);
- }
- }
- }
- catch (SQLException e)
- {
- e.printStackTrace(Towny.pw);
- e.printStackTrace();
- }
- return null;
- }
- public void updateTownName(Town town, String oldName)
- {
- query("CREATE TEMPORARY TABLE towns2 ENGINE=MEMORY SELECT * FROM towns WHERE name='" + oldName + "'");
- query("UPDATE towns2 SET name='" + town.getName() + "' WHERE name='" + oldName + "'");
- query("INSERT INTO towns SELECT * FROM towns2 WHERE name='" + town.getName() + "'");
- query("DROP TABLE towns2");
- query("UPDATE blocks SET town_name='" + town.getName() + "' WHERE town_name='" + oldName + "'");
- query("UPDATE players SET town_name='" + town.getName() + "' WHERE town_name='" + oldName + "'");
- query("UPDATE nations SET capital='" + town.getName() + "' WHERE capital='" + oldName + "'");
- query("DELETE FROM towns WHERE name='" + oldName + "'");
- }
- public void updateNationName(Nation nation, String oldName)
- {
- query("CREATE TEMPORARY TABLE nations2 ENGINE=MEMORY SELECT * FROM nations WHERE name='" + oldName + "'");
- query("UPDATE nations2 SET name='" + nation.getName() + "' WHERE name='" + oldName + "'");
- query("INSERT INTO nations SELECT * FROM nations2 WHERE name='" + nation.getName() + "'");
- query("DROP TABLE nations2");
- query("UPDATE towns SET nation_name='" + nation.getName() + "' WHERE nation_name='" + oldName + "'");
- query("UPDATE players SET nation_name_1='" + nation.getName() + "' WHERE nation_name_1='" + oldName + "'");
- query("UPDATE players SET nation_name_2='" + nation.getName() + "' WHERE nation_name_2='" + oldName + "'");
- query("DELETE FROM towns WHERE name='" + oldName + "'");
- }
- public List<String> getNPCs()
- {
- List<String> names = new ArrayList<String>();
- ResultSet rs = query("SELECT name FROM players WHERE is_npc='1'");
- try
- {
- if (rs != null)
- {
- while(rs.next())
- {
- String name = rs.getString("name");
- if (name != null)
- {
- names.add(name);
- }
- }
- }
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return names;
- }
- }
Add Comment
Please, Sign In to add comment