Player*First*JoinEvent how should i do that? - java

so i tried to make a simple PlayerJoinEvent (AKA. PlayerFirstJoinEvent) for my server. Is there a way to do that? I want to run my code when player joins the server first time. I have tried multiple options like using if(player.hasPlayedBefore()) but it doesent want to work! So, do you have an idea how to fix it or do it with a different method? Thanks to everyone for help!

You haven't probably deleted player data! If new player joins, new player data are created, only then does Spigot know, that the player is new to the server!
To do so:
Shutdown your server
Go to server-folder/world/playerdata/*player-uuid*.dat
Delete player-uuid.dat
Start the server
Warning: This deletes player inventory!
If your server is in offline mode, and you don`t know what your UUID is, use this online tool.
If your server is in online mode, and you don't know your UUID, use namemc
Code to test the event:
#EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
if(!event.getPlayer().hasPlayedBefore()) {
event.getPlayer().sendMessage("Hi!");
}
}
Don't forget to register this event in your main class!

So i solved it and Thanks to everyone for help there wasnt any problem with hasPlayedBefore().

Related

How to let run only one instance of application at a time?

I am working on a GUI application that uses JavaFX(not fxml) and exported as a JAR. For slow machine, impatient user click more than once on JAR, and multiple instances of application started.
I'm looking for a solution to let only one instance can be run at a time on a system and if the user clicks again while the application is running nothing happens. I think it's called singleton but don't know how to implement it.
You could try JUnique. It's an open source library doing exactly what you ask for. Import junique-1.0.4.jar to your project as a library. It's just 10kb file.
It's manual neatly describes how to implement it on a project. For a JavaFX application, implementation would look something like this:
Make sure to import these classes to your main
import it.sauronsoftware.junique.AlreadyLockedException;
import it.sauronsoftware.junique.JUnique;
public static void main(String[] args) {
String appId = "myapplicationid";
boolean alreadyRunning;
try {
JUnique.acquireLock(appId);
alreadyRunning = false;
} catch (AlreadyLockedException e) {
alreadyRunning = true;
}
if (!alreadyRunning) {
launch(args); // <-- This the your default JavaFX start sequence
}else{ //This else is optional. Just to free up memory if you're calling the program from a terminal.
System.exit(1);
}
}
One easy solution that I've used is, when you start the application, it creates a file (I named it .lock but you can call it whatever you want), unless the file already exists, in which case the application terminates its execution instead of creating the file.
You will need to bind your application with a resource. It can be a file, port etc.
You can change the code on startup to check if the file is locked. The below code will give you some idea
FileOutputStream foStream = new FileOutputStream("/tmp/testfile.txt");
FileChannel channel = fileOutputStream.getChannel();
FileLock lock = channel.lock();
If you'd properly package your JavaFX code as a real application instead of just throwing it into a jar, you might get that functionality for free and without all these hacks. If I package my JavaFX code on my Mac with the jpackage tool, the result will be a full featured macOS application. That means that when I double-click its icon somewhere several times, only one instance of the application will be started. This is the default behaviour on Macs and properly packaged JavaFX applications just stick to that rule too. I can't say however what the behaviour on Windows or Linux is because I currently don't have such a box running. Maybe someone who knows can add this as a comment.

BLE with MiBand 3

This is my first time asking so please forgive if I happen to show mistakes.
Simply put, I want to create app that able to interact with Mi Band 3. The interaction I'm able to make is just reading battery info. What I truly want is enabling the real-time heart rate and steps scan.
What have I done before?
I've seen and tried https://github.com/pangliang/miband-sdk-android . It was actually this library that made me think that enabling heart scan from Android is possible to do. I tried following with my Mi Band 3 but failed: setHeartRateScanListener, startHeartRateScan, getBatteryInfo.
I've seen, cloned, and tampered a little with https://github.com/Freeyourgadget/Gadgetbridge . It is amazing, It can interact with my Mi Band 3, but the problem is Gadgetbridge do not have heart rate scan feature and I kinda overwhelmed with the code.
Of course, I've tried https://developer.android.com/guide/topics/connectivity/bluetooth-le . After reading the Pangliang's and Gadgetbridge's code I did following to do heart rate scan.
First, I know that I should put listener right? So here's the function
public void listenHeartRate() {
BluetoothGattCharacteristic characteristic = bluetoothGatt.getService(UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb")).getCharacteristic(UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb"));
bluetoothGatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
descriptor.setValue(new byte[] {0x01, 0x00});
if(!bluetoothGatt.writeDescriptor(descriptor)) {
Log.d(TAG, "tesListenHeartRate: failed");
}
}
And then, I start the scanning with this.
public void startHeartRate() {
Log.d(TAG, "tesHeartRate: start");
BluetoothGattCharacteristic characteristic = bluetoothGatt.getService(UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb")).getCharacteristic(UUID.fromString("00002a39-0000-1000-8000-00805f9b34fb"));
characteristic.setValue(new byte[]{21, 2, 1});
if (!bluetoothGatt.writeCharacteristic(characteristic)) {
Log.d(TAG, "tesStartHeartRate: failed");
}
}
The problem with this is that writeCharacteristic always returning status 3 which means GATT_WRITE_NOT_PERMITTED.
So, where did I go wrong? Thank you very much.
"(...) I nearly gave up, but then realized that I was missing out something. Authentication. Yes, I missed authentication! Thanks to Andrey Nikishaev,
He has explained very well how mi band is being paired and authenticated. That gave me a good head start. And here are the Authentication steps: (...)"
Whole article: https://medium.com/#yogeshojha/i-hacked-xiaomi-miband-3-and-here-is-how-i-did-it-43d68c272391

Odoo mobile sync does not work

I downloaded the odoo framework from the github, I installed it and locally every thing is working just fine, But it does not sync with the server no records came from the server and none go, It just work locally
I have seen the issues part in the github, but i does't give so much help
Is there any one knows what exaclly the problem?? Or any one have working copy of the framework??
I had the same problem but syncing worked fine after commenting out the some lines out of the file ContentResolver.java: search for lines 2326, 2327, 2328
public static void setMasterSyncAutomatically(boolean sync) {
setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId());
}
change them to:
// public static void setMasterSyncAutomatically(boolean sync) {
// setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId());
// }
I know this is not the correct way to do it, because you seem to override the authorisation check, but for me, for now, and for testing purposes it works.
(hope it helps, please don't shoot me)

GWT forbid History.back() to exit current site

I'm wondering if there is a simple way in GWT to forbid History.back() (or .forward()) to lead to an external site, and define a fallback url if one of this happen.
For example :
Assuming that mysite.com#token, contains a component triggering a History.back().
I am on stackoverflow.com, I paste mysite.com#token in my browser, then click on my component which move me back to stackoverflow, is it possible that click goes in mysite.com instead as a back exit my site?
Thanks for your help!
Use this approach
Window.addWindowClosingHandler(new ClosingHandler() {
public void onWindowClosing(ClosingEvent event) {
event.setMessage("Do you want to leave this site");
}
});

Making program extensions

I am making a program for computer surveillance at the moment.
It's for a competition in my country Croatia(InfoKup).
I have several options for sending command to another PC, but I
want to make the possibility for the command extension for people
who know Java. So I want to make the user be able to add some of his
custom commands for the program. For example something like
Minecraft mods. I know it is possible, but how would I go about
doing that.
Any help would be greatly appreciated. My code on GitHub:GitHub
Don't mind the stream thing.
It's something my friend is experimenting with.
EDIT: e.g.
Currently I have the possibility to send popups to another PC. What if the extension maker knew the code to send cmd commands and wants to add that function. He makes an extension and puts it in the extension folder. Voila we have a new possibility.
EDIT 2:
Don't be so harsh on me pls :). Thx for the dynamic class loading tip. I have been looking into that, and it looks promising.
Basically what I want to have possible is the user to drop the "mod/extension/whatever" in the "mod/extension/whatever" folder, and the program would load it and put all of the buttons declared in the class in to the GUI, and with them the function. I think I'm getting the hang of this, but any tips would be helpful.
e.g.
package sth.sth;
import blah.blah.*;
public class ClassSTH extends SchoolarButton{
public ClassSTH(String params){
super(params);
}
#Override
public void OnClick(){
doStuff();
}
}
EDIT:
The problem is easily solvable using Java Reflection! I wish someone posted that as an answer befpre blatanty downvoting a question because pf a GitHub link that was there to prpve that I've actually done something.
This kind of thing can be accomplished by using Java Reflection!
How to load and invoke a method on an external jar:
File f = new File("plugin.jar");
URLClassLoader cl = new URLClassLoader(new URL[]{f.toURI().toURL(), null});
Class<?> clazz = cl.loadClass("epicurus.Client");
Method main = clazz.getMethod("main", String[].class);
main.invoke(null, new Object[]{new String[]{}});

Categories

Resources