Files
src
build.gradle
settings.gradle
Code
8 229
src/main/java/com/kodari/totem3d/Totem3DPlugin.java
package com.kodari.totem3d;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityResurrectEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Locale;
public final class Totem3DPlugin extends JavaPlugin implements Listener, CommandExecutor {
private Sound totemSound;
@Override
public void onEnable() {
saveDefaultConfig();
loadSound();
getServer().getPluginManager().registerEvents(this, this);
if (getCommand("totem3d") != null) {
getCommand("totem3d").setExecutor(this);
}
}
private void loadSound() {
totemSound = null;
if (!getConfig().getBoolean("sound.enabled", true)) {
return;
}
String configuredKey = getConfig().getString("sound.key", "minecraft:item.totem.use");
NamespacedKey key = NamespacedKey.fromString(configuredKey);
if (key == null && !configuredKey.contains(":")) {
key = NamespacedKey.minecraft(configuredKey.toLowerCase(Locale.ROOT));
}
if (key != null) {
totemSound = Registry.SOUNDS.get(key);
}
if (totemSound == null) {
getLogger().warning("Unknown Totem sound key: " + configuredKey);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onTotemUse(EntityResurrectEvent event) {
if (totemSound == null) {
return;
}
Entity entity = event.getEntity();
if (!(entity instanceof Player player)) {
return;
}
float volume = (float) getConfig().getDouble("sound.volume", 1.0D);
float pitch = (float) getConfig().getDouble("sound.pitch", 1.0D);
player.playSound(player.getLocation(), totemSound, volume, pitch);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length != 1 || !args[0].equalsIgnoreCase("reload")) {
sender.sendMessage("§eDùng: /totem3d reload");
return true;
}
if (!sender.hasPermission("totem3d.reload")) {
sender.sendMessage("§cBạn không có quyền dùng lệnh này.");
return true;
}
reloadConfig();
loadSound();
sender.sendMessage("§aĐã reload cấu hình Totem3D.");
return true;
}
}