Got Players database working.

Note you need to be Op to create islands right now.
This commit is contained in:
tastybento 2017-06-12 08:04:26 -07:00
parent 12792f388e
commit 4609d86ff0
8 changed files with 404 additions and 36 deletions

View File

@ -274,6 +274,10 @@ public class BSkyBlock extends JavaPlugin{
* @return the locale for this player
*/
public BSBLocale getLocale(UUID player){
getLogger().info("DEBUG: " + player);
getLogger().info("DEBUG: " + getPlayers() == null ? "Players is null":"Players in not null");
getLogger().info("DEBUG: " + getPlayers().getPlayer(player));
getLogger().info("DEBUG: " + getPlayers().getPlayer(player).getLocale());
String locale = getPlayers().getPlayer(player).getLocale();
if(locale.isEmpty() || !locales.containsKey(locale)) return locales.get(Settings.defaultLanguage);

View File

@ -10,6 +10,7 @@ import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.schematics.Schematic;
import us.tastybento.bskyblock.util.DeleteIslandBlocks;
import us.tastybento.bskyblock.util.Util;
import us.tastybento.bskyblock.util.VaultHelper;
@ -243,13 +244,25 @@ public class IslandCommand extends BSBCommand{
@Override
public boolean canExecute(CommandSender sender, String label, String[] args) {
// TODO Auto-generated method stub
return false;
return true;
}
@Override
public void onExecute(CommandSender sender, String label, String[] args) {
// TODO Auto-generated method stub
if (!(sender instanceof Player)) {
Util.sendMessage(sender, plugin.getLocale().get("error.useInGame"));
}
Player player = (Player)sender;
if (plugin.getIslands().hasIsland(player.getUniqueId())) {
// Delete island
new DeleteIslandBlocks(plugin, plugin.getIslands().getIsland(player.getUniqueId()));
// Create new island
Schematic schematic = plugin.getSchematics().getSchematic("default");
plugin.getIslands().newIsland(player, schematic);
} else {
Util.sendMessage(player, plugin.getLocale(player.getUniqueId()).get("error.noIsland"));
}
}
@Override

View File

@ -15,7 +15,8 @@ public class BSBLocale {
final static String LOCALE_FOLDER = "locales";
private BSkyBlock plugin;
private String localeId;
//private String localeId;
private String languageTag;
private ResourceBundle rb;
Locale localeObject;
@ -27,7 +28,7 @@ public class BSBLocale {
*/
public BSBLocale(BSkyBlock plugin, String localeId) throws MalformedURLException {
this.plugin = plugin;
this.localeId = localeId;
//this.localeId = localeId;
// Check if the folder exists
File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER);
if (!localeDir.exists()) {
@ -39,7 +40,7 @@ public class BSBLocale {
// Does not exist - look in JAR and save if possible
plugin.saveResource(LOCALE_FOLDER + localeId, false);
}
String languageTag = localeId.substring(4, localeId.length() - 4).replace('_', '-');
languageTag = localeId.substring(4, localeId.length() - 4).replace('_', '-');
URL[] urls = {localeDir.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
localeObject = Locale.forLanguageTag(languageTag);
@ -53,9 +54,12 @@ public class BSBLocale {
*/
public String get(String reference) {
// TODO: add placeholder conversion?
plugin.getLogger().info("DEBUG: default lang = " + Settings.defaultLanguage);
plugin.getLogger().info("DEBUG: this locale = " + languageTag);
plugin.getLogger().info("DEBUG: reference = " + reference);
if (rb.containsKey(reference)) {
return ChatColor.translateAlternateColorCodes('&', rb.getString(reference));
} else if (!Settings.defaultLanguage.equals(localeId)){
} else if (!Settings.defaultLanguage.equals(languageTag)){
// Try default lang
return plugin.getLocale().get(reference);
}

View File

@ -3,14 +3,12 @@ package us.tastybento.bskyblock.database.flatfile;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.objects.Island;
public class FlatFileDatabase extends BSBDatabase{
@Override
public AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type) {
return new FlatFileDatabaseHandler<Island>(plugin, Island.class, new FlatFileDatabaseConnecter(plugin, null));
return new FlatFileDatabaseHandler<>(plugin, type, new FlatFileDatabaseConnecter(plugin, null));
}
}

View File

@ -19,6 +19,7 @@ public class PlayersManager{
private BSkyBlock plugin;
private BSBDatabase database;
private AbstractDatabaseHandler<Players> handler;
private HashMap<UUID, Players> playerCache;
private Set<UUID> inTeleport;
@ -30,19 +31,62 @@ public class PlayersManager{
*
* @param plugin
*/
@SuppressWarnings("unchecked")
public PlayersManager(BSkyBlock plugin){
this.plugin = plugin;
database = BSBDatabase.getDatabase();
// Set up the database handler to store and retrieve Players classes
handler = (AbstractDatabaseHandler<Players>) database.getHandler(plugin, Players.class);
playerCache = new HashMap<UUID, Players>();
inTeleport = new HashSet<UUID>();
}
/**
* Load all players
*/
public void load(){
//TODO
playerCache.clear();
inTeleport.clear();
try {
for (Players player : handler.loadObjects()) {
playerCache.put(player.getPlayerUUID(), player);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Save all players
* @param async - if true, save async
*/
public void save(boolean async){
// TODO
if(async){
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
for(Players player : playerCache.values()){
try {
handler.saveObject(player);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
} else {
for(Players player : playerCache.values()){
try {
handler.saveObject(player);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void shutdown(){
@ -51,6 +95,9 @@ public class PlayersManager{
}
public Players getPlayer(UUID uuid){
if (!playerCache.containsKey(uuid)) {
addPlayer(uuid);
}
return playerCache.get(uuid);
}
@ -61,12 +108,14 @@ public class PlayersManager{
public Players addPlayer(final UUID playerUUID) {
if (playerUUID == null)
return null;
//plugin.getLogger().info("DEBUG: added player " + playerUUID);
plugin.getLogger().info("DEBUG: added player " + playerUUID);
if (!playerCache.containsKey(playerUUID)) {
plugin.getLogger().info("DEBUG: new player");
final Players player = new Players(playerUUID);
playerCache.put(playerUUID, player);
return player;
} else {
plugin.getLogger().info("DEBUG: returning cache");
return playerCache.get(playerUUID);
}
}

View File

@ -4,13 +4,12 @@ import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.DatabaseConnectionSettingsImpl;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.objects.Island;
public class MySQLDatabase extends BSBDatabase{
@Override
public AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type) {
return new MySQLDatabaseHandler<Island>(plugin, Island.class, new MySQLDatabaseConnecter(new DatabaseConnectionSettingsImpl()));
return new MySQLDatabaseHandler<>(plugin, type, new MySQLDatabaseConnecter(new DatabaseConnectionSettingsImpl()));
}
}

View File

@ -18,27 +18,32 @@ import us.tastybento.bskyblock.config.Settings;
*/
public class Players extends DataObject {
private HashMap<Integer, Location> homeLocations;
private UUID uuid;
private UUID uniqueId;
private String playerName;
private int resetsLeft;
private String locale;
private String locale = "";
private boolean useControlPanel;
private int deaths;
private HashMap<Location, Date> kickedList;
private HashMap<Location, Long> kickedList;
/**
* @param uuid
* This is required for database storage
*/
public Players() {}
/**
* @param uniqueId
* Constructor - initializes the state variables
*
*/
public Players(final UUID uuid) {
this.uuid = uuid;
public Players(final UUID uniqueId) {
this.uniqueId = uniqueId;
this.homeLocations = new HashMap<Integer,Location>();
this.playerName = "";
this.resetsLeft = Settings.resetLimit;
this.locale = "";
this.useControlPanel = Settings.useControlPanel;
this.kickedList = new HashMap<Location, Date>();
this.kickedList = new HashMap<Location, Long>();
}
/**
@ -63,23 +68,60 @@ public class Players extends DataObject {
}
/**
* Provides a list of all home locations - used when searching for a safe spot to place someone
* @return List of home locations
*/
public HashMap<Integer,Location> getHomeLocations() {
HashMap<Integer,Location> result = new HashMap<Integer,Location>();
for (int number : homeLocations.keySet()) {
result.put(number, homeLocations.get(number));
}
return result;
return homeLocations;
}
/**
* @return the useControlPanel
*/
public boolean isUseControlPanel() {
return useControlPanel;
}
/**
* @param useControlPanel the useControlPanel to set
*/
public void setUseControlPanel(boolean useControlPanel) {
this.useControlPanel = useControlPanel;
}
/**
* @return the kickedList
*/
public HashMap<Location, Long> getKickedList() {
return kickedList;
}
/**
* @param kickedList the kickedList to set
*/
public void setKickedList(HashMap<Location, Long> kickedList) {
this.kickedList = kickedList;
}
/**
* @param homeLocations the homeLocations to set
*/
public void setHomeLocations(HashMap<Integer, Location> homeLocations) {
this.homeLocations = homeLocations;
}
/**
* @param playerName the playerName to set
*/
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public Player getPlayer() {
return Bukkit.getPlayer(uuid);
return Bukkit.getPlayer(uniqueId);
}
public UUID getPlayerUUID() {
return uuid;
return uniqueId;
}
public String getPlayerName() {
@ -134,7 +176,7 @@ public class Players extends DataObject {
* @param uuid
*/
public void setPlayerUUID(final UUID uuid) {
this.uuid = uuid;
this.uniqueId = uuid;
}
/**
@ -213,7 +255,7 @@ public class Players extends DataObject {
// plugin.getLogger().info("DEBUG: Location is known");
// The location is in the list
// Check the date/time
Date kickedDate = kickedList.get(location);
Date kickedDate = new Date(kickedList.get(location));
// plugin.getLogger().info("DEBUG: kicked date = " + kickedDate);
Calendar coolDownTime = Calendar.getInstance();
coolDownTime.setTime(kickedDate);
@ -244,20 +286,18 @@ public class Players extends DataObject {
*/
public void startInviteCoolDownTimer(Location location) {
if (location != null) {
kickedList.put(location, new Date());
kickedList.put(location, System.currentTimeMillis());
}
}
@Override
public String getUniqueId() {
// TODO Auto-generated method stub
return null;
return uniqueId.toString();
}
@Override
public void setUniqueId(String uniqueId) {
// TODO Auto-generated method stub
this.uniqueId = UUID.fromString(uniqueId);
}
}

View File

@ -0,0 +1,261 @@
/*******************************************************************************
* This file is part of BSkyBlock.
*
* BSkyBlock is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BSkyBlock is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BSkyBlock. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
package us.tastybento.bskyblock.util;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.scheduler.BukkitRunnable;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.util.nms.NMSAbstraction;
//import com.wasteofplastic.askyblock.nms.NMSAbstraction;
/**
* Deletes islands fast using chunk regeneration
*
* @author tastybento
*
*/
public class DeleteIslandBlocks {
protected static final int CLEAN_RATE = 2;
private Set<Pair> chunksToClear = new HashSet<Pair>();
//private HashMap<Location, Material> blocksToClear = new HashMap<Location,Material>();
private NMSAbstraction nms = null;
public DeleteIslandBlocks(final BSkyBlock plugin, final Island island) {
final World world = island.getCenter().getWorld();
if (world == null)
return;
// Determine if blocks need to be cleaned up or not
boolean cleanUpBlocks = false;
if (Settings.islandDistance - island.getProtectionRange() < 16) {
cleanUpBlocks = true;
}
int range = island.getProtectionRange() / 2 * +1;
final int minx = island.getMinProtectedX();
final int minz = island.getMinProtectedZ();
final int maxx = island.getMinProtectedX() + island.getProtectionRange();
final int maxz = island.getMinProtectedZ() + island.getProtectionRange();
// plugin.getLogger().info("DEBUG: protection limits are: " + minx +
// ", " + minz + " to " + maxx + ", " + maxz );
int islandSpacing = Settings.islandDistance - island.getProtectionRange();
int minxX = (island.getCenter().getBlockX() - range - islandSpacing);
int minzZ = (island.getCenter().getBlockZ() - range - islandSpacing);
int maxxX = (island.getCenter().getBlockX() + range + islandSpacing);
int maxzZ = (island.getCenter().getBlockZ() + range + islandSpacing);
// plugin.getLogger().info("DEBUG: absolute max limits are: " + minxX +
// ", " + minzZ + " to " + maxxX + ", " + maxzZ );
// get the chunks for these locations
final Chunk minChunk = world.getBlockAt(minx,0,minz).getChunk();
final Chunk maxChunk = world.getBlockAt(maxx, 0, maxz).getChunk();
// Find out what chunks are within the island protection range
// plugin.getLogger().info("DEBUG: chunk limits are: " +
// (minChunk.getBlock(0, 0, 0).getLocation().getBlockX()) + ", " +
// (minChunk.getBlock(0, 0, 0).getLocation().getBlockZ())
// + " to " + (maxChunk.getBlock(15, 0, 15).getLocation().getBlockX()) +
// ", " + (maxChunk.getBlock(15, 0, 15).getLocation().getBlockZ()));
for (int x = minChunk.getX(); x <= maxChunk.getX(); x++) {
for (int z = minChunk.getZ(); z <= maxChunk.getZ(); z++) {
boolean regen = true;
if (world.getChunkAt(x, z).getBlock(0, 0, 0).getX() < minxX) {
// plugin.getLogger().info("DEBUG: min x coord is less than absolute min! "
// + minxX);
regen = false;
}
if (world.getChunkAt(x, z).getBlock(0, 0, 0).getZ() < minzZ) {
// plugin.getLogger().info("DEBUG: min z coord is less than absolute min! "
// + minzZ);
regen = false;
}
if (world.getChunkAt(x, z).getBlock(15, 0, 15).getX() > maxxX) {
// plugin.getLogger().info("DEBUG: max x coord is more than absolute max! "
// + maxxX);
regen = false;
}
if (world.getChunkAt(x, z).getBlock(15, 0, 15).getZ() > maxzZ) {
// plugin.getLogger().info("DEBUG: max z coord in chunk is more than absolute max! "
// + maxzZ);
regen = false;
}
if (regen) {
world.regenerateChunk(x, z);
if (Settings.islandNether && Settings.createNether) {
if (world.equals(IslandWorld.getIslandWorld())) {
IslandWorld.getNetherWorld().regenerateChunk(x, z);
}
if (world.equals(IslandWorld.getNetherWorld())) {
IslandWorld.getIslandWorld().regenerateChunk(x, z);
}
}
} else {
// Add to clear up list if requested
if (cleanUpBlocks) {
chunksToClear.add(new Pair(x,z));
}
}
}
}
// Remove from database
plugin.getIslands().deleteIsland(island.getCenter());
// Clear up any chunks
if (!chunksToClear.isEmpty()) {
try {
nms = Util.getNMSHandler();
} catch (Exception ex) {
plugin.getLogger().warning("Cannot clean up blocks because there is no NMS acceleration available");
return;
}
plugin.getLogger().info("Island delete: There are " + chunksToClear.size() + " chunks that need to be cleared up.");
plugin.getLogger().info("Clean rate is " + CLEAN_RATE + " chunks per second. Should take ~" + Math.round(chunksToClear.size()/CLEAN_RATE) + "s");
new BukkitRunnable() {
@SuppressWarnings("deprecation")
@Override
public void run() {
Iterator<Pair> it = chunksToClear.iterator();
int count = 0;
while (it.hasNext() && count++ < CLEAN_RATE) {
Pair pair = it.next();
//plugin.getLogger().info("DEBUG: There are " + chunksToClear.size() + " chunks that need to be cleared up");
//plugin.getLogger().info("DEBUG: Deleting chunk " + pair.getLeft() + ", " + pair.getRight());
// Check if coords are in island space
for (int x = 0; x < 16; x ++) {
for (int z = 0; z < 16; z ++) {
int xCoord = pair.getLeft() * 16 + x;
int zCoord = pair.getRight() * 16 + z;
if (island.inIslandSpace(xCoord, zCoord)) {
//plugin.getLogger().info(xCoord + "," + zCoord + " is in island space - deleting column");
// Delete all the blocks here
for (int y = 0; y < IslandWorld.getIslandWorld().getMaxHeight(); y ++) {
// Overworld
Block b = IslandWorld.getIslandWorld().getBlockAt(xCoord, y, zCoord);
Material bt = b.getType();
Material setTo = Material.AIR;
// Split depending on below or above water line
if (y < Settings.seaHeight) {
setTo = Material.STATIONARY_WATER;
}
// Grab anything out of containers (do that it is
// destroyed)
switch (bt) {
case CHEST:
case TRAPPED_CHEST:
case FURNACE:
case DISPENSER:
case HOPPER:
final InventoryHolder ih = ((InventoryHolder)b.getState());
ih.getInventory().clear();
b.setType(setTo);
break;
case AIR:
if (setTo.equals(Material.STATIONARY_WATER)) {
nms.setBlockSuperFast(b, setTo.getId(), (byte)0, false);
}
case STATIONARY_WATER:
if (setTo.equals(Material.AIR)) {
nms.setBlockSuperFast(b, setTo.getId(), (byte)0, false);
}
default:
nms.setBlockSuperFast(b, setTo.getId(), (byte)0, false);
break;
}
// Nether, if it exists
if (Settings.islandNether && Settings.createNether && y < IslandWorld.getNetherWorld().getMaxHeight() - 8) {
b = IslandWorld.getNetherWorld().getBlockAt(xCoord, y, zCoord);
bt = b.getType();
if (!b.equals(Material.AIR)) {
setTo = Material.AIR;
// Grab anything out of containers (do that it is
// destroyed)
switch (bt) {
case CHEST:
case TRAPPED_CHEST:
case FURNACE:
case DISPENSER:
case HOPPER:
final InventoryHolder ih = ((InventoryHolder)b.getState());
ih.getInventory().clear();
b.setType(setTo);
break;
default:
nms.setBlockSuperFast(b, setTo.getId(), (byte)0, false);
break;
}
}
}
}
}
}
}
it.remove();
}
if (chunksToClear.isEmpty()){
plugin.getLogger().info("Finished island deletion");
this.cancel();
}
}
}.runTaskTimer(plugin, 0L, 20L);
}
}
/**
* Class that pairs two ints together
* @author tastybento
*
*/
public class Pair {
private final int left;
private final int right;
public Pair(int left, int right) {
this.left = left;
this.right = right;
}
public int getLeft() {
return left;
}
public int getRight() {
return right;
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (!(o instanceof Pair))
return false;
Pair pairo = (Pair) o;
return (this.left == pairo.getLeft()) && (this.right == pairo.getRight());
}
}
}