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 org.bstats.bukkit.Metrics;
import java.util.HashMap;
import java.util.Map;
/** /**
* @author Poslovitch * @author Poslovitch
*/ */
@ -36,6 +39,9 @@ public class BStats {
// Single Line charts // Single Line charts
registerIslandsCountChart(); registerIslandsCountChart();
registerIslandsCreatedChart(); registerIslandsCreatedChart();
// Simple Bar Charts
registerAddonsChart();
} }
private void registerDefaultLanguageChart() { private void registerDefaultLanguageChart() {
@ -68,4 +74,18 @@ public class BStats {
public void increaseIslandsCreatedCount() { public void increaseIslandsCreatedCount() {
islandsCreatedCount++; 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) { private AddonDescription asDescription(YamlConfiguration data) {
AddonDescription.Builder builder = new AddonDescription.Builder(data.getString("main"), data.getString("name")) AddonDescription.Builder builder = new AddonDescription.Builder(data.getString("main"), data.getString("name"), data.getString("version"))
.version(data.getString("version")) .authors(data.getString("authors"))
.authors(data.getString("authors")); .metrics(data.getBoolean("metrics"));
if (data.getString("depend") != null) { if (data.getString("depend") != null) {
builder.dependencies(Arrays.asList(data.getString("depend").split("\\s*,\\s*"))); 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) * @see java.net.URLClassLoader#findClass(java.lang.String)
*/ */
@Override @Override
@Nullable
protected Class<?> findClass(String name) { protected Class<?> findClass(String name) {
return findClass(name, true); return findClass(name, true);
} }
@ -127,7 +128,7 @@ public class AddonClassLoader extends URLClassLoader {
try { try {
computed = super.findClass(key); computed = super.findClass(key);
} catch (ClassNotFoundException | NoClassDefFoundError e) { } catch (ClassNotFoundException | NoClassDefFoundError e) {
computed = null; // Do nothing.
} }
if (computed != null) { if (computed != null) {
loader.setClass(key, computed); loader.setClass(key, computed);

View File

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

View File

@ -110,7 +110,7 @@ public class AddonTest {
@Test @Test
public void testGetDescription() { public void testGetDescription() {
TestClass test = new TestClass(); 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()); assertNull(test.getDescription());
test.setDescription(d); test.setDescription(d);
assertEquals(d, test.getDescription()); assertEquals(d, test.getDescription());
@ -225,7 +225,7 @@ public class AddonTest {
@Test @Test
public void testSetDescription() { public void testSetDescription() {
TestClass test = new TestClass(); 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); test.setDescription(desc);
assertEquals(desc, test.getDescription()); assertEquals(desc, test.getDescription());
} }

View File

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

View File

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