Files
src
build.gradle
settings.gradle
Code
9 639
src/main/java/com/kodari/saferTP/RtpPlugin.java
package com.kodari.saferTP;
import com.cryptomorin.xseries.XSound;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
public final class RtpPlugin extends JavaPlugin implements Listener {
private RtpService rtpService;
@Override
public void onEnable() {
saveDefaultConfig();
rtpService = new RtpService(this);
Objects.requireNonNull(getCommand("rtp"), "The rtp command is missing from plugin.yml")
.setExecutor(new RtpCommand(this));
Bukkit.getPluginManager().registerEvents(this, this);
}
@Override
public void onDisable() {
if (rtpService != null) {
rtpService.clear();
}
}
public RtpService getRtpService() {
return rtpService;
}
public void sendMessage(Player player, String message) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
}
public void sendConfigured(Player player, String path, String fallback) {
sendMessage(player, getConfig().getString(path, fallback));
}
public void playSound(Player player, String path) {
String sound = getConfig().getString(path, "");
if (sound != null && !sound.isBlank()) {
XSound.matchXSound(sound).ifPresent(xSound -> xSound.play(player));
}
}
public void playRandomTeleportSound(Player player, RtpDimension dimension) {
List<String> sounds = getConfig().getStringList("sounds.teleport." + dimension.configKey());
if (sounds.isEmpty()) {
sounds = switch (dimension) {
case OVERWORLD -> List.of("ENTITY_PLAYER_LEVELUP", "BLOCK_AMETHYST_BLOCK_CHIME",
"ENTITY_ENDERMAN_TELEPORT", "ENTITY_EXPERIENCE_ORB_PICKUP");
case NETHER -> List.of("BLOCK_PORTAL_TRAVEL", "BLOCK_FIRE_EXTINGUISH",
"ENTITY_BLAZE_SHOOT", "ENTITY_WITHER_SPAWN");
};
}
String sound = sounds.get(ThreadLocalRandom.current().nextInt(sounds.size()));
XSound.matchXSound(sound).ifPresent(xSound -> xSound.play(player));
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (event.getView().getTopInventory().getHolder() instanceof RtpMenu menu) {
menu.handleClick(event);
}
}
@EventHandler
public void onInventoryDrag(InventoryDragEvent event) {
if (event.getView().getTopInventory().getHolder() instanceof RtpMenu) {
int topSize = event.getView().getTopInventory().getSize();
if (event.getRawSlots().stream().anyMatch(slot -> slot < topSize)) {
event.setCancelled(true);
}
}
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
rtpService.onQuit(event.getPlayer().getUniqueId());
}
}
