-
Notifications
You must be signed in to change notification settings - Fork 2
Guidebook send event #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,16 +91,19 @@ public List<String> onTabComplete(CommandSender sender, Command command, String | |
|
|
||
| // Filter the completions | ||
| return completions.stream() | ||
| .filter(completion -> getMatchingSegments(completion, lastArg)) | ||
| // AreaName completions are structured <worldName>-<projectName>-<guidebookName> | ||
| // So support partial matches | ||
| .filter(completion -> matchesAnySegment(completion, lastArg)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private boolean getMatchingSegments(String input, String searchTerm) { | ||
| private boolean matchesAnySegment(String input, String searchTerm) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current name was misleading |
||
| String[] parts = input.toLowerCase().split("-"); | ||
| int partsLength = parts.length; | ||
|
|
||
| for (int i = 0; i < parts.length; i++) { | ||
| // world-gondor-minas, gondor-minas, minas | ||
| String segment = String.join("-", Arrays.copyOfRange(parts, i, parts.length)); | ||
| // segment = world-gondor-minas, gondor-minas, minas | ||
| for (int i = 0; i < partsLength; i++) { | ||
| String segment = String.join("-", Arrays.copyOfRange(parts, i, partsLength)); | ||
| if (segment.startsWith(searchTerm)) { | ||
| return true; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
|
|
||
| import com.mcmiddleearth.guidebook.GuidebookPlugin; | ||
| import com.mcmiddleearth.guidebook.command.GuidebookShow; | ||
| import com.mcmiddleearth.guidebook.events.GuidebookSendEvent; | ||
| import com.mcmiddleearth.guidebook.listener.PlayerListener; | ||
| import com.mcmiddleearth.guidebook.util.DevUtil; | ||
| import com.mcmiddleearth.guidebook.util.InputUtil; | ||
|
|
@@ -55,8 +56,14 @@ public abstract class InfoArea { | |
|
|
||
| protected Region region; | ||
|
|
||
| private final String areaName; | ||
|
|
||
| private final HashMap<UUID, Instant> lastInformedTimes = new HashMap<>(); | ||
| private final Set<UUID> informedPlayers = new HashSet<>(); | ||
| /** | ||
| * All players currently within the InfoArea | ||
| * Used to determine if a player has entered or left an InfoArea | ||
| */ | ||
| private final Set<UUID> areaPlayers = new HashSet<>(); | ||
|
|
||
| private boolean status; | ||
|
|
||
|
|
@@ -69,7 +76,9 @@ public abstract class InfoArea { | |
|
|
||
| private List<String> description = new ArrayList<>(); | ||
|
|
||
| protected InfoArea() { | ||
| protected InfoArea(String areaName) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the GuidebookSendEvent is called inside |
||
| this.areaName = areaName; | ||
|
|
||
| //scoreboard = Bukkit.getScoreboardManager().getNewScoreboard(); | ||
| bossBar = Bukkit.getServer().createBossBar("unnamed Guidebook area", BarColor.YELLOW, BarStyle.SOLID); | ||
| bossBar.setProgress(0); | ||
|
|
@@ -78,9 +87,9 @@ protected InfoArea() { | |
| status = true; | ||
| } | ||
|
|
||
| public InfoArea(ConfigurationSection config) { | ||
| //this.center = deserializeLocation(config.getConfigurationSection("center")); | ||
| this(); | ||
| public InfoArea(String areaName, ConfigurationSection config) { | ||
| this(areaName); | ||
|
|
||
| if (config.contains("title")) { | ||
| setTitle((String) config.get("title")); | ||
| subtitle = (String) config.get("subtitle"); | ||
|
|
@@ -127,12 +136,11 @@ public void statusOff() { | |
| status = false; | ||
| } | ||
|
|
||
| public boolean isInformed(Player player) { | ||
| return informedPlayers.contains(player.getUniqueId()); | ||
| public boolean containsPlayer(Player player) { | ||
| return areaPlayers.contains(player.getUniqueId()); | ||
| } | ||
|
|
||
|
|
||
| public void onRegionEnter(Player player) { | ||
| public final void onRegionEnter(Player player) { | ||
| UUID playerId = player.getUniqueId(); | ||
| Instant now = Instant.now(); | ||
|
|
||
|
|
@@ -141,31 +149,40 @@ public void onRegionEnter(Player player) { | |
| Instant lastNotified = lastInformedTimes.get(playerId); | ||
| if (Duration.between(lastNotified, now).compareTo(COOLDOWN) < 0) { | ||
| // Player has entered the region, but don't welcome them | ||
| informedPlayers.add(playerId); | ||
| areaPlayers.add(playerId); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| lastInformedTimes.put(playerId, now); | ||
| informedPlayers.add(playerId); | ||
| welcomePlayer(player); | ||
| // Track that the player is in the region even if the Event is cancelled | ||
| // - otherwise onRegionEnter would keep being called! | ||
| areaPlayers.add(playerId); | ||
|
|
||
| GuidebookSendEvent sendEvent = new GuidebookSendEvent(player, areaName); | ||
| sendEvent.callEvent(); | ||
| if (sendEvent.isCancelled()) return; | ||
|
|
||
| boolean notificationsEnabled = !PluginData.isExcluded(player); | ||
| if (notificationsEnabled) { | ||
| welcomePlayer(player); | ||
| lastInformedTimes.put(playerId, now); | ||
| } | ||
| } | ||
|
|
||
| public void onRegionLeave(Player player) { | ||
| public final void onRegionLeave(Player player) { | ||
| UUID playerId = player.getUniqueId(); | ||
| informedPlayers.remove(playerId); | ||
| areaPlayers.remove(playerId); | ||
| bossBar.removePlayer(player); | ||
| } | ||
|
|
||
| public void clearPlayer(Player player) { | ||
| onRegionLeave(player); | ||
| UUID playerId = player.getUniqueId(); | ||
| bossBar.removePlayer(player); | ||
| informedPlayers.remove(playerId); | ||
| lastInformedTimes.remove(playerId); | ||
| } | ||
|
|
||
| public void clearInformedPlayers() { | ||
| for (UUID uuid : informedPlayers) { | ||
| public void clearPlayers() { | ||
| for (UUID uuid : areaPlayers) { | ||
| Player player = Bukkit.getPlayer(uuid); | ||
| if (player != null) { | ||
| clearPlayer(player); | ||
|
|
@@ -291,4 +308,8 @@ private void debugString(String string) { | |
| Logger.getGlobal().info("i: " + string.charAt(i) + " " + Integer.parseInt(String.valueOf(string.charAt(i))) + " " + string.codePointAt(i)); | ||
| } | ||
| } | ||
|
|
||
| public String getName() { | ||
| return this.areaName; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor optimisations
Other than that the main change is moving
PluginData.isExcluded(player)to be inside onRegionEnter