Files
src
build.gradle
settings.gradle
Code
5 378
src/main/java/com/kodari/snowmilk/SnowMilkPlugin.java
package com.kodari.snowmilk;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.lang.reflect.Field;
import java.util.Locale;
import java.util.concurrent.ThreadLocalRandom;
public final class SnowMilkPlugin extends JavaPlugin implements Listener {
private NamespacedKey specialMilkKey;
private XParticle snowParticle;
private Command registeredCommand;
private String commandName;
@Override
public void onEnable() {
saveDefaultConfig();
specialMilkKey = new NamespacedKey(this, "special_milk_bucket");
loadSettings();
getServer().getPluginManager().registerEvents(this, this);
if (!registerConfiguredCommand()) {
getLogger().severe("No se pudo registrar el comando configurado.");
}
}
@Override
public void onDisable() {
if (registeredCommand != null) {
registeredCommand.unregister(getCommandMap());
}
syncCommands();
}
private boolean registerConfiguredCommand() {
String configuredName = getConfig().getString("command", "test");
if (configuredName == null) {
return false;
}
configuredName = configuredName.trim();
while (configuredName.startsWith("/")) {
configuredName = configuredName.substring(1);
}
if (!configuredName.matches("[a-zA-Z0-9_-]+")) {
return false;
}
String newCommandName = configuredName.toLowerCase(Locale.ROOT);
CommandMap commandMap;
try {
commandMap = getCommandMap();
} catch (IllegalStateException exception) {
getLogger().severe("No se pudo acceder al registro de comandos: " + exception.getMessage());
return false;
}
Command newCommand = new Command(newCommandName) {
@Override
public boolean execute(CommandSender sender, String label, String[] args) {
return executeConfiguredCommand(sender, label, args);
}
};
newCommand.setDescription("Comando de efectos de nieve y leche especial.");
newCommand.setUsage("/" + newCommandName + " <jugador> | /" + newCommandName + " reload");
if (registeredCommand != null) {
registeredCommand.unregister(commandMap);
}
if (!commandMap.register(getName().toLowerCase(Locale.ROOT), newCommand)) {
newCommand.unregister(commandMap);
registeredCommand = null;
return false;
}
registeredCommand = newCommand;
commandName = newCommandName;
syncCommands();
return true;
}
private boolean executeConfiguredCommand(CommandSender commandSender, String label, String[] args) {
if (args.length > 0 && args[0].equalsIgnoreCase("reload")) {
reloadConfig();
loadSettings();
if (registerConfiguredCommand()) {
commandSender.sendMessage(message("messages.reload-success", "%command%", commandName));
} else {
commandSender.sendMessage(message("messages.reload-failed"));
}
return true;
}
if (!(commandSender instanceof Player sender)) {
commandSender.sendMessage(message("messages.players-only"));
return true;
}
if (args.length < 1) {
sender.sendMessage(message("messages.usage", "%command%", label));
return true;
}
Player target = Bukkit.getPlayerExact(args[0]);
if (target == null) {
sender.sendMessage(message("messages.player-not-found", "%player%", args[0]));
return true;
}
double maxDistance = getConfig().getDouble("max-distance", 5.0);
if (sender.getWorld() != target.getWorld()
|| sender.getLocation().distanceSquared(target.getLocation()) > maxDistance * maxDistance) {
sender.sendMessage(message("messages.too-far", "%distance%", String.valueOf(maxDistance)));
return true;
}
playCommandSounds(target);
startSnowEffect(sender, target);
if (giveSpecialMilk(sender, target)) {
sender.sendMessage(message("messages.milk-given", "%target%", target.getName()));
} else {
sender.sendMessage(message("messages.snow-only", "%target%", target.getName()));
}
return true;
}
private String message(String path, String... replacements) {
String text = getConfig().getString(path, "");
for (int i = 0; i + 1 < replacements.length; i += 2) {
text = text.replace(replacements[i], replacements[i + 1]);
}
return ChatColor.translateAlternateColorCodes('&', text);
}
private void loadSettings() {
snowParticle = XParticle.of(getConfig().getString("snow.particle", "SNOWFLAKE"))
.or(() -> XParticle.of("SNOW_SHOVEL"))
.or(() -> XParticle.of("CLOUD"))
.orElse(null);
}
private CommandMap getCommandMap() {
try {
Field field = getServer().getClass().getDeclaredField("commandMap");
field.setAccessible(true);
return (CommandMap) field.get(getServer());
} catch (ReflectiveOperationException exception) {
throw new IllegalStateException(exception);
}
}
private void syncCommands() {
try {
getServer().getClass().getMethod("syncCommands").invoke(getServer());
} catch (ReflectiveOperationException ignored) {
}
}
private void playCommandSounds(Player target) {
XSound.matchXSound(getConfig().getString("sounds.milk", "ENTITY_COW_MILK"))
.ifPresent(sound -> sound.play(target));
int soundCount = Math.max(0, getConfig().getInt("sounds.slime-step-count", 3));
long interval = Math.max(1L, getConfig().getLong("sounds.slime-step-interval-ticks", 5L));
if (soundCount == 0) {
return;
}
new BukkitRunnable() {
private int played;
@Override
public void run() {
if (played >= soundCount || !target.isOnline()) {
cancel();
return;
}
XSound.matchXSound(getConfig().getString("sounds.slime-step", "BLOCK_SLIME_BLOCK_STEP"))
.ifPresent(sound -> sound.play(target));
played++;
}
}.runTaskTimer(this, 0L, interval);
}