From 32fd484b589fd2a24660cecff6b535a5f2aba3f2 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 15 Jan 2017 13:22:50 +0000 Subject: [PATCH] Change promote / demote behaviour at the end of tracks - towards #112 --- .../common/commands/user/UserDemote.java | 16 +++++++-- .../common/commands/user/UserPromote.java | 33 +++++++++++++++++-- .../luckperms/common/constants/Message.java | 5 ++- default-lang.yml | 5 ++- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java index 7b621b114..b0f54fb14 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java @@ -139,8 +139,20 @@ public class UserDemote extends SubCommand { } if (previous == null) { - Message.USER_DEMOTE_ERROR_ENDOFTRACK.send(sender, track.getName()); - return CommandResult.STATE_ERROR; + + try { + user.unsetPermission(oldNode); + } catch (ObjectLacksException ignored) {} + + Message.USER_DEMOTE_ENDOFTRACK.send(sender, track.getName(), user.getName(), old); + + LogEntry.build().actor(sender).acted(user) + .action("demote " + args.stream().collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(user, sender, plugin); + plugin.getApiProvider().fireEventAsync(new UserDemoteEvent(new TrackLink(track), new UserLink(user), old, null)); + + return CommandResult.SUCCESS; } if (!plugin.getStorage().loadGroup(previous).join()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java index 0e41d46c6..cc9a8ea1d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java @@ -119,8 +119,37 @@ public class UserPromote extends SubCommand { nodes.removeIf(g -> !track.containsGroup(g.getGroupName())); if (nodes.isEmpty()) { - Message.USER_TRACK_ERROR_NOT_CONTAIN_GROUP.send(sender); - return CommandResult.FAILURE; + String first = track.getGroups().get(0); + + Group nextGroup = plugin.getGroupManager().getIfLoaded(first); + if (nextGroup == null) { + Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, false); + return CommandResult.LOADING_ERROR; + } + + try { + user.setPermission(NodeFactory.newBuilder("group." + first).setServer(server).setWorld(world).build()); + } catch (ObjectAlreadyHasException ignored) {} + + switch (ContextHelper.determine(server, world)) { + case NONE: + Message.USER_TRACK_ADDED_TO_FIRST.send(sender, user.getName(), first); + break; + case SERVER: + Message.USER_TRACK_ADDED_TO_FIRST_SERVER.send(sender, user.getName(), first, server); + break; + case SERVER_AND_WORLD: + Message.USER_TRACK_ADDED_TO_FIRST_SERVER_WORLD.send(sender, user.getName(), first, server, world); + break; + } + + LogEntry.build().actor(sender).acted(user) + .action("promote " + args.stream().collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(user, sender, plugin); + plugin.getApiProvider().fireEventAsync(new UserPromoteEvent(new TrackLink(track), new UserLink(user), null, first)); + + return CommandResult.SUCCESS; } if (nodes.size() != 1) { diff --git a/common/src/main/java/me/lucko/luckperms/common/constants/Message.java b/common/src/main/java/me/lucko/luckperms/common/constants/Message.java index 7f6b4ecdd..6b22677d2 100644 --- a/common/src/main/java/me/lucko/luckperms/common/constants/Message.java +++ b/common/src/main/java/me/lucko/luckperms/common/constants/Message.java @@ -310,6 +310,9 @@ public enum Message { USER_PRIMARYGROUP_ERROR_ALREADYHAS("The user already has this group set as their primary group.", true), USER_PRIMARYGROUP_ERROR_NOTMEMBER("&b{0}&a was not already a member of &b{1}&a, adding them now.", true), USER_TRACK_ERROR_NOT_CONTAIN_GROUP("The user specified isn't already in any groups on this track.", true), + USER_TRACK_ADDED_TO_FIRST("&b{0}&a isn't in any groups on this track, so they were added to the first group, &b{1}&a.", true), + USER_TRACK_ADDED_TO_FIRST_SERVER("&b{0}&a isn't in any groups on this track, so they were added to the first group, &b{1}&a, on server &b{2}&a.", true), + USER_TRACK_ADDED_TO_FIRST_SERVER_WORLD("&b{0}&a isn't in any groups on this track, so they were added to the first group, &b{1}&a, on server &b{2}&a, world &b{3}&a.", true), USER_PROMOTE_SUCCESS("&aPromoting user along track &b{0}&a from &b{1}&a to &b{2}&a.", true), USER_PROMOTE_SUCCESS_SERVER("&aPromoting user along track &b{0}&a from &b{1}&a to &b{2}&a on server &b{3}&a.", true), USER_PROMOTE_SUCCESS_SERVER_WORLD("&aPromoting user along track &b{0}&a from &b{1}&a to &b{2}&a on server &b{3}&a, world &b{4}&a.", true), @@ -322,7 +325,7 @@ public enum Message { USER_DEMOTE_SUCCESS("&aDemoting user along track &b{0}&a from &b{1}&a to &b{2}&a.", true), USER_DEMOTE_SUCCESS_SERVER("&aDemoting user along track &b{0}&a from &b{1}&a to &b{2}&a on server &b{3}&a.", true), USER_DEMOTE_SUCCESS_SERVER_WORLD("&aDemoting user along track &b{0}&a from &b{1}&a to &b{2}&a on server &b{3}&a, world &b{4}&a.", true), - USER_DEMOTE_ERROR_ENDOFTRACK("The end of track &4{0}&c was reached. Unable to demote user.", true), + USER_DEMOTE_ENDOFTRACK("The end of track &4{0}&c was reached, so &4{1}&c was removed from &4{2}&c.", true), USER_DEMOTE_ERROR_MALFORMED( "{PREFIX}The previous group on the track, {0}, no longer exists. Unable to demote user." + "\n" + "{PREFIX}Either create the group, or remove it from the track and try again.", diff --git a/default-lang.yml b/default-lang.yml index 3a20f73fd..5aff01fb6 100644 --- a/default-lang.yml +++ b/default-lang.yml @@ -261,6 +261,9 @@ user-primarygroup-success: "&b{0}&a's primary group was set to &b{1}&a." user-primarygroup-error-alreadyhas: "The user already has this group set as their primary group." user-primarygroup-error-notmember: "&b{0}&a was not already a member of &b{1}&a, adding them now." user-track-error-not-contain-group: "The user specified isn't already in any groups on this track." +user-track-added-to-first: "&b{0}&a isn't in any groups on this track, so they were added to the first group, &b{1}&a." +user-track-added-to-first-server: "&b{0}&a isn't in any groups on this track, so they were added to the first group, &b{1}&a, on server &b{2}&a." +user-track-added-to-first-server-world: "&b{0}&a isn't in any groups on this track, so they were added to the first group, &b{1}&a, on server &b{2}&a, world &b{3}&a." user-promote-success: "&aPromoting user along track &b{0}&a from &b{1}&a to &b{2}&a." user-promote-success-server: "&aPromoting user along track &b{0}&a from &b{1}&a to &b{2}&a on server &b{3}&a." user-promote-success-server-world: "&aPromoting user along track &b{0}&a from &b{1}&a to &b{2}&a on server &b{3}&a, world &b{4}&a." @@ -271,7 +274,7 @@ user-promote-error-malformed: > user-demote-success: "&aDemoting user along track &b{0}&a from &b{1}&a to &b{2}&a." user-demote-success-server: "&aDemoting user along track &b{0}&a from &b{1}&a to &b{2}&a on server &b{3}&a." user-demote-success-server-world: "&aDemoting user along track &b{0}&a from &b{1}&a to &b{2}&a on server &b{3}&a, world &b{4}&a." -user-demote-error-endoftrack: "The end of track &4{0}&c was reached. Unable to demote user." +user-demote-endoftrack: "The end of track &4{0}&c was reached, so &4{1}&c was removed from &4{2}&c." user-demote-error-malformed: > {PREFIX}The previous group on the track, {0}, no longer exists. Unable to demote user.\n {PREFIX}Either create the group, or remove it from the track and try again.