1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

[CI skip] Added a Research unlocking unit test

This commit is contained in:
TheBusyBiscuit 2020-05-17 14:54:16 +02:00
parent 65228fbeb1
commit c69a86ae42
4 changed files with 88 additions and 4 deletions

View File

@ -225,13 +225,11 @@ public class Research implements Keyed {
SlimefunPlugin.getLocal().sendMessage(p, "messages.research.progress", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, getName(p)).replace("%progress%", "0%"));
}, 10L);
}
PlayerProfile.get(p, profile -> {
if (!profile.hasUnlocked(this)) {
Slimefun.runSync(() -> {
ResearchUnlockEvent event = new ResearchUnlockEvent(p, this);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
if (instant) {
finishResearch(p, profile, callback);
@ -255,7 +253,7 @@ public class Research implements Keyed {
profile.setResearched(this, true);
SlimefunPlugin.getLocal().sendMessage(p, "messages.unlocked", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, getName(p)));
callback.accept(p);
if (SlimefunPlugin.getRegistry().isResearchFireworkEnabled() && SlimefunGuideSettings.hasFireworksEnabled(p)) {
FireworkUtils.launchRandom(p, 1);
}

View File

@ -18,6 +18,7 @@ import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.chat.ChatColors;
import io.github.thebusybiscuit.cscorelib2.config.Localization;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.api.SlimefunBranch;
import io.github.thebusybiscuit.slimefun4.core.services.LocalizationService;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
@ -141,7 +142,7 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
List<String> lore = config.getStringList(key.getNamespace() + "." + key.getKey() + ".lore");
lore.replaceAll(line -> ChatColor.GRAY + line);
meta.setLore(lore);
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
});
@ -170,9 +171,14 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
@Override
public void sendMessage(CommandSender sender, String key, boolean addPrefix, UnaryOperator<String> function) {
if (SlimefunPlugin.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) {
return;
}
String prefix = addPrefix ? getPrefix() : "";
if (sender instanceof Player) {
System.out.println(function.apply(getMessage((Player) sender, key)));
sender.sendMessage(ChatColors.color(prefix + function.apply(getMessage((Player) sender, key))));
}
else {

View File

@ -9,6 +9,7 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitTask;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
@ -223,10 +224,20 @@ public final class Slimefun {
}
public static BukkitTask runSync(Runnable r) {
if (SlimefunPlugin.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) {
r.run();
return null;
}
return Bukkit.getScheduler().runTask(SlimefunPlugin.instance, r);
}
public static BukkitTask runSync(Runnable r, long delay) {
if (SlimefunPlugin.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) {
r.run();
return null;
}
return Bukkit.getScheduler().runTaskLater(SlimefunPlugin.instance, r, delay);
}
}

View File

@ -0,0 +1,69 @@
package io.github.thebusybiscuit.slimefun4.tests.researches;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock;
import io.github.thebusybiscuit.slimefun4.api.events.ResearchUnlockEvent;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.core.researching.Research;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
public class TestResearchUnlocking {
private static ServerMock server;
private static SlimefunPlugin plugin;
@BeforeAll
public static void load() throws InterruptedException {
server = MockBukkit.mock();
plugin = MockBukkit.load(SlimefunPlugin.class);
}
@AfterAll
public static void unload() {
MockBukkit.unmock();
}
private Player awaitUnlock(Player player, Research research, boolean instant) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Player> ref = new AtomicReference<>();
// This loads the profile asynchronously
research.unlock(player, instant, p -> {
ref.set(p);
latch.countDown();
});
latch.await(10, TimeUnit.SECONDS);
return ref.get();
}
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testUnlock(boolean instant) throws InterruptedException {
SlimefunPlugin.getRegistry().setResearchingEnabled(true);
Player player = server.addPlayer();
Research research = new Research(new NamespacedKey(plugin, "instant_unlock"), 1842, "Instant", 500);
Player p = awaitUnlock(player, research, instant);
Optional<PlayerProfile> profile = PlayerProfile.find(p);
server.getPluginManager().assertEventFired(ResearchUnlockEvent.class);
Assertions.assertEquals(player, p);
Assertions.assertTrue(profile.isPresent());
Assertions.assertTrue(profile.get().hasUnlocked(research));
}
}