-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(paper): add a provided sender mapper
modern paper manager requires the use of CommandSourceStack which doesn't have built in ways to require a Player or Console sender. this adds a built-in way for common behaviour.
- Loading branch information
Showing
8 changed files
with
280 additions
and
9 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
cloud-paper/src/main/java/org/incendo/cloud/paper/sender/ConsoleSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Incendo | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
package org.incendo.cloud.paper.sender; | ||
|
||
import io.papermc.paper.command.brigadier.CommandSourceStack; | ||
import org.bukkit.command.ConsoleCommandSender; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
public final class ConsoleSender extends GenericSender { | ||
|
||
ConsoleSender(final CommandSourceStack commandSourceStack) { | ||
super(commandSourceStack); | ||
} | ||
|
||
@Override | ||
public @NonNull ConsoleCommandSender sender() { | ||
return (ConsoleCommandSender) super.sender(); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
cloud-paper/src/main/java/org/incendo/cloud/paper/sender/GenericSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Incendo | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
package org.incendo.cloud.paper.sender; | ||
|
||
import io.papermc.paper.command.brigadier.CommandSourceStack; | ||
import org.bukkit.command.CommandSender; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
public class GenericSender implements Sender { | ||
|
||
private final CommandSourceStack commandSourceStack; | ||
|
||
GenericSender(final @NonNull CommandSourceStack commandSourceStack) { | ||
this.commandSourceStack = commandSourceStack; | ||
} | ||
|
||
@Override | ||
public final @NonNull CommandSourceStack commandSourceStack() { | ||
return this.commandSourceStack; | ||
} | ||
|
||
/** | ||
* @see Sender#sender() | ||
*/ | ||
@Override | ||
public @NonNull CommandSender sender() { | ||
return this.commandSourceStack.getSender(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
cloud-paper/src/main/java/org/incendo/cloud/paper/sender/ModernProvidedSenderMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Incendo | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
package org.incendo.cloud.paper.sender; | ||
|
||
import io.papermc.paper.command.brigadier.CommandSourceStack; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.command.ConsoleCommandSender; | ||
import org.bukkit.entity.Player; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.incendo.cloud.SenderMapper; | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
public final class ModernProvidedSenderMapper implements SenderMapper<CommandSourceStack, Sender> { | ||
|
||
/** | ||
* Create a new instance of {@link ModernProvidedSenderMapper}. | ||
* | ||
* @return a new instance of {@link ModernProvidedSenderMapper} | ||
*/ | ||
public static @NonNull ModernProvidedSenderMapper providedSenderMapper() { | ||
return new ModernProvidedSenderMapper(); | ||
} | ||
|
||
ModernProvidedSenderMapper() { | ||
} | ||
|
||
@Override | ||
public @NonNull Sender map(final @NonNull CommandSourceStack base) { | ||
CommandSender commandSender = base.getSender(); | ||
|
||
if (commandSender instanceof ConsoleCommandSender) { | ||
return new ConsoleSender(base); | ||
} | ||
|
||
if (commandSender instanceof Player) { | ||
return new PlayerSender(base); | ||
} | ||
|
||
return new GenericSender(base); | ||
} | ||
|
||
@Override | ||
public @NonNull CommandSourceStack reverse(final @NonNull Sender mapped) { | ||
return mapped.commandSourceStack(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
cloud-paper/src/main/java/org/incendo/cloud/paper/sender/PlayerSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Incendo | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
package org.incendo.cloud.paper.sender; | ||
|
||
import io.papermc.paper.command.brigadier.CommandSourceStack; | ||
import org.bukkit.entity.Player; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
public final class PlayerSender extends GenericSender { | ||
|
||
PlayerSender(final CommandSourceStack commandSourceStack) { | ||
super(commandSourceStack); | ||
} | ||
|
||
@Override | ||
public @NonNull Player sender() { | ||
return (Player) super.sender(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
cloud-paper/src/main/java/org/incendo/cloud/paper/sender/Sender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Incendo | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
package org.incendo.cloud.paper.sender; | ||
|
||
import io.papermc.paper.command.brigadier.CommandSourceStack; | ||
import org.bukkit.command.CommandSender; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
public interface Sender { | ||
|
||
/** | ||
* Gets the command source stack. | ||
* | ||
* @return the command source stack | ||
*/ | ||
@NonNull CommandSourceStack commandSourceStack(); | ||
|
||
/** | ||
* Gets the underlying command sender from the command source stack. | ||
* | ||
* @return the sender. | ||
*/ | ||
@NonNull CommandSender sender(); | ||
} |
4 changes: 4 additions & 0 deletions
4
cloud-paper/src/main/java/org/incendo/cloud/paper/sender/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Built in CommandSender mappings for Modern Paper. | ||
*/ | ||
package org.incendo.cloud.paper.sender; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters