Skip to content

Commit

Permalink
Track and cancel Delays
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
Mystiflow committed Dec 11, 2019
1 parent b70fe0c commit f9ad52f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.mystiflow.catalogue.command;
package io.mystiflow.catalogue;

import com.google.common.base.Joiner;
import io.mystiflow.catalogue.CataloguePlugin;
import io.mystiflow.catalogue.api.Action;
import io.mystiflow.catalogue.api.Catalogue;
import io.mystiflow.catalogue.api.Delay;
import io.mystiflow.catalogue.api.Message;
import net.md_5.bungee.api.ChatColor;
Expand Down Expand Up @@ -77,6 +76,11 @@ public void execute(CommandSender sender, String[] args) {
sender.sendMessage(ChatColor.YELLOW + "/" + getName() + " <execute|list|reload|save>");
}

private void inform(CommandSender sender, Action action) {
sender.sendMessage(ChatColor.BLUE + "Executing '" + action.getType().name() + ":" + action.getAction()
+ " for " + action.getIterations() + " iterations");
}

private void execute(CommandSender sender, Message message) {
List<Action> actions = message.getActions();
for (Action action : actions) {
Expand All @@ -85,9 +89,11 @@ private void execute(CommandSender sender, Message message) {
for (int i = 0; i < action.getIterations(); i++) {
plugin.getProxy().getPluginManager().dispatchCommand(sender, action.getAction());
}
inform(sender, action);
} else if (action.getType() == Action.Type.MESSAGE) {
plugin.getCatalogue().getMessage(action.getAction()).ifPresent(message1 ->
{
inform(sender, action);
for (int i = 0; i < action.getIterations(); i++) {
execute(sender, message1);
}
Expand All @@ -96,14 +102,19 @@ private void execute(CommandSender sender, Message message) {
}
};

int taskID;
Delay delay = action.getDelay();
if (delay == null) {
runnable.run();
taskID = -1;
} else {
plugin.getProxy().getScheduler().schedule(
taskID = plugin.getProxy().getScheduler().schedule(
plugin, runnable, delay.getDelay() * 50L, delay.getRepeatDelay() * 50L, TimeUnit.MILLISECONDS
);
).getId();
}

Integer previousDelayId = action.getActiveDelays().put("$" + sender.getName(), taskID);
plugin.getProxy().getScheduler().cancel(previousDelayId);
}
}
}
33 changes: 33 additions & 0 deletions bungee/src/main/java/io/mystiflow/catalogue/CatalogueListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.mystiflow.catalogue;

import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.ServerSwitchEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;

public class CatalogueListener implements Listener {

private final CataloguePlugin plugin;

public CatalogueListener(CataloguePlugin plugin) {
this.plugin = plugin;
}

@EventHandler
public void onDisconnect(PlayerDisconnectEvent event) {
cancelDelays(event.getPlayer());
}

@EventHandler
public void onServerSwitch(ServerSwitchEvent event) {
cancelDelays(event.getPlayer());
}

private void cancelDelays(ProxiedPlayer player) {
plugin.getCatalogue().getMessages()
.forEach(message -> message.getActions()
.forEach(action -> action.getActiveDelays().remove("$" + player.getName())));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.mystiflow.catalogue.api.Catalogue;
import io.mystiflow.catalogue.api.Delay;
import io.mystiflow.catalogue.api.Message;
import io.mystiflow.catalogue.command.CatalogueCommand;
import io.mystiflow.catalogue.serialisation.ActionAdapter;
import io.mystiflow.catalogue.serialisation.DelayAdapter;
import io.mystiflow.catalogue.serialisation.MessageAdapter;
Expand All @@ -17,7 +16,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class CataloguePlugin extends Plugin {

Expand Down Expand Up @@ -61,6 +59,7 @@ public void onEnable() {
}

getProxy().getPluginManager().registerCommand(this, new CatalogueCommand(this));
getProxy().getPluginManager().registerListener(this, new CatalogueListener(this));
}

@Override
Expand Down
8 changes: 7 additions & 1 deletion bungee/src/main/java/io/mystiflow/catalogue/api/Action.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.mystiflow.catalogue.api;

import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.Expose;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
* Information about a action to use for an {@link Message}
*/
Expand Down Expand Up @@ -35,4 +38,7 @@ public enum Type {
* The delay to add to this action
*/
private Delay delay;

@Expose(deserialize = false, serialize = false)
private final Map<String, Integer> activeDelays = new HashMap<>();
}

0 comments on commit f9ad52f

Please sign in to comment.