AFKGuard allows you to detect even the most tricky AFK machines and cheats, by being fully configurable and accessible!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
2.8 KiB

package com.entryrise.afkguard.gui;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
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;
public abstract class GUI implements Listener {
private int size;
private String title;
private boolean persistent;
public Map<UUID, GUIInventory> invs = null;
// Decorate inventory (Borders, blabla)
public abstract void decorateInventory(Inventory inv);
public abstract void setGUIItems(GUIInventory ginv, UUID u);
protected boolean isPersistent() {
return persistent;
}
public GUI getCurrentInstance() {
return this;
}
public GUI(String title, int size, boolean persistent) {
this.title = title;
this.size = size;
this.persistent = persistent;
invs = new HashMap<UUID, GUIInventory>();
Bukkit.getPluginManager().registerEvents(this, Main.p);
}
public void openInventory(Player p) {
p.openInventory(getInventory(p));
}
public Inventory getInventory(Player p) {
UUID u = p.getUniqueId();
if (invs.containsKey(u)) {
return invs.get(u).inv;
}
return createInventory(u);
}
private Inventory createInventory(UUID u) {
Inventory inv = Bukkit.createInventory(null, size, title);
decorateInventory(inv);
GUIInventory ginv = new GUIInventory(inv);
invs.put(u, ginv);
setGUIItems(ginv, u);
return inv;
}
public void refreshInventory(UUID u) {
GUIInventory ginv = invs.get(u);
if (ginv == null) {
return;
}
ginv.reset();
decorateInventory(ginv.inv);
setGUIItems(ginv, u);
}
@EventHandler
public void onClick(InventoryClickEvent e) {
if (e.isCancelled()) {
return;
}
Player p = (Player) e.getWhoClicked();
Inventory inv = e.getClickedInventory();
ItemStack hand = e.getCursor();
UUID u = p.getUniqueId();
if (!invs.containsKey(u)) {
return;
}
GUIInventory ginv = invs.get(u);
if (!ginv.inv.equals(inv)) {
return;
}
// CANCEL ANYTHING
e.setCancelled(true);
GUIItem gitem = ginv.getGUIItem(e.getSlot());
if (gitem == null) {
return;
}
if (e.isRightClick()) {
gitem.onRightClick(p, inv, hand);
}
if (e.isLeftClick()) {
gitem.onLeftClick(p, inv, hand);
}
}
@EventHandler
public void onClose(InventoryCloseEvent e) {
Player p = (Player) e.getPlayer();
Inventory inv = e.getInventory();
UUID u = p.getUniqueId();
if (!invs.containsKey(u)) {
return;
}
GUIInventory ginv = invs.get(u);
if (!ginv.inv.equals(inv)) {
return;
}
if (isPersistent()) {
return;
}
invs.remove(u);
}
}