· 6 years ago · Oct 19, 2019, 05:10 AM
1package space.essentials;
2
3import java.io.File;
4import java.io.IOException;
5import java.text.SimpleDateFormat;
6import java.util.Date;
7import java.util.ArrayList;
8import java.util.Calendar;
9import java.util.Collection;
10import java.util.GregorianCalendar;
11import java.util.Iterator;
12import java.util.LinkedHashMap;
13import java.util.List;
14import java.util.TimeZone;
15
16import org.bukkit.Bukkit;
17import org.bukkit.GameMode;
18import org.bukkit.Location;
19import org.bukkit.OfflinePlayer;
20import org.bukkit.attribute.Attribute;
21import org.bukkit.command.CommandSender;
22import org.bukkit.command.ProxiedCommandSender;
23import org.bukkit.configuration.file.FileConfiguration;
24import org.bukkit.configuration.file.YamlConfiguration;
25import org.bukkit.enchantments.Enchantment;
26import org.bukkit.entity.Entity;
27import org.bukkit.entity.LivingEntity;
28import org.bukkit.entity.Player;
29import org.bukkit.event.EventHandler;
30import org.bukkit.event.Listener;
31import org.bukkit.event.block.Action;
32import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
33import org.bukkit.event.player.PlayerInteractEvent;
34import org.bukkit.event.player.PlayerJoinEvent;
35import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
36import org.bukkit.help.HelpMap;
37import org.bukkit.help.HelpTopic;
38import org.bukkit.help.IndexHelpTopic;
39import org.bukkit.inventory.ItemStack;
40import org.bukkit.inventory.meta.ItemMeta;
41import org.bukkit.plugin.java.JavaPlugin;
42
43import de.tr7zw.nbtapi.NBTItem;
44import io.github.jorelali.commandapi.api.CommandAPI;
45import io.github.jorelali.commandapi.api.CommandPermission;
46import io.github.jorelali.commandapi.api.arguments.Argument;
47import io.github.jorelali.commandapi.api.arguments.BooleanArgument;
48import io.github.jorelali.commandapi.api.arguments.DynamicSuggestedStringArgument;
49import io.github.jorelali.commandapi.api.arguments.EnchantmentArgument;
50import io.github.jorelali.commandapi.api.arguments.EntitySelectorArgument;
51import io.github.jorelali.commandapi.api.arguments.EntitySelectorArgument.EntitySelector;
52import io.github.jorelali.commandapi.api.arguments.GreedyStringArgument;
53import io.github.jorelali.commandapi.api.arguments.IntegerArgument;
54import io.github.jorelali.commandapi.api.arguments.StringArgument;
55import net.md_5.bungee.api.ChatColor;
56
57public class SpaceEssentialsPlugin extends JavaPlugin implements Listener
58{
59 @Override
60 public void onEnable()
61 {
62 //register all commands
63 registerFly();
64 registerPowertool();
65 registerSetwarp();
66 registerDelwarp();
67 registerWarp();
68 registerTpall();
69 registerFeed();
70 registerHeal();
71 registerEnchantUnsafe();
72 registerSudo();
73 registerTop();
74 registerGma();
75 registerGms();
76 registerGmc();
77 registerGmsp();
78 registerTempban();
79 registerPardon();
80 registerRename();
81 Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> editHelp(), 1L);
82 //register this as the event listener
83 Bukkit.getPluginManager().registerEvents(this, this);
84 }
85
86 private void editHelp()
87 {
88 HelpMap helpMap = Bukkit.getHelpMap();
89 HelpTopic fly = helpMap.getHelpTopic("/fly");
90 HelpTopic powertool = helpMap.getHelpTopic("/powertool");
91 HelpTopic setwarp = helpMap.getHelpTopic("/setwarp");
92 HelpTopic delwarp = helpMap.getHelpTopic("/delwarp");
93 HelpTopic warp = helpMap.getHelpTopic("/warp");
94 HelpTopic tpall = helpMap.getHelpTopic("/tpall");
95 HelpTopic feed = helpMap.getHelpTopic("/feed");
96 HelpTopic heal = helpMap.getHelpTopic("/heal");
97 HelpTopic enchantunsafe = helpMap.getHelpTopic("/enchantunsafe");
98 HelpTopic sudo = helpMap.getHelpTopic("/sudo");
99 HelpTopic top = helpMap.getHelpTopic("/top");
100 HelpTopic gma = helpMap.getHelpTopic("/gma");
101 HelpTopic gms = helpMap.getHelpTopic("/gms");
102 HelpTopic gmc = helpMap.getHelpTopic("/gmc");
103 HelpTopic gmsp = helpMap.getHelpTopic("/gmsp");
104 HelpTopic tempban = helpMap.getHelpTopic("/tempban");
105 HelpTopic pardon = helpMap.getHelpTopic("/pardon-tempban");
106 HelpTopic rename = helpMap.getHelpTopic("/rename");
107 Collection<HelpTopic> topics = new ArrayList<HelpTopic>();
108 topics.add(fly);
109 topics.add(powertool);
110 topics.add(setwarp);
111 topics.add(delwarp);
112 topics.add(warp);
113 topics.add(tpall);
114 topics.add(feed);
115 topics.add(heal);
116 topics.add(enchantunsafe);
117 topics.add(sudo);
118 topics.add(top);
119 topics.add(gma);
120 topics.add(gms);
121 topics.add(gmc);
122 topics.add(gmsp);
123 topics.add(tempban);
124 topics.add(pardon);
125 topics.add(rename);
126 fly.amendTopic("Toggle fly",ChatColor.GOLD + "Description: " + ChatColor.RESET + "Toggle fly\n" + ChatColor.GOLD + "Usage:\n" + ChatColor.RESET + "fly (toggle fly for yourself)\nfly <players> [value] (toggle or set fly for others)");
127 powertool.amendTopic("Create or remove a powertool",ChatColor.GOLD + "Description: " + ChatColor.RESET + "Create or remove a powertool\n" + ChatColor.GOLD + "Usage:\n" + ChatColor.RESET + "powertool (remove a powertool)\npowertool <command> create or edit a powertool\n" + ChatColor.GOLD + "Alias: " + ChatColor.RESET + "pt");
128 helpMap.getHelpTopic("/pt").amendTopic("Create or remove a powertool",ChatColor.GOLD + "Description: " + ChatColor.RESET + "Create or remove a powertool\n" + ChatColor.GOLD + "Usage:\n" + ChatColor.RESET + "powertool (remove a powertool)\npowertool <command> create or edit a powertool");
129 setwarp.amendTopic("Create a warp point", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Create a warp point; can be used with /execute\n" + ChatColor.GOLD + "Usage: " + ChatColor.RESET + "setwarp <name>");
130 delwarp.amendTopic("Delete a warp point", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Delete a warp point\n" + ChatColor.GOLD + "Usage: " + ChatColor.RESET + "delwarp <name>");
131 warp.amendTopic("Warp to a warp point", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Warp to a warp point; can be used with /execute\n" + ChatColor.GOLD + "Usage: " + ChatColor.RESET + "warp <name>");
132 tpall.amendTopic("Teleport all players to your location", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Teleport all players to your location; can be used with /execute\n" + ChatColor.GOLD + "Usage: " + ChatColor.RESET + "tpall");
133 feed.amendTopic("Feed specified players", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Feed specified players\n" + ChatColor.GOLD + "Usage:\n" + ChatColor.RESET + "feed (feed yourself)\nfeed <players> (feed specified players)");
134 heal.amendTopic("Heal specified players", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Heal specified entities\n" + ChatColor.GOLD + "Usage:\n" + ChatColor.RESET + "heal (heal yourself)\nfeed <entities> (heal specified entities)");
135 enchantunsafe.amendTopic("Apply an enchantment without limits", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Apply an enchantment without limits\n" + ChatColor.GOLD + "Usage: " + ChatColor.RESET + "enchantunsafe <enchantment> <level>");
136 sudo.amendTopic("Chat or run a command as another player", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Chat or run a command as another player\n" + ChatColor.GOLD + "Usage: " + ChatColor.RESET + "sudo <player> <text>");
137 top.amendTopic("Teleport to the highest block that isn't air", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Teleport to the highest block that isn't air; can be used with /execute\n" + ChatColor.GOLD + "Usage: " + ChatColor.RESET + "top");
138 gma.amendTopic("Set specified players gamemodes to adventure", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Set specified players gamemodes to adventure\n" + ChatColor.GOLD + "Usage:\n" + ChatColor.RESET + "gma (set your own gamemode to adventure)\ngma <players> (set specified players gamemodes to adventure)");
139 gms.amendTopic("Set specified players gamemodes to survival", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Set specified players gamemodes to survival\n" + ChatColor.GOLD + "Usage:\n" + ChatColor.RESET + "gms (set your own gamemode to survival)\ngms <players> (set specified players gamemodes to survival)");
140 gmc.amendTopic("Set specified players gamemodes to creative", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Set specified players gamemodes to creative\n" + ChatColor.GOLD + "Usage:\n" + ChatColor.RESET + "gmc (set your own gamemode to creative)\ngmc <players> (set specified players gamemodes to creative)");
141 gmsp.amendTopic("Set specified players gamemodes to spectator", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Set specified players gamemodes to spectator\n" + ChatColor.GOLD + "Usage:\n" + ChatColor.RESET + "gmsp (set your own gamemode to spectator)\ngmsp <players> (set specified players gamemodes to spectator)");
142 tempban.amendTopic("Tempoarily ban player", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Tempoarily ban player\n" + ChatColor.GOLD + "Usage: " + ChatColor.RESET + "tempban <player> <seconds> [minutes] [hours] [days] [months] [years]");
143 pardon.amendTopic("Pardon tempbanned player", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Pardon tempbanned player\n" + ChatColor.GOLD + "Usage: " + ChatColor.RESET + "pardon-tempban <player>");
144 rename.amendTopic("Rename item", ChatColor.GOLD + "Description: " + ChatColor.RESET + "Rename item\n" + ChatColor.GOLD + "Usage: " + ChatColor.RESET + "rename <name>");
145 helpMap.addTopic(new IndexHelpTopic("SpaceEssentials","All commands for SpaceEssentials","spaceessentials.help",topics,ChatColor.GRAY + "Below is a list of all SpaceEssentials commands:"));
146 }
147
148 private void registerFly()
149 {
150 //create the arguments hashmap (same as for every other register method)
151 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
152 //register the command using command api (in this case, this is for toggling your own fly status)
153 CommandAPI.getInstance().register("fly", CommandPermission.fromString("spaceessentials.fly.self"), arguments, (sender, args) -> {
154 if(sender instanceof Player)
155 {
156 Player senderP = (Player)sender; //make it so I don't have to do ((Player)sender). for each method I call
157 senderP.setAllowFlight(!senderP.getAllowFlight()); //sets whether they are allowed to fly to the opposite of whatever it currently is
158 senderP.setFlying(senderP.getAllowFlight()); //makes it so they stop flying if they aren't allowed to fly, and stop falling if they are
159 senderP.sendMessage(ChatColor.GOLD + "Fly set to " + senderP.getAllowFlight()); //tells them what it was set to
160 }
161 else
162 {
163 CommandAPI.fail("When run with no arguments, /fly must be run by a player");
164 }
165 });
166 //adds an entity selector argument, accepting players only
167 arguments.put("players", new EntitySelectorArgument(EntitySelector.MANY_PLAYERS));
168 //registers the command for toggling a group of players fly states
169 CommandAPI.getInstance().register("fly", CommandPermission.fromString("spaceessentials.fly.others"), arguments, (sender, args) -> {
170 Collection<Player> players = (Collection<Player>)args[0];
171 players.forEach((player) -> {
172 player.setAllowFlight(!player.getAllowFlight());
173 player.setFlying(player.getAllowFlight());
174 player.sendMessage(ChatColor.GOLD + "Fly set to " + player.getAllowFlight());
175 sender.sendMessage(ChatColor.GOLD + "Fly for " + player.getName() + " set to " + player.getAllowFlight());
176 });
177 });
178 //adds a boolean argument for what to set the fly states of selected players to
179 arguments.put("value", new BooleanArgument());
180 //registers the command for setting a group of players fly status to a specified state
181 CommandAPI.getInstance().register("fly", CommandPermission.fromString("spaceessentials.fly.others"), arguments, (sender, args) -> {
182 Collection<Player> players = (Collection<Player>)args[0];
183 players.forEach((player) -> {
184 player.setAllowFlight((boolean)args[1]);
185 player.setFlying((boolean)args[1]);
186 player.sendMessage(ChatColor.GOLD + "Fly set to " + (boolean)args[1]);
187 sender.sendMessage(ChatColor.GOLD + "Fly for " + player.getName() + " set to " + player.getAllowFlight());
188 });
189 });
190 }
191
192 @EventHandler
193 public void playerInteractEvent(PlayerInteractEvent event)
194 {
195 //if it is a right click
196 if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
197 {
198 ItemStack item = event.getItem();
199 //if the item the player is holding isn't null
200 if(!(item == null))
201 {
202 //gets the nbt from the held item
203 NBTItem itemNbt = new NBTItem(item);
204 //if it has the key "powertool"
205 if(itemNbt.hasKey("powertool"))
206 {
207 if(event.getPlayer().hasPermission("spaceessentials.powertool.use"))
208 {
209 //runs the command contained and cancels the event
210 event.getPlayer().chat(itemNbt.getString("powertool"));
211 event.setCancelled(true);
212 }
213 else
214 {
215 event.getPlayer().sendMessage(ChatColor.RED + "You do not have the permission to use powertools");
216 }
217 }
218 }
219 }
220 }
221
222 private void registerPowertool()
223 {
224 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
225 //registers command for removing a command from an item
226 CommandAPI.getInstance().register("powertool", CommandPermission.fromString("spaceessentials.powertool"), new String[] {"pt"}, arguments, (sender,args) -> {
227 if(sender instanceof Player)
228 {
229 Player senderP = (Player)sender;
230 ItemStack disabling = senderP.getInventory().getItemInMainHand();
231 //if they are holding air or something
232 if(disabling == null || disabling.getItemMeta() == null)
233 {
234 CommandAPI.fail("/powertool can only be run when holding an item!");
235 }
236 else
237 {
238 NBTItem disablingNbt = new NBTItem(disabling);
239 if(disablingNbt.hasKey("powertool"))
240 {
241 disablingNbt.removeKey("powertool");
242 senderP.getInventory().setItemInMainHand(disablingNbt.getItem());
243 senderP.updateInventory();
244 sender.sendMessage(ChatColor.GOLD + "Command removed from item");
245 }
246 else
247 {
248 CommandAPI.fail("Item already wasn't a powertool!");
249 }
250 }
251 }
252 else
253 {
254 CommandAPI.fail("/powertool can only be run by a player!");
255 }
256 });
257 //adds a greedy string argument (an argument that essentially turns the rest of the arguments entered into being processed as one string)
258 arguments.put("command",new GreedyStringArgument());
259 //registers the command for setting the command attached to an item
260 CommandAPI.getInstance().register("powertool", CommandPermission.fromString("spaceessentials.powertool"), new String[] {"pt"}, arguments, (sender,args) -> {
261 if(sender instanceof Player)
262 {
263 Player senderP = (Player)sender;
264 ItemStack powertool = senderP.getInventory().getItemInMainHand();
265 if(powertool == null || powertool.getItemMeta() == null)
266 {
267 CommandAPI.fail("/powertool can only be run when holding an item!");
268 }
269 else
270 {
271 NBTItem powertoolNbt = new NBTItem(powertool);
272 powertoolNbt.setString("powertool", (String)args[0]);
273 senderP.getInventory().setItemInMainHand(powertoolNbt.getItem());
274 senderP.updateInventory();
275 senderP.sendMessage(ChatColor.GOLD + "Item command set to " + (String)args[0]);
276 }
277 }
278 else
279 {
280 CommandAPI.fail("/powertool can only be run by a player!");
281 }
282 });
283 }
284
285 private void registerSetwarp()
286 {
287 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
288 arguments.put("name", new StringArgument());
289 CommandAPI.getInstance().register("setwarp", CommandPermission.fromString("spaceessentials.warp.setwarp"), arguments, (sender, args) -> {
290 if(sender instanceof Player || sender instanceof ProxiedCommandSender)
291 {
292 FileConfiguration warps = YamlConfiguration.loadConfiguration(new File(getDataFolder(),"warps.yml"));
293 List<String> warpNames = (List<String>)warps.getList("names", new ArrayList<String>());
294 List<Location> warpLocations = (List<Location>)warps.getList("locations",new ArrayList<Location>());
295 if(warpNames.contains((String)args[0]))
296 {
297 CommandAPI.fail("warp " + (String)args[0] + " already exists!");
298 return;
299 }
300 if(sender instanceof ProxiedCommandSender)
301 {
302 CommandSender proxy = ((ProxiedCommandSender) sender).getCallee();
303 if(proxy instanceof Entity)
304 {
305 warpLocations.add(((Entity) proxy).getLocation());
306 }
307 else
308 {
309 CommandAPI.fail("/setwarp can only be run by a player or entity (through /execute)!");
310 return;
311 }
312 }
313 else
314 {
315 warpLocations.add(((Player) sender).getLocation());
316 }
317 warpNames.add((String)args[0]);
318 warps.set("names", warpNames);
319 warps.set("locations", warpLocations);
320 try
321 {
322 warps.save(new File(getDataFolder(),"warps.yml"));
323 sender.sendMessage(ChatColor.GOLD + "Created warp " + (String)args[0]);
324 }
325 catch (IOException e)
326 {
327 e.printStackTrace();
328 }
329 }
330 else
331 {
332 CommandAPI.fail("/setwarp can only be run by a player or entity (through /execute)!");
333 }
334 });
335 }
336
337 private void registerDelwarp()
338 {
339 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
340 arguments.put("warp", new DynamicSuggestedStringArgument(() -> {
341 FileConfiguration warps = YamlConfiguration.loadConfiguration(new File(getDataFolder(),"warps.yml"));
342 List<String> warpNames = (List<String>)warps.getList("names", new ArrayList<String>());
343 String[] warpNamesArray = new String[warpNames.size()];
344 for(int i = 0; i < warpNames.size(); i ++)
345 {
346 warpNamesArray[i] = warpNames.get(i);
347 }
348 return warpNamesArray;}));
349 CommandAPI.getInstance().register("delwarp", CommandPermission.fromString("spaceessentials.warp.delwarp"), arguments, (sender, args) -> {
350 FileConfiguration warps = YamlConfiguration.loadConfiguration(new File(getDataFolder(),"warps.yml"));
351 List<String> warpNames = (List<String>)warps.getList("names", new ArrayList<String>());
352 if(warpNames.contains(args[0]))
353 {
354 int index = warpNames.indexOf(args[0]);
355 warpNames.remove(index);
356 List<Location> warpLocations = (List<Location>)warps.getList("locations",new ArrayList<Location>());
357 warpLocations.remove(index);
358 warps.set("names", warpNames);
359 warps.set("locations", warpLocations);
360 try
361 {
362 warps.save(new File(getDataFolder(),"warps.yml"));
363 sender.sendMessage(ChatColor.GOLD + "Deleted warp " + (String)args[0]);
364 }
365 catch (IOException e)
366 {
367 e.printStackTrace();
368 }
369 }
370 else
371 {
372 CommandAPI.fail(ChatColor.RED + "Warp " + (String)args[0] + " does not exist!");
373 }
374 });
375 }
376
377 private void registerWarp()
378 {
379 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
380 arguments.put("warp", new DynamicSuggestedStringArgument(() -> {
381 FileConfiguration warps = YamlConfiguration.loadConfiguration(new File(getDataFolder(),"warps.yml"));
382 List<String> warpNames = (List<String>)warps.getList("names", new ArrayList<String>());
383 String[] warpNamesArray = new String[warpNames.size()];
384 for(int i = 0; i < warpNames.size(); i ++)
385 {
386 warpNamesArray[i] = warpNames.get(i);
387 }
388 return warpNamesArray;}));
389 CommandAPI.getInstance().register("warp", CommandPermission.fromString("spaceessentials.warp.warp"), arguments, (sender, args) -> {
390 CommandSender proxy = sender;
391 if(sender instanceof ProxiedCommandSender)
392 {
393 if(((ProxiedCommandSender) sender).getCallee() instanceof Entity)
394 {
395 proxy = ((ProxiedCommandSender) sender).getCallee();
396 }
397 else
398 {
399 CommandAPI.fail("/warp can only be run by a player or entity (through /execute)!");
400 return;
401 }
402 }
403 else if(!(sender instanceof Player))
404 {
405 CommandAPI.fail("/warp can only be run by a player or entity (through /execute)!");
406 return;
407 }
408 FileConfiguration warps = YamlConfiguration.loadConfiguration(new File(getDataFolder(),"warps.yml"));
409 List<String> warpNames = (List<String>)warps.getList("names", new ArrayList<String>());
410 if(warpNames.contains(args[0]))
411 {
412 Entity senderE = (Entity)proxy;
413 List<Location> warpLocations = (List<Location>)warps.getList("locations",new ArrayList<Location>());
414 senderE.teleport(warpLocations.get(warpNames.indexOf(args[0])), TeleportCause.PLUGIN);
415 proxy.sendMessage(ChatColor.GOLD + "Warped to " + (String)args[0]);
416 }
417 else
418 {
419 CommandAPI.fail("Warp " + (String)args[0] + " does not exist!");
420 }
421 });
422 }
423
424 private void registerTpall()
425 {
426 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
427 CommandAPI.getInstance().register("tpall", CommandPermission.fromString("spaceessentials.tpall"), arguments, (sender, args) -> {
428 if(sender instanceof Player || sender instanceof ProxiedCommandSender)
429 {
430 Entity teleportTo = null;
431 if(sender instanceof ProxiedCommandSender)
432 {
433 if(((ProxiedCommandSender) sender).getCallee() instanceof Entity)
434 {
435 teleportTo = (Entity)((ProxiedCommandSender) sender).getCallee();
436 }
437 else
438 {
439 CommandAPI.fail("/tpall can only be run by a player or entity (through /execute)!");
440 return;
441 }
442 }
443 else
444 {
445 teleportTo = (Player)sender;
446 }
447 for(Player player : Bukkit.getOnlinePlayers())
448 {
449 if(player != sender)
450 {
451 player.teleport(teleportTo,TeleportCause.COMMAND);
452 }
453 }
454 sender.sendMessage(ChatColor.GOLD + "Teleported all players to your location");
455 }
456 else
457 {
458 CommandAPI.fail("/tpall can only be run by a player or entity (through /execute)!");
459 }
460 });
461 }
462
463 private void registerFeed()
464 {
465 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
466 CommandAPI.getInstance().register("feed", CommandPermission.fromString("spaceessentials.feed.self"), arguments, (sender, args) -> {
467 if(sender instanceof Player)
468 {
469 ((Player) sender).setFoodLevel(20);
470 ((Player) sender).setSaturation(5f);
471 sender.sendMessage(ChatColor.GOLD + "You have been fed!");
472 }
473 else
474 {
475 CommandAPI.fail("/feed with no players specified can only be run by a player!");
476 }
477 });
478 arguments.put("players", new EntitySelectorArgument(EntitySelector.MANY_PLAYERS));
479 CommandAPI.getInstance().register("feed", CommandPermission.fromString("spaceessentials.feed.others"), arguments, (sender, args) -> {
480 Collection<Player> players = (Collection<Player>)args[0];
481 players.forEach((player) -> {
482 player.setFoodLevel(20);
483 player.setSaturation(5f);
484 player.sendMessage(ChatColor.GOLD + "You have been fed!");
485 sender.sendMessage(ChatColor.GOLD + "Fed " + player.getName() + "!");
486 });
487 });
488 }
489
490 private void registerHeal()
491 {
492 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
493 CommandAPI.getInstance().register("heal", CommandPermission.fromString("spaceessentials.heal.self"), arguments, (sender, args) -> {
494 if(sender instanceof Player)
495 {
496 ((Player) sender).setHealth(20);
497 sender.sendMessage(ChatColor.GOLD + "You have been healed!");
498 }
499 else
500 {
501 CommandAPI.fail("/heal with no players specified can only be run by a player!");
502 }
503 });
504 arguments.put("entities", new EntitySelectorArgument(EntitySelector.MANY_ENTITIES));
505 CommandAPI.getInstance().register("heal", CommandPermission.fromString("spaceessentials.heal.others"), arguments, (sender, args) -> {
506 Collection<Entity> players = (Collection<Entity>)args[0];
507 players.forEach((player) -> {
508 if(player instanceof LivingEntity)
509 {
510 ((LivingEntity) player).setHealth(((LivingEntity) player).getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
511 player.sendMessage(ChatColor.GOLD + "You have been healed!");
512 sender.sendMessage(ChatColor.GOLD + "Healed " + player.getName() + "!");
513 }
514 });
515 });
516 }
517
518 private void registerEnchantUnsafe()
519 {
520 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
521 arguments.put("enchantment", new EnchantmentArgument());
522 arguments.put("level", new IntegerArgument());
523 CommandAPI.getInstance().register("enchantunsafe", CommandPermission.fromString("spaceessentials.enchantunsafe"), arguments, (sender,args) -> {
524 if(sender instanceof Player)
525 {
526 ItemStack enchanting = ((Player) sender).getInventory().getItemInMainHand();
527 if(enchanting == null || enchanting.getItemMeta() == null)
528 {
529 CommandAPI.fail("/enchantunsafe must be used with an item in hand");
530 }
531 else
532 {
533 int level = (int)args[1];
534 if(level < 0)
535 {
536 CommandAPI.fail("level must be at least 0");
537 }
538 else
539 {
540 enchanting.addUnsafeEnchantment((Enchantment)args[0], level);
541 }
542 }
543 }
544 else
545 {
546 CommandAPI.fail("/enchantunsafe can only be run by a player");
547 }
548 });
549 }
550
551 private void registerSudo()
552 {
553 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
554 arguments.put("player", new EntitySelectorArgument(EntitySelector.ONE_PLAYER));
555 arguments.put("command", new GreedyStringArgument());
556 CommandAPI.getInstance().register("sudo", CommandPermission.fromString("spaceessentials.sudo"), arguments, (sender,args) -> {
557 Player player = (Player)args[0];
558 if(player.hasPermission("spaceessentials.exempt.sudo"))
559 {
560 CommandAPI.fail("Player " + player.getName() + " is exempt from sudo");
561 }
562 else
563 {
564 player.chat((String)args[1]);
565 }
566 });
567 }
568
569 private void registerTop()
570 {
571 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
572 CommandAPI.getInstance().register("top", CommandPermission.fromString("spaceessentials.top"), arguments, (sender, args) -> {
573 if(sender instanceof Player)
574 {
575 Player senderP = (Player) sender;
576 Location senderL = senderP.getLocation().clone();
577 senderL.setY(senderP.getWorld().getHighestBlockYAt(senderL));
578 senderP.teleport(senderL, TeleportCause.COMMAND);
579 }
580 else if(sender instanceof ProxiedCommandSender && ((ProxiedCommandSender) sender).getCallee() instanceof Entity)
581 {
582 Entity senderE = (Entity)((ProxiedCommandSender) sender).getCallee();
583 Location senderL = senderE.getLocation().clone();
584 senderL.setY(senderE.getWorld().getHighestBlockYAt(senderL));
585 senderE.teleport(senderL, TeleportCause.COMMAND);
586 }
587 else
588 {
589 CommandAPI.fail(ChatColor.RED + "/top can only be run by a player or entity (through /execute)!");
590 }
591 });
592 }
593
594 private void registerGma()
595 {
596 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
597 CommandAPI.getInstance().register("gma", CommandPermission.fromString("spaceessentials.gma.self"), arguments, (sender,args) -> {
598 if(sender instanceof Player)
599 {
600 ((Player) sender).setGameMode(GameMode.ADVENTURE);
601 }
602 else
603 {
604 CommandAPI.fail("/gma with no players specified can only be run by a player!");
605 }
606 });
607 arguments.put("players", new EntitySelectorArgument(EntitySelector.MANY_PLAYERS));
608 CommandAPI.getInstance().register("gma", CommandPermission.fromString("spaceessentials.gma.others"), arguments, (sender, args) -> {
609 Collection<Player> players = (Collection<Player>)args[0];
610 players.forEach((player) -> {
611 player.setGameMode(GameMode.ADVENTURE);
612 sender.sendMessage(ChatColor.GOLD + "Gamemode for player " + player.getName() + " set to adventure!");
613 });
614 });
615 }
616
617 private void registerGms()
618 {
619 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
620 CommandAPI.getInstance().register("gms", CommandPermission.fromString("spaceessentials.gms.self"), arguments, (sender,args) -> {
621 if(sender instanceof Player)
622 {
623 ((Player) sender).setGameMode(GameMode.SURVIVAL);
624 }
625 else
626 {
627 CommandAPI.fail("/gms with no players specified can only be run by a player!");
628 }
629 });
630 arguments.put("players", new EntitySelectorArgument(EntitySelector.MANY_PLAYERS));
631 CommandAPI.getInstance().register("gms", CommandPermission.fromString("spaceessentials.gms.others"), arguments, (sender, args) -> {
632 Collection<Player> players = (Collection<Player>)args[0];
633 players.forEach((player) -> {
634 player.setGameMode(GameMode.SURVIVAL);
635 sender.sendMessage(ChatColor.GOLD + "Gamemode for player " + player.getName() + " set to survival!");
636 });
637 });
638 }
639
640 private void registerGmc()
641 {
642 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
643 CommandAPI.getInstance().register("gmc", CommandPermission.fromString("spaceessentials.gmc.self"), arguments, (sender,args) -> {
644 if(sender instanceof Player)
645 {
646 ((Player) sender).setGameMode(GameMode.CREATIVE);
647 }
648 else
649 {
650 CommandAPI.fail("/gmc with no players specified can only be run by a player!");
651 }
652 });
653 arguments.put("players", new EntitySelectorArgument(EntitySelector.MANY_PLAYERS));
654 CommandAPI.getInstance().register("gmc", CommandPermission.fromString("spaceessentials.gmc.others"), arguments, (sender, args) -> {
655 Collection<Player> players = (Collection<Player>)args[0];
656 players.forEach((player) -> {
657 player.setGameMode(GameMode.CREATIVE);
658 sender.sendMessage(ChatColor.GOLD + "Gamemode for player " + player.getName() + " set to creative!");
659 });
660 });
661 }
662
663 private void registerGmsp()
664 {
665 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
666 CommandAPI.getInstance().register("gmsp", CommandPermission.fromString("spaceessentials.gmsp.self"), arguments, (sender,args) -> {
667 if(sender instanceof Player)
668 {
669 ((Player) sender).setGameMode(GameMode.SPECTATOR);
670 }
671 else
672 {
673 CommandAPI.fail("/gmsp with no players specified can only be run by a player!");
674 }
675 });
676 arguments.put("players", new EntitySelectorArgument(EntitySelector.MANY_PLAYERS));
677 CommandAPI.getInstance().register("gmsp", CommandPermission.fromString("spaceessentials.gmsp.others"), arguments, (sender, args) -> {
678 Collection<Player> players = (Collection<Player>)args[0];
679 players.forEach((player) -> {
680 player.setGameMode(GameMode.SPECTATOR);
681 sender.sendMessage(ChatColor.GOLD + "Gamemode for player " + player.getName() + " set to spectator!");
682 });
683 });
684 }
685
686 private void registerTempban()
687 {
688 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
689 registerTempbanHelp(arguments);
690 arguments.put("player", new DynamicSuggestedStringArgument(() -> {
691 Collection<? extends Player> players = Bukkit.getOnlinePlayers();
692 String[] names = new String[players.size()];
693 Iterator<? extends Player> iterator = players.iterator();
694 for(int i = 0; iterator.hasNext(); i ++)
695 {
696 names[i] = iterator.next().getName();
697 }
698 return names;}));
699 registerTempbanHelp(arguments);
700 arguments.put("seconds", new IntegerArgument());
701 registerTempban(arguments);
702 arguments.put("minutes", new IntegerArgument());
703 registerTempban(arguments);
704 arguments.put("hours", new IntegerArgument());
705 registerTempban(arguments);
706 arguments.put("days", new IntegerArgument());
707 registerTempban(arguments);
708 arguments.put("months", new IntegerArgument());
709 registerTempban(arguments);
710 arguments.put("years", new IntegerArgument());
711 registerTempban(arguments);
712 }
713
714 private void registerPardon()
715 {
716 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
717 arguments.put("player", new StringArgument());
718 CommandAPI.getInstance().register("pardon-tempban", CommandPermission.fromString("spaceessentials.pardon"), arguments, (sender, args) -> {
719 File tempbannedFile = new File(getDataFolder(), "tempbanned_players.yml");
720 FileConfiguration reader = YamlConfiguration.loadConfiguration(tempbannedFile);
721 OfflinePlayer p = Bukkit.getOfflinePlayer((String)args[0]);
722 if(reader.contains(p.getUniqueId().toString()))
723 {
724 reader.set(p.getUniqueId().toString(), null);
725 try
726 {
727 reader.save(tempbannedFile);
728 }
729 catch (Exception e)
730 {
731 e.printStackTrace();
732 }
733 sender.sendMessage(ChatColor.GOLD + "Pardoned player " + (String)args[0]);
734 }
735 else
736 {
737 CommandAPI.fail("Player " + (String)args[0] + " was not tempbanned, or has changed their name");
738 }
739 });
740 }
741
742 private void registerTempbanHelp(LinkedHashMap<String, Argument> arguments)
743 {
744 CommandAPI.getInstance().register("tempban", CommandPermission.fromString("spaceessentials.tempban"), arguments, (sender,args) -> {
745 CommandAPI.fail("Usage: /tempban <player> <seconds> [minutes] [hours] [days] [months] [years]");
746 });
747 }
748
749 private void registerTempban(LinkedHashMap<String, Argument> arguments)
750 {
751 CommandAPI.getInstance().register("tempban", CommandPermission.fromString("spaceessentials.tempban"), arguments, (sender,args) -> {
752 Player p = Bukkit.getPlayerExact((String)args[0]);
753 String uuid = "";
754 if(p != null)
755 {
756 if(p.hasPermission("spaceessentials.exempt.tempban")) CommandAPI.fail("Player " + p.getName() + " is exempt from tempban");
757 else uuid = p.getUniqueId().toString();
758 }
759 else uuid = Bukkit.getOfflinePlayer((String)args[0]).getUniqueId().toString();
760 Calendar time = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
761 time.add(Calendar.SECOND, (int)args[1]);
762 if(args.length > 2) time.add(Calendar.MINUTE, (int)args[2]);
763 if(args.length > 3) time.add(Calendar.HOUR, (int)args[3]);
764 if(args.length > 4) time.add(Calendar.DATE, (int)args[4]);
765 if(args.length > 5) time.add(Calendar.MONTH, (int)args[5]);
766 if(args.length == 7) time.add(Calendar.YEAR, (int)args[6]);
767 long dateLong = time.getTime().getTime();
768 File tempbannedFile = new File(getDataFolder(), "tempbanned_players.yml");
769 FileConfiguration reader = YamlConfiguration.loadConfiguration(tempbannedFile);
770 reader.set(uuid,dateLong);
771 try
772 {
773 reader.save(tempbannedFile);
774 }
775 catch (Exception e)
776 {
777 e.printStackTrace();
778 }
779 SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss aa, zzzz");
780 df.setTimeZone(TimeZone.getDefault());
781 sender.sendMessage(ChatColor.GOLD + "Banned player " + (String)args[0] + " until " + df.format(time.getTime()));
782 df.setTimeZone(TimeZone.getTimeZone("GMT"));
783 if(p != null)
784 {
785 p.kickPlayer("You are tempoarily banned from this server.\nBanned until " + df.format(time.getTime()));
786 }
787 });
788 }
789
790 @EventHandler
791 public void playerJoinEvent(PlayerJoinEvent event)
792 {
793 if(event.getPlayer().hasPermission("spaceessentials.exempt.tempban")) return;
794 File tempbannedFile = new File(getDataFolder(), "tempbanned_players.yml");
795 FileConfiguration reader = YamlConfiguration.loadConfiguration(tempbannedFile);
796 if(reader.contains(event.getPlayer().getUniqueId().toString()))
797 {
798 Date currentTime = new Date();
799 Date endTime = new Date(reader.getLong(event.getPlayer().getUniqueId().toString()));
800 if(currentTime.after(endTime))
801 {
802 reader.set(event.getPlayer().getUniqueId().toString(), null);
803 try
804 {
805 reader.save(tempbannedFile);
806 }
807 catch(Exception e)
808 {
809 e.printStackTrace();
810 }
811 }
812 else
813 {
814 SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss aa, zzzz");
815 df.setTimeZone(TimeZone.getTimeZone("GMT"));
816 event.getPlayer().kickPlayer("You are tempoarily banned from this server.\nBanned until " + df.format(endTime));
817 }
818 }
819 }
820
821 private void registerRename()
822 {
823 LinkedHashMap<String, Argument> arguments = new LinkedHashMap();
824 arguments.put("name", new GreedyStringArgument());
825 CommandAPI.getInstance().register("rename", CommandPermission.fromString("spaceessentials.rename"), arguments, (sender,args) -> {
826 if(sender instanceof Player)
827 {
828 Player senderP = (Player)sender;
829 ItemStack renaming = senderP.getInventory().getItemInMainHand();
830 if(renaming == null || renaming.getItemMeta() == null)
831 {
832 CommandAPI.fail("must have an item in hand when using /rename!");
833 }
834 else
835 {
836 ItemMeta renamingMeta = renaming.getItemMeta();
837 renamingMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', (String)args[0]));
838 renaming.setItemMeta(renamingMeta);
839 senderP.getInventory().setItemInMainHand(renaming);
840 senderP.updateInventory();
841 }
842 }
843 else
844 {
845 CommandAPI.fail("/rename can only be run by a player!");
846 }
847 });
848 }
849}