Skip to content

Commit

Permalink
Merge pull request #1 from NthPortal/eventbus
Browse files Browse the repository at this point in the history
Fix threading and switch to Google EventBus
  • Loading branch information
NthPortal authored Feb 17, 2017
2 parents f5f8c4c + 4b6d401 commit f9be8b3
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 46 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/github/nthportal/uhc/UHCPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.github.nthportal.uhc.core.Config;
import com.github.nthportal.uhc.core.CustomListener;
import com.github.nthportal.uhc.core.Timer;
import com.github.nthportal.uhc.events.MainListener;
import com.google.common.eventbus.EventBus;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -15,6 +17,7 @@
public final class UHCPlugin extends JavaPlugin {
public final Logger logger = getLogger();
public final Timer timer = new Timer(this);
public final EventBus eventBus = new EventBus("UHC-Plugin");

@Override
public void onEnable() {
Expand All @@ -29,6 +32,8 @@ public void onEnable() {
confCommand.setTabCompleter(new ConfCommandTabCompleter());

getServer().getPluginManager().registerEvents(new CustomListener(this), this);

eventBus.register(new MainListener(this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
package com.github.nthportal.uhc.commands;

import com.github.nthportal.uhc.UHCPlugin;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainCommandExecutor implements CommandExecutor {
public static final String NAME = "uhc";
public static final String PERMISSION = "uhc-plugin.uhc";

private static final ExecutorService SERVICE;

static {
SERVICE = Executors.newSingleThreadExecutor(
new ThreadFactoryBuilder()
.setNameFormat("uhc-plugin-uhc-starter")
.build()
);
}

private final UHCPlugin plugin;

public MainCommandExecutor(final UHCPlugin plugin) {
this.plugin = plugin;
}

@Override
public boolean onCommand(CommandSender commandSender, Command command, String label, String[] strings) {
public boolean onCommand(final CommandSender commandSender, Command command, String label, String[] strings) {
if ((strings.length == 0) || (strings.length > 1) || !commandSender.hasPermission(PERMISSION)) {
return false;
}
Expand All @@ -28,8 +41,12 @@ public boolean onCommand(CommandSender commandSender, Command command, String la
switch (strings[0].toLowerCase()) {
case Opts.START:
commandSender.sendMessage("Starting UHC...");
success = plugin.timer.start();
commandSender.sendMessage(success ? "Started UHC" : "Unable to start UHC - UHC paused or already running");
SERVICE.submit(new Runnable() {
@Override
public void run() {
commandSender.sendMessage(plugin.timer.start() ? "Started UHC" : "Unable to start UHC - UHC paused or already running");
}
});
break;
case Opts.STOP:
success = plugin.timer.stop();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.nthportal.uhc.core;

import com.github.nthportal.uhc.UHCPlugin;
import com.github.nthportal.uhc.events.UHCPlayerDeathEvent;
import com.github.nthportal.uhc.util.CommandUtil;
import com.google.common.base.Function;
import org.bukkit.event.EventHandler;
Expand All @@ -23,8 +24,7 @@ public void onPlayerDeath(final PlayerDeathEvent event) {
return;
}

List<Function<String, String>> replacements = new ArrayList<>();
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.PLAYER, event.getEntity().getName()));
CommandUtil.executeEventCommands(plugin, Config.Events.ON_DEATH, replacements);
plugin.logger.info("Player died: " + event.getEntity().getName());
plugin.eventBus.post(new UHCPlayerDeathEvent(event.getEntity()));
}
}
46 changes: 17 additions & 29 deletions src/main/java/com/github/nthportal/uhc/core/Timer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.nthportal.uhc.core;

import com.github.nthportal.uhc.UHCPlugin;
import com.github.nthportal.uhc.events.*;
import com.github.nthportal.uhc.util.CommandUtil;
import com.google.common.base.Function;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
Expand Down Expand Up @@ -142,7 +143,7 @@ public boolean pause() {
}
minuteFutures.clear();

onPause();
onPause(elapsedTime);
state = State.PAUSED;
return true;
} finally {
Expand Down Expand Up @@ -196,9 +197,10 @@ public void run() {
}
}

onResume(elapsedTime);

elapsedTime = 0;

onResume();
state = State.RUNNING;
return true;
} finally {
Expand Down Expand Up @@ -244,8 +246,8 @@ private int getValidatedEpisodeLength() {
}

private void countdown() {
onCountdownStart();
int countdownFrom = plugin.getConfig().getInt(Config.COUNTDOWN_FROM);
onCountdownStart(countdownFrom);
for (int i = 0; i < countdownFrom; i++) {
onCountdownMark(countdownFrom - i);
try {
Expand All @@ -270,52 +272,38 @@ private void doEpisodeMarker() {
// Event handling stuff

private void onStart() {
CommandUtil.executeEventCommands(plugin, Config.Events.ON_START);
plugin.eventBus.post(new UHCStartEvent());
}

private void onStop() {
CommandUtil.executeEventCommands(plugin, Config.Events.ON_STOP);
plugin.eventBus.post(new UHCStopEvent());
}

private void onPause() {
CommandUtil.executeEventCommands(plugin, Config.Events.ON_PAUSE);
private void onPause(long timeElapsed) {
plugin.eventBus.post(new UHCPauseEvent(timeElapsed));
}

private void onResume() {
CommandUtil.executeEventCommands(plugin, Config.Events.ON_RESUME);
private void onResume(long timeElapsed) {
plugin.eventBus.post(new UHCResumeEvent(timeElapsed));
}

private void onEpisodeStart() {
final int minutes = interval * (episode - 1);
List<Function<String, String>> replacements = new ArrayList<>();
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.EPISODE, String.valueOf(episode)));
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.MINUTES, String.valueOf(minutes)));
CommandUtil.executeEventCommands(plugin, Config.Events.ON_EPISODE_START, replacements);

// Run episode-specific commands
CommandUtil.executeMappedCommandsMatching(plugin, Config.Events.ON_START_EP_NUM, episode);
plugin.eventBus.post(new UHCEpisodeStartEvent(episode, interval));
}

private void onEpisodeEnd() {
final int minutes = interval * episode;
List<Function<String, String>> replacements = new ArrayList<>();
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.EPISODE, String.valueOf(episode)));
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.MINUTES, String.valueOf(minutes)));
CommandUtil.executeEventCommands(plugin, Config.Events.ON_EPISODE_END, replacements);
plugin.eventBus.post(new UHCEpisodeEndEvent(episode, interval));
}

private void onCountdownStart() {
CommandUtil.executeEventCommands(plugin, Config.Events.ON_COUNTDOWN_START);
private void onCountdownStart(int countingFrom) {
plugin.eventBus.post(new UHCCountdownStartEvent(countingFrom));
}

private void onCountdownMark(final int mark) {
List<Function<String, String>> replacements = new ArrayList<>();
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.COUNTDOWN_MARK, String.valueOf(mark)));
CommandUtil.executeEventCommands(plugin, Config.Events.ON_COUNTDOWN_MARK, replacements);
private void onCountdownMark(int mark) {
plugin.eventBus.post(new UHCCountdownMarkEvent(mark));
}

public enum State {
STOPPED, RUNNING, PAUSED
}

}
76 changes: 76 additions & 0 deletions src/main/java/com/github/nthportal/uhc/events/MainListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.github.nthportal.uhc.events;

import com.github.nthportal.uhc.UHCPlugin;
import com.github.nthportal.uhc.core.Config;
import com.github.nthportal.uhc.util.CommandUtil;
import com.google.common.base.Function;
import com.google.common.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

public class MainListener {
public final UHCPlugin plugin;

public MainListener(UHCPlugin plugin) {
this.plugin = plugin;
}

@Subscribe
public void onPlayerDeath(UHCPlayerDeathEvent event) {
List<Function<String, String>> replacements = new ArrayList<>();
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.PLAYER, event.player.getName()));
CommandUtil.executeEventCommands(plugin, Config.Events.ON_DEATH, replacements);
}

@Subscribe
public void onCountdownStart(UHCCountdownStartEvent event) {
CommandUtil.executeEventCommands(plugin, Config.Events.ON_COUNTDOWN_START);
}

@Subscribe
public void onCountdownMark(UHCCountdownMarkEvent event) {
List<Function<String, String>> replacements = new ArrayList<>();
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.COUNTDOWN_MARK, String.valueOf(event.countdownMark)));
CommandUtil.executeEventCommands(plugin, Config.Events.ON_COUNTDOWN_MARK, replacements);
}

@Subscribe
public void onStart(UHCStartEvent event) {
CommandUtil.executeEventCommands(plugin, Config.Events.ON_START);
}

@Subscribe
public void onStop(UHCStopEvent event) {
CommandUtil.executeEventCommands(plugin, Config.Events.ON_STOP);
}

@Subscribe
public void onPause(UHCPauseEvent event) {
CommandUtil.executeEventCommands(plugin, Config.Events.ON_PAUSE);
}

@Subscribe
public void onResume(UHCResumeEvent event) {
CommandUtil.executeEventCommands(plugin, Config.Events.ON_RESUME);
}

@Subscribe
public void onEpisodeStart(UHCEpisodeStartEvent event) {
List<Function<String, String>> replacements = new ArrayList<>();
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.EPISODE, String.valueOf(event.getEpisodeNumber())));
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.MINUTES, String.valueOf(event.getMinutesElapsed())));
CommandUtil.executeEventCommands(plugin, Config.Events.ON_EPISODE_START, replacements);

// Run episode-specific commands
CommandUtil.executeMappedCommandsMatching(plugin, Config.Events.ON_START_EP_NUM, event.getEpisodeNumber());
}

@Subscribe
public void onEpisdeEnd(UHCEpisodeEndEvent event) {
List<Function<String, String>> replacements = new ArrayList<>();
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.EPISODE, String.valueOf(event.getEpisodeNumber())));
replacements.add(CommandUtil.replacementFunction(CommandUtil.ReplaceTargets.MINUTES, String.valueOf(event.getMinutesElapsed())));
CommandUtil.executeEventCommands(plugin, Config.Events.ON_EPISODE_END, replacements);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.nthportal.uhc.events;

public class UHCCountdownMarkEvent {
public final int countdownMark;

public UHCCountdownMarkEvent(int countdownMark) {
this.countdownMark = countdownMark;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.nthportal.uhc.events;

public class UHCCountdownStartEvent {
public final int countingDownFrom;

public UHCCountdownStartEvent(int countingDownFrom) {
this.countingDownFrom = countingDownFrom;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.nthportal.uhc.events;

public class UHCEpisodeEndEvent {
private final int episodeNumber;
private final int episodeLength;
private final int minutesElapsed;

public UHCEpisodeEndEvent(int episodeNumber, int episodeLength) {
this.episodeNumber = episodeNumber;
this.episodeLength = episodeLength;
this.minutesElapsed = episodeNumber * episodeLength;
}

public int getEpisodeNumber() {
return episodeNumber;
}

public int getEpisodeLength() {
return episodeLength;
}

public int getMinutesElapsed() {
return minutesElapsed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.nthportal.uhc.events;

public class UHCEpisodeStartEvent {
private final int episodeNumber;
private final int episodeLength;
private final int minutesElapsed;

public UHCEpisodeStartEvent(int episodeNumber, int episodeLength) {
this.episodeNumber = episodeNumber;
this.episodeLength = episodeLength;
this.minutesElapsed = episodeLength * (episodeNumber - 1);
}

public int getEpisodeNumber() {
return episodeNumber;
}

public int getEpisodeLength() {
return episodeLength;
}

public int getMinutesElapsed() {
return minutesElapsed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.nthportal.uhc.events;

public class UHCMinuteEvent {
public final int minuteNumber;

public UHCMinuteEvent(int minuteNumber) {
this.minuteNumber = minuteNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.nthportal.uhc.events;

public class UHCPauseEvent {
public final long timeElapsed;

public UHCPauseEvent(long timeElapsed) {
this.timeElapsed = timeElapsed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.nthportal.uhc.events;

import org.bukkit.entity.Player;

public class UHCPlayerDeathEvent {
public final Player player;

public UHCPlayerDeathEvent(Player player) {
this.player = player;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.nthportal.uhc.events;

public class UHCResumeEvent {
public final long timeElapsed;

public UHCResumeEvent(long timeElapsed) {
this.timeElapsed = timeElapsed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.nthportal.uhc.events;

public class UHCStartEvent {
}
Loading

0 comments on commit f9be8b3

Please sign in to comment.