Browse Source

Improvements to sub 1.13 support.

master
Stefatorus 4 years ago
parent
commit
10a1ef4897
  1. 266
      src/com/entryrise/afkguard/Main.java
  2. 177
      src/com/entryrise/afkguard/antiafk/captcha/CaptchaCategory.java
  3. 438
      src/com/entryrise/afkguard/antiafk/captcha/CaptchaGUI.java

266
src/com/entryrise/afkguard/Main.java

@ -1,133 +1,133 @@
package com.entryrise.afkguard;
import java.io.File;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import com.entryrise.afkguard.antiafk.AFKManager;
import com.entryrise.afkguard.antiafk.captcha.CaptchaCategory;
import com.entryrise.afkguard.antiafk.captcha.CaptchaGUI;
import com.entryrise.afkguard.antiafk.captcha.CaptchaGUI.CaptchaData;
import com.entryrise.afkguard.cmd.CommandListener;
import com.entryrise.afkguard.cmd.CommandTabListener;
import com.entryrise.afkguard.listeners.ListenersMain;
import com.entryrise.afkguard.utils.Others;
import com.entryrise.afkguard.utils.VersionMgr;
import net.milkbowl.vault.economy.Economy;
public class Main extends JavaPlugin implements Listener {
public static String USER = "%%__USER__%%";
public static final String PREFIX = "§2§lAFK§f§lGuard §e» §f";
public static JavaPlugin p;
public static boolean paper = false;
private static File file;
public static FileConfiguration config = new YamlConfiguration();
public static Economy econ;
public void onEnable() {
p = this;
file = new File(getDataFolder(), "server.yml");
config = Others.getConfig(file, 1);
paper = VersionMgr.isPaper();
Bukkit.getLogger().info(PREFIX + "Enabling Systems:");
setupEconomy();
EnableClasses(false);
getServer().getPluginManager().registerEvents(this, this);
getCommand("afkguard").setExecutor(new CommandListener());
getCommand("afkguard").setTabCompleter(new CommandTabListener());
}
private boolean setupEconomy() {
if (getServer().getPluginManager().getPlugin("Vault") == null) {
return false;
}
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
return false;
}
econ = rsp.getProvider();
return econ != null;
}
private static void EnableClasses(boolean reload) {
ListenersMain.Enabler(reload);
CaptchaCategory.Enabler(reload);
AFKManager.Enabler(reload);
}
public static void ReloadPlugin(CommandSender s) {
config = Others.getConfig(file, 1);
Bukkit.getLogger().info(PREFIX + "Reloading Systems:");
EnableClasses(true);
s.sendMessage(PREFIX + "Reloaded the config successfully.");
}
@Override
public void onDisable() {
// PacketInjector.Disabler(); <- SEEMS UNIMPORTANT AND GLITCHY
}
public static void failedAttempt(Player p) {
CaptchaGUI.invs.remove(p.getUniqueId());
p.closeInventory();
// p.kickPlayer(getKickMsg());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Main.config.getString("settings.captcha.fail-command").replace("%player%", p.getName()));
}
public static void goodAttempt(Player p, int target) {
CaptchaGUI.invs.remove(p.getUniqueId());
p.closeInventory();
// p.sendMessage(getSuccessfulsg());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Main.config.getString("settings.captcha.success-command").replace("%player%", p.getName()));
AFKManager.setScore(p, target);
}
public static void captchaResult(Player p, Material mat) {
CaptchaData cd = CaptchaGUI.invs.get(p.getUniqueId());
if (!cd.category.getAllowed().contains(mat)) {
failedAttempt(p);
return;
}
goodAttempt(p, cd.counter);
}
public static void openCapcha(Player p, int resetcounter) {
Bukkit.getScheduler().runTaskLater(Main.p, () -> p.openInventory(CaptchaGUI.getInventory(p, resetcounter)), 20);
}
public static void penalizePlayer(Player p, double payment, int reset) {
OfflinePlayer op = Bukkit.getOfflinePlayer(p.getUniqueId());
if (!econ.has(op, payment)) {
return;
}
econ.withdrawPlayer(op, payment);
AFKManager.setScore(p, reset);
}
}
package com.entryrise.afkguard;
import java.io.File;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import com.entryrise.afkguard.antiafk.AFKManager;
import com.entryrise.afkguard.antiafk.captcha.CaptchaCategory;
import com.entryrise.afkguard.antiafk.captcha.CaptchaGUI;
import com.entryrise.afkguard.antiafk.captcha.CaptchaGUI.CaptchaData;
import com.entryrise.afkguard.cmd.CommandListener;
import com.entryrise.afkguard.cmd.CommandTabListener;
import com.entryrise.afkguard.listeners.ListenersMain;
import com.entryrise.afkguard.utils.Others;
import com.entryrise.afkguard.utils.VersionMgr;
import net.milkbowl.vault.economy.Economy;
public class Main extends JavaPlugin implements Listener {
public static String USER = "%%__USER__%%";
public static final String PREFIX = "§2§lAFK§f§lGuard §e» §f";
public static JavaPlugin p;
public static boolean paper = false;
private static File file;
public static FileConfiguration config = new YamlConfiguration();
public static Economy econ;
public void onEnable() {
p = this;
file = new File(getDataFolder(), "server.yml");
config = Others.getConfig(file, 1);
paper = VersionMgr.isPaper();
Bukkit.getLogger().info(PREFIX + "Enabling Systems:");
setupEconomy();
EnableClasses(false);
getServer().getPluginManager().registerEvents(this, this);
getCommand("afkguard").setExecutor(new CommandListener());
getCommand("afkguard").setTabCompleter(new CommandTabListener());
}
private boolean setupEconomy() {
if (getServer().getPluginManager().getPlugin("Vault") == null) {
return false;
}
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
return false;
}
econ = rsp.getProvider();
return econ != null;
}
private static void EnableClasses(boolean reload) {
ListenersMain.Enabler(reload);
CaptchaCategory.Enabler(reload);
AFKManager.Enabler(reload);
}
public static void ReloadPlugin(CommandSender s) {
config = Others.getConfig(file, 1);
Bukkit.getLogger().info(PREFIX + "Reloading Systems:");
EnableClasses(true);
s.sendMessage(PREFIX + "Reloaded the config successfully.");
}
@Override
public void onDisable() {
// PacketInjector.Disabler(); <- SEEMS UNIMPORTANT AND GLITCHY
}
public static void failedAttempt(Player p) {
CaptchaGUI.invs.remove(p.getUniqueId());
p.closeInventory();
// p.kickPlayer(getKickMsg());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Main.config.getString("settings.captcha.fail-command").replace("%player%", p.getName()));
}
public static void goodAttempt(Player p, int target) {
CaptchaGUI.invs.remove(p.getUniqueId());
p.closeInventory();
// p.sendMessage(getSuccessfulsg());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Main.config.getString("settings.captcha.success-command").replace("%player%", p.getName()));
AFKManager.setScore(p, target);
}
public static void captchaResult(Player p, ItemStack mat) {
CaptchaData cd = CaptchaGUI.invs.get(p.getUniqueId());
if (!cd.category.getAllowed().contains(mat)) {
failedAttempt(p);
return;
}
goodAttempt(p, cd.counter);
}
public static void openCapcha(Player p, int resetcounter) {
Bukkit.getScheduler().runTaskLater(Main.p, () -> p.openInventory(CaptchaGUI.getInventory(p, resetcounter)), 20);
}
public static void penalizePlayer(Player p, double payment, int reset) {
OfflinePlayer op = Bukkit.getOfflinePlayer(p.getUniqueId());
if (!econ.has(op, payment)) {
return;
}
econ.withdrawPlayer(op, payment);
AFKManager.setScore(p, reset);
}
}

177
src/com/entryrise/afkguard/antiafk/captcha/CaptchaCategory.java

@ -1,90 +1,87 @@
package com.entryrise.afkguard.antiafk.captcha;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.SplittableRandom;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import com.entryrise.afkguard.Main;
public class CaptchaCategory {
private static SplittableRandom sr = new SplittableRandom();
private static List<CaptchaCategory> categories = new ArrayList<CaptchaCategory>();
public static void Enabler(boolean reload) {
if (!reload) {
Main.p.getServer().getPluginManager().registerEvents(new CaptchaGUI(), Main.p);
}
for (String stg : Main.config.getConfigurationSection("settings.captcha.categories").getKeys(false)) {
categories.add(new CaptchaCategory(stg));
}
Bukkit.getLogger().info(" §e[§a✔§e] §fCategories Added");
}
public static CaptchaCategory getCategory(CaptchaCategory exception) {
// For safety if the random randomly breaks out of some reason or another.
// Will render plugin kind of unsafe but still usable and won't crash server.
int i = 50;
CaptchaCategory categ;
do {
categ = categories.get(sr.nextInt(categories.size()));
} while (categ.equals(exception) && --i > 0);
return categ;
}
public List<Material> getShuffledFiller() {
List<Material> raw = new ArrayList<Material>();
for (CaptchaCategory cat : categories) {
if (cat.equals(this)) {
continue;
}
for (int i = 0; i < 5; i++) {
raw.addAll(cat.members);
}
}
Collections.shuffle(raw);
return raw;
}
// This is where objective stuff starts and no more statics roam the land.
private List<Material> members = new ArrayList<Material>();
private String name;
private CaptchaCategory(String name) {
String loc = "settings.captcha.categories." + name;
this.name = name;
for (String stg : Main.config.getStringList(loc)) {
members.add(Material.getMaterial(stg));
}
}
public Material getRandom(Material exception) {
// For safety if the random randomly breaks out of some reason or another.
// Will render plugin kind of unsafe but still usable and won't crash server.
int i = 50;
Material mat;
do {
mat = members.get(sr.nextInt(members.size()));
} while (mat.equals(exception) && --i > 0);
return mat;
}
public String getName() {
return name;
}
public List<Material> getAllowed() {
return members;
}
}
package com.entryrise.afkguard.antiafk.captcha;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.SplittableRandom;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import com.entryrise.afkguard.Main;
import com.entryrise.afkguard.mineutils.ItemBuilder;
public class CaptchaCategory {
private static SplittableRandom sr = new SplittableRandom();
private static List<CaptchaCategory> categories = new ArrayList<CaptchaCategory>();
public static void Enabler(boolean reload) {
if (!reload) {
Main.p.getServer().getPluginManager().registerEvents(new CaptchaGUI(), Main.p);
}
for (String stg : Main.config.getConfigurationSection("settings.captcha.categories").getKeys(false)) {
categories.add(new CaptchaCategory(stg));
}
Bukkit.getLogger().info(" §e[§a✔§e] §fCategories Added");
}
public static CaptchaCategory getCategory(CaptchaCategory exception) {
// For safety if the random randomly breaks out of some reason or another.
// Will render plugin kind of unsafe but still usable and won't crash server.
int i = 50;
CaptchaCategory categ;
do {
categ = categories.get(sr.nextInt(categories.size()));
} while (categ.equals(exception) && --i > 0);
return categ;
}
public List<ItemStack> getShuffledFiller() {
List<ItemStack> raw = new ArrayList<ItemStack>();
for (CaptchaCategory cat : categories) {
if (cat.equals(this)) {
continue;
}
for (int i = 0; i < 5; i++) {
raw.addAll(cat.members);
}
}
Collections.shuffle(raw);
return raw;
}
// This is where objective stuff starts and no more statics roam the land.
private List<ItemStack> members = new ArrayList<ItemStack>();
private String name;
private CaptchaCategory(String name) {
String loc = "settings.captcha.categories." + name;
this.name = name;
for (String stg : Main.config.getStringList(loc)) {
members.add(new ItemBuilder(Main.config, "settings.captcha.gui.item").setType(stg).build());
}
}
public ItemStack getRandom(ItemStack exception) {
// For safety if the random randomly breaks out of some reason or another.
// Will render plugin kind of unsafe but still usable and won't crash server.
List<ItemStack> items = new ArrayList<>(members);
items.remove(exception);
return items.get(sr.nextInt(items.size()));
}
public String getName() {
return name;
}
public List<ItemStack> getAllowed() {
return members;
}
}

438
src/com/entryrise/afkguard/antiafk/captcha/CaptchaGUI.java

@ -1,216 +1,222 @@
package com.entryrise.afkguard.antiafk.captcha;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SplittableRandom;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
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.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import com.entryrise.afkguard.Main;
import com.entryrise.afkguard.mineutils.ItemBuilder;
public class CaptchaGUI implements Listener {
public static List<Integer> filler = new ArrayList<Integer>();
private static SplittableRandom sr = new SplittableRandom();
// Fillers where selector items can stay;
static {
for (int i = 19; i < 26; i++) {
filler.add(i);
}
for (int i = 28; i < 35; i++) {
filler.add(i);
}
for (int i = 37; i < 44; i++) {
filler.add(i);
}
filler.add(10);
filler.add(11);
filler.add(15);
filler.add(16);
}
public static class CaptchaData {
public CaptchaCategory category;
public Material example;
public Inventory inv;
public int counter;
public CaptchaData() {
this.category = CaptchaCategory.getCategory(null);
this.example = category.getRandom(null);
}
}
public static Map<UUID, CaptchaData> invs = new HashMap<UUID, CaptchaData>();
public static Inventory getInventory(Player p, int counter) {
return getInventory(p.getUniqueId(), counter);
}
public static boolean existsAlready(Player p) {
return existsAlready(p.getUniqueId());
}
public static boolean existsAlready(UUID u) {
return invs.containsKey(u);
}
public static Inventory getInventory(UUID u, int counter) {
if (existsAlready(u)) {
return invs.get(u).inv;
} else if (counter != -1) {
return createInventory(u, counter);
}
return null;
}
private static Inventory createInventory(UUID u, int counter) {
CaptchaData cd = new CaptchaData();
Inventory inv = Bukkit.createInventory(null, 54, getTitle(cd.category.getName()));
setBorders(inv);
setRandomExample(inv, cd);
setRandomAnswers(inv, cd);
cd.inv = inv;
cd.counter = counter;
invs.put(u, cd);
return inv;
}
public static String getTitle(String categoryname) {
return ChatColor.translateAlternateColorCodes('&', Main.config.getString("settings.captcha.gui.title"))
.replace("%category%", categoryname);
}
@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
HumanEntity hm = e.getWhoClicked();
if (!(hm instanceof Player)) {
return;
}
Player p = (Player) hm;
Inventory invent = e.getInventory();
ItemStack itm = e.getCurrentItem();
if (itm == null) {
return;
}
if (!invs.containsKey(p.getUniqueId())) {
return;
}
if (!invent.equals(invs.get(p.getUniqueId()).inv)) {
return;
}
// Done checks to make sure it's a verify gui. We can now do the work required.
int slot = e.getSlot();
// Make sure you're not clicking anywhere except verify items.
e.setCancelled(true);
if (!filler.contains(slot)) {
return;
}
Main.captchaResult(p, itm.getType());
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent e) {
HumanEntity hm = e.getPlayer();
if (!(hm instanceof Player)) {
return;
}
Player p = (Player) hm;
Inventory invent = e.getInventory();
if (!invs.containsKey(p.getUniqueId())) {
return;
}
if (!invent.equals(invs.get(p.getUniqueId()).inv)) {
return;
}
Bukkit.getScheduler().runTaskLater(Main.p, () -> p.openInventory(CaptchaGUI.getInventory(p, -1)), 5);
}
public static void setBorders(Inventory inv) {
ItemStack pane = new ItemBuilder(Main.config, "settings.captcha.gui.glass-pane").build();
for (int i = 4; i <= 49; i += 9) {
inv.setItem(i - 4, pane);
inv.setItem(i + 4, pane);
}
for (int i = 1; i <= 7; i++) {
inv.setItem(i, pane);
inv.setItem(i + 45, pane);
}
inv.setItem(12, pane);
inv.setItem(13, pane);
inv.setItem(14, pane);
}
public static void setRandomExample(Inventory inv, CaptchaData cd) {
ItemStack itm = new ItemBuilder(Main.config, "settings.captcha.gui.example").setType(cd.example.name()).build();
inv.setItem(4, itm);
}
public static void setRandomAnswers(Inventory inv, CaptchaData cd) {
List<Material> mats = cd.category.getShuffledFiller();
for (int i = 0; i < filler.size(); i++) {
Material mat = mats.get(i);
int slot = filler.get(i);
ItemStack itm = prepareItemStack(mat);
inv.setItem(slot, itm);
}
inv.setItem(filler.get(sr.nextInt(filler.size())), prepareItemStack(cd.category.getRandom(cd.example)));
}
private static ItemStack prepareItemStack(Material mat) {
ItemStack itm = new ItemBuilder(Main.config, "settings.captcha.gui.item").setType(mat.name()).build();
return itm;
}
}
package com.entryrise.afkguard.antiafk.captcha;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SplittableRandom;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
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.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import com.entryrise.afkguard.Main;
import com.entryrise.afkguard.mineutils.ItemBuilder;
public class CaptchaGUI implements Listener {
public static List<Integer> filler = new ArrayList<Integer>();
private static SplittableRandom sr = new SplittableRandom();
// Fillers where selector items can stay;
static {
for (int i = 19; i < 26; i++) {
filler.add(i);
}
for (int i = 28; i < 35; i++) {
filler.add(i);
}
for (int i = 37; i < 44; i++) {
filler.add(i);
}
filler.add(10);
filler.add(11);
filler.add(15);
filler.add(16);
}
public static class CaptchaData {
public CaptchaCategory category;
public ItemStack example;
public Inventory inv;
public int counter;
public CaptchaData() {
this.category = CaptchaCategory.getCategory(null);
this.example = category.getRandom(null);
}
}
public static Map<UUID, CaptchaData> invs = new HashMap<UUID, CaptchaData>();
public static Inventory getInventory(Player p, int counter) {
return getInventory(p.getUniqueId(), counter);
}
public static boolean existsAlready(Player p) {
return existsAlready(p.getUniqueId());
}
public static boolean existsAlready(UUID u) {
return invs.containsKey(u);
}
public static Inventory getInventory(UUID u, int counter) {
if (existsAlready(u)) {
return invs.get(u).inv;
} else if (counter != -1) {
return createInventory(u, counter);
}
return null;
}
private static Inventory createInventory(UUID u, int counter) {
CaptchaData cd = new CaptchaData();
Inventory inv = Bukkit.createInventory(null, 54, getTitle(cd.category.getName()));
setBorders(inv);
setRandomExample(inv, cd);
setRandomAnswers(inv, cd);
cd.inv = inv;
cd.counter = counter;
invs.put(u, cd);
return inv;
}
public static String getTitle(String categoryname) {
return ChatColor.translateAlternateColorCodes('&', Main.config.getString("settings.captcha.gui.title"))
.replace("%category%", categoryname);
}
@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
HumanEntity hm = e.getWhoClicked();
if (!(hm instanceof Player)) {
return;
}
Player p = (Player) hm;
Inventory invent = e.getInventory();
ItemStack itm = e.getCurrentItem();
if (itm == null) {
return;
}
if (!invs.containsKey(p.getUniqueId())) {
return;
}
if (!invent.equals(invs.get(p.getUniqueId()).inv)) {
return;
}
// Done checks to make sure it's a verify gui. We can now do the work required.
int slot = e.getSlot();
// Make sure you're not clicking anywhere except verify items.
e.setCancelled(true);
if (!filler.contains(slot)) {
return;
}
Main.captchaResult(p, itm);
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent e) {
HumanEntity hm = e.getPlayer();
if (!(hm instanceof Player)) {
return;
}
Player p = (Player) hm;
Inventory invent = e.getInventory();
if (!invs.containsKey(p.getUniqueId())) {
return;
}
if (!invent.equals(invs.get(p.getUniqueId()).inv)) {
return;
}
Bukkit.getScheduler().runTaskLater(Main.p, () -> p.openInventory(CaptchaGUI.getInventory(p, -1)), 5);
}
public static void setBorders(Inventory inv) {
ItemStack pane = new ItemBuilder(Main.config, "settings.captcha.gui.glass-pane").build();
for (int i = 4; i <= 49; i += 9) {
inv.setItem(i - 4, pane);
inv.setItem(i + 4, pane);
}
for (int i = 1; i <= 7; i++) {
inv.setItem(i, pane);
inv.setItem(i + 45, pane);
}
inv.setItem(12, pane);
inv.setItem(13, pane);
inv.setItem(14, pane);
}
public static void setRandomExample(Inventory inv, CaptchaData cd) {
ItemStack itm = new ItemBuilder(Main.config, "settings.captcha.gui.example").build();
ItemMeta imeta = itm.getItemMeta();
itm = cd.example.clone();
itm.setItemMeta(imeta);
inv.setItem(4, itm);
}
public static void setRandomAnswers(Inventory inv, CaptchaData cd) {
List<ItemStack> mats = cd.category.getShuffledFiller();
for (int i = 0; i < filler.size(); i++) {
ItemStack mat = mats.get(i);
int slot = filler.get(i);
//
// ItemStack itm = prepareItemStack(mat);
inv.setItem(slot, mat);
}
inv.setItem(filler.get(sr.nextInt(filler.size())), cd.category.getRandom(cd.example));
}
// private static ItemStack prepareItemStack(ItemStack type) {
// ItemStack itm = new ItemBuilder(Main.config, "settings.captcha.gui.item").build();
// ItemMeta imeta = itm.getItemMeta();
// itm = type.clone();
// itm.setItemMeta(imeta);
// return itm;
// }
}

Loading…
Cancel
Save