Skip to content

Commit

Permalink
Added option to not use PlayerMoveEvent for checking arenas
Browse files Browse the repository at this point in the history
  • Loading branch information
Despical committed Nov 25, 2023
1 parent 86a0dea commit eb2af6a
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ If you still didn't find an answer see documentation section below.
<dependency>
<groupId>com.github.Despical</groupId>
<artifactId>KOTL</artifactId>
<version>2.7.7</version>
<version>2.7.8</version>
<scope>compile</scope>
</dependency>
```
Expand All @@ -54,7 +54,7 @@ repositories {
```
```
dependencies {
compileOnly group: "com.github.Despical", name: "KOTL", version: "2.7.7;
compileOnly group: "com.github.Despical", name: "KOTL", version: "2.7.8;
}
```
</details>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>me.despical</groupId>
<artifactId>king-of-the-ladder</artifactId>
<version>2.7.7</version>
<version>2.7.8</version>
<name>King of the Ladder</name>
<url>https://www.spigotmc.org/resources/80686/</url>

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/me/despical/kotl/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import me.despical.commons.util.UpdateChecker;
import me.despical.kotl.api.StatsStorage;
import me.despical.kotl.arena.ArenaRegistry;
import me.despical.kotl.arena.managers.ArenaManager;
import me.despical.kotl.commands.AbstractCommand;
import me.despical.kotl.events.ListenerAdapter;
import me.despical.kotl.handlers.ChatManager;
Expand Down Expand Up @@ -62,6 +63,7 @@ public class Main extends JavaPlugin {
private LanguageManager languageManager;
private ArenaRegistry arenaRegistry;
private KitManager kitManager;
private ArenaManager arenaManager;

@Override
public void onEnable() {
Expand Down Expand Up @@ -116,6 +118,7 @@ private void initializeClasses() {
rewardsFactory = new RewardsFactory(this);
arenaRegistry = new ArenaRegistry(this);
kitManager = new KitManager(this);
arenaManager = new ArenaManager(this);

ListenerAdapter.registerEvents(this);
AbstractCommand.registerCommands(this);
Expand Down Expand Up @@ -187,6 +190,11 @@ public KitManager getKitManager() {
return kitManager;
}

@NotNull
public ArenaManager getArenaManager() {
return arenaManager;
}

public boolean getOption(ConfigPreferences.Option option) {
return configPreferences.getOption(option);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/me/despical/kotl/arena/Arena.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,18 @@ public void addPlayer(Player player) {
if (plugin.getOption(ConfigPreferences.Option.SCOREBOARD_ENABLED)) {
user.cacheScoreboard();

scoreboardManager.createScoreboard(player);
plugin.getServer().getScheduler().runTask(plugin, () -> scoreboardManager.createScoreboard(player));
}

if (plugin.getOption(ConfigPreferences.Option.CLEAR_INVENTORY)) {
player.getInventory().clear();
}

if (plugin.getOption(ConfigPreferences.Option.CLEAR_EFFECTS)) {
player.getActivePotionEffects().forEach(effect -> player.removePotionEffect(effect.getType()));
plugin.getServer().getScheduler().runTask(plugin, () -> player.getActivePotionEffects().forEach(effect -> player.removePotionEffect(effect.getType())));
}

player.setGameMode(GameMode.SURVIVAL);
plugin.getServer().getScheduler().runTask(plugin, () -> player.setGameMode(GameMode.SURVIVAL));
player.setFoodLevel(20);

doBarAction(player, 1);
Expand All @@ -318,7 +318,7 @@ public void removePlayer(Player player) {
}

if (plugin.getOption(ConfigPreferences.Option.CLEAR_EFFECTS)) {
player.getActivePotionEffects().forEach(effect -> player.removePotionEffect(effect.getType()));
plugin.getServer().getScheduler().runTask(plugin, () -> player.getActivePotionEffects().forEach(effect -> player.removePotionEffect(effect.getType())));
}

if (plugin.getOption(ConfigPreferences.Option.INVENTORY_MANAGER_ENABLED)) {
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/me/despical/kotl/arena/managers/ArenaManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package me.despical.kotl.arena.managers;

import me.despical.kotl.Main;
import me.despical.kotl.arena.Arena;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;

public class ArenaManager {

private final boolean async;
private final int schedulerType, interval;
private final Main plugin;

public ArenaManager(Main plugin) {
this.plugin = plugin;
this.interval = plugin.getConfig().getInt("Arena-Schedulers.Interval");
this.async = plugin.getConfig().getBoolean("Arena-Schedulers.Async");

switch (schedulerType = plugin.getConfig().getInt("Arena-Schedulers.Type")) {
case 1 -> createGeneralScheduler();
case 2 -> plugin.getArenaRegistry().getArenas().forEach(this::createSchedulerPerArena);
default -> registerDefaultEvent();
}
}

private void createGeneralScheduler() {
var scheduler = plugin.getServer().getScheduler();

if (async) {
scheduler.scheduleAsyncRepeatingTask(plugin, this::searchForPlayers0, 1L, interval);
} else {
scheduler.scheduleSyncRepeatingTask(plugin, this::searchForPlayers0, 1L, interval);
}
}

public void createSchedulerPerArena(Arena arena) {
if (schedulerType != 2) return;

var scheduler = plugin.getServer().getScheduler();

if (async) {
scheduler.scheduleAsyncRepeatingTask(plugin, () -> searchForPlayers1(arena), 1L, interval);
} else {
scheduler.scheduleSyncRepeatingTask(plugin, () -> searchForPlayers1(arena), 1L, interval);
}
}

private void searchForPlayers0() {
// Less operation in small outer loop and big inner loops
for (var iteratedArena : plugin.getArenaRegistry().getArenas()) {
Arena arena = null;

for (var player : plugin.getServer().getOnlinePlayers()) {
final var target = iteratedArena.isInArea(player);

if (target != null) arena = target;

if (!plugin.getArenaRegistry().isInArena(player) && arena != null) {
arena.addPlayer(player);
}

if (plugin.getArenaRegistry().isInArena(player) && arena == null) {
var tempArena = plugin.getArenaRegistry().getArena(player);

tempArena.removePlayer(player);
}
}
}
}

private void searchForPlayers1(Arena arena) {
for (var player : plugin.getServer().getOnlinePlayers()) {
final var target = arena.isInArea(player);
final var targetArena = plugin.getArenaRegistry().getArena(player);
var isInArena = targetArena != null;

if (!isInArena && target != null) {
arena.addPlayer(player);
}

if (isInArena && target == null) {
targetArena.removePlayer(player);
}
}
}

private void registerDefaultEvent() {
plugin.getServer().getPluginManager().registerEvents(new Listener() {

@EventHandler
public void onEnterAndLeaveGameArea(PlayerMoveEvent event) {
var player = event.getPlayer();
var arena = isInArea(player);
var playerArena = plugin.getArenaRegistry().getArena(player);
var isInArena = playerArena != null;

if (!isInArena && arena != null) {
arena.addPlayer(player);
}

if (isInArena && arena == null) {
playerArena.removePlayer(player);
}
}

private Arena isInArea(final Player player) {
for (var arena : plugin.getArenaRegistry().getArenas()) {
final var target = arena.isInArea(player);

if (target != null) return target;
}

return null;
}
}, plugin);
}
}
26 changes: 0 additions & 26 deletions src/main/java/me/despical/kotl/events/ArenaEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,6 @@ public ArenaEvents(Main plugin) {
super(plugin);
}

@EventHandler
public void onEnterAndLeaveGameArea(PlayerMoveEvent event) {
var player = event.getPlayer();
var arena = isInArea(player);

if (!plugin.getArenaRegistry().isInArena(player) && arena != null) {
arena.addPlayer(player);
}

if (plugin.getArenaRegistry().isInArena(player) && arena == null) {
var tempArena = plugin.getArenaRegistry().getArena(player);

tempArena.removePlayer(player);
}
}

@EventHandler
public void onInteractWithPlate(PlayerInteractEvent event) {
var player = event.getPlayer();
Expand Down Expand Up @@ -169,16 +153,6 @@ public void onRespawn(PlayerRespawnEvent event) {
arena.removePlayer(event.getPlayer());
}

private Arena isInArea(final Player player) {
for (var arena : plugin.getArenaRegistry().getArenas()) {
final var target = arena.isInArea(player);

if (target != null) return target;
}

return null;
}

private void spawnFireworks(Arena arena, Player player) {
if (!plugin.getOption(ConfigPreferences.Option.FIREWORKS_ON_NEW_KING)) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ public void injectComponents(SetupInventory setup, StaticPane pane) {
arena.setMaxCorner(LocationSerializer.fromString(config.getString(path + "areaMax")));
arena.setArenaPlate(XMaterial.valueOf(config.getString(path + "arenaPlate")));

plugin.getArenaManager().createSchedulerPerArena(arena);

player.sendMessage(chatManager.coloredRawMessage("&a&l✔ &aValidation succeeded! Registering new arena instance: &e" + arena.getId()));

config.set(path + "isdone", true);
Expand Down
18 changes: 18 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@
# tr - Türkçe
locale: default

Arena-Schedulers:
# Type 1: Creates a general task that loops all arenas and players to check
# if they are inside any arena, if they are they'll be put in the game.
#
# Type 2: Creates tasks for every arena which means every arena will look for
# all online players in their own task.
#
# Type 3 (Slowest for big networks): Registers a PlayerMoveEvent and checks in
# the event if player is inside any arena.
Type: 1

# How many ticks should we wait to lookup for players to put in arenas
Interval: 2

# Should we search for arenas asynchronously?
# In most cases events firing in main thread so async won't work for Type 3.
Async: true

# Enable boss bar support?
Boss-Bar-Enabled: true

Expand Down

0 comments on commit eb2af6a

Please sign in to comment.