summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/misc/Utils.java
blob: e3fd68c5a27eccf7ae00a76f2b1fd5ccc7a16f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.redstoner.misc;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.redstoner.annotations.Version;
import com.redstoner.coremods.moduleLoader.ModuleLoader;

import net.nemez.chatapi.ChatAPI;
import net.nemez.chatapi.click.Message;

/** The utils class containing utility functions. Those include but are not limited to sending formatted messages, broadcasts and more.
 * 
 * @author Pepich */
@Version(major = 4, minor = 0, revision = 2, compatible = 1)
public final class Utils
{
	/** The @SimpleDateFormat used for getting the current date. */
	public static SimpleDateFormat dateFormat = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");
	
	/** The Pattern for a UUID*/
	private static final Pattern UUID_pattern = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
	private static final Pattern Class_pattern = Pattern.compile(".*\\.");
	private static final Pattern NoDolarSign_pattern = Pattern.compile("\\$\\d*");
	
	/** Hidden constructor. Do not instantiate UTILS classes! :) */
	private Utils()
	{}
	
	/** This method broadcasts a message to all players and console that are allowed by the filter. Set the filter to NULL to broadcast to everyone.</br>
	 * If you want to, you can set a message that will be logged to console. Set to null to not log anything.</br>
	 * You can still allow console in the filter to log the original message.
	 * 
	 * @param prefix The prefix for the message. Set to NULL to let it auto generate.
	 * @param message the message to be sent around
	 * @param filter the BroadcastFilter to be applied.</br>
	 *        Write a class implementing the interface and pass it to this method, the "sendTo()" method will be called for each recipient.
	 * @param logmessage the log message to appear in console. Set to null to not log this (you can still log the original message by returning true in the filter).
	 * @return the amount of people that received the message. */
	public static int broadcast(String prefix, String message, BroadcastFilter filter)
	{
		if (prefix == null)
			prefix = "§8[§2" + getCaller() + "§8]: ";
		if (filter == null)
		{
			for (Player p : Bukkit.getOnlinePlayers())
				p.sendMessage(prefix + message);
			Bukkit.getConsoleSender().sendMessage(prefix + message);
			return Bukkit.getOnlinePlayers().size() + 1;
		}
		else
		{
			int count = 0;
			for (Player p : Bukkit.getOnlinePlayers())
				if (filter.sendTo(p))
				{
					p.sendMessage(prefix + message);
					count++;
				}
			if (filter.sendTo(Bukkit.getConsoleSender()))
			{
				Bukkit.getConsoleSender().sendMessage(prefix + message);
				count++;
			}
			return count;
		}
	}
	
	/** This method broadcasts a message to all players and console that are allowed by the filter. Set the filter to NULL to broadcast to everyone.</br>
	 * If you want to, you can set a message that will be logged to console. Set to null to not log anything.</br>
	 * You can still allow console in the filter to log the original message.
	 * 
	 * @param prefix The prefix for the message. Set to NULL to let it auto generate.
	 * @param message the message to be sent around
	 * @param filter the BroadcastFilter to be applied.</br>
	 *        Write a class implementing the interface and pass it to this method, the "sendTo()" method will be called for each recipient.
	 * @param logmessage the log message to appear in console. Set to null to not log this (you can still log the original message by returning true in the filter).
	 */
	public static int broadcast(String prefix, Message message, BroadcastFilter filter)
	{
		if (prefix == null)
			prefix = "§8[§2" + getCaller() + "§8]: ";
		if (filter == null)
		{
			for (Player p : Bukkit.getOnlinePlayers())
				ChatAPI.createMessage(p).appendText(prefix).appendMessage(message).send();
			Bukkit.getConsoleSender().sendMessage(prefix + message.getRawMessage());
			return Bukkit.getOnlinePlayers().size() + 1;
		}
		else
		{
			int count = 0;
			for (Player p : Bukkit.getOnlinePlayers())
				if (filter.sendTo(p))
				{
					ChatAPI.createMessage(p).appendText(prefix).appendMessage(message).send();
					count++;
				}
			if (filter.sendTo(Bukkit.getConsoleSender()))
			{
				Bukkit.getConsoleSender().sendMessage(prefix + message.getRawMessage());
				count++;
			}
			return count;
		}
	}
	
	/** This method will find the next parent caller and return their class name, omitting package names.
	 * 
	 * @return the Name of the calling class. */
	private static final String getCaller()
	{
		StackTraceElement[] stackTrace = (new Exception()).getStackTrace();
		String classname = "Utils";
		for (int i = 0; classname.equals("Utils"); i++)
		{
			classname = Class_pattern.matcher(stackTrace[i].getClassName()).replaceAll("");
		}
		return classname;
	}
	
	/** This method will find the next parent caller and return their class name, omitting package names.
	 * 
	 * @param directCaller used to prevent this method from returning the caller itself. Null if supposed to be ignored.
	 * @return the name of the calling class. */
	public static final String getCaller(String... directCaller)
	{
		if (directCaller == null || directCaller.length == 0)
			return getCaller();
		StackTraceElement[] stackTrace = (new Exception()).getStackTrace();
		String classname = "Utils";
		List<String> callers = Arrays.asList(directCaller);
		for (int i = 0; callers.contains(classname) || classname.equals("Utils"); i++)
		{
			classname = Class_pattern.matcher(stackTrace[i].getClassName()).replaceAll("");
		}
		classname = NoDolarSign_pattern.matcher(classname).replaceAll("");
		return classname;
	}
	
	/** Provides a uniform way of getting the date for all modules.
	 * 
	 * @return The current date in the format "[dd-mm-yyyy hh:mm:ss]" */
	public static String getDate()
	{
		Date date = new Date(System.currentTimeMillis());
		return dateFormat.format(date);
	}
	
	/** Provides a uniform way of getting the (display)name of a @CommandSender.
	 * 
	 * @param sender The @CommandSender to get the name of.
	 * @return The DisplayName of the @CommandSender or if not a @Player, the name in blue. */
	public static String getName(CommandSender sender)
	{
		if (sender instanceof Player)
			return ((Player) sender).getDisplayName();
		else
			return "§9" + sender.getName();
	}
	
	/** Provides a uniform way of getting the UUID of a @CommandSender.
	 * 
	 * @param sender The @CommandSender to get the UUID of.
	 * @return The UUID of the @CommandSender or if not a player, "CONSOLE" in blue. */
	public static String getID(CommandSender sender)
	{
		String id;
		if (sender instanceof Player)
			id = ((Player) sender).getUniqueId().toString();
		else
			id = "CONSOLE";
		return id;
	}
	
	/** Checks if the string is a UUID.
	 * 
	 * @param toCheck String to check.
	 * @return if the string is a UUID.
	 */
	public static boolean isUUID(String toCheck)
	{
	    return UUID_pattern.matcher(toCheck).matches();
	}
	
	public static void run(Runnable r) {
		run(r, 0);
	}
	
	public static void run(Runnable r, int delay) {
		Bukkit.getScheduler().scheduleSyncDelayedTask(ModuleLoader.getPlugin(), r, delay);
	}
	
	
	
}