add more handlers

This commit is contained in:
xGinko 2023-09-05 23:07:12 +02:00
parent 80ef3e7af7
commit 6fb7f96d8f
5 changed files with 185 additions and 4 deletions

View File

@ -56,7 +56,7 @@ public class Config {
Values here need to be valid bukkit Material enums for your server version. Values here need to be valid bukkit Material enums for your server version.
""" """
); );
this.workstation_max_distance = getDouble("optimization.methods.by-workstation.", 4.0, this.workstation_max_distance = getDouble("optimization.methods.by-workstation.disable-range-in-blocks", 4.0,
"How close in blocks a villager needs to be to get optimized by its workstation"); "How close in blocks a villager needs to be to get optimized by its workstation");
this.getList("optimization.methods.by-workstation.workstation-materials", List.of( this.getList("optimization.methods.by-workstation.workstation-materials", List.of(
"COMPOSTER", "SMOKER", "BARREL", "LOOM", "BLAST_FURNACE", "BREWING_STAND", "CAULDRON", "COMPOSTER", "SMOKER", "BARREL", "LOOM", "BLAST_FURNACE", "BREWING_STAND", "CAULDRON",
@ -90,9 +90,8 @@ public class Config {
private ConfigFile loadConfig(File ymlFile) throws Exception { private ConfigFile loadConfig(File ymlFile) throws Exception {
File parent = new File(ymlFile.getParent()); File parent = new File(ymlFile.getParent());
if (!parent.exists()) if (!parent.exists() && !parent.mkdir())
if (!parent.mkdir()) VillagerOptimizer.getLog().severe("Unable to create plugin config directory.");
VillagerOptimizer.getLog().severe("Unable to create plugin config directory.");
if (!ymlFile.exists()) if (!ymlFile.exists())
ymlFile.createNewFile(); // Result can be ignored because this method only returns false if the file already exists ymlFile.createNewFile(); // Result can be ignored because this method only returns false if the file already exists
return ConfigFile.loadConfig(ymlFile); return ConfigFile.loadConfig(ymlFile);

View File

@ -0,0 +1,67 @@
package me.xginko.villageroptimizer.modules;
import io.papermc.paper.event.entity.EntityMoveEvent;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.config.Config;
import me.xginko.villageroptimizer.enums.OptimizationType;
import me.xginko.villageroptimizer.models.VillagerCache;
import me.xginko.villageroptimizer.models.WrappedVillager;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
public class BlockOptimization implements VillagerOptimizerModule, Listener {
private final VillagerCache cache;
private final Config config;
private final boolean shouldLog;
protected BlockOptimization() {
this.cache = VillagerOptimizer.getVillagerCache();
this.config = VillagerOptimizer.getConfiguration();
this.shouldLog = config.getBoolean("optimization.methods.by-specific-block.log", false);
}
@Override
public void enable() {
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@Override
public void disable() {
HandlerList.unregisterAll(this);
}
@Override
public boolean shouldEnable() {
return config.enable_block_optimization;
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onEntityMove(EntityMoveEvent event) {
if (!event.getEntityType().equals(EntityType.VILLAGER)) return;
final Location entityLegs = event.getEntity().getLocation();
if (
config.blocks_that_disable.contains(entityLegs.getBlock().getType())
|| config.blocks_that_disable.contains(entityLegs.clone().subtract(0,1,0).getBlock().getType())
) {
WrappedVillager wVillager = cache.get((Villager) event.getEntity());
if (!wVillager.isOptimized()) {
wVillager.setOptimization(OptimizationType.BLOCK);
if (shouldLog) VillagerOptimizer.getLog().info("Villager moved onto an optimization block at "+wVillager.villager().getLocation());
}
} else {
WrappedVillager wVillager = cache.get((Villager) event.getEntity());
if (wVillager.isOptimized()) {
wVillager.setOptimization(OptimizationType.OFF);
if (shouldLog) VillagerOptimizer.getLog().info("Villager moved away from an optimization block at "+wVillager.villager().getLocation());
}
}
}
}

View File

@ -0,0 +1,67 @@
package me.xginko.villageroptimizer.modules;
import io.papermc.paper.event.player.PlayerNameEntityEvent;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.config.Config;
import me.xginko.villageroptimizer.enums.OptimizationType;
import me.xginko.villageroptimizer.models.VillagerCache;
import me.xginko.villageroptimizer.models.WrappedVillager;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
public class NametagOptimization implements VillagerOptimizerModule, Listener {
private final VillagerCache cache;
private final Config config;
private final boolean shouldLog;
protected NametagOptimization() {
this.cache = VillagerOptimizer.getVillagerCache();
this.config = VillagerOptimizer.getConfiguration();
this.shouldLog = config.getBoolean("optimization.methods.by-nametag.log", false);
}
@Override
public void enable() {
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@Override
public void disable() {
HandlerList.unregisterAll(this);
}
@Override
public boolean shouldEnable() {
return config.enable_nametag_optimization;
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onNametag(PlayerNameEntityEvent event) {
if (!event.getEntity().getType().equals(EntityType.VILLAGER)) return;
Component name = event.getName();
if (name == null) return;
final String nameTag = PlainTextComponentSerializer.plainText().serialize(name);
WrappedVillager wVillager = cache.get((Villager) event.getEntity());
if (config.nametags.contains(nameTag.toLowerCase())) {
if (!wVillager.isOptimized()) {
wVillager.setOptimization(OptimizationType.NAMETAG);
if (shouldLog) VillagerOptimizer.getLog().info(event.getPlayer().getName() + " optimized a villager using nametag: '" + nameTag + "'");
}
} else {
if (wVillager.isOptimized()) {
wVillager.setOptimization(OptimizationType.OFF);
if (shouldLog) VillagerOptimizer.getLog().info(event.getPlayer().getName() + " disabled optimizations for a villager using nametag: '" + nameTag + "'");
}
}
}
}

View File

@ -16,6 +16,11 @@ public interface VillagerOptimizerModule {
modules.add(new AntiVillagerDamage()); modules.add(new AntiVillagerDamage());
modules.add(new AntiVillagerTargetting()); modules.add(new AntiVillagerTargetting());
modules.add(new NametagOptimization());
modules.add(new BlockOptimization());
modules.add(new WorkstationOptimization());
modules.add(new ChunkLimit()); modules.add(new ChunkLimit());
for (VillagerOptimizerModule module : modules) { for (VillagerOptimizerModule module : modules) {

View File

@ -0,0 +1,43 @@
package me.xginko.villageroptimizer.modules;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.config.Config;
import me.xginko.villageroptimizer.models.VillagerCache;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
public class WorkstationOptimization implements VillagerOptimizerModule, Listener {
private final VillagerCache cache;
private final Config config;
private final boolean shouldLog;
protected WorkstationOptimization() {
this.cache = VillagerOptimizer.getVillagerCache();
this.config = VillagerOptimizer.getConfiguration();
this.shouldLog = config.getBoolean("optimization.methods.by-workstation.log", false);
}
@Override
public void enable() {
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@Override
public void disable() {
HandlerList.unregisterAll(this);
}
@Override
public boolean shouldEnable() {
return config.enable_workstation_optimization;
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onEvent() {
}
}