Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Island delete #466

Merged
merged 9 commits into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/main/java/world/bentobox/bentobox/BentoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.HooksManager;
import world.bentobox.bentobox.managers.IslandDeletionManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
Expand Down Expand Up @@ -67,6 +68,8 @@ public class BentoBox extends JavaPlugin {

private boolean isLoaded;

private IslandDeletionManager islandDeletionManager;

@Override
public void onEnable(){
if (!ServerCompatibility.getInstance().checkCompatibility(this).isCanLaunch()) {
Expand Down Expand Up @@ -142,10 +145,10 @@ public void onEnable(){
// Load islands from database - need to wait until all the worlds are loaded
islandsManager.load();

// Save islands & players data asynchronously every X minutes
// Save islands & players data every X minutes
instance.getServer().getScheduler().runTaskTimer(instance, () -> {
playersManager.save(true);
islandsManager.save(true);
playersManager.saveAll();
islandsManager.saveAll();
}, getSettings().getDatabaseBackupPeriod() * 20 * 60L, getSettings().getDatabaseBackupPeriod() * 20 * 60L);

// Make sure all flag listeners are registered.
Expand All @@ -169,13 +172,15 @@ public void onEnable(){
// Setup the Placeholders manager
placeholdersManager = new PlaceholdersManager(this);

// Fire plugin ready event
isLoaded = true;
Bukkit.getServer().getPluginManager().callEvent(new BentoBoxReadyEvent());

// Show banner
User.getInstance(Bukkit.getConsoleSender()).sendMessage("successfully-loaded",
TextVariables.VERSION, instance.getDescription().getVersion(),
"[time]", String.valueOf(System.currentTimeMillis() - startMillis));

// Fire plugin ready event - this should go last after everything else
isLoaded = true;
Bukkit.getServer().getPluginManager().callEvent(new BentoBoxReadyEvent());

});
}

Expand All @@ -196,6 +201,9 @@ private void registerListeners() {
manager.registerEvents(new BannedVisitorCommands(this), this);
// Death counter
manager.registerEvents(new DeathListener(this), this);
// Island Delete Manager
islandDeletionManager = new IslandDeletionManager(this);
manager.registerEvents(islandDeletionManager, this);
}

@Override
Expand Down Expand Up @@ -356,4 +364,11 @@ public Optional<VaultHook> getVault() {
public PlaceholdersManager getPlaceholdersManager() {
return placeholdersManager;
}

/**
* @return the islandDeletionManager
*/
public IslandDeletionManager getIslandDeletionManager() {
return islandDeletionManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public boolean execute(User user, String label, List<String> args) {
return false;
}

// Check if this spot is still being deleted
Location closestIsland = getClosestIsland(user.getLocation());
if (getPlugin().getIslandDeletionManager().inDeletion(closestIsland)) {
user.sendMessage("commands.admin.register.in-deletion");
return false;
}
// Check if island is owned
Optional<Island> island = getIslands().getIslandAt(user.getLocation());
if (island.map(i -> i.getOwner() != null).orElse(false)) {
Expand All @@ -69,7 +75,7 @@ public boolean execute(User user, String label, List<String> args) {
user.sendMessage("commands.admin.register.no-island-here");
this.askConfirmation(user, () -> {
// Make island here
Island i = getIslands().createIsland(getClosestIsland(user.getLocation()), targetUUID);
Island i = getIslands().createIsland(closestIsland, targetUUID);
getIslands().setOwner(user, targetUUID, i);
getWorld().getBlockAt(i.getCenter()).setType(Material.BEDROCK);
user.sendMessage("commands.admin.register.registered-island", "[xyz]", Util.xyz(i.getCenter().toVector()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package world.bentobox.bentobox.api.commands.admin.range;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;

public class AdminRangeResetCommand extends CompositeCommand {

Expand Down Expand Up @@ -50,4 +53,15 @@ public boolean execute(User user, String label, List<String> args) {

return true;
}

@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
if (args.isEmpty()) {
// Don't show every player on the server. Require at least the first letter
return Optional.empty();
}
List<String> options = new ArrayList<>(Util.getOnlinePlayerList(user));
return Optional.of(Util.tabLimit(options, lastArg));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package world.bentobox.bentobox.api.commands.admin.range;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
Expand All @@ -9,6 +11,7 @@
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;

public class AdminRangeSetCommand extends CompositeCommand {

Expand Down Expand Up @@ -72,4 +75,15 @@ public boolean execute(User user, String label, List<String> args) {

return true;
}

@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
if (args.isEmpty()) {
// Don't show every player on the server. Require at least the first letter
return Optional.empty();
}
List<String> options = new ArrayList<>(Util.getOnlinePlayerList(user));
return Optional.of(Util.tabLimit(options, lastArg));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public boolean execute(User user, String label, List<String> args) {
if (inviter != null) {
inviter.sendMessage("commands.island.team.invite.accept.name-joined-your-island", TextVariables.NAME, user.getName());
}
getIslands().save(false);
getIslands().save(island);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import world.bentobox.bentobox.api.events.team.TeamEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;

public class IslandTeamSetownerCommand extends CompositeCommand {
Expand Down Expand Up @@ -59,9 +60,9 @@ public boolean execute(User user, String label, List<String> args) {
return false;
}
// Fire event so add-ons can run commands, etc.
Island island = getIslands().getIsland(getWorld(), playerUUID);
IslandBaseEvent event = TeamEvent.builder()
.island(getIslands()
.getIsland(getWorld(), playerUUID))
.island(island)
.reason(TeamEvent.Reason.SETOWNER)
.involvedPlayer(targetUUID)
.build();
Expand All @@ -70,7 +71,7 @@ public boolean execute(User user, String label, List<String> args) {
return false;
}
getIslands().setOwner(getWorld(), user, targetUUID);
getIslands().save(true);
getIslands().save(island);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public IslandBaseEvent(Island island, UUID playerUUID, boolean admin, Location l
}

/**
* @return the island involved in this event
* @return the island involved in this event. This may be null in the case of deleted islands, so use location instead
*/
public Island getIsland(){
return island;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.Location;

import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.database.objects.IslandDeletion;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;

Expand Down Expand Up @@ -58,9 +59,14 @@ public enum Reason {
*/
CREATED,
/**
* Fired just before any island chunks are to be deleted.
* Fired when an island is to be deleted. Note an island can be deleted without having
* chunks removed.
*/
DELETE,
/**
* Fired when island chunks are going to be deleted
*/
DELETE_CHUNKS,
/**
* Fired after all island chunks have been deleted or set for regeneration by the server
*/
Expand Down Expand Up @@ -168,14 +174,39 @@ private IslandDeleteEvent(Island island, UUID player, boolean admin, Location lo
super(island, player, admin, location);
}
}
/**
* Fired when an island chunks are going to be deleted.
* May be cancelled.
*
*/
public static class IslandDeleteChunksEvent extends IslandBaseEvent {
private final IslandDeletion deletedIslandInfo;

private IslandDeleteChunksEvent(Island island, UUID player, boolean admin, Location location, IslandDeletion deletedIsland) {
// Final variables have to be declared in the constructor
super(island, player, admin, location);
this.deletedIslandInfo = deletedIsland;
}

public IslandDeletion getDeletedIslandInfo() {
return deletedIslandInfo;
}
}
/**
* Fired when an island is deleted.
*
*/
public static class IslandDeletedEvent extends IslandBaseEvent {
private IslandDeletedEvent(Island island, UUID player, boolean admin, Location location) {
private final IslandDeletion deletedIslandInfo;

private IslandDeletedEvent(Island island, UUID player, boolean admin, Location location, IslandDeletion deletedIsland) {
// Final variables have to be declared in the constructor
super(island, player, admin, location);
this.deletedIslandInfo = deletedIsland;
}

public IslandDeletion getDeletedIslandInfo() {
return deletedIslandInfo;
}
}
/**
Expand Down Expand Up @@ -256,6 +287,7 @@ public static class IslandEventBuilder {
private Reason reason = Reason.UNKNOWN;
private boolean admin;
private Location location;
private IslandDeletion deletedIslandInfo;

public IslandEventBuilder island(Island island) {
this.island = island;
Expand Down Expand Up @@ -295,6 +327,11 @@ public IslandEventBuilder location(Location center) {
return this;
}

public IslandEventBuilder deletedIslandInfo(IslandDeletion deletedIslandInfo) {
this.deletedIslandInfo = deletedIslandInfo;
return this;
}

public IslandBaseEvent build() {
// Call the generic event for developers who just want one event and use the Reason enum
Bukkit.getServer().getPluginManager().callEvent(new IslandEvent(island, player, admin, location, reason));
Expand All @@ -316,8 +353,12 @@ public IslandBaseEvent build() {
IslandDeleteEvent delete = new IslandDeleteEvent(island, player, admin, location);
Bukkit.getServer().getPluginManager().callEvent(delete);
return delete;
case DELETE_CHUNKS:
IslandDeleteChunksEvent deleteChunks = new IslandDeleteChunksEvent(island, player, admin, location, deletedIslandInfo);
Bukkit.getServer().getPluginManager().callEvent(deleteChunks);
return deleteChunks;
case DELETED:
IslandDeletedEvent deleted = new IslandDeletedEvent(island, player, admin, location);
IslandDeletedEvent deleted = new IslandDeletedEvent(island, player, admin, location, deletedIslandInfo);
Bukkit.getServer().getPluginManager().callEvent(deleted);
return deleted;
case ENTER:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;

Expand Down Expand Up @@ -38,12 +40,14 @@ public abstract class AbstractDatabaseHandler<T> {
/**
* The addon that is accessing the database, if any.
*/
@Nullable
private Addon addon;

/**
* Get the addon that is accessing the database, if any. May be null.
* @return the addon
*/
@Nullable
public Addon getAddon() {
return addon;
}
Expand All @@ -52,7 +56,7 @@ public Addon getAddon() {
* Set the addon that is accessing the database, if any.
* @param addon the addon to set
*/
public void setAddon(Addon addon) {
public void setAddon(@Nullable Addon addon) {
this.addon = addon;
}

Expand Down Expand Up @@ -84,7 +88,8 @@ protected AbstractDatabaseHandler(BentoBox plugin, Class<T> type, DatabaseConnec
* @param uniqueId - unique ID
* @return <T>
*/
public abstract T loadObject(String uniqueId) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, NoSuchMethodException;
@Nullable
public abstract T loadObject(@NonNull String uniqueId) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, NoSuchMethodException;

/**
* Save T into the corresponding database
Expand All @@ -110,4 +115,12 @@ protected AbstractDatabaseHandler(BentoBox plugin, Class<T> type, DatabaseConnec
* Closes the database
*/
public abstract void close();

/**
* Attempts to delete the object with the uniqueId
* @param uniqueId - uniqueId of object
* @return true if successful, false if there is no such uniqueId
* @since 1.1
*/
public abstract boolean deleteID(String uniqueId);
}
Loading