Skip to content

Commit

Permalink
Fixed session not being remembered for player! + Pagination fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danbovey committed Jan 22, 2018
1 parent 6559ed5 commit c2b21b2
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public SingleTreePlanter(TreeRepository plugin, org.bukkit.entity.Player player,
}

public void actPrimary(Vector location) {
this.plugin.worldedit.pasteSchematic(this.player, this.tree.getSchematic(), location);
Integer roots = this.tree.getRoots();

this.plugin.worldedit.pasteSchematic(this.player, this.tree.getSchematic(), location, roots);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
Pagination pagination = Pagination.parseArgs(args, 2, perPage, group.getList().size());

String page = "";
if(pagination.hasPages()) {
page = ChatColor.DARK_GREEN + "Page: " + ChatColor.WHITE + pagination.getPage();
if(pagination.hasNextPage()) {
page = ChatColor.DARK_GREEN + " Page: " + ChatColor.WHITE + pagination.getPage();
}
player.sendMessage(ChatColor.DARK_GREEN + "Group: " + ChatColor.GRAY + group.getNiceName() + page);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
Pagination pagination = Pagination.parseArgs(args, argIndex, perPage, groups.size());

String page = "";
if(pagination.hasPages()) {
if(pagination.hasNextPage()) {
page = ChatColor.DARK_GREEN + " Page: " + ChatColor.WHITE + pagination.getPage();
}
player.sendMessage("Available tree groups: " + ChatColor.DARK_GRAY + "(Click to view)" + page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void onPlayerInteract(PlayerInteractEvent event) {
}

if(planter != null) {
event.setCancelled(true);
planter.actPrimary(vector);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import com.boveybrawlers.TreeRepository.TreeRepository;
import com.boveybrawlers.TreeRepository.objects.Group;
import com.boveybrawlers.TreeRepository.objects.Tree;
import org.apache.commons.io.FileUtils;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.util.FileUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -24,37 +27,54 @@ public class TreeManager {
public TreeManager(TreeRepository plugin) {
this.plugin = plugin;

// Try to create the schematic directory when we start
String directory = this.plugin.getDataFolder() + File.separator + "schematics";
File schematicDir = new File(directory);
if(!schematicDir.exists()) {
boolean mkdirs = schematicDir.mkdirs();
if(!mkdirs) {
this.plugin.getLogger().log(Level.SEVERE, "Failed to create schematics directory");
return;
} else {
this.plugin.getLogger().log(Level.INFO, "Created schematics directory");
}
}

// Load trees.yml
FileConfiguration cfg = new YamlConfiguration();
try {
cfg.load(this.plugin.treesFile);
} catch(Exception e) {
this.plugin.getLogger().log(Level.SEVERE, "Failed to find trees.yml");
}

// Loop through groups
ConfigurationSection treesConfig = cfg.getConfigurationSection("groups");
treesConfig.getKeys(true).forEach(grp -> {
Group group = new Group(grp);

// Loop through trees in group
List<Map<?, ?>> groupConfig = treesConfig.getMapList(grp);
groupConfig.forEach(treeMap -> {
String code = (String) treeMap.get("code");
if(code == null) {
this.plugin.getLogger().log(Level.WARNING, "trees.yml: Tree found in " + group.getNiceName() + " group with no code");
return;
}
String name = (String) treeMap.get("name");
String description = (String) treeMap.get("description");
if(name == null) {
this.plugin.getLogger().log(Level.WARNING, "trees.yml: Tree with code " + code + " in group " + group.getNiceName() + " has no name");
return;
}

Tree tree = new Tree(plugin, code, name, description);

File dir = new File(this.plugin.getDataFolder() + File.separator + "schematics");
if(!dir.exists()) {
boolean mkdirs = dir.mkdirs();
if(!mkdirs) {
this.plugin.getLogger().log(Level.SEVERE, "Failed to create schematics directory");
return;
} else {
this.plugin.getLogger().log(Level.INFO, "Created schematics directory");
}
Tree tree = new Tree(directory, code, name, description);
Integer roots = (Integer) treeMap.get("roots");
if(roots != null) {
tree.setRoots(roots);
}

// Copy the tree to the plugin folder if it doesn't exist
// Copy the tree to the plugin's schematics folder if it doesn't exist
if(!tree.getSchematic().exists()) {
FileUtil.copy(tree.getSchematic(), new File(this.plugin.getDataFolder() + File.separator + "schematics", tree.getCode() + ".schematic"));
}
Expand All @@ -78,7 +98,7 @@ public List<Group> getGroups() {
}

public Group getGroup(String name) {
return this.groups.stream().filter(grp -> grp.getName().equals(name))
return this.groups.stream().filter(grp -> grp.getName().equals(name) || grp.getNiceName().equals(name))
.findFirst()
.orElse(null);
}
Expand Down
30 changes: 14 additions & 16 deletions src/main/java/com/boveybrawlers/TreeRepository/objects/Tree.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.boveybrawlers.TreeRepository.objects;

import com.boveybrawlers.TreeRepository.TreeRepository;
import org.apache.commons.io.FileUtils;

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

public class Tree {

Expand All @@ -14,22 +9,17 @@ public class Tree {
private String code;
private String name;
private String description;
private Integer roots;

public Tree(TreeRepository plugin, String code, String name, String description) {
public Tree(String directory, String code, String name, String description) {
this.code = code;
this.name = name;
this.description = description;
this.roots = 0;

// Store the reference to the file
this.schematicFile = new File(directory + File.separator + this.code + ".schematic");

// plugin.getLogger().log(Level.INFO, plugin.getDataFolder() + File.separator + "schematics", this.code + ".schematic");
// TODO: Move to manager?
String path = "schematics" + File.separator + this.code + ".schematic";
this.schematicFile = new File(plugin.getDataFolder() + File.separator + path);
InputStream is = plugin.getResource(path);
try {
FileUtils.copyInputStreamToFile(is, this.schematicFile);
} catch(IOException e) {
e.printStackTrace();
}
}

public String getCode() {
Expand All @@ -44,6 +34,14 @@ public String getDescription() {
return this.description;
}

public Integer getRoots() {
return this.roots;
}

public void setRoots(Integer roots) {
this.roots = roots;
}

public File getSchematic() {
return this.schematicFile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class Pagination {
public Pagination(int page, int perPage, int listSize) {
this.page = page;
this.perPage = perPage;
this.from = page * perPage;
this.to = page * perPage + perPage;
this.from = page * perPage - perPage;
this.to = page * perPage - 1;
this.listSize = listSize;
}

Expand All @@ -35,7 +35,7 @@ public int getPage() {
return this.page;
}

public boolean hasPages() {
public boolean hasNextPage() {
return this.listSize > this.page * this.perPage;
}

Expand All @@ -52,7 +52,7 @@ public TextComponent create(String prevCmd, String nextCmd) {
TextComponent actionNext = new TextComponent("[Next ->]");

prevCmd = prevCmd.replace("{prev}", this.page - 1 + "");
prevCmd = prevCmd.replace("{next}", this.page + 1 + "");
nextCmd = nextCmd.replace("{next}", this.page + 1 + "");

if(this.page > 1) {
actionPrev.setColor(ChatColor.WHITE);
Expand All @@ -62,7 +62,7 @@ public TextComponent create(String prevCmd, String nextCmd) {
actionPrev.setColor(net.md_5.bungee.api.ChatColor.DARK_GRAY);
}

if(this.hasPages()) {
if(this.hasNextPage()) {
actionNext.setColor(ChatColor.WHITE);
actionNext.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, nextCmd));
actionNext.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Go to the next page").create()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public boolean hasPlugin() {
return this.we != null;
}

/**
* Copies a schematic file to a player's clipboard
*
* @param player The player who actioned the edit
* @param file The schematic file of the tree
* @return LocalSession
*/
public LocalSession copySchematic(Player player, File file) {
com.sk89q.worldedit.entity.Player wePlayer = this.getPlayer(player);
WorldData worldData = wePlayer.getWorld().getWorldData();
Expand Down Expand Up @@ -74,8 +81,15 @@ public LocalSession copySchematic(Player player, File file) {
return session;
}

/**
* Paste a schematic file at a location
* @param player The player who has actioned the edit
* @param file The schematic file of the tree
* @param location The vector location of the paste
* @param roots How many blocks of roots under the ground the tree has
*/
@SuppressWarnings("deprecation")
public void pasteSchematic(Player player, File file, Vector location) {
public void pasteSchematic(Player player, File file, Vector location, Integer roots) {
com.sk89q.worldedit.entity.Player wePlayer = this.getPlayer(player);

// Move the tree above the block
Expand Down Expand Up @@ -108,6 +122,8 @@ public void pasteSchematic(Player player, File file, Vector location) {
} catch(WorldEditException e) {
e.printStackTrace();
}

session.remember(editSession);
}

public com.sk89q.worldedit.entity.Player getPlayer(Player player) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/trees.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ groups:
- code: AFR1L
name: "Grandidier's Baobab Fullgrown"
description: "An imposing and unusual tree from Madagascar. Birch wood, spruce leaves, 31 blocks"
roots: 0
- code: AFR1S
name: "Grandidier's Baobab Halfgrown"
description: "An imposing and unusual tree from Madagascar. Birch wood, spruce leaves, 15 blocks"
Expand Down Expand Up @@ -34,6 +35,7 @@ groups:
name: "Knobthorn Fullgrown"
description: "Large deciduous tree often browsed by giraffes. Oak wood, oak leaves, 19 blocks"
- code: AFR6S
name: "Knobthorn Halfgrown"
description: "Medium deciduous tree often browsed by giraffes. Oak wood, oak leaves, 12 blocks"
- code: AFR7L
name: "Marula Fullgrown"
Expand Down

0 comments on commit c2b21b2

Please sign in to comment.