Files
src
build.gradle
Code
21 1,877
src/main/java/com/guardarmy/GuardArmy.java
package com.guardarmy;
import com.guardarmy.commands.GuardCommand;
import com.guardarmy.inventory.gui.GUIListener;
import com.guardarmy.inventory.gui.GUIManager;
import com.guardarmy.listeners.CraftListener;
import com.guardarmy.listeners.GuardListener;
import com.guardarmy.managers.ConfigManager;
import com.guardarmy.managers.DataManager;
import com.guardarmy.managers.GuardAIManager;
import com.guardarmy.managers.GuardManager;
import com.guardarmy.models.GuardType;
import com.cryptomorin.xseries.XMaterial;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
@Getter
public class GuardArmy extends JavaPlugin implements Listener {
private ConfigManager configManager;
private DataManager dataManager;
private GuardManager guardManager;
private GuardAIManager guardAIManager;
private GUIManager guiManager;
private NamespacedKey guardTypeKey;
@Override
public void onEnable() {
this.guardTypeKey = new NamespacedKey(this, "guard_spawn_type");
this.configManager = new ConfigManager(this);
this.dataManager = new DataManager(this);
this.guardManager = new GuardManager(this, configManager, dataManager);
this.guardAIManager = new GuardAIManager(this, guardManager, configManager);
this.guiManager = new GUIManager();
registerCommands();
registerListeners();
getLogger().info("GuardArmy has been enabled!");
getLogger().info("Version: " + getDescription().getVersion());
}
@Override
public void onDisable() {
if (guardManager != null) {
guardManager.clearAll();
}
if (dataManager != null) {
dataManager.clearAll();
}
getLogger().info("GuardArmy has been disabled!");
}
private void registerCommands() {
GuardCommand guardCommand = new GuardCommand(this, guiManager, guardManager);
getCommand("guard").setExecutor(guardCommand);
getCommand("guard").setTabCompleter(guardCommand);
}
private void registerListeners() {
GUIListener guiListener = new GUIListener(guiManager);
Bukkit.getPluginManager().registerEvents(guiListener, this);
GuardListener guardListener = new GuardListener(this, guardManager, guiManager);
Bukkit.getPluginManager().registerEvents(guardListener, this);
CraftListener craftListener = new CraftListener(this, configManager);
Bukkit.getPluginManager().registerEvents(craftListener, this);
Bukkit.getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.RIGHT_CLICK_AIR) {
return;
}
Player player = event.getPlayer();
ItemStack item = event.getItem();
if (item == null || item.getItemMeta() == null) {
return;
}
if (!item.getItemMeta().getPersistentDataContainer().has(guardTypeKey, PersistentDataType.STRING)) {
return;
}
String typeString = item.getItemMeta().getPersistentDataContainer()
.get(guardTypeKey, PersistentDataType.STRING);
if (typeString == null) {
return;
}
GuardType type;
try {
type = GuardType.valueOf(typeString);
} catch (IllegalArgumentException e) {
return;
}
if (!player.hasPermission("guardarmy.spawn")) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&',
configManager.getMessage("no-permission")));
event.setCancelled(true);
return;
}
guardManager.spawnGuard(player, type, player.getLocation());
item.setAmount(item.getAmount() - 1);
event.setCancelled(true);
}
}