Skip to content

Commit 1b54eea

Browse files
committed
Added StackUtils#isSimilar, refactored tests to use static imports
1 parent 5c2d66a commit 1b54eea

File tree

18 files changed

+313
-180
lines changed

18 files changed

+313
-180
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>io.github.mooy1</groupId>
88
<artifactId>InfinityLib</artifactId>
9-
<version>1.3.6</version>
9+
<version>1.3.7</version>
1010

1111
<properties>
1212
<maven.compiler.source>1.8</maven.compiler.source>

src/main/java/io/github/mooy1/infinitylib/common/StackUtils.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,48 @@ public static ItemStack itemByIdOrType(String idOrType) {
6262
return item == null ? new ItemStack(Material.valueOf(idOrType)) : item.getItem().clone();
6363
}
6464

65+
/**
66+
* Returns true when both items:
67+
* - Are null or air
68+
* - Have the same slimefun id
69+
* - Have the same type and display name or lack thereof
70+
*/
71+
public static boolean isSimilar(@Nullable ItemStack first, @Nullable ItemStack second) {
72+
if (first == null || first.getType().isAir()) {
73+
return second == null || second.getType().isAir();
74+
} else if (second == null || second.getType().isAir()) {
75+
return false;
76+
} else if (first.hasItemMeta()) {
77+
if (second.hasItemMeta()) {
78+
ItemMeta firstMeta = first.getItemMeta();
79+
ItemMeta secondMeta = second.getItemMeta();
80+
String firstId = getId(firstMeta);
81+
if (firstId == null) {
82+
if (getId(secondMeta) == null) {
83+
if (first.getType() == second.getType()) {
84+
if (firstMeta.hasDisplayName()) {
85+
return secondMeta.hasDisplayName()
86+
&& firstMeta.getDisplayName().equals(secondMeta.getDisplayName());
87+
} else {
88+
return !secondMeta.hasDisplayName();
89+
}
90+
} else {
91+
return false;
92+
}
93+
} else {
94+
return false;
95+
}
96+
} else {
97+
return firstId.equals(getId(secondMeta));
98+
}
99+
} else {
100+
return false;
101+
}
102+
} else if (second.hasItemMeta()) {
103+
return false;
104+
} else {
105+
return first.getType() == second.getType();
106+
}
107+
}
108+
65109
}

src/main/java/io/github/mooy1/infinitylib/machines/CraftingBlockRecipe.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@
55
import org.bukkit.entity.Player;
66
import org.bukkit.inventory.ItemStack;
77

8+
import io.github.mooy1.infinitylib.common.StackUtils;
89
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
910
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.ItemStackSnapshot;
1011
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.ItemUtils;
11-
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
1212

1313
@Getter
1414
public final class CraftingBlockRecipe {
1515

16-
private final ItemStack[] inputs;
16+
private final ItemStack[] recipe;
1717
final ItemStack output;
1818
final SlimefunItem item;
1919

20-
CraftingBlockRecipe(ItemStack output, ItemStack[] inputs) {
20+
CraftingBlockRecipe(ItemStack output, ItemStack[] recipe) {
2121
this.output = output;
22-
this.inputs = ItemStackSnapshot.wrapArray(inputs);
22+
this.recipe = ItemStackSnapshot.wrapArray(recipe);
2323
this.item = SlimefunItem.getByItem(output);
2424
}
2525

2626
boolean check(ItemStackSnapshot[] input) {
27-
for (int i = 0; i < inputs.length; i++) {
28-
if (!SlimefunUtils.isItemSimilar(input[i], inputs[i], false, true)) {
27+
for (int i = 0; i < recipe.length; i++) {
28+
boolean similar = StackUtils.isSimilar(input[i], recipe[i]);
29+
if (!similar && (recipe[i] == null || recipe[i].getAmount() > input[i].getAmount())) {
2930
return false;
3031
}
3132
}
@@ -37,9 +38,9 @@ boolean check(Player p) {
3738
}
3839

3940
void consume(ItemStack[] input) {
40-
for (int i = 0; i < inputs.length; i++) {
41-
if (inputs[i] != null) {
42-
ItemUtils.consumeItem(input[i], inputs[i].getAmount(), true);
41+
for (int i = 0; i < recipe.length; i++) {
42+
if (recipe[i] != null) {
43+
ItemUtils.consumeItem(input[i], recipe[i].getAmount(), true);
4344
}
4445
}
4546
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package io.github.mooy1.infinitylib;
22

3-
import org.junit.jupiter.api.Assertions;
43
import org.junit.jupiter.api.Test;
54

5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
7+
68
class TestInfinityLib {
79

810
@Test
911
void testVersion() {
10-
Assertions.assertNotEquals("${project.version}", InfinityLib.VERSION);
12+
assertNotEquals("${project.version}", InfinityLib.VERSION);
1113
}
1214

1315
@Test
1416
void testPackage() {
15-
Assertions.assertEquals("io.github.mooy1.infinitylib", InfinityLib.PACKAGE);
17+
assertEquals("io.github.mooy1.infinitylib", InfinityLib.PACKAGE);
1618
}
1719

1820
@Test
1921
void testAddonPackage() {
20-
Assertions.assertEquals("io.github.mooy1", InfinityLib.ADDON_PACKAGE);
22+
assertEquals("io.github.mooy1", InfinityLib.ADDON_PACKAGE);
2123
}
2224

2325
}

src/test/java/io/github/mooy1/infinitylib/commands/TestAddonCommand.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.bukkit.command.CommandMap;
77
import org.bukkit.command.CommandSender;
88
import org.junit.jupiter.api.AfterAll;
9-
import org.junit.jupiter.api.Assertions;
109
import org.junit.jupiter.api.BeforeAll;
1110
import org.junit.jupiter.api.Test;
1211

@@ -41,27 +40,27 @@ public static void unload() {
4140

4241
@Test
4342
void testNoSuchCommand() {
44-
Assertions.assertThrows(NullPointerException.class, () -> new AddonCommand("fail"));
43+
assertThrows(NullPointerException.class, () -> new AddonCommand("fail"));
4544
}
4645

4746
@Test
4847
void testAddParentToChildCommand() {
4948
ParentCommand child = new ParentCommand("child", "");
5049
ParentCommand parent = new ParentCommand("parent", "").addSub(child);
51-
Assertions.assertThrows(IllegalArgumentException.class, () -> child.addSub(parent));
50+
assertThrows(IllegalArgumentException.class, () -> child.addSub(parent));
5251
}
5352

5453
@Test
5554
void testAddSubCommandToSelf() {
5655
ParentCommand parent = new ParentCommand("parent", "");
57-
Assertions.assertThrows(IllegalArgumentException.class, () -> parent.addSub(parent));
56+
assertThrows(IllegalArgumentException.class, () -> parent.addSub(parent));
5857
}
5958

6059
@Test
6160
void testDuplicateSubCommand() {
6261
ParentCommand child = new ParentCommand("child", "");
6362
ParentCommand parent = new ParentCommand("parent", "").addSub(child);
64-
Assertions.assertThrows(IllegalArgumentException.class, () -> parent.addSub(child));
63+
assertThrows(IllegalArgumentException.class, () -> parent.addSub(child));
6564
}
6665

6766
@Test
@@ -86,8 +85,8 @@ void testParentCommand() {
8685
@Test
8786
void testDefaultCommands() {
8887
PlayerMock p = server.addPlayer();
89-
Assertions.assertTrue(getResponse(p, command, "info").contains("Info"));
90-
Assertions.assertTrue(getResponse(p, command, "aliases").contains("Aliases"));
88+
assertTrue(getResponse(p, command, "info").contains("Info"));
89+
assertTrue(getResponse(p, command, "aliases").contains("Aliases"));
9190
}
9291

9392
@Test
@@ -96,13 +95,13 @@ void testOpSubCommand() {
9695
addonCommand.addSub(new MockSubCommand("op", true));
9796

9897
PlayerMock p = server.addPlayer();
99-
Assertions.assertFalse(getResponse(p, command).contains(op));
98+
assertFalse(getResponse(p, command).contains(op));
10099
assertNoResponse(p, op);
101100
assertNoCompletion(p, op);
102101
assertNoCompletion(p, op, op);
103102

104103
p.setOp(true);
105-
Assertions.assertTrue(getResponse(p, command).contains(op));
104+
assertTrue(getResponse(p, command).contains(op));
106105
assertResponse(p, op);
107106
assertCompletion(p, op);
108107
assertCompletion(p, op, op);
@@ -114,13 +113,13 @@ void testPermissionSubCommand() {
114113
addonCommand.addSub(new MockSubCommand(perm, perm));
115114

116115
PlayerMock p = server.addPlayer();
117-
Assertions.assertFalse(getResponse(p, command).contains(perm));
116+
assertFalse(getResponse(p, command).contains(perm));
118117
assertNoResponse(p, perm);
119118
assertNoCompletion(p, perm);
120119
assertNoCompletion(p, perm, perm);
121120

122121
p.addAttachment(AbstractAddon.instance()).setPermission(perm, true);
123-
Assertions.assertTrue(getResponse(p, command).contains(perm));
122+
assertTrue(getResponse(p, command).contains(perm));
124123
assertResponse(p, perm);
125124
assertCompletion(p, perm);
126125
assertCompletion(p, perm, perm);
@@ -134,8 +133,8 @@ void testHelp() {
134133
String help1 = getResponse(p, command);
135134
String help2 = getResponse(p, command, help);
136135

137-
Assertions.assertTrue(help1.contains("Help"));
138-
Assertions.assertEquals(help1, help2);
136+
assertTrue(help1.contains("Help"));
137+
assertEquals(help1, help2);
139138
}
140139

141140
private static String getResponse(PlayerMock p, String... args) {
@@ -157,11 +156,11 @@ private static void assertNoResponse(CommandSender sender, String... args) {
157156
}
158157

159158
private static void assertCompletion(CommandSender sender, String completion, String... args) {
160-
Assertions.assertTrue(completions(sender, args).contains(completion));
159+
assertTrue(completions(sender, args).contains(completion));
161160
}
162161

163162
private static void assertNoCompletion(CommandSender sender, String completion, String... args) {
164-
Assertions.assertFalse(completions(sender, args).contains(completion));
163+
assertFalse(completions(sender, args).contains(completion));
165164
}
166165

167166
private static List<String> completions(CommandSender sender, String... args) {

src/test/java/io/github/mooy1/infinitylib/common/TestCoolDowns.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import java.util.UUID;
44

55
import org.junit.jupiter.api.AfterAll;
6-
import org.junit.jupiter.api.Assertions;
76
import org.junit.jupiter.api.BeforeAll;
87
import org.junit.jupiter.api.Test;
98

109
import be.seeseemelk.mockbukkit.MockBukkit;
1110
import be.seeseemelk.mockbukkit.ServerMock;
1211

12+
import static org.junit.jupiter.api.Assertions.assertFalse;
13+
import static org.junit.jupiter.api.Assertions.assertTrue;
14+
1315
class TestCoolDowns {
1416

1517
private static ServerMock server;
@@ -30,10 +32,10 @@ void testCheckAndReset() {
3032
CoolDowns max = new CoolDowns(System.currentTimeMillis());
3133
UUID uuid = server.addPlayer().getUniqueId();
3234

33-
Assertions.assertTrue(min.checkAndReset(uuid));
34-
Assertions.assertTrue(max.checkAndReset(uuid));
35-
Assertions.assertTrue(min.check(uuid));
36-
Assertions.assertFalse(max.check(uuid));
35+
assertTrue(min.checkAndReset(uuid));
36+
assertTrue(max.checkAndReset(uuid));
37+
assertTrue(min.check(uuid));
38+
assertFalse(max.check(uuid));
3739
}
3840

3941
}

src/test/java/io/github/mooy1/infinitylib/common/TestEvents.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
import org.bukkit.event.EventPriority;
77
import org.bukkit.event.Listener;
88
import org.junit.jupiter.api.AfterAll;
9-
import org.junit.jupiter.api.Assertions;
109
import org.junit.jupiter.api.BeforeAll;
1110
import org.junit.jupiter.api.Test;
1211

1312
import be.seeseemelk.mockbukkit.MockBukkit;
1413
import io.github.mooy1.infinitylib.core.MockAddon;
1514

15+
import static io.github.mooy1.infinitylib.common.Events.addHandler;
16+
import static io.github.mooy1.infinitylib.common.Events.call;
17+
import static io.github.mooy1.infinitylib.common.Events.registerListener;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
1620
class TestEvents implements Listener {
1721

1822
private static boolean listenerCalled;
@@ -30,23 +34,23 @@ public static void unload() {
3034

3135
@Test
3236
void testCallEvent() {
33-
Events.call(new MockEvent());
37+
call(new MockEvent());
3438
MockBukkit.getMock().getPluginManager().assertEventFired(MockEvent.class);
3539
}
3640

3741
@Test
3842
void testRegisterListener() {
39-
Events.registerListener(this);
40-
Events.call(new MockEvent());
41-
Assertions.assertTrue(listenerCalled);
43+
registerListener(this);
44+
call(new MockEvent());
45+
assertTrue(listenerCalled);
4246
}
4347

4448
@Test
4549
void testAddHandler() {
4650
AtomicBoolean called = new AtomicBoolean();
47-
Events.addHandler(MockEvent.class, EventPriority.MONITOR, true, e -> called.set(true));
48-
Events.call(new MockEvent());
49-
Assertions.assertTrue(called.get());
51+
addHandler(MockEvent.class, EventPriority.MONITOR, true, e -> called.set(true));
52+
call(new MockEvent());
53+
assertTrue(called.get());
5054
}
5155

5256
@EventHandler

0 commit comments

Comments
 (0)