Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onChangeMap add-on compliance, output added #4318

Merged
merged 7 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import net.rptools.parser.function.AbstractFunction;

public class MapFunctions extends AbstractFunction {
public static final String ON_CHANGE_MAP_CALLBACK = "onChangeMap";
private static final MapFunctions instance = new MapFunctions();

private MapFunctions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.rptools.maptool.client.functions.DefinesSpecialVariables;
import net.rptools.maptool.client.functions.TokenMoveFunctions;
import net.rptools.maptool.client.functions.UserDefinedMacroFunctions;
import net.rptools.maptool.events.ZoneLoadedListener;
import net.rptools.maptool.model.InitiativeList;
import net.rptools.maptool.model.TokenProperty;
import net.rptools.parser.function.Function;
Expand Down Expand Up @@ -97,9 +98,9 @@ public class MapToolScriptSyntax extends MapToolScriptTokenMaker {

static String[] RESERVED_WORDS_2 = {
"onCampaignLoad",
"onChangeMap",
"onChangeSelection",
"onMouseOverEvent",
ZoneLoadedListener.ON_CHANGE_MAP_CALLBACK,
TokenMoveFunctions.ON_MULTIPLE_TOKENS_MOVED_COMPLETE_CALLBACK,
TokenMoveFunctions.ON_TOKEN_MOVE_COMPLETE_CALLBACK,
InitiativeList.ON_INITIATIVE_CHANGE_VETOABLE_MACRO_CALLBACK,
Expand Down
39 changes: 30 additions & 9 deletions src/main/java/net/rptools/maptool/events/ZoneLoadedListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,49 @@
*/
package net.rptools.maptool.events;

import static net.rptools.maptool.client.functions.MapFunctions.ON_CHANGE_MAP_CALLBACK;

import com.google.common.eventbus.Subscribe;
import java.util.Collections;
import java.util.concurrent.ExecutionException;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.events.ZoneLoaded;
import net.rptools.maptool.model.Token;
import net.rptools.maptool.client.ui.zone.ZoneRenderer;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.library.Library;
import net.rptools.maptool.model.library.LibraryManager;
import net.rptools.maptool.util.EventMacroUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class ZoneLoadedListener {
private static final Logger LOGGER = LogManager.getLogger(EventMacroUtil.class);
public static final String ON_CHANGE_MAP_CALLBACK = "onChangeMap";

public ZoneLoadedListener() {
new MapToolEventBus().getMainEventBus().register(this);
}

@Subscribe
public void OnChangedMap(ZoneLoaded event) {
var libTokens = EventMacroUtil.getEventMacroTokens(ON_CHANGE_MAP_CALLBACK);
String prefix = ON_CHANGE_MAP_CALLBACK + "@";

for (Token handler : libTokens) {
EventMacroUtil.callEventHandlerOld(
prefix + handler.getName(), "", handler, Collections.emptyMap(), true);
ZoneRenderer currentZR = MapTool.getFrame().getCurrentZoneRenderer();
try {
var libs = new LibraryManager().getLegacyEventTargets(ON_CHANGE_MAP_CALLBACK).get();
if (!libs.isEmpty()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's better to exit the method if libs.isEmpty(). Generally it's better to handle the conditions you don't want first. Otherwise they tend to be forgotten. You also get less indent.

for (Library handler : libs) {
try {
String libraryNamespace = handler.getNamespace().get();
EventMacroUtil.callEventHandler(
ON_CHANGE_MAP_CALLBACK,
libraryNamespace,
currentZR.getZone().getId().toString() + "," + currentZR.getZone().toString(),
null,
Collections.emptyMap());
} catch (InterruptedException | ExecutionException e) {
throw new AssertionError("Error retrieving library namespace");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to add the original exception as inner exception or at least log it. Otherwise it will be difficult to determine the reason for the AssertionError if it ever occurs in a log file.

}
}
}
} catch (InterruptedException | ExecutionException e) {
LOGGER.error(I18N.getText("library.error.retrievingEventHandler"), e);
}
}
}