1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Another one bites the Unit Tests

This commit is contained in:
TheBusyBiscuit 2020-05-12 00:50:01 +02:00
parent 05e36bf839
commit 471a271518
6 changed files with 112 additions and 37 deletions

View File

@ -20,8 +20,9 @@
## Release Candidate 12 (TBD)
#### Additions
* Added Unit Tests
* Added Ukrainian translations
* Added /sf backpack to restore lost backpacks
* Added automated Unit Tests
#### Changes
* Little performance improvements

View File

@ -150,6 +150,10 @@ public class SlimefunRegistry {
return enableResearches;
}
public void setFreeCreativeResearchingEnabled(boolean enabled) {
freeCreativeResearches = enabled;
}
public boolean isFreeCreativeResearchingEnabled() {
return freeCreativeResearches;
}

View File

@ -1,21 +1,18 @@
package io.github.thebusybiscuit.slimefun4.core.commands.subcommands;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.core.commands.SlimefunCommand;
import io.github.thebusybiscuit.slimefun4.core.commands.SubCommand;
import io.github.thebusybiscuit.slimefun4.utils.PatternUtils;
import me.mrCookieSlime.Slimefun.Lists.SlimefunItems;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Lists.SlimefunItems;
import me.mrCookieSlime.Slimefun.api.Slimefun;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.List;
class BackpackCommand extends SubCommand {
@ -44,19 +41,23 @@ class BackpackCommand extends SubCommand {
SlimefunPlugin.getLocal().sendMessage(sender, "messages.no-permission", true);
return;
}
if (args.length != 3) {
SlimefunPlugin.getLocal().sendMessage(sender, "messages.usage", true,
msg -> msg.replace("%usage%", "/sf backpack <Player> <ID>"));
SlimefunPlugin.getLocal().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf backpack <Player> <ID>"));
return;
}
final Player p = (Player) sender;
Player p = (Player) sender;
if (!PatternUtils.NUMERIC.matcher(args[2]).matches()) {
SlimefunPlugin.getLocal().sendMessage(sender, "commands.backpack.invalid-id");
return;
}
final int id = Integer.parseInt(args[2]);
final OfflinePlayer owner = Bukkit.getOfflinePlayer(args[1]);
int id = Integer.parseInt(args[2]);
@SuppressWarnings("deprecation")
OfflinePlayer owner = Bukkit.getOfflinePlayer(args[1]);
if (!owner.hasPlayedBefore()) {
SlimefunPlugin.getLocal().sendMessage(sender, "commands.backpack.player-never-joined");
return;
@ -67,6 +68,7 @@ class BackpackCommand extends SubCommand {
SlimefunPlugin.getLocal().sendMessage(sender, "commands.backpack.backpack-does-not-exist");
return;
}
Slimefun.runSync(() -> {
ItemStack item = SlimefunItems.RESTORED_BACKPACK.clone();
SlimefunPlugin.getBackpackListener().setBackpackId(p, item, 2, id);

View File

@ -21,13 +21,14 @@ import org.bukkit.inventory.ItemStack;
public class RestoredBackpack extends SlimefunBackpack {
/**
* This will create a new {@link SlimefunBackpack} with the given arguments.
* This will create a new {@link SlimefunBackpack} with the given arguments.
*
* @param category
* the category to bind this {@link SlimefunBackpack} to
*/
public RestoredBackpack(Category category) {
super(54, category, SlimefunItems.RESTORED_BACKPACK, RecipeType.NULL, new ItemStack[9]);
this.hidden = true;
}
}

View File

@ -1,5 +1,24 @@
package io.github.thebusybiscuit.slimefun4.implementation.setup;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import io.github.thebusybiscuit.cscorelib2.inventory.InvUtils;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.cscorelib2.materials.MaterialCollections;
@ -157,6 +176,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.SeismicAx
import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.SwordOfBeheading;
import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.VampireBlade;
import me.mrCookieSlime.CSCoreLibPlugin.CSCoreLib;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Lists.SlimefunItems;
import me.mrCookieSlime.Slimefun.Objects.Category;
@ -164,28 +184,9 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunMachine;
import me.mrCookieSlime.Slimefun.Objects.handlers.MultiBlockInteractionHandler;
import me.mrCookieSlime.Slimefun.Objects.handlers.RainbowTicker;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.Slimefun;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList;
import java.util.List;
/**
* This static utility class holds the recipes of all items.

View File

@ -2,14 +2,17 @@ package io.github.thebusybiscuit.slimefun4.tests.researches;
import java.util.Optional;
import org.bukkit.GameMode;
import org.bukkit.Material;
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.api.Test;
import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.core.researching.Research;
import io.github.thebusybiscuit.slimefun4.mocks.TestUtilities;
@ -18,11 +21,12 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
public class TestResearches {
private static ServerMock server;
private static SlimefunPlugin plugin;
@BeforeAll
public static void load() {
MockBukkit.mock();
server = MockBukkit.mock();
plugin = MockBukkit.load(SlimefunPlugin.class);
}
@ -103,4 +107,66 @@ public class TestResearches {
SlimefunPlugin.getRegistry().setResearchingEnabled(true);
}
@Test
public void testAddItems() {
NamespacedKey key = new NamespacedKey(plugin, "addItemsResearch");
Research research = new Research(key, 17, "Test", 100);
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "RESEARCH_ITEMS_TEST", new CustomItem(Material.LAPIS_LAZULI, "&9Adding items is fun"));
item.register(plugin);
research.addItems(item.getItem(), null);
Assertions.assertTrue(research.getAffectedItems().contains(item));
Assertions.assertEquals(research, item.getResearch());
}
@Test
public void testPlayerCanUnlockDisabledResearch() {
SlimefunPlugin.getRegistry().setResearchingEnabled(false);
Player player = server.addPlayer();
NamespacedKey key = new NamespacedKey(plugin, "disabledUnlockableResearch");
Research research = new Research(key, 567, "Test", 100);
Assertions.assertTrue(research.canUnlock(player));
SlimefunPlugin.getRegistry().setResearchingEnabled(true);
}
@Test
public void testFreeCreativeResearch() {
SlimefunPlugin.getRegistry().setResearchingEnabled(true);
Player player = server.addPlayer();
NamespacedKey key = new NamespacedKey(plugin, "freeCreativeResearch");
Research research = new Research(key, 153, "Test", 100);
SlimefunPlugin.getRegistry().setFreeCreativeResearchingEnabled(false);
player.setGameMode(GameMode.SURVIVAL);
Assertions.assertFalse(research.canUnlock(player));
player.setGameMode(GameMode.CREATIVE);
Assertions.assertFalse(research.canUnlock(player));
SlimefunPlugin.getRegistry().setFreeCreativeResearchingEnabled(true);
player.setGameMode(GameMode.SURVIVAL);
Assertions.assertFalse(research.canUnlock(player));
player.setGameMode(GameMode.CREATIVE);
Assertions.assertTrue(research.canUnlock(player));
}
@Test
public void testUnlockableResearch() {
Player player = server.addPlayer();
NamespacedKey key = new NamespacedKey(plugin, "freeCreativeResearch");
Research research = new Research(key, 235, "Test", 4);
Assertions.assertFalse(research.canUnlock(player));
player.setLevel(8);
Assertions.assertTrue(research.canUnlock(player));
}
}