Portfolio
  • Home
  • About
package me.scotth0828.ChatEdit.Main;

import java.util.List;

import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;

import net.md_5.bungee.api.ChatColor;

public class Handler implements Listener {

	Main main;
	Message msg;

	public Handler(Main main) {
		this.main = main;
		msg = new Message(main);
	}

	@EventHandler
	public void playerChat(AsyncPlayerChatEvent e) {

		String rawMessage = e.getMessage();
		String fMessage = String.format(e.getFormat(), e.getPlayer().getDisplayName(), rawMessage);
		if (main.hasPerm(e.getPlayer(), "ChatEdit.Colors"))
			fMessage = String.format(e.getFormat(), e.getPlayer().getDisplayName(), msg.ColoredMsg(rawMessage));

		String type = main.users.getData().getString(e.getPlayer().getUniqueId() + ".type");

		if (type.equals(""))
			type = (String) main.getConfig().getList("ChatEdit.ChatType").get(0);

		boolean targeted = false;

		for (Player pl : e.getRecipients()) {

			if (rawMessage.toLowerCase().contains(pl.getDisplayName().toLowerCase())
					&& !e.getPlayer().getDisplayName().toLowerCase().equals(pl.getDisplayName().toLowerCase()))
				targeted = true;

			String PLType = main.users.getData().getString(pl.getUniqueId() + ".type");

			if (PLType.equals(""))
				PLType = (String) main.getConfig().getList("ChatEdit.ChatType").get(0);

			if (main.users.getData().getBoolean(pl.getUniqueId() + ".chatadmin") && !e.getPlayer().equals(pl)) {
				pl.sendMessage(ChatColor.RED + "[" + type + "] " + ChatColor.WHITE + e.getPlayer().getDisplayName()
						+ ": " + ChatColor.AQUA + rawMessage);
			} else {

				if (!targeted) {

					if (PLType.equals("nearby")) {
						if (pl.getWorld() == e.getPlayer().getWorld()
								&& pl.getPlayer().getLocation().distance(e.getPlayer().getLocation()) <= main
										.getConfig().getInt("ChatEdit.Default.Radius")) {
							if (type.equals("nearby")) {
								pl.sendMessage(fMessage);
							}

						}
					} else if (!PLType.equals("off")) {
						List<String> types = main.getConfig().getStringList("ChatEdit.ChatType");
						for (String s : types) {
							if (type.equals(s) && PLType.equals(s)) {
								pl.sendMessage(fMessage);
							}
						}
					}
				} else {
					pl.playSound(pl.getLocation(), Sound.ORB_PICKUP, 3.0F, 0.5F);
					fMessage = String.format(e.getFormat(), e.getPlayer().getDisplayName(),
							ChatColor.GOLD + rawMessage);
					if (main.hasPerm(e.getPlayer(), "ChatEdit.Colors"))
						fMessage = String.format(e.getFormat(), e.getPlayer().getDisplayName(),
								ChatColor.GOLD + msg.ColoredMsg(rawMessage));
					pl.sendMessage(fMessage);
					targeted = false;
				}

			}

		}

		e.getRecipients().clear();

	}

	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent e) {
		try {
			if (main.users.getData().getString(e.getPlayer().getUniqueId() + ".type") == null) {
				main.users.getData().set(e.getPlayer().getUniqueId() + ".type",
						main.getConfig().getList("ChatEdit.ChatType").get(0));
				main.users.saveData();
			}
			if (main.users.getData().getString(e.getPlayer().getUniqueId() + ".radius") == null) {
				main.users.getData().set(e.getPlayer().getUniqueId() + ".radius",
						main.getConfig().getString("ChatEdit.Default.Radius"));
				main.users.saveData();
			}

			if (main.users.getData().getString(e.getPlayer().getUniqueId() + ".chatadmin") == null) {
				main.users.getData().set(e.getPlayer().getUniqueId() + ".chatadmin", false);
				main.users.saveData();
			}

			msg.send(e.getPlayer(), "Your chat type is set to " + ChatColor.BLUE
					+ main.users.getData().getString(e.getPlayer().getUniqueId() + ".type"));
		} catch (Exception ex) {
		}
	}

	@EventHandler
	public void onClick(InventoryClickEvent e) {

		if (e.getInventory().getTitle().equals(ChatColor.BLUE + "Chat Types")) {
			List<String> types = main.getTypes();

			for (int i = 0; i < types.size(); i++) {
				if (e.getSlot() == i) {
					e.setCancelled(true);
					main.users.getData().set(e.getWhoClicked().getUniqueId() + ".type", types.get(i));
					msg.send((Player) e.getWhoClicked(),
							"You will now be able to see all chat messages in " + ChatColor.BLUE + types.get(i));
					e.getWhoClicked().closeInventory();
				}
			}
		}

	}

}
package me.scotth0828.ChatEdit.Main;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

import net.md_5.bungee.api.ChatColor;

public class Main extends JavaPlugin {

	SettingsManager users;
	Message msg;

	@Override
	public void onEnable() {
		getServer().getPluginManager().registerEvents(new Handler(this), this);
		loadConfiguration();

		msg = new Message(this);
	}

	@Override
	public void onDisable() {
		saveConfig();
		users.saveData();
	}

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

		if (sender instanceof Player) {

			Player player = (Player) sender;

			if (cmd.getName().equalsIgnoreCase("ChatType")) {

				if (args.length > 1) {
					msg.send(player, ChatColor.RED + "You must input one of the chat types.");
					return false;
				} else if (args.length == 0) {
					msg.send(player, "Your current chat type is set to " + ChatColor.BLUE
							+ users.getData().getString(player.getUniqueId() + ".type"));
					return true;
				}

				if (args[0].toLowerCase().equals("nearby")) {
					users.getData().set(player.getUniqueId() + ".type", "nearby");
					msg.send(player, "You will now be able to see all nearby chat messages within your set radius!");
				} else if (args[0].toLowerCase().equals("off")) {
					users.getData().set(player.getUniqueId() + ".type", "off");
					msg.send(player, "You will now see no chat messages!");
				} else {

					List<String> types = getConfig().getStringList("ChatEdit.ChatType");

					for (String s : types) {
						if (args[0].toLowerCase().equals(s)) {
							users.getData().set(player.getUniqueId() + ".type", s);
							msg.send(player, "You will now be able to see all chat messages in " + ChatColor.BLUE + s);
							return true;
						}
					}

					msg.send(player,
							ChatColor.RED + "That chat type does not exist! Use one of the chat types as a value!");
				}

				users.saveData();

				return true;

			}

			if (cmd.getName().equalsIgnoreCase("ChatR")) {

				if (args.length > 1) {
					msg.send(player, ChatColor.RED + "You must input the radius you want, the default is "
							+ getConfig().getString("ChatEdit.Default.Radius") + ".");
					return false;
				} else if (args.length == 0) {
					msg.send(player, "Your current radius is set to " + ChatColor.BLUE
							+ users.getData().getString(player.getUniqueId() + ".radius"));
					return true;
				}

				if (isStringInt(args[0])) {
					int num = Integer.parseInt(args[0]);
					users.getData().set(player.getUniqueId() + ".radius", num);
					msg.send(player, "Your radius has been set to " + args[0]);
					users.saveData();

					return true;
				} else {
					msg.send(player, ChatColor.RED + "Your radius must be a number!");
					return false;
				}

			}

			if (cmd.getName().equalsIgnoreCase("ChatEditReload")) {
				reloadConfiguration();
				msg.send(player, "Reloaded Successfully!");

				return true;
			}

			if (cmd.getName().equalsIgnoreCase("ChatF")) {
				if (args.length > 2 || args.length < 2) {
					return false;
				}

				String target = args[0];
				String type = args[1].toLowerCase();

				for (Player p : getServer().getOnlinePlayers()) {
					if (p.getDisplayName().equals(target)) {

						List<String> types = getConfig().getStringList("ChatEdit.ChatType");

						types.add("nearby");
						types.add("off");

						for (int i = 0; i < types.size(); i++) {

							if (type.equals(types.get(i))) {
								break;
							} else if (i == types.size() - 1 && !type.equals(types.get(i))) {
								msg.send(player, ChatColor.RED + "You must input one of the chat types.");
								return true;
							}
						}

						users.getData().set(p.getUniqueId() + ".type", type);
						msg.send(player, "You have set " + ChatColor.GOLD + p.getDisplayName() + ChatColor.GREEN
								+ " to " + ChatColor.BLUE + type);
						msg.send(p, "Your chat type has been set to " + ChatColor.BLUE + type);
						return true;
					}
				}
				msg.send(player, ChatColor.RED + "That is not a valid player!");
				return true;
			}

			if (cmd.getName().equalsIgnoreCase("ChatAdmin")) {
				if (users.getData().getBoolean(player.getUniqueId() + ".chatadmin")) {
					users.getData().set(player.getUniqueId() + ".chatadmin", false);
					msg.send(player, "You will now only see your current chat type!");
				} else {
					users.getData().set(player.getUniqueId() + ".chatadmin", true);
					msg.send(player, "You will now view all chat types!");
				}
				return true;
			}

			if (cmd.getName().equalsIgnoreCase("ChatL")) {
				List<String> types = getTypes();

				ItemStack t = new ItemStack(Material.ENCHANTED_BOOK);
				ItemMeta meta = t.getItemMeta();

				Inventory inv = Bukkit.createInventory(null, 36, ChatColor.BLUE + "Chat Types");

				for (int i = 0; i < types.size(); i++) {
					meta.setDisplayName(ChatColor.DARK_PURPLE + types.get(i));
					List<String> r = new ArrayList<>();
					int amount = 0;
					int online = getServer().getOnlinePlayers().size();
					for (Player p : getServer().getOnlinePlayers()) {
						if (users.getData().getString(p.getUniqueId() + ".type").equals(types.get(i))) {
							amount++;
						}
					}
					if (!types.get(i).equals("nearby") && !types.get(i).equals("off"))
						r.add(amount + "/" + online + " in chat");
					meta.setLore(r);
					t.setItemMeta(meta);
					inv.setItem(i, t);
				}

				player.openInventory(inv);

				return true;
			}
		}

		return false;
	}
	
	public boolean hasPerm(Player ply, String permission) {
		if(ply.hasPermission(permission)) {
			return true;
		}
		return false;
	}

	public List<String> getTypes() {
		List<String> types = getConfig().getStringList("ChatEdit.ChatType");

		types.add("nearby");
		types.add("off");

		return types;
	}

	public boolean isStringInt(String s) {
		try {
			Integer.parseInt(s);
			return true;
		} catch (NumberFormatException ex) {
			return false;
		}
	}

	public void loadConfiguration() {
		saveDefaultConfig();
		users = new SettingsManager(this, "users");
		users.saveData();
	}

	public void reloadConfiguration() {
		this.reloadConfig();
		users.reloadData();
	}

}
package me.scotth0828.ChatEdit.Main;

import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

public class Message {

	Main main;

	HashMap<String, ChatColor> colors = new HashMap<>();

	public Message(Main main) {
		this.main = main;
		addColors();
	}

	public void send(Player ply, String msg) {
		ply.sendMessage(ChatColor.GOLD + "[ChatEdit] " + ChatColor.GREEN + msg);
	}

	public String ColoredMsg(String str) {

		char[] c = str.toCharArray();
		for (int x = 0; x < c.length; x++) {

			if (c[x] == '[') {

				for (int y = x; y < c.length; y++) {

					if (c[y] == ']') {
						if (colors.containsKey(str.substring(x + 1, y).toLowerCase())) {
							str = str.substring(0, x) + colors.get(str.substring(x + 1, y).toLowerCase())
									+ str.substring(y + 1, str.length());
							c = str.toCharArray();
							x = 0;
						}
					}

				}

			}

		}

		return str;
	}

	private void addColors() {
		colors.put("aqua", ChatColor.AQUA);
		colors.put("black", ChatColor.BLACK);
		colors.put("blue", ChatColor.BLUE);
		colors.put("darkaqua", ChatColor.DARK_AQUA);
		colors.put("darkblue", ChatColor.DARK_BLUE);
		colors.put("darkgray", ChatColor.DARK_GRAY);
		colors.put("darkgreen", ChatColor.DARK_GREEN);
		colors.put("darkpurple", ChatColor.DARK_PURPLE);
		colors.put("darkred", ChatColor.DARK_RED);
		colors.put("gold", ChatColor.GOLD);
		colors.put("gray", ChatColor.GRAY);
		colors.put("green", ChatColor.GREEN);
		colors.put("lightpurple", ChatColor.LIGHT_PURPLE);
		colors.put("red", ChatColor.RED);
		colors.put("white", ChatColor.WHITE);
		colors.put("yellow", ChatColor.YELLOW);
	}

}
package me.scotth0828.ChatEdit.Main;

import java.io.File;
import java.io.IOException;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginDescriptionFile;

public class SettingsManager {
	
	Main main;
	
	FileConfiguration data;
	File dfile;
	
	public SettingsManager(Main main, String fileName) {
		
		this.main = main;
		
		if (!main.getDataFolder().exists()) {
			main.getDataFolder().mkdir();
		}
		
		dfile = new File(main.getDataFolder(), fileName+".yml");
		
		if (!dfile.exists()) {
			try {
				dfile.createNewFile();
			}
			catch (IOException e) {
				Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create " + dfile.getName());
			}
		}
		
		data = YamlConfiguration.loadConfiguration(dfile);
	}
	
	public FileConfiguration getData() {
		return data;
	}
	
	public void saveData() {
		try {
			data.save(dfile);
		}
		catch (IOException e) {
			Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save " + dfile.getName());
		}
	}
	
	public void reloadData() {
		data = YamlConfiguration.loadConfiguration(dfile);
	}
	
	public PluginDescriptionFile getDesc() {
		return main.getDescription();
	}
}
© 2021 Copyright: scottsportfolio