Files
src
build.gradle
Code
3 91
src/main/java/com/kodari/invisibilityguard/InvisibilityGuardPlugin.java
package com.kodari.invisibilityguard;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
public final class InvisibilityGuardPlugin extends JavaPlugin implements Listener {
private final Set<UUID> exemptPlayers = new HashSet<>();
@Override
public void onEnable() {
getServer().getScheduler().runTaskTimer(this, this::checkPlayers, 0L, 20L);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!command.getName().equalsIgnoreCase("invisexception")) {
return false;
}
if (!(sender instanceof Player player)) {
sender.sendMessage("This command can only be used by a player.");
return true;
}
UUID playerId = player.getUniqueId();
if (exemptPlayers.remove(playerId)) {
player.sendMessage("You are no longer exempt from the invisibility check.");
} else {
exemptPlayers.add(playerId);
player.sendMessage("You are now exempt from the invisibility check.");
}
return true;
}
private void checkPlayers() {
for (Player player : getServer().getOnlinePlayers()) {
if (exemptPlayers.contains(player.getUniqueId()) || isInvisible(player)) {
continue;
}
Location current = player.getLocation();
player.teleport(new Location(player.getWorld(), current.getX(), -10_000_000.0,
current.getZ(), current.getYaw(), current.getPitch()));
}
}
private boolean isInvisible(Player player) {
for (PotionEffect effect : player.getActivePotionEffects()) {
if (effect.getType().getKey().getKey().equalsIgnoreCase("invisibility")) {
return true;
}
}
return false;
}
}