Skip to content

Commit

Permalink
Implemented new data backend
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Aug 24, 2024
1 parent fd031e2 commit e87b7ce
Show file tree
Hide file tree
Showing 26 changed files with 387 additions and 879 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.willfp.eco.core.data.handlers;

import com.willfp.eco.core.data.keys.PersistentDataKey;
import com.willfp.eco.core.data.keys.PersistentDataKeyType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
* Handles persistent data.
*/
public abstract class PersistentDataHandler implements Registrable {
/**
* The id of the handler.
* The id.
*/
private final String id;

Expand All @@ -30,7 +33,7 @@ public abstract class PersistentDataHandler implements Registrable {
/**
* Create a new persistent data handler.
*
* @param id The id of the handler.
* @param id The id.
*/
protected PersistentDataHandler(@NotNull final String id) {
this.id = id;
Expand Down Expand Up @@ -134,54 +137,47 @@ public final Set<SerializedProfile> serializeData(@NotNull final Set<PersistentD
/**
* Load profile data.
*
* @param data The data.
* @param profile The profile.
*/
@SuppressWarnings("unchecked")
public final void loadProfileData(@NotNull Set<SerializedProfile> data) {
for (SerializedProfile profile : data) {
for (Map.Entry<PersistentDataKey<?>, Object> entry : profile.data().entrySet()) {
PersistentDataKey<?> key = entry.getKey();
Object value = entry.getValue();

// This cast is safe because the data is serialized
write(profile.uuid(), (PersistentDataKey<? super Object>) key, value);
}
public final void loadSerializedProfile(@NotNull final SerializedProfile profile) {
for (Map.Entry<PersistentDataKey<?>, Object> entry : profile.data().entrySet()) {
PersistentDataKey<?> key = entry.getKey();
Object value = entry.getValue();

// This cast is safe because the data is serialized
write(profile.uuid(), (PersistentDataKey<? super Object>) key, value);
}
}

/**
* Await outstanding writes.
*/
public final void awaitOutstandingWrites() throws InterruptedException {
boolean success = executor.awaitTermination(15, TimeUnit.SECONDS);
boolean success = executor.awaitTermination(2, TimeUnit.MINUTES);

if (!success) {
throw new InterruptedException("Failed to await outstanding writes");
}
}

@Override
public final @NotNull String getID() {
@NotNull
public final String getID() {
return id;
}

@Override
public final boolean equals(@Nullable final Object obj) {
if (this == obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
public boolean equals(@NotNull final Object obj) {
if (!(obj instanceof PersistentDataHandler other)) {
return false;
}

PersistentDataHandler that = (PersistentDataHandler) obj;

return id.equals(that.id);
return other.getClass().equals(this.getClass());
}

@Override
public final int hashCode() {
return id.hashCode();
public int hashCode() {
return this.getClass().hashCode();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.willfp.eco.core.Eco
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.PluginLike
import com.willfp.eco.core.PluginProps
import com.willfp.eco.core.Prerequisite
import com.willfp.eco.core.command.CommandBase
import com.willfp.eco.core.command.PluginCommandBase
import com.willfp.eco.core.config.ConfigType
Expand Down Expand Up @@ -44,8 +43,7 @@ import com.willfp.eco.internal.proxy.EcoProxyFactory
import com.willfp.eco.internal.scheduling.EcoScheduler
import com.willfp.eco.internal.spigot.data.DataYml
import com.willfp.eco.internal.spigot.data.KeyRegistry
import com.willfp.eco.internal.spigot.data.ProfileHandler
import com.willfp.eco.internal.spigot.data.storage.HandlerType
import com.willfp.eco.internal.spigot.data.profiles.ProfileHandler
import com.willfp.eco.internal.spigot.integrations.bstats.MetricHandler
import com.willfp.eco.internal.spigot.math.DelegatedExpressionHandler
import com.willfp.eco.internal.spigot.math.ImmediatePlaceholderTranslationExpressionHandler
Expand Down Expand Up @@ -74,18 +72,15 @@ import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.SkullMeta
import org.bukkit.persistence.PersistentDataContainer
import java.net.URLClassLoader
import java.util.*
import java.util.UUID

private val loadedEcoPlugins = mutableMapOf<String, EcoPlugin>()

@Suppress("UNUSED")
class EcoImpl : EcoSpigotPlugin(), Eco {
override val dataYml = DataYml(this)

override val profileHandler = ProfileHandler(
HandlerType.valueOf(this.configYml.getString("data-handler").uppercase()),
this
)
override val profileHandler = ProfileHandler(this)

init {
getProxy(CommonsInitializerProxy::class.java).init(this)
Expand Down Expand Up @@ -290,10 +285,10 @@ class EcoImpl : EcoSpigotPlugin(), Eco {
bukkitAudiences

override fun getServerProfile() =
profileHandler.loadServerProfile()
profileHandler.getServerProfile()

override fun loadPlayerProfile(uuid: UUID) =
profileHandler.load(uuid)
profileHandler.getPlayerProfile(uuid)

override fun createDummyEntity(location: Location): Entity =
getProxy(DummyEntityFactoryProxy::class.java).createDummyEntity(location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.willfp.eco.core.integrations.mcmmo.McmmoManager
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager
import com.willfp.eco.core.integrations.shop.ShopManager
import com.willfp.eco.core.items.Items
import com.willfp.eco.core.items.tag.VanillaItemTag
import com.willfp.eco.core.packet.PacketListener
import com.willfp.eco.core.particle.Particles
import com.willfp.eco.core.price.Prices
Expand Down Expand Up @@ -62,11 +61,10 @@ import com.willfp.eco.internal.price.PriceFactoryXP
import com.willfp.eco.internal.price.PriceFactoryXPLevels
import com.willfp.eco.internal.recipes.AutocrafterPatch
import com.willfp.eco.internal.spigot.arrows.ArrowDataListener
import com.willfp.eco.internal.spigot.data.DataListener
import com.willfp.eco.internal.spigot.data.DataYml
import com.willfp.eco.internal.spigot.data.PlayerBlockListener
import com.willfp.eco.internal.spigot.data.ProfileHandler
import com.willfp.eco.internal.spigot.data.storage.ProfileSaver
import com.willfp.eco.internal.spigot.data.profiles.ProfileHandler
import com.willfp.eco.internal.spigot.data.profiles.ProfileLoadListener
import com.willfp.eco.internal.spigot.drops.CollatedRunnable
import com.willfp.eco.internal.spigot.eventlisteners.EntityDeathByEntityListeners
import com.willfp.eco.internal.spigot.eventlisteners.NaturalExpGainListenersPaper
Expand Down Expand Up @@ -259,9 +257,6 @@ abstract class EcoSpigotPlugin : EcoPlugin() {
// Init FIS
this.getProxy(FastItemStackFactoryProxy::class.java).create(ItemStack(Material.AIR)).unwrap()

// Preload categorized persistent data keys
profileHandler.initialize()

// Init adventure
if (!Prerequisite.HAS_PAPER.isMet) {
bukkitAudiences = BukkitAudiences.create(this)
Expand All @@ -282,14 +277,11 @@ abstract class EcoSpigotPlugin : EcoPlugin() {
override fun createTasks() {
CollatedRunnable(this)

this.scheduler.runLater(3) {
profileHandler.migrateIfNeeded()
if (!profileHandler.migrateIfNecessary()) {
profileHandler.profileWriter.startTickingAutosave()
profileHandler.profileWriter.startTickingSaves()
}

profileHandler.startAutosaving()

ProfileSaver(this, profileHandler).startTicking()

this.scheduler.runTimer(
this.configYml.getInt("display-frame-ttl").toLong(),
this.configYml.getInt("display-frame-ttl").toLong(),
Expand Down Expand Up @@ -428,7 +420,7 @@ abstract class EcoSpigotPlugin : EcoPlugin() {
GUIListener(this),
ArrowDataListener(this),
ArmorChangeEventListeners(this),
DataListener(this, profileHandler),
ProfileLoadListener(this, profileHandler),
PlayerBlockListener(this),
ServerLocking
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ object KeyRegistry {
this.registry[key.key] = key
}

fun getRegisteredKeys(): MutableSet<PersistentDataKey<*>> {
return registry.values.toMutableSet()
fun getRegisteredKeys(): Set<PersistentDataKey<*>> {
return registry.values.toSet()
}

private fun <T> validateKey(key: PersistentDataKey<T>) {
Expand Down
Loading

0 comments on commit e87b7ce

Please sign in to comment.