Added protection around null owners.

This commit is contained in:
tastybento 2017-07-05 08:41:12 -07:00
parent 3d74c4a427
commit dee635f15f
7 changed files with 67 additions and 24 deletions

View File

@ -10,7 +10,6 @@ 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;
@ -255,10 +254,16 @@ public class IslandCommand extends BSBCommand{
}
Player player = (Player)sender;
if (plugin.getIslands().hasIsland(player.getUniqueId())) {
plugin.getIslands().deletePlayerIsland(player.getUniqueId(), true);
// Create new island
// Get the player's old island
Island oldIsland = plugin.getIslands().getIsland(player.getUniqueId());
plugin.getLogger().info("DEBUG: old island is at " + oldIsland.getCenter().getBlockX() + "," + oldIsland.getCenter().getBlockZ());
// Remove them from this island (it still exists and will be deleted later)
plugin.getIslands().removePlayer(player.getUniqueId());
plugin.getLogger().info("DEBUG: old island's owner is " + oldIsland.getOwner());
// Create new island and then delete the old one
plugin.getLogger().info("DEBUG: making new island ");
Schematic schematic = plugin.getSchematics().getSchematic("default");
plugin.getIslands().newIsland(player, schematic);
plugin.getIslands().newIsland(player, schematic, oldIsland);
} else {
Util.sendMessage(player, plugin.getLocale(player.getUniqueId()).get("error.noIsland"));

View File

@ -335,7 +335,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
}
@Override
protected void deleteObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
public void deleteObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
// The file name of the Yaml file.
PropertyDescriptor propertyDescriptor = new PropertyDescriptor("uniqueId", type);
Method method = propertyDescriptor.getReadMethod();

View File

@ -157,6 +157,6 @@ public abstract class AbstractDatabaseHandler<T> {
* @throws SecurityException
* @throws NoSuchMethodException
*/
protected abstract void deleteObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, NoSuchMethodException, SecurityException;
public abstract void deleteObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, NoSuchMethodException, SecurityException;
}

View File

@ -156,7 +156,7 @@ public class IslandsManager {
}
/**
* Create an island with owner. Note this does not create the schematic. It just reates the island data object.
* Create an island with owner. Note this does not create the schematic. It just creates the island data object.
* @param location
* @param owner UUID
*/
@ -206,6 +206,8 @@ public class IslandsManager {
//getWarpSignsListener().removeWarp(player);
Island island = getIsland(player);
if (island != null) {
// Set the owner of the island to no one.
island.setOwner(null);
if (removeBlocks) {
removePlayersFromIsland(island, player);
new DeleteIslandBlocks(plugin, island);
@ -443,7 +445,7 @@ public class IslandsManager {
*/
public boolean homeTeleport(final Player player, int number) {
Location home = null;
//plugin.getLogger().info("home teleport called for #" + number);
plugin.getLogger().info("home teleport called for #" + number);
home = getSafeHomeLocation(player.getUniqueId(), number);
//plugin.getLogger().info("home get safe loc = " + home);
// Check if the player is a passenger in a boat
@ -458,12 +460,12 @@ public class IslandsManager {
}
}
if (home == null) {
//plugin.getLogger().info("Fixing home location using safe spot teleport");
plugin.getLogger().info("Fixing home location using safe spot teleport");
// Try to fix this teleport location and teleport the player if possible
new SafeSpotTeleport(plugin, player, plugin.getPlayers().getHomeLocation(player.getUniqueId(), number), number);
return true;
}
//plugin.getLogger().info("DEBUG: home loc = " + home + " teleporting");
plugin.getLogger().info("DEBUG: home loc = " + home + " teleporting");
//home.getChunk().load();
player.teleport(home);
//player.sendBlockChange(home, Material.GLOWSTONE, (byte)0);
@ -737,11 +739,24 @@ public class IslandsManager {
/**
* Makes an island using schematic. No permission checks are made. They have to be decided
* before this method is called.
* before this method is called. If oldIsland is not null, it will be deleted after the new
* island is made.
* @param player
* @param schematic
*/
public void newIsland(final Player player, final Schematic schematic) {
public void newIsland(Player player, Schematic schematic) {
newIsland(player, schematic, null);
}
/**
* Makes an island using schematic. No permission checks are made. They have to be decided
* before this method is called. If oldIsland is not null, it will be deleted after the new
* island is made.
* @param player
* @param schematic
* @param oldIsland - the old island to be deleted after the new island is made
*/
public void newIsland(final Player player, final Schematic schematic, Island oldIsland) {
plugin.getLogger().info("DEBUG: new island");
//long time = System.nanoTime();
final UUID playerUUID = player.getUniqueId();
@ -752,6 +767,13 @@ public class IslandsManager {
plugin.getLogger().info("DEBUG: finding island location");
Location next = getNextIsland(player.getUniqueId());
plugin.getLogger().info("DEBUG: found " + next);
// Add to the grid
Island myIsland = plugin.getIslands().createIsland(next, playerUUID);
myIsland.setLevelHandicap(schematic.getLevelHandicap());
// Save the player so that if the server is reset weird things won't happen
plugin.getPlayers().save(true);
// Clear any old home locations (they should be clear, but just in case)
plugin.getPlayers().clearHomeLocations(playerUUID);
@ -779,13 +801,13 @@ public class IslandsManager {
next = next.toVector().toLocation(IslandWorld.getNetherWorld());
// Set the player's island location to this new spot
//plugin.getPlayers().setIslandLocation(playerUUID, next);
schematic.pasteSchematic(next, player, true, firstTime ? PasteReason.NEW_ISLAND: PasteReason.RESET);
schematic.pasteSchematic(next, player, true, firstTime ? PasteReason.NEW_ISLAND: PasteReason.RESET, oldIsland);
} else {
// Over world start
//plugin.getLogger().info("DEBUG: pasting");
//long timer = System.nanoTime();
// Paste the island and teleport the player home
schematic.pasteSchematic(next, player, true, firstTime ? PasteReason.NEW_ISLAND: PasteReason.RESET);
schematic.pasteSchematic(next, player, true, firstTime ? PasteReason.NEW_ISLAND: PasteReason.RESET, oldIsland);
//double diff = (System.nanoTime() - timer)/1000000;
//plugin.getLogger().info("DEBUG: nano time = " + diff + " ms");
//plugin.getLogger().info("DEBUG: pasted overworld");
@ -809,11 +831,7 @@ public class IslandsManager {
}
}
}
// Add to the grid
Island myIsland = plugin.getIslands().createIsland(next, playerUUID);
myIsland.setLevelHandicap(schematic.getLevelHandicap());
// Save the player so that if the server is reset weird things won't happen
plugin.getPlayers().save(true);
// Start the reset cooldown
//if (!firstTime) {
@ -870,7 +888,7 @@ public class IslandsManager {
@Override
public void run() {
schematic.pasteSchematic(loc, player, false, PasteReason.PARTNER);
schematic.pasteSchematic(loc, player, false, PasteReason.PARTNER, null);
}}, 60L);
@ -969,4 +987,8 @@ public class IslandsManager {
}
}
public AbstractDatabaseHandler<Island> getHandler() {
return handler;
}
}

View File

@ -728,7 +728,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
* @see us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler#deleteObject(java.lang.Object)
*/
@Override
protected void deleteObject(T instance)
public void deleteObject(T instance)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, IntrospectionException, SQLException, NoSuchMethodException, SecurityException {
// Delete this object from all tables

View File

@ -485,7 +485,7 @@ public class Island extends DataObject {
* @return the island display name or the owner's name if none is set
*/
public String getName(){
return (name != null) ? name : Bukkit.getServer().getOfflinePlayer(owner).getName();
return (name != null && owner != null) ? name : Bukkit.getServer().getOfflinePlayer(owner).getName();
}
/**

View File

@ -70,6 +70,8 @@ import net.milkbowl.vault.economy.EconomyResponse;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.config.Settings.GameType;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.util.DeleteIslandBlocks;
import us.tastybento.bskyblock.util.Util;
import us.tastybento.bskyblock.util.VaultHelper;
import us.tastybento.bskyblock.util.nms.NMSAbstraction;
@ -776,9 +778,10 @@ public class Schematic {
* This method pastes a schematic.
* @param loc
* @param player
* @param oldIsland
* @param partner
*/
public void pasteSchematic(final Location loc, final Player player, boolean teleport, final PasteReason reason) {
public void pasteSchematic(final Location loc, final Player player, boolean teleport, final PasteReason reason, Island oldIsland) {
// If this is not a file schematic, paste the default island
if (this.file == null) {
if (Settings.GAMETYPE == GameType.ACIDISLAND) {
@ -1042,6 +1045,7 @@ public class Schematic {
//player.setInvulnerable(true);
// Check distance. If it's too close, warp to spawn to try to clear the client's cache
//plugin.getLogger().info("DEBUG: view dist = " + plugin.getServer().getViewDistance());
/*
if (player.getWorld().equals(world)) {
//plugin.getLogger().info("DEBUG: same world");
int distSq = (int)((player.getLocation().distanceSquared(loc) - (Settings.islandDistance * Settings.islandDistance)/16));
@ -1050,7 +1054,7 @@ public class Schematic {
//plugin.getLogger().info("DEBUG: teleporting");
player.teleport(world.getSpawnLocation());
}
}
}*/
plugin.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
@Override
@ -1100,6 +1104,18 @@ public class Schematic {
//IslandCmd.runCommands(Settings.resetCommands, player);
}
}
// Delete the old island if required
if (oldIsland != null) {
plugin.getLogger().info("DEBUG: Deleting old island");
new DeleteIslandBlocks(plugin, oldIsland);
try {
plugin.getIslands().getHandler().deleteObject(oldIsland);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}, 10L);