Improve testing and change playtime permissions

This commit is contained in:
okx-code
2021-06-25 18:18:29 +01:00
parent e125f962db
commit 59e97d3f61
21 changed files with 127 additions and 61 deletions
@@ -201,7 +201,7 @@ public class InfoCommand implements TabExecutor {
element = next;
}
return true;
} else if (args[0].equalsIgnoreCase("playtime") && (sender.hasPermission("rankup.playtime.get") || sender.hasPermission("rankup.playtime.set"))) {
} else if (args[0].equalsIgnoreCase("playtime") && (sender.hasPermission("rankup.playtime.get") || sender.hasPermission("rankup.playtime"))) {
Statistic playOneTick;
try {
playOneTick = Statistic.valueOf("PLAY_ONE_MINUTE");
@@ -241,7 +241,7 @@ public class InfoCommand implements TabExecutor {
}
player.sendMessage(ChatColor.LIGHT_PURPLE + who + " played for " + minutes + " minutes.");
return true;
} else if (args[1].equalsIgnoreCase("set") && sender.hasPermission("rankup.playtime.set")) {
} else if (args[1].equalsIgnoreCase("set") && sender.hasPermission("rankup.playtime")) {
if (args.length < 4) {
sender.sendMessage(ChatColor.GREEN + "/" + label + " " + args[0] + " set <player> <minutes>" + ChatColor.YELLOW + " Update the playtime statistic for a player");
return true;
@@ -264,7 +264,7 @@ public class InfoCommand implements TabExecutor {
player.setStatistic(playOneTick, minutes * 20 * 60);
player.sendMessage(ChatColor.LIGHT_PURPLE + "Updated playtime for " + player.getName() + " to " + minutes + " minutes");
return true;
} else if (args[1].equalsIgnoreCase("add") && sender.hasPermission("rankup.playtime.set")) {
} else if (args[1].equalsIgnoreCase("add") && sender.hasPermission("rankup.playtime")) {
if (args.length < 4) {
sender.sendMessage(ChatColor.GREEN + "/" + label + " " + args[0] + " add <player> <minutes>" + ChatColor.YELLOW + " Increase the playtime statistic for a player");
return true;
@@ -304,7 +304,7 @@ public class InfoCommand implements TabExecutor {
ChatColor.GREEN + "/" + label + " " + args[0] + " get [player] " + ChatColor.YELLOW
+ " Get amount of minutes played");
}
if (sender.hasPermission("rankup.playtime.set")) {
if (sender.hasPermission("rankup.playtime")) {
sender.sendMessage(
ChatColor.GREEN + "/" + label + " " + args[0] + " set <player> <minutes>"
+ ChatColor.YELLOW + " Update the playtime statistic for a player");
@@ -356,7 +356,7 @@ public class InfoCommand implements TabExecutor {
list.add("forceprestige");
list.add("rankdown");
}
if (sender.hasPermission("rankup.playtime.get") || sender.hasPermission("rankup.playtime.set")) {
if (sender.hasPermission("rankup.playtime.get") || sender.hasPermission("rankup.playtime")) {
list.add("playtime");
}
return StringUtil.copyPartialMatches(args[0], list, new ArrayList<>());
@@ -372,7 +372,7 @@ public class InfoCommand implements TabExecutor {
if (sender.hasPermission("rankup.playtime.get")) {
options.add("get");
}
if (sender.hasPermission("rankup.playtime.set")) {
if (sender.hasPermission("rankup.playtime")) {
options.add("set");
options.add("add");
}
@@ -33,14 +33,13 @@ public class PebbleMessageBuilder implements MessageBuilder {
}
private void replaceInitial() {
Function<Player, Object> lastMinute = player -> {
lastMinuteContext.put("ranks", player -> {
List<RankContext> ranks = new ArrayList<>();
for (Rank rank : plugin.getRankups().getTree()) {
ranks.add(new RankContext(plugin, player, rank));
}
return ranks;
};
lastMinuteContext.put("ranks", lastMinute);
});
lastMinuteContext.put("player", HumanEntity::getName);
}
+4 -1
View File
@@ -61,7 +61,10 @@ permissions:
rankup.maxrankup:
default: true
rankup.playtime:
description: Use all /rankup3 playtime subcommands for anyone.
children:
rankup.playtime.get: true
rankup.playtime.set: true
default: op
rankup.playtime.get:
description: Use /rankup3 playtime get for anyone.
default: op
@@ -1,10 +1,11 @@
package sh.okx.rankup;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import sh.okx.rankup.messages.Message;
import sh.okx.rankup.ranks.Rank;
import sh.okx.rankup.ranks.RankElement;
@@ -1,10 +1,11 @@
package sh.okx.rankup;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class RankupCommandTest extends RankupTest {
@Test
@@ -1,9 +1,10 @@
package sh.okx.rankup;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import sh.okx.rankup.placeholders.RankupExpansion;
public class RankupPlaceholderTest extends RankupTest {
+6 -7
View File
@@ -10,12 +10,11 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import org.junit.After;
import org.junit.Before;
import sh.okx.rankup.economy.TestEconomyProvider;
import org.junit.jupiter.api.BeforeEach;
import sh.okx.rankup.providers.TestEconomyProvider;
import sh.okx.rankup.hook.GroupProvider;
import sh.okx.rankup.hook.TestGroupProvider;
import sh.okx.rankup.hook.TestPermissionManager;
import sh.okx.rankup.providers.TestGroupProvider;
import sh.okx.rankup.providers.TestPermissionManager;
public abstract class RankupTest {
private final File testResourceFolder;
@@ -37,7 +36,7 @@ public abstract class RankupTest {
protected ServerMock server;
protected RankupPlugin plugin;
@Before
@BeforeEach
public void setup() {
System.setProperty("RANKUP_TEST", "true");
@@ -77,7 +76,7 @@ public abstract class RankupTest {
}
}
@After
@BeforeEach
public void tearDown() {
MockBukkit.unmock();
System.clearProperty("RANKUP_TEST");
@@ -0,0 +1,56 @@
package sh.okx.rankup.commands;
import static org.junit.jupiter.api.Assertions.*;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.bukkit.ChatColor;
import org.bukkit.Statistic;
import org.junit.jupiter.api.Test;
import sh.okx.rankup.RankupTest;
public class ComandPlaytimeTest extends RankupTest {
@Test
public void testAdd() {
PlayerMock player = server.addPlayer();
player.setStatistic(Statistic.PLAY_ONE_MINUTE, ticks(10));
player.addAttachment(plugin, "rankup.playtime", true);
player.performCommand("pru playtime add " + player.getName() + " 20");
assertEquals(ticks(30), player.getStatistic(Statistic.PLAY_ONE_MINUTE));
}
@Test
public void testSet() {
PlayerMock player = server.addPlayer();
player.setStatistic(Statistic.PLAY_ONE_MINUTE, ticks(20));
player.addAttachment(plugin, "rankup.playtime", true);
player.performCommand("pru playtime set " + player.getName() + " 25");
assertEquals(ticks(25), player.getStatistic(Statistic.PLAY_ONE_MINUTE));
}
@Test
public void testGetSelf() {
PlayerMock player = server.addPlayer();
player.setStatistic(Statistic.PLAY_ONE_MINUTE, ticks(5));
player.addAttachment(plugin, "rankup.playtime.get", true);
player.performCommand("pru playtime get " + player.getName());
player.assertSaid(ChatColor.LIGHT_PURPLE + "You have played for 5 minutes.");
player.assertNoMoreSaid();
}
private int ticks(int minutes) {
return minutes * 20 * 60;
}
private int minutes(int ticks) {
return ticks / 20 / 60;
}
}
@@ -2,7 +2,7 @@ package sh.okx.rankup.legacy;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import java.text.DecimalFormat;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import sh.okx.rankup.RankupTest;
public class LegacyPlaceholderTest extends RankupTest {
@@ -1,13 +1,13 @@
package sh.okx.rankup.messages;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class MessageBuilderTest {
@Test
public void testFailIfEmpty() {
assertThat(new StringMessageBuilder("").failIfEmpty(), instanceOf(NullMessageBuilder.class));
assertTrue(new StringMessageBuilder("").failIfEmpty() instanceof NullMessageBuilder);
}
}
@@ -1,10 +1,10 @@
package sh.okx.rankup.messages;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import sh.okx.rankup.RankupTest;
public class RankupPlaceholderTest extends RankupTest {
@@ -1,12 +1,13 @@
package sh.okx.rankup.pebble;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import sh.okx.rankup.text.pebble.PebbleTextProcessor;
public class PebbleTest {
@@ -1,10 +1,11 @@
package sh.okx.rankup.economy;
package sh.okx.rankup.providers;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import sh.okx.rankup.economy.Economy;
public class TestEconomy implements Economy {
private final Map<UUID, Double> balances = new HashMap<>();
@@ -1,4 +1,7 @@
package sh.okx.rankup.economy;
package sh.okx.rankup.providers;
import sh.okx.rankup.economy.Economy;
import sh.okx.rankup.economy.EconomyProvider;
public class TestEconomyProvider implements EconomyProvider {
@Override
@@ -1,9 +1,10 @@
package sh.okx.rankup.hook;
package sh.okx.rankup.providers;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.UUID;
import sh.okx.rankup.hook.GroupProvider;
public class TestGroupProvider implements GroupProvider {
private Multimap<UUID, String> groups = ArrayListMultimap.create();
@@ -1,4 +1,7 @@
package sh.okx.rankup.hook;
package sh.okx.rankup.providers;
import sh.okx.rankup.hook.GroupProvider;
import sh.okx.rankup.hook.PermissionManager;
public class TestPermissionManager implements PermissionManager {
private final GroupProvider groupProvider;