Skip to content

Commit

Permalink
为 mark 增加了 list ,修改了获取消息方法的签名。
Browse files Browse the repository at this point in the history
  • Loading branch information
yueyinqiu committed Jul 2, 2020
1 parent bd7b1d3 commit 7b187e1
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 55 deletions.
11 changes: 11 additions & 0 deletions NWorldPermissions/res/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ messages:
&r&3> Hello, {senderName}&r&3! You can try these commands.
/wp marks add <mark_name> -- To set a mark, then you can use it later.
/wp marks remove <mark_name> -- To remove a mark.
/wp marks list -- To list all the marks.
# Available Arg(s): {senderName}.
add:
without-a-position: |
Expand All @@ -97,6 +98,16 @@ messages:
failed: |
&r&3> Oh, what happened? {senderName}&r&3, a strange power has stopped me removing the mark.
# Available Arg(s): {senderName}, {markName}.
list:
# The following three messages will get jointed together before being sent.
# They will be jointed as "{beginning}{mark1}{separator}{mark2}{separator}...{markX}{ending}".
# The blank characters will be jointed as they are, expect the ones at the beginning of "beginning" and at the end of "ending".
beginning: "&r&3> Well, {senderName}&r&3. There are the marks:\n"
# Available Arg(s): {senderName}.
separator: "\n"
# Available Arg(s): {senderName}.
ending: "\nThat's all."
# Available Arg(s): {senderName}.

reload:
help: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

public class MarksManager
{
Expand All @@ -22,9 +23,10 @@ public class MarksManager

public void reloadConfiguration()
{
rootPlugin.saveResource("marks.yml", false);
configuration = YamlConfiguration.loadConfiguration(new File(
rootPlugin.getDataFolder().getAbsolutePath(), "marks.yml"));
File file = new File(rootPlugin.getDataFolder().getAbsolutePath(), "marks.yml");
if (!file.exists())
rootPlugin.saveResource("marks.yml", false);
configuration = YamlConfiguration.loadConfiguration(file);
}

public void setMark(String name, Location mark) throws IOException
Expand All @@ -43,4 +45,8 @@ public Location getMark(String name)
}
return (Location) oLocation;
}
public Set<String> allMarksName()
{
return configuration.getKeys(false);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package top.nololiyt.worldpermissions;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.omg.CORBA.NVList;
import sun.rmi.runtime.Log;
import top.nololiyt.worldpermissions.entities.DotDividedStringBuilder;
import top.nololiyt.worldpermissions.entities.StringPair;

Expand All @@ -23,32 +24,43 @@ public class MessagesManager

public void reloadConfiguration()
{
rootPlugin.saveResource("messages.yml", false);
configuration = YamlConfiguration.loadConfiguration(new File(
rootPlugin.getDataFolder().getAbsolutePath(), "messages.yml"));
File file = new File(rootPlugin.getDataFolder().getAbsolutePath(), "messages.yml");
if (!file.exists())
rootPlugin.saveResource("messages.yml", false);
configuration = YamlConfiguration.loadConfiguration(file);
}

public void sendMessage(DotDividedStringBuilder node, StringPair[] stringPairs, CommandSender target)
public void sendMessage(StringPair[] stringPairs, CommandSender target, DotDividedStringBuilder node)
{
String key = node.toString();
String result = configuration.getString(key);

if (result == null)
{
rootPlugin.getLogger().severe(
"File 'messages.yml' is corrupted and '" + key
+ "' is missing.");
return;
}

result = result.trim();
sendMessage(stringPairs, target, result);
}

public void sendMessage(StringPair[] stringPairs, CommandSender target, String message)
{
String result = message.trim();
if (result.isEmpty())
return;
result = ChatColor.translateAlternateColorCodes('&',result);
result = ChatColor.translateAlternateColorCodes('&', result);
for (StringPair pair : stringPairs)
{
result = result.replace(pair.getKey(), pair.getValue());
}
target.sendMessage(result);
}

public String getItem(DotDividedStringBuilder node)
{
rootPlugin.getLogger().info(node.toString());
return configuration.getString(node.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public void onEnable() {
messagesManager = new MessagesManager(this);
marksManager = new MarksManager(this);

// messagesManager.reloadConfiguration();
// marksManager.reloadConfiguration();

getCommand("nworldpermissions").setExecutor(new RootCommandExecutor(this));

Bukkit.getPluginManager().registerEvents(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected boolean sendHelp(DotDividedStringBuilder messageKey,
StringPair.senderName(commandSender.getName())
};

rootPlugin.getMessagesManager().sendMessage(messageKey, pairs, commandSender);
rootPlugin.getMessagesManager().sendMessage(pairs, commandSender, messageKey);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private boolean sendHelp(DotDividedStringBuilder messageKey,
};

rootPlugin.getMessagesManager().sendMessage(
messageKey, pairs, commandSender);
pairs, commandSender, messageKey);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package top.nololiyt.worldpermissions.commands.marks;

import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import top.nololiyt.worldpermissions.MarksManager;
import top.nololiyt.worldpermissions.RootPlugin;
import top.nololiyt.worldpermissions.entities.DotDividedStringBuilder;
import top.nololiyt.worldpermissions.entities.StringPair;
import top.nololiyt.worldpermissions.commands.Executor;

import java.io.File;
import java.io.IOException;

public class AddExecutor extends Executor
Expand Down Expand Up @@ -40,7 +38,7 @@ protected boolean run(int layer, RootPlugin rootPlugin, DotDividedStringBuilder
};

rootPlugin.getMessagesManager().sendMessage(
messageKey.append("without-a-position"), pairs,commandSender);
pairs, commandSender, messageKey.append("without-a-position"));
return true;
}

Expand All @@ -58,22 +56,22 @@ protected boolean run(int layer, RootPlugin rootPlugin, DotDividedStringBuilder
if(marksManager.getMark(args[layer]) != null)
{
rootPlugin.getMessagesManager().sendMessage(
messageKey.append("with-occupied-name"), cPairs,sender);
cPairs, sender, messageKey.append("with-occupied-name"));
return true;
}
marksManager.setMark(args[layer],sender.getLocation());


rootPlugin.getMessagesManager().sendMessage(
messageKey.append("completed"), cPairs,sender);
cPairs, sender, messageKey.append("completed"));
return true;
}
catch(IOException ex)
{
ex.printStackTrace();

rootPlugin.getMessagesManager().sendMessage(
messageKey.append("failed"), cPairs,sender);
cPairs, sender, messageKey.append("failed"));
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package top.nololiyt.worldpermissions.commands.marks;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import top.nololiyt.worldpermissions.MarksManager;
import top.nololiyt.worldpermissions.MessagesManager;
import top.nololiyt.worldpermissions.RootPlugin;
import top.nololiyt.worldpermissions.commands.Executor;
import top.nololiyt.worldpermissions.entities.DotDividedStringBuilder;
import top.nololiyt.worldpermissions.entities.StringPair;

import java.io.IOException;

public class ListExecutor extends Executor
{
protected final static String layerName = "list";

@Override
protected String permissionName()
{
return null;
}

@Override
protected String messageKey()
{
return layerName;
}

@Override
protected boolean run(int layer, RootPlugin rootPlugin, DotDividedStringBuilder permission,
DotDividedStringBuilder messageKey, CommandSender commandSender,
String[] args)
{
MessagesManager messagesManager = rootPlugin.getMessagesManager();

StringBuilder message = new StringBuilder();
message.append(messagesManager.getItem(
new DotDividedStringBuilder(messageKey).append("beginning")));

String separator = messagesManager.getItem(
new DotDividedStringBuilder(messageKey).append("separator"));
for (String name : rootPlugin.getMarksManager().allMarksName())
{
message.append(name);
message.append(separator);
}

int ml = message.length();
message.delete(ml - separator.length(), ml);
message.append(messagesManager.getItem(
new DotDividedStringBuilder(messageKey).append("ending")));

messagesManager.sendMessage(new StringPair[]{
StringPair.senderName(commandSender.getName())
}, commandSender, message.toString());
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ protected CommandLayer nextLayer(String arg)
return new AddExecutor();
case "remove":
return new RemoveExecutor();
case "list":
return new ListExecutor();
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package top.nololiyt.worldpermissions.commands.marks;

import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import top.nololiyt.worldpermissions.MarksManager;
import top.nololiyt.worldpermissions.RootPlugin;
import top.nololiyt.worldpermissions.entities.DotDividedStringBuilder;
import top.nololiyt.worldpermissions.entities.StringPair;
import top.nololiyt.worldpermissions.commands.Executor;

import java.io.File;
import java.io.IOException;

public class RemoveExecutor extends Executor
Expand Down Expand Up @@ -46,7 +44,7 @@ protected boolean run(int layer, RootPlugin rootPlugin, DotDividedStringBuilder
if(marksManager.getMark(args[layer]) == null)
{
rootPlugin.getMessagesManager().sendMessage(
messageKey.append("no-such-mark"), cPairs,commandSender);
cPairs, commandSender, messageKey.append("no-such-mark"));
return true;
}

Expand All @@ -57,12 +55,12 @@ protected boolean run(int layer, RootPlugin rootPlugin, DotDividedStringBuilder
ex.printStackTrace();

rootPlugin.getMessagesManager().sendMessage(
messageKey.append("failed"), cPairs,commandSender);
cPairs, commandSender, messageKey.append("failed"));
return true;
}

rootPlugin.getMessagesManager().sendMessage(
messageKey.append("completed"), cPairs,commandSender);
cPairs, commandSender, messageKey.append("completed"));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected boolean run(int layer, RootPlugin rootPlugin, DotDividedStringBuilder
};

rootPlugin.getMessagesManager().sendMessage(
messageKey, pairs,commandSender);
pairs, commandSender, messageKey);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected boolean run(int layer, RootPlugin rootPlugin, DotDividedStringBuilder
};

rootPlugin.getMessagesManager().sendMessage(
messageKey, pairs,commandSender);
pairs, commandSender, messageKey);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected boolean run(int layer, RootPlugin rootPlugin, DotDividedStringBuilder
};

rootPlugin.getMessagesManager().sendMessage(
messageKey, pairs,commandSender);
pairs, commandSender, messageKey);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ protected boolean run(int layer, RootPlugin rootPlugin, DotDividedStringBuilder
rootPlugin.getConfig().getBoolean("offline-players-tracker.record-only"))
{
rootPlugin.getMessagesManager().sendMessage(
messageKey.append("tracker-not-enabled"), basePairs,commandSender);
basePairs, commandSender, messageKey.append("tracker-not-enabled"));
return true;
}

World world = Bukkit.getWorld(args[layer]);
if (world == null)
{
rootPlugin.getMessagesManager().sendMessage(
messageKey.append("no-such-world"), basePairs,commandSender);
basePairs, commandSender, messageKey.append("no-such-world"));
return true;
}

Location location = rootPlugin.getMarksManager().getMark(markName);
if (location == null)
{
rootPlugin.getMessagesManager().sendMessage(
messageKey.append("no-such-mark"), basePairs,commandSender);
basePairs, commandSender, messageKey.append("no-such-mark"));
return true;
}

Expand Down Expand Up @@ -101,7 +101,7 @@ protected boolean run(int layer, RootPlugin rootPlugin, DotDividedStringBuilder
StringPair.senderName(commandSender.getName())
};
rootPlugin.getMessagesManager().sendMessage(
messageKey.append("completed"), cPairs,commandSender);
cPairs, commandSender, messageKey.append("completed"));
return true;
}
}
Loading

0 comments on commit 7b187e1

Please # to comment.