Skip to content

Commit

Permalink
Add support for generic events in Java client
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Jun 22, 2024
1 parent 0808509 commit f9ea55c
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/games/stendhal/client/events/EventFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* $Id$ */
/***************************************************************************
* (C) Copyright 2003-2010 - Stendhal *
* (C) Copyright 2003-2024 - Stendhal *
***************************************************************************
***************************************************************************
* *
Expand Down Expand Up @@ -123,6 +123,8 @@ private static Event<Entity> 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) {
Expand Down
54 changes: 54 additions & 0 deletions src/games/stendhal/client/events/GenericEvent.java
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);
}
}
}
64 changes: 64 additions & 0 deletions src/games/stendhal/client/events/generic/SubEvent.java
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 src/games/stendhal/client/events/generic/package-info.java
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;

0 comments on commit f9ea55c

Please sign in to comment.