Skip to content

Commit

Permalink
Merge pull request #4318 from Jmr3366/onChangeMap-upd
Browse files Browse the repository at this point in the history
onChangeMap add-on compliance, output added
  • Loading branch information
cwisniew authored Nov 3, 2023
2 parents fdfddbb + 2d80b51 commit 574115a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
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
41 changes: 32 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,51 @@
*/
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()) {
return;
}
for (Library handler : libs) {
try {
String libraryNamespace = handler.getNamespace().get();
EventMacroUtil.callEventHandler(
ON_CHANGE_MAP_CALLBACK,
libraryNamespace,
currentZR.getZone().getId().toString(),
null,
Collections.emptyMap());
} catch (InterruptedException | ExecutionException e) {
LOGGER.error(I18N.getText("library.error.notFound"), e);
throw new AssertionError("Error retrieving library namespace");
}
}
} catch (InterruptedException | ExecutionException e) {
LOGGER.error(I18N.getText("library.error.retrievingEventHandler"), e);
}
}
}

0 comments on commit 574115a

Please sign in to comment.