diff --git a/src/main/java/rip/tilly/bedwars/BedWars.java b/src/main/java/rip/tilly/bedwars/BedWars.java index d351093..04acf41 100644 --- a/src/main/java/rip/tilly/bedwars/BedWars.java +++ b/src/main/java/rip/tilly/bedwars/BedWars.java @@ -15,6 +15,7 @@ import rip.tilly.bedwars.mongo.MongoManager; import rip.tilly.bedwars.player.PlayerDataHandler; import rip.tilly.bedwars.player.PlayerDataManager; import rip.tilly.bedwars.utils.CC; +import rip.tilly.bedwars.utils.config.file.Config; import java.util.Arrays; @@ -27,6 +28,8 @@ public final class BedWars extends JavaPlugin { @Getter private static BedWars instance; + private Config mainConfig, arenasConfig; + private MongoManager mongoManager; private PlayerDataManager playerDataManager; @@ -34,7 +37,8 @@ public final class BedWars extends JavaPlugin { public void onEnable() { instance = this; - this.saveDefaultConfig(); + this.mainConfig = new Config("config", this); + this.arenasConfig = new Config("arenas", this); Bukkit.getConsoleSender().sendMessage("------------------------------------------------"); Bukkit.getConsoleSender().sendMessage(CC.translate("&dBedWars &8- &av" + getDescription().getVersion())); diff --git a/src/main/java/rip/tilly/bedwars/game/Game.java b/src/main/java/rip/tilly/bedwars/game/Game.java new file mode 100644 index 0000000..37f4d6c --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/game/Game.java @@ -0,0 +1,24 @@ +package rip.tilly.bedwars.game; + +import lombok.Getter; +import lombok.Setter; + +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +/** + * Created by Lucanius + * Project: BedWars + */ +@Getter +@Setter +public class Game { + + private final UUID gameId = UUID.randomUUID(); + private final List teams; + + public Game(GameTeam... teams) { + this.teams = Arrays.asList(teams); + } +} diff --git a/src/main/java/rip/tilly/bedwars/game/GameTeam.java b/src/main/java/rip/tilly/bedwars/game/GameTeam.java new file mode 100644 index 0000000..5ee671d --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/game/GameTeam.java @@ -0,0 +1,50 @@ +package rip.tilly.bedwars.game; + +import lombok.Getter; +import lombok.Setter; +import org.bukkit.entity.Player; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.player.PlayerTeam; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.stream.Stream; + +/** + * Created by Lucanius + * Project: BedWars + */ +@Getter +@Setter +public class GameTeam { + + protected final BedWars plugin = BedWars.getInstance(); + + private final List allPlayers; + private final List playingPlayers = new ArrayList<>(); + private UUID leader; + + private final PlayerTeam playerTeam; + + public GameTeam(UUID leader, List allPlayers, PlayerTeam playerTeam) { + this.leader = leader; + this.allPlayers = allPlayers; + this.playingPlayers.addAll(allPlayers); + + this.playerTeam = playerTeam; + } + + public void removePlayer(UUID uuid) { + this.playingPlayers.remove(uuid); + } + + public Stream allPlayers() { + return this.allPlayers.stream().map(this.plugin.getServer()::getPlayer).filter(Objects::nonNull); + } + + public Stream playingPlayers() { + return this.playingPlayers.stream().map(this.plugin.getServer()::getPlayer).filter(Objects::nonNull); + } +} diff --git a/src/main/java/rip/tilly/bedwars/game/arena/Arena.java b/src/main/java/rip/tilly/bedwars/game/arena/Arena.java new file mode 100644 index 0000000..d8639e1 --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/game/arena/Arena.java @@ -0,0 +1,60 @@ +package rip.tilly.bedwars.game.arena; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import rip.tilly.bedwars.utils.CustomLocation; + +import java.util.List; + +@Getter +@Setter +@AllArgsConstructor +@RequiredArgsConstructor +/** + * Created by Lucanius + * Project: BedWars + */ +public class Arena { + + private final String name; + + private List copiedArenas; + private List availableArenas; + + private String icon; + private int iconData; + + private CustomLocation a; + private CustomLocation b; + + private CustomLocation min; + private CustomLocation max; + + private CustomLocation teamAmin; + private CustomLocation teamAmax; + + private CustomLocation teamBmin; + private CustomLocation teamBmax; + + private int deadZone; + private int buildMax; + + private boolean enabled; + + public CopiedArena getAvailableArena() { + CopiedArena copiedArena = this.availableArenas.get(0); + this.availableArenas.remove(0); + + return copiedArena; + } + + public void addCopiedArena(CopiedArena copiedArena) { + this.copiedArenas.add(copiedArena); + } + + public void addAvailableArena(CopiedArena copiedArena) { + this.availableArenas.add(copiedArena); + } +} diff --git a/src/main/java/rip/tilly/bedwars/game/arena/CopiedArena.java b/src/main/java/rip/tilly/bedwars/game/arena/CopiedArena.java new file mode 100644 index 0000000..38f3f8d --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/game/arena/CopiedArena.java @@ -0,0 +1,30 @@ +package rip.tilly.bedwars.game.arena; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import rip.tilly.bedwars.utils.CustomLocation; + +@Getter +@Setter +@AllArgsConstructor +@RequiredArgsConstructor +/** + * Created by Lucanius + * Project: BedWars + */ +public class CopiedArena { + + private CustomLocation a; + private CustomLocation b; + + private CustomLocation min; + private CustomLocation max; + + private CustomLocation teamAmin; + private CustomLocation teamAmax; + + private CustomLocation teamBmin; + private CustomLocation teamBmax; +} diff --git a/src/main/java/rip/tilly/bedwars/managers/ArenaManager.java b/src/main/java/rip/tilly/bedwars/managers/ArenaManager.java new file mode 100644 index 0000000..43303f2 --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/managers/ArenaManager.java @@ -0,0 +1,55 @@ +package rip.tilly.bedwars.managers; + +import lombok.Getter; +import lombok.Setter; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.game.arena.Arena; +import rip.tilly.bedwars.game.arena.CopiedArena; +import rip.tilly.bedwars.utils.config.file.Config; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class ArenaManager { + + private final BedWars plugin = BedWars.getInstance(); + private final Config config = this.plugin.getArenasConfig(); + + @Getter private final Map arenas = new HashMap<>(); + @Getter private final Map arenaMatchUUIDs = new HashMap<>(); + + @Getter @Setter private int generatingArenaRunnable; + + public ArenaManager() { + + } + + private void loadArenas() { + FileConfiguration fileConfig = config.getConfig(); + ConfigurationSection section = fileConfig.getConfigurationSection("arenas"); + if (section == null) { + return; + } + + section.getKeys(false).forEach(name -> { + String icon = section.getString(name + ".icon") == null ? Material.PAPER.name() : section.getString(name + ".icon"); + int iconData = section.getInt(name + ".icon-data"); + + String a = section.getString(name + ".a"); + String b = section.getString(name + ".b"); + String min = section.getString(name + ".min"); + String max = section.getString(name + ".max"); + String teamAmin = section.getString(name + ".teamAmin"); + String teamAmax = section.getString(name + ".teamAmax"); + String teamBmin = section.getString(name + ".teamBmin"); + String teamBmax = section.getString(name + ".teamBmax"); + + int deadZone = section.getInt(name + ".deadZone"); + int buildMax = section.getInt(name + ".buildMax"); + }); + } +} diff --git a/src/main/java/rip/tilly/bedwars/mongo/MongoManager.java b/src/main/java/rip/tilly/bedwars/mongo/MongoManager.java index aa741f8..fd8ce8a 100644 --- a/src/main/java/rip/tilly/bedwars/mongo/MongoManager.java +++ b/src/main/java/rip/tilly/bedwars/mongo/MongoManager.java @@ -9,8 +9,10 @@ import lombok.Getter; import org.bson.Document; import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; import rip.tilly.bedwars.BedWars; import rip.tilly.bedwars.utils.CC; +import rip.tilly.bedwars.utils.config.file.Config; import java.util.Collections; @@ -23,7 +25,10 @@ public class MongoManager { private final MongoManager instance; private final BedWars plugin = BedWars.getInstance(); - private final ConfigurationSection config = this.plugin.getConfig().getConfigurationSection("MONGO"); + + private final Config configFile = this.plugin.getMainConfig(); + private final FileConfiguration fileConfig = configFile.getConfig(); + private final ConfigurationSection config = fileConfig.getConfigurationSection("MONGO"); private MongoClient mongoClient; private MongoDatabase mongoDatabase; diff --git a/src/main/java/rip/tilly/bedwars/utils/CustomLocation.java b/src/main/java/rip/tilly/bedwars/utils/CustomLocation.java new file mode 100644 index 0000000..f5b6169 --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/utils/CustomLocation.java @@ -0,0 +1,125 @@ +package rip.tilly.bedwars.utils; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; + +import java.util.StringJoiner; + +@Getter +@Setter +@AllArgsConstructor +public class CustomLocation { + + private final long timestamp = System.currentTimeMillis(); + + private String world; + + private double x; + private double y; + private double z; + + private float yaw; + private float pitch; + + public CustomLocation(double x, double y, double z) { + this(x, y, z, 0.0F, 0.0F); + } + + public CustomLocation(String world, double x, double y, double z) { + this(world, x, y, z, 0.0F, 0.0F); + } + + public CustomLocation(double x, double y, double z, float yaw, float pitch) { + this("world", x, y, z, yaw, pitch); + } + + public static CustomLocation fromBukkitLocation(Location location) { + return new CustomLocation(location.getWorld().getName(), location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); + } + + public static CustomLocation stringToLocation(String string) { + String[] split = string.split(", "); + + double x = Double.parseDouble(split[0]); + double y = Double.parseDouble(split[1]); + double z = Double.parseDouble(split[2]); + + CustomLocation customLocation = new CustomLocation(x, y, z); + if (split.length == 4) { + customLocation.setWorld(split[3]); + } else if (split.length >= 5) { + customLocation.setYaw(Float.parseFloat(split[3])); + customLocation.setPitch(Float.parseFloat(split[4])); + if (split.length >= 6) { + customLocation.setWorld(split[5]); + } + } + + return customLocation; + } + + public static String locationToString(CustomLocation loc) { + StringJoiner joiner = new StringJoiner(", "); + joiner.add(Double.toString(loc.getX())); + joiner.add(Double.toString(loc.getY())); + joiner.add(Double.toString(loc.getZ())); + if (loc.getYaw() == 0.0f && loc.getPitch() == 0.0f) { + if (loc.getWorld().equals("world")) { + return joiner.toString(); + } else { + joiner.add(loc.getWorld()); + return joiner.toString(); + } + } else { + joiner.add(Float.toString(loc.getYaw())); + joiner.add(Float.toString(loc.getPitch())); + if (loc.getWorld().equals("world")) { + return joiner.toString(); + } else { + joiner.add(loc.getWorld()); + return joiner.toString(); + } + } + } + + public Location toBukkitLocation() { + return new Location(this.toBukkitWorld(), this.x, this.y, this.z, this.yaw, this.pitch); + } + + public World toBukkitWorld() { + if (this.world == null) { + return Bukkit.getServer().getWorlds().get(0); + } else { + return Bukkit.getServer().getWorld(this.world); + } + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof CustomLocation)) { + return false; + } + + CustomLocation location = (CustomLocation) obj; + + return location.x == this.x && location.y == this.y && location.z == this.z && location.pitch == this.pitch && location.yaw == this.yaw; + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("x", this.x) + .append("y", this.y) + .append("z", this.z) + .append("yaw", this.yaw) + .append("pitch", this.pitch) + .append("world", this.world) + .append("timestamp", this.timestamp) + .toString(); + } +} diff --git a/src/main/java/rip/tilly/bedwars/utils/config/ConfigCursor.java b/src/main/java/rip/tilly/bedwars/utils/config/ConfigCursor.java new file mode 100644 index 0000000..4c0f083 --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/utils/config/ConfigCursor.java @@ -0,0 +1,88 @@ +package rip.tilly.bedwars.utils.config; + +import org.bukkit.Bukkit; +import org.bukkit.World; + +import java.util.List; +import java.util.Set; +import java.util.UUID; + +public class ConfigCursor { + + private final FileConfig fileConfig; + + private String path; + + public ConfigCursor(FileConfig fileConfig, String path) { + this.fileConfig = fileConfig; + this.path = path; + } + + public FileConfig getFileConfig() { + return this.fileConfig; + } + + public String getPath() { + return this.path; + } + + public void setPath(String path) { + this.path = path; + } + + public boolean exists() { + return exists(null); + } + + public boolean exists(String path) { + return this.fileConfig.getConfig().contains(this.path + ((path == null) ? "" : ("." + path))); + } + + public Set getKeys() { + return getKeys(null); + } + + public Set getKeys(String path) { + return this.fileConfig.getConfig().getConfigurationSection(this.path + ((path == null) ? "" : ("." + path))).getKeys(false); + } + + public boolean getBoolean(String path) { + return this.fileConfig.getConfig().getBoolean(((this.path == null) ? "" : (this.path + ".")) + "." + path); + } + + public int getInt(String path) { + return this.fileConfig.getConfig().getInt(((this.path == null) ? "" : (this.path + ".")) + "." + path); + } + + public long getLong(String path) { + return this.fileConfig.getConfig().getLong(((this.path == null) ? "" : (this.path + ".")) + "." + path); + } + + public String getString(String path) { + return this.fileConfig.getConfig().getString(((this.path == null) ? "" : (this.path + ".")) + "." + path); + } + + public List getStringList(String path) { + return this.fileConfig.getConfig().getStringList(((this.path == null) ? "" : (this.path + ".")) + "." + path); + } + + public UUID getUuid(String path) { + return UUID.fromString(this.fileConfig.getConfig().getString(this.path + "." + path)); + } + + public World getWorld(String path) { + return Bukkit.getWorld(this.fileConfig.getConfig().getString(this.path + "." + path)); + } + + public void set(Object value) { + set(null, value); + } + + public void set(String path, Object value) { + this.fileConfig.getConfig().set(this.path + ((path == null) ? "" : ("." + path)), value); + } + + public void save() { + this.fileConfig.save(); + } +} diff --git a/src/main/java/rip/tilly/bedwars/utils/config/FileConfig.java b/src/main/java/rip/tilly/bedwars/utils/config/FileConfig.java new file mode 100644 index 0000000..ba74ae0 --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/utils/config/FileConfig.java @@ -0,0 +1,50 @@ +package rip.tilly.bedwars.utils.config; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.io.IOException; + +public class FileConfig { + + private File file; + + private FileConfiguration config; + + public File getFile() { + return this.file; + } + + public FileConfiguration getConfig() { + return this.config; + } + + public FileConfig(JavaPlugin plugin, String fileName) { + this.file = new File(plugin.getDataFolder(), fileName); + if (!this.file.exists()) { + this.file.getParentFile().mkdirs(); + if (plugin.getResource(fileName) == null) { + try { + this.file.createNewFile(); + } catch (IOException e) { + plugin.getLogger().severe("Failed to create new file " + fileName); + } + } else { + plugin.saveResource(fileName, false); + } + } + this.config = YamlConfiguration.loadConfiguration(this.file); + } + + public void save() { + try { + this.config.save(this.file); + } catch (IOException e) { + Bukkit.getLogger().severe("Could not save config file " + this.file.toString()); + e.printStackTrace(); + } + } +} diff --git a/src/main/java/rip/tilly/bedwars/utils/config/file/Config.java b/src/main/java/rip/tilly/bedwars/utils/config/file/Config.java new file mode 100644 index 0000000..d5656ff --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/utils/config/file/Config.java @@ -0,0 +1,39 @@ +package rip.tilly.bedwars.utils.config.file; + +import lombok.Getter; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.io.IOException; + +@Getter +public class Config { + + private final FileConfiguration config; + private final File configFile; + protected boolean wasCreated; + + public Config(String name, JavaPlugin plugin) { + this.configFile = new File(plugin.getDataFolder() + "/" + name + ".yml"); + if (!this.configFile.exists()) { + try { + this.configFile.getParentFile().mkdirs(); + this.configFile.createNewFile(); + this.wasCreated = true; + } catch (IOException e) { + e.printStackTrace(); + } + } + this.config = YamlConfiguration.loadConfiguration(this.configFile); + } + + public void save() { + try { + this.config.save(configFile); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/rip/tilly/bedwars/utils/config/file/ConfigFile.java b/src/main/java/rip/tilly/bedwars/utils/config/file/ConfigFile.java new file mode 100644 index 0000000..407a055 --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/utils/config/file/ConfigFile.java @@ -0,0 +1,103 @@ +package rip.tilly.bedwars.utils.config.file; + +import lombok.Getter; +import org.bukkit.ChatColor; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ConfigFile { + + @Getter private File file; + @Getter private YamlConfiguration configuration; + + public ConfigFile(JavaPlugin plugin, String name) { + file = new File(plugin.getDataFolder(), name + ".yml"); + + if (!file.getParentFile().exists()) { + file.getParentFile().mkdir(); + } + + plugin.saveResource(name + ".yml", false); + + configuration = YamlConfiguration.loadConfiguration(file); + } + + public double getDouble(String path) { + if (configuration.contains(path)) { + return configuration.getDouble(path); + } + return 0; + } + + public int getInt(String path) { + if (configuration.contains(path)) { + return configuration.getInt(path); + } + return 0; + } + + public boolean getBoolean(String path) { + if (configuration.contains(path)) { + return configuration.getBoolean(path); + } + return false; + } + + public String getString(String path) { + if (configuration.contains(path)) { + return ChatColor.translateAlternateColorCodes('&', configuration.getString(path)); + } + return "ERROR: STRING NOT FOUND"; + } + + public String getString(String path, String callback, boolean colorize) { + if (configuration.contains(path)) { + if (colorize) { + return ChatColor.translateAlternateColorCodes('&', configuration.getString(path)); + } else { + return configuration.getString(path); + } + } + return callback; + } + + public List getReversedStringList(String path) { + List list = getStringList(path); + if (list != null) { + int size = list.size(); + List toReturn = new ArrayList<>(); + for (int i = size - 1; i >= 0; i--) { + toReturn.add(list.get(i)); + } + return toReturn; + } + return Arrays.asList("ERROR: STRING LIST NOT FOUND!"); + } + + public List getStringList(String path) { + if (configuration.contains(path)) { + ArrayList strings = new ArrayList<>(); + for (String string : configuration.getStringList(path)) { + strings.add(ChatColor.translateAlternateColorCodes('&', string)); + } + return strings; + } + return Arrays.asList("ERROR: STRING LIST NOT FOUND!"); + } + + public List getStringListOrDefault(String path, List toReturn) { + if (configuration.contains(path)) { + ArrayList strings = new ArrayList<>(); + for (String string : configuration.getStringList(path)) { + strings.add(ChatColor.translateAlternateColorCodes('&', string)); + } + return strings; + } + return toReturn; + } +}