Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
6.7 kB
0
Indexable
Never
package org.lifestealsmp.combatlog;

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

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
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.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.managers.RegionManager;

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

public class main extends JavaPlugin implements Listener {

	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);
						} 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);
	}
	
	private WorldGuardPlugin getWorldGuard() {
        Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");

        // WorldGuard may not be loaded
        if (plugin == null || !(plugin instanceof WorldGuardPlugin)) {
            return null; // Throws an error or disable your plugin
        }

        return (WorldGuardPlugin) plugin;
    }

	@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();
			
			World duels = Bukkit.getServer().getWorld("duels");
			if(whoHit.getLocation().getWorld().equals(duels) || whoWasHit.getLocation().getWorld().equals(duels)) {
				return;
			}

			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());

			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());

				saveConfig();
			}
		}
	}

	@EventHandler
	public void onPlayerDeath(PlayerDeathEvent event) {
		if (event.getEntity() instanceof Player) {
			Player player = (Player) event.getEntity().getPlayer();
			if (getConfig().contains(player.getName())) {
				getConfig().set(player.getName(), null);
				saveConfig();
			}
		}
	}

	@EventHandler
	public void onQuitEvent(PlayerQuitEvent event) {
		Player player = (Player) event.getPlayer();
		if (getConfig().getString(player.getName()) != null) {
			quittingPlayers.add(player.getName());
	        ItemStack offHandItem = event.getPlayer().getInventory().getItemInOffHand();
	        if (offHandItem != null && offHandItem.getType() == Material.TOTEM_OF_UNDYING) {
	            event.getPlayer().getInventory().setItemInOffHand(new ItemStack(Material.AIR));
	        }
			String damagerString = getConfig().getString(player.getName() + ".Damager");
			Player damager = Bukkit.getPlayer(damagerString);
			player.damage(player.getHealth() + 100, damager);

			getConfig().set(player.getName(), null);
			saveConfig();
		}
	}

	@EventHandler
	public void onPlayerMove(PlayerMoveEvent event) {
		Player player = (Player) event.getPlayer();
		ItemStack item = player.getInventory().getItemInMainHand();
		if (getConfig().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 (getConfig().contains(player.getName())) {
				event.setCancelled(true);
			}
		}
	}

	@EventHandler
	public void onCmd(PlayerCommandPreprocessEvent event) {
		Player player = (Player) event.getPlayer();
		if (getConfig().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