Skip to content

Commit

Permalink
Improved GenericTrigger.java
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Nov 21, 2019
1 parent 3fb8b93 commit 7d3faff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
</repositories>

<distributionManagement>
<snapshotRepository>
<id>i7mc</id>
<url>http://p.i7mc.com:8082/repository/internal/</url>
</snapshotRepository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/caoli5288/simpleorm</url>
</repository>
</distributionManagement>

<dependencies>
Expand Down
35 changes: 28 additions & 7 deletions src/main/java/com/mengcraft/simpleorm/GenericTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.bukkit.configuration.serialization.ConfigurationSerializable;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -20,34 +21,54 @@ public class GenericTrigger {

private final Multimap<String, TriggerListener> functions = ArrayListMultimap.create();

public TriggerListener on(@NonNull String category, @NonNull BiConsumer<ImmutableMap<String, Object>, Map<String, Object>> function) {
TriggerListener listener = new TriggerListener(category, function);
/**
* @deprecated
*/
public TriggerListener on(@NonNull String category, @NonNull BiConsumer<ImmutableMap<String, Object>, Map<String, Object>> consumer) {
return on(category, processor(consumer));
}

public TriggerListener on(@NonNull String category, @NonNull IProcessor processor) {
TriggerListener listener = new TriggerListener(category, processor);
functions.put(category, listener);
return listener;
}

public Map<String, Object> trigger(@NonNull String category, @NonNull ImmutableMap<String, Object> params) {
Map<String, Object> object = new HashMap<>();
for (TriggerListener listener : functions.get(category)) {
listener.function.accept(params, object);
listener.processor.process(params, object);
}
return object;
}

public Map<String, Object> trigger(@NonNull String category, @NonNull ConfigurationSerializable serializable) {
return trigger(category, ImmutableMap.copyOf(serializable.serialize()));
}

public Map<String, Object> trigger(@NonNull String category) {
return trigger(category, ImmutableMap.of());
}

@EqualsAndHashCode(exclude = "function")
private IProcessor processor(BiConsumer<ImmutableMap<String, Object>, Map<String, Object>> consumer) {
return consumer::accept;
}

public interface IProcessor {

void process(ImmutableMap<String, Object> params, Map<String, Object> result);
}

@EqualsAndHashCode(of = "id")
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class TriggerListener {

private final UUID id = UUID.randomUUID();
private final String category;
private final BiConsumer<ImmutableMap<String, Object>, Map<String, Object>> function;
private final IProcessor processor;

public void cancel() {
functions.remove(category, this);
public boolean cancel() {
return functions.remove(category, this);
}
}

Expand Down

0 comments on commit 7d3faff

Please sign in to comment.