From c003a7d7e79633cad34219aa36a413a3d5e07a32 Mon Sep 17 00:00:00 2001 From: Peaches_MLG Date: Sat, 23 Sep 2023 12:32:14 +0100 Subject: [PATCH] Added IslandCreateEvent --- .../api/IslandCreateEvent.java | 78 +++++++++++++++++++ .../commands/RegenCommand.java | 9 ++- .../managers/IslandManager.java | 17 ++-- 3 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/iridium/iridiumskyblock/api/IslandCreateEvent.java diff --git a/src/main/java/com/iridium/iridiumskyblock/api/IslandCreateEvent.java b/src/main/java/com/iridium/iridiumskyblock/api/IslandCreateEvent.java new file mode 100644 index 000000000..c1d0a0fd9 --- /dev/null +++ b/src/main/java/com/iridium/iridiumskyblock/api/IslandCreateEvent.java @@ -0,0 +1,78 @@ +package com.iridium.iridiumskyblock.api; + +import com.iridium.iridiumskyblock.configs.Schematics; +import com.iridium.iridiumskyblock.database.User; +import lombok.Getter; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@Getter + +public class IslandCreateEvent extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + @Nullable + private String islandName; + @NotNull + private final User user; + @NotNull + private Schematics.SchematicConfig schematicConfig; + + public IslandCreateEvent(@NotNull User user, @Nullable String islandName, Schematics.@NotNull SchematicConfig schematicConfig) { + this.islandName = islandName; + this.user = user; + this.schematicConfig = schematicConfig; + } + + @NotNull + public HandlerList getHandlers() { + return handlers; + } + + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + /** + * The name of the Island.
+ * null indicates that the name of the Player is used as the Island name + * because it hasn't been set. + * + * @return the name of the Island or null + */ + @Nullable + public String getIslandName() { + return islandName; + } + + /** + * + * Sets the name of the Island.
+ * set it to null to default to the player's name + * + * @param islandName The name of the island + */ + public void setIslandName(@Nullable String islandName) { + this.islandName = islandName; + } + + /** + * Sets the schematic of the new island + * + * @param schematicConfig The schematic configuration of the island + */ + + public void setSchematicConfig(Schematics.@NotNull SchematicConfig schematicConfig) { + this.schematicConfig = schematicConfig; + } +} diff --git a/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java b/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java index 4ffd0c627..fdedd1ce4 100644 --- a/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java +++ b/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java @@ -2,6 +2,7 @@ import com.iridium.iridiumcore.utils.StringUtils; import com.iridium.iridiumskyblock.IridiumSkyblock; +import com.iridium.iridiumskyblock.configs.Schematics; import com.iridium.iridiumskyblock.database.Island; import com.iridium.iridiumskyblock.database.User; import com.iridium.iridiumskyblock.gui.RegenGUI; @@ -34,16 +35,18 @@ public void execute(User user, Island island, String[] args, IridiumTeams schematicConfig = IridiumSkyblock.getInstance().getSchematics().schematics.keySet().stream() + Optional schematic = IridiumSkyblock.getInstance().getSchematics().schematics.keySet().stream() .filter(config -> config.equalsIgnoreCase(args[0])) .findFirst(); - if (!schematicConfig.isPresent()) { + if (!schematic.isPresent()) { player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().unknownSchematic .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) )); return; } + Schematics.SchematicConfig schematicConfig = IridiumSkyblock.getInstance().getSchematics().schematics.get(schematic.get()); + IridiumSkyblock.getInstance().getIslandManager().getTeamMembers(island).stream() .map(User::getPlayer) .filter(Objects::nonNull) @@ -51,7 +54,7 @@ public void execute(User user, Island island, String[] args, IridiumTeams Bukkit.getScheduler().runTask(IridiumSkyblock.getInstance(), () -> { + IridiumSkyblock.getInstance().getIslandManager().generateIsland(island, schematicConfig).thenRun(() -> Bukkit.getScheduler().runTask(IridiumSkyblock.getInstance(), () -> { if (IridiumSkyblock.getInstance().getTeamManager().teleport(player, island.getHome(), island)) { player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().teleportingHome .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) diff --git a/src/main/java/com/iridium/iridiumskyblock/managers/IslandManager.java b/src/main/java/com/iridium/iridiumskyblock/managers/IslandManager.java index 905c444ef..7acaea250 100644 --- a/src/main/java/com/iridium/iridiumskyblock/managers/IslandManager.java +++ b/src/main/java/com/iridium/iridiumskyblock/managers/IslandManager.java @@ -8,6 +8,7 @@ import com.iridium.iridiumcore.utils.Placeholder; import com.iridium.iridiumcore.utils.StringUtils; import com.iridium.iridiumskyblock.IridiumSkyblock; +import com.iridium.iridiumskyblock.api.IslandCreateEvent; import com.iridium.iridiumskyblock.configs.Schematics; import com.iridium.iridiumskyblock.database.Island; import com.iridium.iridiumskyblock.database.User; @@ -110,20 +111,25 @@ public CompletableFuture createTeam(@NotNull Player owner, String name) String schematic = schematicNameCompletableFuture.join(); if (schematic == null) return null; + User user = IridiumSkyblock.getInstance().getUserManager().getUser(owner); + Schematics.SchematicConfig schematicConfig = IridiumSkyblock.getInstance().getSchematics().schematics.get(schematic); + + IslandCreateEvent islandCreateEvent = new IslandCreateEvent(user, name, schematicConfig); + Bukkit.getPluginManager().callEvent(islandCreateEvent); + if (islandCreateEvent.isCancelled()) return null; + owner.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().creatingIsland .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) )); - User user = IridiumSkyblock.getInstance().getUserManager().getUser(owner); - Island island = new Island(name); - + Island island = new Island(islandCreateEvent.getIslandName()); IridiumSkyblock.getInstance().getDatabaseManager().registerIsland(island).join(); user.setTeam(island); user.setUserRank(Rank.OWNER.getId()); - generateIsland(island, schematic).join(); + generateIsland(island, islandCreateEvent.getSchematicConfig()).join(); Bukkit.getScheduler().runTask(IridiumSkyblock.getInstance(), () -> { teleport(owner, island.getHome(), island); IridiumSkyblock.getInstance().getNms().sendTitle(owner, IridiumSkyblock.getInstance().getConfiguration().islandCreateTitle, IridiumSkyblock.getInstance().getConfiguration().islandCreateSubTitle, 20, 40, 20); @@ -136,9 +142,8 @@ public CompletableFuture createTeam(@NotNull Player owner, String name) }); } - public CompletableFuture generateIsland(Island island, String schematic) { + public CompletableFuture generateIsland(Island island, Schematics.SchematicConfig schematicConfig) { return CompletableFuture.runAsync(() -> { - Schematics.SchematicConfig schematicConfig = IridiumSkyblock.getInstance().getSchematics().schematics.get(schematic); setHome(island, schematicConfig); deleteIslandBlocks(island).join(); IridiumSkyblock.getInstance().getSchematicManager().pasteSchematic(island, schematicConfig).join();