From f9ea55cd8c61a5514239595a33c00b4a835c2b06 Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Sat, 18 May 2024 08:19:51 -0700 Subject: [PATCH] Add support for generic events in Java client --- .../stendhal/client/events/EventFactory.java | 4 +- .../stendhal/client/events/GenericEvent.java | 54 ++++++++++++++++ .../client/events/generic/SubEvent.java | 64 +++++++++++++++++++ .../client/events/generic/package-info.java | 12 ++++ 4 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 src/games/stendhal/client/events/GenericEvent.java create mode 100644 src/games/stendhal/client/events/generic/SubEvent.java create mode 100644 src/games/stendhal/client/events/generic/package-info.java diff --git a/src/games/stendhal/client/events/EventFactory.java b/src/games/stendhal/client/events/EventFactory.java index ee83b0ba92e..754bddafc9c 100644 --- a/src/games/stendhal/client/events/EventFactory.java +++ b/src/games/stendhal/client/events/EventFactory.java @@ -1,6 +1,6 @@ /* $Id$ */ /*************************************************************************** - * (C) Copyright 2003-2010 - Stendhal * + * (C) Copyright 2003-2024 - Stendhal * *************************************************************************** *************************************************************************** * * @@ -123,6 +123,8 @@ private static Event createEventsForEntity(Entity entity, RPEvent rpeven event = new EntityMessageEvent(); } else if (name.equals(Events.GLOBAL_VISUAL)) { event = new GlobalVisualEffectEvent(); + } else if (name.equals(Events.GENERIC)) { + event = new GenericEvent(); } if (event != null) { diff --git a/src/games/stendhal/client/events/GenericEvent.java b/src/games/stendhal/client/events/GenericEvent.java new file mode 100644 index 00000000000..e79f46b090c --- /dev/null +++ b/src/games/stendhal/client/events/GenericEvent.java @@ -0,0 +1,54 @@ +/*************************************************************************** + * Copyright © 2024 - Faiumoni e. V. * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +package games.stendhal.client.events; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.log4j.Logger; + +import games.stendhal.client.entity.Entity; +import games.stendhal.client.events.generic.SubEvent; + + +/** + * A generic event that will execute a registered sub-event. + * + * TODO: allow execution without an associated entity + */ +public class GenericEvent extends Event { + + /** Registered sub-events. */ + private static Map> registry = new HashMap>(); + + + @Override + public void execute() { + final String subevent = event.get("subevent"); + if (subevent == null || !registry.containsKey(subevent)) { + Logger.getLogger(GenericEvent.class).warn("Unknown generic event: " + subevent); + return; + } + final String[] flags = event.has("flags") ? event.get("flags").split(",") : new String[] {}; + try { + final Constructor constructor = registry.get(subevent) + .getConstructor(String[].class); + constructor.newInstance((Object) flags).execute(entity); + } catch (NoSuchMethodException|SecurityException|InstantiationException|IllegalAccessException + |IllegalArgumentException|InvocationTargetException e) { + Logger.getLogger(GenericEvent.class).error("Failed to create sub-event instance", e); + } + } +} diff --git a/src/games/stendhal/client/events/generic/SubEvent.java b/src/games/stendhal/client/events/generic/SubEvent.java new file mode 100644 index 00000000000..69245816528 --- /dev/null +++ b/src/games/stendhal/client/events/generic/SubEvent.java @@ -0,0 +1,64 @@ +/*************************************************************************** + * Copyright © 2024 - Faiumoni e. V. * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +package games.stendhal.client.events.generic; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import games.stendhal.client.entity.Entity; + + +/** + * Sub-event for generic events. + */ +public abstract class SubEvent { + + /** List of enabled flags. */ + private final List flags; + + + /** + * Creates a new event. + * + * @param flags + * List of enabled flags. + */ + public SubEvent(final String[] flags) { + this.flags = Collections.unmodifiableList(Arrays.asList(flags)); + } + + /** + * Called when the event occurs. + * + * @param entity + * Entity associated with event. + */ + public abstract void execute(Entity entity); + + /** + * Checks if a flag has been specified. + * + * @param flag + * Name of flag checking for. + * @return + * {@code true} if {@code flag} found in list of enabled flags. + */ + protected boolean flagEnabled(final String flag) { + for (final String f: flags) { + if (f.equals(flag)) { + return true; + } + } + return false; + } +} diff --git a/src/games/stendhal/client/events/generic/package-info.java b/src/games/stendhal/client/events/generic/package-info.java new file mode 100644 index 00000000000..57166302071 --- /dev/null +++ b/src/games/stendhal/client/events/generic/package-info.java @@ -0,0 +1,12 @@ +/*************************************************************************** + * Copyright © 2024 - Faiumoni e. V. * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +package games.stendhal.client.events.generic;