API: Add methods for setting and editing meta tags

This commit is contained in:
William278 committed 2023-03-24 16:42:11 +00:00
1 parent f3d6f86568
commit 4ebbba730c
3 files changed
+111

No files matched your search

@@ -16,6 +16,7 @@ import net.william278.huskhomes.user.User;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@@ -352,6 +353,46 @@ public abstract class BaseHuskHomesAPI {
plugin.runAsync(() -> plugin.getManager().homes().setHomePosition(home, position));
}
/**
* Set the meta tags of a home
*
* @param owner The {@link User} to set the meta tags of the home for
* @param name The name of the home
* @param tags The meta tags to set
* @since 4.0
*/
public final void setHomeMetaTags(@NotNull User owner, @NotNull String name, @NotNull Map<String, String> tags) {
plugin.runAsync(() -> plugin.getManager().homes().setHomeMetaTags(owner, name, tags));
}
/**
* Set the meta tags of a home
*
* @param home The {@link Home} to set the meta tags of
* @param tags The meta tags to set
* @since 4.0
*/
public final void setHomeMetaTags(@NotNull Home home, @NotNull Map<String, String> tags) {
plugin.runAsync(() -> plugin.getManager().homes().setHomeMetaTags(home, tags));
}
/**
* Edit the meta tags of a home
*
* @param owner The {@link User} to edit the meta tags of the home for
* @param name The name of the home
* @param tagEditor The {@link Consumer} to edit the meta tags with
* @since 4.0
*/
public final void editHomeMetaTags(@NotNull User owner, @NotNull String name, @NotNull Consumer<Map<String, String>> tagEditor) {
plugin.runAsync(() -> plugin.getDatabase().getHome(owner, name).ifPresent(home -> {
final Map<String, String> tags = home.getMeta().getTags();
tagEditor.accept(tags);
setHomeMetaTags(home, tags);
}));
}
/**
* Get a list of {@link Warp}s local to this server
*
@@ -482,6 +523,45 @@ public abstract class BaseHuskHomesAPI {
plugin.runAsync(() -> plugin.getManager().warps().setWarpPosition(warp, position));
}
/**
* Set the meta tags of a {@link Warp} by name
*
* @param name The name of the warp to set the meta tags of
* @param tags The new meta tags of the warp
* @since 4.0
*/
public final void setWarpMetaTags(@NotNull String name, @NotNull Map<String, String> tags) {
plugin.runAsync(() -> plugin.getManager().warps().setWarpMetaTags(name, tags));
}
/**
* Set the meta tags of a {@link Warp}
*
* @param warp The {@link Warp} to set the meta tags of
* @param tags The new meta tags of the warp
* @since 4.0
*/
public final void setWarpMetaTags(@NotNull Warp warp, @NotNull Map<String, String> tags) {
plugin.runAsync(() -> plugin.getManager().warps().setWarpMetaTags(warp, tags));
}
/**
* Edit the meta tags of a {@link Warp} by name
*
* @param name The name of the warp to edit the meta tags of
* @param tagEditor A {@link Consumer} that will be passed the current meta tags of the warp
* and should edit them in-place
* @since 4.0
*/
public final void editWarpMetaTag(@NotNull String name, @NotNull Consumer<Map<String, String>> tagEditor) {
plugin.runAsync(() -> plugin.getDatabase().getWarp(name).ifPresent(warp -> {
final Map<String, String> tags = warp.getMeta().getTags();
tagEditor.accept(tags);
setWarpMetaTags(warp, tags);
}));
}
/**
* Get the canonical {@link Position} of the spawn point. Note that if cross-server and global spawn
* are enabled in the config, this may not return a position on this server.
@@ -264,4 +264,19 @@ public class HomesManager {
this.cacheHome(home, true);
}
public void setHomeMetaTags(@NotNull User owner, @NotNull String name, @NotNull Map<String, String> tags) throws ValidationException {
final Optional<Home> optionalHome = plugin.getDatabase().getHome(owner, name);
if (optionalHome.isEmpty()) {
throw new ValidationException(ValidationException.Type.NOT_FOUND);
}
this.setHomeMetaTags(optionalHome.get(), tags);
}
public void setHomeMetaTags(@NotNull Home home, @NotNull Map<String, String> tags) {
home.getMeta().setTags(tags);
plugin.getDatabase().saveHome(home);
this.cacheHome(home, true);
}
}
@@ -11,6 +11,7 @@ import net.william278.huskhomes.util.ValidationException;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -175,4 +176,19 @@ public class WarpsManager {
this.cacheWarp(warp, true);
}
public void setWarpMetaTags(@NotNull String name, @NotNull Map<String, String> tags) throws ValidationException {
final Optional<Warp> optionalWarp = plugin.getDatabase().getWarp(name);
if (optionalWarp.isEmpty()) {
throw new ValidationException(ValidationException.Type.NOT_FOUND);
}
this.setWarpMetaTags(optionalWarp.get(), tags);
}
public void setWarpMetaTags(@NotNull Warp warp, @NotNull Map<String, String> tags) {
warp.getMeta().setTags(tags);
plugin.getDatabase().saveWarp(warp);
this.cacheWarp(warp, true);
}
}