Added ConfigSerializer, ConfigDeserializer, made Testable extend Predicate

This commit is contained in:
Auxilor
2022-03-02 13:48:05 +00:00
parent 866ba8440d
commit 88a7fb53a3
7 changed files with 178 additions and 1 deletions

View File

@@ -0,0 +1,45 @@
package com.willfp.eco.core.entities.ai;
import com.google.common.collect.HashBiMap;
import com.willfp.eco.core.entities.ai.entity.EntityGoalAvoidEntity;
import com.willfp.eco.core.serialization.KeyedDeserializer;
import org.bukkit.NamespacedKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
/**
* Class to manage entity goals.
*/
public final class EntityGoals {
/**
* All registered deserializers.
*/
private static final Map<NamespacedKey, KeyedDeserializer<? extends EntityGoal<?>>> BY_KEY = HashBiMap.create();
/**
* minecraft:avoid_entity.
*/
public static final KeyedDeserializer<EntityGoalAvoidEntity> AVOID_ENTITY = register(EntityGoalAvoidEntity.DESERIALIZER);
/**
* Get deserializer by key.
*
* @param key The key.
* @return The deserializer, or null if not found.
*/
@Nullable
public static KeyedDeserializer<? extends EntityGoal<?>> getByKey(@NotNull final NamespacedKey key) {
return BY_KEY.get(key);
}
private static <T extends KeyedDeserializer<? extends EntityGoal<?>>> T register(@NotNull final T toRegister) {
BY_KEY.put(toRegister.getKey(), toRegister);
return toRegister;
}
private EntityGoals() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
}

View File

@@ -1,10 +1,18 @@
package com.willfp.eco.core.entities.ai.entity;
import com.willfp.eco.core.config.interfaces.Config;
import com.willfp.eco.core.entities.Entities;
import com.willfp.eco.core.entities.TestableEntity;
import com.willfp.eco.core.entities.ai.EntityGoal;
import com.willfp.eco.core.serialization.KeyedDeserializer;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.function.Predicate;
/**
@@ -23,5 +31,53 @@ public record EntityGoalAvoidEntity(
double fastSpeed,
@NotNull Predicate<LivingEntity> filter
) implements EntityGoal<Mob> {
/**
* The deserializer for the goal.
*/
public static final KeyedDeserializer<EntityGoalAvoidEntity> DESERIALIZER = new Deserializer();
@SuppressWarnings("unchecked")
private static final class Deserializer implements KeyedDeserializer<EntityGoalAvoidEntity> {
@Override
@Nullable
public EntityGoalAvoidEntity deserialize(@NotNull final Config config) {
if (!(
config.has("avoidClass")
&& config.has("fleeDistance")
&& config.has("slowSpeed")
&& config.has("fastSpeed")
&& config.has("filter")
)) {
return null;
}
try {
TestableEntity filter = Entities.lookup(config.getString("filter"));
return new EntityGoalAvoidEntity(
(Class<? extends LivingEntity>)
Objects.requireNonNull(
EntityType.valueOf(config.getString("avoidClass").toUpperCase()).getEntityClass()
),
config.getDouble("fleeDistance"),
config.getDouble("slowSpeed"),
config.getDouble("fastSpeed"),
filter::matches
);
} catch (Exception e) {
/*
Exceptions could be caused by configs having values of a wrong type,
invalid enum parameters, etc. Serializers shouldn't throw exceptions,
so we encapsulate them as null.
*/
return null;
}
}
@NotNull
@Override
public NamespacedKey getKey() {
return NamespacedKey.minecraft("avoid_entity");
}
}
}

View File

@@ -2,12 +2,14 @@ package com.willfp.eco.core.lookup;
import org.jetbrains.annotations.Nullable;
import java.util.function.Predicate;
/**
* Interface for testing if any object matches another object.
*
* @param <T> The type of object.
*/
public interface Testable<T> {
public interface Testable<T> extends Predicate<T> {
/**
* If object matches the test.
*
@@ -15,4 +17,9 @@ public interface Testable<T> {
* @return If matches.
*/
boolean matches(@Nullable T other);
@Override
default boolean test(@Nullable T other) {
return this.matches(other);
}
}

View File

@@ -0,0 +1,21 @@
package com.willfp.eco.core.serialization;
import com.willfp.eco.core.config.interfaces.Config;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Deserialize objects from configs.
*
* @param <T> The type of object to deserialize.
*/
public interface ConfigDeserializer<T> {
/**
* Deserialize a config to an object.
*
* @param config The config.
* @return The object, or null if invalid.
*/
@Nullable
T deserialize(@NotNull Config config);
}

View File

@@ -0,0 +1,20 @@
package com.willfp.eco.core.serialization;
import com.willfp.eco.core.config.interfaces.Config;
import org.jetbrains.annotations.NotNull;
/**
* Serialize objects to configs.
*
* @param <T> The type of object to serialize.
*/
public interface ConfigSerializer<T> {
/**
* Serialize an object to a config.
*
* @param obj The object.
* @return The config.
*/
@NotNull
Config serialize(@NotNull T obj);
}

View File

@@ -0,0 +1,14 @@
package com.willfp.eco.core.serialization;
import org.bukkit.Keyed;
/**
* Deserialize objects from configs.
* <p>
* Has a key.
*
* @param <T> The type of object to deserialize.
*/
public interface KeyedDeserializer<T> extends ConfigDeserializer<T>, Keyed {
}

View File

@@ -0,0 +1,14 @@
package com.willfp.eco.core.serialization;
import org.bukkit.Keyed;
/**
* Serialize objects to configs.
* <p>
* Has a key.
*
* @param <T> The type of object to serialize.
*/
public interface KeyedSerializer<T> extends ConfigSerializer<T>, Keyed {
}