blob: f2fde6f12d21768e6a7f38426dae1e4705e0f965 (
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
|
package com.redstoner.modules.misc;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MiscUtil {
private static final Pattern durationPattern = Pattern.compile("^(?:(?:(\\d*)y)?(?:(\\d*)m)?(?:(\\d*)d)?(?:(\\d*)h)?(?:(\\d*)m)?(?:(\\d*)s)?){1}$");
private static int getIntGroup(Matcher matcher, int group) {
String strGroup = matcher.group(group);
return Integer.parseInt(strGroup == null ? "0" : strGroup);
}
protected static int parseDuration(String duration) {
Matcher m = durationPattern.matcher(duration);
if (m.matches()) {
int years = getIntGroup(m, 1);
int months = getIntGroup(m, 2);
int days = getIntGroup(m, 3);
int hours = getIntGroup(m, 4);
int minutes = getIntGroup(m, 5);
int seconds = getIntGroup(m, 6);
return (years * 31557600) + (months * 2629800) + (days * 86400) + (hours * 3600) + (minutes * 60) + seconds;
} else {
return -1;
}
}
protected static int getPing(Player player) {
try {
Object entityPlayer = player.getClass().getMethod("getHandle").invoke(player);
int ping = (int) entityPlayer.getClass().getField("ping").get(entityPlayer);
return ping;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | NoSuchFieldException e) {
e.printStackTrace();
}
return -1;
}
protected static boolean canBuild(Player player, Location location) {
BlockBreakEvent event = new BlockBreakEvent(location.getBlock(), player);
Bukkit.getPluginManager().callEvent(event);
return !event.isCancelled();
}
}
|