even cleaner approach

This commit is contained in:
xGinko 2024-01-25 16:04:47 +01:00
parent 604f16b5df
commit 6c4b30916d
2 changed files with 40 additions and 41 deletions

View File

@ -41,10 +41,10 @@ public final class WrappedVillager {
*/
public boolean isOptimized() {
if (!parseOther) {
return isOptimized(Keys.Origin.VillagerOptimizer);
return isOptimized(Keys.Namespaces.VillagerOptimizer);
}
for (Keys.Origin pluginOrigin : Keys.Origin.values()) {
if (isOptimized(pluginOrigin)) return true;
for (Keys.Namespaces pluginNamespaces : Keys.Namespaces.values()) {
if (isOptimized(pluginNamespaces)) return true;
}
return false;
}
@ -52,8 +52,8 @@ public final class WrappedVillager {
/**
* @return True if the villager is optimized by the supported plugin, otherwise false.
*/
public boolean isOptimized(Keys.Origin origin) {
return switch (origin) {
public boolean isOptimized(Keys.Namespaces namespaces) {
return switch (namespaces) {
case VillagerOptimizer -> dataContainer.has(Keys.Own.OPTIMIZATION_TYPE.key(), PersistentDataType.STRING);
case AntiVillagerLag -> dataContainer.has(Keys.AntiVillagerLag.OPTIMIZED_ANY.key(), PersistentDataType.STRING)
|| dataContainer.has(Keys.AntiVillagerLag.OPTIMIZED_WORKSTATION.key(), PersistentDataType.STRING)
@ -82,7 +82,7 @@ public final class WrappedVillager {
*/
public void setOptimization(OptimizationType type) {
if (type.equals(OptimizationType.NONE) && isOptimized()) {
if (!parseOther || isOptimized(Keys.Origin.VillagerOptimizer)) {
if (!parseOther || isOptimized(Keys.Namespaces.VillagerOptimizer)) {
dataContainer.remove(Keys.Own.OPTIMIZATION_TYPE.key());
}
VillagerOptimizer.getScheduler().runAtEntity(villager, enableAI -> {
@ -100,19 +100,19 @@ public final class WrappedVillager {
*/
public @NotNull OptimizationType getOptimizationType() {
if (!parseOther) {
return getOptimizationType(Keys.Origin.VillagerOptimizer);
return getOptimizationType(Keys.Namespaces.VillagerOptimizer);
}
OptimizationType optimizationType = getOptimizationType(Keys.Origin.VillagerOptimizer);
OptimizationType optimizationType = getOptimizationType(Keys.Namespaces.VillagerOptimizer);
if (optimizationType != OptimizationType.NONE) {
return optimizationType;
}
return getOptimizationType(Keys.Origin.AntiVillagerLag);
return getOptimizationType(Keys.Namespaces.AntiVillagerLag);
}
public @NotNull OptimizationType getOptimizationType(Keys.Origin origin) {
return switch (origin) {
public @NotNull OptimizationType getOptimizationType(Keys.Namespaces namespaces) {
return switch (namespaces) {
case VillagerOptimizer -> {
if (isOptimized(Keys.Origin.VillagerOptimizer)) {
if (isOptimized(Keys.Namespaces.VillagerOptimizer)) {
yield OptimizationType.valueOf(dataContainer.get(Keys.Own.OPTIMIZATION_TYPE.key(), PersistentDataType.STRING));
}
yield OptimizationType.NONE;

View File

@ -1,6 +1,5 @@
package me.xginko.villageroptimizer.enums;
import me.xginko.villageroptimizer.VillagerOptimizer;
import org.bukkit.NamespacedKey;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.plugin.Plugin;
@ -8,10 +7,33 @@ import org.bukkit.plugin.Plugin;
import java.util.Locale;
public class Keys {
public enum Namespaces {
VillagerOptimizer("VillagerOptimizer"),
AntiVillagerLag("AntiVillagerLag");
public enum Origin {
VillagerOptimizer,
AntiVillagerLag;
private final String namespace;
Namespaces(String pluginName) {
this.namespace = pluginName;
}
public String namespace() {
return namespace;
}
}
/**
* Returns a NamespacedKey as if it was created by a specific plugin.
* This is possible because they are created using {@link NamespacedKey#NamespacedKey(Plugin, String)},
* meaning the Namespace is always the return of {@link Plugin#getName()} && {@link String#toLowerCase()}
* using {@link Locale#ROOT}
*
* @param pluginName The plugin name as configured in plugin.yml, section name
* @param key The key name
*
* @return a {@link NamespacedKey} that can be used to test for and read data stored by plugins
* from a {@link PersistentDataContainer}
*/
public static NamespacedKey getKey(String pluginName, String key) {
return new NamespacedKey(pluginName.toLowerCase(Locale.ROOT), key);
}
public enum Own {
@ -24,22 +46,12 @@ public class Keys {
private final NamespacedKey key;
Own(String key) {
this.key = getKey(key);
this.key = Keys.getKey(Namespaces.VillagerOptimizer.namespace(), key);
}
public NamespacedKey key() {
return key;
}
/**
* Returns a NamespacedKey created by VillagerOptimizer.
*
* @return a {@link NamespacedKey} that can be used to test for and read data stored by VillagerOptimizer
* from a {@link PersistentDataContainer}
*/
public static NamespacedKey getKey(String key) {
return new NamespacedKey(VillagerOptimizer.getInstance(), key);
}
}
public enum AntiVillagerLag {
@ -53,24 +65,11 @@ public class Keys {
private final NamespacedKey key;
AntiVillagerLag(String avlKey) {
this.key = getKey(avlKey);
this.key = Keys.getKey(Namespaces.AntiVillagerLag.namespace(), avlKey);
}
public NamespacedKey key() {
return key;
}
/**
* Returns a NamespacedKey as if it was created by AntiVillagerLag.
* This is possible because they are created using {@link NamespacedKey#NamespacedKey(Plugin, String)},
* meaning the Namespace is always the return of {@link Plugin#getName()} && {@link String#toLowerCase()}
* using {@link Locale#ROOT}
*
* @return a {@link NamespacedKey} that can be used to test for and read data stored by AntiVillagerLag
* from a {@link PersistentDataContainer}
*/
public static NamespacedKey getKey(String key) {
return new NamespacedKey("AntiVillagerLag".toLowerCase(Locale.ROOT), key);
}
}
}