Further work for Issue #6.

This commit is contained in:
Tobias Ottenweller 2013-02-26 14:16:39 +01:00
parent 880071140a
commit 1bfc886969
2 changed files with 74 additions and 26 deletions

View File

@ -2,7 +2,6 @@ package de.craftinc.gates.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -157,8 +156,8 @@ public abstract class BaseCommand
} }
else else
{ {
Plugin.log(Level.FINE, "Command sender is no player! Switching to bukki for checking permissions!"); // sender is no player Ð there is no information about the senders locations
return this.sender.hasPermission(this.requiredPermission); return Plugin.permission.has(this.sender, this.requiredPermission);
} }
@ -166,7 +165,14 @@ public abstract class BaseCommand
if (this.requiredPermission.equals(Plugin.permissionInfo)) if (this.requiredPermission.equals(Plugin.permissionInfo))
{ {
hasPermission = Plugin.permission.has(p.getWorld(), p.getName(), this.requiredPermission); if (this.hasGateParam)
{
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
}
else
{
hasPermission = Plugin.permission.has(p.getWorld(), p.getName(), this.requiredPermission);
}
} }
else if (this.requiredPermission.equals(Plugin.permissionUse) ) else if (this.requiredPermission.equals(Plugin.permissionUse) )
{ {
@ -174,7 +180,6 @@ public abstract class BaseCommand
} }
else if (this.requiredPermission.equals(Plugin.permissionManage)) else if (this.requiredPermission.equals(Plugin.permissionManage))
{ {
if (this.needsPermissionAtCurrentLocation && this.hasGateParam) if (this.needsPermissionAtCurrentLocation && this.hasGateParam)
{ {
boolean hasPersmissionAtCurrentLocation = Plugin.permission.has(p.getWorld(), p.getName(), this.requiredPermission); boolean hasPersmissionAtCurrentLocation = Plugin.permission.has(p.getWorld(), p.getName(), this.requiredPermission);

View File

@ -6,6 +6,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import de.craftinc.gates.Gate; import de.craftinc.gates.Gate;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
@ -45,12 +46,42 @@ public class CommandList extends BaseCommand
} }
/**
* Method for returning a collection of gates the player is allowed to see.
*/
protected Collection<Gate> getAllGates()
{
Collection<Gate> gates = Gate.getAll();
if (this.sender instanceof Player && Plugin.permission != null)
{
Player p = (Player)this.sender;
Collection<Gate> gatesCopy = new ArrayList<Gate>(gates); // create a copy since we cannot iterate over a collection while modifying it!
for (Gate gate : gatesCopy) {
if (!Plugin.permission.has(gate.getLocation().getWorld(), p.getName(), this.requiredPermission))
{
gates.remove(gate);
}
else if (gate.getExit() != null && !Plugin.permission.has(gate.getExit().getWorld(), p.getName(), this.requiredPermission))
{
gates.remove(gate);
}
}
}
return gates;
}
// pages start at 1 // pages start at 1
// will return null if requested page not availible // will return null if requested page not availible
protected List<String> message(int page) protected List<String> message(int page)
{ {
Collection<Gate> gates = Gate.getAll(); Collection<Gate> gates = this.getAllGates();
if (gates.size() == 0) { if (gates.size() == 0) {
return null; return null;
@ -163,12 +194,13 @@ public class CommandList extends BaseCommand
currentPage++; currentPage++;
} }
if (pageMessages.isEmpty()) { if (pageMessages.isEmpty())
{
return null; return null;
} }
else { else {
ArrayList<String> retVal = new ArrayList<String>(); ArrayList<String> retVal = new ArrayList<String>();
retVal.add(TextUtil.titleize("List of all gates (" + page + "/" + + --currentPage + ")")); retVal.add(TextUtil.titleize("List of all gates (" + page + "/" + --currentPage + ")"));
retVal.addAll(pageMessages); retVal.addAll(pageMessages);
return retVal; return retVal;
@ -176,30 +208,41 @@ public class CommandList extends BaseCommand
} }
protected int getPageParameter()
{
int page = 1;
try {
page = new Integer(parameters.get(0));
}
catch (Exception e) { }
return page;
}
public void perform() public void perform()
{ {
Collection<Gate> gates = Gate.getAll(); int page = this.getPageParameter();
if (gates.size() == 0) { List<String> messages = message(page);
sendMessage(ChatColor.RED + "There are no gates yet.");
} if (messages == null)
else { {
int page = 1; if (page == 1) // no gates exist
{
try { sendMessage(ChatColor.RED + "There are no gates yet. " + ChatColor.RESET +
page = new Integer(parameters.get(0)); "(Note that you might not be allowed to get information about certain gates)");
}
catch (Exception e) {
} }
else // the requested page does not exist
List<String> messages = message(page); {
if (messages == null) {
sendMessage(ChatColor.RED + "The requested page is not availible"); sendMessage(ChatColor.RED + "The requested page is not availible");
} }
else {
sendMessage(messages); }
} else
{
sendMessage(messages);
} }
} }
} }