improve keyring
This commit is contained in:
parent
b9acbf9751
commit
e2176fa1e3
@ -22,7 +22,7 @@ public final class WrappedVillager {
|
||||
WrappedVillager(@NotNull Villager villager) {
|
||||
this.villager = villager;
|
||||
this.dataContainer = villager.getPersistentDataContainer();
|
||||
this.parseOther = VillagerOptimizer.getConfiguration().support_other_plugins;
|
||||
this.parseOther = me.xginko.villageroptimizer.VillagerOptimizer.getConfiguration().support_other_plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,9 +44,9 @@ public final class WrappedVillager {
|
||||
*/
|
||||
public boolean isOptimized() {
|
||||
if (!parseOther) {
|
||||
return isOptimized(Keyring.Namespaces.VillagerOptimizer);
|
||||
return isOptimized(Keyring.Spaces.VillagerOptimizer);
|
||||
}
|
||||
for (Keyring.Namespaces pluginNamespaces : Keyring.Namespaces.values()) {
|
||||
for (Keyring.Spaces pluginNamespaces : Keyring.Spaces.values()) {
|
||||
if (isOptimized(pluginNamespaces)) return true;
|
||||
}
|
||||
return false;
|
||||
@ -55,12 +55,12 @@ public final class WrappedVillager {
|
||||
/**
|
||||
* @return True if the villager is optimized by the supported plugin, otherwise false.
|
||||
*/
|
||||
public boolean isOptimized(Keyring.Namespaces namespaces) {
|
||||
public boolean isOptimized(Keyring.Spaces namespaces) {
|
||||
return switch (namespaces) {
|
||||
case VillagerOptimizer -> dataContainer.has(Keyring.Own.OPTIMIZATION_TYPE.key(), PersistentDataType.STRING);
|
||||
case AntiVillagerLag -> dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_ANY.key(), PersistentDataType.STRING)
|
||||
|| dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_WORKSTATION.key(), PersistentDataType.STRING)
|
||||
|| dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_BLOCK.key(), PersistentDataType.STRING);
|
||||
case VillagerOptimizer -> dataContainer.has(Keyring.VillagerOptimizer.OPTIMIZATION_TYPE.getKey(), PersistentDataType.STRING);
|
||||
case AntiVillagerLag -> dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_ANY.getKey(), PersistentDataType.STRING)
|
||||
|| dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_WORKSTATION.getKey(), PersistentDataType.STRING)
|
||||
|| dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_BLOCK.getKey(), PersistentDataType.STRING);
|
||||
};
|
||||
}
|
||||
|
||||
@ -71,8 +71,8 @@ public final class WrappedVillager {
|
||||
public boolean canOptimize(final long cooldown_millis) {
|
||||
if (parseOther) {
|
||||
if (
|
||||
dataContainer.has(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.key(), PersistentDataType.LONG)
|
||||
&& System.currentTimeMillis() <= 1000 * dataContainer.get(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.key(), PersistentDataType.LONG)
|
||||
dataContainer.has(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)
|
||||
&& System.currentTimeMillis() <= 1000 * dataContainer.get(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
@ -84,17 +84,17 @@ public final class WrappedVillager {
|
||||
* @param type OptimizationType the villager should be set to.
|
||||
*/
|
||||
public void setOptimization(OptimizationType type) {
|
||||
VillagerOptimizer.getFoliaLib().getImpl().runAtEntityTimer(villager, setOptimization -> {
|
||||
me.xginko.villageroptimizer.VillagerOptimizer.getFoliaLib().getImpl().runAtEntityTimer(villager, setOptimization -> {
|
||||
// Keep repeating task until villager is no longer trading with a player
|
||||
if (villager.isTrading()) return;
|
||||
|
||||
if (type.equals(OptimizationType.NONE) && isOptimized()) {
|
||||
if (!parseOther || isOptimized(Keyring.Namespaces.VillagerOptimizer))
|
||||
dataContainer.remove(Keyring.Own.OPTIMIZATION_TYPE.key());
|
||||
if (!parseOther || isOptimized(Keyring.Spaces.VillagerOptimizer))
|
||||
dataContainer.remove(Keyring.VillagerOptimizer.OPTIMIZATION_TYPE.getKey());
|
||||
villager.setAware(true);
|
||||
villager.setAI(true); // Done for stability so villager is guaranteed to wake up
|
||||
} else {
|
||||
dataContainer.set(Keyring.Own.OPTIMIZATION_TYPE.key(), PersistentDataType.STRING, type.name());
|
||||
dataContainer.set(Keyring.VillagerOptimizer.OPTIMIZATION_TYPE.getKey(), PersistentDataType.STRING, type.name());
|
||||
villager.setAware(false);
|
||||
}
|
||||
|
||||
@ -108,31 +108,31 @@ public final class WrappedVillager {
|
||||
*/
|
||||
public @NotNull OptimizationType getOptimizationType() {
|
||||
if (!parseOther) {
|
||||
return getOptimizationType(Keyring.Namespaces.VillagerOptimizer);
|
||||
return getOptimizationType(Keyring.Spaces.VillagerOptimizer);
|
||||
}
|
||||
OptimizationType optimizationType = getOptimizationType(Keyring.Namespaces.VillagerOptimizer);
|
||||
OptimizationType optimizationType = getOptimizationType(Keyring.Spaces.VillagerOptimizer);
|
||||
if (optimizationType != OptimizationType.NONE) {
|
||||
return optimizationType;
|
||||
}
|
||||
return getOptimizationType(Keyring.Namespaces.AntiVillagerLag);
|
||||
return getOptimizationType(Keyring.Spaces.AntiVillagerLag);
|
||||
}
|
||||
|
||||
public @NotNull OptimizationType getOptimizationType(Keyring.Namespaces namespaces) {
|
||||
public @NotNull OptimizationType getOptimizationType(Keyring.Spaces namespaces) {
|
||||
return switch (namespaces) {
|
||||
case VillagerOptimizer -> {
|
||||
if (isOptimized(Keyring.Namespaces.VillagerOptimizer)) {
|
||||
yield OptimizationType.valueOf(dataContainer.get(Keyring.Own.OPTIMIZATION_TYPE.key(), PersistentDataType.STRING));
|
||||
if (isOptimized(Keyring.Spaces.VillagerOptimizer)) {
|
||||
yield OptimizationType.valueOf(dataContainer.get(Keyring.VillagerOptimizer.OPTIMIZATION_TYPE.getKey(), PersistentDataType.STRING));
|
||||
}
|
||||
yield OptimizationType.NONE;
|
||||
}
|
||||
case AntiVillagerLag -> {
|
||||
if (dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_BLOCK.key(), PersistentDataType.STRING)) {
|
||||
if (dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_BLOCK.getKey(), PersistentDataType.STRING)) {
|
||||
yield OptimizationType.BLOCK;
|
||||
}
|
||||
if (dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_WORKSTATION.key(), PersistentDataType.STRING)) {
|
||||
if (dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_WORKSTATION.getKey(), PersistentDataType.STRING)) {
|
||||
yield OptimizationType.WORKSTATION;
|
||||
}
|
||||
if (dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_ANY.key(), PersistentDataType.STRING)) {
|
||||
if (dataContainer.has(Keyring.AntiVillagerLag.OPTIMIZED_ANY.getKey(), PersistentDataType.STRING)) {
|
||||
yield OptimizationType.COMMAND; // Best we can do
|
||||
}
|
||||
yield OptimizationType.NONE;
|
||||
@ -144,15 +144,15 @@ public final class WrappedVillager {
|
||||
* Saves the system time in millis when the villager was last optimized.
|
||||
*/
|
||||
public void saveOptimizeTime() {
|
||||
dataContainer.set(Keyring.Own.LAST_OPTIMIZE.key(), PersistentDataType.LONG, System.currentTimeMillis());
|
||||
dataContainer.set(Keyring.VillagerOptimizer.LAST_OPTIMIZE.getKey(), PersistentDataType.LONG, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The system time in millis when the villager was last optimized, 0L if the villager was never optimized.
|
||||
*/
|
||||
public long getLastOptimize() {
|
||||
if (dataContainer.has(Keyring.Own.LAST_OPTIMIZE.key(), PersistentDataType.LONG)) {
|
||||
return dataContainer.get(Keyring.Own.LAST_OPTIMIZE.key(), PersistentDataType.LONG);
|
||||
if (dataContainer.has(Keyring.VillagerOptimizer.LAST_OPTIMIZE.getKey(), PersistentDataType.LONG)) {
|
||||
return dataContainer.get(Keyring.VillagerOptimizer.LAST_OPTIMIZE.getKey(), PersistentDataType.LONG);
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
@ -169,15 +169,15 @@ public final class WrappedVillager {
|
||||
long remainingMillis = 0L;
|
||||
|
||||
if (parseOther) {
|
||||
if (dataContainer.has(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.key(), PersistentDataType.LONG)) {
|
||||
remainingMillis = System.currentTimeMillis() - dataContainer.get(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.key(), PersistentDataType.LONG);
|
||||
if (dataContainer.has(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)) {
|
||||
remainingMillis = System.currentTimeMillis() - dataContainer.get(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG);
|
||||
}
|
||||
}
|
||||
|
||||
if (remainingMillis > 0) return remainingMillis;
|
||||
|
||||
if (dataContainer.has(Keyring.Own.LAST_OPTIMIZE.key(), PersistentDataType.LONG)) {
|
||||
return System.currentTimeMillis() - (dataContainer.get(Keyring.Own.LAST_OPTIMIZE.key(), PersistentDataType.LONG) + cooldown_millis);
|
||||
if (dataContainer.has(Keyring.VillagerOptimizer.LAST_OPTIMIZE.getKey(), PersistentDataType.LONG)) {
|
||||
return System.currentTimeMillis() - (dataContainer.get(Keyring.VillagerOptimizer.LAST_OPTIMIZE.getKey(), PersistentDataType.LONG) + cooldown_millis);
|
||||
}
|
||||
|
||||
return cooldown_millis;
|
||||
@ -207,7 +207,7 @@ public final class WrappedVillager {
|
||||
* Saves the time of the in-game world when the entity was last restocked.
|
||||
*/
|
||||
public void saveRestockTime() {
|
||||
dataContainer.set(Keyring.Own.LAST_RESTOCK.key(), PersistentDataType.LONG, villager.getWorld().getFullTime());
|
||||
dataContainer.set(Keyring.VillagerOptimizer.LAST_RESTOCK.getKey(), PersistentDataType.LONG, villager.getWorld().getFullTime());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -215,12 +215,12 @@ public final class WrappedVillager {
|
||||
*/
|
||||
public long getLastRestock() {
|
||||
long lastRestock = 0L;
|
||||
if (dataContainer.has(Keyring.Own.LAST_RESTOCK.key(), PersistentDataType.LONG)) {
|
||||
lastRestock = dataContainer.get(Keyring.Own.LAST_RESTOCK.key(), PersistentDataType.LONG);
|
||||
if (dataContainer.has(Keyring.VillagerOptimizer.LAST_RESTOCK.getKey(), PersistentDataType.LONG)) {
|
||||
lastRestock = dataContainer.get(Keyring.VillagerOptimizer.LAST_RESTOCK.getKey(), PersistentDataType.LONG);
|
||||
}
|
||||
if (parseOther) {
|
||||
if (dataContainer.has(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.key(), PersistentDataType.LONG)) {
|
||||
long lastAVLRestock = dataContainer.get(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.key(), PersistentDataType.LONG);
|
||||
if (dataContainer.has(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.getKey(), PersistentDataType.LONG)) {
|
||||
long lastAVLRestock = dataContainer.get(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.getKey(), PersistentDataType.LONG);
|
||||
if (lastRestock < lastAVLRestock) {
|
||||
lastRestock = lastAVLRestock;
|
||||
}
|
||||
@ -230,7 +230,7 @@ public final class WrappedVillager {
|
||||
}
|
||||
|
||||
public long getRestockCooldownMillis(final long cooldown_millis) {
|
||||
return dataContainer.has(Keyring.Own.LAST_RESTOCK.key(), PersistentDataType.LONG) ? (villager.getWorld().getFullTime() - (dataContainer.get(Keyring.Own.LAST_RESTOCK.key(), PersistentDataType.LONG) + cooldown_millis)) : cooldown_millis;
|
||||
return dataContainer.has(Keyring.VillagerOptimizer.LAST_RESTOCK.getKey(), PersistentDataType.LONG) ? (villager.getWorld().getFullTime() - (dataContainer.get(Keyring.VillagerOptimizer.LAST_RESTOCK.getKey(), PersistentDataType.LONG) + cooldown_millis)) : cooldown_millis;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -256,8 +256,8 @@ public final class WrappedVillager {
|
||||
}
|
||||
|
||||
if (parseOther) {
|
||||
return !dataContainer.has(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.key(), PersistentDataType.LONG)
|
||||
|| System.currentTimeMillis() > dataContainer.get(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.key(), PersistentDataType.LONG) * 1000;
|
||||
return !dataContainer.has(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)
|
||||
|| System.currentTimeMillis() > dataContainer.get(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG) * 1000;
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -267,7 +267,7 @@ public final class WrappedVillager {
|
||||
* Saves the time of the in-game world when the entity was last leveled up.
|
||||
*/
|
||||
public void saveLastLevelUp() {
|
||||
dataContainer.set(Keyring.Own.LAST_LEVELUP.key(), PersistentDataType.LONG, villager.getWorld().getFullTime());
|
||||
dataContainer.set(Keyring.VillagerOptimizer.LAST_LEVELUP.getKey(), PersistentDataType.LONG, villager.getWorld().getFullTime());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -277,22 +277,22 @@ public final class WrappedVillager {
|
||||
* @return The time of the in-game world when the entity was last leveled up.
|
||||
*/
|
||||
public long getLastLevelUpTime() {
|
||||
return dataContainer.has(Keyring.Own.LAST_LEVELUP.key(), PersistentDataType.LONG) ? dataContainer.get(Keyring.Own.LAST_LEVELUP.key(), PersistentDataType.LONG) : 0L;
|
||||
return dataContainer.has(Keyring.VillagerOptimizer.LAST_LEVELUP.getKey(), PersistentDataType.LONG) ? dataContainer.get(Keyring.VillagerOptimizer.LAST_LEVELUP.getKey(), PersistentDataType.LONG) : 0L;
|
||||
}
|
||||
|
||||
public long getLevelCooldownMillis(final long cooldown_millis) {
|
||||
return dataContainer.has(Keyring.Own.LAST_LEVELUP.key(), PersistentDataType.LONG) ? (villager.getWorld().getFullTime() - (dataContainer.get(Keyring.Own.LAST_LEVELUP.key(), PersistentDataType.LONG) + cooldown_millis)) : cooldown_millis;
|
||||
return dataContainer.has(Keyring.VillagerOptimizer.LAST_LEVELUP.getKey(), PersistentDataType.LONG) ? (villager.getWorld().getFullTime() - (dataContainer.get(Keyring.VillagerOptimizer.LAST_LEVELUP.getKey(), PersistentDataType.LONG) + cooldown_millis)) : cooldown_millis;
|
||||
}
|
||||
|
||||
public void memorizeName(final Component customName) {
|
||||
dataContainer.set(Keyring.Own.LAST_OPTIMIZE_NAME.key(), PersistentDataType.STRING, MiniMessage.miniMessage().serialize(customName));
|
||||
dataContainer.set(Keyring.VillagerOptimizer.LAST_OPTIMIZE_NAME.getKey(), PersistentDataType.STRING, MiniMessage.miniMessage().serialize(customName));
|
||||
}
|
||||
|
||||
public @Nullable Component getMemorizedName() {
|
||||
return dataContainer.has(Keyring.Own.LAST_OPTIMIZE_NAME.key(), PersistentDataType.STRING) ? MiniMessage.miniMessage().deserialize(dataContainer.get(Keyring.Own.LAST_OPTIMIZE_NAME.key(), PersistentDataType.STRING)) : null;
|
||||
return dataContainer.has(Keyring.VillagerOptimizer.LAST_OPTIMIZE_NAME.getKey(), PersistentDataType.STRING) ? MiniMessage.miniMessage().deserialize(dataContainer.get(Keyring.VillagerOptimizer.LAST_OPTIMIZE_NAME.getKey(), PersistentDataType.STRING)) : null;
|
||||
}
|
||||
|
||||
public void forgetName() {
|
||||
dataContainer.remove(Keyring.Own.LAST_OPTIMIZE_NAME.key());
|
||||
dataContainer.remove(Keyring.VillagerOptimizer.LAST_OPTIMIZE_NAME.getKey());
|
||||
}
|
||||
}
|
@ -1,23 +1,33 @@
|
||||
package me.xginko.villageroptimizer.enums;
|
||||
|
||||
import net.kyori.adventure.key.KeyPattern;
|
||||
import net.kyori.adventure.key.Namespaced;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.intellij.lang.annotations.Subst;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class Keyring {
|
||||
|
||||
public enum Namespaces {
|
||||
public enum Spaces implements Namespaced {
|
||||
VillagerOptimizer("VillagerOptimizer"),
|
||||
AntiVillagerLag("AntiVillagerLag");
|
||||
|
||||
private final String pluginName;
|
||||
Namespaces(String pluginName) {
|
||||
this.pluginName = pluginName;
|
||||
@KeyPattern.Namespace
|
||||
private final @NotNull String namespace;
|
||||
|
||||
Spaces(@NotNull @KeyPattern.Namespace String pluginName) {
|
||||
this.namespace = pluginName.toLowerCase(Locale.ROOT);
|
||||
}
|
||||
public String pluginName() {
|
||||
return pluginName;
|
||||
|
||||
@Override
|
||||
@KeyPattern.Namespace
|
||||
public @NotNull String namespace() {
|
||||
return namespace;
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,33 +37,36 @@ public class Keyring {
|
||||
* 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 pluginName The plugin name as configured in plugin.yml, under 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) {
|
||||
public static NamespacedKey getKey(@NotNull String pluginName, @NotNull String key) {
|
||||
return new NamespacedKey(pluginName.toLowerCase(Locale.ROOT), key);
|
||||
}
|
||||
|
||||
public enum Own {
|
||||
public enum VillagerOptimizer implements Keyed {
|
||||
OPTIMIZATION_TYPE("optimization-type"),
|
||||
LAST_OPTIMIZE("last-optimize"),
|
||||
LAST_LEVELUP("last-levelup"),
|
||||
LAST_RESTOCK("last-restock"),
|
||||
LAST_OPTIMIZE_NAME("last-optimize-name");
|
||||
|
||||
private final NamespacedKey key;
|
||||
Own(String key) {
|
||||
this.key = Keyring.getKey(Namespaces.VillagerOptimizer.pluginName(), key);
|
||||
private final @NotNull NamespacedKey key;
|
||||
|
||||
VillagerOptimizer(@NotNull String key) {
|
||||
this.key = new NamespacedKey(Spaces.VillagerOptimizer.namespace(), key);
|
||||
}
|
||||
public NamespacedKey key() {
|
||||
|
||||
@Override
|
||||
public @NotNull NamespacedKey getKey() {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
public enum AntiVillagerLag {
|
||||
public enum AntiVillagerLag implements Keyed {
|
||||
NEXT_OPTIMIZATION_SYSTIME_SECONDS("cooldown"), // Returns LONG -> (System.currentTimeMillis() / 1000) + cooldown seconds
|
||||
LAST_RESTOCK_WORLDFULLTIME("time"), // Returns LONG -> villager.getWorld().getFullTime()
|
||||
NEXT_LEVELUP_SYSTIME_SECONDS("levelCooldown"), // Returns LONG -> (System.currentTimeMillis() / 1000) + cooldown seconds
|
||||
@ -61,11 +74,14 @@ public class Keyring {
|
||||
OPTIMIZED_BLOCK("disabledByBlock"), // Returns STRING -> key().toString()
|
||||
OPTIMIZED_WORKSTATION("disabledByWorkstation"); // Returns STRING -> key().toString()
|
||||
|
||||
private final NamespacedKey key;
|
||||
AntiVillagerLag(String avlKey) {
|
||||
this.key = Keyring.getKey(Namespaces.AntiVillagerLag.pluginName(), avlKey);
|
||||
private final @NotNull NamespacedKey key;
|
||||
|
||||
AntiVillagerLag(@NotNull String avlKey) {
|
||||
this.key = new NamespacedKey(Spaces.AntiVillagerLag.namespace(), avlKey);
|
||||
}
|
||||
public NamespacedKey key() {
|
||||
|
||||
@Override
|
||||
public @NotNull NamespacedKey getKey() {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
package me.xginko.villageroptimizer.modules.optimization;
|
||||
|
||||
import io.papermc.paper.event.entity.EntityMoveEvent;
|
||||
import me.xginko.villageroptimizer.VillagerCache;
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import me.xginko.villageroptimizer.WrappedVillager;
|
||||
import me.xginko.villageroptimizer.config.Config;
|
||||
import me.xginko.villageroptimizer.enums.permissions.Bypass;
|
||||
import me.xginko.villageroptimizer.enums.OptimizationType;
|
||||
import me.xginko.villageroptimizer.enums.permissions.Bypass;
|
||||
import me.xginko.villageroptimizer.enums.permissions.Optimize;
|
||||
import me.xginko.villageroptimizer.events.VillagerOptimizeEvent;
|
||||
import me.xginko.villageroptimizer.events.VillagerUnoptimizeEvent;
|
||||
@ -13,7 +14,6 @@ import me.xginko.villageroptimizer.modules.VillagerOptimizerModule;
|
||||
import me.xginko.villageroptimizer.utils.CommonUtil;
|
||||
import net.kyori.adventure.text.TextReplacementConfig;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
@ -28,6 +28,8 @@ import org.bukkit.event.block.BlockPlaceEvent;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static me.xginko.villageroptimizer.utils.CommonUtil.getWorkstationProfession;
|
||||
|
||||
public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener {
|
||||
|
||||
private final VillagerCache villagerCache;
|
||||
@ -72,6 +74,15 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
|
||||
return VillagerOptimizer.getConfiguration().getBoolean("optimization-methods.workstation-optimization.enable", false);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
private void onVillagerMove(EntityMoveEvent event) {
|
||||
if (!event.getEntity().getType().equals(EntityType.VILLAGER)) return;
|
||||
Villager villager = (Villager) event.getEntity();
|
||||
if (villager.getProfession().equals(Villager.Profession.NONE)) return;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
private void onBlockPlace(BlockPlaceEvent event) {
|
||||
Block placed = event.getBlock();
|
||||
@ -189,23 +200,4 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
|
||||
if (log_enabled)
|
||||
VillagerOptimizer.getLog().info(player.getName() + " unoptimized a villager by breaking workstation: '" + broken.getType().toString().toLowerCase() + "'");
|
||||
}
|
||||
|
||||
private Villager.Profession getWorkstationProfession(final Material workstation) {
|
||||
return switch (workstation) {
|
||||
case BARREL -> Villager.Profession.FISHERMAN;
|
||||
case CARTOGRAPHY_TABLE -> Villager.Profession.CARTOGRAPHER;
|
||||
case SMOKER -> Villager.Profession.BUTCHER;
|
||||
case SMITHING_TABLE -> Villager.Profession.TOOLSMITH;
|
||||
case GRINDSTONE -> Villager.Profession.WEAPONSMITH;
|
||||
case BLAST_FURNACE -> Villager.Profession.ARMORER;
|
||||
case CAULDRON -> Villager.Profession.LEATHERWORKER;
|
||||
case BREWING_STAND -> Villager.Profession.CLERIC;
|
||||
case COMPOSTER -> Villager.Profession.FARMER;
|
||||
case FLETCHING_TABLE -> Villager.Profession.FLETCHER;
|
||||
case LOOM -> Villager.Profession.SHEPHERD;
|
||||
case LECTERN -> Villager.Profession.LIBRARIAN;
|
||||
case STONECUTTER -> Villager.Profession.MASON;
|
||||
default -> Villager.Profession.NONE;
|
||||
};
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package me.xginko.villageroptimizer.utils;
|
||||
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@ -40,4 +41,23 @@ public class CommonUtil {
|
||||
villager.shakeHead();
|
||||
} catch (NoSuchMethodError ignored) {}
|
||||
}
|
||||
|
||||
public static Villager.Profession getWorkstationProfession(@NotNull Material workstation) {
|
||||
return switch (workstation) {
|
||||
case BARREL -> Villager.Profession.FISHERMAN;
|
||||
case CARTOGRAPHY_TABLE -> Villager.Profession.CARTOGRAPHER;
|
||||
case SMOKER -> Villager.Profession.BUTCHER;
|
||||
case SMITHING_TABLE -> Villager.Profession.TOOLSMITH;
|
||||
case GRINDSTONE -> Villager.Profession.WEAPONSMITH;
|
||||
case BLAST_FURNACE -> Villager.Profession.ARMORER;
|
||||
case CAULDRON -> Villager.Profession.LEATHERWORKER;
|
||||
case BREWING_STAND -> Villager.Profession.CLERIC;
|
||||
case COMPOSTER -> Villager.Profession.FARMER;
|
||||
case FLETCHING_TABLE -> Villager.Profession.FLETCHER;
|
||||
case LOOM -> Villager.Profession.SHEPHERD;
|
||||
case LECTERN -> Villager.Profession.LIBRARIAN;
|
||||
case STONECUTTER -> Villager.Profession.MASON;
|
||||
default -> Villager.Profession.NONE;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user