Add MongoDB Save Method, Renamed Listeners.java to GeneralListeners.java and basically finished the FFA Logic

This commit is contained in:
Matheus 2024-03-09 16:21:57 -04:00
parent 469cbcbea5
commit 02fef3ccce
7 changed files with 215 additions and 36 deletions

View File

@ -45,6 +45,12 @@
<version>4.7.0</version> <version>4.7.0</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.8.2</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>

View File

@ -1,5 +1,10 @@
package dev.lugami.otaku; package dev.lugami.otaku;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import com.qrakn.honcho.Honcho; import com.qrakn.honcho.Honcho;
import dev.lugami.otaku.board.Board; import dev.lugami.otaku.board.Board;
import dev.lugami.otaku.commands.*; import dev.lugami.otaku.commands.*;
@ -8,11 +13,12 @@ import dev.lugami.otaku.essentials.Essentials;
import dev.lugami.otaku.hotbar.Hotbar; import dev.lugami.otaku.hotbar.Hotbar;
import dev.lugami.otaku.kit.Kit; import dev.lugami.otaku.kit.Kit;
import dev.lugami.otaku.listener.FFAListener; import dev.lugami.otaku.listener.FFAListener;
import dev.lugami.otaku.listener.Listeners; import dev.lugami.otaku.listener.GeneralListeners;
import dev.lugami.otaku.profile.Profile; import dev.lugami.otaku.profile.Profile;
import dev.lugami.otaku.profile.ProfileListener; import dev.lugami.otaku.profile.ProfileListener;
import dev.lugami.otaku.utils.CombatManager; import dev.lugami.otaku.utils.CombatManager;
import dev.lugami.otaku.utils.EnderpearlManager; import dev.lugami.otaku.utils.EnderpearlManager;
import dev.lugami.otaku.utils.SaveMethodType;
import dev.lugami.otaku.utils.board.Assemble; import dev.lugami.otaku.utils.board.Assemble;
import dev.lugami.otaku.utils.board.AssembleStyle; import dev.lugami.otaku.utils.board.AssembleStyle;
import dev.lugami.otaku.utils.config.BasicConfigurationFile; import dev.lugami.otaku.utils.config.BasicConfigurationFile;
@ -41,7 +47,8 @@ public class Main extends JavaPlugin {
@Getter private Assemble board; @Getter private Assemble board;
@Getter private CombatManager combatManager; @Getter private CombatManager combatManager;
@Getter private EnderpearlManager enderpearlManager; @Getter private EnderpearlManager enderpearlManager;
@Getter private MongoDatabase mongoDatabase;
@Getter private SaveMethodType saveMethodType;
public void onEnable() { public void onEnable() {
setInstance(this); setInstance(this);
honcho = new Honcho(this); honcho = new Honcho(this);
@ -51,13 +58,22 @@ public class Main extends JavaPlugin {
profileConfig = new BasicConfigurationFile(this, "profiles"); profileConfig = new BasicConfigurationFile(this, "profiles");
kitsConfig = new BasicConfigurationFile(this, "kits"); kitsConfig = new BasicConfigurationFile(this, "kits");
board = new Assemble(this, new Board(), 2, false, AssembleStyle.MODIFIED, true); board = new Assemble(this, new Board(), 2, false, AssembleStyle.MODIFIED, true);
try {
saveMethodType = SaveMethodType.valueOf(mainConfig.getString("SAVE_METHOD"));
} catch (Exception ex) {
saveMethodType = SaveMethodType.defaultTo();
}
combatManager = new CombatManager(); combatManager = new CombatManager();
combatManager.runTaskTimer(this, 0, 2); combatManager.runTaskTimer(this, 0, 2);
enderpearlManager = new EnderpearlManager(); enderpearlManager = new EnderpearlManager();
enderpearlManager.runTaskTimer(this, 0, 2); enderpearlManager.runTaskTimer(this, 0, 2);
if (saveMethodType == SaveMethodType.MONGO) {
loadMongo();
}
Kit.init(); Kit.init();
Hotbar.init(); Hotbar.init();
Arrays.asList(new FFAListener(), new Listeners(), new ProfileListener(), new MenuListener()).forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this)); Profile.init();
Arrays.asList(new FFAListener(), new GeneralListeners(), new ProfileListener(), new MenuListener()).forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this));
Arrays.asList(new FFACommand(), new FFASetSpawnCommand(), new KitSetIconCommand(), new KitCreateCommand(), new KitDeleteCommand(), new KitGetLoadoutCommand(), new KitSetLoadoutCommand(), new KitsCommand(), new KitSetLocationCommand(), new MainMenuCommand(), new FFAJoinCommand(), new FFAQuitCommand(), new KitCommand(), new ReKitCommand()).forEach(command -> honcho.registerCommand(command)); Arrays.asList(new FFACommand(), new FFASetSpawnCommand(), new KitSetIconCommand(), new KitCreateCommand(), new KitDeleteCommand(), new KitGetLoadoutCommand(), new KitSetLoadoutCommand(), new KitsCommand(), new KitSetLocationCommand(), new MainMenuCommand(), new FFAJoinCommand(), new FFAQuitCommand(), new KitCommand(), new ReKitCommand()).forEach(command -> honcho.registerCommand(command));
getServer().getWorlds().forEach(essentials::clearEntities); getServer().getWorlds().forEach(essentials::clearEntities);
} }
@ -68,7 +84,25 @@ public class Main extends JavaPlugin {
player.kickPlayer("Server restarting"); player.kickPlayer("Server restarting");
} }
Kit.saveAll(); Kit.saveAll();
//Profile.saveAll();
Profile.removeAll(); Profile.removeAll();
} }
private void loadMongo() {
if (mainConfig.getBoolean("MONGO.AUTHENTICATION.ENABLED")) {
mongoDatabase = new MongoClient(
new ServerAddress(
mainConfig.getString("MONGO.HOST"),
mainConfig.getInteger("MONGO.PORT")
),
MongoCredential.createCredential(
mainConfig.getString("MONGO.AUTHENTICATION.USERNAME"),
"admin", mainConfig.getString("MONGO.AUTHENTICATION.PASSWORD").toCharArray()
),
MongoClientOptions.builder().build()
).getDatabase(mainConfig.getString("MONGO.DATABASE"));
} else {
mongoDatabase = new MongoClient(mainConfig.getString("MONGO.HOST"), mainConfig.getInteger("MONGO.PORT"))
.getDatabase(mainConfig.getString("MONGO.DATABASE"));
}
}
} }

View File

@ -9,8 +9,8 @@ import dev.lugami.otaku.hotbar.HotbarItem;
import dev.lugami.otaku.kit.Kit; import dev.lugami.otaku.kit.Kit;
import dev.lugami.otaku.kit.KitLoadout; import dev.lugami.otaku.kit.KitLoadout;
import dev.lugami.otaku.profile.Profile; import dev.lugami.otaku.profile.Profile;
import dev.lugami.otaku.profile.data.ProfileState;
import dev.lugami.otaku.profile.data.ProfileKitData; import dev.lugami.otaku.profile.data.ProfileKitData;
import dev.lugami.otaku.profile.data.ProfileState;
import dev.lugami.otaku.utils.*; import dev.lugami.otaku.utils.*;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -20,6 +20,8 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -193,5 +195,29 @@ public class FFAListener implements Listener {
} }
} }
@EventHandler
public void onDamage(EntityDamageEvent event) {
if (event.getEntity() instanceof Player) {
Player player = (Player) event.getEntity();
if (event.getCause() == EntityDamageEvent.DamageCause.FALL) {
if (Profile.getOrCreate(player).getState() == ProfileState.PLAYING) {
event.setCancelled(Profile.getOrCreate(player).getFFA().getKitRules().isNoFall());
}
}
} else {
return;
}
}
@EventHandler
public void onDamage2(EntityDamageByEntityEvent event) {
if (event.getEntity() instanceof Player) {
Player player = (Player) event.getEntity();
if (Profile.getOrCreate(player).getFFA().getKitRules().isNoDamage()) {
event.setDamage(0.0);
}
}
}
} }

View File

@ -22,7 +22,7 @@ import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.player.*; import org.bukkit.event.player.*;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
public class Listeners implements Listener { public class GeneralListeners implements Listener {
@EventHandler @EventHandler
public void drop(PlayerDropItemEvent event) { public void drop(PlayerDropItemEvent event) {

View File

@ -1,17 +1,24 @@
package dev.lugami.otaku.profile; package dev.lugami.otaku.profile;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.ReplaceOptions;
import dev.lugami.otaku.Main; import dev.lugami.otaku.Main;
import dev.lugami.otaku.kit.Kit; import dev.lugami.otaku.kit.Kit;
import dev.lugami.otaku.profile.data.ProfileKitData; import dev.lugami.otaku.profile.data.ProfileKitData;
import dev.lugami.otaku.profile.data.ProfileSettings; import dev.lugami.otaku.profile.data.ProfileSettings;
import dev.lugami.otaku.profile.data.ProfileState; import dev.lugami.otaku.profile.data.ProfileState;
import dev.lugami.otaku.utils.CC;
import dev.lugami.otaku.utils.Cooldown; import dev.lugami.otaku.utils.Cooldown;
import dev.lugami.otaku.utils.FFACache; import dev.lugami.otaku.utils.FFACache;
import dev.lugami.otaku.utils.SaveMethodType;
import lombok.Data; import lombok.Data;
import lombok.Getter; import lombok.Getter;
import org.bson.Document;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
@ -21,6 +28,7 @@ public class Profile {
@Getter @Getter
private static List<Profile> profiles = new ArrayList<>(); private static List<Profile> profiles = new ArrayList<>();
@Getter private static MongoCollection<Document> collection;
private Player player; private Player player;
private ProfileState state; private ProfileState state;
@ -28,6 +36,35 @@ public class Profile {
private ProfileSettings settings; private ProfileSettings settings;
private Cooldown reKitCooldown; private Cooldown reKitCooldown;
public static void init() {
if (Main.getInstance().getSaveMethodType() == SaveMethodType.MONGO) {
collection = Main.getInstance().getMongoDatabase().getCollection("profiles");
}
// Players might have joined before the plugin finished loading
for (Player player : Bukkit.getOnlinePlayers()) {
Profile profile = new Profile(player.getUniqueId());
try {
profile.load();
} catch (Exception e) {
player.kickPlayer(CC.RED + "The server is loading...");
continue;
}
new Profile(player.getUniqueId());
}
// Save every 5 minutes to prevent data loss
new BukkitRunnable() {
@Override
public void run() {
for (Profile profile : Profile.getProfiles()) {
profile.save();
}
}
}.runTaskTimerAsynchronously(Main.getInstance(), 6000L, 6000L);
}
public Profile(UUID uuid) { public Profile(UUID uuid) {
player = Bukkit.getPlayer(uuid); player = Bukkit.getPlayer(uuid);
state = ProfileState.LOBBY; state = ProfileState.LOBBY;
@ -81,37 +118,93 @@ public class Profile {
} }
void load() { void load() {
ConfigurationSection mainSection = Main.getInstance().getProfileConfig().getConfiguration().getConfigurationSection("stats." + getPlayer().getName()); if (Main.getInstance().getSaveMethodType() == SaveMethodType.MONGO && collection != null) {
for (Kit kit : Kit.getKits()) { Document document = collection.find(Filters.eq("uuid", player.getUniqueId().toString())).first();
if (mainSection.getConfigurationSection(kit.getName()) != null) {
ConfigurationSection kitSection = mainSection.getConfigurationSection(kit.getName()); if (document == null) {
ProfileKitData profileKitData = new ProfileKitData(); this.save();
profileKitData.kills(kitSection.getInt("kills", 0)); return;
profileKitData.deaths(kitSection.getInt("deaths", 0));
profileKitData.killstreak(kitSection.getInt("killstreak", 0));
kitData.put(kit, profileKitData);
} }
Document opts = (Document) document.get("settings");
this.settings.scoreboard(opts.getBoolean("scoreboard"));
this.settings.autoRekit(opts.getBoolean("autoRekit"));
this.settings.dropProtect(opts.getBoolean("dropProtect"));
Document kitStatistics = (Document) document.get("kitStatistics");
for (String key : kitStatistics.keySet()) {
Document kitDocument = (Document) kitStatistics.get(key);
Kit kit = Kit.getByName(key);
if (kit != null) {
ProfileKitData profileKitData = new ProfileKitData();
profileKitData.kills(kitDocument.getInteger("kills"));
profileKitData.deaths(kitDocument.getInteger("deaths"));
profileKitData.killstreak(kitDocument.getInteger("killstreak"));
kitData.put(kit, profileKitData);
}
}
} else {
ConfigurationSection mainSection = Main.getInstance().getProfileConfig().getConfiguration().getConfigurationSection("stats." + getPlayer().getName());
for (Kit kit : Kit.getKits()) {
if (mainSection.getConfigurationSection(kit.getName()) != null) {
ConfigurationSection kitSection = mainSection.getConfigurationSection(kit.getName());
ProfileKitData profileKitData = new ProfileKitData();
profileKitData.kills(kitSection.getInt("kills", 0));
profileKitData.deaths(kitSection.getInt("deaths", 0));
profileKitData.killstreak(kitSection.getInt("killstreak", 0));
kitData.put(kit, profileKitData);
}
}
settings.scoreboard(Main.getInstance().getProfileConfig().getConfiguration().getBoolean("settings." + getPlayer().getName() + ".scoreboard", true));
settings.autoRekit(Main.getInstance().getProfileConfig().getConfiguration().getBoolean("settings." + getPlayer().getName() + ".autoRekit", true));
settings.dropProtect(Main.getInstance().getProfileConfig().getConfiguration().getBoolean("settings." + getPlayer().getName() + ".dropProtect", true));
if (Main.getInstance().getSaveMethodType() == SaveMethodType.MONGO && collection == null) Bukkit.getLogger().warning("Profile#getCollection was null, so " + player.getName() + "'s profile was loaded using the fallback flatfile method.");
} }
settings.scoreboard(Main.getInstance().getProfileConfig().getConfiguration().getBoolean("settings." + getPlayer().getName() + ".scoreboard", true));
settings.autoRekit(Main.getInstance().getProfileConfig().getConfiguration().getBoolean("settings." + getPlayer().getName() + ".autoRekit", true));
settings.dropProtect(Main.getInstance().getProfileConfig().getConfiguration().getBoolean("settings." + getPlayer().getName() + ".dropProtect", true));
} }
public void save() { public void save() {
for (Kit kit : Kit.getKits()) { if (Main.getInstance().getSaveMethodType() == SaveMethodType.MONGO && collection != null) {
ProfileKitData profileKitData = kitData.get(kit); Document document = new Document();
if (profileKitData == null) profileKitData = new ProfileKitData(); document.put("uuid", player.getUniqueId().toString());
Main.getInstance().getProfileConfig().getConfiguration().set("stats." + getPlayer().getName() + "." + kit.getName() + ".kills", profileKitData.kills());
Main.getInstance().getProfileConfig().getConfiguration().set("stats." + getPlayer().getName() + "." + kit.getName() + ".deaths", profileKitData.deaths()); Document optionsDocument = new Document();
Main.getInstance().getProfileConfig().getConfiguration().set("stats." + getPlayer().getName() + "." + kit.getName() + ".killstreak", profileKitData.killstreak()); optionsDocument.put("scoreboard", settings.scoreboard());
} optionsDocument.put("autoRekit", settings.autoRekit());
Main.getInstance().getProfileConfig().getConfiguration().set("settings." + getPlayer().getName() + ".scoreboard", settings.scoreboard()); optionsDocument.put("dropProtect", settings.dropProtect());
Main.getInstance().getProfileConfig().getConfiguration().set("settings." + getPlayer().getName() + ".autoRekit", settings.autoRekit()); document.put("settings", optionsDocument);
Main.getInstance().getProfileConfig().getConfiguration().set("settings." + getPlayer().getName() + ".dropProtect", settings.dropProtect());
try { Document kitStatisticsDocument = new Document();
Main.getInstance().getProfileConfig().getConfiguration().save(Main.getInstance().getProfileConfig().getFile());
} catch (IOException e) { for (Map.Entry<Kit, ProfileKitData> entry : kitData.entrySet()) {
e.printStackTrace(); Document kitDocument = new Document();
kitDocument.put("kills", entry.getValue().kills());
kitDocument.put("deaths", entry.getValue().deaths());
kitDocument.put("killstreak", entry.getValue().killstreak());
kitStatisticsDocument.put(entry.getKey().getName(), kitDocument);
}
document.put("kitStatistics", kitStatisticsDocument);
collection.replaceOne(Filters.eq("uuid", player.getUniqueId().toString()), document, new ReplaceOptions().upsert(true));
} else {
for (Kit kit : Kit.getKits()) {
ProfileKitData profileKitData = kitData.get(kit);
if (profileKitData == null) profileKitData = new ProfileKitData();
Main.getInstance().getProfileConfig().getConfiguration().set("stats." + getPlayer().getName() + "." + kit.getName() + ".kills", profileKitData.kills());
Main.getInstance().getProfileConfig().getConfiguration().set("stats." + getPlayer().getName() + "." + kit.getName() + ".deaths", profileKitData.deaths());
Main.getInstance().getProfileConfig().getConfiguration().set("stats." + getPlayer().getName() + "." + kit.getName() + ".killstreak", profileKitData.killstreak());
}
Main.getInstance().getProfileConfig().getConfiguration().set("settings." + getPlayer().getName() + ".scoreboard", settings.scoreboard());
Main.getInstance().getProfileConfig().getConfiguration().set("settings." + getPlayer().getName() + ".autoRekit", settings.autoRekit());
Main.getInstance().getProfileConfig().getConfiguration().set("settings." + getPlayer().getName() + ".dropProtect", settings.dropProtect());
try {
Main.getInstance().getProfileConfig().getConfiguration().save(Main.getInstance().getProfileConfig().getFile());
} catch (IOException e) {
e.printStackTrace();
}
if (Main.getInstance().getSaveMethodType() == SaveMethodType.MONGO && collection == null) Bukkit.getLogger().warning("Profile#getCollection was null, so " + player.getName() + "'s profile was saved using the fallback flatfile method.");
} }
} }
@ -123,8 +216,4 @@ public class Profile {
profiles.clear(); profiles.clear();
} }
public static void saveAll() {
for (Profile profile : profiles) profile.save();
}
} }

View File

@ -0,0 +1,12 @@
package dev.lugami.otaku.utils;
public enum SaveMethodType {
MONGO, FLATFILE;
public static SaveMethodType defaultTo() {
return FLATFILE;
}
}

View File

@ -1,3 +1,15 @@
MONGO:
HOST: "127.0.0.1"
PORT: 27017
DATABASE: otaku
AUTHENTICATION:
ENABLED: false
USERNAME: ""
PASSWORD: ""
# Possible values: MONGO, FLATFILE
SAVE_METHOD: "MONGO"
HOTBAR_ITEMS: HOTBAR_ITEMS:
MAIN_MENU: MAIN_MENU:
MATERIAL: "NETHER_STAR" MATERIAL: "NETHER_STAR"