Add greeting-title and farewell-title flags.

Totally didn't accidentally include the flags in last commit.
Closes WORLDGUARD-4008.
This commit is contained in:
wizjany 2019-05-04 22:48:05 -04:00
parent c516eb2174
commit 82451595cb
6 changed files with 123 additions and 36 deletions

View File

@ -19,6 +19,7 @@
package com.sk89q.worldguard; package com.sk89q.worldguard;
import com.google.common.annotations.Beta;
import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.weather.WeatherType; import com.sk89q.worldedit.world.weather.WeatherType;
@ -197,4 +198,13 @@ default Association getAssociation(List<ProtectedRegion> regions) {
* @param location The location * @param location The location
*/ */
void setCompassTarget(Location location); void setCompassTarget(Location location);
/**
* This should preferably take Components but there's no way to do that yet
*
* @param title the title to display
* @param subtitle the subtitle to display
*/
@Beta
void sendTitle(String title, String subtitle);
} }

View File

@ -22,16 +22,17 @@
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.Location;
import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.commands.CommandUtils;
import com.sk89q.worldguard.protection.ApplicableRegionSet; import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.Flags; import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.session.MoveType; import com.sk89q.worldguard.session.MoveType;
import com.sk89q.worldguard.session.Session; import com.sk89q.worldguard.session.Session;
import com.sk89q.worldguard.util.MessagingUtil;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.function.BiConsumer;
public class FarewellFlag extends Handler { public class FarewellFlag extends Handler {
@ -44,49 +45,53 @@ public FarewellFlag create(Session session) {
} }
private Set<String> lastMessageStack = Collections.emptySet(); private Set<String> lastMessageStack = Collections.emptySet();
private Set<String> lastTitleStack = Collections.emptySet();
public FarewellFlag(Session session) { public FarewellFlag(Session session) {
super(session); super(session);
} }
private Set<String> getMessages(LocalPlayer player, ApplicableRegionSet set) { private Set<String> getMessages(LocalPlayer player, ApplicableRegionSet set, Flag<String> flag) {
return Sets.newLinkedHashSet(set.queryAllValues(player, Flags.FAREWELL_MESSAGE)); return Sets.newLinkedHashSet(set.queryAllValues(player, flag));
} }
@Override @Override
public void initialize(LocalPlayer player, Location current, ApplicableRegionSet set) { public void initialize(LocalPlayer player, Location current, ApplicableRegionSet set) {
lastMessageStack = getMessages(player, set); lastMessageStack = getMessages(player, set, Flags.FAREWELL_MESSAGE);
lastTitleStack = getMessages(player, set, Flags.FAREWELL_TITLE);
} }
@Override @Override
public boolean onCrossBoundary(LocalPlayer player, Location from, Location to, ApplicableRegionSet toSet, Set<ProtectedRegion> entered, Set<ProtectedRegion> exited, MoveType moveType) { public boolean onCrossBoundary(LocalPlayer player, Location from, Location to, ApplicableRegionSet toSet,
Set<String> messages = getMessages(player, toSet); Set<ProtectedRegion> entered, Set<ProtectedRegion> exited, MoveType moveType) {
lastMessageStack = collectAndSend(player, toSet, Flags.FAREWELL_MESSAGE, lastMessageStack, MessagingUtil::sendStringToChat);
lastTitleStack = collectAndSend(player, toSet, Flags.FAREWELL_TITLE, lastTitleStack, MessagingUtil::sendStringToTitle);
return true;
}
private Set<String> collectAndSend(LocalPlayer player, ApplicableRegionSet toSet, Flag<String> flag,
Set<String> stack, BiConsumer<LocalPlayer, String> msgFunc) {
Set<String> messages = getMessages(player, toSet, flag);
if (!messages.isEmpty()) { if (!messages.isEmpty()) {
// Due to flag priorities, we have to collect the lower // Due to flag priorities, we have to collect the lower
// priority flag values separately // priority flag values separately
for (ProtectedRegion region : toSet) { for (ProtectedRegion region : toSet) {
String message = region.getFlag(Flags.FAREWELL_MESSAGE); String message = region.getFlag(flag);
if (message != null) { if (message != null) {
messages.add(message); messages.add(message);
} }
} }
} }
for (String message : lastMessageStack) { for (String message : stack) {
if (!messages.contains(message)) { if (!messages.contains(message)) {
String effective = CommandUtils.replaceColorMacros(message); msgFunc.accept(player, message);
effective = WorldGuard.getInstance().getPlatform().getMatcher().replaceMacros(player, effective);
for (String mess : effective.replaceAll("\\\\n", "\n").split("\\n")) {
player.printRaw(mess);
}
break; break;
} }
} }
return messages;
lastMessageStack = messages;
return true;
} }
} }

View File

@ -22,17 +22,18 @@
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.Location;
import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.commands.CommandUtils;
import com.sk89q.worldguard.protection.ApplicableRegionSet; import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.Flags; import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.session.MoveType; import com.sk89q.worldguard.session.MoveType;
import com.sk89q.worldguard.session.Session; import com.sk89q.worldguard.session.Session;
import com.sk89q.worldguard.util.MessagingUtil;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.function.BiConsumer;
public class GreetingFlag extends Handler { public class GreetingFlag extends Handler {
@ -45,39 +46,48 @@ public GreetingFlag create(Session session) {
} }
private Set<String> lastMessageStack = Collections.emptySet(); private Set<String> lastMessageStack = Collections.emptySet();
private Set<String> lastTitleStack = Collections.emptySet();
public GreetingFlag(Session session) { public GreetingFlag(Session session) {
super(session); super(session);
} }
private Set<String> getMessages(LocalPlayer player, ApplicableRegionSet set, Flag<String> flag) {
return Sets.newLinkedHashSet(set.queryAllValues(player, flag));
}
@Override @Override
public boolean onCrossBoundary(LocalPlayer player, Location from, Location to, ApplicableRegionSet toSet, Set<ProtectedRegion> entered, Set<ProtectedRegion> exited, MoveType moveType) { public boolean onCrossBoundary(LocalPlayer player, Location from, Location to, ApplicableRegionSet toSet,
Collection<String> messages = toSet.queryAllValues(player, Flags.GREET_MESSAGE); Set<ProtectedRegion> entered, Set<ProtectedRegion> exited, MoveType moveType) {
lastMessageStack = sendAndCollect(player, toSet, Flags.GREET_MESSAGE, lastMessageStack, MessagingUtil::sendStringToChat);
lastTitleStack = sendAndCollect(player, toSet, Flags.GREET_TITLE, lastTitleStack, MessagingUtil::sendStringToTitle);
return true;
}
private Set<String> sendAndCollect(LocalPlayer player, ApplicableRegionSet toSet, Flag<String> flag,
Set<String> stack, BiConsumer<LocalPlayer, String> msgFunc) {
Collection<String> messages = getMessages(player, toSet, flag);
for (String message : messages) { for (String message : messages) {
if (!lastMessageStack.contains(message)) { if (!stack.contains(message)) {
String effective = CommandUtils.replaceColorMacros(message); msgFunc.accept(player, message);
effective = WorldGuard.getInstance().getPlatform().getMatcher().replaceMacros(player, effective);
for (String mess : effective.replaceAll("\\\\n", "\n").split("\\n")) {
player.printRaw(mess);
}
break; break;
} }
} }
lastMessageStack = Sets.newHashSet(messages); stack = Sets.newHashSet(messages);
if (!lastMessageStack.isEmpty()) { if (!stack.isEmpty()) {
// Due to flag priorities, we have to collect the lower // Due to flag priorities, we have to collect the lower
// priority flag values separately // priority flag values separately
for (ProtectedRegion region : toSet) { for (ProtectedRegion region : toSet) {
String message = region.getFlag(Flags.GREET_MESSAGE); String message = region.getFlag(flag);
if (message != null) { if (message != null) {
lastMessageStack.add(message); stack.add(message);
} }
} }
} }
return true; return stack;
} }
} }

View File

@ -0,0 +1,52 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.util;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.commands.CommandUtils;
public final class MessagingUtil {
private MessagingUtil() {
}
public static void sendStringToChat(LocalPlayer player, String message) {
String effective = CommandUtils.replaceColorMacros(message);
effective = WorldGuard.getInstance().getPlatform().getMatcher().replaceMacros(player, effective);
for (String mess : effective.replaceAll("\\\\n", "\n").split("\\n")) {
player.printRaw(mess);
}
}
public static void sendStringToTitle(LocalPlayer player, String message) {
String[] parts = message.replaceAll("\\\\n", "\n").split("\\n", 2);
String title = CommandUtils.replaceColorMacros(parts[0]);
title = WorldGuard.getInstance().getPlatform().getMatcher().replaceMacros(player, title);
if (parts.length > 1) {
String subtitle = CommandUtils.replaceColorMacros(parts[0]);
subtitle = WorldGuard.getInstance().getPlatform().getMatcher().replaceMacros(player, subtitle);
player.sendTitle(title, subtitle);
} else {
player.sendTitle(title, null);
}
}
}

View File

@ -28,9 +28,9 @@
import com.sk89q.worldedit.util.HandSide; import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.weather.WeatherType; import com.sk89q.worldedit.world.weather.WeatherType;
import org.junit.Ignore;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -38,7 +38,7 @@
import javax.annotation.Nullable; import javax.annotation.Nullable;
@org.junit.Ignore @Ignore
public class TestPlayer extends AbstractPlayerActor implements LocalPlayer { public class TestPlayer extends AbstractPlayerActor implements LocalPlayer {
private final UUID uuid = UUID.randomUUID(); private final UUID uuid = UUID.randomUUID();
@ -173,6 +173,11 @@ public void setCompassTarget(Location location) {
} }
@Override
public void sendTitle(String title, String subtitle) {
}
@Override @Override
public void printRaw(String msg) { public void printRaw(String msg) {
System.out.println("-> TestPlayer{" + this.name + "}: " + msg); System.out.println("-> TestPlayer{" + this.name + "}: " + msg);

View File

@ -168,6 +168,11 @@ public void setCompassTarget(Location location) {
getPlayer().setCompassTarget(BukkitAdapter.adapt(location)); getPlayer().setCompassTarget(BukkitAdapter.adapt(location));
} }
@Override
public void sendTitle(String title, String subtitle) {
getPlayer().sendTitle(title, subtitle, -1, -1, -1);
}
@Override @Override
public String[] getGroups() { public String[] getGroups() {
return plugin.getGroups(getPlayer()); return plugin.getGroups(getPlayer());