Bukkit - Update a sign every second - scheduler

I am trying to make a sign update every second, but I just don't know what to do.
I already have a finished code with a scheduler, but I just don't know where I should put it.
It would be very very nice if someone could help me or even give me an example code...
Here is my code:
public class Main extends JavaPlugin implements Listener{
DateFormat dFormat = new SimpleDateFormat("HH:mm");
Date d = new Date();
public void onEnable() {
System.out.println("[BookshelfDrop] BookshelfDrop v" + this.getDescription().getVersion() + " enabled");
getServer().getPluginManager().registerEvents(this, this);
}
public void onDisable() {
System.out.println("[BookshelfDrop] BookshelfDrop disabled");
}
#EventHandler
public void onChange(SignChangeEvent event) {
if (event.getLine(0).equalsIgnoreCase("[clock]")) {
Sign s = (Sign) event.getBlock().getState();
s.setLine(0, "[clock]");
Bukkit.getServer()
.getScheduler()
.scheduleSyncRepeatingTask(
Bukkit.getPluginManager().getPlugin("BookshelfDrop"),
new Runnable() {
public void run() {
s.setLine(1, "§a" + dFormat.format(d));
s.update();
}
}, 0, 20L);
}
}

You can create an ArrayList to put all your signs
public ArrayList<Sign> yourSigns = new ArrayList<Sign>();
Then add this to your onSignChange
public void onChange(SignChangeEvent event) {
if (event.getLine(0).equalsIgnoreCase("[clock]")) {
Sign s = (Sign) event.getBlock().getState();
yourSigns.add(s);
}
On your onEnable() method, you can then put your clock, making a loop through each sign on your signs list.

Related

Boolean not changing values from one class to another

I'm currently working on this Clash Royale style game in Java FX and I have a game timer in one class that I set in my test condition. However, when I check its value over on the GameView class it's always false.
I've looked at many examples and the latest I tried making my properties static in my timer class but that didn't have any effect either. Hoping someone could steel me in the right direction. Thanks!
public class GameTimer {
public int seconds = 180;
public static boolean endGame;
public static boolean returnBool () {
return endGame;
}
public static boolean isEndGame() {
return endGame;
}
public static void setEndGame(boolean endGame) {
GameTimer.endGame = endGame;
}
Timer timer = new Timer();
TimerTask task = new TimerTask(){
public void run(){
seconds--;
System.out.println("180 / " + seconds);
if (seconds == 170){
System.out.println("170 seconds hit the spot" );
endGame = true;
this.cancel();
}
}
};
public void start(){
timer.scheduleAtFixedRate(task, 1000, 1000);
isEndGame();
}
}
public class GameViewManager {
public void gameTimer() {
GameTimer timer = new GameTimer();
timer.start();
//System.out.println(timer.returnBool());
if (timer.isEndGame()){
//gameStage.close();
System.out.println("test");
}
}
}

GWT Fire ValueChangeEvent after a specific time

I have an webapp with a large collection. So my user get a text box to filter the collection.
But every time the user put in one letter the filter action starts. This is very slow some times.
So I want only get the value change event when the user stop typing for one second.
I tried it this way:
#Override
public void onValueChange( ValueChangeEvent<String> event )
{
Timer t = new Timer()
{
#Override
public void run()
{
addChangeHandler( new ChangeHandler()
{
#Override
public void onChange( ChangeEvent event1 )
{
ValueChangeEvent.fire( TextBoxPSG.this, getValue() );
}
} );
}
};
t.schedule( 15000 );
}
But this doesn't work.
Maybe someone has an Idea or the same problem.
Thanks in advance.
Do not use ValueChangeEvent. Use KeyUpEvent.
private static Timer timer = new Timer() {
#Override
public void run() {
// do you filter work;
}
};
...
myTextBox.addKeyUpHandler(new KeyUpHandler() {
#Override
public void onKeyUp(KeyUpEvent event) {
if (timer.isRunning()) {
timer.cancel();
}
timer.schedule(1000);
}
});

Timer is still firing after Clicking on other Links in GWT

So I have a timer and it keeps on firing even though I cleared the Panel and loaded other model... my question is, how to cancel a timer when I unload a model?
So here is part of my code
public Display(List<Clarification> result) {
if (result.size() == 0) {
Window.alert("EMPTY");
} else {
RootPanel.get("Dev1").clear();
t = new Timer() {
public void run() {
cd = new ClarificationDispatcher();
cd.getClarificationsCount(result.size());
}
};
t.scheduleRepeating(5000);
}
I tried to cancel the Timer onUnload() method however, I don't believe it is getting called at all...
Thanks!
Steps to follow
use window.onunload event that is called when page is refreshed
first export cancelTimer() method to java script using JSNI and register cancelTimerFunction as java script function that is called on page unload
cancel timer on window close
Code:
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
private static Timer timer = null;
public void onModuleLoad() {
exportCancelTimer();
final Label label = new Label("Hello ");
timer = new Timer() {
#Override
public void run() {
label.setText("Hello " + Math.random() * 100);
}
};
timer.scheduleRepeating(500);
RootPanel.get().add(label);
Window.addCloseHandler(new CloseHandler<Window>() {
#Override
public void onClose(CloseEvent<Window> event) {
timer.cancel();
}
});
}
public static void cancelTimer() {
if (timer != null) {
System.out.println("cancel");
timer.cancel();
}
}
public static native void exportCancelTimer() /*-{
$wnd.cancelTimerFunction = $entry(#com.x.y.z.GWTProject::cancelTimer());
$wnd.onunload = $wnd.cancelTimerFunction;
}-*/;

GWT ValueChangeHandler and getting before value

I want to get values of my textBox before change its value and after changed its value.
String beforeValue = "";
TextBox textBox = new TextBox();
textBox.addFocusHandler(new FocusHandler() {
public void onFocus(final FocusEvent event) {
beforeValue = textBox.getText();
}
});
textBox.addValueChangeHandler(new ValueChangeHandler<String>() {
public void onValueChange(final ValueChangeEvent<String> event) {
System.out.println("Before value is " + beforeValue);
System.out.println("After value is " + textBox.getText());
}
});
As above codes , I need two handlers (FocusHandler and ValueChangeHadler) to get before value and after value . My question is how can I get it by one Handler or another simple and easy way ? I don't want to use two handlers to get it. Any suggestions would be appreciated. Thanks in advance !
Your idea(using 2 handlers) is fair enough but its buggy. I don't think it can be done in a better way. If you want to use a single handler, create a custom class wrapper using the two handlers.
Here is the code for you.
public abstract class MyValueChangeHandler<T> implements ValueChangeHandler<T> {
T prevValue = null;
T value = null;
public MyValueChangeHandler(final ValueBoxBase<T> widget) {
widget.addFocusHandler(new FocusHandler() {
public void onFocus(FocusEvent event) {
prevValue = widget.getValue();
}
});
}
#Override
public void onValueChange(ValueChangeEvent<T> event) {
value = event.getValue();
onValueChange(value, prevValue);
// or
// onValueChange(event, prevValue);
prevValue = value;
}
public abstract void onValueChange(T value, T prevValue);
// or
// public abstract void onValueChange(ValueChangeEvent<T> event, T prevValue);
}
And you can use it as,
TextBox box = new TextBox();
box.addValueChangeHandler(new MyValueChangeHandler<String>(box) {
#Override
public void onValueChange(String value, String prevValue) {
Window.alert("Prev Value : " + prevValue + " CurrnetValue: "
+ value);
}
});

gwt client session time out

I am using gwt 2.3 with gwtp framework.In this application I wan to maintain a session time of 5 mins.This means if current user is not doing up to 5 min and he comes after five min then on his first event/action on screen a he should be be logged out.
In gwt there is class named Timer which can be used in this issues.But I am not getting how to recognize action of user on the screen.I did google on it, & found the code for gwt-ext.Below is the code of gwt-ext
Ext.get(“pagePanel”).addListener(“click”, new EventCallback() {
#Override
public void execute(EventObject e) {
MessageBox.alert(“On Mouse Click”);
}
});
Ext.get(“pagePanel”).addListener(“keydown”, new EventCallback() {
#Override
public void execute(EventObject e) {
MessageBox.alert(“On Key Press Click”);
}
});
In above code tag in working properly so I am attaching link from where I got this code.here
Same type of code I am looking in gwt.If there any other better way to do this then please let me know. Thanks in advance
If action/event can be really everythin, I would solve it with a
NativePreviewHandler in the following way:
boolean expired;
final Timer logoutTimer = new Timer() {
#Override
public void run() {
expired = true;
}
};
NativePreviewHandler nph = new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (!expired) {
logoutTimer.cancel();
logoutTimer.schedule(300000);
} else {
// do your logout stuff here
}
}
};
Event.addNativePreviewHandler(nph);
If the user shell be logged out without a new action after 5 minutes:
final Timer logoutTimer = new Timer() {
#Override
public void run() {
// do your logout stuff here
}
};
NativePreviewHandler nph = new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
// Of course do this only when logged in:
logoutTimer.cancel();
logoutTimer.schedule(300000);
}
};
Event.addNativePreviewHandler(nph);