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,103 +10,104 @@ 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;
public class Portal {
private static AdvancedPortalsPlugin plugin;
public static boolean portalsActive = false;
public static AdvancedPortal[] Portals;
private static boolean ShowBungeeMessage;
public Portal(AdvancedPortalsPlugin plugin) {
ConfigAccessor config = new ConfigAccessor(plugin, "Config.yml");
ShowBungeeMessage = config.getConfig().getBoolean("ShowBungeeWarpMessage");
Portal.plugin = plugin;
Portal.loadPortals();
}
public Portal(AdvancedPortalsPlugin plugin) {
ConfigAccessor config = new ConfigAccessor(plugin, "Config.yml");
ShowBungeeMessage = config.getConfig().getBoolean("ShowBungeeWarpMessage");
Portal.plugin = plugin;
Portal.loadPortals();
}
/**
* This can be used to move the get keys to different sections
*
*
* ConfigurationSection section = config.getSection("sectionname");
*
*
* section.getKeys(false);
*
*
*/
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];
for(int i = 0; i <= PortalSet.toArray().length - 1; i++){
Portals[i] = new AdvancedPortal();
}
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);
}
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"));
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 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];
for(int i = 0; i <= PortalSet.toArray().length - 1; i++){
Portals[i] = new AdvancedPortal();
}
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);
}
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"));
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, String serverName, PortalArg... portalArgs) {
if(!pos1.getWorld().equals(pos2.getWorld())){
plugin.getLogger().log(Level.WARNING, "pos1 and pos2 must be in the same world!");
return "\u00A7cPortal creation error, pos1 and pos2 must be in the same world!";
}
int LowX = 0;
int LowY = 0;
int LowY = 0;
int LowZ = 0;
int HighX = 0;
int HighY = 0;
int HighZ = 0;
if(pos1.getX() > pos2.getX()){
LowX = (int) pos2.getX();
HighX = (int) pos1.getX();
@ -131,65 +132,65 @@ public class Portal {
LowZ = (int) pos1.getZ();
HighZ = (int) pos2.getZ();
}
Location checkpos1 = new Location(pos1.getWorld(), HighX, HighY, HighZ);
Location checkpos2 = new Location(pos2.getWorld(), LowX, LowY, LowZ);
if(checkPortalOverlap(checkpos1, checkpos2)){
plugin.getLogger().log(Level.WARNING, "Portals must not overlap!");
return "\u00A7cPortal creation error, portals must not overlap!";
}
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
config.getConfig().set(name + ".world", pos1.getWorld().getName());
config.getConfig().set(name + ".triggerblock", checkMaterial(triggerBlock));
config.getConfig().set(name + ".destination", destination);
config.getConfig().set(name + ".bungee", serverName);
config.getConfig().set(name + ".pos1.X", HighX);
config.getConfig().set(name + ".pos1.Y", HighY);
config.getConfig().set(name + ".pos1.Z", HighZ);
config.getConfig().set(name + ".pos2.X", LowX);
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();
loadPortals();
return "\u00A7aPortal creation successful!";
}
// make this actually work!
private static boolean checkPortalOverlap(Location pos1, Location pos2) {
if(portalsActive){
int portalId = 0;
for(@SuppressWarnings("unused") Object portal : Portal.Portals){
if(Portals[portalId].worldName.equals(pos2.getWorld().getName())){ // checks that the cubes arnt overlapping by seeing if all 4 corners are not in side another
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos1.getBlockX(), Portals[portalId].pos1.getBlockY(), Portals[portalId].pos1.getBlockZ())){return true;}
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos1.getBlockX(), Portals[portalId].pos1.getBlockY(), Portals[portalId].pos2.getBlockZ())){return true;}
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos1.getBlockX(), Portals[portalId].pos2.getBlockY(), Portals[portalId].pos1.getBlockZ())){return true;}
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos2.getBlockX(), Portals[portalId].pos1.getBlockY(), Portals[portalId].pos1.getBlockZ())){return true;}
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos2.getBlockX(), Portals[portalId].pos2.getBlockY(), Portals[portalId].pos2.getBlockZ())){return true;}
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos2.getBlockX(), Portals[portalId].pos1.getBlockY(), Portals[portalId].pos2.getBlockZ())){return true;}
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos1.getBlockX(), Portals[portalId].pos2.getBlockY(), Portals[portalId].pos2.getBlockZ())){return true;}
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos2.getBlockX(), Portals[portalId].pos2.getBlockY(), Portals[portalId].pos1.getBlockZ())){return true;}
}
portalId++;
@ -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) {
@ -217,15 +218,11 @@ 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;
String BlockID = config.getConfig().getString("DefaultPortalTriggerBlock");
try
@ -236,39 +233,39 @@ public class Portal {
{
triggerBlockType = Material.getMaterial(BlockID);
}
if(triggerBlockType == null){
triggerBlockType = Material.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;
}
public static void redefine(Location pos1, Location pos2, String name){
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
config.getConfig().set(name + ".pos1.X", pos1.getX());
config.getConfig().set(name + ".pos1.Y", pos1.getY());
config.getConfig().set(name + ".pos1.Z", pos1.getZ());
config.getConfig().set(name + ".pos2.X", pos2.getX());
config.getConfig().set(name + ".pos2.Y", pos2.getY());
config.getConfig().set(name + ".pos2.Z", pos2.getZ());
config.saveConfig();
loadPortals();
}
public static void remove(String name){
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
Object[] keys = config.getConfig().getKeys(true).toArray();
for(int i = keys.length - 1; i >= 0; i--){
String key = keys[i].toString();
@ -279,41 +276,41 @@ public class Portal {
config.getConfig().set(name, null);
// 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 + ".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 + ".pos1", null);
config.getConfig().set(name + ".pos2", null);
config.getConfig().set(name, 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 + ".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, null);*/
config.saveConfig();
loadPortals();
}
public static boolean portalExists(String portalName){
ConfigAccessor portalconfig = new ConfigAccessor(plugin, "Portals.yml");
String posX = portalconfig.getConfig().getString(portalName + ".pos1.X");
if(posX == null){
return false;
}
@ -321,12 +318,12 @@ public class Portal {
return true;
}
}
public static boolean activate(Player player, String portalName) {
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
// add other variables or filter code here, or somehow have a way to register them
if(config.getConfig().getString(portalName + ".bungee") != null){
if(ShowBungeeMessage){
player.sendMessage("\u00A7a[\u00A7eAdvancedPortals\u00A7a] Attempting to warp to \u00A7e" + config.getConfig().getString(portalName + ".bungee") + "\u00A7a.");
@ -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,28 +416,30 @@ 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;
}
}
// add code for if the portal doesnt have a destination but a exemptPlayer location
}
public static void rename(String oldName, String newName){
// set it so it gets all data from one and puts it into another place
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
Set<String> keys = config.getConfig().getKeys(true);
for(String key: keys){
if(key.startsWith(oldName + ".")){
@ -393,17 +453,17 @@ public class Portal {
{
config.getConfig().set(key.replace(oldName + ".", newName + "."), config.getConfig().getString(key));
}
}
}
}
config.saveConfig();
remove(oldName);
}
}