Development Guide

Modifier

Internal Modifier extension allows developers to create their own modifiers.

Modifier Data

Modifier Data has no restrictions on what it has to implement. It is up to you what you will store inside it.

com.warxim.petep.extension.internal.modifier.factory.ModifierData
/**
 * Modifier data base class
 * <p>Modifier data can contain any serializable data.</p>
 * <p>Data should be immutable!</p>
 */
@PetepAPI
public abstract class ModifierData {
}

Modifier

Modifier contains the main logic (method process() returns false if PDU should be dropped).

com.warxim.petep.extension.internal.modifier.factory.Modifier
/**
 * Modifier rule for processing modification of PDUs.
 */
@PetepAPI
public abstract class Modifier {
    protected final ModifierFactory factory;
    protected final ModifierData data;

    /**
     * Constructs modifier
     * @param factory Factory that created this modifier
     * @param data Data configuration for this modifier
     */
    protected Modifier(ModifierFactory factory, ModifierData data) {
        this.factory = factory;
        this.data = data;
    }

    /**
     * Processes PDU in the modifier.
     * @param pdu PDU to be processed
     * @return {@code false} if PDU should be dropped
     */
    public abstract boolean process(PDU pdu);

    /**
     * Gets modifier factory.
     * @return Modifier factory of this modifier
     */
    public ModifierFactory getFactory() {
        return factory;
    }

    /**
     * Gets modifier data configuration.
     * @return Modifier data of this modifier
     */
    public ModifierData getData() {
        return data;
    }

    /**
     * Creates deep copy of the Modifier.
     * @return Deep copy of the Modifier
     */
    public Modifier copy() {
        return factory.createModifier(data);
    }

    @Override
    public String toString() {
        return factory.getName();
    }
}

Modifier Configurator

Configurator is graphical element used for configuration of the modifier.

com.warxim.petep.extension.internal.modifier.factory.ModifierConfigurator
/**
 * Modifier configurator for configurating modifier data.
 */
@PetepAPI
public abstract class ModifierConfigurator extends ConfigPane<ModifierData> {
    /**
     * Constructs modifier configurator.
     * @param template Path to JavaFX template for this configurator
     * @throws IOException If the template could not be loaded
     */
    protected ModifierConfigurator(String template) throws IOException {
        super(template);
    }
}

Modifier Factory

Modifier Factory creates the modifier and also provides configuration pane (if it exists).

com.warxim.petep.extension.internal.modifier.factory.ModifierFactory
/**
 * Modifier factory for generating modifier rules.
 */
@PetepAPI
public abstract class ModifierFactory {
    /**
     * Gets factory code
     * @return Factory code (for configuration purposes)
     */
    public abstract String getCode();

    /**
     * Gets factory name
     * @return Factory name (visible for user)
     */
    public abstract String getName();

    /**
     * Creates modifier using given data.
     * @param data Data for the modifier
     * @return New modifier instance
     */
    public abstract Modifier createModifier(ModifierData data);

    /**
     * Get type of modifier configuration
     * @return Type of configuration for deserialization from JSON or empty optional if no configuration is needed
     */
    public abstract Optional<Type> getConfigType();

    /**
     * Creates config pane for modifier data.
     * @throws IOException If there has been problem with loading FXML template
     * @return Modifier configurator for configuring modifier data or empty optional if no configurator is needed
     */
    public abstract Optional<ModifierConfigurator> createConfigPane() throws IOException;

    @Override
    public String toString() {
        return getName();
    }
}

Registration

In order to register ModifierFactory, you have to use the beforeInit method in your extension:

petep.PetepExtension: public void beforeInit(ExtensionHelper helper)
var maybeModifier = helper.getExtension("modifier");
if (maybeModifier.isEmpty()) {
    return;
}
var modifier = (ModifierApi) maybeModifier.get();

if (modifier.registerModifierFactory(new ExampleModifierFactory())) {
    Logger.getGlobal().info("Example modifier registered!");
}