Added ability to edit the BlueprintBundle in IslandCreate/ResetEvent

https://github.com/BentoBoxWorld/BentoBox/issues/635
This commit is contained in:
Florian CUNY 2019-07-04 14:31:21 +02:00
parent 00ac8dd12a
commit d9d4805ea8
2 changed files with 68 additions and 5 deletions

View File

@ -5,7 +5,10 @@ import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.blueprints.Blueprint;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBundle;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.database.objects.IslandDeletion;
import world.bentobox.bentobox.lists.Flags;
@ -177,9 +180,27 @@ public class IslandEvent extends IslandBaseEvent {
*
*/
public static class IslandCreateEvent extends IslandBaseEvent {
private IslandCreateEvent(Island island, UUID player, boolean admin, Location location) {
private @NonNull BlueprintBundle blueprintBundle;
private IslandCreateEvent(Island island, UUID player, boolean admin, Location location, @NonNull BlueprintBundle blueprintBundle) {
// Final variables have to be declared in the constructor
super(island, player, admin, location);
this.blueprintBundle = blueprintBundle;
}
/**
* @since 1.6.0
*/
@NonNull
public BlueprintBundle getBlueprintBundle() {
return blueprintBundle;
}
/**
* @since 1.6.0
*/
public void setBlueprintBundle(@NonNull BlueprintBundle blueprintBundle) {
this.blueprintBundle = blueprintBundle;
}
}
/**
@ -307,9 +328,27 @@ public class IslandEvent extends IslandBaseEvent {
* May be cancelled.
*/
public static class IslandResetEvent extends IslandBaseEvent {
private IslandResetEvent(Island island, UUID player, boolean admin, Location location) {
private @NonNull BlueprintBundle blueprintBundle;
private IslandResetEvent(Island island, UUID player, boolean admin, Location location, @NonNull BlueprintBundle blueprintBundle) {
// Final variables have to be declared in the constructor
super(island, player, admin, location);
this.blueprintBundle = blueprintBundle;
}
/**
* @since 1.6.0
*/
@NonNull
public BlueprintBundle getBlueprintBundle() {
return blueprintBundle;
}
/**
* @since 1.6.0
*/
public void setBlueprintBundle(@NonNull BlueprintBundle blueprintBundle) {
this.blueprintBundle = blueprintBundle;
}
}
/**
@ -341,6 +380,7 @@ public class IslandEvent extends IslandBaseEvent {
private boolean admin;
private Location location;
private IslandDeletion deletedIslandInfo;
private BlueprintBundle blueprintBundle;
public IslandEventBuilder island(Island island) {
this.island = island;
@ -385,6 +425,15 @@ public class IslandEvent extends IslandBaseEvent {
return this;
}
/**
* @since 1.6.0
*/
@NonNull
public IslandEventBuilder blueprintBundle(@NonNull BlueprintBundle blueprintBundle) {
this.blueprintBundle = blueprintBundle;
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));
@ -399,7 +448,7 @@ public class IslandEvent extends IslandBaseEvent {
Bukkit.getServer().getPluginManager().callEvent(ban);
return ban;
case CREATE:
IslandCreateEvent create = new IslandCreateEvent(island, player, admin, location);
IslandCreateEvent create = new IslandCreateEvent(island, player, admin, location, blueprintBundle);
Bukkit.getServer().getPluginManager().callEvent(create);
return create;
case CREATED:
@ -431,7 +480,7 @@ public class IslandEvent extends IslandBaseEvent {
Bukkit.getServer().getPluginManager().callEvent(lock);
return lock;
case RESET:
IslandResetEvent reset = new IslandResetEvent(island, player, admin, location);
IslandResetEvent reset = new IslandResetEvent(island, player, admin, location, blueprintBundle);
Bukkit.getServer().getPluginManager().callEvent(reset);
return reset;
case RESETTED:

View File

@ -37,7 +37,7 @@ public class NewIsland {
private final User user;
private final Reason reason;
private final World world;
private final String name;
private String name;
private final boolean noPaste;
private GameModeAddon addon;
@ -185,10 +185,24 @@ public class NewIsland {
.reason(reason)
.island(island)
.location(island.getCenter())
.blueprintBundle(plugin.getBlueprintsManager().getBlueprintBundles(addon).get(name))
.build();
if (event.isCancelled()) {
return;
}
// Get the new BlueprintBundle if it was changed
switch (reason) {
case CREATE:
name = ((IslandEvent.IslandCreateEvent) event).getBlueprintBundle().getUniqueId();
break;
case RESET:
name = ((IslandEvent.IslandResetEvent) event).getBlueprintBundle().getUniqueId();
break;
default:
break;
}
// Task to run after creating the island
Runnable task = () -> {
// Set initial spawn point if one exists