Added addons metrics chart + added AddonDescription#isMetrics so that addons need to explicitely opt in

Deprecated AddonDescription.Builder#Builder(String, String) and AddonDescription.Builder#version(String).
Added AddonDescription.Builder#Builder(String, String, String).
Updated tests
This commit is contained in:
Florian CUNY 2019-01-18 22:06:06 +01:00
parent 0f5b5b5693
commit 5ce8362fd5
6 changed files with 108 additions and 37 deletions

View File

@ -2,6 +2,9 @@ package world.bentobox.bentobox;
import org.bstats.bukkit.Metrics;
import java.util.HashMap;
import java.util.Map;
/**
* @author Poslovitch
*/
@ -36,6 +39,9 @@ public class BStats {
// Single Line charts
registerIslandsCountChart();
registerIslandsCreatedChart();
// Simple Bar Charts
registerAddonsChart();
}
private void registerDefaultLanguageChart() {
@ -68,4 +74,18 @@ public class BStats {
public void increaseIslandsCreatedCount() {
islandsCreatedCount++;
}
/**
* Sends the enabled addons of this server.
* @since 1.1
*/
private void registerAddonsChart() {
metrics.addCustomChart(new Metrics.SimpleBarChart("addons", () -> {
Map<String, Integer> map = new HashMap<>();
plugin.getAddonsManager().getEnabledAddons().stream()
.filter(addon -> addon.getDescription().isMetrics())
.forEach(addon -> map.put(addon.getDescription().getName(), 1));
return map;
}));
}
}

View File

@ -84,9 +84,9 @@ public class AddonClassLoader extends URLClassLoader {
}
private AddonDescription asDescription(YamlConfiguration data) {
AddonDescription.Builder builder = new AddonDescription.Builder(data.getString("main"), data.getString("name"))
.version(data.getString("version"))
.authors(data.getString("authors"));
AddonDescription.Builder builder = new AddonDescription.Builder(data.getString("main"), data.getString("name"), data.getString("version"))
.authors(data.getString("authors"))
.metrics(data.getBoolean("metrics"));
if (data.getString("depend") != null) {
builder.dependencies(Arrays.asList(data.getString("depend").split("\\s*,\\s*")));
@ -102,6 +102,7 @@ public class AddonClassLoader extends URLClassLoader {
* @see java.net.URLClassLoader#findClass(java.lang.String)
*/
@Override
@Nullable
protected Class<?> findClass(String name) {
return findClass(name, true);
}
@ -127,7 +128,7 @@ public class AddonClassLoader extends URLClassLoader {
try {
computed = super.findClass(key);
} catch (ClassNotFoundException | NoClassDefFoundError e) {
computed = null;
// Do nothing.
}
if (computed != null) {
loader.setClass(key, computed);

View File

@ -1,5 +1,7 @@
package world.bentobox.bentobox.api.addons;
import org.eclipse.jdt.annotation.NonNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -9,13 +11,18 @@ import java.util.List;
*/
public final class AddonDescription {
private String main;
private String name;
private String version;
private String description;
private List<String> authors;
private List<String> dependencies;
private List<String> softDependencies;
private final @NonNull String main;
private final @NonNull String name;
private final @NonNull String version;
private final @NonNull String description;
private final @NonNull List<String> authors;
private final @NonNull List<String> dependencies;
private final @NonNull List<String> softDependencies;
/**
* Whether the addon should be included in Metrics or not.
* @since 1.1
*/
private final boolean metrics;
private AddonDescription(Builder builder) {
this.main = builder.main;
@ -25,24 +32,30 @@ public final class AddonDescription {
this.authors = builder.authors;
this.dependencies = builder.dependencies;
this.softDependencies = builder.softDependencies;
this.metrics = builder.metrics;
}
@NonNull
public String getName() {
return name;
}
@NonNull
public String getMain() {
return main;
}
@NonNull
public String getVersion() {
return version;
}
@NonNull
public String getDescription() {
return description;
}
@NonNull
public List<String> getAuthors() {
return authors;
}
@ -50,6 +63,7 @@ public final class AddonDescription {
/**
* @return the dependencies
*/
@NonNull
public List<String> getDependencies() {
return dependencies;
}
@ -57,49 +71,93 @@ public final class AddonDescription {
/**
* @return the softDependencies
*/
@NonNull
public List<String> getSoftDependencies() {
return softDependencies;
}
public static class Builder {
private String main;
private String name;
private String version;
private String description;
private List<String> authors = new ArrayList<>();
private List<String> dependencies = new ArrayList<>();
private List<String> softDependencies = new ArrayList<>();
/**
* Returns whether the addon should be included in Metrics or not.
* @return {@code true} if the addon should be included in Metrics reports, {@code false} otherwise.
* @since 1.1
*/
public boolean isMetrics() {
return metrics;
}
public Builder(String main, String name) {
public static class Builder {
private @NonNull String main;
private @NonNull String name;
private @NonNull String version;
private @NonNull String description = "";
private @NonNull List<String> authors = new ArrayList<>();
private @NonNull List<String> dependencies = new ArrayList<>();
private @NonNull List<String> softDependencies = new ArrayList<>();
private boolean metrics = false;
/**
* @deprecated As of 1.1, use {@link Builder#Builder(String, String, String)} instead.
*/
@Deprecated
public Builder(@NonNull String main, @NonNull String name) {
this.main = main;
this.name = name;
this.version = "";
}
public Builder version(String version) {
/**
* @since 1.1
*/
public Builder(@NonNull String main, @NonNull String name, @NonNull String version) {
this.main = main;
this.name = name;
this.version = version;
}
/**
* @deprecated As of 1.1, for removal.
*/
@Deprecated
@NonNull
public Builder version(@NonNull String version) {
this.version = version;
return this;
}
public Builder description(String description) {
@NonNull
public Builder description(@NonNull String description) {
this.description = description;
return this;
}
public Builder authors(String... authors) {
@NonNull
public Builder authors(@NonNull String... authors) {
this.authors = Arrays.asList(authors);
return this;
}
public Builder dependencies(List<String> dependencies) {
@NonNull
public Builder dependencies(@NonNull List<String> dependencies) {
this.dependencies = dependencies;
return this;
}
public Builder softDependencies(List<String> softDependencies) {
@NonNull
public Builder softDependencies(@NonNull List<String> softDependencies) {
this.softDependencies = softDependencies;
return this;
}
/**
* @since 1.1
*/
@NonNull
public Builder metrics(boolean metrics) {
this.metrics = metrics;
return this;
}
@NonNull
public AddonDescription build() {
return new AddonDescription(this);
}

View File

@ -110,7 +110,7 @@ public class AddonTest {
@Test
public void testGetDescription() {
TestClass test = new TestClass();
AddonDescription d = new AddonDescription.Builder("main", "name").build();
AddonDescription d = new AddonDescription.Builder("main", "name", "1.0").build();
assertNull(test.getDescription());
test.setDescription(d);
assertEquals(d, test.getDescription());
@ -225,7 +225,7 @@ public class AddonTest {
@Test
public void testSetDescription() {
TestClass test = new TestClass();
AddonDescription desc = new AddonDescription.Builder("main", "name").build();
AddonDescription desc = new AddonDescription.Builder("main", "name", "1.0").build();
test.setDescription(desc);
assertEquals(desc, test.getDescription());
}

View File

@ -259,7 +259,7 @@ public class UserTest {
@Test
public void testSendMessageOverrideWithAddon() {
GameModeAddon addon = mock(GameModeAddon.class);
AddonDescription desc = new AddonDescription.Builder("mock", "name").build();
AddonDescription desc = new AddonDescription.Builder("mock", "name", "1.0").build();
when(addon.getDescription()).thenReturn(desc);
Optional<GameModeAddon> optionalAddon = Optional.of(addon);
when(iwm .getAddon(any())).thenReturn(optionalAddon);
@ -275,7 +275,6 @@ public class UserTest {
when(lm.get(any(), any())).thenReturn("");
user.sendMessage("a.reference");
Mockito.verify(player, Mockito.never()).sendMessage(Mockito.anyString());
}
@Test
@ -288,7 +287,6 @@ public class UserTest {
when(lm.get(any(), any())).thenReturn(allColors.toString());
user.sendMessage("a.reference");
Mockito.verify(player, Mockito.never()).sendMessage(Mockito.anyString());
}
@Test
@ -304,7 +302,6 @@ public class UserTest {
user = User.getInstance((CommandSender)null);
user.sendRawMessage(raw);
Mockito.verify(player, Mockito.never()).sendMessage(Mockito.anyString());
}
@Test
@ -320,7 +317,6 @@ public class UserTest {
user.notify("a.reference");
Mockito.verify(notifier).notify(user, translation);
}
@ -330,7 +326,6 @@ public class UserTest {
user.setGameMode(gm);
}
Mockito.verify(player, Mockito.times(GameMode.values().length)).setGameMode(Mockito.any());
}
@Test
@ -339,7 +334,6 @@ public class UserTest {
Location loc = mock(Location.class);
user.teleport(loc);
Mockito.verify(player).teleport(loc);
}
@Test
@ -348,7 +342,6 @@ public class UserTest {
when(player.getWorld()).thenReturn(world);
User user = User.getInstance(player);
assertEquals(world, user.getWorld());
}
@Test
@ -409,7 +402,6 @@ public class UserTest {
user1 = User.getInstance((UUID)null);
assertFalse(user2.equals(user1));
}
@Test

View File

@ -296,7 +296,7 @@ public class LocalesManagerTest {
AddonsManager am = mock(AddonsManager.class);
List<Addon> none = new ArrayList<>();
Addon addon = mock(Addon.class);
AddonDescription desc = new AddonDescription.Builder("", "AcidIsland").build();
AddonDescription desc = new AddonDescription.Builder("", "AcidIsland", "1.0").build();
when(addon.getDescription()).thenReturn(desc);
// Create a tmp folder to jar up
File localeDir = new File(LOCALE_FOLDER);