Compare commits

..

13 Commits

Author SHA1 Message Date
Auxilor
af486580c1 Updated to 6.52.0 2023-02-27 16:31:19 +00:00
Auxilor
7955a94f14 Added missing annotations 2023-02-25 19:03:18 +00:00
Auxilor
c99a1bd50a loadListeners is no longer abstract 2023-02-25 19:02:14 +00:00
Auxilor
93364247de Merge branch 'master' into develop 2023-02-25 14:49:15 +00:00
Auxilor
49612eddcb Updated to 6.51.4 2023-02-24 14:02:16 +00:00
Auxilor
834c29f843 Fixed placeholder bug with additional players, prevented NaN being evaluated by crunch 2023-02-24 14:02:04 +00:00
Auxilor
a806ac039d Added regex validation for Ids 2023-02-23 21:15:12 +00:00
Auxilor
0ca2651af0 Added ListMap#append 2023-02-23 21:07:04 +00:00
Auxilor
6157fdcfa1 Improved DefaultMap 2023-02-23 21:04:01 +00:00
Auxilor
36cfcd24c2 Added DefaultMap 2023-02-23 20:51:21 +00:00
Auxilor
d4558db40c Added registry 2023-02-23 18:33:28 +00:00
Auxilor
3f8448fee1 Updated to 6.51.3 2023-02-18 12:26:39 +00:00
Auxilor
8b6e15457c Fixed clearing entity goals on 1.19+ 2023-02-18 12:26:32 +00:00
13 changed files with 586 additions and 9 deletions

View File

@@ -713,7 +713,9 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike {
*
* @return A list of all listeners.
*/
protected abstract List<Listener> loadListeners();
protected List<Listener> loadListeners() {
return new ArrayList<>();
}
/**
* Useful for custom LangYml implementations.
@@ -862,6 +864,7 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike {
* @param pluginName The name.
* @return The plugin.
*/
@Nullable
public static EcoPlugin getPlugin(@NotNull final String pluginName) {
return Eco.get().getPluginByName(pluginName);
}
@@ -871,6 +874,7 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike {
*
* @return The set of names.
*/
@NotNull
public static Set<String> getPluginNames() {
return new HashSet<>(Eco.get().getLoadedPlugins());
}

View File

@@ -276,6 +276,35 @@ public final class PlaceholderManager {
@NotNull final Collection<AdditionalPlayer> additionalPlayers) {
String processed = text;
/*
Why am I doing statics at the start, but player statics at the end?
Additional players let you use something like victim as a player to parse in relation to,
for example doing %victim_player_health%, which would parse the health of the victim.
However, something like libreforge will also inject %victim_max_health%, which is unrelated
to additional players, and instead holds a constant value. So, eco saw this, smartly thought
"ah, it's an additional player, let's parse it", and then tried to parse %max_health% with
relation to the victim, which resolved to zero. So, we have to parse statics and player statics
that might include a prefix first, then additional players, then player statics with the support
of additional players.
This was a massive headache and took so many reports before I clocked what was going on.
Oh well, at least it's fixed now.
*/
for (InjectablePlaceholder injection : context.getPlaceholderInjections()) {
if (injection instanceof StaticPlaceholder placeholder) {
processed = processed.replace("%" + placeholder.getIdentifier() + "%", placeholder.getValue());
} else if (injection instanceof PlayerStaticPlaceholder placeholder && player != null) {
processed = processed.replace("%" + placeholder.getIdentifier() + "%", placeholder.getValue(player));
}
}
// Prevent running 2 scans if there are no additional players.
if (!additionalPlayers.isEmpty()) {
List<String> found = findPlaceholdersIn(text);
@@ -301,15 +330,14 @@ public final class PlaceholderManager {
processed = integration.translate(processed, player);
}
// DON'T REMOVE THIS, IT'S NOT DUPLICATE CODE.
for (InjectablePlaceholder injection : context.getPlaceholderInjections()) {
// Do I know this is a bad way of doing this? Yes.
// I know it's deprecated, but it's fast.
if (injection instanceof StaticPlaceholder placeholder) {
processed = processed.replace("%" + placeholder.getIdentifier() + "%", placeholder.getValue());
} else if (injection instanceof PlayerStaticPlaceholder placeholder && player != null) {
if (injection instanceof PlayerStaticPlaceholder placeholder && player != null) {
processed = processed.replace("%" + placeholder.getIdentifier() + "%", placeholder.getValue(player));
}
}
return processed;
}

View File

@@ -0,0 +1,147 @@
package com.willfp.eco.core.map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* A map with a default value.
*
* @param <K> The key type.
* @param <V> The value type.
*/
public class DefaultMap<K, V> implements Map<K, V> {
/**
* The map.
*/
private final Map<K, V> map;
/**
* The default value.
*/
private final V defaultValue;
/**
* Create a new default map.
*
* @param defaultValue The default value.
*/
public DefaultMap(@NotNull final V defaultValue) {
this.map = new HashMap<>();
this.defaultValue = defaultValue;
}
/**
* Create a new default map.
*
* @param map The map.
* @param defaultValue The default value.
*/
public DefaultMap(@NotNull final Map<K, V> map, @NotNull final V defaultValue) {
this.map = map;
this.defaultValue = defaultValue;
}
@Override
@NotNull
@SuppressWarnings("unchecked")
public V get(@Nullable final Object key) {
if (key == null) {
return defaultValue;
}
if (map.get(key) == null) {
map.put((K) key, defaultValue);
}
return map.get(key);
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean containsKey(@Nullable final Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(@Nullable final Object value) {
return map.containsValue(value);
}
@Override
public V put(@NotNull final K key, @Nullable final V value) {
return map.put(key, value);
}
@Override
public V remove(@NotNull final Object key) {
return map.remove(key);
}
@Override
public void putAll(@NotNull final Map<? extends K, ? extends V> m) {
map.putAll(m);
}
@Override
public void clear() {
map.clear();
}
@NotNull
@Override
public Set<K> keySet() {
return map.keySet();
}
@NotNull
@Override
public Collection<V> values() {
return map.values();
}
@NotNull
@Override
public Set<Entry<K, V>> entrySet() {
return map.entrySet();
}
/**
* Create a new nested map.
*
* @param <K> The key type.
* @param <K1> The nested key type.
* @param <V> The value type.
* @return The nested map.
*/
@NotNull
public static <K, K1, V> DefaultMap<K, Map<K1, V>> createNestedMap() {
return new DefaultMap<>(new HashMap<>());
}
/**
* Create a new nested list map.
*
* @param <K> The key type.
* @param <K1> The nested key type.
* @param <V> The value type.
* @return The nested list map.
*/
@NotNull
public static <K, K1, V> DefaultMap<K, ListMap<K1, V>> createNestedListMap() {
return new DefaultMap<>(new ListMap<>());
}
}

View File

@@ -0,0 +1,33 @@
package com.willfp.eco.core.map;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
/**
* Maps keys to lists of values.
*
* @param <K> The key type.
* @param <V> The value type.
*/
public class ListMap<K, V> extends DefaultMap<K, List<V>> {
/**
* Create a new list map.
*/
public ListMap() {
super(new ArrayList<>());
}
/**
* Append a value to a key.
*
* @param key The key.
* @param value The value.
*/
void append(@NotNull final K key,
@NotNull final V value) {
this.get(key).add(value);
}
}

View File

@@ -0,0 +1,36 @@
package com.willfp.eco.core.registry;
import org.jetbrains.annotations.NotNull;
/**
* An object that can be registered.
*
* @see Registry
*/
public interface Registrable {
/**
* Get the ID of the element.
*
* @return The ID.
*/
@NotNull
String getID();
/**
* Called when the element is registered.
* <p>
* This is called after registration.
*/
default void onRegister() {
// Do nothing by default.
}
/**
* Called when the element is removed.
* <p>
* This is called before removal.
*/
default void onRemove() {
// Do nothing by default.
}
}

View File

@@ -0,0 +1,111 @@
package com.willfp.eco.core.registry;
import org.apache.commons.lang.Validate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
/**
* A registry for {@link Registrable}s.
*
* @param <T> The type of {@link Registrable}.
*/
public abstract class Registry<T extends Registrable> {
/**
* The ID pattern.
*/
private static final Pattern ID_PATTERN = Pattern.compile("[a-z0-9_]{1,100}");
/**
* The registry.
*/
private final Map<String, T> registry = new HashMap<>();
/**
* Instantiate a new registry.
*/
protected Registry() {
}
/**
* Register a new element.
*
* @param element The element to register.
* @return The element.
*/
@NotNull
public T register(@NotNull final T element) {
Validate.isTrue(ID_PATTERN.matcher(element.getID()).matches(), "ID must match pattern: " + ID_PATTERN.pattern());
registry.put(element.getID(), element);
element.onRegister();
return element;
}
/**
* Remove an element.
*
* @param element The element.
* @return The element.
*/
public T remove(@NotNull final T element) {
element.onRemove();
registry.remove(element.getID());
return element;
}
/**
* Remove an element by ID.
*
* @param id The ID.
* @return The element.
*/
@Nullable
public T remove(@NotNull final String id) {
T element = registry.get(id);
if (element != null) {
element.onRemove();
}
return registry.remove(id);
}
/**
* Get an element by ID.
*
* @param id The ID.
* @return The element, or null if not found.
*/
@Nullable
public T get(@NotNull final String id) {
return registry.get(id);
}
/**
* Clear the registry.
*/
public void clear() {
for (T value : registry.values()) {
remove(value);
}
}
/**
* Get all elements.
*
* @return All elements.
*/
public Set<T> values() {
return Set.copyOf(registry.values());
}
}

View File

@@ -0,0 +1,27 @@
@file:JvmName("DefaultMapExtensions")
package com.willfp.eco.core.map
/**
* @see DefaultMap
*/
fun <K : Any, V : Any> defaultMap(defaultValue: V) =
DefaultMap<K, V>(defaultValue)
/**
* @see ListMap
*/
fun <K : Any, V : Any> listMap() =
ListMap<K, V>()
/**
* @see DefaultMap.createNestedMap
*/
fun <K : Any, K1 : Any, V : Any> nestedMap() =
DefaultMap.createNestedMap<K, K1, V>()
/**
* @see DefaultMap.createNestedListMap
*/
fun <K : Any, K1 : Any, V : Any> nestedListMap() =
DefaultMap.createNestedListMap<K, K1, V>()

View File

@@ -2,7 +2,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_19_R1
import com.willfp.eco.core.entities.ai.EntityController
import com.willfp.eco.internal.spigot.proxy.EntityControllerFactoryProxy
import com.willfp.eco.internal.spigot.proxy.common.ai.EcoEntityController
import com.willfp.eco.internal.spigot.proxy.v1_19_R1.entity.EcoEntityController
import org.bukkit.entity.Mob
class EntityControllerFactory : EntityControllerFactoryProxy {

View File

@@ -0,0 +1,95 @@
package com.willfp.eco.internal.spigot.proxy.v1_19_R1.entity
import com.willfp.eco.core.entities.ai.CustomGoal
import com.willfp.eco.core.entities.ai.EntityController
import com.willfp.eco.core.entities.ai.EntityGoal
import com.willfp.eco.core.entities.ai.TargetGoal
import com.willfp.eco.internal.spigot.proxy.common.ai.CustomGoalFactory
import com.willfp.eco.internal.spigot.proxy.common.ai.getGoalFactory
import com.willfp.eco.internal.spigot.proxy.common.toPathfinderMob
import net.minecraft.world.entity.PathfinderMob
import net.minecraft.world.entity.ai.goal.Goal
import org.bukkit.entity.Mob
class EcoEntityController<T : Mob>(
private val handle: T
) : EntityController<T> {
override fun addEntityGoal(priority: Int, goal: EntityGoal<in T>): EntityController<T> {
val nms = getNms() ?: return this
nms.goalSelector.addGoal(
priority,
goal.getGoalFactory()?.create(goal, nms) ?: return this
)
return this
}
override fun removeEntityGoal(goal: EntityGoal<in T>): EntityController<T> {
val nms = getNms() ?: return this
val predicate: (Goal) -> Boolean = if (goal is CustomGoal<*>) {
{ CustomGoalFactory.isGoalOfType(it, goal) }
} else {
{ goal.getGoalFactory()?.isGoalOfType(it) == true }
}
for (wrapped in nms.goalSelector.availableGoals.toSet()) {
if (predicate(wrapped.goal)) {
nms.goalSelector.removeGoal(wrapped.goal)
}
}
return this
}
override fun clearEntityGoals(): EntityController<T> {
val nms = getNms() ?: return this
nms.goalSelector.availableGoals.clear()
return this
}
override fun addTargetGoal(priority: Int, goal: TargetGoal<in T>): EntityController<T> {
val nms = getNms() ?: return this
nms.targetSelector.addGoal(
priority, goal.getGoalFactory()?.create(goal, nms) ?: return this
)
nms.targetSelector
return this
}
override fun removeTargetGoal(goal: TargetGoal<in T>): EntityController<T> {
val nms = getNms() ?: return this
val predicate: (Goal) -> Boolean = if (goal is CustomGoal<*>) {
{ CustomGoalFactory.isGoalOfType(it, goal) }
} else {
{ goal.getGoalFactory()?.isGoalOfType(it) == true }
}
for (wrapped in nms.targetSelector.availableGoals.toSet()) {
if (predicate(wrapped.goal)) {
nms.targetSelector.removeGoal(wrapped.goal)
}
}
return this
}
override fun clearTargetGoals(): EntityController<T> {
val nms = getNms() ?: return this
nms.targetSelector.availableGoals.clear()
return this
}
private fun getNms(): PathfinderMob? {
return handle.toPathfinderMob()
}
override fun getEntity(): T {
return handle
}
}

View File

@@ -2,7 +2,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_19_R2
import com.willfp.eco.core.entities.ai.EntityController
import com.willfp.eco.internal.spigot.proxy.EntityControllerFactoryProxy
import com.willfp.eco.internal.spigot.proxy.common.ai.EcoEntityController
import com.willfp.eco.internal.spigot.proxy.v1_19_R2.entity.EcoEntityController
import org.bukkit.entity.Mob
class EntityControllerFactory : EntityControllerFactoryProxy {

View File

@@ -0,0 +1,95 @@
package com.willfp.eco.internal.spigot.proxy.v1_19_R2.entity
import com.willfp.eco.core.entities.ai.CustomGoal
import com.willfp.eco.core.entities.ai.EntityController
import com.willfp.eco.core.entities.ai.EntityGoal
import com.willfp.eco.core.entities.ai.TargetGoal
import com.willfp.eco.internal.spigot.proxy.common.ai.CustomGoalFactory
import com.willfp.eco.internal.spigot.proxy.common.ai.getGoalFactory
import com.willfp.eco.internal.spigot.proxy.common.toPathfinderMob
import net.minecraft.world.entity.PathfinderMob
import net.minecraft.world.entity.ai.goal.Goal
import org.bukkit.entity.Mob
class EcoEntityController<T : Mob>(
private val handle: T
) : EntityController<T> {
override fun addEntityGoal(priority: Int, goal: EntityGoal<in T>): EntityController<T> {
val nms = getNms() ?: return this
nms.goalSelector.addGoal(
priority,
goal.getGoalFactory()?.create(goal, nms) ?: return this
)
return this
}
override fun removeEntityGoal(goal: EntityGoal<in T>): EntityController<T> {
val nms = getNms() ?: return this
val predicate: (Goal) -> Boolean = if (goal is CustomGoal<*>) {
{ CustomGoalFactory.isGoalOfType(it, goal) }
} else {
{ goal.getGoalFactory()?.isGoalOfType(it) == true }
}
for (wrapped in nms.goalSelector.availableGoals.toSet()) {
if (predicate(wrapped.goal)) {
nms.goalSelector.removeGoal(wrapped.goal)
}
}
return this
}
override fun clearEntityGoals(): EntityController<T> {
val nms = getNms() ?: return this
nms.goalSelector.availableGoals.clear()
return this
}
override fun addTargetGoal(priority: Int, goal: TargetGoal<in T>): EntityController<T> {
val nms = getNms() ?: return this
nms.targetSelector.addGoal(
priority, goal.getGoalFactory()?.create(goal, nms) ?: return this
)
nms.targetSelector
return this
}
override fun removeTargetGoal(goal: TargetGoal<in T>): EntityController<T> {
val nms = getNms() ?: return this
val predicate: (Goal) -> Boolean = if (goal is CustomGoal<*>) {
{ CustomGoalFactory.isGoalOfType(it, goal) }
} else {
{ goal.getGoalFactory()?.isGoalOfType(it) == true }
}
for (wrapped in nms.targetSelector.availableGoals.toSet()) {
if (predicate(wrapped.goal)) {
nms.targetSelector.removeGoal(wrapped.goal)
}
}
return this
}
override fun clearTargetGoals(): EntityController<T> {
val nms = getNms() ?: return this
nms.targetSelector.availableGoals.clear()
return this
}
private fun getNms(): PathfinderMob? {
return handle.toPathfinderMob()
}
override fun getEntity(): T {
return handle
}
}

View File

@@ -3,8 +3,8 @@ package com.willfp.eco.internal.spigot.math
import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager
import com.willfp.eco.core.placeholder.AdditionalPlayer
import com.willfp.eco.core.math.MathContext
import com.willfp.eco.core.placeholder.AdditionalPlayer
import com.willfp.eco.core.placeholder.PlaceholderInjectable
import org.bukkit.entity.Player
import redempt.crunch.CompiledExpression
@@ -28,6 +28,7 @@ private val max = Function("max", 2) {
fun evaluateExpression(expression: String, context: MathContext) =
evaluateExpression(expression, context.player, context.injectableContext, context.additionalPlayers)
.let { if (!it.isFinite()) 0.0 else it } // Fixes NaN bug.
private fun evaluateExpression(
expression: String,

View File

@@ -1,3 +1,3 @@
version = 6.51.2
version = 6.52.0
plugin-name = eco
kotlin.code.style = official