Skip to content

Commit

Permalink
Added IslandCreateEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachesMLG committed Sep 23, 2023
1 parent 7256ee7 commit c003a7d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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.<br>
* 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.<br>
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,24 +35,26 @@ public void execute(User user, Island island, String[] args, IridiumTeams<Island
return;
}

Optional<String> schematicConfig = IridiumSkyblock.getInstance().getSchematics().schematics.keySet().stream()
Optional<String> 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)
.forEach(member -> member.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().regeneratingIsland
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)
)));

IridiumSkyblock.getInstance().getIslandManager().generateIsland(island, schematicConfig.get()).thenRun(() -> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,20 +111,25 @@ public CompletableFuture<Island> 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);
Expand All @@ -136,9 +142,8 @@ public CompletableFuture<Island> createTeam(@NotNull Player owner, String name)
});
}

public CompletableFuture<Void> generateIsland(Island island, String schematic) {
public CompletableFuture<Void> 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();
Expand Down

0 comments on commit c003a7d

Please sign in to comment.