Files
src
pom.xml
Code
4 247
src/main/java/pl/sigma/afknpc/SigmaAfkNpc.java
package pl.sigma.afknpc;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCLeftClickEvent;
import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.trait.SkinTrait;
import net.ess3.api.IEssentials;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public final class SigmaAfkNpc extends JavaPlugin implements Listener {
private NPC npc;
@Override
public void onEnable() {
saveDefaultConfig();
getServer().getPluginManager().registerEvents(this, this);
getServer().getScheduler().runTask(this, this::restoreNpc);
}
@Override
public void onDisable() {
if (npc != null) {
npc.destroy();
npc = null;
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!sender.hasPermission("sigmaafk.admin")) {
sender.sendMessage("§cNie masz uprawnień do tej komendy.");
return true;
}
if (command.getName().equalsIgnoreCase("spawnafk")) {
if (!(sender instanceof Player player)) {
sender.sendMessage("§cTej komendy może użyć tylko gracz.");
return true;
}
if (hasSavedLocation() || npc != null) {
sender.sendMessage("§eNPC AFK już istnieje. Najpierw użyj /usunafk.");
return true;
}
Location location = player.getLocation();
if (!createNpc(location)) {
sender.sendMessage("§cNie udało się utworzyć NPC AFK. Sprawdź konsolę.");
return true;
}
getConfig().set("npc.world", location.getWorld().getName());
getConfig().set("npc.x", location.getX());
getConfig().set("npc.y", location.getY());
getConfig().set("npc.z", location.getZ());
getConfig().set("npc.yaw", location.getYaw());
getConfig().set("npc.pitch", location.getPitch());
saveConfig();
sender.sendMessage("§aUtworzono NPC AFK.");
return true;
}
if (command.getName().equalsIgnoreCase("usunafk")) {
if (npc == null && !hasSavedLocation()) {
sender.sendMessage("§eNPC AFK nie istnieje.");
return true;
}
if (npc != null) {
npc.destroy();
npc = null;
}
getConfig().set("npc.world", "");
saveConfig();
sender.sendMessage("§aUsunięto NPC AFK.");
return true;
}
return false;
}
private boolean hasSavedLocation() {
return !getConfig().getString("npc.world", "").isBlank();
}
private void restoreNpc() {
if (npc != null || !hasSavedLocation()) {
return;
}
String worldName = getConfig().getString("npc.world", "");
World world = Bukkit.getWorld(worldName);
if (world == null) {
getLogger().warning("Świat NPC AFK nie jest załadowany: " + worldName);
return;
}
Location location = new Location(world, getConfig().getDouble("npc.x"), getConfig().getDouble("npc.y"),
getConfig().getDouble("npc.z"), (float) getConfig().getDouble("npc.yaw"),
(float) getConfig().getDouble("npc.pitch"));
if (!createNpc(location)) {
getLogger().warning("Nie udało się odtworzyć NPC AFK.");
}
}
private boolean createNpc(Location location) {
NPC created = CitizensAPI.getTemporaryNPCRegistry().createNPC(EntityType.PLAYER,
ChatColor.translateAlternateColorCodes('&', getConfig().getString("npc.name", "&d&lAFK")));
created.setProtected(true);
created.setUseMinecraftAI(false);
created.getOrAddTrait(SkinTrait.class).setSkinName(getConfig().getString("npc.skin", "CanadianMilo"));
if (!created.spawn(location)) {
created.destroy();
return false;
}
created.getEntity().setGravity(false);
if (created.getEntity() instanceof org.bukkit.entity.LivingEntity living) {
living.setCollidable(false);
}
npc = created;
return true;
}
@EventHandler
public void onWorldLoad(WorldLoadEvent event) {
if (event.getWorld().getName().equals(getConfig().getString("npc.world", ""))) {
getServer().getScheduler().runTask(this, this::restoreNpc);
}
}
@EventHandler
public void onRightClick(NPCRightClickEvent event) {
if (event.getNPC().equals(npc)) {
teleport(event.getClicker());
}
}
@EventHandler
public void onLeftClick(NPCLeftClickEvent event) {
if (event.getNPC().equals(npc)) {
teleport(event.getClicker());
}
}
@EventHandler
public void onDamage(EntityDamageEvent event) {
if (npc != null && npc.isSpawned() && event.getEntity().equals(npc.getEntity())) {
event.setCancelled(true);
}
}
private void teleport(Player player) {
String command = getConfig().getString("npc.teleport-command", "warp afk").trim();
if (command.startsWith("/")) {
command = command.substring(1).trim();
}
String[] parts = command.split("\\s+");
if (parts.length == 2 && parts[0].equalsIgnoreCase("warp")) {
Plugin plugin = getServer().getPluginManager().getPlugin("Essentials");
if (!(plugin instanceof IEssentials essentials) || !plugin.isEnabled()) {
player.sendMessage("§cEssentialsX jest niedostępny. Skontaktuj się z administracją.");
return;
}
try {
Location destination = essentials.getWarps().getWarp(parts[1]);
if (destination == null || destination.getWorld() == null) {
player.sendMessage("§cWarp " + parts[1] + " nie istnieje lub jego świat jest niedostępny.");
return;
}
if (player.teleport(destination)) {
player.sendMessage("§aPrzeniesiono na warp " + parts[1] + ".");
} else {
player.sendMessage("§cTeleportacja została zablokowana.");
}
} catch (Exception exception) {
player.sendMessage("§cWarp " + parts[1] + " nie istnieje lub jego świat jest niedostępny.");
}
} else if (command.isEmpty() || !player.performCommand(command)) {
player.sendMessage("§cNie udało się wykonać komendy teleportacji. Skontaktuj się z administracją.");
}
}
}