Skip to content

Commit 1cb2392

Browse files
committed
Added CheckPlayerAnchorCommand + message colors
1 parent e5770e4 commit 1cb2392

File tree

7 files changed

+109
-14
lines changed

7 files changed

+109
-14
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package me.fadingfog.anchorfix.commands;
2+
3+
import me.fadingfog.anchorfix.util.AnchorStorage;
4+
import net.minecraft.util.org.apache.commons.lang3.StringUtils;
5+
import org.bukkit.*;
6+
import org.bukkit.entity.Player;
7+
8+
import java.text.MessageFormat;
9+
import java.util.*;
10+
11+
@SuppressWarnings("unchecked")
12+
public class CheckPlayerAnchorCommand extends SubCommand {
13+
private final AnchorStorage anchorStorage = AnchorStorage.getInstance();
14+
private String resultMessage;
15+
16+
@Override
17+
public String getName() {
18+
return "check";
19+
}
20+
21+
@Override
22+
public String getResultMessage() {
23+
return resultMessage;
24+
}
25+
26+
@Override
27+
public void perform(Player player, String[] args) {
28+
29+
if (args.length > 0) {
30+
String target = args[0];
31+
Map<String,List<Map<String,String>>> playerAnchors = anchorStorage.getPlayerAnchors(target);
32+
33+
resultMessage = ChatColor.DARK_GREEN + MessageFormat.format(
34+
"{0} has: {1} personal, {2} passive anchors",
35+
target,
36+
playerAnchors.get("personal") != null ? playerAnchors.get("personal").size() : 0,
37+
playerAnchors.get("passive") != null ? playerAnchors.get("passive").size() : 0
38+
);
39+
40+
if (args.length > 1 && Objects.equals(args[1], "-i")) {
41+
Map<String, List<Collection<String>>> anchorListPerType = new HashMap<>();
42+
43+
for (Map.Entry<String,List<Map<String,String>>> pair : playerAnchors.entrySet()) { // for each type of anchor
44+
List<Collection<String>> anchorList = new ArrayList<>();
45+
46+
for (Map<String, String> anchorInfo : pair.getValue()) { // for each anchor in map
47+
anchorList.add(anchorInfo.values());
48+
}
49+
50+
anchorListPerType.put(pair.getKey(), anchorList);
51+
}
52+
53+
resultMessage += MessageFormat.format(
54+
"\nPersonal: {0}\nPassive: {1}",
55+
StringUtils.join(anchorListPerType.get("personal"), ", "),
56+
StringUtils.join(anchorListPerType.get("passive"), ", ")
57+
);
58+
}
59+
} else {
60+
resultMessage = ChatColor.RED + "You didn't provide the name of the target player";
61+
}
62+
63+
64+
// List<Map<String, String>> anchorList = anchorStorage.getAnchorList();
65+
//
66+
// for (Map<String, String> anchorData : anchorList) {
67+
// String worldType = anchorData.get("world");
68+
// int x = Integer.parseInt(anchorData.get("x"));
69+
// int y = Integer.parseInt(anchorData.get("y"));
70+
// int z = Integer.parseInt(anchorData.get("z"));
71+
//
72+
// World world = Bukkit.getWorld(worldType);
73+
// Block block = world.getBlockAt(x, y, z);
74+
// Material material = block.getType();
75+
//
76+
// if (!material.toString().equals("RAILCRAFT_MACHINEALPHA")) {
77+
// Location loc = new Location(world, x, y, z);
78+
// anchorStorage.removeAnchor(loc);
79+
// }
80+
// }
81+
82+
}
83+
}

src/main/java/me/fadingfog/anchorfix/commands/CleanAnchorStorageCommand.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package me.fadingfog.anchorfix.commands;
22

33
import me.fadingfog.anchorfix.util.AnchorStorage;
4-
import org.bukkit.Bukkit;
5-
import org.bukkit.Location;
6-
import org.bukkit.Material;
7-
import org.bukkit.World;
4+
import org.bukkit.*;
85
import org.bukkit.block.Block;
96
import org.bukkit.entity.Player;
107

@@ -21,8 +18,8 @@ public String getName() {
2118
}
2219

2320
@Override
24-
public String getSuccessMessage() {
25-
return "Successfully removed " + COUNTER + " anchor(s) from config";
21+
public String getResultMessage() {
22+
return ChatColor.DARK_GREEN + "Successfully removed " + COUNTER + " anchor(s) from config";
2623
}
2724

2825
@Override

src/main/java/me/fadingfog/anchorfix/commands/CommandManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
import org.bukkit.entity.Player;
77

88
import java.util.ArrayList;
9+
import java.util.Arrays;
910

1011
public class CommandManager implements CommandExecutor {
1112
private final ArrayList<SubCommand> subCommands = new ArrayList<>();
1213

1314
public CommandManager() {
1415
subCommands.add(new RemoveAllNPCCommand());
1516
subCommands.add(new CleanAnchorStorageCommand());
17+
subCommands.add(new CheckPlayerAnchorCommand());
1618
}
1719

1820
@Override
@@ -25,8 +27,8 @@ public boolean onCommand(CommandSender sender, Command command, String s, String
2527
for (int i = 0; i < getSubCommands().size(); i++) {
2628
SubCommand sCommand = getSubCommands().get(i);
2729
if (args[0].equalsIgnoreCase(sCommand.getName())) {
28-
sCommand.perform(player, args);
29-
player.sendMessage(sCommand.getSuccessMessage());
30+
sCommand.perform(player, Arrays.copyOfRange(args, 1, args.length));
31+
player.sendMessage(sCommand.getResultMessage());
3032
return true;
3133
}
3234
}

src/main/java/me/fadingfog/anchorfix/commands/RemoveAllNPCCommand.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.minecraft.server.v1_7_R4.EntityPlayer;
44
import net.minecraft.server.v1_7_R4.WorldServer;
55
import org.bukkit.Bukkit;
6+
import org.bukkit.ChatColor;
67
import org.bukkit.World;
78
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
89
import org.bukkit.entity.Player;
@@ -20,8 +21,8 @@ public String getName() {
2021
}
2122

2223
@Override
23-
public String getSuccessMessage() {
24-
return "Successfully removed " + COUNTER + " npc";
24+
public String getResultMessage() {
25+
return ChatColor.DARK_GREEN + "Successfully removed " + COUNTER + " npc";
2526
}
2627

2728
@Override

src/main/java/me/fadingfog/anchorfix/commands/SubCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public abstract class SubCommand {
66

77
public abstract String getName();
88

9-
public abstract String getSuccessMessage();
9+
public abstract String getResultMessage();
1010

1111
public abstract void perform(Player player, String[] args);
1212

src/main/java/me/fadingfog/anchorfix/util/AnchorStorage.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.*;
1313

1414

15-
@SuppressWarnings({"unchecked", "rawtypes"})
15+
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
1616
public class AnchorStorage {
1717

1818
private static AnchorStorage instance;
@@ -46,7 +46,7 @@ public FileConfiguration getConfig(){
4646
return config;
4747
}
4848

49-
public List<Map<String, String>> getAnchorList() {
49+
public List<Map<String, String>> getAnchorList() { // return info about all anchors
5050
reload();
5151

5252
List<Map<String, String>> anchorList = new ArrayList();
@@ -64,7 +64,7 @@ public List<Map<String, String>> getAnchorList() {
6464
return anchorList;
6565
}
6666

67-
public List<Map<String, String>> getAnchorList(Player player) {
67+
public List<Map<String, String>> getAnchorList(Player player) { // return info about all anchors of player
6868
reload();
6969

7070
List<Map<String, String>> anchorList = new ArrayList();
@@ -82,6 +82,17 @@ public List<Map<String, String>> getAnchorList(Player player) {
8282
return anchorList;
8383
}
8484

85+
public Map<String,List<Map<String,String>>> getPlayerAnchors(String playerName) { // return player anchors by type
86+
reload();
87+
88+
Map<String, Object> playerAnchors = new HashMap<>();
89+
String path = MessageFormat.format("anchor.{0}", playerName);
90+
91+
if (config.isSet(path)) playerAnchors = config.getConfigurationSection(path).getValues(true);
92+
93+
return (Map<String,List<Map<String,String>>>) (Object) playerAnchors;
94+
}
95+
8596
public void setAnchor(Player player, String type, Location location) {
8697
reload();
8798

src/main/resources/plugin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ commands:
1111
usage: |
1212
/<command> clean - remove missing in the world anchors from the config
1313
/<command> remove - remove all npc created by this plugin
14+
/<command> check <player> [-i] - get a list of player anchors; -i for detailed info
1415
permission: anchorfix
1516
permissions:
1617
anchorfix:

0 commit comments

Comments
 (0)