Added base command

This commit is contained in:
Alastair 2015-11-15 22:52:29 +00:00
parent 24a76e453d
commit bf09540855
3 changed files with 260 additions and 175 deletions

View File

@ -131,8 +131,8 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
else if(args[i].toLowerCase().startsWith("command:") && args[i].length() > 8){ // not completely implemented
executesCommand = true;
portalCommand = parseArgVariable(args,i,"command:");
i += this.portalArgsStringLength;
extraData.add(new PortalArg("command", portalCommand));
i = this.portalArgsStringLength;
extraData.add(new PortalArg("command.1", portalCommand));
}
}
if(!hasName){
@ -173,6 +173,10 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
player.sendMessage("\u00A7apermission: \u00A7e(none needed)");
}
if(executesCommand){
player.sendMessage("\u00A7acommand: \u00A7e" + portalCommand);
}
Material triggerBlockMat = Material.getMaterial(0);
if(hasTriggerBlock){
triggerBlockMat = Material.getMaterial(triggerBlock.toUpperCase());
@ -349,7 +353,7 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
String posX = portalConfig.getConfig().getString(portalName + ".pos1.X");
if(posX != null){
Portal.remove(portalName);
sender.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] The portal \u00A77" + portalName + " has been removed!");
sender.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] The portal \u00A77" + portalName + "\u00A7c has been removed!");
}
else{
sender.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] The portal you had selected no longer seems to exist!");
@ -417,10 +421,13 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
private String parseArgVariable(String[] args, int currentArg, String argStarter) {
String variableString = args[currentArg].replaceFirst(argStarter,"");
this.portalArgsStringLength = 1;
if(variableString.charAt(0) == '"'){
variableString = variableString.substring(1,variableString.length());
currentArg++;
for( ; currentArg < args.length; currentArg++){
variableString += " " + args[currentArg];
this.portalArgsStringLength += 1;
if(variableString.charAt(variableString.length() - 1) == '"'){
variableString = variableString.substring(0,variableString.length() - 1);
break;
@ -458,6 +465,24 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
else{
sender.sendMessage(" \u00A7ctriggerBlock\u00A7e: null");
}
if(portalConfig.getConfig().getString(portalName + ".portalArgs.command.1") != null){
int commands = 0;
String command = portalConfig.getConfig().getString(portalName + ".portalArgs.command.1");
// TODO possibly change code so it counds number of subvalues rather than a loop.
while(command != null){
command = portalConfig.getConfig().getString(portalName + ".portalArgs.command." + ++commands);
}
if(--commands > 1){
sender.sendMessage(" \u00A7acommands\u00A7e: " + commands + " commands");
}
else{
sender.sendMessage(" \u00A7acommands\u00A7e: " + commands + " command");
}
}
else{
sender.sendMessage(" \u00A7ccommands\u00A7e: none");
}
sender.sendMessage("");
Player player = (Player)sender;

View File

@ -294,7 +294,7 @@ public class Listeners implements Listener {
// UseOnlyServerMadeAxe being set to true makes is so only the axe generated by the server can be used so other iron axes can be used normally,
// by default its false but it is a nice feature in case the user wants to use the axe normally too, such as a admin playing survival or it being used
// as a weapon.
if((!UseOnlyServerAxe || (event.getItem() != null && event.getItem().getItemMeta().getDisplayName().equals("\u00A7ePortal Region Selector")) && event.getPlayer().getItemInHand().getTypeId() == WandMaterial.getId())) {
if(event.getItem() != null && (!UseOnlyServerAxe || event.getItem().getItemMeta().getDisplayName().equals("\u00A7ePortal Region Selector")) && event.getPlayer().getItemInHand().getTypeId() == WandMaterial.getId()) {
// This checks if the action was a left or right click and if it was directly effecting a block.
if(event.getAction() == Action.LEFT_CLICK_BLOCK) {

View File

@ -10,6 +10,7 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
import java.util.Set;
import java.util.logging.Level;
@ -24,13 +25,13 @@ public class Portal {
private static boolean ShowBungeeMessage;
public Portal(AdvancedPortalsPlugin plugin) {
ConfigAccessor config = new ConfigAccessor(plugin, "Config.yml");
ShowBungeeMessage = config.getConfig().getBoolean("ShowBungeeWarpMessage");
public Portal(AdvancedPortalsPlugin plugin) {
ConfigAccessor config = new ConfigAccessor(plugin, "Config.yml");
ShowBungeeMessage = config.getConfig().getBoolean("ShowBungeeWarpMessage");
Portal.plugin = plugin;
Portal.loadPortals();
}
Portal.plugin = plugin;
Portal.loadPortals();
}
/**
* This can be used to move the get keys to different sections
@ -41,56 +42,56 @@ public class Portal {
*
*/
public static void loadPortals(){
public static void loadPortals(){
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
Set<String> PortalSet = config.getConfig().getKeys(false);
if(PortalSet.size() > 0){
Portals = new AdvancedPortal[PortalSet.toArray().length];
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
Set<String> PortalSet = config.getConfig().getKeys(false);
if(PortalSet.size() > 0){
Portals = new AdvancedPortal[PortalSet.toArray().length];
for(int i = 0; i <= PortalSet.toArray().length - 1; i++){
Portals[i] = new AdvancedPortal();
}
for(int i = 0; i <= PortalSet.toArray().length - 1; i++){
Portals[i] = new AdvancedPortal();
}
int portalId = 0;
for(Object portal: PortalSet.toArray()){
int portalId = 0;
for(Object portal: PortalSet.toArray()){
Material blockType = Material.PORTAL;
String BlockID = config.getConfig().getString(portal.toString() + ".triggerblock");
try
{
Integer.parseInt(BlockID);
System.out.println("Block names must be given not IDs");
}
catch(NumberFormatException e)
{
blockType = Material.getMaterial(BlockID);
}
Material blockType = Material.PORTAL;
String BlockID = config.getConfig().getString(portal.toString() + ".triggerblock");
try
{
Integer.parseInt(BlockID);
System.out.println("Block names must be given not IDs");
}
catch(NumberFormatException e)
{
blockType = Material.getMaterial(BlockID);
}
if(blockType == null){
blockType = Material.PORTAL;
}
if(blockType == null){
blockType = Material.PORTAL;
}
Portals[portalId].trigger = blockType;
Portals[portalId].portalName = portal.toString();
Portals[portalId].worldName = config.getConfig().getString(portal.toString() + ".world");
World world = Bukkit.getWorld(config.getConfig().getString(portal.toString() + ".world"));
Portals[portalId].pos1 = new Location(world, config.getConfig().getInt(portal.toString() + ".pos1.X"), config.getConfig().getInt(portal.toString() + ".pos1.Y"), config.getConfig().getInt(portal.toString() + ".pos1.Z"));
Portals[portalId].pos2 = new Location(world, config.getConfig().getInt(portal.toString() + ".pos2.X"), config.getConfig().getInt(portal.toString() + ".pos2.Y"), config.getConfig().getInt(portal.toString() + ".pos2.Z"));
Portals[portalId].trigger = blockType;
Portals[portalId].portalName = portal.toString();
Portals[portalId].worldName = config.getConfig().getString(portal.toString() + ".world");
World world = Bukkit.getWorld(config.getConfig().getString(portal.toString() + ".world"));
Portals[portalId].pos1 = new Location(world, config.getConfig().getInt(portal.toString() + ".pos1.X"), config.getConfig().getInt(portal.toString() + ".pos1.Y"), config.getConfig().getInt(portal.toString() + ".pos1.Z"));
Portals[portalId].pos2 = new Location(world, config.getConfig().getInt(portal.toString() + ".pos2.X"), config.getConfig().getInt(portal.toString() + ".pos2.Y"), config.getConfig().getInt(portal.toString() + ".pos2.Z"));
portalId++;
}
portalsActive = true;
}
else{
portalsActive = false;
}
portalId++;
}
portalsActive = true;
}
else{
portalsActive = false;
}
}
}
public static String create(Location pos1, Location pos2 , String name, String destination, Material triggerBlock, PortalArg... extraData) {
return create(pos1, pos2, name, destination, triggerBlock, null, extraData);
}
public static String create(Location pos1, Location pos2 , String name, String destination, Material triggerBlock, PortalArg... extraData) {
return create(pos1, pos2, name, destination, triggerBlock, null, extraData);
}
public static String create(Location pos1, Location pos2, String name, String destination, Material triggerBlock, String serverName, PortalArg... portalArgs) {
@ -158,9 +159,9 @@ public class Portal {
config.getConfig().set(name + ".pos2.Y", LowY);
config.getConfig().set(name + ".pos2.Z", LowZ);
for(PortalArg arg: portalArgs){
config.getConfig().set(name + ".portalArgs." + arg.argName, arg.value);
}
for(PortalArg arg: portalArgs){
config.getConfig().set(name + ".portalArgs." + arg.argName, arg.value);
}
config.saveConfig();
@ -199,13 +200,13 @@ public class Portal {
}
private static boolean checkOverLapPortal(Location pos1, Location pos2, int posX, int posY, int posZ) {
if(pos1.getX() >= posX && pos1.getY() >= posX && pos1.getZ() >= posZ){
if((pos2.getX()) <= posX && pos2.getY() <= posY && pos2.getZ() <= posZ){
return true;
if(pos1.getX() >= posX && pos1.getY() >= posX && pos1.getZ() >= posZ){
if((pos2.getX()) <= posX && pos2.getY() <= posY && pos2.getZ() <= posZ){
return true;
}
}
return false;
}
return false;
}
private static String checkMaterial(Material triggerBlock) {
@ -218,12 +219,8 @@ public class Portal {
return triggerBlock.toString();
}
public static String create(Location pos1, Location pos2, String portalName, String name, String destination, PortalArg... extraData) {
return create(pos1, pos2, name, destination,(String) null);
}
@SuppressWarnings("deprecation")
public static String create(Location pos1, Location pos2, String name, String destination, String serverName) { // add stuff for destination names or coordinates
public static String create(Location pos1, Location pos2, String name, String destination, String serverName, PortalArg... extraData) { // add stuff for destination names or coordinates
ConfigAccessor config = new ConfigAccessor(plugin, "Config.yml");
Material triggerBlockType;
@ -243,7 +240,7 @@ public class Portal {
// TODO add a for loop which scans through the addArgs and adds them to the config so that the application can use them
String result = create(pos1, pos2, name, destination, triggerBlockType, serverName);
String result = create(pos1, pos2, name, destination, triggerBlockType, serverName, extraData);
return result;
}
@ -281,28 +278,28 @@ public class Portal {
// TODO add code to check if people have the portal selected and notify if removed.
/**Set<String> keys = config.getConfig().getKeys(true);
for(String key: keys){
if(key.startsWith(name)){
config.getConfig().set(key, null);
}
}*/
for(String key: keys){
if(key.startsWith(name)){
config.getConfig().set(key, null);
}
}*/
/**config.getConfig().set(name + ".world", null);
config.getConfig().set(name + ".triggerblock", null);
config.getConfig().set(name + ".destination", null);
config.getConfig().set(name + ".triggerblock", null);
config.getConfig().set(name + ".destination", null);
config.getConfig().set(name + ".pos1.X", null);
config.getConfig().set(name + ".pos1.Y", null);
config.getConfig().set(name + ".pos1.Z", null);
config.getConfig().set(name + ".pos1.X", null);
config.getConfig().set(name + ".pos1.Y", null);
config.getConfig().set(name + ".pos1.Z", null);
config.getConfig().set(name + ".pos2.X", null);
config.getConfig().set(name + ".pos2.Y", null);
config.getConfig().set(name + ".pos2.Z", null);
config.getConfig().set(name + ".pos2.X", null);
config.getConfig().set(name + ".pos2.Y", null);
config.getConfig().set(name + ".pos2.Z", null);
config.getConfig().set(name + ".pos1", null);
config.getConfig().set(name + ".pos2", null);
config.getConfig().set(name + ".pos1", null);
config.getConfig().set(name + ".pos2", null);
config.getConfig().set(name, null);*/
config.getConfig().set(name, null);*/
config.saveConfig();
@ -339,10 +336,71 @@ public class Portal {
}
else{
boolean showFailMessage = true;
if(config.getConfig().getString(portalName + ".portalArgs.command.1") != null){
showFailMessage = false;
int commandLine = 1;
String command = config.getConfig().getString(portalName + ".portalArgs.command." + commandLine);
do{
// (?i) makes the search case insensitive
command = command.replaceAll("@player", player.getName());
plugin.getLogger().log(Level.SEVERE, "Portal command: " + command);
if(command.startsWith("#")){
command = command.substring(1);
plugin.getLogger().log(Level.SEVERE, "Portal command: " + command);
plugin.getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
}
else if(command.startsWith("!")){
command = command.substring(1);
boolean wasOp = player.isOp();
try{
player.setOp(true);
player.performCommand(command);
}
finally {
player.setOp(wasOp);
}
}
else if(command.startsWith("^")){
command = command.substring(1);
PermissionAttachment permissionAttachment = null;
try{
permissionAttachment = player.addAttachment(plugin, "*", true);
player.performCommand(command);
}
finally {
player.removeAttachment(permissionAttachment);
}
}
else{
player.performCommand(command);
}
command = config.getConfig().getString(portalName + ".portalArgs.command." + ++commandLine);
}while(command != null);
}
if(config.getConfig().getString(portalName + ".destination") != null){
ConfigAccessor configDesti = new ConfigAccessor(plugin, "Destinations.yml");
String destiName = config.getConfig().getString(portalName + ".destination");
if(configDesti.getConfig().getString(destiName + ".world") != null){
String permission = config.getConfig().getString(portalName + ".portalArgs.permission");
if(permission == null || (permission != null && player.hasPermission(permission)) || player.isOp()){
if(configDesti.getConfig().getString(destiName + ".world") != null){
boolean warped = Destination.warp(player, destiName);
return warped;
}
else{
player.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] The destination you are currently attempting to warp to doesnt exist!");
plugin.getLogger().log(Level.SEVERE, "The portal '" + portalName + "' has just had a warp "
+ "attempt and either the data is corrupt or that destination listed doesn't exist!");
return false;
}
}
else{
player.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] You do not have permission to use this portal!");
return false;
}
/*if(configDesti.getConfig().getString(destiName + ".world") != null){
String permission = config.getConfig().getString(portalName + ".portalArgs.permission");
if(permission == null || (permission != null && player.hasPermission(permission)) || player.isOp()){
boolean warped = Destination.warp(player, destiName);
@ -358,13 +416,15 @@ public class Portal {
plugin.getLogger().log(Level.SEVERE, "The portal '" + portalName + "' has just had a warp "
+ "attempt and either the data is corrupt or that destination listed doesn't exist!");
return false;
}
}*/
}
else{
player.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] The portal you are trying to use doesn't have a destination!");
plugin.getLogger().log(Level.SEVERE, "The portal '" + portalName + "' has just had a warp "
+ "attempt and either the data is corrupt or portal doesn't exist!");
if(showFailMessage) {
player.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] The portal you are trying to use doesn't have a destination!");
plugin.getLogger().log(Level.SEVERE, "The portal '" + portalName + "' has just had a warp "
+ "attempt and either the data is corrupt or portal doesn't exist!");
}
return false;
}
}