Skip to content

Commit 78c3223

Browse files
authored
Merge pull request #29 from BentoBoxWorld/develop
Release 1.13.0
2 parents c15c2e5 + 2101886 commit 78c3223

File tree

7 files changed

+149
-95
lines changed

7 files changed

+149
-95
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ jobs:
1414
- uses: actions/checkout@v3
1515
with:
1616
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
17-
- name: Set up JDK 17
17+
- name: Set up JDK 21
1818
uses: actions/setup-java@v3
1919
with:
20-
distribution: 'adopt'
21-
java-version: 17
20+
distribution: 'temurin'
21+
java-version: 21
2222
- name: Cache SonarCloud packages
2323
uses: actions/cache@v3
2424
with:
@@ -35,4 +35,5 @@ jobs:
3535
env:
3636
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
3737
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
38+
MAVEN_OPTS: "-XX:+EnableDynamicAgentLoading"
3839
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar

pom.xml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@
5555
<properties>
5656
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5757
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
58-
<java.version>17</java.version>
58+
<java.version>21</java.version>
5959
<!-- Non-minecraft related dependencies -->
60-
<powermock.version>2.0.9</powermock.version>
60+
<mockito.version>5.12.0</mockito.version>
6161
<!-- More visible way how to change dependency versions -->
62-
<spigot.version>1.20.3-R0.1-SNAPSHOT</spigot.version>
63-
<bentobox.version>2.0.0-SNAPSHOT</bentobox.version>
62+
<spigot.version>1.20.4-R0.1-SNAPSHOT</spigot.version>
63+
<bentobox.version>2.4.1-SNAPSHOT</bentobox.version>
6464
<!-- Revision variable removes warning about dynamic version -->
6565
<revision>${build.version}-SNAPSHOT</revision>
6666
<!-- Do not change unless you want different name for local builds. -->
6767
<build.number>-LOCAL</build.number>
6868
<!-- This allows to change between versions. -->
69-
<build.version>1.12.0</build.version>
69+
<build.version>1.13.0</build.version>
7070
<!-- Sonar Cloud -->
7171
<sonar.projectKey>BentoBoxWorld_addon-invSwitcher</sonar.projectKey>
7272
<sonar.organization>bentobox-world</sonar.organization>
@@ -142,19 +142,19 @@
142142
<dependency>
143143
<groupId>org.mockito</groupId>
144144
<artifactId>mockito-core</artifactId>
145-
<version>3.11.2</version>
145+
<version>${mockito.version}</version>
146146
<scope>test</scope>
147147
</dependency>
148148
<dependency>
149-
<groupId>org.powermock</groupId>
150-
<artifactId>powermock-module-junit4</artifactId>
151-
<version>${powermock.version}</version>
149+
<groupId>org.mockito</groupId>
150+
<artifactId>mockito-inline</artifactId>
151+
<version>5.0.0</version>
152152
<scope>test</scope>
153153
</dependency>
154154
<dependency>
155-
<groupId>org.powermock</groupId>
156-
<artifactId>powermock-api-mockito2</artifactId>
157-
<version>${powermock.version}</version>
155+
<groupId>junit</groupId>
156+
<artifactId>junit</artifactId>
157+
<version>4.13.2</version>
158158
<scope>test</scope>
159159
</dependency>
160160
<dependency>
@@ -202,15 +202,15 @@
202202
<plugin>
203203
<groupId>org.apache.maven.plugins</groupId>
204204
<artifactId>maven-compiler-plugin</artifactId>
205-
<version>3.7.0</version>
205+
<version>3.13.0</version>
206206
<configuration>
207207
<release>${java.version}</release>
208208
</configuration>
209209
</plugin>
210210
<plugin>
211211
<groupId>org.apache.maven.plugins</groupId>
212212
<artifactId>maven-surefire-plugin</artifactId>
213-
<version>3.0.0-M5</version>
213+
<version>3.3.0</version>
214214
<configuration>
215215
<argLine>
216216
${argLine}
@@ -283,7 +283,7 @@
283283
<plugin>
284284
<groupId>org.jacoco</groupId>
285285
<artifactId>jacoco-maven-plugin</artifactId>
286-
<version>0.8.10</version>
286+
<version>0.8.12</version>
287287
<configuration>
288288
<append>true</append>
289289
<excludes>

src/main/java/com/wasteofplastic/invswitcher/Store.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 - 2021 tastybento
2+
* Copyright (c) 2017 - 2024 tastybento
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy
55
* of this software and associated documentation files (the "Software"), to deal
@@ -58,11 +58,6 @@
5858
public class Store {
5959
private static final CharSequence THE_END = "_the_end";
6060
private static final CharSequence NETHER = "_nether";
61-
@SuppressWarnings("deprecation")
62-
private static final List<Material> BLOCKS = Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy()).toList();
63-
@SuppressWarnings("deprecation")
64-
private static final List<Material> ITEMS = Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy()).toList();
65-
private static final List<EntityType> LIVING_ENTITIES = Arrays.stream(EntityType.values()).filter(EntityType::isAlive).toList();
6661
private final Database<InventoryStorage> database;
6762
private final Map<UUID, InventoryStorage> cache;
6863
private final InvSwitcher addon;
@@ -265,21 +260,24 @@ private void saveStats(InventoryStorage store, Player player, String worldName)
265260
Map<EntityType, Integer> entMap;
266261
switch (s.getType()) {
267262
case BLOCK -> {
268-
map = BLOCKS.stream().filter(m -> player.getStatistic(s, m) > 0)
263+
map = Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy())
264+
.filter(m -> player.getStatistic(s, m) > 0)
269265
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
270266
if (!map.isEmpty()) {
271267
store.getBlockStats(worldName).put(s, map);
272268
}
273269
}
274270
case ITEM -> {
275-
map = ITEMS.stream().filter(m -> player.getStatistic(s, m) > 0)
271+
map = Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy())
272+
.filter(m -> player.getStatistic(s, m) > 0)
276273
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
277274
if (!map.isEmpty()) {
278275
store.getItemStats(worldName).put(s, map);
279276
}
280277
}
281278
case ENTITY -> {
282-
entMap = LIVING_ENTITIES.stream().filter(m -> player.getStatistic(s, m) > 0)
279+
entMap = Arrays.stream(EntityType.values()).filter(EntityType::isAlive)
280+
.filter(m -> player.getStatistic(s, m) > 0)
283281
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
284282
if (!entMap.isEmpty()) {
285283
store.getEntityStats(worldName).put(s, entMap);
@@ -364,11 +362,15 @@ private void resetAdv(Player player) {
364362
}
365363
}
366364

365+
@SuppressWarnings("deprecation")
367366
private void resetStats(Player player, Statistic s) {
368367
switch (s.getType()) {
369-
case BLOCK -> BLOCKS.forEach(m -> player.setStatistic(s, m, 0));
370-
case ITEM -> ITEMS.forEach(m -> player.setStatistic(s, m, 0));
371-
case ENTITY -> LIVING_ENTITIES.forEach(en -> player.setStatistic(s, en, 0));
368+
case BLOCK -> Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy())
369+
.forEach(m -> player.setStatistic(s, m, 0));
370+
case ITEM -> Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy())
371+
.forEach(m -> player.setStatistic(s, m, 0));
372+
case ENTITY ->
373+
Arrays.stream(EntityType.values()).filter(EntityType::isAlive).forEach(en -> player.setStatistic(s, en, 0));
372374
case UNTYPED -> player.setStatistic(s, 0);
373375
}
374376
}

src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.mockito.ArgumentMatchers.any;
77
import static org.mockito.ArgumentMatchers.anyString;
88
import static org.mockito.ArgumentMatchers.eq;
9+
import static org.mockito.Mockito.mockStatic;
910
import static org.mockito.Mockito.never;
1011
import static org.mockito.Mockito.times;
1112
import static org.mockito.Mockito.verify;
@@ -15,6 +16,7 @@
1516
import java.io.FileInputStream;
1617
import java.io.FileOutputStream;
1718
import java.io.IOException;
19+
import java.lang.reflect.Field;
1820
import java.nio.file.Files;
1921
import java.nio.file.Path;
2022
import java.nio.file.Paths;
@@ -34,10 +36,9 @@
3436
import org.junit.Test;
3537
import org.junit.runner.RunWith;
3638
import org.mockito.Mock;
37-
import org.powermock.api.mockito.PowerMockito;
38-
import org.powermock.core.classloader.annotations.PrepareForTest;
39-
import org.powermock.modules.junit4.PowerMockRunner;
40-
import org.powermock.reflect.Whitebox;
39+
import org.mockito.MockedStatic;
40+
import org.mockito.Mockito;
41+
import org.mockito.junit.MockitoJUnitRunner;
4142

4243
import com.wasteofplastic.invswitcher.listeners.PlayerListener;
4344

@@ -52,8 +53,7 @@
5253
* @author tastybento
5354
*
5455
*/
55-
@RunWith(PowerMockRunner.class)
56-
@PrepareForTest({Bukkit.class, BentoBox.class})
56+
@RunWith(MockitoJUnitRunner.class)
5757
public class InvSwitcherTest {
5858

5959
private static File jFile;
@@ -95,11 +95,20 @@ public static void beforeClass() throws IOException {
9595
}
9696

9797
/**
98+
* @throws SecurityException
99+
* @throws NoSuchFieldException
100+
* @throws IllegalAccessException
101+
* @throws IllegalArgumentException
98102
*/
99103
@Before
100-
public void setUp() {
104+
public void setUp()
105+
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
101106
// Set up plugin
102-
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
107+
// Use reflection to set the private static field "instance" in BentoBox
108+
Field instanceField = BentoBox.class.getDeclaredField("instance");
109+
110+
instanceField.setAccessible(true);
111+
instanceField.set(null, plugin);
103112
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
104113

105114
// The database type has to be created one line before the thenReturn() to work!
@@ -117,10 +126,6 @@ public void setUp() {
117126
// Addons manager
118127
when(plugin.getAddonsManager()).thenReturn(am);
119128

120-
// Bukkit
121-
PowerMockito.mockStatic(Bukkit.class);
122-
when(Bukkit.getWorld(anyString())).thenReturn(world);
123-
124129
// World
125130
when(world.getName()).thenReturn("bskyblock-world");
126131
}
@@ -165,12 +170,17 @@ public void testOnEnable() {
165170
*/
166171
@Test
167172
public void testOnDisable() {
168-
addon.onLoad();
169-
addon.getSettings().setWorlds(Set.of("bskyblock-world"));
170-
addon.allLoaded();
171-
addon.onDisable();
172-
PowerMockito.verifyStatic(Bukkit.class);
173-
Bukkit.getOnlinePlayers();
173+
// Mock the static method
174+
try (MockedStatic<Bukkit> mockedBukkit = mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS)) {
175+
when(Bukkit.getWorld(anyString())).thenReturn(world);
176+
// Run code to test
177+
addon.onLoad();
178+
addon.getSettings().setWorlds(Set.of("bskyblock-world"));
179+
addon.allLoaded();
180+
addon.onDisable();
181+
// Verify that the static method was never called
182+
mockedBukkit.verify(() -> Bukkit.getOnlinePlayers());
183+
}
174184
}
175185

176186
/**
@@ -188,12 +198,17 @@ public void testOnLoad() {
188198
*/
189199
@Test
190200
public void testAllLoaded() {
191-
addon.onLoad();
192-
addon.getSettings().setWorlds(Set.of("bskyblock-world"));
193-
addon.allLoaded();
194-
verify(plugin).log("[InvSwitcher] Hooking into the following worlds:");
195-
verify(plugin, times(3)).log("[InvSwitcher] bskyblock-world");
196-
verify(am).registerListener(eq(addon), any(PlayerListener.class));
201+
// Mock the static method
202+
try (MockedStatic<Bukkit> mockedBukkit = mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS)) {
203+
when(Bukkit.getWorld(anyString())).thenReturn(world);
204+
// Run code to test
205+
addon.onLoad();
206+
addon.getSettings().setWorlds(Set.of("bskyblock-world"));
207+
addon.allLoaded();
208+
verify(plugin).log("[InvSwitcher] Hooking into the following worlds:");
209+
verify(plugin, times(3)).log("[InvSwitcher] bskyblock-world");
210+
verify(am).registerListener(eq(addon), any(PlayerListener.class));
211+
}
197212

198213
}
199214

0 commit comments

Comments
 (0)