feat: Expose original home/warp in Edit events

Also fixes an editwarp description locale placeholder issue
This commit is contained in:
William committed 2023-12-27 17:37:14 +00:00
1 parent 04dfa71c87
commit d519014cce
17 files changed
+143 -46

No files matched your search

+1 -1
View File
@@ -10,7 +10,7 @@ plugins {
}
group 'net.william278'
version "$ext.plugin_version${versionMetadata()}"
version "$ext.plugin_version"
description "$ext.plugin_description"
defaultTasks 'licenseFormat', 'check', 'build'
@@ -36,7 +36,7 @@ import java.util.List;
public interface BukkitEventDispatcher extends EventDispatcher {
@Override
default <T extends Event> @NotNull boolean fireIsCancelled(@NotNull T event) {
default <T extends Event> boolean fireIsCancelled(@NotNull T event) {
Bukkit.getPluginManager().callEvent((org.bukkit.event.Event) event);
return event instanceof Cancellable cancellable && cancellable.isCancelled();
}
@@ -77,8 +77,9 @@ public interface BukkitEventDispatcher extends EventDispatcher {
}
@Override
default @NotNull IHomeEditEvent getHomeEditEvent(@NotNull Home home, @NotNull CommandUser editor) {
return new HomeEditEvent(home, editor);
default @NotNull IHomeEditEvent getHomeEditEvent(@NotNull Home edited, @NotNull Home original,
@NotNull CommandUser editor) {
return new HomeEditEvent(edited, original, editor);
}
@Override
@@ -93,8 +94,9 @@ public interface BukkitEventDispatcher extends EventDispatcher {
}
@Override
default @NotNull IWarpEditEvent getWarpEditEvent(@NotNull Warp warp, @NotNull CommandUser editor) {
return new WarpEditEvent(warp, editor);
default @NotNull IWarpEditEvent getWarpEditEvent(@NotNull Warp edited, @NotNull Warp original,
@NotNull CommandUser editor) {
return new WarpEditEvent(edited, original, editor);
}
@Override
@@ -31,11 +31,13 @@ public class HomeEditEvent extends Event implements IHomeEditEvent, Cancellable
private static final HandlerList HANDLER_LIST = new HandlerList();
private final Home home;
private final Home original;
private final CommandUser editor;
private boolean cancelled;
public HomeEditEvent(@NotNull Home home, @NotNull CommandUser editor) {
this.home = home;
public HomeEditEvent(@NotNull Home edited, @NotNull Home original, @NotNull CommandUser editor) {
this.home = edited;
this.original = original;
this.editor = editor;
}
@@ -66,6 +68,12 @@ public class HomeEditEvent extends Event implements IHomeEditEvent, Cancellable
return home;
}
@NotNull
@Override
public Home getOriginalHome() {
return original;
}
@Override
@NotNull
public CommandUser getEditor() {
@@ -31,11 +31,13 @@ public class WarpEditEvent extends Event implements IWarpEditEvent, Cancellable
private static final HandlerList HANDLER_LIST = new HandlerList();
private final Warp warp;
private final Warp original;
private final CommandUser editor;
private boolean cancelled;
public WarpEditEvent(@NotNull Warp warp, @NotNull CommandUser editor) {
public WarpEditEvent(@NotNull Warp warp, @NotNull Warp original, @NotNull CommandUser editor) {
this.warp = warp;
this.original = warp;
this.editor = editor;
}
@@ -66,6 +68,12 @@ public class WarpEditEvent extends Event implements IWarpEditEvent, Cancellable
return warp;
}
@NotNull
@Override
public Warp getOriginalWarp() {
return original;
}
@Override
@NotNull
public CommandUser getEditor() {
@@ -78,7 +78,7 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
private void setHomeName(@NotNull CommandUser executor, @NotNull Home home, boolean ownerEditing,
@NotNull String[] args) {
final String oldName = home.getName();
final Home original = home.copy();
final Optional<String> optionalName = parseStringArg(args, 1);
if (optionalName.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_syntax",
@@ -88,7 +88,7 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
}
home.getMeta().setName(optionalName.get());
plugin.fireEvent(plugin.getHomeEditEvent(home, executor), (event) -> {
plugin.fireEvent(plugin.getHomeEditEvent(home, original, executor), (event) -> {
final String newName = event.getHome().getName();
try {
plugin.getManager().homes().setHomeName(home, newName);
@@ -98,11 +98,11 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
}
if (ownerEditing) {
plugin.getLocales().getLocale("edit_home_update_name", oldName, newName)
plugin.getLocales().getLocale("edit_home_update_name", original.getName(), newName)
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("edit_home_update_name_other", home.getOwner().getUsername(),
oldName, newName)
plugin.getLocales().getLocale("edit_home_update_name_other",
home.getOwner().getUsername(), original.getName(), newName)
.ifPresent(executor::sendMessage);
}
});
@@ -110,7 +110,7 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
private void setHomeDescription(@NotNull CommandUser executor, @NotNull Home home, boolean ownerEditing,
@NotNull String[] args) {
final String oldDescription = home.getMeta().getDescription();
final Home original = home.copy();
final Optional<String> optionalDescription = parseGreedyArguments(args);
if (optionalDescription.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_syntax",
@@ -120,7 +120,7 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
}
home.getMeta().setDescription(optionalDescription.get());
plugin.fireEvent(plugin.getHomeEditEvent(home, executor), (event) -> {
plugin.fireEvent(plugin.getHomeEditEvent(home, original, executor), (event) -> {
final String newDescription = event.getHome().getMeta().getDescription();
try {
plugin.getManager().homes().setHomeDescription(home, newDescription);
@@ -131,11 +131,12 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
if (ownerEditing) {
plugin.getLocales().getLocale("edit_home_update_description",
home.getName(), oldDescription, newDescription)
home.getName(), original.getMeta().getDescription(), newDescription)
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("edit_home_update_description_other", home.getOwner().getUsername(),
home.getName(), oldDescription, newDescription)
plugin.getLocales().getLocale("edit_home_update_description_other",
home.getOwner().getUsername(), home.getName(),
original.getMeta().getDescription(), newDescription)
.ifPresent(executor::sendMessage);
}
});
@@ -148,8 +149,9 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
return;
}
final Home original = home.copy();
home.update(user.getPosition());
plugin.fireEvent(plugin.getHomeEditEvent(home, executor), (event) -> {
plugin.fireEvent(plugin.getHomeEditEvent(home, original, executor), (event) -> {
try {
plugin.getManager().homes().setHomePosition(home, home);
} catch (ValidationException e) {
@@ -183,12 +185,13 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
}
// Set the home privacy
final Home original = home.copy();
home.setPublic(parseStringArg(args, 1)
.map("public"::equalsIgnoreCase)
.orElse(!home.isPublic()));
// Fire the event
plugin.fireEvent(plugin.getHomeEditEvent(home, executor), (event) -> {
plugin.fireEvent(plugin.getHomeEditEvent(home, original, executor), (event) -> {
try {
plugin.getManager().homes().setHomePrivacy(
home.getOwner().equals(executor) ? (OnlineUser) executor : home.getOwner(),
@@ -250,7 +253,7 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
home.getUuid().toString())
.ifPresent(messages::add);
if (home.getMeta().getDescription().length() > 0) {
if (!home.getMeta().getDescription().isEmpty()) {
plugin.getLocales().getLocale("edit_home_menu_description",
plugin.getLocales().truncateText(home.getMeta().getDescription(), 50),
plugin.getLocales().wrapText(home.getMeta().getDescription(), 40))
@@ -66,7 +66,7 @@ public class EditWarpCommand extends SavedPositionCommand<Warp> {
}
private void setWarpName(@NotNull CommandUser executor, @NotNull Warp warp, @NotNull String[] args) {
final String oldName = warp.getName();
final Warp original = warp.copy();
final Optional<String> optionalName = parseStringArg(args, 1);
if (optionalName.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_syntax",
@@ -74,9 +74,8 @@ public class EditWarpCommand extends SavedPositionCommand<Warp> {
.ifPresent(executor::sendMessage);
return;
}
warp.getMeta().setName(optionalName.get());
plugin.fireEvent(plugin.getWarpEditEvent(warp, executor), (event) -> {
plugin.fireEvent(plugin.getWarpEditEvent(warp, original, executor), (event) -> {
try {
plugin.getManager().warps().setWarpName(warp, warp.getName());
} catch (ValidationException e) {
@@ -84,12 +83,13 @@ public class EditWarpCommand extends SavedPositionCommand<Warp> {
return;
}
plugin.getLocales().getLocale("edit_warp_update_name", oldName, optionalName.get())
plugin.getLocales().getLocale("edit_warp_update_name", original.getName(), optionalName.get())
.ifPresent(executor::sendMessage);
});
}
private void setWarpDescription(@NotNull CommandUser executor, @NotNull Warp warp, @NotNull String[] args) {
final Warp original = warp.copy();
final Optional<String> optionalDescription = parseGreedyArguments(args);
if (optionalDescription.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_syntax",
@@ -99,7 +99,7 @@ public class EditWarpCommand extends SavedPositionCommand<Warp> {
}
warp.getMeta().setDescription(optionalDescription.get());
plugin.fireEvent(plugin.getWarpEditEvent(warp, executor), (event) -> {
plugin.fireEvent(plugin.getWarpEditEvent(warp, original, executor), (event) -> {
try {
plugin.getManager().warps().setWarpDescription(warp, warp.getMeta().getDescription());
} catch (ValidationException e) {
@@ -107,8 +107,8 @@ public class EditWarpCommand extends SavedPositionCommand<Warp> {
return;
}
plugin.getLocales().getLocale("edit_warp_update_description",
warp.getName(), warp.getMeta().getDescription())
plugin.getLocales().getLocale("edit_warp_update_description", warp.getName(),
original.getMeta().getDescription(), warp.getMeta().getDescription())
.ifPresent(executor::sendMessage);
});
}
@@ -120,8 +120,9 @@ public class EditWarpCommand extends SavedPositionCommand<Warp> {
return;
}
final Warp original = warp.copy();
warp.update(user.getPosition());
plugin.fireEvent(plugin.getWarpEditEvent(warp, executor), (event) -> {
plugin.fireEvent(plugin.getWarpEditEvent(warp, original, executor), (event) -> {
try {
plugin.getManager().warps().setWarpPosition(warp, warp);
} catch (ValidationException e) {
@@ -153,7 +154,7 @@ public class EditWarpCommand extends SavedPositionCommand<Warp> {
warp.getUuid().toString())
.ifPresent(messages::add);
if (warp.getMeta().getDescription().length() > 0) {
if (!warp.getMeta().getDescription().isEmpty()) {
plugin.getLocales().getLocale("edit_warp_menu_description",
plugin.getLocales().truncateText(warp.getMeta().getDescription(), 50),
plugin.getLocales().wrapText(warp.getMeta().getDescription(), 40))
@@ -87,7 +87,8 @@ public interface EventDispatcher {
@NotNull CommandUser creator);
@NotNull
IHomeEditEvent getHomeEditEvent(@NotNull Home home, @NotNull CommandUser editor);
IHomeEditEvent getHomeEditEvent(@NotNull Home home, @NotNull Home original,
@NotNull CommandUser editor);
@NotNull
IHomeDeleteEvent getHomeDeleteEvent(@NotNull Home home, @NotNull CommandUser deleter);
@@ -96,7 +97,8 @@ public interface EventDispatcher {
IWarpCreateEvent getWarpCreateEvent(@NotNull String name, @NotNull Position position, @NotNull CommandUser creator);
@NotNull
IWarpEditEvent getWarpEditEvent(@NotNull Warp warp, @NotNull CommandUser editor);
IWarpEditEvent getWarpEditEvent(@NotNull Warp warp, @NotNull Warp original,
@NotNull CommandUser editor);
@NotNull
IWarpDeleteEvent getWarpDeleteEvent(@NotNull Warp warp, @NotNull CommandUser deleter);
@@ -36,6 +36,14 @@ public interface IHomeEditEvent extends Cancellable {
@NotNull
Home getHome();
/**
* Get the original home before it was edited.
*
* @return the {@link Home} before it was edited
*/
@NotNull
Home getOriginalHome();
/**
* Get the user who is editing the home.
*
@@ -36,6 +36,14 @@ public interface IWarpEditEvent extends Cancellable {
@NotNull
Warp getWarp();
/**
* Get the original warp before it was edited.
*
* @return the {@link Warp} before it was edited
*/
@NotNull
Warp getOriginalWarp();
/**
* Get the user who is editing the warp.
*
@@ -86,6 +86,15 @@ public class Home extends SavedPosition {
return new Home(position, meta, owner);
}
@NotNull
public Home copy() {
return Home.from(
getX(), getY(), getZ(), getYaw(), getPitch(),
getWorld(), getServer(), getMeta().copy(),
getUuid(), getOwner(), isPublic()
);
}
/**
* Get the {@link User} who owns this home.
*
@@ -57,8 +57,17 @@ public class PositionMeta {
return PositionMeta.from(name, description, Instant.now(), "");
}
@NotNull
public PositionMeta copy() {
return PositionMeta.from(
getName(), getDescription(),
getCreationTime().plusNanos(0),
getSerializedTags()
);
}
/**
* Deserialize a JSON string into a {@link Map} of meta tags.
* Deserialize a JSON string into a {@link Map} of meta-tags.
*
* @param serializedTags The JSON string to deserialize
* @return The deserialized {@link Map}
@@ -45,15 +45,26 @@ public class Warp extends SavedPosition {
super(position, meta);
}
@NotNull
public static Warp from(double x, double y, double z, float yaw, float pitch, @NotNull World world,
@NotNull String server, @NotNull PositionMeta positionMeta, @NotNull UUID uuid) {
return new Warp(x, y, z, yaw, pitch, world, server, positionMeta, uuid);
}
@NotNull
public static Warp from(@NotNull Position position, @NotNull PositionMeta meta) {
return new Warp(position, meta);
}
@NotNull
public Warp copy() {
return new Warp(
getX(), getY(), getZ(), getYaw(), getPitch(),
getWorld(), getServer(), getMeta().copy(),
getUuid()
);
}
@NotNull
public static String getPermission(@NotNull String warpName) {
return PERMISSION_PREFIX + warpName.toLowerCase();
@@ -24,10 +24,9 @@ import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.util.ActionResult;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.user.CommandUser;
import org.apache.commons.lang3.function.TriFunction;
import org.jetbrains.annotations.NotNull;
import java.util.function.BiFunction;
public interface HomeEditCallback extends FabricEventCallback<IHomeEditEvent> {
@NotNull
@@ -47,7 +46,7 @@ public interface HomeEditCallback extends FabricEventCallback<IHomeEditEvent> {
});
@NotNull
BiFunction<Home, CommandUser, IHomeEditEvent> SUPPLIER = (home, editor) ->
TriFunction<Home, Home, CommandUser, IHomeEditEvent> SUPPLIER = (home, original, editor) ->
new IHomeEditEvent() {
private boolean cancelled = false;
@@ -57,6 +56,12 @@ public interface HomeEditCallback extends FabricEventCallback<IHomeEditEvent> {
return home;
}
@NotNull
@Override
public Home getOriginalHome() {
return original;
}
@Override
@NotNull
public CommandUser getEditor() {
@@ -24,10 +24,9 @@ import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.util.ActionResult;
import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.user.CommandUser;
import org.apache.commons.lang3.function.TriFunction;
import org.jetbrains.annotations.NotNull;
import java.util.function.BiFunction;
public interface WarpEditCallback extends FabricEventCallback<IWarpEditEvent> {
@NotNull
@@ -47,7 +46,7 @@ public interface WarpEditCallback extends FabricEventCallback<IWarpEditEvent> {
});
@NotNull
BiFunction<Warp, CommandUser, IWarpEditEvent> SUPPLIER = (warp, editor) ->
TriFunction<Warp, Warp, CommandUser, IWarpEditEvent> SUPPLIER = (warp, original, editor) ->
new IWarpEditEvent() {
private boolean cancelled = false;
@@ -57,6 +56,12 @@ public interface WarpEditCallback extends FabricEventCallback<IWarpEditEvent> {
return warp;
}
@NotNull
@Override
public Warp getOriginalWarp() {
return original;
}
@Override
@NotNull
public CommandUser getEditor() {
@@ -77,8 +77,9 @@ public interface SpongeEventDispatcher extends EventDispatcher {
}
@Override
default @NotNull IHomeEditEvent getHomeEditEvent(@NotNull Home home, @NotNull CommandUser editor) {
return new SpongeHomeEditEvent(home, editor);
default @NotNull IHomeEditEvent getHomeEditEvent(@NotNull Home home, @NotNull Home original,
@NotNull CommandUser editor) {
return new SpongeHomeEditEvent(home, original, editor);
}
@Override
@@ -93,8 +94,9 @@ public interface SpongeEventDispatcher extends EventDispatcher {
}
@Override
default @NotNull IWarpEditEvent getWarpEditEvent(@NotNull Warp warp, @NotNull CommandUser editor) {
return new SpongeWarpEditEvent(warp, editor);
default @NotNull IWarpEditEvent getWarpEditEvent(@NotNull Warp warp, @NotNull Warp original,
@NotNull CommandUser editor) {
return new SpongeWarpEditEvent(warp, original, editor);
}
@Override
@@ -30,10 +30,12 @@ public class SpongeHomeEditEvent implements IHomeEditEvent, Event, Cancellable {
private boolean cancelled = false;
private final Home home;
private final Home original;
private final CommandUser editor;
public SpongeHomeEditEvent(@NotNull Home home, @NotNull CommandUser editor) {
public SpongeHomeEditEvent(@NotNull Home home, @NotNull Home original, @NotNull CommandUser editor) {
this.home = home;
this.original = original;
this.editor = editor;
}
@@ -53,6 +55,12 @@ public class SpongeHomeEditEvent implements IHomeEditEvent, Event, Cancellable {
return home;
}
@NotNull
@Override
public Home getOriginalHome() {
return original;
}
@NotNull
@Override
public CommandUser getEditor() {
@@ -30,10 +30,12 @@ public class SpongeWarpEditEvent implements IWarpEditEvent, Event, Cancellable {
private boolean cancelled = false;
private final Warp warp;
private final Warp original;
private final CommandUser editor;
public SpongeWarpEditEvent(@NotNull Warp warp, @NotNull CommandUser editor) {
public SpongeWarpEditEvent(@NotNull Warp warp, @NotNull Warp original, @NotNull CommandUser editor) {
this.warp = warp;
this.original = original;
this.editor = editor;
}
@@ -53,6 +55,12 @@ public class SpongeWarpEditEvent implements IWarpEditEvent, Event, Cancellable {
return warp;
}
@NotNull
@Override
public Warp getOriginalWarp() {
return original;
}
@NotNull
@Override
public CommandUser getEditor() {