Add player analytics (Plan) hook

This commit is contained in:
William committed 2022-08-26 19:06:35 +01:00
1 parent 1866c457f1
commit 1cff7aaafb
7 files changed
+215 -6

No files matched your search

@@ -13,10 +13,7 @@ import net.william278.huskhomes.config.Spawn;
import net.william278.huskhomes.database.Database;
import net.william278.huskhomes.database.MySqlDatabase;
import net.william278.huskhomes.database.SqLiteDatabase;
import net.william278.huskhomes.hook.BlueMapHook;
import net.william278.huskhomes.hook.DynMapHook;
import net.william278.huskhomes.hook.PluginHook;
import net.william278.huskhomes.hook.VaultEconomyHook;
import net.william278.huskhomes.hook.*;
import net.william278.huskhomes.listener.BukkitEventListener;
import net.william278.huskhomes.listener.EventListener;
import net.william278.huskhomes.messenger.NetworkMessenger;
@@ -156,11 +153,9 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
// Register plugin hooks (Economy, Maps, Plan)
this.pluginHooks = new HashSet<>();
if (settings.economy && Bukkit.getPluginManager().getPlugin("Vault") != null) {
pluginHooks.add(new VaultEconomyHook(this));
}
if (settings.doMapHook) {
switch (settings.mappingPlugin) {
case DYNMAP -> {
@@ -177,6 +172,9 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
}
getMapHook().ifPresent(mapHook -> savedPositionManager.setMapHook(mapHook));
}
if (Bukkit.getPluginManager().getPlugin("Plan") != null) {
pluginHooks.add(new PlanHook(this));
}
if (pluginHooks.size() > 0) {
pluginHooks.forEach(PluginHook::initialize);
+1
View File
@@ -9,6 +9,7 @@ softdepend:
- Vault
- dynmap
- BlueMap
- Plan
libraries:
- 'redis.clients:jedis:${jedis_version}'
- 'mysql:mysql-connector-java:${mysql_driver_version}'
+2
View File
@@ -16,7 +16,9 @@ dependencies {
compileOnly 'mysql:mysql-connector-java:' + mysql_driver_version
compileOnly 'org.apache.commons:commons-text:' + commons_text_version
compileOnly 'com.github.BlueMap-Minecraft:BlueMapAPI:v2.1.0'
compileOnly 'com.github.plan-player-analytics:Plan:5.4.1690'
testImplementation 'com.github.plan-player-analytics:Plan:5.4.1690'
testImplementation 'com.github.yuriy-g:simple-java-plot:5c502c2e98'
}
@@ -0,0 +1,131 @@
package net.william278.huskhomes.hook;
import com.djrapitops.plan.extension.CallEvents;
import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.annotation.*;
import com.djrapitops.plan.extension.icon.Color;
import com.djrapitops.plan.extension.icon.Family;
import net.william278.huskhomes.database.Database;
import net.william278.huskhomes.player.UserData;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
@PluginInfo(
name = "HuskHomes",
iconName = "home",
iconFamily = Family.SOLID,
color = Color.LIGHT_BLUE
)
@SuppressWarnings("unused")
public class PlanDataExtension implements DataExtension {
private Database database;
private boolean crossServer;
private static final String UNKNOWN_STRING = "N/A";
protected PlanDataExtension(@NotNull Database database, boolean crossServer) {
this.database = database;
}
protected PlanDataExtension() {
}
@Override
public CallEvents[] callExtensionMethodsOn() {
return new CallEvents[]{
CallEvents.PLAYER_JOIN,
CallEvents.PLAYER_LEAVE
};
}
@BooleanProvider(
text = "Has Data",
description = "Whether this user has HuskHomes data.",
iconName = "bell",
iconFamily = Family.SOLID,
conditionName = "hasData",
hidden = true
)
public boolean getHasUserData(@NotNull UUID uuid) {
return database.getUserData(uuid).join().isPresent();
}
@NumberProvider(
text = "Home Count",
description = "The number of homes this user has set.",
iconName = "home",
iconFamily = Family.SOLID,
priority = 5
)
@Conditional("hasData")
public long getHomeCount(@NotNull UUID uuid) {
return database.getUserData(uuid).join()
.map(userData -> (long) database.getHomes(userData.user()).join().size())
.orElse(0L);
}
@NumberProvider(
text = "Public Homes",
description = "The number of homes this user has made public.",
iconName = "sun",
iconFamily = Family.SOLID,
priority = 4
)
@Conditional("hasData")
public long getPublicHomeCount(@NotNull UUID uuid) {
return database.getUserData(uuid).join()
.map(userData -> database.getHomes(userData.user()).join()
.stream().filter(home -> home.isPublic).count())
.orElse(0L);
}
@NumberProvider(
text = "Home Slots",
description = "The number of extra home slots this user has purchased.",
iconName = "money-check-alt",
iconFamily = Family.SOLID,
priority = 3
)
@Conditional("hasData")
public long getPurchasedHomeSlots(@NotNull UUID uuid) {
return database.getUserData(uuid).join()
.map(userData -> (long) userData.homeSlots())
.orElse(0L);
}
@BooleanProvider(
text = "Ignoring /tpa Requests",
description = "Whether this player is ignoring /tpa requests.",
iconName = "phone-slash",
iconFamily = Family.SOLID,
priority = 2
)
@Conditional("hasData")
public boolean isIgnoringTeleportRequests(@NotNull UUID uuid) {
return database.getUserData(uuid).join()
.map(UserData::ignoringTeleports)
.orElse(false);
}
@StringProvider(
text = "Offline Position",
description = "The location where this user logged out.",
iconName = "door-open",
iconFamily = Family.SOLID,
priority = 1
)
@Conditional("hasData")
public String getOfflinePosition(@NotNull UUID uuid) {
return database.getUserData(uuid).join()
.map(userData -> database.getOfflinePosition(userData.user()).join()
.map(position -> "x: " + (int) position.x + ", y: " + (int) position.y + ", z: " + (int) position.z
+ " (" + position.world.name +
((crossServer) ? "/" + position.server.name + ")" : ")"))
.orElse(UNKNOWN_STRING))
.orElse(UNKNOWN_STRING);
}
}
@@ -0,0 +1,63 @@
package net.william278.huskhomes.hook;
import com.djrapitops.plan.capability.CapabilityService;
import com.djrapitops.plan.extension.ExtensionService;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.HuskHomesInitializationException;
import net.william278.huskhomes.database.Database;
import net.william278.huskhomes.util.Logger;
import org.jetbrains.annotations.NotNull;
import java.util.logging.Level;
/**
* Hooks into Plan to provide the {@link PlanDataExtension} with stats on the web panel
*/
public class PlanHook extends PluginHook {
private final Database database;
private final Logger logger;
public PlanHook(@NotNull HuskHomes implementor) {
super(implementor, "Plan");
this.database = implementor.getDatabase();
this.logger = implementor.getLoggingAdapter();
}
@Override
public boolean initialize() throws HuskHomesInitializationException {
if (!areAllCapabilitiesAvailable()) {
return false;
}
registerDataExtension();
handlePlanReload();
return true;
}
private boolean areAllCapabilitiesAvailable() {
CapabilityService capabilities = CapabilityService.getInstance();
return capabilities.hasCapability("DATA_EXTENSION_VALUES");
}
private void registerDataExtension() {
try {
ExtensionService.getInstance().register(new PlanDataExtension(database, plugin.getSettings().crossServer));
} catch (IllegalStateException planIsNotEnabled) {
logger.log(Level.SEVERE, "Plan extension hook failed to register. Plan is not enabled.", planIsNotEnabled);
// Plan is not enabled, handle exception
} catch (IllegalArgumentException dataExtensionImplementationIsInvalid) {
logger.log(Level.SEVERE, "Plan extension hook failed to register. Data hook implementation is invalid.", dataExtensionImplementationIsInvalid);
// The DataExtension implementation has an implementation error, handle exception
}
}
// Re-register the extension when plan enables
private void handlePlanReload() {
CapabilityService.getInstance().registerEnableListener(isPlanEnabled -> {
if (isPlanEnabled) {
registerDataExtension();
}
});
}
}
@@ -0,0 +1,14 @@
package net.william278.huskhomes.hook;
import com.djrapitops.plan.extension.extractor.ExtensionExtractor;
import org.junit.jupiter.api.Test;
public class PlanDataExtensionTests {
// Throws IllegalArgumentException if there is an implementation error or warning.
@Test
public void testPlanDataExtension() {
new ExtensionExtractor(new PlanDataExtension()).validateAnnotations();
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB