Skip to content

Commit

Permalink
world tag: handle unloaded worlds better
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 23, 2020
1 parent eb3bfa5 commit 9059fab
Showing 1 changed file with 9 additions and 10 deletions.
Expand Up @@ -236,7 +236,6 @@ public static void registerTags() {
// -->
registerTag("living_entities", (attribute, object) -> {
ArrayList<EntityTag> entities = new ArrayList<>();

for (Entity entity : object.getLivingEntitiesForTag()) {
entities.add(new EntityTag(entity));
}
Expand All @@ -252,7 +251,6 @@ public static void registerTags() {
// -->
registerTag("players", (attribute, object) -> {
ArrayList<PlayerTag> players = new ArrayList<>();

for (Player player : object.getWorld().getPlayers()) {
if (!EntityTag.isNPC(player)) {
players.add(new PlayerTag(player));
Expand All @@ -270,9 +268,7 @@ public static void registerTags() {
// -->
registerTag("spawned_npcs", (attribute, object) -> {
ArrayList<NPCTag> npcs = new ArrayList<>();

World thisWorld = object.getWorld();

for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (npc.isSpawned() && npc.getEntity().getLocation().getWorld().equals(thisWorld)) {
npcs.add(new NPCTag(npc));
Expand All @@ -290,9 +286,7 @@ public static void registerTags() {
// -->
registerTag("npcs", (attribute, object) -> {
ArrayList<NPCTag> npcs = new ArrayList<>();

World thisWorld = object.getWorld();

for (NPC npc : CitizensAPI.getNPCRegistry()) {
Location location = npc.getStoredLocation();
if (location != null) {
Expand All @@ -302,7 +296,6 @@ public static void registerTags() {
}
}
}

return new ListTag(npcs);
});

Expand Down Expand Up @@ -383,8 +376,8 @@ public static void registerTags() {
// @description
// Returns the name of the world.
// -->
registerTag("name", (attribute, object) -> {
return new ElementTag(object.getWorld().getName());
tagProcessor.registerTag("name", (attribute, object) -> {
return new ElementTag(object.world_name);
});

// <--[tag]
Expand Down Expand Up @@ -755,7 +748,13 @@ else if (time >= 12500) {
public static ObjectTagProcessor<WorldTag> tagProcessor = new ObjectTagProcessor<>();

public static void registerTag(String name, TagRunnable.ObjectInterface<WorldTag> runnable, String... variants) {
tagProcessor.registerTag(name, runnable, variants);
tagProcessor.registerTag(name, (attribute, object) -> {
if (object.getWorld() == null) {
attribute.echoError("World '" + object.world_name + "' is unloaded, cannot process tag.");
return null;
}
return runnable.run(attribute, object);
}, variants);
}

@Override
Expand Down

0 comments on commit 9059fab

Please sign in to comment.