Skip to content
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

Added support for MiniMessage Components #1886

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions COMPILING.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Compiling
=========

You can compile WorldGuard as long as you have some version of Java greater than or equal to 16 installed. Gradle will download JDK 16 specifically if needed,
You can compile WorldGuard as long as you have some version of Java greater than or equal to 17 installed. Gradle will download JDK 17 specifically if needed,
but it needs some version of Java to bootstrap from.

Note that if you have JRE 16 installed, Gradle will currently attempt to use that to compile, which will not work. It is easiest to uninstall JRE 16 and
replace it with JDK 16.
Note that if you have JRE 17 installed, Gradle will currently attempt to use that to compile, which will not work. It is easiest to uninstall JRE 16 and
replace it with JDK 17.

The build process uses Gradle, which you do *not* need to download. WorldGuard is a multi-module project with three modules:

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ask that you make note of the following guidelines.
* **Follow the [Oracle coding conventions](https://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html).**
We can't stress this enough; if your code has notable issues, it may delay
the process significantly.
* **Target Java 16 for source and compilation.**
* **Target Java 17 for source and compilation.**
* **Use only spaces for indentation.** Our indents are 4-spaces long, and tabs
are unacceptable.
* **Wrap code to a 89 column limit.** We do this to make side by side diffs
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ A Bukkit server implementation (such as [Paper](https://papermc.io)) and the [Wo
Compiling
---------

The project is written for Java 16 and our build process makes use of
The project is written for Java 17 and our build process makes use of
[Gradle](http://gradle.org).

Dependencies are automatically handled by Gradle.
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ object Versions {
const val SQUIRRELID = "0.3.0"
const val GUAVA = "31.0.1-jre"
const val FINDBUGS = "3.0.2"
const val ADVENTURE = "4.10.1"
}
1 change: 1 addition & 0 deletions config/checkstyle/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<allow pkg="org.flywaydb"/>
<allow pkg="org.yaml"/>
<allow pkg="org.enginehub.piston"/>
<allow pkg="net.kyori.adventure"/>

<subpackage name="util.profile.resolver">
<allow pkg="org.bukkit"/>
Expand Down
4 changes: 2 additions & 2 deletions worldguard-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ configurations {

dependencies {
"api"(project(":worldguard-core"))
"compileOnly"("io.papermc.paper:paper-api:1.17.1-R0.1-SNAPSHOT")
"runtimeOnly"("org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT") {
"compileOnly"("io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT")
"runtimeOnly"("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT") {
exclude("junit", "junit")
}
"api"("com.sk89q.worldedit:worldedit-bukkit:${Versions.WORLDEDIT}") { isTransitive = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
import com.sk89q.worldedit.world.weather.WeatherTypes;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.util.MessagingUtil;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;
import org.bukkit.BanList.Type;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.concurrent.CompletableFuture;

public class BukkitPlayer extends com.sk89q.worldedit.bukkit.BukkitPlayer implements LocalPlayer {

protected final WorldGuardPlugin plugin;
Expand Down Expand Up @@ -173,12 +176,13 @@ public void setCompassTarget(Location location) {
}

@Override
public void sendTitle(String title, String subtitle) {
if (WorldGuard.getInstance().getPlatform().getGlobalStateManager().get(getWorld()).forceDefaultTitleTimes) {
getPlayer().sendTitle(title, subtitle, 10, 70, 20);
} else {
getPlayer().sendTitle(title, subtitle, -1, -1, -1);
}
public void sendMessage(Component message) {
getPlayer().sendMessage(message);
}

@Override
public void showTitle(Title title) {
getPlayer().showTitle(title);
}

@Override
Expand All @@ -187,19 +191,8 @@ public void resetFallDistance() {
}

@Override
public void teleport(Location location, String successMessage, String failMessage) {
PaperLib.teleportAsync(getPlayer(), BukkitAdapter.adapt(location))
.thenApply(success -> {
if (success) {
// The success message can be cleared via flag
if (!successMessage.isEmpty()) {
MessagingUtil.sendStringToChat(this, successMessage);
}
} else {
printError(failMessage);
}
return success;
});
public CompletableFuture<Boolean> teleport(Location location) {
return PaperLib.teleportAsync(getPlayer(), BukkitAdapter.adapt(location));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.internal.platform.StringMatcher;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class BukkitStringMatcher implements StringMatcher {
Expand Down Expand Up @@ -226,21 +230,13 @@ public World getWorldByName(String worldName) {
}

@Override
public String replaceMacros(Actor sender, String message) {
Collection<? extends Player> online = Bukkit.getServer().getOnlinePlayers();

message = message.replace("%name%", sender.getName());
message = message.replace("%id%", sender.getUniqueId().toString());
message = message.replace("%online%", String.valueOf(online.size()));

if (sender instanceof LocalPlayer) {
LocalPlayer player = (LocalPlayer) sender;
World world = (World) player.getExtent();

message = message.replace("%world%", world.getName());
message = message.replace("%health%", String.valueOf(player.getHealth()));
}

return message;
public TagResolver replacements(LocalPlayer sender) {
return TagResolver.builder()
.resolver(Placeholder.unparsed("name", sender.getName()))
.resolver(Placeholder.unparsed("id", sender.getUniqueId().toString()))
.resolver(Placeholder.unparsed("online", String.valueOf(Bukkit.getServer().getOnlinePlayers().size())))
.resolver(Placeholder.unparsed("world", sender.getWorld().getName()))
.resolver(Placeholder.unparsed("health", String.valueOf(sender.getHealth())))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import com.sk89q.worldguard.protection.managers.storage.sql.SQLDriver;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.util.logging.RecordMessagePrefixer;
import io.papermc.lib.PaperLib;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.DrilldownPie;
import org.bstats.charts.SimplePie;
Expand Down Expand Up @@ -140,6 +141,13 @@ public void onEnable() {
ClassSourceValidator verifier = new ClassSourceValidator(this);
verifier.reportMismatches(ImmutableList.of(WorldGuard.class, ProtectedRegion.class, Flag.class));

if (!PaperLib.isPaper()) {
// TODO: Tidy up this message
getLogger().severe("Unable to start WorldGuard. This version does only support Paper.");
getLogger().severe("Update your server or downgrade WorldGuard.");
throw new RuntimeException("No capable platform found.");
}

configureLogger();

getDataFolder().mkdirs(); // Need to create the plugins/WorldGuard folder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
import com.sk89q.worldguard.bukkit.util.Events;
import com.sk89q.worldguard.bukkit.util.InteropUtils;
import com.sk89q.worldguard.bukkit.util.Materials;
import com.sk89q.worldguard.commands.CommandUtils;
import com.sk89q.worldguard.config.WorldConfiguration;
import com.sk89q.worldguard.protection.association.RegionAssociable;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.StateFlag.State;
import com.sk89q.worldguard.protection.regions.RegionQuery;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
Expand All @@ -67,6 +67,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* Handle events that need to be processed by region protection.
Expand Down Expand Up @@ -101,8 +102,7 @@ private void tellErrorMessage(DelegateEvent event, Cause cause, Location locatio

Object rootCause = cause.getRootCause();

if (rootCause instanceof Player) {
Player player = (Player) rootCause;
if (rootCause instanceof Player player) {

long now = System.currentTimeMillis();
Long lastTime = WGMetadata.getIfPresent(player, DENY_MESSAGE_KEY, Long.class);
Expand All @@ -118,9 +118,7 @@ private void tellErrorMessage(DelegateEvent event, Cause cause, Location locatio

static void formatAndSendDenyMessage(String what, LocalPlayer localPlayer, String message) {
if (message == null || message.isEmpty()) return;
message = WorldGuard.getInstance().getPlatform().getMatcher().replaceMacros(localPlayer, message);
message = CommandUtils.replaceColorMacros(message);
localPlayer.printRaw(message.replace("%what%", what));
localPlayer.sendMessage(WorldGuard.getInstance().getMiniMessage().deserialize(message, Placeholder.unparsed("what", what)));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions worldguard-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ dependencies {
"implementation"("org.yaml:snakeyaml:1.30")
"implementation"("com.google.guava:guava:${Versions.GUAVA}")

"api"("net.kyori:adventure-api:${Versions.ADVENTURE}")
"api"("net.kyori:adventure-text-minimessage:${Versions.ADVENTURE}")
"api"("net.kyori:adventure-text-serializer-legacy:${Versions.ADVENTURE}")

"compileOnly"("com.google.code.findbugs:jsr305:${Versions.FINDBUGS}")
"testImplementation"("org.hamcrest:hamcrest-library:1.2.1")
}
Expand Down
29 changes: 18 additions & 11 deletions worldguard-core/src/main/java/com/sk89q/worldguard/LocalPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

package com.sk89q.worldguard;

import com.google.common.annotations.Beta;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.weather.WeatherType;
import com.sk89q.worldguard.domains.Association;
import com.sk89q.worldguard.protection.association.RegionAssociable;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

public interface LocalPlayer extends Player, RegionAssociable {

Expand Down Expand Up @@ -200,24 +203,28 @@ default Association getAssociation(List<ProtectedRegion> regions) {
void setCompassTarget(Location location);

/**
* This should preferably take Components but there's no way to do that yet
* Send the message to the player
*
* @param title the title to display
* @param subtitle the subtitle to display
* @param message the message as component
*/
@Beta
void sendTitle(String title, String subtitle);
void sendMessage(Component message);

/**
* Send the message to the player
*
* @param title the title
*/
void showTitle(Title title);

/**
* Clears fall distance.
*/
void resetFallDistance();

/**
* Teleport the player, potentially async, displaying the message on a success.
* @param location location to teleport to
* @param successMessage message to display on success
* @param failMessage message to display on failure
* Teleport the player, potentially async.
* @param location location to teleport to
* @return a future object whether the teleport was successful
*/
void teleport(Location location, String successMessage, String failMessage);
CompletableFuture<Boolean> teleport(Location location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.sk89q.worldguard.protection.flags.registry.SimpleFlagRegistry;
import com.sk89q.worldguard.util.WorldGuardExceptionConverter;
import com.sk89q.worldguard.util.concurrent.EvenMoreExecutors;
import net.kyori.adventure.text.minimessage.MiniMessage;

import java.io.File;
import java.io.IOException;
Expand All @@ -61,6 +62,8 @@ public final class WorldGuard {
private ListeningExecutorService executorService;
private WorldGuardExceptionConverter exceptionConverter = new WorldGuardExceptionConverter();

private MiniMessage miniMessage = MiniMessage.miniMessage();

static {
Flags.registerAll();
}
Expand Down Expand Up @@ -235,4 +238,7 @@ public static String getVersion() {
return version;
}

public MiniMessage getMiniMessage() {
return miniMessage;
}
}
Loading