mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
Add EssentialsX importer, update docs (#390)
This commit is contained in:
1 parent
dba87cf428
commit
58f1dc6111
25 files changed
+581
-105
No files matched your search
@@ -37,6 +37,7 @@ import net.william278.huskhomes.hook.Hook;
|
||||
import net.william278.huskhomes.hook.PlaceholderAPIHook;
|
||||
import net.william278.huskhomes.hook.RedisEconomyHook;
|
||||
import net.william278.huskhomes.hook.VaultEconomyHook;
|
||||
import net.william278.huskhomes.importer.EssentialsXImporter;
|
||||
import net.william278.huskhomes.listener.BukkitEventListener;
|
||||
import net.william278.huskhomes.listener.EventListener;
|
||||
import net.william278.huskhomes.manager.Manager;
|
||||
@@ -212,6 +213,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
public void registerHooks() {
|
||||
HuskHomes.super.registerHooks();
|
||||
|
||||
// Hooks
|
||||
if (getSettings().doEconomy()) {
|
||||
if (isDependencyLoaded("RedisEconomy")) {
|
||||
getHooks().add(new RedisEconomyHook(this));
|
||||
@@ -222,6 +224,11 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
if (isDependencyLoaded("PlaceholderAPI")) {
|
||||
getHooks().add(new PlaceholderAPIHook(this));
|
||||
}
|
||||
|
||||
// Importers
|
||||
if (isDependencyLoaded("Essentials")) {
|
||||
getHooks().add(new EssentialsXImporter(this));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
package net.william278.huskhomes.importer;
|
||||
|
||||
import com.earth2me.essentials.Essentials;
|
||||
import com.earth2me.essentials.Warps;
|
||||
import net.william278.huskhomes.BukkitHuskHomes;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import net.william278.huskhomes.util.BukkitAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class EssentialsXImporter extends Importer {
|
||||
|
||||
private final Essentials essentials;
|
||||
|
||||
public EssentialsXImporter(@NotNull HuskHomes plugin) {
|
||||
super("EssentialsX", List.of(ImportData.USERS, ImportData.HOMES, ImportData.WARPS), plugin);
|
||||
this.essentials = (Essentials) ((BukkitHuskHomes) plugin).getServer().getPluginManager().getPlugin("Essentials");
|
||||
}
|
||||
|
||||
private int importUsers() {
|
||||
final AtomicInteger usersImported = new AtomicInteger();
|
||||
for (UUID uuid : essentials.getUserMap().getAllUniqueUsers()) {
|
||||
final User user = User.of(
|
||||
uuid,
|
||||
essentials.getUser(uuid).getLastAccountName()
|
||||
);
|
||||
plugin.getDatabase().ensureUser(user);
|
||||
plugin.editUserData(user, (editor -> editor.setHomeSlots(Math.max(
|
||||
plugin.getSettings().getFreeHomeSlots(),
|
||||
essentials.getUser(uuid).getHomes().size()
|
||||
))));
|
||||
usersImported.getAndIncrement();
|
||||
}
|
||||
return usersImported.get();
|
||||
}
|
||||
|
||||
private int importHomes() {
|
||||
final AtomicInteger homesImported = new AtomicInteger();
|
||||
for (UUID uuid : essentials.getUserMap().getAllUniqueUsers()) {
|
||||
final com.earth2me.essentials.User essentialsUser = essentials.getUser(uuid);
|
||||
for (String homeName : essentialsUser.getHomes()) {
|
||||
BukkitAdapter.adaptLocation(essentialsUser.getHome(homeName))
|
||||
.map(location -> Position.at(location, plugin.getServerName()))
|
||||
.ifPresent(position -> {
|
||||
plugin.getManager().homes().createHome(
|
||||
User.of(uuid, essentialsUser.getLastAccountName()),
|
||||
this.normalizeName(homeName),
|
||||
position,
|
||||
true, true
|
||||
);
|
||||
homesImported.getAndIncrement();
|
||||
});
|
||||
}
|
||||
}
|
||||
return homesImported.get();
|
||||
}
|
||||
|
||||
private int importWarps() throws Throwable {
|
||||
final AtomicInteger warpsImported = new AtomicInteger();
|
||||
final Warps warps = essentials.getWarps();
|
||||
for (String warpName : warps.getList()) {
|
||||
BukkitAdapter.adaptLocation(warps.getWarp(warpName))
|
||||
.map(location -> Position.at(location, plugin.getServerName()))
|
||||
.ifPresent(position -> {
|
||||
plugin.getManager().warps().createWarp(
|
||||
this.normalizeName(warpName),
|
||||
position,
|
||||
true
|
||||
);
|
||||
warpsImported.getAndIncrement();
|
||||
});
|
||||
}
|
||||
return warpsImported.get();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String normalizeName(@NotNull String name) {
|
||||
if (plugin.getValidator().isValidName(name)) {
|
||||
return name;
|
||||
}
|
||||
|
||||
// Remove spaces
|
||||
name = name.replaceAll(" ", "_");
|
||||
|
||||
// Remove unicode characters
|
||||
if (!plugin.getSettings().doAllowUnicodeNames()) {
|
||||
name = name.replaceAll("[^A-Za-z0-9_-]", "");
|
||||
}
|
||||
|
||||
// Ensure the name is not blank
|
||||
if (name.isBlank()) {
|
||||
name = "imported-" + UUID.randomUUID().toString().substring(0, 5);
|
||||
}
|
||||
|
||||
// Ensure name is not too long
|
||||
if (name.length() > 16) {
|
||||
name = name.substring(0, 16);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int importData(@NotNull Importer.ImportData importData) throws Throwable {
|
||||
return switch (importData) {
|
||||
case USERS -> importUsers();
|
||||
case HOMES -> importHomes();
|
||||
case WARPS -> importWarps();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user