Skip to content

Commit

Permalink
Code Cleanup, Added comments, bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord Kekz committed May 24, 2019
1 parent 77d04cc commit a0f99d3
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 24 deletions.
11 changes: 11 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="F:/Spiele/Minecraft/Plugin-Dev-Workspace/spigot-api-1.14.1-R0.1-SNAPSHOT.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>mc.quickmaths</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
13 changes: 5 additions & 8 deletions src/de/lordkekz/mc/quickmaths/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
Expand All @@ -17,8 +16,9 @@ public class FileManager {
private static File textsFile;
private static String initializedLang;


public static void setup(QuickmathsPlugin pl) {
// load config
// load config & save (in case defaults need to be copied)
configFile = new File("plugins/Quickmaths", "config.yml");
configFile.getParentFile().mkdirs();
config = YamlConfiguration.loadConfiguration(configFile);
Expand All @@ -31,6 +31,8 @@ public static void setup(QuickmathsPlugin pl) {
config.addDefault("minNumberSize", 0);
config.addDefault("maxNumberCount", 4);
config.addDefault("minNumberCount", 2);
config.options().copyDefaults(true);
saveConfig();

// load texts
textsFile = new File("plugins/Quickmaths", config.getString("language")+".yml");
Expand All @@ -45,10 +47,6 @@ public static void setup(QuickmathsPlugin pl) {
}
texts = YamlConfiguration.loadConfiguration(textsFile);
initializedLang = config.getString("language");

// save config
config.options().copyDefaults(true);
saveConfig();
}

public static void saveConfig() {
Expand All @@ -68,9 +66,8 @@ public static void reloadConfig() {
}

public static void reloadTexts() {
if (!initializedLang.equalsIgnoreCase(config.getString("language"))) {
if (!initializedLang.equalsIgnoreCase(config.getString("language"))) // change texts file if language changed in config
textsFile = new File("plugins/Quickmaths", config.getString("language")+".yml");
}
texts = YamlConfiguration.loadConfiguration(textsFile);
}

Expand Down
27 changes: 12 additions & 15 deletions src/de/lordkekz/mc/quickmaths/QuickmathsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ public void onEnable() {

@Override
public void onDisable() {

// nothing to do
}

@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event) {
if (processor!=null && processor.hasOpenQuestion()) {
if (processor!=null && processor.hasOpenQuestion()) // process chatevent if it could be a response to a question
processor.processChatEvent(event);
}
}

@Override
Expand All @@ -69,7 +68,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, Str
if (processor.isEnabled()) {
sender.sendMessage(ChatColor.YELLOW+FileManager.getTexts().getString("command.alreadyEnabled"));
} else {
processor.schedule(0);
processor.schedule(0); // schedules broadcast cycle
FileManager.getConfig().set("enable", true);
FileManager.saveConfig();
sender.sendMessage(ChatColor.GREEN+FileManager.getTexts().getString("command.enabled"));
Expand All @@ -79,56 +78,54 @@ public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, Str
if (!processor.isEnabled()) {
sender.sendMessage(ChatColor.YELLOW+FileManager.getTexts().getString("command.alreadyDisabled"));
} else {
processor.cancel();
processor.cancel(); // cancels broadcast cycle
FileManager.getConfig().set("enable", false);
FileManager.saveConfig();
sender.sendMessage(ChatColor.GREEN+FileManager.getTexts().getString("command.disabled"));
}
return true;
} else if ((args[0].equalsIgnoreCase("ask"))) {
if (processor.hasOpenQuestion() && !overrideOpenQuestion) {
if (processor.hasOpenQuestion() && !overrideOpenQuestion) { // there is an open question; sender needs to execute the command again to confirm override
sender.sendMessage(ChatColor.RED+FileManager.getTexts().getString("command.hasOpenQuestion"));
overrideOpenQuestion=true;
} else if (args.length==1) {
} else if (args.length==1) { // no custom question/answer given; asking automatic question
overrideOpenQuestion=false;
processor.askAuto();
} else if (args.length>=3) {
} else if (args.length>=3) { // custom question and answer given; parsing (possibly multi-word) arguments
overrideOpenQuestion=false;
System.out.println(Arrays.toString(args));
System.out.println("tsts");
int i=1;
// collect arguments for question
if (args[i].charAt(0)!='"') return false;
StringBuilder qu = new StringBuilder(args[i]).deleteCharAt(0);
if (args[i].charAt(args[i].length()-1)=='"') {
qu.deleteCharAt(qu.length()-1);
i++;
} else for (i++; i<args.length; i++) {
System.out.println(i);
qu.append(' ').append(args[i]);
if (args[i].charAt(args[i].length()-1)=='"') {
qu.deleteCharAt(qu.length()-1);
i++;
break;
}
}
System.out.println(qu);

// collect arguments for answer
if (args[i].charAt(0)!='"') return false;
StringBuilder ans = new StringBuilder(args[i]).deleteCharAt(0);
if (args[i].charAt(args[i].length()-1)=='"') {
ans.deleteCharAt(ans.length()-1);
i++;
} else for (i++; i<args.length; i++) {
System.out.println(i);
ans.append(' ').append(args[i]);
if (args[i].charAt(args[i].length()-1)=='"') {
ans.deleteCharAt(ans.length()-1);
i++;
break;
}
}
System.out.println(ans);
processor.ask(qu.toString().trim(), ans.toString().trim(), 0);

// make processor ask the question
processor.ask(qu.toString().trim(), ans.toString().trim(), 0);
} else {
return false;
}
Expand Down
37 changes: 36 additions & 1 deletion src/de/lordkekz/mc/quickmaths/QuickmathsProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public QuickmathsProcessor(QuickmathsPlugin pl) {
this.pl = pl;
}

/**
* Generates an question-answer pair.
*/
private void makeQuestion() {
Random r = new Random();
int numCount = r.nextInt(FileManager.getConfig().getInt("maxNumberCount")-FileManager.getConfig().getInt("minNumberCount"))+FileManager.getConfig().getInt("minNumberCount");
Expand Down Expand Up @@ -50,16 +53,26 @@ private void makeQuestion() {
solution = Integer.toString(sol);
}

/**
* @return whether there is an question that can be answered
*/
public boolean hasOpenQuestion() {
return question!=null;
}

/**
* @return whether the QuickmathsProcessor will ask questions regularly
*/
public boolean isEnabled() {
return enabled;
}

/**
* Processes an event. If the message is the correct answer to the active question the player will be rewarded.
* @param event the event to be processed
*/
public void processChatEvent(AsyncPlayerChatEvent event) {
if(event.getMessage().trim().equalsIgnoreCase(solution)) {
if(hasOpenQuestion() && event.getMessage().trim().equalsIgnoreCase(solution)) {
question = null;
event.getPlayer().giveExpLevels(FileManager.getConfig().getInt("reward"));

Expand All @@ -74,12 +87,19 @@ public void processChatEvent(AsyncPlayerChatEvent event) {
}
}

/**
* Disables the QuickmathsProcessor.
*/
public void cancel() {
enabled=false;
question=solution=null;
Bukkit.getScheduler().cancelTasks(pl);
}

/**
* Enables the QuickmathsProcessor and schedules regular broadcasts.
* @param delay the delay of the first broadcast (in ticks)
*/
public void schedule(long delay) {
if (enabled) return;
enabled=true;
Expand All @@ -89,6 +109,13 @@ public void schedule(long delay) {
}, delay, FileManager.getConfig().getInt("waitingTime"));
}

/**
* Cancels the regular questions and asks given question.
* After the question is answered or the time is up it re-schedules the regular cycle (if it was scheduled before).
* @param question the qustion
* @param answer the answer
* @param delay the delay before asking
*/
public void ask(String question, String answer, int delay) {
boolean wasEnabled = enabled;
cancel();
Expand All @@ -97,13 +124,21 @@ public void ask(String question, String answer, int delay) {
ask(wasEnabled);
}

/**
* Cancels the regular questions and immediately asks a question.
* After the question is answered or the time is up it re-schedules the regular cycle (if it was scheduled before).
*/
public void askAuto() {
boolean wasEnabled = enabled;
cancel();
makeQuestion();
ask(wasEnabled);
}

/**
* Broadcasts the question. If the time is up before someone correctly answers the question it will also broadcast that.
* @param reschedule whether or not to re-schedule the regular cycle; relevant for manually initiated questions.
*/
private void ask(boolean reschedule) {
askTime = System.currentTimeMillis();
Bukkit.broadcastMessage(
Expand Down

0 comments on commit a0f99d3

Please sign in to comment.