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

@@ -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();