Skip to content

Commit

Permalink
Merge pull request #33 from APDevTeam/assault-fixes
Browse files Browse the repository at this point in the history
Assault bossbars and fix multiple assaults
  • Loading branch information
TylerS1066 authored Jul 2, 2024
2 parents 1e9e2bf + b2afe50 commit ca7c0c2
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 69 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<description>Warfare features of Movecraft</description>

<properties>
<java.version>13</java.version>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>https://github.com/APDevTeam/Movecraft-Warfare</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import net.countercraft.movecraft.repair.MovecraftRepair;
import net.countercraft.movecraft.util.Tags;
import net.countercraft.movecraft.warfare.bar.AssaultBarManager;
import net.countercraft.movecraft.warfare.bar.config.PlayerManager;
import net.countercraft.movecraft.warfare.commands.*;
import net.countercraft.movecraft.warfare.config.Config;
import net.countercraft.movecraft.warfare.features.assault.AssaultManager;
import net.countercraft.movecraft.warfare.features.assault.RegionDamagedSign;
import net.countercraft.movecraft.warfare.features.assault.commands.AssaultCommand;
import net.countercraft.movecraft.warfare.features.assault.commands.AssaultInfoCommand;
import net.countercraft.movecraft.warfare.features.assault.commands.AssaultRepairCommand;
import net.countercraft.movecraft.warfare.features.assault.listener.AssaultExplosionListener;
import net.countercraft.movecraft.warfare.features.siege.SiegeCommand;
import net.countercraft.movecraft.warfare.features.siege.SiegeLeaderListener;
import net.countercraft.movecraft.warfare.features.siege.SiegeManager;
import net.countercraft.movecraft.warfare.localisation.I18nSupport;
Expand All @@ -35,6 +34,12 @@ public void onEnable() {

saveDefaultConfig();

File folder = new File(getDataFolder(), "userdata");
if (!folder.exists()) {
getLogger().info("Created userdata directory");
folder.mkdirs();
}

String[] languages = { "en" };
for (String s : languages) {
if (!new File(getDataFolder() + "/localisation/mcwlang_" + s + ".properties").exists()) {
Expand All @@ -56,6 +61,9 @@ public void onEnable() {
Config.SiegeEnable = false;
}

var playerManager = new PlayerManager();
getServer().getPluginManager().registerEvents(playerManager, this);

if (Config.AssaultEnable) {
assaultManager = new AssaultManager(this);
assaultManager.runTaskTimerAsynchronously(this, 0, 20);
Expand Down Expand Up @@ -85,12 +93,18 @@ public void onEnable() {
}
}

// Startup assault bar manager (every second)
AssaultBarManager assaultBarManager = new AssaultBarManager(playerManager);
assaultBarManager.runTaskTimerAsynchronously(this, 15, 20);
getServer().getPluginManager().registerEvents(assaultBarManager, this);

getServer().getPluginManager().registerEvents(new AssaultExplosionListener(), this);
getServer().getPluginManager().registerEvents(new RegionDamagedSign(), this);
}

getCommand("assaultinfo").setExecutor(new AssaultInfoCommand());
getCommand("assault").setExecutor(new AssaultCommand());
getCommand("assaultbar").setExecutor(new AssaultBarCommand(playerManager));
getCommand("assaultrepair").setExecutor(new AssaultRepairCommand());

if (Config.SiegeEnable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package net.countercraft.movecraft.warfare.bar;

import net.countercraft.movecraft.warfare.bar.config.PlayerManager;
import net.countercraft.movecraft.warfare.config.Config;
import net.countercraft.movecraft.warfare.features.assault.Assault;
import net.countercraft.movecraft.warfare.features.assault.events.AssaultLoseEvent;
import net.countercraft.movecraft.warfare.features.assault.events.AssaultPreStartEvent;
import net.countercraft.movecraft.warfare.features.assault.events.AssaultStartEvent;
import net.countercraft.movecraft.warfare.features.assault.events.AssaultWinEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

public class AssaultBarManager extends BukkitRunnable implements Listener {
private final Map<Assault, BossBar> bossBars = new HashMap<>();
private final PlayerManager manager;

public AssaultBarManager(PlayerManager manager) {
this.manager = manager;
}

@Override
public void run() {
for (Map.Entry<Assault, BossBar> entry : bossBars.entrySet()) {
Assault assault = entry.getKey();
BossBar bossBar = entry.getValue();
switch (assault.getStage().get()) {
case PREPARATION:
long elapsed = Duration.between(assault.getStartTime(), LocalDateTime.now()).toMillis();
bossBar.setProgress(Math.min(elapsed / (Config.AssaultDelay * 1000.0), 1.0));
bossBar.setTitle(assault.getRegionName() + ": " + String.format("%,d", Math.round(((Config.AssaultDelay * 1000.0) - elapsed) / 1000.0)));
break;
case IN_PROGRESS:
bossBar.setProgress(Math.min((double) assault.getDamages() / assault.getMaxDamages(), 1.0));
bossBar.setTitle(assault.getRegionName() + ": " + String.format("%,d", assault.getDamages()) + " / " + String.format("%,d", assault.getMaxDamages()));
break;
}

Location location = assault.getHitBox().getMidPoint().toBukkit(assault.getWorld());
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.getWorld() != assault.getWorld()) {
bossBar.removePlayer(player);
continue;
}

if (!manager.getAssaultBarSetting(player)) {
bossBar.removePlayer(player);
continue;
}

if (player.getLocation().distanceSquared(location) > 1000 * 1000) {
bossBar.removePlayer(player);
continue;
}

bossBar.addPlayer(player);
}
}
}

@EventHandler
public void onAssaultPreStart(@NotNull AssaultPreStartEvent e) {
bossBars.put(e.getAssault(), Bukkit.createBossBar(e.getAssault().getRegionName(), BarColor.YELLOW, BarStyle.SOLID));
}

@EventHandler
public void onAssaultStart(@NotNull AssaultStartEvent e) {
bossBars.get(e.getAssault()).setColor(BarColor.RED);
}

@EventHandler
public void onAssaultWin(@NotNull AssaultWinEvent e) {
remove(e.getAssault());
}

@EventHandler
public void onAssaultLose(@NotNull AssaultLoseEvent e) {
remove(e.getAssault());
}

private void remove(Assault assault) {
BossBar bossBar = bossBars.remove(assault);
bossBar.setVisible(false);
bossBar.removeAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.countercraft.movecraft.warfare.bar.config;

import org.jetbrains.annotations.Nullable;

import java.util.UUID;

public class PlayerConfig {
@Nullable
private UUID owner;
private boolean assaultBarSetting = true;
private boolean siegeBarSetting = true;

public PlayerConfig() {
}

public PlayerConfig(UUID owner) {
this.owner = owner;
}

@Nullable
public UUID getOwner() {
return owner;
}

public boolean getAssaultBarSetting() {
return assaultBarSetting;
}

public void toggleAssaultBarSetting() {
assaultBarSetting = !assaultBarSetting;
}

public boolean getSiegeBarSetting() {
return siegeBarSetting;
}

public void toggleSiegeBarSetting() {
siegeBarSetting = !siegeBarSetting;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package net.countercraft.movecraft.warfare.bar.config;

import net.countercraft.movecraft.warfare.MovecraftWarfare;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.server.PluginDisableEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import net.countercraft.movecraft.warfare.MovecraftWarfare;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import java.util.WeakHashMap;

public class PlayerManager implements Listener {
private final Map<Player, PlayerConfig> cache = new WeakHashMap<>();

@Nullable
public boolean getAssaultBarSetting(Player player) {
var config = cache.get(player);
if (config == null)
return true;

return config.getAssaultBarSetting();
}

public void toggleAssaultBarSetting(Player player) {
var config = cache.get(player);
if (config == null) {
config = loadPlayer(player);
cache.put(player, config);
}

config.toggleAssaultBarSetting();
}

@Nullable
public boolean getSiegeBarSetting(Player player) {
var config = cache.get(player);
if (config == null)
return true;

return config.getSiegeBarSetting();
}

public void toggleSiegeBarSetting(Player player) {
var config = cache.get(player);
if (config == null) {
config = loadPlayer(player);
cache.put(player, config);
}

config.toggleSiegeBarSetting();
}

private void savePlayer(Player player) {
var config = cache.get(player);
if (config == null)
return;

Gson gson = buildGson();
String str = null;
try {
str = gson.toJson(config);
} catch (JsonIOException e) {
e.printStackTrace();
return;
}

File file = getFile(player.getUniqueId());
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(str);
writer.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}

@NotNull
private PlayerConfig loadPlayer(@NotNull Player player) {
File file = getFile(player.getUniqueId());
if (!file.exists() || !file.isFile() || !file.canRead())
return new PlayerConfig(player.getUniqueId());

Gson gson = buildGson();
PlayerConfig config = null;
try {
config = gson.fromJson(new FileReader(file), new TypeToken<PlayerConfig>() {
}.getType());
} catch (FileNotFoundException ignored) {
return new PlayerConfig(player.getUniqueId());
} catch (JsonSyntaxException | JsonIOException e) {
e.printStackTrace();
}
return config;
}

private File getFile(UUID owner) {
return new File(
MovecraftWarfare.getInstance().getDataFolder().getAbsolutePath() + "/userdata/" + owner + ".json");
}

private static Gson buildGson() {
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
builder.serializeNulls();
return builder.create();
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerJoin(@NotNull PlayerJoinEvent e) {
Player player = e.getPlayer();
var config = loadPlayer(player);
cache.put(player, config);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerQuit(@NotNull PlayerQuitEvent e) {
Player player = e.getPlayer();
savePlayer(player);
cache.remove(player);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPluginDisable(@NotNull PluginDisableEvent e) {
if (e.getPlugin() != MovecraftWarfare.getInstance())
return;

for (Player p : cache.keySet()) {
savePlayer(p);
}
cache.clear();
}
}
Loading

0 comments on commit ca7c0c2

Please sign in to comment.