Files
src
build.gradle
settings.gradle
Code
11 1,174
src/main/java/com/kodari/redstonelimiter/RedstoneLimiterPlugin.java
package com.kodari.redstonelimiter;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public final class RedstoneLimiterPlugin extends JavaPlugin {
private LimiterSettings settings;
private ActivityTracker tracker;
private NotificationManager notifications;
private RedisBridge redis;
private final Map<UUID, PendingVisit> pendingVisits = new ConcurrentHashMap<>();
@Override
public void onEnable() {
saveDefaultConfig();
settings = new LimiterSettings(this);
tracker = new ActivityTracker(settings);
notifications = new NotificationManager(settings);
getServer().getPluginManager().registerEvents(new LimiterListener(this), this);
getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
LimiterCommand command = new LimiterCommand(this);
getCommand("redstonelimiter").setExecutor(command);
getCommand("redstonelimiter").setTabCompleter(command);
redis = new RedisBridge(this, settings);
redis.start();
getServer().getScheduler().runTaskTimer(this, () -> tracker.cleanup(), 1200L, 1200L);
}
@Override
public void onDisable() {
if (redis != null) {
redis.close();
}
getServer().getMessenger().unregisterOutgoingPluginChannel(this, "BungeeCord");
}
public void reloadSettings() {
reloadConfig();
settings = new LimiterSettings(this);
tracker.updateSettings(settings);
notifications.updateSettings(settings);
if (redis != null) {
redis.close();
}
redis = new RedisBridge(this, settings);
redis.start();
}
public void handleBlocked(Location location, String material, String reason) {
String world = location.getWorld().getName();
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
if (notifications.notifyAlert(settings.serverName(), world, x, y, z, material, reason) && settings.redisEnabled()) {
redis.publishAlert(settings.serverName(), world, x, y, z, material, reason);
}
if (settings.feedbackEnabled()) {
Location effectLocation = location.clone().add(0.5, 0.5, 0.5);
XParticle.of(settings.feedbackParticle()).ifPresent(particle ->
location.getWorld().spawnParticle(particle.get(), effectLocation, settings.feedbackCount(), 0.25, 0.25, 0.25, 0.01));
}
}
public void connectToServer(Player player, String server) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(bytes);
output.writeUTF("Connect");
output.writeUTF(server);
player.sendPluginMessage(this, "BungeeCord", bytes.toByteArray());
} catch (IOException exception) {
getLogger().warning("Could not send proxy connection request: " + exception.getMessage());
}
}
public void acceptRemoteVisit(String targetServer, UUID playerId, String world, double x, double y, double z) {
if (!targetServer.equalsIgnoreCase(settings.serverName())) {
return;
}
PendingVisit visit = new PendingVisit(world, x, y, z);
pendingVisits.put(playerId, visit);
Player player = Bukkit.getPlayer(playerId);
if (player != null) {
applyPendingVisit(player);
}
}
public void applyPendingVisit(Player player) {
PendingVisit visit = pendingVisits.remove(player.getUniqueId());
if (visit == null) {
return;
}
Bukkit.getScheduler().runTask(this, () -> {
World world = Bukkit.getWorld(visit.world());
if (world == null) {
player.sendMessage(net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacyAmpersand()
.deserialize(settings.message("visit-world-missing", "&cThat world is not loaded on this server.")));
return;
}
player.teleport(new Location(world, visit.x(), visit.y(), visit.z()));
player.sendMessage(net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacyAmpersand()
.deserialize(settings.message("visit-complete", "&aTeleported to the affected location.")));
});
}
public LimiterSettings settings() {
return settings;
}
public ActivityTracker tracker() {
return tracker;
}
public NotificationManager notifications() {
return notifications;
}
public RedisBridge redis() {
return redis;
}
private record PendingVisit(String world, double x, double y, double z) {
}
}