Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
8.3 kB
0
Indexable
Never
package org.lifestealsmp.combatlog;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Arrow;
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.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityToggleGlideEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;

public class main extends JavaPlugin implements Listener {

	List<String> addedPlayers = getConfig().getStringList("Added");
	Set<String> quittingPlayers = new HashSet<>();

	public void onEnable() {
		Bukkit.getServer().getPluginManager().registerEvents(this, this);
		saveDefaultConfig();

		Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
			public void run() {
				for (Player onlineplayers : Bukkit.getOnlinePlayers()) {
					if (getConfig().contains(onlineplayers.getName())) {
						int time = getConfig().getInt(onlineplayers.getName() + ".Time");
						if (time == 0) {
							getConfig().set(onlineplayers.getName(), null);
							addedPlayers.remove(onlineplayers.getName());
							getConfig().set("Added", addedPlayers);
							Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
									"region removemember -w world safezone " + onlineplayers.getName());
							Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
									"region removemember -w world outerPvP " + onlineplayers.getName());
						} else {
							getConfig().set(onlineplayers.getName() + ".Time", time - 1);
							TextComponent tc1 = new TextComponent(
									ChatColor.translateAlternateColorCodes('&', "&4&lCombat Tagged: " + time));
							onlineplayers.spigot().sendMessage(ChatMessageType.ACTION_BAR, tc1);
						}
					}
				}
				saveConfig();
			}
		}, 0, 20);
	}

	public void onDisable() {

	}

	@EventHandler
	public void EntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
		if (event.getEntity() instanceof Player && event.getDamager() instanceof Entity
				&& event.getDamager() instanceof Player) {
			if (event.isCancelled()) {
				return;
			}
			Player whoHit = (Player) event.getDamager();
			Player whoWasHit = (Player) event.getEntity();

			if (quittingPlayers.contains(whoWasHit.getName())) {
				quittingPlayers.remove(whoWasHit.getName()); // Remove the player from the quittingPlayers set.
				return; // Exit the method without setting the timers.
			}

			getConfig().set(whoHit.getName() + ".Time", 30);
			getConfig().set(whoHit.getName() + ".Damager", whoWasHit.getName());
			getConfig().set(whoWasHit.getName() + ".Time", 30);
			getConfig().set(whoWasHit.getName() + ".Damager", whoHit.getName());

			if (!addedPlayers.contains(whoHit.getName())) {
				Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
						"region addmember -w world safezone " + whoHit.getName());
				Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
						"region addmember -w world outerPvP " + whoHit.getName());
				addedPlayers.add(whoHit.getName());
				getConfig().set("Added", addedPlayers);
			}
			if (!addedPlayers.contains(whoWasHit.getName())) {
				Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
						"region addmember -w world safezone " + whoWasHit.getName());
				Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
						"region addmember -w world outerPvP " + whoWasHit.getName());
				addedPlayers.add(whoWasHit.getName());
				getConfig().set("Added", addedPlayers);
			}

			saveConfig();
		}
	}

	@EventHandler
	public void onEntityDamageByArrow(EntityDamageByEntityEvent event) {
		if (event.getEntity() instanceof Player && event.getDamager() instanceof Arrow) {
			if (event.isCancelled()) {
				return;
			}
			Player whoWasHit = (Player) event.getEntity();
			Arrow arrow = (Arrow) event.getDamager();
			if (arrow.getShooter() instanceof Player) {
				Player shooter = (Player) arrow.getShooter();

				if (quittingPlayers.contains(whoWasHit.getName())) {
					quittingPlayers.remove(whoWasHit.getName()); // Remove the player from the quittingPlayers set.
					return; // Exit the method without setting the timers.
				}

				getConfig().set(shooter.getName() + ".Time", 30);
				getConfig().set(shooter.getName() + ".Damager", whoWasHit.getName());
				getConfig().set(whoWasHit.getName() + ".Time", 30);
				getConfig().set(whoWasHit.getName() + ".Damager", shooter.getName());

				if (!addedPlayers.contains(shooter.getName())) {
					Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
							"region addmember -w world safezone " + shooter.getName());
					Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
							"region addmember -w world outerPvP " + shooter.getName());
					addedPlayers.add(shooter.getName());
					getConfig().set("Added", addedPlayers);
				}
				if (!addedPlayers.contains(whoWasHit.getName())) {
					Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
							"region addmember -w world safezone " + whoWasHit.getName());
					Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
							"region addmember -w world outerPvP " + whoWasHit.getName());
					addedPlayers.add(whoWasHit.getName());
					getConfig().set("Added", addedPlayers);
				}

				saveConfig();
			}
		}
	}

	@EventHandler
	public void onPlayerDeath(PlayerDeathEvent event) {
		if (event.getEntity() instanceof Player) {
			Player player = (Player) event.getEntity().getPlayer();
			if (addedPlayers.contains(player.getName())) {
				getConfig().set(player.getName(), null);
				addedPlayers.remove(player.getName());
				getConfig().set("Added", addedPlayers);
				Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
						"region removemember -w world safezone " + player.getName());
				Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
						"region removemember -w world outerPvP " + player.getName());
				saveConfig();
			}
		}
	}

	@EventHandler
	public void onQuitEvent(PlayerQuitEvent event) {
		Player player = (Player) event.getPlayer();
		if (getConfig().getString(player.getName()) != null) {
			quittingPlayers.add(player.getName());
			String damagerString = getConfig().getString(player.getName() + ".Damager");
			Player damager = Bukkit.getPlayer(damagerString);
			player.damage(player.getHealth() + 1, damager);

			getConfig().set(player.getName(), null);
			addedPlayers.remove(player.getName());
			getConfig().set("Added", addedPlayers);
			Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
					"region removemember -w world safezone " + player.getName());
			Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
					"region removemember -w world outerPvP " + player.getName());
			saveConfig();
		}
	}

	@EventHandler
	public void onPlayerMove(PlayerMoveEvent event) {
		Player player = (Player) event.getPlayer();
		ItemStack item = player.getInventory().getItemInMainHand();
		if (addedPlayers.contains(player.getName())) {
			if (item.getType() == Material.TRIDENT) {
				if (player.isRiptiding()) {
					event.setCancelled(true);
				}
			}
		}
	}

	@EventHandler(priority = EventPriority.HIGHEST)
	public void onEntityToggleGlide(EntityToggleGlideEvent event) {
		if (event.getEntity() instanceof Player) {
			Player player = (Player) event.getEntity();
			if (addedPlayers.contains(player.getName())) {
				event.setCancelled(true);
			}
		}
	}

	@EventHandler
	public void onCmd(PlayerCommandPreprocessEvent event) {
		Player player = (Player) event.getPlayer();
		if (addedPlayers.contains(player.getName())) {
			if (!event.getPlayer().hasPermission("essentials.gamemode.spectator")) {
				event.setCancelled(true);
				event.getPlayer().sendMessage(ChatColor.RED + "Commands are blocked while in combat!");
			}
		}
	}
}
Leave a Comment