Skip to content

Commit 6b5a41a

Browse files
committed
Initial commit
0 parents  commit 6b5a41a

File tree

12 files changed

+435
-0
lines changed

12 files changed

+435
-0
lines changed

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.ear
17+
*.zip
18+
*.tar.gz
19+
*.rar
20+
21+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22+
hs_err_pid*
23+
*.iml
24+
target/
25+
.idea/
26+
dependency-reduced-pom.xml

pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>me.ifydev</groupId>
8+
<artifactId>CommandScheduler</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.projectlombok</groupId>
14+
<artifactId>lombok</artifactId>
15+
<version>1.16.10</version>
16+
<type>jar</type>
17+
</dependency>
18+
19+
<dependency>
20+
<groupId>org.spigotmc</groupId>
21+
<artifactId>spigot-api</artifactId>
22+
<version>1.12-R0.1-SNAPSHOT</version>
23+
</dependency>
24+
</dependencies>
25+
26+
<properties>
27+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28+
<maven.compiler.source>1.8</maven.compiler.source>
29+
<maven.compiler.target>1.8</maven.compiler.target>
30+
</properties>
31+
32+
<build>
33+
<finalName>${project.artifactId}</finalName>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-compiler-plugin</artifactId>
38+
<version>3.6.0</version>
39+
<configuration>
40+
<source>1.8</source>
41+
<target>1.8</target>
42+
<compilerArgs>
43+
<arg>-parameters</arg>
44+
</compilerArgs>
45+
</configuration>
46+
<executions>
47+
<execution>
48+
<id>compile-project</id>
49+
<phase>compile</phase>
50+
<goals>
51+
<goal>compile</goal>
52+
</goals>
53+
</execution>
54+
</executions>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
</project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package me.ifydev.commandscheduler;
2+
3+
import lombok.Getter;
4+
import me.ifydev.commandscheduler.commands.ScheduleCommand;
5+
import me.ifydev.commandscheduler.commands.ScheduleListCommand;
6+
import me.ifydev.commandscheduler.commands.StopScheduleCommand;
7+
import org.bukkit.Bukkit;
8+
import org.bukkit.configuration.ConfigurationSection;
9+
import org.bukkit.configuration.file.FileConfiguration;
10+
import org.bukkit.plugin.java.JavaPlugin;
11+
12+
import java.io.File;
13+
import java.util.List;
14+
import java.util.Optional;
15+
import java.util.logging.Level;
16+
17+
/**
18+
* @author Innectic
19+
* @since 06/20/2018
20+
*/
21+
public class CommandSchedulerPlugin extends JavaPlugin {
22+
23+
@Getter private Optional<ScheduleManager> scheduleManager = Optional.empty();
24+
25+
@Override
26+
public void onEnable() {
27+
createConfig();
28+
29+
this.scheduleManager = Optional.of(new ScheduleManager());
30+
31+
getCommand("schedule").setExecutor(new ScheduleCommand());
32+
getCommand("unschedule").setExecutor(new StopScheduleCommand());
33+
getCommand("listschedule").setExecutor(new ScheduleListCommand());
34+
35+
getLogger().info("Loading current schedules...");
36+
long start = System.currentTimeMillis();
37+
loadScheduledCommands();
38+
long end = System.currentTimeMillis() - start;
39+
40+
getLogger().info("Loaded scheduled commands in " + (end / 1000) + " seconds.");
41+
}
42+
43+
@Override
44+
public void onDisable() {
45+
Bukkit.getScheduler().cancelTasks(this);
46+
}
47+
48+
private void createConfig() {
49+
try {
50+
if (!getDataFolder().exists()) {
51+
boolean created = getDataFolder().mkdirs();
52+
if (!created) getLogger().log(Level.SEVERE, "Could not create config!");
53+
}
54+
File file = new File(getDataFolder(), "config.yml");
55+
if (!file.exists()) {
56+
getLogger().info("Config.yml not found, creating!");
57+
saveDefaultConfig();
58+
} else {
59+
getLogger().info("Config.yml found, loading!");
60+
}
61+
} catch (Exception e) {
62+
e.printStackTrace();
63+
}
64+
}
65+
66+
private void loadScheduledCommands() {
67+
FileConfiguration config = getConfig();
68+
69+
ConfigurationSection commandSection = config.getConfigurationSection("commands");
70+
if (commandSection == null) return;
71+
72+
commandSection.getKeys(false).forEach(section -> {
73+
if (commandSection.isString(section + ".command") && commandSection.isList(section + ".arguments") && commandSection.isInt(section + ".time")) {
74+
String command = commandSection.getString(section + ".command");
75+
List<String> arguments = commandSection.getStringList(section + ".arguments");
76+
int time = commandSection.getInt(section + ".time");
77+
78+
this.scheduleManager.ifPresent(manager -> manager.startScheduleCommand(section, command, arguments, time));
79+
}
80+
});
81+
}
82+
83+
public static CommandSchedulerPlugin getPlugin() {
84+
return CommandSchedulerPlugin.getPlugin(CommandSchedulerPlugin.class);
85+
}
86+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package me.ifydev.commandscheduler;
2+
3+
/**
4+
* @author Innectic
5+
* @since 06/20/2018
6+
*/
7+
public class Constants {
8+
9+
public static final String PREFIX = "&2&lCommand Scheduler> ";
10+
11+
public static final String PERMISSION_DENIED = PREFIX + "&c&lYou don't have permission for this command!";
12+
public static final String COMMAND_SCHEDULED = PREFIX + "&2&lCommand '<CMD>' scheduled to run every <TIME> seconds.";
13+
public static final String COMMAND_UNSCHEDULED = PREFIX + "&2&lCommand '<CMD>' is unscheduled.";
14+
public static final String NOT_ENOUGH_ARGUMENTS_SCHEDULE = PREFIX + "&2&lNot enough arguments! &e&l/schedule <name> <time_in_seconds> <command> [args...]";
15+
public static final String NOT_ENOUGH_ARGUMENTS_UNSCHEDULE = PREFIX + "&2&lNot enough arguments! &e&l/unschedule <name>";
16+
public static final String TIME_MUST_BE_A_NUMBER = PREFIX + "&2&lTime must be a number!";
17+
public static final String COMMAND_IS_NOT_SCHEDULED = PREFIX + "&2&lCommand is not scheduled!";
18+
public static final String CANNOT_REUSE_NICKNAME = PREFIX + "&2&lCannot reuse nicknames that are in use!";
19+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package me.ifydev.commandscheduler;
2+
3+
import lombok.Getter;
4+
import org.bukkit.Bukkit;
5+
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
/**
11+
* @author Innectic
12+
* @since 06/20/2018
13+
*/
14+
public class ScheduleManager {
15+
16+
private Map<String, Integer> scheduled = new HashMap<>();
17+
@Getter private Map<String, String> schedule = new HashMap<>();
18+
19+
public void startScheduleCommand(String nickname, String command, List<String> arguments, int time) {
20+
int id = Bukkit.getScheduler().runTaskTimer(CommandSchedulerPlugin.getPlugin(), () -> {
21+
String cmd = command + " " + String.join(" ", arguments);
22+
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
23+
}, 0L, time * 20).getTaskId();
24+
25+
scheduled.put(nickname, id);
26+
schedule.put(nickname, command + " " + String.join(" ", arguments));
27+
28+
CommandSchedulerPlugin plugin = CommandSchedulerPlugin.getPlugin();
29+
plugin.getConfig().set("commands." + nickname + ".command", command);
30+
plugin.getConfig().set("commands." + nickname + ".arguments", arguments);
31+
plugin.getConfig().set("commands." + nickname + ".time", time);
32+
33+
try {
34+
plugin.saveConfig();
35+
} catch (Exception e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
40+
public void stopCommand(String nickname) {
41+
Integer id = scheduled.getOrDefault(nickname, null);
42+
43+
if (id == null) return;
44+
Bukkit.getScheduler().cancelTask(id);
45+
schedule.remove(nickname);
46+
47+
CommandSchedulerPlugin.getPlugin().getConfig().set("commands." + nickname, null);
48+
try {
49+
CommandSchedulerPlugin.getPlugin().saveConfig();
50+
} catch (Exception e) {
51+
e.printStackTrace();
52+
}
53+
}
54+
55+
public boolean nicknameUsed(String command) {
56+
return scheduled.containsKey(command);
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package me.ifydev.commandscheduler.commands;
2+
3+
import me.ifydev.commandscheduler.CommandSchedulerPlugin;
4+
import me.ifydev.commandscheduler.Constants;
5+
import me.ifydev.commandscheduler.ScheduleManager;
6+
import me.ifydev.commandscheduler.util.ChatUtil;
7+
import me.ifydev.commandscheduler.util.MiscUtil;
8+
import org.bukkit.command.Command;
9+
import org.bukkit.command.CommandExecutor;
10+
import org.bukkit.command.CommandSender;
11+
12+
import java.util.Arrays;
13+
import java.util.Optional;
14+
15+
/**
16+
* @author Innectic
17+
* @since 06/20/2018
18+
*/
19+
public class ScheduleCommand implements CommandExecutor {
20+
21+
@Override
22+
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
23+
if (!sender.hasPermission("commandscheduler.command.schedule")) {
24+
sender.sendMessage(ChatUtil.convertString(Constants.PERMISSION_DENIED));
25+
return false;
26+
}
27+
28+
if (args.length < 3) {
29+
sender.sendMessage(ChatUtil.convertString(Constants.NOT_ENOUGH_ARGUMENTS_SCHEDULE));
30+
return false;
31+
}
32+
Optional<Integer> timeCheck = MiscUtil.tryInt(args[1]);
33+
if (!timeCheck.isPresent()) {
34+
sender.sendMessage(ChatUtil.convertString(Constants.TIME_MUST_BE_A_NUMBER));
35+
return false;
36+
}
37+
38+
String nickname = args[0];
39+
int time = timeCheck.get();
40+
String command = args[2];
41+
String[] arguments = ChatUtil.getRemainingArgs(3, args);
42+
43+
CommandSchedulerPlugin plugin = CommandSchedulerPlugin.getPlugin();
44+
if (!plugin.getScheduleManager().isPresent()) return false;
45+
46+
ScheduleManager scheduleManager = plugin.getScheduleManager().get();
47+
if (scheduleManager.nicknameUsed(nickname)) {
48+
sender.sendMessage(ChatUtil.convertString(Constants.CANNOT_REUSE_NICKNAME));
49+
return false;
50+
}
51+
52+
scheduleManager.startScheduleCommand(nickname, command, Arrays.asList(arguments), time);
53+
54+
sender.sendMessage(ChatUtil.convertString(Constants.COMMAND_SCHEDULED.replace("<CMD>", command).replace("<TIME>", Integer.toString(time))));
55+
56+
return false;
57+
}
58+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package me.ifydev.commandscheduler.commands;
2+
3+
import me.ifydev.commandscheduler.CommandSchedulerPlugin;
4+
import me.ifydev.commandscheduler.Constants;
5+
import me.ifydev.commandscheduler.util.ChatUtil;
6+
import org.bukkit.ChatColor;
7+
import org.bukkit.command.Command;
8+
import org.bukkit.command.CommandExecutor;
9+
import org.bukkit.command.CommandSender;
10+
11+
/**
12+
* @author Innectic
13+
* @since 06/20/2018
14+
*/
15+
public class ScheduleListCommand implements CommandExecutor {
16+
17+
@Override
18+
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
19+
if (!sender.hasPermission("commandscheduler.command.list")) {
20+
sender.sendMessage(ChatUtil.convertString(Constants.PERMISSION_DENIED));
21+
return false;
22+
}
23+
24+
CommandSchedulerPlugin.getPlugin().getScheduleManager().ifPresent(manager -> {
25+
manager.getSchedule().forEach((nickname, cmd) ->
26+
sender.sendMessage(ChatColor.YELLOW + " > " + ChatColor.DARK_BLUE + "" + ChatColor.BOLD +nickname + ChatColor.WHITE + " - " + ChatColor.GREEN + ChatColor.BOLD + cmd));
27+
});
28+
29+
return false;
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package me.ifydev.commandscheduler.commands;
2+
3+
import me.ifydev.commandscheduler.CommandSchedulerPlugin;
4+
import me.ifydev.commandscheduler.Constants;
5+
import me.ifydev.commandscheduler.util.ChatUtil;
6+
import org.bukkit.command.Command;
7+
import org.bukkit.command.CommandExecutor;
8+
import org.bukkit.command.CommandSender;
9+
10+
/**
11+
* @author Innectic
12+
* @since 06/20/2018
13+
*/
14+
public class StopScheduleCommand implements CommandExecutor {
15+
16+
@Override
17+
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
18+
if (!sender.hasPermission("commandscheduler.command.unschedule")) {
19+
sender.sendMessage(ChatUtil.convertString(Constants.PERMISSION_DENIED));
20+
return false;
21+
}
22+
23+
if (args.length < 1) {
24+
sender.sendMessage(ChatUtil.convertString(Constants.NOT_ENOUGH_ARGUMENTS_UNSCHEDULE));
25+
return false;
26+
}
27+
28+
CommandSchedulerPlugin plugin = CommandSchedulerPlugin.getPlugin();
29+
30+
String nickname = args[0];
31+
if (plugin.getScheduleManager().isPresent() && !plugin.getScheduleManager().get().nicknameUsed(nickname)) {
32+
sender.sendMessage(ChatUtil.convertString(Constants.COMMAND_IS_NOT_SCHEDULED));
33+
return false;
34+
}
35+
36+
plugin.getScheduleManager().get().stopCommand(nickname);
37+
sender.sendMessage(ChatUtil.convertString(Constants.COMMAND_UNSCHEDULED.replace("<CMD>", nickname)));
38+
return false;
39+
}
40+
}

0 commit comments

Comments
 (0)