Skip to content

Commit

Permalink
Merge pull request #29 from BentoBoxWorld/develop
Browse files Browse the repository at this point in the history
Release 1.13.0
  • Loading branch information
tastybento authored Jul 5, 2024
2 parents c15c2e5 + 2101886 commit 78c3223
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 95 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
distribution: 'adopt'
java-version: 17
distribution: 'temurin'
java-version: 21
- name: Cache SonarCloud packages
uses: actions/cache@v3
with:
Expand All @@ -35,4 +35,5 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
MAVEN_OPTS: "-XX:+EnableDynamicAgentLoading"
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
30 changes: 15 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<java.version>21</java.version>
<!-- Non-minecraft related dependencies -->
<powermock.version>2.0.9</powermock.version>
<mockito.version>5.12.0</mockito.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.20.3-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.0.0-SNAPSHOT</bentobox.version>
<spigot.version>1.20.4-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.4.1-SNAPSHOT</bentobox.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>1.12.0</build.version>
<build.version>1.13.0</build.version>
<!-- Sonar Cloud -->
<sonar.projectKey>BentoBoxWorld_addon-invSwitcher</sonar.projectKey>
<sonar.organization>bentobox-world</sonar.organization>
Expand Down Expand Up @@ -142,19 +142,19 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.11.2</version>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -202,15 +202,15 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<version>3.13.0</version>
<configuration>
<release>${java.version}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<version>3.3.0</version>
<configuration>
<argLine>
${argLine}
Expand Down Expand Up @@ -283,7 +283,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<version>0.8.12</version>
<configuration>
<append>true</append>
<excludes>
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/com/wasteofplastic/invswitcher/Store.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 - 2021 tastybento
* Copyright (c) 2017 - 2024 tastybento
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -58,11 +58,6 @@
public class Store {
private static final CharSequence THE_END = "_the_end";
private static final CharSequence NETHER = "_nether";
@SuppressWarnings("deprecation")
private static final List<Material> BLOCKS = Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy()).toList();
@SuppressWarnings("deprecation")
private static final List<Material> ITEMS = Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy()).toList();
private static final List<EntityType> LIVING_ENTITIES = Arrays.stream(EntityType.values()).filter(EntityType::isAlive).toList();
private final Database<InventoryStorage> database;
private final Map<UUID, InventoryStorage> cache;
private final InvSwitcher addon;
Expand Down Expand Up @@ -265,21 +260,24 @@ private void saveStats(InventoryStorage store, Player player, String worldName)
Map<EntityType, Integer> entMap;
switch (s.getType()) {
case BLOCK -> {
map = BLOCKS.stream().filter(m -> player.getStatistic(s, m) > 0)
map = Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy())
.filter(m -> player.getStatistic(s, m) > 0)
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
if (!map.isEmpty()) {
store.getBlockStats(worldName).put(s, map);
}
}
case ITEM -> {
map = ITEMS.stream().filter(m -> player.getStatistic(s, m) > 0)
map = Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy())
.filter(m -> player.getStatistic(s, m) > 0)
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
if (!map.isEmpty()) {
store.getItemStats(worldName).put(s, map);
}
}
case ENTITY -> {
entMap = LIVING_ENTITIES.stream().filter(m -> player.getStatistic(s, m) > 0)
entMap = Arrays.stream(EntityType.values()).filter(EntityType::isAlive)
.filter(m -> player.getStatistic(s, m) > 0)
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
if (!entMap.isEmpty()) {
store.getEntityStats(worldName).put(s, entMap);
Expand Down Expand Up @@ -364,11 +362,15 @@ private void resetAdv(Player player) {
}
}

@SuppressWarnings("deprecation")
private void resetStats(Player player, Statistic s) {
switch (s.getType()) {
case BLOCK -> BLOCKS.forEach(m -> player.setStatistic(s, m, 0));
case ITEM -> ITEMS.forEach(m -> player.setStatistic(s, m, 0));
case ENTITY -> LIVING_ENTITIES.forEach(en -> player.setStatistic(s, en, 0));
case BLOCK -> Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy())
.forEach(m -> player.setStatistic(s, m, 0));
case ITEM -> Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy())
.forEach(m -> player.setStatistic(s, m, 0));
case ENTITY ->
Arrays.stream(EntityType.values()).filter(EntityType::isAlive).forEach(en -> player.setStatistic(s, en, 0));
case UNTYPED -> player.setStatistic(s, 0);
}
}
Expand Down
63 changes: 39 additions & 24 deletions src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -15,6 +16,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -34,10 +36,9 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import com.wasteofplastic.invswitcher.listeners.PlayerListener;

Expand All @@ -52,8 +53,7 @@
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class})
@RunWith(MockitoJUnitRunner.class)
public class InvSwitcherTest {

private static File jFile;
Expand Down Expand Up @@ -95,11 +95,20 @@ public static void beforeClass() throws IOException {
}

/**
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
@Before
public void setUp() {
public void setUp()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Use reflection to set the private static field "instance" in BentoBox
Field instanceField = BentoBox.class.getDeclaredField("instance");

instanceField.setAccessible(true);
instanceField.set(null, plugin);
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());

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

// Bukkit
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getWorld(anyString())).thenReturn(world);

// World
when(world.getName()).thenReturn("bskyblock-world");
}
Expand Down Expand Up @@ -165,12 +170,17 @@ public void testOnEnable() {
*/
@Test
public void testOnDisable() {
addon.onLoad();
addon.getSettings().setWorlds(Set.of("bskyblock-world"));
addon.allLoaded();
addon.onDisable();
PowerMockito.verifyStatic(Bukkit.class);
Bukkit.getOnlinePlayers();
// Mock the static method
try (MockedStatic<Bukkit> mockedBukkit = mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS)) {
when(Bukkit.getWorld(anyString())).thenReturn(world);
// Run code to test
addon.onLoad();
addon.getSettings().setWorlds(Set.of("bskyblock-world"));
addon.allLoaded();
addon.onDisable();
// Verify that the static method was never called
mockedBukkit.verify(() -> Bukkit.getOnlinePlayers());
}
}

/**
Expand All @@ -188,12 +198,17 @@ public void testOnLoad() {
*/
@Test
public void testAllLoaded() {
addon.onLoad();
addon.getSettings().setWorlds(Set.of("bskyblock-world"));
addon.allLoaded();
verify(plugin).log("[InvSwitcher] Hooking into the following worlds:");
verify(plugin, times(3)).log("[InvSwitcher] bskyblock-world");
verify(am).registerListener(eq(addon), any(PlayerListener.class));
// Mock the static method
try (MockedStatic<Bukkit> mockedBukkit = mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS)) {
when(Bukkit.getWorld(anyString())).thenReturn(world);
// Run code to test
addon.onLoad();
addon.getSettings().setWorlds(Set.of("bskyblock-world"));
addon.allLoaded();
verify(plugin).log("[InvSwitcher] Hooking into the following worlds:");
verify(plugin, times(3)).log("[InvSwitcher] bskyblock-world");
verify(am).registerListener(eq(addon), any(PlayerListener.class));
}

}

Expand Down
Loading

0 comments on commit 78c3223

Please sign in to comment.