-
Notifications
You must be signed in to change notification settings - Fork 0
fix: require marketblock.admin for /marketblock #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5b74af7
fix: require marketblock.admin for /marketblock
ryanbarlow97 8e956fc
fix: save a new trade only if its answer beat a quit
ryanbarlow97 dc56120
fix: handle one /marketblock add answer at a time
ryanbarlow97 41dc88a
fix: let a late chat message through instead of dropping it
ryanbarlow97 83c6551
fix: recheck the admin permission under the conversation lock
ryanbarlow97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
45 changes: 45 additions & 0 deletions
45
src/main/java/net/tfminecraft/marketblock/manager/commands/TradeInput.java
This file contains hidden or 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,45 @@ | ||
| package net.tfminecraft.marketblock.manager.commands; | ||
|
|
||
| /** | ||
| * Parses the numbers typed during /marketblock add. Each method returns null when the text is not | ||
| * a usable value, so a typo or a value such as NaN or Infinity cannot reach a saved trade. | ||
| */ | ||
| public final class TradeInput { | ||
|
|
||
| public static final String CANCEL = "cancel"; | ||
|
|
||
| private TradeInput() { | ||
| } | ||
|
|
||
| public static boolean isCancel(String message) { | ||
| return message != null && message.trim().equalsIgnoreCase(CANCEL); | ||
| } | ||
|
|
||
| /** Demand limit: a finite number of at least 1, the lowest a trade allows. */ | ||
| public static Double demandLimit(String message) { | ||
| Double value = finite(message); | ||
| return value != null && value >= 1 ? value : null; | ||
| } | ||
|
|
||
| /** Price change: a finite number of at least 0. */ | ||
| public static Double priceChange(String message) { | ||
| Double value = finite(message); | ||
| return value != null && value >= 0 ? value : null; | ||
| } | ||
|
|
||
| /** Resting price: a finite number above 0. */ | ||
| public static Double restingPrice(String message) { | ||
| Double value = finite(message); | ||
| return value != null && value > 0 ? value : null; | ||
| } | ||
|
|
||
| private static Double finite(String message) { | ||
| if (message == null) return null; | ||
| try { | ||
| double value = Double.parseDouble(message.trim()); | ||
| return Double.isFinite(value) ? value : null; | ||
| } catch (NumberFormatException e) { | ||
| return null; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
89 changes: 89 additions & 0 deletions
89
src/test/java/net/tfminecraft/marketblock/manager/CommandManagerTest.java
This file contains hidden or 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,89 @@ | ||
| package net.tfminecraft.marketblock.manager; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.io.InputStreamReader; | ||
| import java.io.Reader; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
|
|
||
| import org.bukkit.command.Command; | ||
| import org.bukkit.command.CommandSender; | ||
| import org.bukkit.configuration.file.YamlConfiguration; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import net.tfminecraft.marketblock.manager.commands.ConversationManager; | ||
| import net.tfminecraft.marketblock.manager.commands.TabCompletion; | ||
|
|
||
| class CommandManagerTest { | ||
| private final CommandManager commands = new CommandManager(); | ||
| private final Command command = new Command("marketblock") { | ||
| @Override | ||
| public boolean execute(CommandSender sender, String label, String[] args) { | ||
| return false; | ||
| } | ||
| }; | ||
| private FakePlayer tester; | ||
|
|
||
| @AfterEach | ||
| void endConversation() { | ||
| if (tester != null) ConversationManager.endConversation(tester.player); | ||
| } | ||
|
|
||
| @Test | ||
| void aPlayerWithoutPermissionCannotStartAddingATrade() { | ||
| tester = new FakePlayer(false); | ||
|
|
||
| assertTrue(commands.onCommand(tester.player, command, "marketblock", new String[] { "add" })); | ||
|
|
||
| assertNull(ConversationManager.getConversation(tester.player)); | ||
| assertEquals("搂cYou do not have permission to use this command.", tester.lastMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void everySubcommandIsRefusedWithoutPermission() { | ||
| // reload would reach JavaPlugin.getPlugin and throw here if the check did not stop it first. | ||
| for (String sub : List.of("add", "delete", "reset", "resetall", "reload")) { | ||
| tester = new FakePlayer(false); | ||
|
|
||
| commands.onCommand(tester.player, command, "marketblock", new String[] { sub, "stone" }); | ||
|
|
||
| assertEquals(List.of("搂cYou do not have permission to use this command."), tester.messages, sub); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void anAdminGetsPastThePermissionCheck() { | ||
| tester = new FakePlayer(true); | ||
|
|
||
| commands.onCommand(tester.player, command, "marketblock", new String[] { "add" }); | ||
|
|
||
| assertEquals("搂cYou must hold an item in your hand to add a trade.", tester.lastMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void tabCompletionIsEmptyWithoutPermission() { | ||
| TabCompletion completion = new TabCompletion(); | ||
|
|
||
| assertTrue(completion.onTabComplete(new FakePlayer(false).player, command, "marketblock", new String[] { "" }).isEmpty()); | ||
| assertEquals(List.of("add", "delete", "reload", "reset", "resetall"), | ||
| completion.onTabComplete(new FakePlayer(true).player, command, "marketblock", new String[] { "" })); | ||
| } | ||
|
|
||
| @Test | ||
| void pluginYmlRequiresTheAdminPermission() throws Exception { | ||
| YamlConfiguration yaml; | ||
| try (Reader reader = new InputStreamReader( | ||
| getClass().getClassLoader().getResourceAsStream("plugin.yml"), StandardCharsets.UTF_8)) { | ||
| yaml = YamlConfiguration.loadConfiguration(reader); | ||
| } | ||
|
|
||
| assertEquals(CommandManager.ADMIN_PERMISSION, yaml.getString("commands.marketblock.permission")); | ||
| assertNotNull(yaml.getConfigurationSection("permissions." + CommandManager.ADMIN_PERMISSION)); | ||
| assertEquals("op", yaml.getString("permissions." + CommandManager.ADMIN_PERMISSION + ".default")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.