How to reload config.yml file on command in Bukkit Plugin? - java

I was trying to reload config.yml file on command with Bukkit Plugin, I don't know how to do it.
I searched on google and I found one answer, but when I used it, config.yml was not generating. Here's my code:
BlockChanger
Please help

First you need to remove the final modifier from your config variables, else this can't refresh from config file.
Then you need a method for reload the config and set the config variables again. An example based on your code:
#Override
public void onEnable() {
loadConfig(this);
}
private final String prefix = ChatColor.AQUA + "[";
private String prefixTrue;
private String prefixFalse;
public void loadConfig(Plugin plugin) {
File file = new File(plugin.getDataFolder().getAbsolutePath() + "/config.yml");
FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
prefixTrue = prefix + cfg.getString("prefix") + "]" + ChatColor.GREEN + " ";
prefixFalse = prefix + cfg.getString("prefix") + "]" + ChatColor.RED + " ";
}
Make sure that you call the method loadConfig in onEnable and every time you want to reload the config

Related

Log function call on new line in Java and C#

I have a logging function in CSharp and Java that I use in walking the stack. How do I make each log print to a new line only. Below are my Java and CSharp Functions.
public static void LogFunctionCall(String parameters){
Object trace = Thread.currentThread().getStackTrace()[3];
android.util.Log.i("##################" + trace.toString()+ "", parameters );
}
the java version is this
public static void LogFunctionCall(string parameters,
[System.Runtime.CompilerServices.CallerMemberName] string methodName = "",
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
var stackFrame = new StackFrame(1);
var callerMethod = stackFrame.GetMethod();
var className = callerMethod.DeclaringType;
System.Diagnostics.Trace.WriteLine("CCCCCCCCCCCCCCCC" + " " + className + " " + methodName + " " + sourceLineNumber + " " + parameters + "\n");
}
I code on a windows machine.
Please where exactly do I need to place the new line character. I tried this
public static void LogFunctionCall(String parameters){
Object trace = Thread.currentThread().getStackTrace()[3];
android.util.Log.i("##################" + trace.toString()+ "", parameters + "\n" );
}
but I still saw some of the logs being clumped up on a single line.
Instead of \n, try \r\n (carriage return and newline). Some text editors will display differently, so the newline may be in there, but whatever app you're using to read the logs might not be displaying it correctly.
You could also try
System.lineSeparator();
I've seen instances where the /n won't work but the lineSep does.
Also, because it hasn't been mentioned, Environment.NewLine will give you the new line character that is configured for the current environment.

How to run command line that run java app from java code?

I have the following code:
private static String[] createCommandLinux(String cvPath, String jsonPath, String libPath) {
List<String> command = new ArrayList<>();
command.add("java");
command.add("-cp");
command.add("'" + libPath + "/ResumeTransducer/bin/*:" +
libPath + "/GATEFiles/lib/*:" +
libPath + "/GATEFiles/bin/gate.jar:" +
libPath + "/ResumeTransducer/lib/*'");
command.add("code4goal.antony.resumeparser.ResumeParserProgram");
command.add("'" + cvPath + "'");
command.add("'" + jsonPath + "'");
String[] commandArr = new String[command.size()];
commandArr = command.toArray(commandArr);
return commandArr;
}
...
Runtime.getRuntime().exec(createCommandLinux(cvPath,jsonPath,libPath));
...
I copy the command to run on MACOS terminal and it works fine. But when run this command within java application, it can not work as terminal does. It notifies that it can not find the main class. Do you know what the issue with it on MAXOS ?
Thanks

Get the class name in the #BeforeSuite annotation - TestNG - Java

Is there a way to get the name of the class that the script was started from inside the #BeforeSuite annotation when not executed via xml file?
Doing this:
reportName = new Exception().getStackTrace()[0].getClassName();
returns the class itself that contains the #BeforeSuite annotation and this:
reportName = new Exception().getStackTrace()[1].getClassName();
returns sun.reflect.NativeMethodAccessorlmpl
If I execute a script directly from a separate class, I want to get that info because I am using it to name my Extent Report file name.
In case you are wondering what the code inside the #BeforeSuite annotation looks like:
// Set Extent Report file name from the global properties file
String reportName = ctx.getCurrentXmlTest().getSuite().getName();
if (reportName.equals("Default suite"))
{
reportName = new Exception().getStackTrace()[0].getClassName();
}
String timeStamp = new SimpleDateFormat("HH.mm.ss").format(new Date());
// Initialize Extent Reports and modify the looks/output
String extentReportPath = "";
if (extent == null) {
if (os.equals("Mac"))
{
extentReportPath = reportPath + "/" + project + "-" + reportName + "-" + environment + "-" + browser + "-" + timeStamp + ".html";
}
else if (os.equals("Windows"))
{
extentReportPath = reportPath + "\\" + project + "-" + reportName + "-" + environment + "-" + browser + "-" + timeStamp + ".html";
}
// Start new report
extent = new ExtentReports(extentReportPath, true);
}
There's more to it, but this is the part pertinent to my question.
---UPDATE---
This was my solution:
// Set Extent Report file name from the global properties file
String reportName = ctx.getCurrentXmlTest().getSuite().getName();
if (reportName.equals("Default suite"))
{
List<ITestNGMethod> allMethods = ctx.getSuite().getAllMethods();
for (ITestNGMethod method : allMethods)
{
String fullMethod = method.toString();
int indexOf = fullMethod.indexOf(".");
reportName = fullMethod.replace(fullMethod.substring(indexOf), ""); }
}
You can pass argument ITestContext to the beforesuite method. testNG will auto-inject it. This should have the information you looking for.
context.getSuite().getAllMethods -> List of TestNGMethods.getRealClass() or getTestClass().

weird behaviour of setting new File Name in android

I need to create new file in andorid with Java. I do it like this :
public static File getAbosoluteFile(String relativePath, Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
return new File(context.getExternalFilesDir(null), "AE " +".jpg");
} else {
Toast.makeText(context, "internal", Toast.LENGTH_SHORT).show();
return new File(context.getFilesDir(), "AE" +".jpg");
}
}
but when I put it like this - it works properly, but when I change the name , for example using string from the method :
public static String getCurrentDate()
{
String returnDate = null;
Calendar currentDate = Calendar.getInstance();
int minute = currentDate.get(Calendar.MINUTE);
String editedMinute;
if (minute<10)
editedMinute = "0" + Integer.toString(minute);
else
editedMinute = Integer.toString(minute);
returnDate= (Integer.toString((currentDate.get(Calendar.MONTH) + 1)) + "/" +Integer.toString(currentDate.get(Calendar.DAY_OF_MONTH)) + "/" +Integer.toString(currentDate.get(Calendar.YEAR)) + " " +
Integer.toString(currentDate.get(Calendar.HOUR_OF_DAY)) + ":" + editedMinute);
return returnDate;
}
So , even when I use return new File(context.getExternalFilesDir(null), "12/12/12 " +".jpg");
the file is doesn't create too. I tried to use the screening in the name - thought reason is in this, but the same result.
Is Java's new File(File dir, String name) is so depending to the name format? Or what the reason ?
You can't use / in your file name. It is used as a file separator.
For eg.If you have a folder named App in your SD card and in it a sub folder called Sub the path is something like mnt/sdcard/App/Sub(mnt/sdcard is just for example it is not fixed)
So you can see why you can't use / in a file name.
Try using _ in place of /.

Java - Updating static variables

I have two classes in java that need to run at the same time - A Crawler class ( that basically implements a web crawler, and keeps printing out urls as it encounters them ), and an Indexer class, which as of now, is supposed to simply print the urls crawled.
For this, my Indexer class has a Queue :
public static Queue<String> urls = new LinkedList();
And in the toVisit() function of my Crawler class, I have the following :
Indexer.urls.add( url ) // where url is a String
The Crawler is working totally fine, since it prints out all the urls that it has encountered, but for some reason, these urls do not get added to the Queue in my Indexer class. Any idea why this may be the case ?
The toVisit() method from Crawler.java is as follows :
public void visit(Page page) {
int docid = page.getWebURL().getDocid();
String url = page.getWebURL().getURL();
String domain = page.getWebURL().getDomain();
String path = page.getWebURL().getPath();
String subDomain = page.getWebURL().getSubDomain();
String parentUrl = page.getWebURL().getParentUrl();
System.out.println("Docid: " + docid);
System.out.println("URL: " + url);
System.out.println("Domain: '" + domain + "'");
System.out.println("Sub-domain: '" + subDomain + "'");
System.out.println("Path: '" + path + "'");
System.out.println("Parent page: " + parentUrl);
Indexer.urls.add( url );
System.out.println("=============");
}
Code from my Indexer class :
public static Queue<String> urls = new LinkedList();
public static void main( String[] args )
{
while( urls.isEmpty() )
{
//System.out.println("Empty send queue");
Thread.sleep(sleepTime);
}
System.out.println( urls.poll() );
}
Okay, so I solved my problem by doing as suggested by BigMike. I implemented the Runnable interface in my two classes, and then ran those 2 classes as threads within the main function of a new third class.
Thanks everyone for all your help ! :)

Categories