Arena system and some changes (not finished)

This commit is contained in:
Luca 2021-11-21 16:14:04 +01:00
parent 002b05820e
commit 1cca6eb41c
12 changed files with 635 additions and 2 deletions

View File

@ -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()));

View File

@ -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<GameTeam> teams;
public Game(GameTeam... teams) {
this.teams = Arrays.asList(teams);
}
}

View File

@ -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<UUID> allPlayers;
private final List<UUID> playingPlayers = new ArrayList<>();
private UUID leader;
private final PlayerTeam playerTeam;
public GameTeam(UUID leader, List<UUID> 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<Player> allPlayers() {
return this.allPlayers.stream().map(this.plugin.getServer()::getPlayer).filter(Objects::nonNull);
}
public Stream<Player> playingPlayers() {
return this.playingPlayers.stream().map(this.plugin.getServer()::getPlayer).filter(Objects::nonNull);
}
}

View File

@ -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<CopiedArena> copiedArenas;
private List<CopiedArena> 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);
}
}

View File

@ -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;
}

View File

@ -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<String, Arena> arenas = new HashMap<>();
@Getter private final Map<CopiedArena, UUID> 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");
});
}
}

View File

@ -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;

View File

@ -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();
}
}

View File

@ -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<String> getKeys() {
return getKeys(null);
}
public Set<String> 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<String> 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();
}
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}

View File

@ -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<String> getReversedStringList(String path) {
List<String> list = getStringList(path);
if (list != null) {
int size = list.size();
List<String> 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<String> getStringList(String path) {
if (configuration.contains(path)) {
ArrayList<String> 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<String> getStringListOrDefault(String path, List<String> toReturn) {
if (configuration.contains(path)) {
ArrayList<String> strings = new ArrayList<>();
for (String string : configuration.getStringList(path)) {
strings.add(ChatColor.translateAlternateColorCodes('&', string));
}
return strings;
}
return toReturn;
}
}