summaryrefslogtreecommitdiff
path: root/src/com/redstoner/modules/challenge/Challenge.java
blob: 0d020a8c969ebcc451a7115a8fe3e2f17a077ae8 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.redstoner.modules.challenge;

import java.io.File;
import java.util.Random;

import org.bukkit.command.CommandSender;
import org.json.simple.JSONArray;

import com.nemez.cmdmgr.Command;
import com.redstoner.annotations.Version;
import com.redstoner.misc.JsonManager;
import com.redstoner.misc.Main;
import com.redstoner.misc.Utils;
import com.redstoner.modules.Module;

@Version(major = 2, minor = 0, revision = 1, compatible = 2)
public class Challenge implements Module
{
	private File challengeLocation = new File(Main.plugin.getDataFolder(), "challenges.json");
	private JSONArray challenges;
	
	@Override
	public boolean onEnable()
	{
		challenges = JsonManager.getArray(challengeLocation);
		if (challenges == null)
			challenges = new JSONArray();
		return true;
	}
	
	@Override
	public void onDisable()
	{
		saveChallenges();
	}
	
	@SuppressWarnings("unchecked")
	@Command(hook = "addchallenge")
	public boolean addChallenge(CommandSender sender, String text)
	{
		if (challenges.contains(text))
			Utils.sendErrorMessage(sender, null, "That challenge already exists!");
		else
		{
			Utils.sendMessage(sender, null, "Successfully added a new challenge!");
			challenges.add(text);
			saveChallenges();
		}
		return true;
	}
	
	@Command(hook = "delchallenge")
	public boolean delChallenge(CommandSender sender, int id)
	{
		if (challenges.size() == 0)
		{
			Utils.sendErrorMessage(sender, null, "There are no challenges yet!");
			return true;
		}
		if (id < 0 || id >= challenges.size())
		{
			Utils.sendErrorMessage(sender, null, "The ID must be at least 0 and at most " + (challenges.size() - 1));
			return true;
		}
		Utils.sendMessage(sender, null, "Successfully deleted the challenge: " + challenges.remove(id), '&');
		saveChallenges();
		return true;
	}
	
	@SuppressWarnings("unchecked")
	@Command(hook = "setchallenge")
	public boolean setChallenge(CommandSender sender, int id, String text)
	{
		if (challenges.size() == 0)
		{
			Utils.sendErrorMessage(sender, null, "There are no challenges yet!");
			return true;
		}
		if (id < 0 || id >= challenges.size())
		{
			Utils.sendErrorMessage(sender, null, "The ID must be at least 0 and at most " + (challenges.size() - 1));
			return true;
		}
		Utils.sendMessage(sender, null,
				"Successfully changed the challenge: &a" + challenges.get(id) + " &7to: &e" + text, '&');
		challenges.set(id, text);
		saveChallenges();
		return true;
	}
	
	@Command(hook = "challengeid")
	public boolean challengeId(CommandSender sender, int id)
	{
		if (challenges.size() == 0)
		{
			Utils.sendErrorMessage(sender, null, "There are no challenges yet!");
			return true;
		}
		if (id < 0 || id >= challenges.size())
		{
			Utils.sendErrorMessage(sender, null, "The ID must be at least 0 and at most " + (challenges.size() - 1));
			return true;
		}
		Utils.sendMessage(sender, null, "&a" + challenges.get(id), '&');
		return true;
	}
	
	@Command(hook = "challenge")
	public boolean challenge(CommandSender sender)
	{
		if (challenges.size() == 0)
		{
			Utils.sendErrorMessage(sender, null, "There are no challenges yet!");
			return true;
		}
		int id = (new Random()).nextInt(challenges.size());
		Utils.sendMessage(sender, null, "&a" + challenges.get(id), '&');
		return true;
	}
	
	@Command(hook = "listchallenges")
	public boolean listChallenges(CommandSender sender, int page)
	{
		if (challenges.size() == 0)
		{
			Utils.sendErrorMessage(sender, null, "There are no challenges yet!");
			return true;
		}
		page = page - 1;
		int start = page * 10;
		int end = start + 10;
		int pages = (int) Math.ceil(challenges.size() / 10d);
		if (start < 0)
		{
			Utils.sendErrorMessage(sender, null, "Page number too small, must be at least 0!");
			return true;
		}
		if (start > challenges.size())
		{
			Utils.sendErrorMessage(sender, null, "Page number too big, must be at most " + pages + "!");
			return true;
		}
		Utils.sendModuleHeader(sender);
		Utils.sendMessage(sender, "", "&ePage " + (page + 1) + "/" + pages + ":", '&');
		for (int i = start; i < end && i < challenges.size(); i++)
			Utils.sendMessage(sender, "", "&a" + i + "&8: &e" + challenges.get(i), '&');
		return true;
	}
	
	@Command(hook = "listchallengesdef")
	public boolean listChallengesDefault(CommandSender sender)
	{
		return listChallenges(sender, 1);
	}
	
	@Command(hook = "searchchallenge")
	public boolean search(CommandSender sender, boolean insensitive, String text)
	{
		Utils.sendModuleHeader(sender);
		boolean found = false;
		if (insensitive)
		{
			text = text.toLowerCase();
			for (int i = 0; i < challenges.size(); i++)
			{
				if (((String) challenges.get(i)).toLowerCase().contains(text))
				{
					Utils.sendMessage(sender, "", "&a" + i + "&8: &e" + challenges.get(i), '&');
					found = true;
				}
			}
		}
		else
		{
			for (int i = 0; i < challenges.size(); i++)
			{
				if (((String) challenges.get(i)).contains(text))
				{
					Utils.sendMessage(sender, "", "&a" + i + "&8: &e" + challenges.get(i), '&');
					found = true;
				}
			}
		}
		if (!found)
		{
			Utils.sendMessage(sender, "", "&cCouldn't find any matching challenges.", '&');
		}
		return true;
	}
	
	@Command(hook = "matchchallenge")
	public boolean match(CommandSender sender, boolean insensitive, String regex)
	{
		Utils.sendModuleHeader(sender);
		boolean found = false;
		if (insensitive)
		{
			regex = regex.toLowerCase();
			for (int i = 0; i < challenges.size(); i++)
			{
				if (((String) challenges.get(i)).toLowerCase().matches(regex))
				{
					Utils.sendMessage(sender, "", "&a" + i + ": " + challenges.get(i), '&');
					found = true;
				}
			}
		}
		else
		{
			for (int i = 0; i < challenges.size(); i++)
			{
				if (((String) challenges.get(i)).matches(regex))
				{
					Utils.sendMessage(sender, "", "&a" + i + ": " + challenges.get(i), '&');
					found = true;
				}
			}
		}
		if (!found)
		{
			Utils.sendMessage(sender, "", "&cCouldn't find any matching challenges.", '&');
		}
		return true;
	}
	
	public void saveChallenges()
	{
		JsonManager.save(challenges, challengeLocation);
	}
	
	// @noformat
	@Override
	public String getCommandString()
	{
		return "command challenge {\n" + 
				"    add [string:text...] {\n" + 
				"        help Adds a challenge.;\n" + 
				"        run addchallenge text;\n" + 
				"        perm utils.challenge.add;\n" + 
				"    }\n" + 
				"    del [int:id] {\n" + 
				"        help Removes a challenge.;\n" + 
				"        run delchallenge id;\n" + 
				"        perm utils.challenge.admin;\n" + 
				"    }\n" + 
				"    set [int:id] [string:text...] {\n" + 
				"        help Sets a challenge.;\n" + 
				"        run setchallenge id text;\n" + 
				"        perm utils.challenge.admin;\n" + 
				"    }\n" + 
				"    id [int:id] {\n" + 
				"        help Get a paticular challenge.;\n" + 
				"        run challengeid id;\n" + 
				"        perm utils.challenge.id;\n" + 
				"    }\n" + 
				"    list [int:page] {\n" + 
				"        help Shows challenges.;\n" + 
				"        run listchallenges page;\n" + 
				"        perm utils.challenge.list;\n" + 
				"    }\n" + 
				"    list {\n" + 
				"        help Shows challenges.;\n" + 
				"        run listchallengesdef;\n" + 
				"        perm utils.challenge.list;\n" + 
				"    }\n" + 
				"    search [flag:-i] [string:text...] {\n" + 
				"        help Search challenges.;\n" + 
				"        run searchchallenge -i text;\n" + 
				"        perm utils.challenge.search;\n" + 
				"    }\n" + 
				"    match [flag:-i] [string:regex...] {\n" + 
				"        help Search challenges. But better.;\n" + 
				"        run matchchallenge -i regex;\n" + 
				"        perm utils.challenge.match;\n" + 
				"    }\n" + 
				"    [empty] {\n" + 
				"        help Gives a challenge.;\n" + 
				"        run challenge;\n" + 
				"        perm utils.challenge;\n" + 
				"    }\n" + 
				"}";
	}
	// @format
}