diff options
author | Dico200 <dico.karssiens@gmail.com> | 2018-07-27 23:20:44 +0100 |
---|---|---|
committer | Dico200 <dico.karssiens@gmail.com> | 2018-07-27 23:20:44 +0100 |
commit | 2a726e1b618b38e635b249d7d09000ec2ea9ac17 (patch) | |
tree | c370942595e36ffce24e0873c710c06110973a17 | |
parent | a9c19ff5cfbf6d99d46cc38d0e294275cdc7f61e (diff) |
Work on commands a bit more
14 files changed, 240 insertions, 245 deletions
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/AbstractChatController.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/AbstractChatController.java index 7d88d0e..52c1e30 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/AbstractChatController.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/AbstractChatController.java @@ -4,9 +4,29 @@ import io.dico.dicore.command.CommandException; import io.dico.dicore.command.EMessageType; import io.dico.dicore.command.ExecutionContext; import io.dico.dicore.command.ICommandAddress; +import io.dico.dicore.command.chat.help.HelpPages; import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; public class AbstractChatController implements IChatController { + private @NotNull HelpPages helpPages; + + public AbstractChatController(@NotNull HelpPages helpPages) { + this.helpPages = helpPages; + } + + public AbstractChatController() { + this(HelpPages.newDefaultHelpPages()); + } + + @NotNull + public HelpPages getHelpPages() { + return helpPages; + } + + public void setHelpPages(@NotNull HelpPages helpPages) { + this.helpPages = helpPages; + } @Override public void sendMessage(ExecutionContext context, EMessageType type, String message) { @@ -37,12 +57,12 @@ public class AbstractChatController implements IChatController { @Override public void sendHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) { - sendMessage(sender, EMessageType.INSTRUCTION, HelpCache.getHelpCache(address).getHelpPage(page)); + sender.sendMessage(helpPages.getHelpPage(sender, context, address, page)); } @Override public void sendSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) { - sendMessage(sender, EMessageType.INSTRUCTION, HelpCache.getHelpCache(address).getSyntax()); + sender.sendMessage(helpPages.getSyntax(sender, context, address)); } @Override @@ -83,4 +103,9 @@ public class AbstractChatController implements IChatController { return ""; } + @Override + public String filterMessage(String message) { + return message; + } + } diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/ChatControllers.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/ChatControllers.java index 709f791..723edda 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/ChatControllers.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/ChatControllers.java @@ -1,17 +1,5 @@ package io.dico.dicore.command.chat; -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.command.chat.help.IHelpComponent; -import io.dico.dicore.command.chat.help.IHelpTopic; -import io.dico.dicore.command.chat.help.IPageBuilder; -import io.dico.dicore.command.chat.help.IPageLayout; -import io.dico.dicore.command.chat.help.defaults.*; -import org.bukkit.command.CommandSender; - -import java.util.Arrays; -import java.util.List; - /** * Static factory methods for {@link IChatController} */ @@ -27,26 +15,6 @@ public class ChatControllers { } static { - defaultChat = new AbstractChatController() { - IPageBuilder pageBuilder = new DefaultPageBuilder(); - IPageLayout pageLayout = new DefaultPageLayout(); - List<IHelpTopic> topics = Arrays.asList(new DescriptionHelpTopic(), new SyntaxHelpTopic(), new SubcommandsHelpTopic()); - - @Override - public void sendHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) { - sender.sendMessage(pageBuilder.getPage(topics, pageLayout, address, sender, context, page, 12)); - } - - @Override - public void sendSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) { - List<IHelpComponent> components = topics.get(1).getComponents(address, sender, context); - if (components.isEmpty()) { - sendHelpMessage(sender, context, address, 1); - } else { - sender.sendMessage(DefaultPageBuilder.combine(components)); - } - } - - }; + defaultChat = new AbstractChatController(); } } diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/HelpCache.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/HelpCache.java deleted file mode 100644 index ed91d69..0000000 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/HelpCache.java +++ /dev/null @@ -1,186 +0,0 @@ -package io.dico.dicore.command.chat; - -import io.dico.dicore.command.Command; -import io.dico.dicore.command.EMessageType; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.command.parameter.Parameter; -import io.dico.dicore.command.parameter.ParameterList; - -import java.util.*; -import java.util.stream.Collectors; - -public class HelpCache { - private static Map<ICommandAddress, HelpCache> caches = new IdentityHashMap<>(); - private ICommandAddress address; - private String shortSyntax; - private String[] lines; - private int[] pageStarts; - - public static HelpCache getHelpCache(ICommandAddress address) { - return caches.computeIfAbsent(address, HelpCache::new); - } - - private HelpCache(ICommandAddress address) { - this.address = address; - } - - private void loadHelp() { - List<String> lines = new ArrayList<>(); - List<Integer> potentialPageStarts = new ArrayList<>(); - int curLineIdx = 0; - potentialPageStarts.add(curLineIdx); - - String curLine = address.getChatController().getMessagePrefixForType(EMessageType.INSTRUCTION); - curLine += address.getChatController().getChatFormatForType(EMessageType.INSTRUCTION); - curLine += getSyntax(); - lines.add(curLine); - curLineIdx++; - - if (address.hasCommand()) { - Command command = address.getCommand(); - String[] description = command.getDescription(); - if (description != null && description.length > 0) { - for (String line : description) { - curLine = address.getChatController().getChatFormatForType(EMessageType.INFORMATIVE).toString(); - curLine += line; - lines.add(curLine); - curLineIdx++; - } - } - } - - List<ICommandAddress> children = address.getChildren().values().stream() - .distinct() - .sorted(Comparator.comparing(ICommandAddress::getMainKey)) - .collect(Collectors.toList()); - - for (ICommandAddress address : children) { - potentialPageStarts.add(curLineIdx); - - curLine = this.address.getChatController().getChatFormatForType(EMessageType.INSTRUCTION) + "/"; - if (address.isDepthLargerThan(2)) { - curLine += "... "; - } - curLine += address.getMainKey(); - curLine += getHelpCache(address).getShortSyntax(); - lines.add(curLine); - curLineIdx++; - - if (address.hasCommand()) { - String shortDescription = address.getCommand().getShortDescription(); - if (shortDescription != null) { - curLine = this.address.getChatController().getChatFormatForType(EMessageType.INFORMATIVE).toString(); - curLine += shortDescription; - lines.add(curLine); - curLineIdx++; - } - } - } - - this.lines = lines.toArray(new String[lines.size()]); - - // compute where the pages start with a maximum page size of 10 - List<Integer> pageStarts = new ArrayList<>(); - pageStarts.add(0); - int maxLength = 10; - int curPageEndTarget = maxLength; - for (int i = 1, n = potentialPageStarts.size(); i < n; i++) { - int index = potentialPageStarts.get(i); - if (index == curPageEndTarget) { - pageStarts.add(curPageEndTarget); - curPageEndTarget += maxLength; - } else if (index > curPageEndTarget) { - curPageEndTarget = potentialPageStarts.get(i - 1); - pageStarts.add(curPageEndTarget); - curPageEndTarget += maxLength; - } - } - - int[] pageStartsArray = new int[pageStarts.size()]; - for (int i = 0, n = pageStartsArray.length; i < n; i++) { - pageStartsArray[i] = pageStarts.get(i); - } - this.pageStarts = pageStartsArray; - } - - /** - * Get a help page - * - * @param page the 0-bound page number (first page is page 0) - * @return the help page - */ - public String getHelpPage(int page) { - if (lines == null) { - loadHelp(); - } - - //System.out.println(Arrays.toString(lines)); - - if (page >= pageStarts.length) { - //System.out.println("page >= pageStarts.length: " + Arrays.toString(pageStarts)); - return ""; - } else if (page < 0) { - throw new IllegalArgumentException("Page number is negative"); - } - - int start = pageStarts[page]; - int end = page + 1 == pageStarts.length ? lines.length : pageStarts[page + 1]; - //System.out.println("start = " + start); - //System.out.println("end = " + end); - return String.join("\n", Arrays.copyOfRange(lines, start, end)); - } - - public int getTotalPageCount() { - return pageStarts.length; - } - - /** - * The latter syntax of the command, prefixed by a space. - * - * @return The latter part of the syntax for this command. That is, without the actual command name. - */ - public String getShortSyntax() { - if (shortSyntax != null) { - return shortSyntax; - } - - StringBuilder syntax = new StringBuilder(); - if (address.hasCommand()) { - Command command = address.getCommand(); - ParameterList list = command.getParameterList(); - Parameter<?, ?> repeated = list.getRepeatedParameter(); - - int requiredCount = list.getRequiredCount(); - List<Parameter<?, ?>> indexedParameters = list.getIndexedParameters(); - for (int i = 0, n = indexedParameters.size(); i < n; i++) { - syntax.append(i < requiredCount ? " <" : " ["); - Parameter<?, ?> param = indexedParameters.get(i); - syntax.append(param.getName()); - if (param == repeated) { - syntax.append("..."); - } - syntax.append(i < requiredCount ? '>' : ']'); - } - - Map<String, Parameter<?, ?>> parametersByName = list.getParametersByName(); - for (Parameter<?, ?> param : parametersByName.values()) { - if (param.isFlag()) { - syntax.append(" [").append(param.getName()); - if (param.expectsInput()) { - syntax.append(" <>"); - } - syntax.append(']'); - } - } - } else { - syntax.append(' '); - } - this.shortSyntax = syntax.toString(); - return this.shortSyntax; - } - - public String getSyntax() { - return '/' + address.getAddress() + getShortSyntax(); - } - -} diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/IChatController.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/IChatController.java index 79a3d48..271ea20 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/IChatController.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/IChatController.java @@ -25,4 +25,6 @@ public interface IChatController { String getMessagePrefixForType(EMessageType type); + String filterMessage(String message); + } diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/HelpPages.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/HelpPages.java new file mode 100644 index 0000000..5798b3c --- /dev/null +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/HelpPages.java @@ -0,0 +1,92 @@ +package io.dico.dicore.command.chat.help; + +import io.dico.dicore.command.ExecutionContext; +import io.dico.dicore.command.ICommandAddress; +import io.dico.dicore.command.chat.help.defaults.*; +import org.bukkit.permissions.Permissible; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class HelpPages { + private @NotNull IPageBuilder pageBuilder; + private @NotNull IPageLayout pageLayout; + private int pageLength; + private @NotNull List<IHelpTopic> helpTopics; + private @NotNull IHelpTopic syntaxTopic; + + public HelpPages(@NotNull IPageBuilder pageBuilder, @NotNull IPageLayout pageLayout, int pageLength, @NotNull IHelpTopic syntaxTopic, @NotNull List<IHelpTopic> helpTopics) { + this.pageBuilder = pageBuilder; + this.pageLayout = pageLayout; + this.pageLength = pageLength; + this.syntaxTopic = syntaxTopic; + this.helpTopics = helpTopics; + } + + public HelpPages(IPageBuilder pageBuilder, IPageLayout pageLayout, int pageLength, IHelpTopic syntaxTopic, IHelpTopic... helpTopics) { + this(pageBuilder, pageLayout, pageLength, syntaxTopic, new ArrayList<>(Arrays.asList(helpTopics))); + } + + @SuppressWarnings("RedundantArrayCreation") + public static HelpPages newDefaultHelpPages() { + IHelpTopic syntaxTopic = new SyntaxHelpTopic(); + return new HelpPages(new DefaultPageBuilder(), new DefaultPageLayout(), 12, + syntaxTopic, new IHelpTopic[]{new DescriptionHelpTopic(), syntaxTopic, new SubcommandsHelpTopic()}); + } + + public @NotNull IPageBuilder getPageBuilder() { + return pageBuilder; + } + + public void setPageBuilder(@NotNull IPageBuilder pageBuilder) { + this.pageBuilder = pageBuilder; + } + + public @NotNull IPageLayout getPageLayout() { + return pageLayout; + } + + public void setPageLayout(@NotNull IPageLayout pageLayout) { + this.pageLayout = pageLayout; + } + + public int getPageLength() { + return pageLength; + } + + public void setPageLength(int pageLength) { + this.pageLength = pageLength; + } + + public @NotNull IHelpTopic getSyntaxTopic() { + return syntaxTopic; + } + + public void setSyntaxTopic(@NotNull IHelpTopic syntaxTopic) { + this.syntaxTopic = syntaxTopic; + } + + @NotNull + public List<IHelpTopic> getHelpTopics() { + return helpTopics; + } + + public void setHelpTopics(@NotNull List<IHelpTopic> helpTopics) { + this.helpTopics = helpTopics; + } + + public @NotNull String getHelpPage(Permissible viewer, ExecutionContext context, ICommandAddress address, int page) { + return pageBuilder.getPage(helpTopics, pageLayout, address, viewer, context, page, pageLength); + } + + public @NotNull String getSyntax(Permissible viewer, ExecutionContext context, ICommandAddress address) { + List<IHelpComponent> components = syntaxTopic.getComponents(address, viewer, context); + if (components.isEmpty()) { + return getHelpPage(viewer, context, address, 1); + } + return DefaultPageBuilder.combine(components); + } + +} diff --git a/javaStyle.xml b/javaStyle.xml new file mode 100644 index 0000000..ec482f0 --- /dev/null +++ b/javaStyle.xml @@ -0,0 +1,39 @@ +<code_scheme name="Dico" version="173"> + <option name="RIGHT_MARGIN" value="150" /> + <option name="FORMATTER_TAGS_ENABLED" value="true" /> + <JavaCodeStyleSettings> + <option name="REPLACE_INSTANCEOF" value="true" /> + <option name="REPLACE_CAST" value="true" /> + <option name="ANNOTATION_PARAMETER_WRAP" value="1" /> + <option name="ALIGN_MULTILINE_ANNOTATION_PARAMETERS" value="true" /> + </JavaCodeStyleSettings> + <XML> + <option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" /> + </XML> + <codeStyleSettings language="JAVA"> + <option name="METHOD_ANNOTATION_WRAP" value="1" /> + <option name="FIELD_ANNOTATION_WRAP" value="1" /> + <option name="VARIABLE_ANNOTATION_WRAP" value="1" /> + <arrangement> + <groups> + <group> + <type>GETTERS_AND_SETTERS</type> + <order>KEEP</order> + </group> + <group> + <type>OVERRIDDEN_METHODS</type> + <order>KEEP</order> + </group> + <group> + <type>DEPENDENT_METHODS</type> + <order>DEPTH_FIRST</order> + </group> + </groups> + </arrangement> + </codeStyleSettings> + <codeStyleSettings language="Scala"> + <indentOptions> + <option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" /> + </indentOptions> + </codeStyleSettings> +</code_scheme>
\ No newline at end of file diff --git a/src/main/kotlin/io/dico/parcels2/Parcel.kt b/src/main/kotlin/io/dico/parcels2/Parcel.kt index 4df60b1..0bdaafd 100644 --- a/src/main/kotlin/io/dico/parcels2/Parcel.kt +++ b/src/main/kotlin/io/dico/parcels2/Parcel.kt @@ -3,19 +3,41 @@ package io.dico.parcels2 import io.dico.parcels2.math.Vec2i import io.dico.parcels2.util.getPlayerName import io.dico.parcels2.util.hasBuildAnywhere +import io.dico.parcels2.util.isValid +import io.dico.parcels2.util.uuid import org.bukkit.Bukkit +import org.bukkit.OfflinePlayer import org.bukkit.entity.Player import java.util.* -interface ParcelData { - var owner: ParcelOwner? +interface AddedData { val added: Map<UUID, AddedStatus> fun getAddedStatus(uuid: UUID): AddedStatus fun setAddedStatus(uuid: UUID, status: AddedStatus): Boolean - fun isBanned(uuid: UUID): Boolean - fun isAllowed(uuid: UUID): Boolean - fun canBuild(player: Player): Boolean + + fun compareAndSetAddedStatus(uuid: UUID, expect: AddedStatus, status: AddedStatus): Boolean = + (getAddedStatus(uuid) == expect).also { if (it) setAddedStatus(uuid, status) } + + fun isAllowed(uuid: UUID) = getAddedStatus(uuid) == AddedStatus.ALLOWED + fun allow(uuid: UUID) = setAddedStatus(uuid, AddedStatus.ALLOWED) + fun disallow(uuid: UUID) = compareAndSetAddedStatus(uuid, AddedStatus.ALLOWED, AddedStatus.DEFAULT) + fun isBanned(uuid: UUID) = getAddedStatus(uuid) == AddedStatus.BANNED + fun ban(uuid: UUID) = setAddedStatus(uuid, AddedStatus.BANNED) + fun unban(uuid: UUID) = compareAndSetAddedStatus(uuid, AddedStatus.BANNED, AddedStatus.DEFAULT) + + fun isAllowed(player: OfflinePlayer) = isAllowed(player.uuid) + fun allow(player: OfflinePlayer) = allow(player.uuid) + fun disallow(player: OfflinePlayer) = disallow(player.uuid) + fun isBanned(player: OfflinePlayer) = isBanned(player.uuid) + fun ban(player: OfflinePlayer) = ban(player.uuid) + fun unban(player: OfflinePlayer) = unban(player.uuid) +} + +interface ParcelData : AddedData { + var owner: ParcelOwner? + + fun canBuild(player: OfflinePlayer, checkAdmin: Boolean = true, checkGlobal: Boolean = true): Boolean var allowInteractInputs: Boolean var allowInteractInventory: Boolean @@ -23,7 +45,6 @@ interface ParcelData { fun isOwner(uuid: UUID): Boolean { return owner?.uuid == uuid } - } /** @@ -58,7 +79,7 @@ class Parcel(val world: ParcelWorld, val pos: Vec2i) : ParcelData { override fun getAddedStatus(uuid: UUID) = data.getAddedStatus(uuid) override fun isBanned(uuid: UUID) = data.isBanned(uuid) override fun isAllowed(uuid: UUID) = data.isAllowed(uuid) - override fun canBuild(player: Player) = data.canBuild(player) + override fun canBuild(player: OfflinePlayer, checkAdmin: Boolean, checkGlobal: Boolean) = data.canBuild(player) override var owner: ParcelOwner? get() = data.owner @@ -94,20 +115,19 @@ class Parcel(val world: ParcelWorld, val pos: Vec2i) : ParcelData { var hasBlockVisitors: Boolean = false; private set } -class ParcelDataHolder : ParcelData { +open class AddedDataHolder : AddedData { override var added = mutableMapOf<UUID, AddedStatus>() - override var owner: ParcelOwner? = null - override fun getAddedStatus(uuid: UUID): AddedStatus = added.getOrDefault(uuid, AddedStatus.DEFAULT) override fun setAddedStatus(uuid: UUID, status: AddedStatus): Boolean = status.takeIf { it != AddedStatus.DEFAULT } ?.let { added.put(uuid, it) != it } ?: added.remove(uuid) != null +} - override fun isBanned(uuid: UUID) = getAddedStatus(uuid) == AddedStatus.BANNED - override fun isAllowed(uuid: UUID) = getAddedStatus(uuid) == AddedStatus.ALLOWED - override fun canBuild(player: Player) = isAllowed(player.uniqueId) - || owner?.matches(player, allowNameMatch = false) ?: false - || player.hasBuildAnywhere +class ParcelDataHolder : AddedDataHolder(), ParcelData { + override var owner: ParcelOwner? = null + override fun canBuild(player: OfflinePlayer, checkAdmin: Boolean, checkGlobal: Boolean) = isAllowed(player.uniqueId) + || owner.let { it != null && it.matches(player, allowNameMatch = false) } + || (checkAdmin && player is Player && player.hasBuildAnywhere) override var allowInteractInputs = true override var allowInteractInventory = true @@ -144,14 +164,14 @@ class ParcelOwner(val uuid: UUID? = null, if (name != null) this.name = name else { - val offlinePlayer = Bukkit.getOfflinePlayer(uuid).takeIf { it.isOnline() || it.hasPlayedBefore() } + val offlinePlayer = Bukkit.getOfflinePlayer(uuid).takeIf { it.isValid } this.name = offlinePlayer?.name } } val playerName get() = getPlayerName(uuid, name) - fun matches(player: Player, allowNameMatch: Boolean = false): Boolean { + fun matches(player: OfflinePlayer, allowNameMatch: Boolean = false): Boolean { return uuid?.let { it == player.uniqueId } ?: false || (allowNameMatch && name?.let { it == player.name } ?: false) } @@ -162,5 +182,5 @@ class ParcelOwner(val uuid: UUID? = null, @Suppress("DEPRECATION") val offlinePlayer get() = (uuid?.let { Bukkit.getOfflinePlayer(it) } ?: Bukkit.getOfflinePlayer(name)) - ?.takeIf { it.isOnline() || it.hasPlayedBefore() } + ?.takeIf { it.isValid } } diff --git a/src/main/kotlin/io/dico/parcels2/command/NamedParcelTargetType.kt b/src/main/kotlin/io/dico/parcels2/command/NamedParcelTargetType.kt index ceb226f..bb7add9 100644 --- a/src/main/kotlin/io/dico/parcels2/command/NamedParcelTargetType.kt +++ b/src/main/kotlin/io/dico/parcels2/command/NamedParcelTargetType.kt @@ -6,6 +6,7 @@ import io.dico.dicore.command.parameter.type.ParameterConfig import io.dico.dicore.command.parameter.type.ParameterType import io.dico.parcels2.ParcelWorld import io.dico.parcels2.Worlds +import io.dico.parcels2.util.isValid import org.bukkit.Bukkit import org.bukkit.OfflinePlayer import org.bukkit.command.CommandSender @@ -42,7 +43,7 @@ class ParcelHomeParameterType(val worlds: Worlds) : ParameterType<NamedParcelTar @Suppress("UsePropertyAccessSyntax") private fun getOfflinePlayer(input: String, parameter: Parameter<*, *>) = Bukkit.getOfflinePlayer(input) - ?.takeIf { it.isOnline() || it.hasPlayedBefore() } + ?.takeIf { it.isValid } ?: invalidInput(parameter, "do not know who $input is") override fun parse(parameter: Parameter<NamedParcelTarget, NamedParcelDefaultValue>, diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelAddCommands.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelAddCommands.kt index 7e27d87..02e9123 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelAddCommands.kt +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelAddCommands.kt @@ -1,8 +1,10 @@ package io.dico.parcels2.command +import io.dico.dicore.command.Validate import io.dico.dicore.command.annotation.Cmd import io.dico.dicore.command.annotation.Desc import io.dico.parcels2.ParcelsPlugin +import io.dico.parcels2.util.hasAdminManage import org.bukkit.OfflinePlayer import org.bukkit.entity.Player @@ -13,7 +15,10 @@ class ParcelAddCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) shortVersion = "allows a player to build on this parcel") @ParcelRequire(owner = true) fun ParcelScope.cmdAllow(sender: Player, player: OfflinePlayer): Any? { - TODO() + Validate.isTrue(parcel.owner != null && !sender.hasAdminManage, "This parcel is unowned") + Validate.isTrue(!parcel.owner!!.matches(player), "The target already owns the parcel") + Validate.isTrue(parcel.allow(player), "${player.name} is already allowed to build on this parcel") + return "${player.name} is now allowed to build on this parcel" } @Cmd("disallow", aliases = ["remove", "forbid"]) @@ -22,7 +27,8 @@ class ParcelAddCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) shortVersion = "disallows a player to build on this parcel") @ParcelRequire(owner = true) fun ParcelScope.cmdDisallow(sender: Player, player: OfflinePlayer): Any? { - TODO() + Validate.isTrue(parcel.disallow(player), "${player.name} is not currently allowed to build on this parcel") + return "${player.name} is not allowed to build on this parcel anymore" } @Cmd("ban", aliases = ["deny"]) @@ -31,7 +37,10 @@ class ParcelAddCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) shortVersion = "bans a player from this parcel") @ParcelRequire(owner = true) fun ParcelScope.cmdBan(sender: Player, player: OfflinePlayer): Any? { - TODO() + Validate.isTrue(parcel.owner != null && !sender.hasAdminManage, "This parcel is unowned") + Validate.isTrue(!parcel.owner!!.matches(player), "The owner cannot be banned from the parcel") + Validate.isTrue(parcel.ban(player), "${player.name} is already banned from this parcel") + return "${player.name} is now banned from this parcel" } @Cmd("unban", aliases = ["undeny"]) @@ -40,7 +49,8 @@ class ParcelAddCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) shortVersion = "unbans a player from this parcel") @ParcelRequire(owner = true) fun ParcelScope.cmdUnban(sender: Player, player: OfflinePlayer): Any? { - TODO() + Validate.isTrue(parcel.unban(player), "${player.name} is not currently banned from this parcel") + return "${player.name} is not banned from this parcel anymore" } }
\ No newline at end of file diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelAdminCommands.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelAdminCommands.kt new file mode 100644 index 0000000..79e4474 --- /dev/null +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelAdminCommands.kt @@ -0,0 +1,7 @@ +package io.dico.parcels2.command + +import io.dico.parcels2.ParcelsPlugin + +class ParcelAdminCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { + +}
\ No newline at end of file diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt index 964e282..3cd6e1c 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt @@ -9,6 +9,7 @@ import io.dico.parcels2.logger fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher { //@formatter:off return CommandBuilder() + .setChatController(ParcelsChatController()) .addParameterType(false, ParcelParameterType(plugin.worlds)) .addParameterType(true, ParcelHomeParameterType(plugin.worlds)) @@ -21,6 +22,10 @@ fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher { .registerCommands(ParcelOptionCommands(plugin)) .parent() + .group("admin", "a") + .registerCommands(ParcelAdminCommands(plugin)) + .parent() + .putDebugCommands(plugin) .parent() diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelsChatController.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelsChatController.kt new file mode 100644 index 0000000..5b2d25d --- /dev/null +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelsChatController.kt @@ -0,0 +1,9 @@ +package io.dico.parcels2.command + +import io.dico.dicore.command.chat.AbstractChatController + +class ParcelsChatController : AbstractChatController() { + + + +}
\ No newline at end of file diff --git a/src/main/kotlin/io/dico/parcels2/util/PlayerExtensions.kt b/src/main/kotlin/io/dico/parcels2/util/PlayerExtensions.kt index 9054e3f..6597441 100644 --- a/src/main/kotlin/io/dico/parcels2/util/PlayerExtensions.kt +++ b/src/main/kotlin/io/dico/parcels2/util/PlayerExtensions.kt @@ -8,6 +8,9 @@ import org.bukkit.entity.Player import org.bukkit.plugin.java.JavaPlugin inline val OfflinePlayer.uuid get() = uniqueId +@Suppress("UsePropertyAccessSyntax") +inline val OfflinePlayer.isValid get() = isOnline() || hasPlayedBefore() + inline val Player.hasBanBypass get() = hasPermission("parcels.admin.bypass.ban") inline val Player.hasGamemodeBypass get() = hasPermission("parcels.admin.bypass.gamemode") inline val Player.hasBuildAnywhere get() = hasPermission("parcels.admin.bypass.build") diff --git a/src/main/kotlin/io/dico/parcels2/util/UUIDUtil.kt b/src/main/kotlin/io/dico/parcels2/util/UUIDUtil.kt index 6cdbe3a..10fbbbb 100644 --- a/src/main/kotlin/io/dico/parcels2/util/UUIDUtil.kt +++ b/src/main/kotlin/io/dico/parcels2/util/UUIDUtil.kt @@ -7,7 +7,7 @@ import java.util.* @Suppress("UsePropertyAccessSyntax") fun getPlayerName(uuid: UUID?, ifUnknown: String? = null): String { - return uuid?.let { Bukkit.getOfflinePlayer(uuid)?.takeIf { it.isOnline() || it.hasPlayedBefore() }?.name } + return uuid?.let { Bukkit.getOfflinePlayer(uuid)?.takeIf { it.isValid }?.name } ?: ifUnknown ?: ":unknown_name:" } |