initial commit

This commit is contained in:
Erik Bročko 2019-08-15 02:10:05 +02:00
commit b0d07bed96
4 changed files with 241 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
maven/
target/
.settings
.project
.classpath

46
pom.xml Normal file
View File

@ -0,0 +1,46 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.gamesites.mc</groupId>
<artifactId>AntiAFK</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>AntiAFK</name>
<description>AntiAFK Spigot plugin</description>
<organization>
<name>GameSites</name>
<url>https://gamesites.cz</url>
</organization>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14.4-R0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>12</release>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
</project>

19
resources/plugin.yml Normal file
View File

@ -0,0 +1,19 @@
name: AntiAFK
main: cz.gamesites.mc.antiafk.AntiAFK
version: 0.0.1
api-version: 1.14
author: ericek111
website: https://gamesites.cz/
commands:
afklist:
aliases: whoisafk
description: Core plugin controls.
permission: antiafk.afklist
permissions:
antiafk.afklist:
description: Allows to list AFK players (including invisible admins).
default: true
antiafk.ignore:
description: With this permission, you are completely ignored by the AntiAFK plugin.
default: false

View File

@ -0,0 +1,171 @@
package cz.gamesites.mc.antiafk;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import net.md_5.bungee.api.ChatColor;
public class AntiAFK extends JavaPlugin implements Listener {
// frequency of checks
private static final int CHECK_INTERVAL = 2 * 20;
// how many times a check has to fail in order to mark the player as AFK
private static final int STRIKES_AFK = 3;
private static AntiAFK INSTANCE = null;
private HashMap<Player, int[]> lastPositions = new HashMap<>();
private HashMap<Player, Integer> strikes = new HashMap<>();
private ArrayList<Inventory> inventories = new ArrayList<>();
private static final ItemStack yesItem = new ItemStack(Material.DIAMOND, 1);
private static final ItemStack noItem = new ItemStack(Material.GRAY_STAINED_GLASS_PANE, 1);
static {
ItemMeta yesMeta = yesItem.getItemMeta();
yesMeta.setDisplayName("§aAno, jsem tady.");
yesItem.setItemMeta(yesMeta);
ItemMeta noMeta = yesItem.getItemMeta();
noMeta.setDisplayName("§cNe, nejsem tady.");
noItem.setItemMeta(noMeta);
}
@Override
public void onEnable() {
INSTANCE = this;
Bukkit.getServer().getPluginManager().registerEvents(this, this);
createInventories(20);
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> {
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (p.hasPermission("antiafk.ignore"))
continue;
int bX = p.getLocation().getBlockX();
int bY = p.getLocation().getBlockY();
int bZ = p.getLocation().getBlockZ();
int[] newPos = new int[] { bX, bY, bZ };
int[] lastPos = this.lastPositions.get(p);
if (lastPos == null || !Arrays.equals(lastPos, newPos)) {
this.lastPositions.put(p, newPos);
this.strikes.put(p, 0);
continue;
}
int strikenum = strikes.merge(p, 1, Integer::sum);
if (strikenum == STRIKES_AFK) {
int idx = ThreadLocalRandom.current().nextInt(0, inventories.size());
p.openInventory(inventories.get(idx));
p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 0.5F, 0.5F);
}
this.lastPositions.put(p, newPos);
}
}, CHECK_INTERVAL, CHECK_INTERVAL);
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
this.lastPositions.remove(event.getPlayer());
this.strikes.remove(event.getPlayer());
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("afklist")) {
long playersAway = strikes.entrySet().stream().filter(x -> x.getValue() > STRIKES_AFK).count();
if (playersAway > 0) {
sender.sendMessage(
ChatColor.GOLD + "[AntiAFK] " +
ChatColor.YELLOW + "Players away " + ChatColor.GRAY + "(" + playersAway + ")" + ChatColor.YELLOW + ": "
);
} else {
sender.sendMessage(ChatColor.GOLD + "[AntiAFK] " + ChatColor.YELLOW + "There are no AFK players!");
}
strikes.entrySet().stream()
.filter(x -> x.getValue() > STRIKES_AFK)
.sorted((e1, e2) -> Integer.compare(e2.getValue(), e1.getValue()))
.forEach(x -> {
int awayMin = x.getValue() * CHECK_INTERVAL / 20 / 60;
sender.sendMessage(x.getKey().getDisplayName() + ChatColor.GRAY + ": " + awayMin + " min.");
});
return true;
}
return false;
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (this.inventories.contains(event.getInventory())) {
event.setCancelled(true);
HumanEntity ent = event.getWhoClicked();
if (!(ent instanceof Player))
return;
ItemStack is = event.getCurrentItem();
if (is != null && is.equals(yesItem)) {
strikes.put((Player) ent, 0);
ent.closeInventory();
}
}
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
if (this.inventories.contains(event.getInventory()) && this.strikes.get(event.getPlayer()) > 0) {
Bukkit.getScheduler().runTask(this, () -> event.getPlayer().openInventory(event.getInventory()));
}
}
private void createInventories(int count) {
for (int i = 0; i < count; i++) {
this.inventories.add(Bukkit.createInventory(null, 5 * 9, "AFK kontrola - klikni na diamant"));
}
Random rand = new Random();
for (Inventory inv : this.inventories) {
int rSlot = rand.nextInt(inv.getSize());
for (int i = 0; i < inv.getSize(); i++) {
inv.setItem(i, i == rSlot ? yesItem : noItem);
}
}
}
public static List<Player> getAwayPlayers() {
return INSTANCE.strikes.entrySet()
.stream()
.filter(x -> x.getValue() > STRIKES_AFK)
.map(x -> x.getKey())
.collect(Collectors.toList());
}
}