Skip to content

Commit 2fc5b05

Browse files
committed
Fix for default schematic files not being created
Fixes #4
1 parent c2b21b2 commit 2fc5b05

24 files changed

Lines changed: 45 additions & 21 deletions

src/main/java/com/boveybrawlers/TreeRepository/commands/CopyCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
3737
return true;
3838
}
3939

40-
return false;
40+
player.sendMessage(ChatColor.RED + "Usage: /tr copy [code]");
41+
return true;
4142
}
4243

4344
}

src/main/java/com/boveybrawlers/TreeRepository/commands/GroupCommand.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
3838
}
3939

4040
int perPage = 4;
41+
// page argument is at index 2 after group and [name]
4142
Pagination pagination = Pagination.parseArgs(args, 2, perPage, group.getList().size());
43+
if(pagination == null) {
44+
player.sendMessage(ChatColor.RED + "Usage: /tr group [name] (page)");
45+
return true;
46+
}
4247

4348
String page = "";
4449
if(pagination.hasNextPage()) {
@@ -92,7 +97,8 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
9297
return true;
9398
}
9499

95-
return false;
100+
player.sendMessage(ChatColor.RED + "Usage: /tr group [name] (page)");
101+
return true;
96102
}
97103

98104
}

src/main/java/com/boveybrawlers/TreeRepository/commands/ListCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
4141

4242
int perPage = 10;
4343
Pagination pagination = Pagination.parseArgs(args, argIndex, perPage, groups.size());
44+
if(pagination == null) {
45+
player.sendMessage(ChatColor.RED + "Usage: /tr list (page)");
46+
return true;
47+
}
4448

4549
String page = "";
4650
if(pagination.hasNextPage()) {

src/main/java/com/boveybrawlers/TreeRepository/commands/ToolCommand.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,11 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
5858
wand.setItemMeta(meta);
5959

6060
player.getInventory().addItem(wand);
61-
} else {
62-
if(player.hasMetadata(ToolCommand.toolType) && player.hasMetadata(ToolCommand.toolCode)) {
63-
player.removeMetadata(ToolCommand.toolType, this.plugin);
64-
player.removeMetadata(ToolCommand.toolCode, this.plugin);
6561

66-
player.sendMessage(ChatColor.DARK_AQUA + "Tree tool unbound from wooden hoe");
67-
} else {
68-
player.performCommand("treerepo help");
69-
}
62+
return true;
7063
}
7164

65+
player.sendMessage(ChatColor.RED + "Usage: /tr tool [code]");
7266
return true;
7367
}
7468

src/main/java/com/boveybrawlers/TreeRepository/commands/TreeRepoCommand.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
3131
subCommands.put("list", new ListCommand(this.plugin));
3232
subCommands.put("tool", new ToolCommand(this.plugin));
3333

34-
// TODO: Command to build a temporary collection of trees to use in a tool
35-
3634
if(args.length > 0) {
3735
CommandExecutor executor = subCommands.get(args[0].toLowerCase());
3836
if(executor == null) {

src/main/java/com/boveybrawlers/TreeRepository/managers/TreeManager.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.bukkit.configuration.ConfigurationSection;
88
import org.bukkit.configuration.file.FileConfiguration;
99
import org.bukkit.configuration.file.YamlConfiguration;
10-
import org.bukkit.util.FileUtil;
1110

1211
import java.io.File;
1312
import java.io.IOException;
@@ -27,8 +26,10 @@ public class TreeManager {
2726
public TreeManager(TreeRepository plugin) {
2827
this.plugin = plugin;
2928

29+
String schematicsDir = "schematics";
30+
3031
// Try to create the schematic directory when we start
31-
String directory = this.plugin.getDataFolder() + File.separator + "schematics";
32+
String directory = this.plugin.getDataFolder() + File.separator + schematicsDir;
3233
File schematicDir = new File(directory);
3334
if(!schematicDir.exists()) {
3435
boolean mkdirs = schematicDir.mkdirs();
@@ -68,15 +69,27 @@ public TreeManager(TreeRepository plugin) {
6869
return;
6970
}
7071

71-
Tree tree = new Tree(directory, code, name, description);
72+
Tree tree = new Tree(directory, group, code, name, description);
7273
Integer roots = (Integer) treeMap.get("roots");
7374
if(roots != null) {
7475
tree.setRoots(roots);
7576
}
7677

77-
// Copy the tree to the plugin's schematics folder if it doesn't exist
78+
// Check the tree's schematic exists at plugins/TreeRepository/{group}/{code}.schematic
7879
if(!tree.getSchematic().exists()) {
79-
FileUtil.copy(tree.getSchematic(), new File(this.plugin.getDataFolder() + File.separator + "schematics", tree.getCode() + ".schematic"));
80+
// The tree doesn't exist so let's see if it exists in our resources
81+
InputStream is = this.plugin.getResource(schematicsDir + File.separator + tree.getFilePath());
82+
if(is != null) {
83+
try {
84+
FileUtils.copyInputStreamToFile(is, tree.getSchematic());
85+
} catch(IOException e) {
86+
e.printStackTrace();
87+
// Don't add the tree
88+
return;
89+
}
90+
} else {
91+
return;
92+
}
8093
}
8194

8295
group.add(tree);

src/main/java/com/boveybrawlers/TreeRepository/objects/Tree.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@
44

55
public class Tree {
66

7+
private String filePath;
78
private File schematicFile;
89

910
private String code;
1011
private String name;
1112
private String description;
1213
private Integer roots;
1314

14-
public Tree(String directory, String code, String name, String description) {
15+
public Tree(String directory, Group group, String code, String name, String description) {
1516
this.code = code;
1617
this.name = name;
1718
this.description = description;
1819
this.roots = 0;
1920

2021
// Store the reference to the file
21-
this.schematicFile = new File(directory + File.separator + this.code + ".schematic");
22+
this.filePath = group.getName() + File.separator + this.code + ".schematic";
23+
this.schematicFile = new File(directory + File.separator + this.filePath);
24+
}
2225

26+
public String getFilePath() {
27+
return this.filePath;
2328
}
2429

2530
public String getCode() {

src/main/java/com/boveybrawlers/TreeRepository/utils/Pagination.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ public Pagination(int page, int perPage, int listSize) {
2525
public static Pagination parseArgs(String[] args, int argIndex, int perPage, int listSize) {
2626
int page = 1;
2727
if(args.length > argIndex) {
28-
page = Integer.parseInt(args[argIndex]);
28+
try {
29+
page = Integer.parseInt(args[argIndex]);
30+
} catch(NumberFormatException ex) {
31+
return null;
32+
}
2933
}
3034

3135
return new Pagination(page, perPage, listSize);
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)