Skip to content

Commit

Permalink
Add PlayerDataLockedEvent.java
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Jun 10, 2019
1 parent 6914550 commit 81f2f92
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mengcraft.playersql.event;

import lombok.Data;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;

import java.sql.Timestamp;

@Data
public class PlayerDataLockedEvent extends PlayerEvent {

public static final HandlerList HANDLER_LIST = new HandlerList();
private final Timestamp lastUpdate;
private Result result = Result.DEFAULT;

public PlayerDataLockedEvent(Player who, Timestamp lastUpdate) {
super(who);
this.lastUpdate = lastUpdate;
}

@Override
public HandlerList getHandlers() {
return null;
}

public static HandlerList getHandlerList() {
return HANDLER_LIST;
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/mengcraft/playersql/task/FetchUserTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import com.mengcraft.playersql.PlayerData;
import com.mengcraft.playersql.PluginMain;
import com.mengcraft.playersql.UserManager;
import com.mengcraft.playersql.event.PlayerDataLockedEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.UUID;

import static com.mengcraft.playersql.PluginMain.nil;
Expand Down Expand Up @@ -57,6 +61,23 @@ public synchronized void run() {
} else {
cancel();

PlayerDataLockedEvent e = new PlayerDataLockedEvent(player, user.getLastUpdate());
Bukkit.getPluginManager().callEvent(e);
switch (e.getResult()) {
case DENY:
kickPlayer();
return;
case DEFAULT:
// Todo configurable lock hold time.
if (e.getLastUpdate().toInstant().until(Instant.now(), ChronoUnit.MINUTES) <= 10) {
kickPlayer();
return;
}
break;
case ALLOW:
break;
}

LocalDataMgr.transfer(player);// TODO move to server thread if any exception

manager.addFetched(user);
Expand All @@ -69,4 +90,8 @@ public synchronized void run() {
}
}

private void kickPlayer() {
player.kickPlayer("Your player data has been locked.\nYou should wait some minutes or contact server operator.");
}

}

0 comments on commit 81f2f92

Please sign in to comment.