Browse Source

Improvements to sub 1.13 support.

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

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

@ -3,13 +3,13 @@ package com.entryrise.afkguard;
import java.io.File; import java.io.File;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -104,7 +104,7 @@ public class Main extends JavaPlugin implements Listener {
AFKManager.setScore(p, target); AFKManager.setScore(p, target);
} }
public static void captchaResult(Player p, Material mat) {
public static void captchaResult(Player p, ItemStack mat) {
CaptchaData cd = CaptchaGUI.invs.get(p.getUniqueId()); CaptchaData cd = CaptchaGUI.invs.get(p.getUniqueId());
if (!cd.category.getAllowed().contains(mat)) { if (!cd.category.getAllowed().contains(mat)) {
failedAttempt(p); failedAttempt(p);

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

@ -6,9 +6,10 @@ import java.util.List;
import java.util.SplittableRandom; import java.util.SplittableRandom;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import com.entryrise.afkguard.Main; import com.entryrise.afkguard.Main;
import com.entryrise.afkguard.mineutils.ItemBuilder;
public class CaptchaCategory { public class CaptchaCategory {
@ -38,8 +39,8 @@ public class CaptchaCategory {
return categ; return categ;
} }
public List<Material> getShuffledFiller() {
List<Material> raw = new ArrayList<Material>();
public List<ItemStack> getShuffledFiller() {
List<ItemStack> raw = new ArrayList<ItemStack>();
for (CaptchaCategory cat : categories) { for (CaptchaCategory cat : categories) {
if (cat.equals(this)) { if (cat.equals(this)) {
continue; continue;
@ -55,7 +56,7 @@ public class CaptchaCategory {
// This is where objective stuff starts and no more statics roam the land. // This is where objective stuff starts and no more statics roam the land.
private List<Material> members = new ArrayList<Material>();
private List<ItemStack> members = new ArrayList<ItemStack>();
private String name; private String name;
private CaptchaCategory(String name) { private CaptchaCategory(String name) {
@ -63,27 +64,23 @@ public class CaptchaCategory {
this.name = name; this.name = name;
for (String stg : Main.config.getStringList(loc)) { for (String stg : Main.config.getStringList(loc)) {
members.add(Material.getMaterial(stg));
members.add(new ItemBuilder(Main.config, "settings.captcha.gui.item").setType(stg).build());
} }
} }
public Material getRandom(Material exception) {
public ItemStack getRandom(ItemStack exception) {
// For safety if the random randomly breaks out of some reason or another. // 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. // 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;
List<ItemStack> items = new ArrayList<>(members);
items.remove(exception);
return items.get(sr.nextInt(items.size()));
} }
public String getName() { public String getName() {
return name; return name;
} }
public List<Material> getAllowed() {
public List<ItemStack> getAllowed() {
return members; return members;
} }

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

@ -9,7 +9,6 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -18,6 +17,7 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import com.entryrise.afkguard.Main; import com.entryrise.afkguard.Main;
import com.entryrise.afkguard.mineutils.ItemBuilder; import com.entryrise.afkguard.mineutils.ItemBuilder;
@ -49,7 +49,7 @@ public class CaptchaGUI implements Listener {
public static class CaptchaData { public static class CaptchaData {
public CaptchaCategory category; public CaptchaCategory category;
public Material example;
public ItemStack example;
public Inventory inv; public Inventory inv;
public int counter; public int counter;
@ -145,7 +145,7 @@ public class CaptchaGUI implements Listener {
return; return;
} }
Main.captchaResult(p, itm.getType());
Main.captchaResult(p, itm);
} }
@ -190,27 +190,33 @@ public class CaptchaGUI implements Listener {
} }
public static void setRandomExample(Inventory inv, CaptchaData cd) { public static void setRandomExample(Inventory inv, CaptchaData cd) {
ItemStack itm = new ItemBuilder(Main.config, "settings.captcha.gui.example").setType(cd.example.name()).build();
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); inv.setItem(4, itm);
} }
public static void setRandomAnswers(Inventory inv, CaptchaData cd) { public static void setRandomAnswers(Inventory inv, CaptchaData cd) {
List<Material> mats = cd.category.getShuffledFiller();
List<ItemStack> mats = cd.category.getShuffledFiller();
for (int i = 0; i < filler.size(); i++) { for (int i = 0; i < filler.size(); i++) {
Material mat = mats.get(i);
ItemStack mat = mats.get(i);
int slot = filler.get(i); int slot = filler.get(i);
ItemStack itm = prepareItemStack(mat);
inv.setItem(slot, itm);
//
// ItemStack itm = prepareItemStack(mat);
inv.setItem(slot, mat);
} }
inv.setItem(filler.get(sr.nextInt(filler.size())), prepareItemStack(cd.category.getRandom(cd.example)));
inv.setItem(filler.get(sr.nextInt(filler.size())), 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;
}
// 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