-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from APDevTeam/assault-fixes
Assault bossbars and fix multiple assaults
- Loading branch information
Showing
13 changed files
with
403 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/net/countercraft/movecraft/warfare/bar/AssaultBarManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/net/countercraft/movecraft/warfare/bar/config/PlayerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
src/main/java/net/countercraft/movecraft/warfare/bar/config/PlayerManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.