forked from arianne/stendhal
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generic events in Java client
- Loading branch information
1 parent
0808509
commit f9ea55c
Showing
4 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Entity> { | ||
|
||
/** Registered sub-events. */ | ||
private static Map<String, Class<? extends SubEvent>> registry = new HashMap<String, | ||
Class<? extends SubEvent>>(); | ||
|
||
|
||
@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<? extends SubEvent> 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/games/stendhal/client/events/generic/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |