improve block and workstation optimization

This commit is contained in:
xGinko 2023-09-27 11:04:58 +02:00
parent 5f0f2aa460
commit d0dc4a3346
6 changed files with 50 additions and 65 deletions

View File

@ -35,7 +35,7 @@ public final class VillagerOptimizer extends JavaPlugin {
private static Config config;
private static Logger logger;
private final static Style plugin_style = Style.style(TextColor.color(102,255,230), TextDecoration.BOLD);
public final static Style plugin_style = Style.style(TextColor.color(102,255,230), TextDecoration.BOLD);
@Override
public void onEnable() {
@ -59,9 +59,9 @@ public final class VillagerOptimizer extends JavaPlugin {
console.sendMessage(Component.text("").style(plugin_style).append(Component.text("https://github.com/xGinko/VillagerOptimizer").color(NamedTextColor.GRAY)).append(Component.text("").style(plugin_style)));
console.sendMessage(Component.text("│ │").style(plugin_style));
console.sendMessage(Component.text("│ │").style(plugin_style));
console.sendMessage(Component.text("").style(plugin_style).append(Component.text(" ➤ Loading Translations...").style(plugin_style)).append(Component.text("")).decorate(TextDecoration.BOLD));
console.sendMessage(Component.text("").style(plugin_style).append(Component.text(" ➤ Loading Translations...").style(plugin_style)).append(Component.text("").style(plugin_style)));
reloadLang(true);
console.sendMessage(Component.text("").style(plugin_style).append(Component.text(" ➤ Loading Config...").style(plugin_style)).append(Component.text("")).decorate(TextDecoration.BOLD));
console.sendMessage(Component.text("").style(plugin_style).append(Component.text(" ➤ Loading Config...").style(plugin_style)).append(Component.text("").style(plugin_style)));
reloadConfiguration();
console.sendMessage(Component.text("").style(plugin_style).append(Component.text(" ➤ Registering Commands...").style(plugin_style)).append(Component.text("").style(plugin_style)));
VillagerOptimizerCommand.reloadCommands();

View File

@ -1,5 +1,6 @@
package me.xginko.villageroptimizer.commands.villageroptimizer;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.commands.SubCommand;
import me.xginko.villageroptimizer.commands.VillagerOptimizerCommand;
import me.xginko.villageroptimizer.commands.villageroptimizer.subcommands.ReloadSubCmd;
@ -58,17 +59,17 @@ public class VillagerOptimizerCmd implements TabCompleter, VillagerOptimizerComm
private void sendCommandOverview(CommandSender sender) {
if (!sender.hasPermission(Permissions.Commands.RELOAD.get()) && !sender.hasPermission(Permissions.Commands.VERSION.get())) return;
sender.sendMessage(Component.text("-----------------------------------------------------").color(NamedTextColor.GRAY));
sender.sendMessage(Component.text("VillagerOptimizer Commands").color(NamedTextColor.BLUE));
sender.sendMessage(Component.text("VillagerOptimizer Commands").color(VillagerOptimizer.plugin_style.color()));
sender.sendMessage(Component.text("-----------------------------------------------------").color(NamedTextColor.GRAY));
subCommands.forEach(subCommand -> sender.sendMessage(
subCommand.getSyntax().append(Component.text(" - ").color(NamedTextColor.DARK_GRAY)).append(subCommand.getDescription())));
sender.sendMessage(
Component.text("/optimizevillagers <blockradius>").color(NamedTextColor.BLUE)
Component.text("/optimizevillagers <blockradius>").color(VillagerOptimizer.plugin_style.color())
.append(Component.text(" - ").color(NamedTextColor.DARK_GRAY))
.append(Component.text("Optimize villagers in a radius").color(NamedTextColor.GRAY))
);
sender.sendMessage(
Component.text("/unoptmizevillagers <blockradius>").color(NamedTextColor.BLUE)
Component.text("/unoptmizevillagers <blockradius>").color(VillagerOptimizer.plugin_style.color())
.append(Component.text(" - ").color(NamedTextColor.DARK_GRAY))
.append(Component.text("Unoptimize villagers in a radius").color(NamedTextColor.GRAY))
);

View File

@ -9,27 +9,30 @@ import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.CommandSender;
public class ReloadSubCmd extends SubCommand {
@Override
public String getLabel() {
return "reload";
}
@Override
public TextComponent getDescription() {
return Component.text("Reload the plugin configuration.").color(NamedTextColor.GRAY);
}
@Override
public TextComponent getSyntax() {
return Component.text("/villageroptimizer reload").color(NamedTextColor.BLUE);
return Component.text("/villageroptimizer reload").color(VillagerOptimizer.plugin_style.color());
}
@Override
public void perform(CommandSender sender, String[] args) {
if (sender.hasPermission(Permissions.Commands.RELOAD.get())) {
sender.sendMessage(Component.text("Reloading VillagerOptimizer...").color(NamedTextColor.BLUE));
sender.sendMessage(Component.text("Reloading VillagerOptimizer...").color(NamedTextColor.WHITE));
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
plugin.getServer().getAsyncScheduler().runNow(plugin, reloadPlugin -> {
plugin.reloadPlugin();
sender.sendMessage(Component.text("Reload complete.").color(NamedTextColor.AQUA));
sender.sendMessage(Component.text("Reload complete.").color(NamedTextColor.GREEN));
});
} else {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);

View File

@ -8,21 +8,23 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.command.CommandSender;
public class VersionSubCmd extends SubCommand {
@Override
public String getLabel() {
return "version";
}
@Override
public TextComponent getDescription() {
return Component.text("Show the plugin version.").color(NamedTextColor.GRAY);
}
@Override
public TextComponent getSyntax() {
return Component.text("/villageroptimizer version").color(NamedTextColor.BLUE);
return Component.text("/villageroptimizer version").color(VillagerOptimizer.plugin_style.color());
}
@Override
@ -31,14 +33,17 @@ public class VersionSubCmd extends SubCommand {
final PluginMeta pluginMeta = VillagerOptimizer.getInstance().getPluginMeta();
sender.sendMessage(
Component.newline()
.append(Component.text(pluginMeta.getName()+" "+pluginMeta.getVersion())
.color(NamedTextColor.BLUE).decorate(TextDecoration.BOLD)
.clickEvent(ClickEvent.openUrl(pluginMeta.getWebsite())))
.append(Component.text(" by ")
.color(NamedTextColor.GRAY))
.append(Component.text(pluginMeta.getAuthors().get(0))
.append(
Component.text(pluginMeta.getName()+" "+pluginMeta.getVersion())
.style(VillagerOptimizer.plugin_style)
.clickEvent(ClickEvent.openUrl(pluginMeta.getWebsite()))
)
.append(Component.text(" by ").color(NamedTextColor.GRAY))
.append(
Component.text(pluginMeta.getAuthors().get(0))
.color(NamedTextColor.WHITE)
.clickEvent(ClickEvent.openUrl("https://github.com/xGinko")))
.clickEvent(ClickEvent.openUrl("https://github.com/xGinko"))
)
.append(Component.newline())
);
} else {

View File

@ -40,9 +40,8 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
this.villagerCache = VillagerOptimizer.getCache();
Config config = VillagerOptimizer.getConfiguration();
config.addComment("optimization-methods.block-optimization.enable", """
When enabled, villagers standing on the configured specific blocks will become optimized once a\s
player interacts with them. If the block is broken or moved, the villager will become unoptimized\s
again once a player interacts with the villager afterwards.""");
When enabled, the closest villager standing near a configured block being placed will be optimized.\s
If a configured block is broken nearby, the closest villager will become unoptimized again.""");
config.getList("optimization-methods.block-optimization.materials", List.of(
"LAPIS_BLOCK", "GLOWSTONE", "IRON_BLOCK"
), "Values here need to be valid bukkit Material enums for your server version."
@ -116,10 +115,10 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
closestOptimizableVillager.setOptimization(OptimizationType.BLOCK);
closestOptimizableVillager.saveOptimizeTime();
if (shouldNotifyPlayer) {
final String villagerType = closestOptimizableVillager.villager().getProfession().toString().toLowerCase();
final String vilProfession = closestOptimizableVillager.villager().getProfession().toString().toLowerCase();
final String placedType = placed.getType().toString().toLowerCase();
VillagerOptimizer.getLang(player.locale()).block_optimize_success.forEach(line -> player.sendMessage(line
.replaceText(TextReplacementConfig.builder().matchLiteral("%vil_profession%").replacement(villagerType).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%vil_profession%").replacement(vilProfession).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%blocktype%").replacement(placedType).build())
));
}
@ -166,10 +165,10 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
closestOptimizedVillager.setOptimization(OptimizationType.NONE);
if (shouldNotifyPlayer) {
final String villagerType = closestOptimizedVillager.villager().getProfession().toString().toLowerCase();
final String vilProfession = closestOptimizedVillager.villager().getProfession().toString().toLowerCase();
final String brokenType = broken.getType().toString().toLowerCase();
VillagerOptimizer.getLang(player.locale()).block_unoptimize_success.forEach(line -> player.sendMessage(line
.replaceText(TextReplacementConfig.builder().matchLiteral("%vil_profession%").replacement(villagerType).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%vil_profession%").replacement(vilProfession).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%blocktype%").replacement(brokenType).build())
));
}

View File

@ -8,7 +8,6 @@ import me.xginko.villageroptimizer.enums.OptimizationType;
import me.xginko.villageroptimizer.enums.Permissions;
import me.xginko.villageroptimizer.modules.VillagerOptimizerModule;
import me.xginko.villageroptimizer.utils.CommonUtil;
import me.xginko.villageroptimizer.utils.LogUtil;
import net.kyori.adventure.text.TextReplacementConfig;
import org.bukkit.Location;
import org.bukkit.Material;
@ -24,36 +23,20 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import java.util.HashSet;
import java.util.List;
public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener {
private final VillagerCache villagerCache;
private final HashSet<Material> workstations_that_disable = new HashSet<>(14);
private final boolean shouldLog, shouldNotifyPlayer;
private final long cooldown;
private final double search_radius;
private final boolean shouldLog, shouldNotifyPlayer;
public OptimizeByWorkstation() {
shouldEnable();
this.villagerCache = VillagerOptimizer.getCache();
Config config = VillagerOptimizer.getConfiguration();
config.addComment("optimization-methods.workstation-optimization.enable", """
When enabled, villagers near a configured radius to a workstation specific to your config\s
will be optimized.""");
config.getList("optimization-methods.workstation-optimization.workstation-materials", List.of(
"COMPOSTER", "SMOKER", "BARREL", "LOOM", "BLAST_FURNACE", "BREWING_STAND", "CAULDRON",
"FLETCHING_TABLE", "CARTOGRAPHY_TABLE", "LECTERN", "SMITHING_TABLE", "STONECUTTER", "GRINDSTONE"
), "Values here need to be valid bukkit Material enums for your server version."
).forEach(configuredMaterial -> {
try {
Material disableBlock = Material.valueOf(configuredMaterial);
this.workstations_that_disable.add(disableBlock);
} catch (IllegalArgumentException e) {
LogUtil.materialNotRecognized("workstation-optimization", configuredMaterial);
}
});
When enabled, the closest villager near a matching workstation being placed will be optimized.\s
If a nearby matching workstation is broken, the villager will become unoptimized again.""");
this.search_radius = config.getDouble("optimization-methods.workstation-optimization.search-radius-in-blocks", 2.0, """
The radius in blocks a villager can be away from the player when he places a workstation.\s
The closest unoptimized villager to the player will be optimized.""") / 2;
@ -84,18 +67,15 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockPlace(BlockPlaceEvent event) {
Block placed = event.getBlock();
final Material placedType = placed.getType();
if (!workstations_that_disable.contains(placedType)) return;
Villager.Profession workstationProfession = getWorkstationProfession(placed.getType());
if (workstationProfession.equals(Villager.Profession.NONE)) return;
Player player = event.getPlayer();
if (!player.hasPermission(Permissions.Optimize.WORKSTATION.get())) return;
Villager.Profession workstationProfession = getWorkstationProfession(placedType);
if (workstationProfession.equals(Villager.Profession.NONE)) return;
final Location workstationLoc = placed.getLocation();
WrappedVillager closestOptimizableVillager = null;
double closestDistance = Double.MAX_VALUE;
for (Entity entity : workstationLoc.getNearbyEntities(search_radius, search_radius, search_radius)) {
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
Villager villager = (Villager) entity;
@ -119,10 +99,10 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
closestOptimizableVillager.setOptimization(OptimizationType.WORKSTATION);
closestOptimizableVillager.saveOptimizeTime();
if (shouldNotifyPlayer) {
final String villagerType = closestOptimizableVillager.villager().getProfession().toString().toLowerCase();
final String vilProfession = closestOptimizableVillager.villager().getProfession().toString().toLowerCase();
final String workstation = placed.getType().toString().toLowerCase();
VillagerOptimizer.getLang(player.locale()).workstation_optimize_success.forEach(line -> player.sendMessage(line
.replaceText(TextReplacementConfig.builder().matchLiteral("%vil_profession%").replacement(villagerType).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%vil_profession%").replacement(vilProfession).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%workstation%").replacement(workstation).build())
));
}
@ -141,15 +121,13 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockBreak(BlockBreakEvent event) {
Block placed = event.getBlock();
final Material brokenType = placed.getType();
if (!workstations_that_disable.contains(brokenType)) return;
Block broken = event.getBlock();
Villager.Profession workstationProfession = getWorkstationProfession(broken.getType());
if (workstationProfession.equals(Villager.Profession.NONE)) return;
Player player = event.getPlayer();
if (!player.hasPermission(Permissions.Optimize.WORKSTATION.get())) return;
Villager.Profession workstationProfession = getWorkstationProfession(brokenType);
if (workstationProfession.equals(Villager.Profession.NONE)) return;
final Location workstationLoc = placed.getLocation();
final Location workstationLoc = broken.getLocation();
WrappedVillager closestOptimizedVillager = null;
double closestDistance = Double.MAX_VALUE;
@ -164,7 +142,6 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
if (distance < closestDistance) {
final OptimizationType type = wVillager.getOptimizationType();
if (type.equals(OptimizationType.WORKSTATION) || type.equals(OptimizationType.COMMAND)) {
closestOptimizedVillager = wVillager;
closestDistance = distance;
}
@ -175,18 +152,18 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
closestOptimizedVillager.setOptimization(OptimizationType.NONE);
if (shouldNotifyPlayer) {
final String villagerType = closestOptimizedVillager.villager().getProfession().toString().toLowerCase();
final String workstation = placed.getType().toString().toLowerCase();
final String vilProfession = closestOptimizedVillager.villager().getProfession().toString().toLowerCase();
final String workstation = broken.getType().toString().toLowerCase();
VillagerOptimizer.getLang(player.locale()).workstation_unoptimize_success.forEach(line -> player.sendMessage(line
.replaceText(TextReplacementConfig.builder().matchLiteral("%vil_profession%").replacement(villagerType).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%vil_profession%").replacement(vilProfession).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%workstation%").replacement(workstation).build())
));
}
if (shouldLog)
VillagerOptimizer.getLog().info(player.getName() + " unoptimized a villager by breaking workstation: '" + placed.getType().toString().toLowerCase() + "'");
VillagerOptimizer.getLog().info(player.getName() + " unoptimized a villager by breaking workstation: '" + broken.getType().toString().toLowerCase() + "'");
}
private Villager.Profession getWorkstationProfession(Material workstation) {
private Villager.Profession getWorkstationProfession(final Material workstation) {
return switch (workstation) {
case BARREL -> Villager.Profession.FISHERMAN;
case CARTOGRAPHY_TABLE -> Villager.Profession.CARTOGRAPHER;
@ -204,4 +181,4 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
default -> Villager.Profession.NONE;
};
}
}
}