/
TipSet.java
116 lines (101 loc) · 3.48 KB
/
TipSet.java
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
package net.namedfork.bukkit.Tips;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
/**
*
* @author zydeco
*/
public class TipSet implements Runnable {
private final World world;
private final int delay, period;
private final String[] tips;
private final Random rng;
private final String id;
private final Permission perm;
private int nextTip;
public TipSet(String id, World world, int delay, int period, List<String> tips, long seed) {
this.id = id;
this.world = world;
this.delay = delay;
this.period = period;
this.tips = tips.toArray(new String[0]);
this.rng = seed == 0?null:new Random(seed);
nextTip = -1;
// format tips
for(int i=0; i < this.tips.length; i++) {
this.tips[i] = formatTip(this.tips[i]);
}
// register permission
if (id != null) {
PluginManager pm = Bukkit.getServer().getPluginManager();
String permName = "tips.receive."+id;
if (pm.getPermission(permName) == null) {
perm = new Permission(permName, PermissionDefault.FALSE);
System.out.println("REGISTERING PERMISSION "+perm.getName());
pm.addPermission(perm);
} else {
perm = pm.getPermission(permName);
}
} else {
perm = null;
}
}
public void run() {
// find a tip
if (tips.length == 0) return;
else if (tips.length == 1) {
nextTip = 0;
} else if (rng != null) {
nextTip = rng.nextInt(tips.length);
} else {
nextTip = (nextTip + 1) % tips.length;
}
final String tip = tips[nextTip];
if (world == null && perm == null) {
// tip for everyone
Bukkit.getServer().broadcastMessage(tip);
} else if (world == null && perm != null) {
// global tip with permission
Player players[] = Bukkit.getServer().getOnlinePlayers();
for(Player p: players) {
if (p.hasPermission(perm)) System.out.println(p.getName() + " has permission " + perm.getName());
if (p.hasPermission("tips.receive.*")) System.out.println(p.getName() + " has permission tips.receive.*");
if (p.hasPermission(perm) || p.hasPermission("tips.receive.*")) {
p.sendMessage(tip);
}
}
} else {
// tip for world
Iterator<Player> i = world.getPlayers().iterator();
while (i.hasNext()) {
Player p = i.next();
if (perm == null || p.hasPermission(perm) || p.hasPermission("tips.receive.*")) {
p.sendMessage(tip);
}
}
}
}
// replace $ with \u00A7 for color codes, and $$ with $
private String formatTip(String tip) {
return tip.replace("$", "\u00A7").replace("\u00A7$", "$");
}
public World getWorld() {
return world;
}
public int getDelay() {
return delay;
}
public int getPeriod() {
return period;
}
public String[] getTips() {
return tips;
}
}