-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand AFK and Exclusion APIs to allow control of logic flow + more
Also change examples and remove stafffacilities
- Loading branch information
Showing
11 changed files
with
165 additions
and
123 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,15 @@ | ||
package xyz.nkomarn.harbor.api; | ||
|
||
import java.util.function.BiFunction; | ||
import org.bukkit.configuration.Configuration; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public enum LogicType implements BiFunction<Boolean, Boolean, Boolean> { | ||
AND, OR, XOR; | ||
/** | ||
* An enum to represent the type of logic to be used when combining multiple Providers | ||
*/ | ||
public enum LogicType { | ||
AND, OR; | ||
|
||
@Override | ||
public Boolean apply(Boolean a, Boolean b) { | ||
if(this.equals(AND)) | ||
return a && b; | ||
else if(this.equals(OR)) | ||
return a || b; | ||
else if(this.equals(XOR)) | ||
return a ^ b; | ||
else | ||
throw new UnsupportedOperationException("Unsupported logic type"); | ||
public static LogicType fromConfig(@NotNull Configuration configuration, String path, LogicType defaultType) { | ||
return valueOf(configuration.getString(path, defaultType.toString()).toUpperCase().trim()); | ||
} | ||
} |
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
67 changes: 66 additions & 1 deletion
67
src/main/java/xyz/nkomarn/harbor/provider/DefaultAFKProvider.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 |
---|---|---|
@@ -1,20 +1,85 @@ | ||
package xyz.nkomarn.harbor.provider; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.HandlerList; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
import org.jetbrains.annotations.NotNull; | ||
import xyz.nkomarn.harbor.Harbor; | ||
import xyz.nkomarn.harbor.api.AFKProvider; | ||
import xyz.nkomarn.harbor.listener.AfkListeners; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class DefaultAFKProvider implements AFKProvider, Listener { | ||
private final Harbor harbor; | ||
private final boolean enabled; | ||
private final Map<UUID, Instant> playerActivity; | ||
private final AfkListeners listeners; | ||
|
||
public DefaultAFKProvider(@NotNull Harbor harbor) { | ||
this.harbor = harbor; | ||
playerActivity = new HashMap<>(); | ||
if (enabled = (harbor.getConfig().getBoolean("afk-detection.fallback-enabled", true))) { | ||
harbor.getLogger().info("Registering fallback AFK detection system."); | ||
listeners = new AfkListeners(this); | ||
harbor.getServer().getPluginManager().registerEvents(this, harbor); | ||
} else { | ||
harbor.getLogger().info("Not registering fallback AFK detection system."); | ||
listeners = null; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isAFK(Player player) { | ||
return false; | ||
if (!enabled || !playerActivity.containsKey(player.getUniqueId())) { | ||
return false; | ||
} | ||
|
||
long minutes = playerActivity.get(player.getUniqueId()).until(Instant.now(), ChronoUnit.MINUTES); | ||
return minutes >= harbor.getConfiguration().getInteger("afk-detection.timeout"); | ||
} | ||
|
||
/** | ||
* Sets the given player's last activity to the current timestamp. | ||
* | ||
* @param player The player to update. | ||
*/ | ||
public void updateActivity(@NotNull Player player) { | ||
playerActivity.put(player.getUniqueId(), Instant.now()); | ||
} | ||
|
||
|
||
/** | ||
* Enables Harbor's fallback listeners for AFK detection if other AFKProviders are not present. | ||
*/ | ||
public void enableListeners() { | ||
if (enabled) { | ||
listeners.runTaskTimer(harbor, 1, 1); | ||
harbor.getServer().getPluginManager().registerEvents(listeners, harbor); | ||
} | ||
} | ||
|
||
/** | ||
* Disables Harbor's fallback listeners for AFK detection if other AFKProviders are present. | ||
*/ | ||
|
||
public void disableListeners() { | ||
if (enabled) { | ||
listeners.cancel(); | ||
harbor.getLogger().info("Unregistering fallback AFK detection system."); | ||
} | ||
} | ||
|
||
@EventHandler | ||
public void onQuit(PlayerQuitEvent event) { | ||
playerActivity.remove(event.getPlayer().getUniqueId()); | ||
} | ||
} |
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
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
Oops, something went wrong.