2017-12-30 08:36:36 +01:00
|
|
|
package net.citizensnpcs;
|
|
|
|
|
2018-06-07 10:59:52 +02:00
|
|
|
import java.io.File;
|
2021-02-04 03:10:48 +01:00
|
|
|
import java.util.Arrays;
|
2018-06-07 10:59:52 +02:00
|
|
|
import java.util.Iterator;
|
2020-07-18 15:10:55 +02:00
|
|
|
import java.util.List;
|
2018-06-07 10:59:52 +02:00
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
2019-02-10 05:17:15 +01:00
|
|
|
import java.util.UUID;
|
2018-06-07 10:59:52 +02:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.command.CommandSender;
|
2022-06-11 19:37:38 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2021-07-18 13:30:45 +02:00
|
|
|
import org.bukkit.event.HandlerList;
|
2022-06-11 19:37:38 +02:00
|
|
|
import org.bukkit.event.inventory.InventoryType;
|
2022-06-12 13:49:21 +02:00
|
|
|
import org.bukkit.inventory.Inventory;
|
2022-06-11 19:37:38 +02:00
|
|
|
import org.bukkit.inventory.InventoryView;
|
2019-02-05 11:12:02 +01:00
|
|
|
import org.bukkit.inventory.meta.SkullMeta;
|
2018-06-07 10:59:52 +02:00
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
2017-12-30 08:36:36 +01:00
|
|
|
import com.google.common.collect.Iterables;
|
2020-07-18 15:18:53 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2017-12-30 08:36:36 +01:00
|
|
|
import com.google.common.collect.Maps;
|
2019-02-05 11:12:02 +01:00
|
|
|
import com.mojang.authlib.GameProfile;
|
|
|
|
import com.mojang.authlib.properties.Property;
|
2018-06-07 10:59:52 +02:00
|
|
|
|
2022-11-13 05:50:45 +01:00
|
|
|
import net.byteflux.libby.BukkitLibraryManager;
|
|
|
|
import net.byteflux.libby.Library;
|
2022-11-13 09:02:17 +01:00
|
|
|
import net.byteflux.libby.LibraryManager;
|
|
|
|
import net.byteflux.libby.logging.LogLevel;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.Settings.Setting;
|
|
|
|
import net.citizensnpcs.api.CitizensAPI;
|
|
|
|
import net.citizensnpcs.api.CitizensPlugin;
|
2022-06-11 19:37:38 +02:00
|
|
|
import net.citizensnpcs.api.InventoryHelper;
|
2019-02-05 11:12:02 +01:00
|
|
|
import net.citizensnpcs.api.SkullMetaProvider;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.api.ai.speech.SpeechFactory;
|
|
|
|
import net.citizensnpcs.api.command.CommandManager;
|
|
|
|
import net.citizensnpcs.api.command.Injector;
|
2018-06-07 10:59:52 +02:00
|
|
|
import net.citizensnpcs.api.event.CitizensDisableEvent;
|
|
|
|
import net.citizensnpcs.api.event.CitizensEnableEvent;
|
|
|
|
import net.citizensnpcs.api.event.CitizensPreReloadEvent;
|
|
|
|
import net.citizensnpcs.api.event.CitizensReloadEvent;
|
|
|
|
import net.citizensnpcs.api.event.DespawnReason;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.api.exception.NPCLoadException;
|
|
|
|
import net.citizensnpcs.api.npc.NPCDataStore;
|
|
|
|
import net.citizensnpcs.api.npc.NPCRegistry;
|
|
|
|
import net.citizensnpcs.api.npc.SimpleNPCDataStore;
|
|
|
|
import net.citizensnpcs.api.scripting.EventRegistrar;
|
|
|
|
import net.citizensnpcs.api.scripting.ObjectProvider;
|
|
|
|
import net.citizensnpcs.api.scripting.ScriptCompiler;
|
|
|
|
import net.citizensnpcs.api.trait.TraitFactory;
|
2018-06-07 10:59:52 +02:00
|
|
|
import net.citizensnpcs.api.util.Messaging;
|
|
|
|
import net.citizensnpcs.api.util.NBTStorage;
|
|
|
|
import net.citizensnpcs.api.util.Storage;
|
|
|
|
import net.citizensnpcs.api.util.Translator;
|
|
|
|
import net.citizensnpcs.api.util.YamlStorage;
|
|
|
|
import net.citizensnpcs.commands.AdminCommands;
|
|
|
|
import net.citizensnpcs.commands.EditorCommands;
|
|
|
|
import net.citizensnpcs.commands.NPCCommands;
|
|
|
|
import net.citizensnpcs.commands.TemplateCommands;
|
|
|
|
import net.citizensnpcs.commands.TraitCommands;
|
|
|
|
import net.citizensnpcs.commands.WaypointCommands;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.editor.Editor;
|
|
|
|
import net.citizensnpcs.npc.CitizensNPCRegistry;
|
|
|
|
import net.citizensnpcs.npc.CitizensTraitFactory;
|
|
|
|
import net.citizensnpcs.npc.NPCSelector;
|
|
|
|
import net.citizensnpcs.npc.ai.speech.CitizensSpeechFactory;
|
|
|
|
import net.citizensnpcs.npc.profile.ProfileFetcher;
|
|
|
|
import net.citizensnpcs.npc.skin.Skin;
|
2022-07-26 17:50:28 +02:00
|
|
|
import net.citizensnpcs.trait.ShopTrait;
|
2018-06-07 10:59:52 +02:00
|
|
|
import net.citizensnpcs.util.Messages;
|
|
|
|
import net.citizensnpcs.util.NMS;
|
|
|
|
import net.citizensnpcs.util.PlayerUpdateTask;
|
|
|
|
import net.citizensnpcs.util.Util;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.milkbowl.vault.economy.Economy;
|
|
|
|
|
|
|
|
public class Citizens extends JavaPlugin implements CitizensPlugin {
|
2020-07-18 15:18:53 +02:00
|
|
|
private final List<NPCRegistry> anonymousRegistries = Lists.newArrayList();
|
2021-02-04 03:10:48 +01:00
|
|
|
private final List<NPCRegistry> citizensBackedRegistries = Lists.newArrayList();
|
2017-12-30 08:36:36 +01:00
|
|
|
private final CommandManager commands = new CommandManager();
|
|
|
|
private Settings config;
|
2021-07-15 13:41:03 +02:00
|
|
|
private boolean enabled;
|
2022-06-11 19:37:38 +02:00
|
|
|
private final InventoryHelper inventoryHelper = new InventoryHelper() {
|
2022-06-12 13:49:21 +02:00
|
|
|
@Override
|
|
|
|
public InventoryView openAnvilInventory(Player player, Inventory inventory, String title) {
|
|
|
|
return NMS.openAnvilInventory(player, inventory, title);
|
|
|
|
}
|
|
|
|
|
2022-06-11 19:37:38 +02:00
|
|
|
@Override
|
|
|
|
public void updateInventoryTitle(Player player, InventoryView view, String newTitle) {
|
|
|
|
if (view.getTopInventory().getType() == InventoryType.CRAFTING
|
|
|
|
|| view.getTopInventory().getType() == InventoryType.CREATIVE
|
|
|
|
|| view.getTopInventory().getType() == InventoryType.PLAYER)
|
|
|
|
return;
|
|
|
|
NMS.updateInventoryTitle(player, view, newTitle);
|
|
|
|
}
|
|
|
|
};
|
2017-12-30 08:36:36 +01:00
|
|
|
private CitizensNPCRegistry npcRegistry;
|
2022-11-16 14:58:05 +01:00
|
|
|
private ProtocolLibListener protocolListener;
|
2021-07-21 12:51:04 +02:00
|
|
|
private boolean saveOnDisable = true;
|
2017-12-30 08:36:36 +01:00
|
|
|
private NPCDataStore saves;
|
|
|
|
private NPCSelector selector;
|
2022-07-26 17:50:28 +02:00
|
|
|
private Storage shops;
|
2019-02-05 11:12:02 +01:00
|
|
|
private final SkullMetaProvider skullMetaProvider = new SkullMetaProvider() {
|
|
|
|
@Override
|
|
|
|
public String getTexture(SkullMeta meta) {
|
2019-02-10 05:17:15 +01:00
|
|
|
if (NMS.getProfile(meta) == null)
|
|
|
|
return null;
|
2019-02-05 11:12:02 +01:00
|
|
|
return Iterables.getFirst(NMS.getProfile(meta).getProperties().get("textures"), new Property("", ""))
|
|
|
|
.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setTexture(String string, SkullMeta meta) {
|
2019-02-10 05:17:15 +01:00
|
|
|
UUID uuid = meta.getOwningPlayer() == null ? UUID.randomUUID() : meta.getOwningPlayer().getUniqueId();
|
|
|
|
NMS.setProfile(meta, new GameProfile(uuid, string));
|
2019-02-05 11:12:02 +01:00
|
|
|
}
|
|
|
|
};
|
2017-12-30 08:36:36 +01:00
|
|
|
private CitizensSpeechFactory speechFactory;
|
|
|
|
private final Map<String, NPCRegistry> storedRegistries = Maps.newHashMap();
|
|
|
|
private CitizensTraitFactory traitFactory;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NPCRegistry createAnonymousNPCRegistry(NPCDataStore store) {
|
2020-07-18 15:10:55 +02:00
|
|
|
CitizensNPCRegistry anon = new CitizensNPCRegistry(store, "anonymous-" + UUID.randomUUID().toString());
|
|
|
|
anonymousRegistries.add(anon);
|
|
|
|
return anon;
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 03:10:48 +01:00
|
|
|
@Override
|
|
|
|
public NPCRegistry createCitizensBackedNPCRegistry(NPCDataStore store) {
|
|
|
|
CitizensNPCRegistry anon = new CitizensNPCRegistry(store, "anonymous-citizens-" + UUID.randomUUID().toString());
|
|
|
|
citizensBackedRegistries.add(anon);
|
|
|
|
return anon;
|
|
|
|
}
|
|
|
|
|
2017-12-30 08:36:36 +01:00
|
|
|
@Override
|
|
|
|
public NPCRegistry createNamedNPCRegistry(String name, NPCDataStore store) {
|
2020-07-17 03:52:00 +02:00
|
|
|
NPCRegistry created = new CitizensNPCRegistry(store, name);
|
2017-12-30 08:36:36 +01:00
|
|
|
storedRegistries.put(name, created);
|
|
|
|
return created;
|
|
|
|
}
|
|
|
|
|
|
|
|
private NPCDataStore createStorage(File folder) {
|
|
|
|
Storage saves = null;
|
|
|
|
String type = Setting.STORAGE_TYPE.asString();
|
|
|
|
if (type.equalsIgnoreCase("nbt")) {
|
|
|
|
saves = new NBTStorage(new File(folder + File.separator + Setting.STORAGE_FILE.asString()),
|
|
|
|
"Citizens NPC Storage");
|
|
|
|
}
|
|
|
|
if (saves == null) {
|
|
|
|
saves = new YamlStorage(new File(folder, Setting.STORAGE_FILE.asString()), "Citizens NPC Storage");
|
|
|
|
}
|
|
|
|
if (!saves.load())
|
|
|
|
return null;
|
|
|
|
return SimpleNPCDataStore.create(saves);
|
|
|
|
}
|
|
|
|
|
2021-02-04 03:10:48 +01:00
|
|
|
private void despawnNPCs(boolean save) {
|
2022-03-09 20:17:27 +01:00
|
|
|
for (NPCRegistry registry : Iterables.concat(Arrays.asList(npcRegistry), citizensBackedRegistries)) {
|
|
|
|
if (registry == null)
|
|
|
|
continue;
|
2021-02-04 03:10:48 +01:00
|
|
|
if (save) {
|
2022-03-09 20:17:27 +01:00
|
|
|
registry.saveToStore();
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
2022-03-09 20:17:27 +01:00
|
|
|
registry.despawnNPCs(DespawnReason.RELOAD);
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void enableSubPlugins() {
|
|
|
|
File root = new File(getDataFolder(), Setting.SUBPLUGIN_FOLDER.asString());
|
|
|
|
if (!root.exists() || !root.isDirectory())
|
|
|
|
return;
|
|
|
|
File[] files = root.listFiles();
|
|
|
|
for (File file : files) {
|
|
|
|
Plugin plugin;
|
|
|
|
try {
|
|
|
|
plugin = Bukkit.getPluginManager().loadPlugin(file);
|
|
|
|
} catch (Exception e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (plugin == null)
|
|
|
|
continue;
|
|
|
|
// code beneath modified from CraftServer
|
|
|
|
try {
|
|
|
|
Messaging.logTr(Messages.LOADING_SUB_PLUGIN, plugin.getDescription().getFullName());
|
|
|
|
plugin.onLoad();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Messaging.severeTr(Messages.ERROR_INITALISING_SUB_PLUGIN, ex.getMessage(),
|
|
|
|
plugin.getDescription().getFullName());
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NMS.loadPlugins();
|
|
|
|
}
|
|
|
|
|
2022-09-07 21:01:39 +02:00
|
|
|
@Override
|
|
|
|
public CommandManager getCommandManager() {
|
|
|
|
return commands;
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public net.citizensnpcs.api.npc.NPCSelector getDefaultNPCSelector() {
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
2022-06-11 19:37:38 +02:00
|
|
|
@Override
|
|
|
|
public InventoryHelper getInventoryHelper() {
|
|
|
|
return inventoryHelper;
|
|
|
|
}
|
|
|
|
|
2017-12-30 08:36:36 +01:00
|
|
|
@Override
|
|
|
|
public NPCRegistry getNamedNPCRegistry(String name) {
|
2020-07-17 03:52:00 +02:00
|
|
|
if (name.equals(npcRegistry.getName()))
|
|
|
|
return npcRegistry;
|
2017-12-30 08:36:36 +01:00
|
|
|
return storedRegistries.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterable<NPCRegistry> getNPCRegistries() {
|
|
|
|
return new Iterable<NPCRegistry>() {
|
|
|
|
@Override
|
|
|
|
public Iterator<NPCRegistry> iterator() {
|
|
|
|
return new Iterator<NPCRegistry>() {
|
|
|
|
Iterator<NPCRegistry> stored;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNext() {
|
|
|
|
return stored == null ? true : stored.hasNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NPCRegistry next() {
|
|
|
|
if (stored == null) {
|
2021-02-04 03:10:48 +01:00
|
|
|
stored = Iterables
|
|
|
|
.concat(storedRegistries.values(), anonymousRegistries, citizensBackedRegistries)
|
|
|
|
.iterator();
|
2017-12-30 08:36:36 +01:00
|
|
|
return npcRegistry;
|
|
|
|
}
|
|
|
|
return stored.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NPCRegistry getNPCRegistry() {
|
|
|
|
return npcRegistry;
|
|
|
|
}
|
|
|
|
|
|
|
|
public NPCSelector getNPCSelector() {
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ClassLoader getOwningClassLoader() {
|
|
|
|
return getClassLoader();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public File getScriptFolder() {
|
|
|
|
return new File(getDataFolder(), "scripts");
|
|
|
|
}
|
|
|
|
|
2019-02-05 11:12:02 +01:00
|
|
|
@Override
|
|
|
|
public SkullMetaProvider getSkullMetaProvider() {
|
|
|
|
return skullMetaProvider;
|
|
|
|
}
|
|
|
|
|
2017-12-30 08:36:36 +01:00
|
|
|
@Override
|
|
|
|
public SpeechFactory getSpeechFactory() {
|
|
|
|
return speechFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TraitFactory getTraitFactory() {
|
|
|
|
return traitFactory;
|
|
|
|
}
|
|
|
|
|
2022-11-13 05:50:45 +01:00
|
|
|
private void loadMavenLibraries() {
|
2022-11-14 16:53:14 +01:00
|
|
|
getLogger().info("Loading external libraries");
|
2022-11-13 12:32:02 +01:00
|
|
|
|
2022-11-13 09:02:17 +01:00
|
|
|
LibraryManager lib = new BukkitLibraryManager(this);
|
2022-11-13 05:50:45 +01:00
|
|
|
lib.addMavenCentral();
|
2022-11-13 09:02:17 +01:00
|
|
|
lib.setLogLevel(LogLevel.WARN);
|
2022-11-13 05:50:45 +01:00
|
|
|
// Unfortunately, transitive dependency management is not supported in this library.
|
|
|
|
lib.loadLibrary(Library.builder().groupId("ch{}ethz{}globis{}phtree").artifactId("phtree").version("2.5.0")
|
|
|
|
.relocate("ch{}ethz{}globis{}phtree", "clib{}phtree").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}sf{}trove4j").artifactId("trove4j").version("3.0.3")
|
|
|
|
.relocate("gnu{}trove", "clib{}trove").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-text-minimessage")
|
|
|
|
.version("4.11.0").relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-platform-bukkit").version("4.1.2")
|
|
|
|
.relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-platform-api").version("4.1.2")
|
|
|
|
.relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-platform-facet").version("4.1.2")
|
|
|
|
.relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-platform-viaversion")
|
|
|
|
.version("4.1.2").relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-api").version("4.11.0")
|
|
|
|
.relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-text-serializer-bungeecord")
|
|
|
|
.version("4.1.2").relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-text-serializer-legacy")
|
|
|
|
.version("4.11.0").relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-text-serializer-gson")
|
|
|
|
.version("4.11.0").relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-text-serializer-gson-legacy-impl")
|
|
|
|
.version("4.11.0").relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-nbt").version("4.11.0")
|
|
|
|
.relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-key").version("4.11.0")
|
|
|
|
.relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("examination-api").version("1.3.0")
|
|
|
|
.relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("examination-string").version("1.3.0")
|
|
|
|
.relocate("net{}kyori", "clib{}net{}kyori").build());
|
|
|
|
}
|
|
|
|
|
2017-12-30 08:36:36 +01:00
|
|
|
@Override
|
|
|
|
public boolean onCommand(CommandSender sender, org.bukkit.command.Command command, String cmdName, String[] args) {
|
2022-09-07 21:01:39 +02:00
|
|
|
// TODO: use injector?
|
|
|
|
Object[] methodArgs = { sender, selector == null ? null : selector.getSelected(sender) };
|
2017-12-30 08:36:36 +01:00
|
|
|
return commands.executeSafe(command, args, sender, methodArgs);
|
|
|
|
}
|
|
|
|
|
2021-07-25 02:52:03 +02:00
|
|
|
public void onDependentPluginDisable() {
|
2022-07-26 17:50:28 +02:00
|
|
|
storeNPCs(false);
|
2021-07-25 02:52:03 +02:00
|
|
|
saveOnDisable = false;
|
|
|
|
}
|
|
|
|
|
2017-12-30 08:36:36 +01:00
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
2022-07-22 09:22:55 +02:00
|
|
|
if (!enabled) {
|
2021-07-15 13:41:03 +02:00
|
|
|
return;
|
2022-07-22 09:22:55 +02:00
|
|
|
}
|
2021-07-15 13:41:03 +02:00
|
|
|
Bukkit.getPluginManager().callEvent(new CitizensDisableEvent());
|
|
|
|
Editor.leaveAll();
|
2021-07-21 12:51:04 +02:00
|
|
|
despawnNPCs(saveOnDisable);
|
2021-07-18 13:30:45 +02:00
|
|
|
HandlerList.unregisterAll(this);
|
2021-07-15 13:41:03 +02:00
|
|
|
npcRegistry = null;
|
|
|
|
enabled = false;
|
2021-07-21 12:51:04 +02:00
|
|
|
saveOnDisable = true;
|
2021-07-25 02:52:03 +02:00
|
|
|
NMS.shutdown();
|
2021-07-15 13:41:03 +02:00
|
|
|
CitizensAPI.shutdown();
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
2022-11-13 05:50:45 +01:00
|
|
|
loadMavenLibraries();
|
|
|
|
|
2018-06-29 16:35:13 +02:00
|
|
|
CitizensAPI.setImplementation(this);
|
2018-06-07 10:59:52 +02:00
|
|
|
config = new Settings(getDataFolder());
|
2017-12-30 08:36:36 +01:00
|
|
|
setupTranslator();
|
|
|
|
// Disable if the server is not using the compatible Minecraft version
|
|
|
|
String mcVersion = Util.getMinecraftRevision();
|
|
|
|
try {
|
|
|
|
NMS.loadBridge(mcVersion);
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (Messaging.isDebugging()) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
Messaging.severeTr(Messages.CITIZENS_INCOMPATIBLE, getDescription().getVersion(), mcVersion);
|
2021-07-15 13:41:03 +02:00
|
|
|
enabled = true;
|
2020-04-30 18:26:17 +02:00
|
|
|
Bukkit.getPluginManager().disablePlugin(this);
|
2017-12-30 08:36:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
registerScriptHelpers();
|
|
|
|
|
|
|
|
saves = createStorage(getDataFolder());
|
2022-07-26 17:50:28 +02:00
|
|
|
shops = new YamlStorage(new File(getDataFolder(), "shops.yml"));
|
|
|
|
if (saves == null || !shops.load()) {
|
2017-12-30 08:36:36 +01:00
|
|
|
Messaging.severeTr(Messages.FAILED_LOAD_SAVES);
|
2020-04-30 18:26:17 +02:00
|
|
|
Bukkit.getPluginManager().disablePlugin(this);
|
2017-12-30 08:36:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-29 16:47:46 +02:00
|
|
|
speechFactory = new CitizensSpeechFactory();
|
2020-07-18 15:10:55 +02:00
|
|
|
npcRegistry = new CitizensNPCRegistry(saves, "citizens");
|
2017-12-30 08:36:36 +01:00
|
|
|
traitFactory = new CitizensTraitFactory();
|
|
|
|
selector = new NPCSelector(this);
|
|
|
|
|
2020-04-30 18:26:17 +02:00
|
|
|
Bukkit.getPluginManager().registerEvents(new EventListen(storedRegistries), this);
|
2022-07-26 21:27:49 +02:00
|
|
|
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
|
|
|
new CitizensPlaceholders(selector).register();
|
|
|
|
}
|
2022-11-16 14:58:05 +01:00
|
|
|
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) {
|
|
|
|
protocolListener = new ProtocolLibListener(this);
|
|
|
|
}
|
2017-12-30 08:36:36 +01:00
|
|
|
|
2022-07-21 17:13:51 +02:00
|
|
|
setupEconomy();
|
2017-12-30 08:36:36 +01:00
|
|
|
|
|
|
|
registerCommands();
|
|
|
|
enableSubPlugins();
|
|
|
|
NMS.load(commands);
|
2020-04-30 18:26:17 +02:00
|
|
|
Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
|
2019-11-12 08:17:54 +01:00
|
|
|
commands.registerTabCompletion(this);
|
2017-12-30 08:36:36 +01:00
|
|
|
|
|
|
|
// Setup NPCs after all plugins have been enabled (allows for multiworld
|
|
|
|
// support and for NPCs to properly register external settings)
|
2020-11-15 13:32:16 +01:00
|
|
|
if (getServer().getScheduler().scheduleSyncDelayedTask(this, new CitizensLoadTask(), 1) == -1) {
|
2017-12-30 08:36:36 +01:00
|
|
|
Messaging.severeTr(Messages.LOAD_TASK_NOT_SCHEDULED);
|
2020-04-30 18:26:17 +02:00
|
|
|
Bukkit.getPluginManager().disablePlugin(this);
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onImplementationChanged() {
|
|
|
|
Messaging.severeTr(Messages.CITIZENS_IMPLEMENTATION_DISABLED);
|
|
|
|
Bukkit.getPluginManager().disablePlugin(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void registerCommandClass(Class<?> clazz) {
|
|
|
|
try {
|
|
|
|
commands.register(clazz);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Messaging.logTr(Messages.CITIZENS_INVALID_COMMAND_CLASS);
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void registerCommands() {
|
|
|
|
commands.setInjector(new Injector(this));
|
|
|
|
// Register command classes
|
|
|
|
commands.register(AdminCommands.class);
|
|
|
|
commands.register(EditorCommands.class);
|
|
|
|
commands.register(NPCCommands.class);
|
|
|
|
commands.register(TemplateCommands.class);
|
|
|
|
commands.register(TraitCommands.class);
|
|
|
|
commands.register(WaypointCommands.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void registerScriptHelpers() {
|
|
|
|
ScriptCompiler compiler = CitizensAPI.getScriptCompiler();
|
|
|
|
compiler.registerGlobalContextProvider(new EventRegistrar(this));
|
|
|
|
compiler.registerGlobalContextProvider(new ObjectProvider("plugin", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reload() throws NPCLoadException {
|
|
|
|
Editor.leaveAll();
|
|
|
|
config.reload();
|
2021-02-04 03:10:48 +01:00
|
|
|
despawnNPCs(false);
|
2017-12-30 08:36:36 +01:00
|
|
|
ProfileFetcher.reset();
|
|
|
|
Skin.clearCache();
|
|
|
|
getServer().getPluginManager().callEvent(new CitizensPreReloadEvent());
|
|
|
|
|
2018-03-23 11:13:51 +01:00
|
|
|
saves.reloadFromSource();
|
2017-12-30 08:36:36 +01:00
|
|
|
saves.loadInto(npcRegistry);
|
|
|
|
|
2022-07-26 17:50:28 +02:00
|
|
|
shops.load();
|
|
|
|
ShopTrait.loadShops(shops.getKey(""));
|
|
|
|
|
2017-12-30 08:36:36 +01:00
|
|
|
getServer().getPluginManager().callEvent(new CitizensReloadEvent());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeNamedNPCRegistry(String name) {
|
|
|
|
storedRegistries.remove(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void scheduleSaveTask(int delay) {
|
2020-11-15 13:32:16 +01:00
|
|
|
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new CitizensSaveTask(), delay, delay);
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
|
|
|
|
2019-01-17 18:55:31 +01:00
|
|
|
@Override
|
|
|
|
public void setDefaultNPCDataStore(NPCDataStore store) {
|
2019-01-17 18:56:47 +01:00
|
|
|
if (store == null) {
|
|
|
|
throw new IllegalArgumentException("must be non-null");
|
|
|
|
}
|
2021-02-04 03:10:48 +01:00
|
|
|
despawnNPCs(true);
|
2019-01-17 18:55:31 +01:00
|
|
|
this.saves = store;
|
2020-07-17 03:52:00 +02:00
|
|
|
this.npcRegistry = new CitizensNPCRegistry(saves, "citizens-global-" + UUID.randomUUID().toString());
|
2021-02-04 03:10:48 +01:00
|
|
|
saves.loadInto(npcRegistry);
|
2019-01-17 18:55:31 +01:00
|
|
|
}
|
|
|
|
|
2017-12-30 08:36:36 +01:00
|
|
|
private void setupEconomy() {
|
|
|
|
try {
|
|
|
|
RegisteredServiceProvider<Economy> provider = Bukkit.getServicesManager().getRegistration(Economy.class);
|
|
|
|
if (provider != null && provider.getProvider() != null) {
|
|
|
|
Economy economy = provider.getProvider();
|
|
|
|
Bukkit.getPluginManager().registerEvents(new PaymentListener(economy), this);
|
|
|
|
}
|
2022-06-08 08:07:33 +02:00
|
|
|
Messaging.logTr(Messages.LOADED_ECONOMY);
|
2017-12-30 08:36:36 +01:00
|
|
|
} catch (NoClassDefFoundError e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setupTranslator() {
|
|
|
|
Locale locale = Locale.getDefault();
|
|
|
|
String setting = Setting.LOCALE.asString();
|
|
|
|
if (!setting.isEmpty()) {
|
|
|
|
String[] parts = setting.split("[\\._]");
|
|
|
|
switch (parts.length) {
|
|
|
|
case 1:
|
|
|
|
locale = new Locale(parts[0]);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
locale = new Locale(parts[0], parts[1]);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
locale = new Locale(parts[0], parts[1], parts[2]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Translator.setInstance(new File(getDataFolder(), "lang"), locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startMetrics() {
|
|
|
|
try {
|
2020-05-06 12:08:12 +02:00
|
|
|
Metrics metrics = new Metrics(this, 2463);
|
2018-04-18 16:22:38 +02:00
|
|
|
|
|
|
|
metrics.addCustomChart(new Metrics.SingleLineChart("total_npcs", new Callable<Integer>() {
|
2017-12-30 08:36:36 +01:00
|
|
|
@Override
|
2018-04-18 16:22:38 +02:00
|
|
|
public Integer call() {
|
2018-06-07 10:59:52 +02:00
|
|
|
if (npcRegistry == null)
|
|
|
|
return 0;
|
2017-12-30 08:36:36 +01:00
|
|
|
return Iterables.size(npcRegistry);
|
|
|
|
}
|
2018-04-18 16:22:38 +02:00
|
|
|
}));
|
2020-05-06 12:08:12 +02:00
|
|
|
/*
|
|
|
|
TODO: not implemented yet
|
|
|
|
metrics.addCustomChart(new Metrics.MultiLineChart("traits", new Callable<Map<String, Integer>>() {
|
|
|
|
@Override
|
|
|
|
public Map<String, Integer> call() throws Exception {
|
|
|
|
return traitFactory.getTraitPlot();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
*/
|
2018-04-18 16:22:38 +02:00
|
|
|
} catch (Exception e) {
|
2017-12-30 08:36:36 +01:00
|
|
|
Messaging.logTr(Messages.METRICS_ERROR_NOTIFICATION, e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void storeNPCs() {
|
2022-07-26 17:50:28 +02:00
|
|
|
storeNPCs(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void storeNPCs(boolean async) {
|
2017-12-30 08:36:36 +01:00
|
|
|
if (saves == null)
|
|
|
|
return;
|
2019-05-24 12:32:51 +02:00
|
|
|
saves.storeAll(npcRegistry);
|
2022-07-26 17:50:28 +02:00
|
|
|
ShopTrait.saveShops(shops.getKey(""));
|
2017-12-30 08:36:36 +01:00
|
|
|
if (async) {
|
|
|
|
saves.saveToDisk();
|
2022-07-26 17:50:28 +02:00
|
|
|
new Thread(() -> {
|
|
|
|
shops.save();
|
|
|
|
}).start();
|
2017-12-30 08:36:36 +01:00
|
|
|
} else {
|
2022-07-26 17:50:28 +02:00
|
|
|
shops.save();
|
2017-12-30 08:36:36 +01:00
|
|
|
saves.saveToDiskImmediate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 13:32:16 +01:00
|
|
|
private class CitizensLoadTask implements Runnable {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
saves.loadInto(npcRegistry);
|
2022-07-26 18:19:47 +02:00
|
|
|
ShopTrait.loadShops(shops.getKey(""));
|
|
|
|
|
2020-11-15 13:32:16 +01:00
|
|
|
Messaging.logTr(Messages.NUM_LOADED_NOTIFICATION, Iterables.size(npcRegistry), "?");
|
|
|
|
startMetrics();
|
|
|
|
scheduleSaveTask(Setting.SAVE_TASK_DELAY.asInt());
|
|
|
|
Bukkit.getPluginManager().callEvent(new CitizensEnableEvent());
|
|
|
|
new PlayerUpdateTask().runTaskTimer(Citizens.this, 0, 1);
|
2021-07-15 13:41:03 +02:00
|
|
|
enabled = true;
|
2020-11-15 13:32:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class CitizensSaveTask implements Runnable {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2022-07-26 17:50:28 +02:00
|
|
|
storeNPCs(false);
|
2020-11-15 13:32:16 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|