Progress on island stuff
This commit is contained in:
parent
a363382a0f
commit
93f38561b0
|
|
@ -53,6 +53,8 @@ public class CometSkyBlockPlugin extends JavaPlugin implements CometSkyBlockAPI
|
|||
islandManager = new IslandManager(this);
|
||||
playerManager = new PlayerManager(this);
|
||||
worldGenerator = new MasterWorldGenerator(this);
|
||||
|
||||
worldGenerator.checkMasterIslandWorld();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -17,8 +17,7 @@ public abstract class PlayerSubCommand extends SubCommand {
|
|||
sender.sendRichMessage(plugin.messagesConfiguration().get().commands().notAPlayer());
|
||||
return false;
|
||||
}
|
||||
// Todo - load islandplayerdata
|
||||
return execute(player, new IslandPlayer(), args);
|
||||
return execute(player, IslandPlayer.getIslandPlayer(player.getUniqueId()), args);
|
||||
}
|
||||
|
||||
protected abstract boolean execute(Player player, IslandPlayer islandPlayer, String... args);
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ public class ChallengeCommand extends PlayerSubCommand {
|
|||
super(plugin, "challenge");
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
// TODO - Challenges, ChallengesGUI and ChallengesCommand
|
||||
@Override
|
||||
public boolean execute(Player player, IslandPlayer islandPlayer, String[] args) {
|
||||
// open challenge inventory - not implemented yet -- TODO
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ public class IslandCommand extends PlayerSubCommand {
|
|||
registerSubCommand(new IslandLeave(plugin));
|
||||
registerSubCommand(new IslandMembers(plugin));
|
||||
registerSubCommand(new IslandLevel(plugin));
|
||||
registerSubCommand(new IslandTop(plugin));
|
||||
// registerSubCommand(new IslandTop(plugin));
|
||||
registerSubCommand(new IslandOptions(plugin));
|
||||
registerSubCommand(new IslandConfirm(plugin));
|
||||
registerSubCommand(new IslandInvite(plugin));
|
||||
|
|
@ -47,7 +47,7 @@ public class IslandCommand extends PlayerSubCommand {
|
|||
"<gold>/island level: </gold>" + help.level() + "<newline>" +
|
||||
"<gold>/island members: </gold>" + help.members() + "<newline>" +
|
||||
"<gold>/island options: </gold>" + help.options() + "<newline>" +
|
||||
"<gold>/island top: </gold>" + help.top() + "<newline>" +
|
||||
// "<gold>/island top: </gold>" + help.top() + "<newline>" +
|
||||
"<gold>/island visit: </gold>" + help.visit() + "<newline>" +
|
||||
"<gold>/island confirm: </gold>" + help.confirm() + "<newline>" +
|
||||
"<gold>/island challenges: </gold>" + help.challenges()
|
||||
|
|
|
|||
|
|
@ -19,16 +19,42 @@ public class IslandGo extends PlayerSubCommand {
|
|||
|
||||
@Override
|
||||
public boolean execute(Player player, IslandPlayer islandPlayer, String[] args) {
|
||||
if (islandPlayer.islandId() != 0) {
|
||||
Island island = Island.getIsland(islandPlayer.islandUUID());
|
||||
if (island == null) {
|
||||
player.sendRichMessage("<red>Could not load your island. Contact an administrator");
|
||||
return true;
|
||||
}
|
||||
World islandWorld = plugin.worldGenerator().loadIslandWorld(island.worldName());
|
||||
if (islandWorld == null) {
|
||||
player.sendRichMessage("<red>Could not load your islandWorld. Contact an administrator");
|
||||
return true;
|
||||
}
|
||||
player.teleportAsync(islandWorld.getSpawnLocation());
|
||||
return true;
|
||||
}
|
||||
player.sendRichMessage("Generating a new island...");
|
||||
plugin.worldGenerator().createNewIslandWorld(result -> {
|
||||
World world = Bukkit.getWorld(result);
|
||||
if (world == null) {
|
||||
plugin.getLogger().warning("Failed to create world " + result);
|
||||
return;
|
||||
}
|
||||
|
||||
Island.IslandBuilder islandBuilder = new Island.IslandBuilder(world.getUID(), world.getName());
|
||||
plugin.islandManager().addIsland(player.getWorld().getUID(), islandBuilder.build());
|
||||
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(result.substring(result.lastIndexOf('-') + 1).trim());
|
||||
} catch (NumberFormatException exception) {
|
||||
id = plugin.islandManager().getLastID();
|
||||
plugin.getLogger().warning("Caught a NumberFormatException while generating island for player " + player.getUniqueId());
|
||||
}
|
||||
// TODO - builder patterns?
|
||||
Island island = Island.getIsland(world.getUID());
|
||||
island.owner(player.getUniqueId());
|
||||
island.worldName(world.getName());
|
||||
island.level(0);
|
||||
islandPlayer.islandOwner(true);
|
||||
islandPlayer.islandUUID(island.islandUUID());
|
||||
islandPlayer.islandId(id);
|
||||
player.teleportAsync(world.getSpawnLocation());
|
||||
});
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@ package com.alttd.cometskyblock.commands.island;
|
|||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.commands.PlayerSubCommand;
|
||||
import com.alttd.cometskyblock.island.Island;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class IslandSetOwner extends PlayerSubCommand {
|
||||
|
|
@ -10,9 +13,38 @@ public class IslandSetOwner extends PlayerSubCommand {
|
|||
public IslandSetOwner(CometSkyBlockPlugin plugin) {
|
||||
super(plugin, "setowner");
|
||||
}
|
||||
|
||||
// TODO - complete IslandSetOwnerCommand
|
||||
@Override
|
||||
public boolean execute(Player player, IslandPlayer islandPlayer, String[] args) {
|
||||
if (args.length < 1) {
|
||||
// Wrong usage
|
||||
return true;
|
||||
}
|
||||
if (!islandPlayer.islandOwner()) {
|
||||
// You must be island owner to do this command.
|
||||
return true;
|
||||
}
|
||||
Player target = Bukkit.getPlayer(args[0]);
|
||||
if (target == null) {
|
||||
// Target is not online
|
||||
return true;
|
||||
}
|
||||
if (target == player) {
|
||||
// You are already the island owner.
|
||||
return true;
|
||||
}
|
||||
Island island = Island.getIsland(islandPlayer.islandUUID());
|
||||
if (island == null) {
|
||||
// Could not load island, contact administrator
|
||||
return true;
|
||||
}
|
||||
// if (!island.members().contains(target.getUniqueId())) {
|
||||
// // Target must be a member of your island
|
||||
// return true;
|
||||
// }
|
||||
island.owner(target.getUniqueId());
|
||||
islandPlayer.islandOwner(false);
|
||||
IslandPlayer.getIslandPlayer(target.getUniqueId()).islandOwner(true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,23 @@ public class IslandSethome extends PlayerSubCommand {
|
|||
public IslandSethome(CometSkyBlockPlugin plugin) {
|
||||
super(plugin, "sethome");
|
||||
}
|
||||
|
||||
// TODO -- Finish sethome command
|
||||
@Override
|
||||
public boolean execute(Player player, IslandPlayer islandPlayer, String[] args) {
|
||||
MessageConfiguration.Commands.Island.SetHome setHome = plugin.messagesConfiguration().get().commands().island().setHome();
|
||||
|
||||
if (!islandPlayer.islandOwner()) {
|
||||
// You must be island owner to do this command.
|
||||
return true;
|
||||
}
|
||||
if (!player.getWorld().getUID().equals(islandPlayer.islandUUID())) {
|
||||
// You must be on your island to use this command.
|
||||
return true;
|
||||
}
|
||||
if (player.getFallDistance() > 0 || player.isFlying()) {
|
||||
// You can't use this command when falling
|
||||
return true;
|
||||
}
|
||||
player.getWorld().setSpawnLocation(player.getLocation());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ public class IslandTop extends PlayerSubCommand {
|
|||
public IslandTop(CometSkyBlockPlugin plugin) {
|
||||
super(plugin, "top");
|
||||
}
|
||||
|
||||
// TODO - Finish TOP command
|
||||
@Override
|
||||
public boolean execute(Player player, IslandPlayer islandPlayer, String[] args) {
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,11 @@ package com.alttd.cometskyblock.commands.island;
|
|||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.commands.PlayerSubCommand;
|
||||
import com.alttd.cometskyblock.island.Island;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class IslandVisit extends PlayerSubCommand {
|
||||
|
|
@ -11,8 +15,42 @@ public class IslandVisit extends PlayerSubCommand {
|
|||
super(plugin, "visit");
|
||||
}
|
||||
|
||||
// TODO -- finish IslandVisitCommand and Request
|
||||
@Override
|
||||
public boolean execute(Player player, IslandPlayer islandPlayer, String[] args) {
|
||||
if (args.length < 1) {
|
||||
// send usage
|
||||
return true;
|
||||
}
|
||||
Player target = Bukkit.getPlayer(args[0]);
|
||||
if (player == target) {
|
||||
// can't visit self
|
||||
return true;
|
||||
}
|
||||
if (target == null) {
|
||||
// target offline
|
||||
return true;
|
||||
}
|
||||
IslandPlayer islandPlayer1 = IslandPlayer.getIslandPlayer(target.getUniqueId());
|
||||
if (islandPlayer1.islandId() == 0) {
|
||||
// target does not have an island
|
||||
return true;
|
||||
}
|
||||
Island island = Island.getIsland(islandPlayer1.islandUUID());
|
||||
if (island == null) {
|
||||
// failed to load island
|
||||
return true;
|
||||
}
|
||||
if (island.visitNeedsRequest()) {
|
||||
// send request
|
||||
return true;
|
||||
}
|
||||
World islandWorld = plugin.worldGenerator().loadIslandWorld(island.worldName());
|
||||
if (islandWorld == null) {
|
||||
// could not load world
|
||||
return true;
|
||||
}
|
||||
player.teleportAsync(islandWorld.getSpawnLocation());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.alttd.cometskyblock.configuration;
|
||||
|
||||
import com.alttd.cometskyblock.island.Island;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import lombok.Getter;
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
|
||||
|
|
@ -12,10 +13,10 @@ import java.util.UUID;
|
|||
@SuppressWarnings({"CanBeFinal", "FieldMayBeFinal"})
|
||||
public class IslandConfiguration implements Configuration {
|
||||
|
||||
private int id;
|
||||
@Getter private Map<UUID, Island> islands;
|
||||
@Getter int id;
|
||||
|
||||
public int getAndIncrementId() {
|
||||
return id = id + 1;
|
||||
id++;
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,77 +1,153 @@
|
|||
package com.alttd.cometskyblock.island;
|
||||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ConfigSerializable
|
||||
@Getter
|
||||
public class Island {
|
||||
public class Island extends YamlConfiguration {
|
||||
|
||||
protected UUID worldId;
|
||||
protected String worldName;
|
||||
protected String islandName;
|
||||
@Setter protected int level;
|
||||
@Setter protected UUID owner;
|
||||
protected final List<UUID> members = new ArrayList<>();
|
||||
private static final Map<UUID, Island> configs = new HashMap<>();
|
||||
public static final UUID NILL_UUID = new UUID(0L, 0L);
|
||||
|
||||
public static Island getIsland(UUID uuid) {
|
||||
synchronized (configs) {
|
||||
return configs.computeIfAbsent(uuid, k -> new Island(uuid));
|
||||
}
|
||||
}
|
||||
|
||||
public static void remove(UUID uuid) {
|
||||
synchronized (configs) {
|
||||
configs.remove(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeAll() {
|
||||
synchronized (configs) {
|
||||
configs.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private final File file;
|
||||
private final Object saveLock = new Object();
|
||||
@Getter private final UUID islandUUID;
|
||||
|
||||
private Island(UUID uuid) {
|
||||
super();
|
||||
this.islandUUID = uuid;
|
||||
this.file = new File(CometSkyBlockPlugin.instance().getDataFolder(), "IslandData" + File.separator + uuid + ".yml");
|
||||
reload();
|
||||
}
|
||||
|
||||
private void reload() {
|
||||
synchronized (saveLock) {
|
||||
try {
|
||||
load(file);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void save() {
|
||||
synchronized (saveLock) {
|
||||
try {
|
||||
save(file);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int islandId() {
|
||||
return getInt("island.id", 0);
|
||||
}
|
||||
|
||||
public void islandId(int id) {
|
||||
set("island.id", id);
|
||||
save();
|
||||
}
|
||||
|
||||
public String worldName() {
|
||||
return getString("island.worldname");
|
||||
}
|
||||
|
||||
public void worldName(String worldName) {
|
||||
set("island.worldname", worldName);
|
||||
save();
|
||||
}
|
||||
|
||||
public String islandName() {
|
||||
return getString("island.islandname");
|
||||
}
|
||||
|
||||
public void islandName(String islandName) {
|
||||
set("island.islandname", islandName);
|
||||
save();
|
||||
}
|
||||
|
||||
public int level() {
|
||||
return getInt("island.id", 0);
|
||||
}
|
||||
|
||||
public void level(int id) {
|
||||
set("island.level", id);
|
||||
save();
|
||||
}
|
||||
|
||||
public UUID owner() {
|
||||
String string = getString("island.owner");
|
||||
if (string == null || string.isEmpty())
|
||||
return NILL_UUID;
|
||||
|
||||
return UUID.fromString(string);
|
||||
}
|
||||
|
||||
public void owner(UUID uuid) {
|
||||
set("island.owner", uuid.toString());
|
||||
save();
|
||||
}
|
||||
|
||||
public List<UUID> members() {
|
||||
List<String> members = getStringList("island.members");
|
||||
return members.stream()
|
||||
.map(this::stringToUUID)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void members(List<UUID> members) {
|
||||
set("island.members", members.stream()
|
||||
.map(uuid -> uuid == null ? "null" : uuid.toString())
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
private String uuidToString(UUID uuid) {
|
||||
return (uuid == null ? NILL_UUID : uuid).toString();
|
||||
}
|
||||
|
||||
private UUID stringToUUID(String s) {
|
||||
return s == null || s.isEmpty() ? NILL_UUID : UUID.fromString(s);
|
||||
}
|
||||
|
||||
public boolean canBuild(UUID uuid) {
|
||||
return owner.equals(uuid) || members.contains(uuid);
|
||||
return owner().equals(uuid) || members().contains(uuid);
|
||||
}
|
||||
|
||||
public boolean addMember(UUID uuid) {
|
||||
return this.members.add(uuid);
|
||||
public void addMember(UUID uuid) {
|
||||
List<UUID> list = members();
|
||||
list.add(uuid);
|
||||
members(list);
|
||||
}
|
||||
|
||||
public boolean removeMember(UUID uuid) {
|
||||
return this.members.remove(uuid);
|
||||
public void removeMember(UUID uuid) {
|
||||
List<UUID> list = members();
|
||||
list.remove(uuid);
|
||||
members(list);
|
||||
}
|
||||
|
||||
public static class IslandBuilder extends Island {
|
||||
private final UUID worldUUID;
|
||||
private final String worldName;
|
||||
private String islandName;
|
||||
private int level;
|
||||
private UUID owner;
|
||||
private final List<UUID> members = new ArrayList<>();
|
||||
|
||||
public IslandBuilder(UUID worldUUID, String worldName) {
|
||||
this.worldUUID = worldUUID;
|
||||
this.worldName = worldName;
|
||||
}
|
||||
|
||||
public IslandBuilder islandName(String islandName) {
|
||||
this.islandName = islandName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IslandBuilder level(int level) {
|
||||
this.level = level;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IslandBuilder owner(UUID owner) {
|
||||
this.owner = owner;
|
||||
return this;
|
||||
}
|
||||
public IslandBuilder members(List<UUID> members) {
|
||||
this.members.addAll(members);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Island build() {
|
||||
Island island = new Island();
|
||||
island.worldId = this.worldUUID;
|
||||
island.worldName = this.worldName;
|
||||
island.islandName = this.islandName;
|
||||
island.level = this.level;
|
||||
island.owner = this.owner;
|
||||
island.members.addAll(this.members);
|
||||
return island;
|
||||
}
|
||||
// TODO - Island settings
|
||||
public boolean visitNeedsRequest() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,91 @@
|
|||
package com.alttd.cometskyblock.island;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@ConfigSerializable
|
||||
public class IslandPlayer {
|
||||
private UUID playerId;
|
||||
private final int islandId = 0;
|
||||
private final boolean isOwner = false;
|
||||
public class IslandPlayer extends YamlConfiguration {
|
||||
|
||||
public Player getPlayer() {
|
||||
return playerId != null ? Bukkit.getPlayer(playerId) : null;
|
||||
private static final Map<UUID, IslandPlayer> configs = new HashMap<>();
|
||||
|
||||
public static IslandPlayer getIslandPlayer(UUID uuid) {
|
||||
synchronized (configs) {
|
||||
return configs.computeIfAbsent(uuid, k -> new IslandPlayer(uuid));
|
||||
}
|
||||
}
|
||||
|
||||
public static void remove(UUID uuid) {
|
||||
synchronized (configs) {
|
||||
configs.remove(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeAll() {
|
||||
synchronized (configs) {
|
||||
configs.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private final File file;
|
||||
private final Object saveLock = new Object();
|
||||
|
||||
private IslandPlayer(UUID uuid) {
|
||||
super();
|
||||
this.file = new File(CometSkyBlockPlugin.instance().getDataFolder(), "PlayerData" + File.separator + uuid + ".yml");
|
||||
reload();
|
||||
}
|
||||
|
||||
private void reload() {
|
||||
synchronized (saveLock) {
|
||||
try {
|
||||
load(file);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void save() {
|
||||
synchronized (saveLock) {
|
||||
try {
|
||||
save(file);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public UUID islandUUID() {
|
||||
String string = getString("island.uuid");
|
||||
if (string == null || string.isEmpty())
|
||||
return Island.NILL_UUID;
|
||||
|
||||
return UUID.fromString(string);
|
||||
}
|
||||
|
||||
public void islandUUID(UUID uuid) {
|
||||
set("island.uuid", uuid.toString());
|
||||
save();
|
||||
}
|
||||
|
||||
public int islandId() {
|
||||
return getInt("island.id", 0);
|
||||
}
|
||||
|
||||
public void islandId(int id) {
|
||||
set("island.id", id);
|
||||
save();
|
||||
}
|
||||
|
||||
public boolean islandOwner() {
|
||||
return getBoolean("island.is-owner", false);
|
||||
}
|
||||
|
||||
public void islandOwner(boolean owner) {
|
||||
set("island.is-owner", owner);
|
||||
save();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.alttd.cometskyblock.listeners;
|
|||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.island.Island;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import com.destroystokyo.paper.MaterialTags;
|
||||
import org.bukkit.GameRule;
|
||||
import org.bukkit.World;
|
||||
|
|
@ -21,14 +22,20 @@ public class BedListener implements Listener {
|
|||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onBedPlace(BlockPlaceEvent event) {
|
||||
if (!MaterialTags.BEDS.isTagged(event.getBlockPlaced())) {
|
||||
return;
|
||||
}
|
||||
Player player = event.getPlayer();
|
||||
World world = player.getWorld();
|
||||
Island island = plugin.islandManager().getIsland(world);
|
||||
IslandPlayer islandPlayer = IslandPlayer.getIslandPlayer(player.getUniqueId());
|
||||
if (!world.getUID().equals(islandPlayer.islandUUID())) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
Island island = Island.getIsland(world.getUID());
|
||||
if (island == null) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,45 +3,31 @@ package com.alttd.cometskyblock.managers;
|
|||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.configuration.ConfigurationContainer;
|
||||
import com.alttd.cometskyblock.configuration.IslandConfiguration;
|
||||
import com.alttd.cometskyblock.island.Island;
|
||||
import org.bukkit.World;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
public class IslandManager {
|
||||
|
||||
private final CometSkyBlockPlugin plugin;
|
||||
|
||||
ConfigurationContainer<IslandConfiguration> islands;
|
||||
// TODO - move all storage data to another system - SQL / flatfile
|
||||
// This loader is not made for constant saves and loads
|
||||
ConfigurationContainer<IslandConfiguration> islandData;
|
||||
|
||||
public IslandManager(CometSkyBlockPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
|
||||
Path path = new File(plugin.getDataFolder(), "IslandData").toPath();
|
||||
islands = ConfigurationContainer.load(plugin.getSLF4JLogger(), path, IslandConfiguration.class, "islands");
|
||||
}
|
||||
|
||||
public Island getIsland(World world) {
|
||||
return getIsland(world.getUID());
|
||||
}
|
||||
|
||||
public Island getIsland(UUID uuid) {
|
||||
return islands.get().islands().get(uuid);
|
||||
}
|
||||
|
||||
public void addIsland(UUID uuid, Island island) {
|
||||
islands.get().islands().put(uuid, island);
|
||||
islands.save();
|
||||
islandData = ConfigurationContainer.load(plugin.getSLF4JLogger(), plugin.getDataFolder().toPath(), IslandConfiguration.class, "islands");
|
||||
}
|
||||
|
||||
public int getNextId() {
|
||||
int id = islands.get().getAndIncrementId();
|
||||
islands.save();
|
||||
int id = islandData.get().getAndIncrementId();
|
||||
islandData.save();
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getLastID() {
|
||||
return islandData.get().id();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ public class IslandGenerator extends BlockPopulator {
|
|||
|
||||
// try to add a tree
|
||||
if (limitedRegion.isInRegion(startX + treeX, startY + 1, startX + treeZ)) {
|
||||
Location location = new Location(null, startX + treeX, startY + 1, startX + treeZ);
|
||||
Location location = new Location(null, startX + treeX, limitedRegion.getHighestBlockYAt(startX + treeX, startX + treeZ), startX + treeZ);
|
||||
// Force air above to clear any flora preventing tree from growing
|
||||
limitedRegion.setType(location.clone().add(0, +1, 0), Material.AIR);
|
||||
// always set a dirt block under the tree location
|
||||
|
|
|
|||
|
|
@ -5,6 +5,11 @@ import com.alttd.cometskyblock.configuration.PluginConfiguration;
|
|||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.WorldCreator;
|
||||
import org.bukkit.WorldType;
|
||||
import org.codehaus.plexus.util.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This is the main island that will be used to create all the following islands for players.
|
||||
|
|
@ -13,32 +18,30 @@ public class MasterWorldGenerator {
|
|||
|
||||
CometSkyBlockPlugin plugin;
|
||||
public static final String WORLD_NAME = "CometSkyBlockWorld-<id>";
|
||||
public static final String MASTER_WORLD_NAME = "CometSkyBlockMasterWorld";
|
||||
|
||||
public MasterWorldGenerator(CometSkyBlockPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void checkIslandWorld() {
|
||||
if (plugin.getServer().getWorld(WORLD_NAME) == null) {
|
||||
createNewIslandWorld(result -> {
|
||||
plugin.getLogger().info(WORLD_NAME + " has been created");
|
||||
});
|
||||
public void checkMasterIslandWorld() {
|
||||
if (plugin.getServer().getWorld(MASTER_WORLD_NAME) == null) {
|
||||
createMasterIslandWorld(result ->
|
||||
plugin.getLogger().info(MASTER_WORLD_NAME + " has been created")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public void createNewIslandWorld(Callback callback) {
|
||||
final int id = plugin.islandManager().getNextId();
|
||||
public void createMasterIslandWorld(Callback callback) {
|
||||
plugin.getServer().getScheduler().runTask(plugin, () -> {
|
||||
WorldCreator worldCreator = new WorldCreator(WORLD_NAME.replace("<id>",
|
||||
id + ""
|
||||
));
|
||||
worldCreator.generator(new CometIslandGenerator());
|
||||
WorldCreator worldCreator = new WorldCreator(MASTER_WORLD_NAME);
|
||||
worldCreator.generator(new CometVoidGenerator());
|
||||
worldCreator.environment(World.Environment.NORMAL);
|
||||
worldCreator.generateStructures(true);
|
||||
|
||||
World world = worldCreator.createWorld();
|
||||
if (world == null) {
|
||||
plugin.getLogger().warning("Could not create CometSkyBlock MasterIslandWorld! Does the world(" + WORLD_NAME + ") already exist?");
|
||||
plugin.getLogger().warning("Could not create CometSkyBlock MasterIslandWorld! Does the world(" + MASTER_WORLD_NAME + ") already exist?");
|
||||
return;
|
||||
}
|
||||
PluginConfiguration pluginConfiguration = plugin.pluginConfiguration().get();
|
||||
|
|
@ -47,12 +50,76 @@ public class MasterWorldGenerator {
|
|||
world.setAutoSave(true);
|
||||
world.setSpawnLocation(pluginConfiguration.worldGenerator().spawnX(), pluginConfiguration.worldGenerator().spawnY(), pluginConfiguration.worldGenerator().spawnz()); // TODO
|
||||
|
||||
Bukkit.getScheduler().runTask(plugin, () -> callback.onQueryDone(MASTER_WORLD_NAME));
|
||||
});
|
||||
}
|
||||
|
||||
public void createNewIslandWorld(Callback callback) {
|
||||
final int id = plugin.islandManager().getNextId();
|
||||
plugin.getServer().getScheduler().runTask(plugin, () -> {
|
||||
String worldName = WORLD_NAME.replace("<id>", id + "");
|
||||
File dir = new File(plugin.getServer().getWorldContainer().getAbsolutePath(), worldName);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
|
||||
File source = new File(plugin.getServer().getWorldContainer().getAbsolutePath(), MASTER_WORLD_NAME);
|
||||
|
||||
try {
|
||||
FileUtils.copyDirectory(source, dir);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
File[] files = dir.listFiles();
|
||||
for (File file : files) {
|
||||
if (file.getName().equalsIgnoreCase("uid.dat") || file.getName().equalsIgnoreCase("session.lock")) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
WorldCreator worldCreator = new WorldCreator(worldName);
|
||||
worldCreator.generator(new CometIslandGenerator());
|
||||
worldCreator.environment(World.Environment.NORMAL);
|
||||
worldCreator.generateStructures(true);
|
||||
World newIsland = worldCreator.createWorld();
|
||||
if (newIsland == null) {
|
||||
return;
|
||||
}
|
||||
// TODO Load a schematic into this world?
|
||||
// Currently random islands are generated by CometIslandGenerator()
|
||||
plugin.getServer().getWorlds().add(newIsland);
|
||||
PluginConfiguration pluginConfiguration = plugin.pluginConfiguration().get();
|
||||
newIsland.getWorldBorder().setCenter(pluginConfiguration.worldGenerator().centerX(), pluginConfiguration.worldGenerator().centerZ());
|
||||
newIsland.getWorldBorder().setSize(pluginConfiguration.worldGenerator().size());
|
||||
newIsland.setAutoSave(true);
|
||||
newIsland.setSpawnLocation(pluginConfiguration.worldGenerator().spawnX(), pluginConfiguration.worldGenerator().spawnY(), pluginConfiguration.worldGenerator().spawnz()); // TODO
|
||||
} else {
|
||||
plugin.getLogger().severe("Folder already exists!");
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().runTask(plugin, () -> callback.onQueryDone(WORLD_NAME.replace("<id>", id + "")));
|
||||
});
|
||||
}
|
||||
|
||||
public World loadIslandWorld(String islandName) {
|
||||
World islandWorld = Bukkit.getWorld(islandName);
|
||||
if (islandWorld != null)
|
||||
return islandWorld;
|
||||
|
||||
WorldCreator worldCreator = new WorldCreator(islandName);
|
||||
worldCreator.generator(new CometIslandGenerator());
|
||||
worldCreator.environment(World.Environment.NORMAL);
|
||||
worldCreator.generateStructures(true);
|
||||
World newIsland = worldCreator.createWorld();
|
||||
if (newIsland == null) {
|
||||
plugin.getLogger().warning("Failed to load world " + islandName);
|
||||
return null;
|
||||
}
|
||||
|
||||
return newIsland;
|
||||
}
|
||||
|
||||
public interface Callback {
|
||||
void onQueryDone(String result);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user