Files
src
pom.xml
README.txt
Code
11 1,304
src/main/java/com/rtpcinematic/RTPPlugin.java
package com.rtpcinematic;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityToggleGlideEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffectType;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class RTPPlugin extends JavaPlugin implements Listener {
private record Pending(GameMode mode, Location loc) {}
private final MiniMessage mm = MiniMessage.miniMessage();
final Map<UUID, RTPSession> sessions = new HashMap<>();
private final Set<UUID> searching = new HashSet<>();
private final Map<UUID, Long> cooldowns = new HashMap<>();
private final Map<UUID, Long> protectedUntil = new HashMap<>();
private final Map<UUID, Pending> pending = new HashMap<>();
private ZoneManager zones;
private File dataFile;
@Override
public void onEnable() {
saveDefaultConfig();
if (getConfig().getInt("config-version", 0) < 2) { // config lama terlalu berat -> cadangkan & buat ulang
File cf = new File(getDataFolder(), "config.yml");
File bak = new File(getDataFolder(), "config_old.yml");
if (bak.exists()) bak.delete();
cf.renameTo(bak);
saveResource("config.yml", true);
reloadConfig();
getLogger().info("config.yml lama dicadangkan ke config_old.yml dan dibuat ulang (config-version 2).");
}
zones = new ZoneManager(this);
zones.load();
dataFile = new File(getDataFolder(), "data.yml");
loadData();
getServer().getPluginManager().registerEvents(this, this);
RTPCommand cmd = new RTPCommand(this);
getCommand("rtp").setExecutor(cmd);
getCommand("rtp").setTabCompleter(cmd);
}
@Override
public void onDisable() {
for (RTPSession s : new ArrayList<>(sessions.values())) s.quit();
saveData();
}
public void reload() { reloadConfig(); zones.load(); }
public ZoneManager zones() { return zones; }
// ───────── teks ─────────
public Component mm(String s) { return mm.deserialize(s).decoration(TextDecoration.ITALIC, false); }
private String fill(String key, String... kv) {
String s = getConfig().getString("messages." + key, key);
for (int i = 0; i + 1 < kv.length; i += 2) s = s.replace("{" + kv[i] + "}", kv[i + 1]);
return s;
}
public Component comp(String key, String... kv) { return mm.deserialize(fill(key, kv)); }
public Component msg(String key, String... kv) { return mm.deserialize(fill("prefix") + fill(key, kv)); }
public String plainName(Zone z) { return PlainTextComponentSerializer.plainText().serialize(mm.deserialize(z.displayName)); }
public static String formatTime(long sec) { return sec >= 60 ? (sec / 60) + "m " + (sec % 60) + "s" : sec + "s"; }
/** spec: "ia:namespace:id" (ItemsAdder) atau nama Material (+ custom-model-data). */
public ItemStack resolveItem(String spec, int cmd, Material fallback) {
if (spec != null && spec.toLowerCase().startsWith("ia:")) {
if (getServer().getPluginManager().isPluginEnabled("ItemsAdder")) {
try {
Class<?> c = Class.forName("dev.lone.itemsadder.api.CustomStack");
Object cs = c.getMethod("getInstance", String.class).invoke(null, spec.substring(3));
if (cs != null) return (ItemStack) c.getMethod("getItemStack").invoke(cs);
getLogger().warning("Item ItemsAdder tidak ditemukan: " + spec.substring(3) + " (sudah /iazip?)");
} catch (Throwable t) { getLogger().warning("Gagal memuat item ItemsAdder: " + t); }
}
return new ItemStack(fallback);
}
Material m = spec == null ? null : Material.matchMaterial(spec);
ItemStack it = new ItemStack(m == null ? fallback : m);
if (cmd > 0) { ItemMeta meta = it.getItemMeta(); meta.setCustomModelData(cmd); it.setItemMeta(meta); }
return it;
}
// ───────── cooldown ─────────
public long cooldownRemaining(Player p) {
if (p.hasPermission("rtp.bypass")) return 0;
Long u = cooldowns.get(p.getUniqueId());
return u == null ? 0 : Math.max(0, (u - System.currentTimeMillis() + 999) / 1000);
}
void setCooldown(Player p) {
if (p.hasPermission("rtp.bypass")) return;
cooldowns.put(p.getUniqueId(), System.currentTimeMillis() + getConfig().getLong("cooldown-seconds", 300) * 1000);
saveData();
}
void refundCooldown(Player p) { if (cooldowns.remove(p.getUniqueId()) != null) saveData(); }
// ───────── data (cooldown + pemulihan sesi yang terputus) ─────────
void markPending(Player p, GameMode mode, Location loc) { pending.put(p.getUniqueId(), new Pending(mode, loc)); saveData(); }
void clearPending(Player p) { if (pending.remove(p.getUniqueId()) != null) saveData(); }
private void loadData() {
if (!dataFile.exists()) return;
YamlConfiguration y = YamlConfiguration.loadConfiguration(dataFile);
long now = System.currentTimeMillis();
ConfigurationSection s = y.getConfigurationSection("cooldowns");
if (s != null) for (String k : s.getKeys(false)) {
long u = s.getLong(k);
if (u > now) try { cooldowns.put(UUID.fromString(k), u); } catch (IllegalArgumentException ignored) {}
}
ConfigurationSection ps = y.getConfigurationSection("pending");
if (ps != null) for (String k : ps.getKeys(false)) {
try {
GameMode gm = GameMode.valueOf(ps.getString(k + ".mode", "SURVIVAL"));
pending.put(UUID.fromString(k), new Pending(gm, ps.getLocation(k + ".location")));
} catch (IllegalArgumentException ignored) {}
}
}
private void saveData() {
if (dataFile == null) return;
YamlConfiguration y = new YamlConfiguration();
cooldowns.forEach((k, v) -> y.set("cooldowns." + k, v));
pending.forEach((k, v) -> { y.set("pending." + k + ".mode", v.mode().name()); y.set("pending." + k + ".location", v.loc()); });
try { getDataFolder().mkdirs(); y.save(dataFile); } catch (IOException e) { getLogger().warning(e.getMessage()); }
}
// ───────── proteksi damage ─────────
public void protect(Player p, int seconds) {
if (seconds <= 0) return;
long until = System.currentTimeMillis() + seconds * 1000L;
protectedUntil.put(p.getUniqueId(), until);
p.sendMessage(msg("protection-on", "seconds", String.valueOf(seconds)));
Bukkit.getScheduler().runTaskLater(this, () -> {
Long cur = protectedUntil.get(p.getUniqueId());
if (cur != null && cur == until) {
protectedUntil.remove(p.getUniqueId());
if (p.isOnline()) p.sendMessage(msg("protection-off"));
}
}, seconds * 20L + 2);
}
private boolean isProtected(Player p) {
Long u = protectedUntil.get(p.getUniqueId());
return u != null && u > System.currentTimeMillis();
}
private boolean busy(Player p) { return sessions.containsKey(p.getUniqueId()); }
// ───────── mulai RTP ─────────
public void startRTP(Player p, Zone z) { startRTP(p, z, false); }
/** force = abaikan cooldown & tidak memasang cooldown (untuk /rtp admin test). */
public void startRTP(Player p, Zone z, boolean force) {
UUID id = p.getUniqueId();
if (busy(p) || searching.contains(id)) { p.sendMessage(msg("busy")); return; }
if (!force && z.permission != null && !p.hasPermission(z.permission)) { p.sendMessage(msg("no-permission")); return; }
long rem = force ? 0 : cooldownRemaining(p);
if (rem > 0) { p.sendMessage(msg("cooldown", "time", formatTime(rem))); return; }
World w = Bukkit.getWorld(z.world);
if (w == null) { p.sendMessage(msg("no-world")); return; }
searching.add(id);
p.sendMessage(msg("searching"));
SafeLocator.find(this, z, loc -> {
searching.remove(id);
if (!p.isOnline() || busy(p)) return;
if (loc == null) { p.sendMessage(msg("no-location")); return; }
if (!force) setCooldown(p);
boolean cinematic = getConfig().getBoolean("cinematic.enabled", true)
&& w.getEnvironment() != World.Environment.NETHER;
if (cinematic) new RTPSession(this, p, z, loc).start();
else instant(p, loc);
});