Hi, I’m trying to add a custom “post piece move” hook from an extension, similar to the behavior of SASLActivationChecker.
I want to run logic after a piece has been moved, but without modifying the base VASL module.
I’ve tried hooking into the drag/drop chain from the extension, but intercepting the event directly seems to interfere with the move and can cause the piece to bounce back.
Is there a supported mechanism in VASL/VASSAL for an extension to observe a completed move, or a recommended way to implement this kind of post-move behavior?
The repo is available at https://github.com/marangonico/asl_pbs
Thanks!
Hi again,
I figured out the issue.
My first attempt hooked into the drag/drop chain using ASLPieceMover.AbstractDragHandler.makeDropTarget(). I expected this to let me observe drops, but makeDropTarget() replaces the existing listener stored in dropTargetListeners. In this case that listener was the Map itself, which is responsible for dispatching the event that eventually triggers movePieces(). By replacing it, I was unintentionally swallowing the drop event, so the piece bounced back.
What seems to work better is implementing CommandEncoder instead:
public class ASLPBSChecker extends AbstractConfigurable implements CommandEncoder {
@Override
public void addTo(Buildable parent) {
GameModule.getGameModule().addCommandEncoder(this);
}
@Override
public String encode(Command c) {
visitCommands(c);
return null;
}
@Override
public Command decode(String s) {
return null;
}
private void visitCommands(Command c) {
if (c == null || c.isNull()) return;
if (c instanceof MovePiece) {
onPieceMoved((MovePiece) c);
}
for (Command sub : c.getSubCommands()) {
visitCommands(sub);
}
}
}
encode() gets called for locally generated commands, including drag/drop and keyboard moves, and returning null lets the normal encoder continue without interfering.
I still need to verify that everything else behaves correctly, but at least this approach no longer breaks movement.