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

[CI skip] Added more unit tests to cover multiblocks

This commit is contained in:
TheBusyBiscuit 2020-05-17 16:41:16 +02:00
parent d7a7230d6c
commit e3ba751cf9
4 changed files with 109 additions and 21 deletions

View File

@ -1,10 +1,12 @@
package io.github.thebusybiscuit.slimefun4.api.events;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import io.github.thebusybiscuit.slimefun4.core.MultiBlock;
@ -14,13 +16,13 @@ import io.github.thebusybiscuit.slimefun4.core.MultiBlock;
* @author TheBusyBiscuit
*
*/
public class MultiBlockInteractEvent extends Event implements Cancellable {
public class MultiBlockInteractEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Player player;
private final MultiBlock multiBlock;
private final Block clickedBlock;
private final BlockFace clickedFace;
private boolean cancelled;
public HandlerList getHandlers() {
@ -31,19 +33,11 @@ public class MultiBlockInteractEvent extends Event implements Cancellable {
return handlers;
}
public MultiBlockInteractEvent(Player p, MultiBlock mb, Block clicked) {
this.player = p;
public MultiBlockInteractEvent(Player p, MultiBlock mb, Block clicked, BlockFace face) {
super(p);
this.multiBlock = mb;
this.clickedBlock = clicked;
}
/**
* This returns the {@link Player} who interacted with our {@link MultiBlock}
*
* @return The {@link Player} who interacted with the {@link MultiBlock}
*/
public Player getPlayer() {
return player;
this.clickedFace = face;
}
/**
@ -64,6 +58,15 @@ public class MultiBlockInteractEvent extends Event implements Cancellable {
return clickedBlock;
}
/**
* This returns the {@link BlockFace} that was clicked.
*
* @return The {@link BlockFace} that was clicked
*/
public BlockFace getClickedFace() {
return clickedFace;
}
@Override
public boolean isCancelled() {
return cancelled;

View File

@ -38,7 +38,9 @@ public class MultiBlockListener implements Listener {
@EventHandler
public void onRightClick(PlayerInteractEvent e) {
if (e.getAction() != Action.RIGHT_CLICK_BLOCK || e.getHand() != EquipmentSlot.HAND) return;
if (e.getAction() != Action.RIGHT_CLICK_BLOCK || e.getHand() != EquipmentSlot.HAND) {
return;
}
Player p = e.getPlayer();
Block b = e.getClickedBlock();
@ -57,7 +59,7 @@ public class MultiBlockListener implements Listener {
MultiBlock mb = multiblocks.getLast();
mb.getSlimefunItem().callItemHandler(MultiBlockInteractionHandler.class, handler -> handler.onInteract(p, mb, b));
Bukkit.getPluginManager().callEvent(new MultiBlockInteractEvent(p, mb, b));
Bukkit.getPluginManager().callEvent(new MultiBlockInteractEvent(p, mb, b, e.getBlockFace()));
}
}

View File

@ -0,0 +1,89 @@
package io.github.thebusybiscuit.slimefun4.tests.listeners;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
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.api.events.MultiBlockInteractEvent;
import io.github.thebusybiscuit.slimefun4.core.MultiBlock;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.MultiBlockListener;
import io.github.thebusybiscuit.slimefun4.mocks.TestUtilities;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
public class TestMultiblockListener {
private static SlimefunPlugin plugin;
private static MultiBlockListener listener;
private static MultiBlock multiblock;
private static ServerMock server;
@BeforeAll
public static void load() {
server = MockBukkit.mock();
plugin = MockBukkit.load(SlimefunPlugin.class);
listener = new MultiBlockListener(plugin);
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "MULTIBLOCK_LISTENER_TEST", new CustomItem(Material.DIAMOND, "&9Some multiblock item"));
multiblock = new MultiBlock(item, new Material[] { null, Material.EMERALD_BLOCK, null, null, Material.DIAMOND_BLOCK, null, null, Material.LAPIS_BLOCK, null }, BlockFace.SELF);
SlimefunPlugin.getRegistry().getMultiBlocks().add(multiblock);
}
@AfterAll
public static void unload() {
MockBukkit.unmock();
}
@Test
public void testNoMultiblock() {
Player player = server.addPlayer();
World world = server.addSimpleWorld("Multiblock Test World");
Block b = world.getBlockAt(3456, 90, -100);
b.setType(Material.STONE);
PlayerInteractEvent event = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, new ItemStack(Material.AIR), b, BlockFace.NORTH);
listener.onRightClick(event);
// No Multiblock, so nothing should happen
Assertions.assertEquals(Result.ALLOW, event.useInteractedBlock());
}
@Test
public void testMultiblock() {
Player player = server.addPlayer();
World world = server.addSimpleWorld("Multiblock Test World");
Block top = world.getBlockAt(1234, 92, -60);
top.setType(multiblock.getStructure()[1]);
Block self = world.getBlockAt(1234, 91, -60);
self.setType(multiblock.getStructure()[4]);
Block bottom = world.getBlockAt(1234, 90, -60);
bottom.setType(multiblock.getStructure()[7]);
PlayerInteractEvent event = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, new ItemStack(Material.AIR), self, BlockFace.NORTH);
listener.onRightClick(event);
Assertions.assertEquals(Result.DENY, event.useInteractedBlock());
server.getPluginManager().assertEventFired(MultiBlockInteractEvent.class, e -> {
Assertions.assertEquals(player, e.getPlayer());
Assertions.assertEquals(self, e.getClickedBlock());
Assertions.assertEquals(BlockFace.NORTH, e.getClickedFace());
Assertions.assertEquals(multiblock, e.getMultiBlock());
return true;
});
}
}

View File

@ -56,11 +56,9 @@ public class TestMultiBlocks {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "MULTIBLOCK_TEST", new CustomItem(Material.BRICK, "&5Multiblock Test"));
MultiBlock multiblock = new MultiBlock(item, new Material[] { null, null, null, Material.DIAMOND_BLOCK, null, Material.DIAMOND_BLOCK, null, Material.DISPENSER, null }, BlockFace.DOWN);
Assertions.assertTrue(multiblock.isSymmetric());
MultiBlock multiblock2 = new MultiBlock(item, new Material[] { Material.EMERALD_BLOCK, null, null, Material.EMERALD_BLOCK, null, Material.DIAMOND_BLOCK, Material.EMERALD_BLOCK, Material.DISPENSER, null }, BlockFace.DOWN);
Assertions.assertFalse(multiblock2.isSymmetric());
}
@ -69,13 +67,9 @@ public class TestMultiBlocks {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "MULTIBLOCK_TEST", new CustomItem(Material.BRICK, "&5Multiblock Test"));
MultiBlock multiblock = new MultiBlock(item, new Material[] { Material.BIRCH_WOOD, Material.BIRCH_WOOD, Material.BIRCH_WOOD, null, Material.CRAFTING_TABLE, null, Material.BIRCH_WOOD, Material.DISPENSER, Material.BIRCH_WOOD }, BlockFace.DOWN);
MultiBlock multiblock2 = new MultiBlock(item, new Material[] { Material.BIRCH_WOOD, Material.BIRCH_WOOD, Material.BIRCH_WOOD, null, Material.CRAFTING_TABLE, null, Material.BIRCH_WOOD, Material.DISPENSER, Material.BIRCH_WOOD }, BlockFace.DOWN);
MultiBlock multiblock3 = new MultiBlock(item, new Material[] { Material.BIRCH_WOOD, Material.BIRCH_WOOD, Material.BIRCH_WOOD, null, Material.EMERALD_BLOCK, null, Material.BIRCH_WOOD, Material.DISPENSER, Material.BIRCH_WOOD }, BlockFace.DOWN);
MultiBlock multiblock4 = new MultiBlock(item, new Material[] { Material.DROPPER, Material.BIRCH_WOOD, Material.BIRCH_WOOD, null, Material.DIAMOND_BLOCK, null, Material.BIRCH_WOOD, Material.DISPENSER, Material.TNT }, BlockFace.DOWN);
MultiBlock multiblock5 = new MultiBlock(item, new Material[] { Material.BIRCH_WOOD, Material.BIRCH_WOOD, Material.BIRCH_WOOD, null, Material.CRAFTING_TABLE, null, Material.BIRCH_WOOD, Material.DISPENSER, Material.BIRCH_WOOD }, BlockFace.SELF);
Assertions.assertTrue(multiblock.isSymmetric());