Skip to content

Commit

Permalink
add a Citizens late-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 15, 2021
1 parent 5ea333a commit 2cdac73
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 41 deletions.
37 changes: 33 additions & 4 deletions plugin/src/main/java/com/denizenscript/denizen/Denizen.java
Expand Up @@ -7,6 +7,7 @@
import com.denizenscript.denizen.events.server.ServerStopScriptEvent;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.InventoryTag;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.objects.notable.NotableManager;
import com.denizenscript.denizen.objects.properties.PropertyRegistry;
Expand All @@ -16,6 +17,7 @@
import com.denizenscript.denizen.scripts.containers.core.*;
import com.denizenscript.denizen.scripts.triggers.TriggerRegistry;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.tags.core.NPCTagBase;
import com.denizenscript.denizen.tags.core.ServerTagBase;
import com.denizenscript.denizen.utilities.*;
import com.denizenscript.denizen.utilities.command.DenizenCommandHandler;
Expand Down Expand Up @@ -175,11 +177,18 @@ else if (javaVersion.startsWith("9") || javaVersion.startsWith("1.9") || javaVer
triggerRegistry = new TriggerRegistry();
notableManager = new NotableManager();
tagManager = new TagManager();
boolean citizensBork = false;
try {
// Activate dependencies
Depends.initialize();
if (Depends.citizens == null || !Depends.citizens.isEnabled()) {
getLogger().warning("Citizens does not seem to be activated! Denizen will have greatly reduced functionality!");
if (Depends.citizens == null) {
if (Bukkit.getPluginManager().getPlugin("Citizens") != null) {
citizensBork = true;
getLogger().warning("Citizens is present but does seem to be activated! You may have an error earlier in your logs, or you may have a broken plugin load order.");
}
else {
getLogger().warning("Citizens does not seem to be available! Denizen will have greatly reduced functionality!");
}
}
startedSuccessful = true;
}
Expand Down Expand Up @@ -383,8 +392,28 @@ else if (javaVersion.startsWith("9") || javaVersion.startsWith("1.9") || javaVer
catch (Throwable ex) {
Debug.echoError(ex);
}
final boolean hadCitizensBork = citizensBork;
// Run everything else on the first server tick
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {
try {
if (hadCitizensBork) {
Depends.setupCitizens();
if (Depends.citizens != null) {
getLogger().warning("Citizens was activated late - this means a plugin load order error occurred. You may have plugins with invalid 'plugin.yml' files (eg that use the 'loadbefore' directive, or that have circular dependencies).");
npcHelper = new DenizenNPCHelper(this);
Depends.citizens.registerCommandClass(NPCCommandHandler.class);
TraitRegistry.registerMainTraits();
triggerRegistry.registerCoreMembers();
commandRegistry.registerCitizensCommands();
ScriptEventRegistry.registerCitizensEvents();
new NPCTagBase();
ObjectFetcher.registerWithObjectFetcher(NPCTag.class, NPCTag.tagProcessor);
}
}
}
catch (Throwable ex) {
Debug.echoError(ex);
}
try {
exCommand.processTagList();
// Reload notables from notables.yml into memory
Expand All @@ -410,8 +439,8 @@ else if (javaVersion.startsWith("9") || javaVersion.startsWith("1.9") || javaVer
}
Debug.log("Denizen fully loaded at: " + TimeTag.now().format());
}
catch (Exception e) {
Debug.echoError(e);
catch (Throwable ex) {
Debug.echoError(ex);
}
}, 1);
new BukkitRunnable() {
Expand Down
Expand Up @@ -18,6 +18,12 @@

public class ScriptEventRegistry {

public static void registerCitizensEvents() {
ScriptEvent.registerScriptEvent(new NPCNavigationScriptEvent());
ScriptEvent.registerScriptEvent(new NPCSpawnScriptEvent());
ScriptEvent.registerScriptEvent(new NPCStuckScriptEvent());
}

public static void registerMainEvents() {

// Block events
Expand Down Expand Up @@ -99,9 +105,7 @@ public static void registerMainEvents() {

// NPC events
if (Depends.citizens != null) {
ScriptEvent.registerScriptEvent(new NPCNavigationScriptEvent());
ScriptEvent.registerScriptEvent(new NPCSpawnScriptEvent());
ScriptEvent.registerScriptEvent(new NPCStuckScriptEvent());
registerCitizensEvents();
}

// Item events
Expand Down
Expand Up @@ -18,6 +18,8 @@

public class BukkitCommandRegistry extends CommandRegistry {

public static BukkitCommandRegistry instance;

public static class AutoNoCitizensCommand extends AbstractCommand {

public static void registerMany(String... names) {
Expand All @@ -30,6 +32,7 @@ public static void registerFor(String name) {
AutoNoCitizensCommand cmd = new AutoNoCitizensCommand();
cmd.name = name;
cmd.syntax = "(Citizens Required)";
instance.register(cmd.name, cmd);
}

public String name;
Expand All @@ -44,7 +47,35 @@ public void execute(ScriptEntry scriptEntry) {
}
}

public void registerCitizensCommands() {
// entity
registerCommand(AnimateCommand.class);
// npc
registerCommand(ActionCommand.class);
registerCommand(AnchorCommand.class);
registerCommand(AssignmentCommand.class);
registerCommand(BreakCommand.class);
registerCommand(CreateCommand.class);
registerCommand(DespawnCommand.class);
registerCommand(DisengageCommand.class);
registerCommand(EngageCommand.class);
registerCommand(FishCommand.class);
registerCommand(LookcloseCommand.class);
registerCommand(PauseCommand.class);
registerCommand(PauseCommand.ResumeCommand.class);
registerCommand(PoseCommand.class);
registerCommand(PushableCommand.class);
registerCommand(SitCommand.class);
registerCommand(SleepCommand.class);
registerCommand(StandCommand.class);
registerCommand(TraitCommand.class);
registerCommand(TriggerCommand.class);
registerCommand(VulnerableCommand.class);
// player
}

public void registerCommands() {
instance = this;

registerCoreCommands();

Expand All @@ -55,9 +86,6 @@ public void registerCommands() {
registerCommand(ZapCommand.class);
// entity
registerCommand(AgeCommand.class);
if (Depends.citizens != null) {
registerCommand(AnimateCommand.class);
}
registerCommand(AttachCommand.class);
registerCommand(AttackCommand.class);
registerCommand(BurnCommand.class);
Expand Down Expand Up @@ -93,36 +121,10 @@ public void registerCommands() {
registerCommand(NBTCommand.class);
registerCommand(ScribeCommand.class);
registerCommand(TakeCommand.class);
// npc
if (Depends.citizens != null) {
registerCommand(ActionCommand.class);
registerCommand(AnchorCommand.class);
registerCommand(AssignmentCommand.class);
registerCommand(BreakCommand.class);
registerCommand(CreateCommand.class);
registerCommand(DespawnCommand.class);
registerCommand(DisengageCommand.class);
registerCommand(EngageCommand.class);
registerCommand(FishCommand.class);
registerCommand(LookcloseCommand.class);
registerCommand(PauseCommand.class);
registerCommand(PauseCommand.ResumeCommand.class);
registerCommand(PoseCommand.class);
registerCommand(PushableCommand.class);
registerCommand(SitCommand.class);
registerCommand(SleepCommand.class);
registerCommand(StandCommand.class);
registerCommand(TraitCommand.class);
registerCommand(TriggerCommand.class);
registerCommand(VulnerableCommand.class);
}
// player
registerCommand(ActionBarCommand.class);
registerCommand(AdvancementCommand.class);
registerCommand(BlockCrackCommand.class);
if (Depends.citizens != null) {
registerCommand(ChatCommand.class);
}
registerCommand(ClickableCommand.class);
registerCommand(CompassCommand.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_15)) {
Expand Down Expand Up @@ -176,7 +178,10 @@ public void registerCommands() {
registerCommand(WeatherCommand.class);
registerCommand(WorldBorderCommand.class);

if (Depends.citizens == null) {
if (Depends.citizens != null) {
registerCitizensCommands();
}
else {
AutoNoCitizensCommand.registerMany("ACTION", "ANCHOR", "ANIMATE", "ASSIGNMENT", "BREAK", "CHAT", "CREATE", "DESPAWN",
"DISENGAGE", "ENGAGE", "FISH", "LOOKCLOSE", "PAUSE", "RESUME", "POSE", "PUSHABLE", "RENAME", "SIT", "STAND", "TRAIT", "TRIGGER", "VULNERABLE");
}
Expand Down
Expand Up @@ -146,7 +146,7 @@ else if (!scriptEntry.hasObject("entities")
if (Utilities.getEntryNPC(scriptEntry) == null
|| !Utilities.getEntryNPC(scriptEntry).isValid()
|| !Utilities.getEntryNPC(scriptEntry).isSpawned()) {
throw new InvalidArgumentsException("Must have a valid spawned NPC attached.");
throw new InvalidArgumentsException("Must have a valid spawned NPC attached, or an entity specified.");
}
else {
scriptEntry.addObject("entities", Collections.singletonList(Utilities.getEntryNPC(scriptEntry).getDenizenEntity()));
Expand Down
Expand Up @@ -21,12 +21,16 @@ public class Depends {
public static Plugin vault = null;

public static void initialize() {
vault = Bukkit.getServer().getPluginManager().getPlugin("Vault");
setupBungee();
setupVault();
setupCitizens();
}

public static void setupVault() {
vault = Bukkit.getServer().getPluginManager().getPlugin("Vault");
setupEconomy();
setupPermissions();
setupChat();
setupCitizens();
}

public static void setupBungee() {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -11,7 +11,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<craftbukkit.version>1.17-R0.1-SNAPSHOT</craftbukkit.version>
<citizens.version>2.0.27-SNAPSHOT</citizens.version>
<citizens.version>2.0.28-SNAPSHOT</citizens.version>
<dcore.version>1.90-SNAPSHOT</dcore.version>
<BUILD_NUMBER>Unknown</BUILD_NUMBER>
<BUILD_CLASS>CUSTOM</BUILD_CLASS>
Expand Down

0 comments on commit 2cdac73

Please sign in to comment.