Skip to content

Commit

Permalink
Save last online time self instead of using the Bukkit API
Browse files Browse the repository at this point in the history
The Bukkit API returns an incorrect last online time when the player.dat
file is not present (for example when a server saves inventories etc.
somewhere else, or when it is cleaned).

Improves landlord handling when there is no UUID present in the config
files.

Closes #34
  • Loading branch information
NLthijs48 committed Jul 28, 2015
1 parent d466658 commit f76c1b8
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import nl.evolutioncoding.areashop.interfaces.AreaShopInterface;
import nl.evolutioncoding.areashop.interfaces.WorldEditInterface;
import nl.evolutioncoding.areashop.interfaces.WorldGuardInterface;
import nl.evolutioncoding.areashop.listeners.PlayerLoginListener;
import nl.evolutioncoding.areashop.listeners.PlayerLoginLogoutListener;
import nl.evolutioncoding.areashop.listeners.SignBreakListener;
import nl.evolutioncoding.areashop.listeners.SignChangeListener;
import nl.evolutioncoding.areashop.listeners.SignClickListener;
Expand Down Expand Up @@ -74,7 +74,7 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface {

/* Constants for handling file versions */
public static final String versionFiles = "files";
public static final int versionFilesCurrent = 2;
public static final int versionFilesCurrent = 3;

/* Keys for replacing parts of flags, commands, strings */
public static final String tagPlayerName = "%player%";
Expand Down Expand Up @@ -211,7 +211,7 @@ public void onEnable() {
this.getServer().getPluginManager().registerEvents(new SignChangeListener(this), this);
this.getServer().getPluginManager().registerEvents(new SignBreakListener(this), this);
this.getServer().getPluginManager().registerEvents(new SignClickListener(this), this);
this.getServer().getPluginManager().registerEvents(new PlayerLoginListener(this), this);
this.getServer().getPluginManager().registerEvents(new PlayerLoginLogoutListener(this), this);

setupTasks();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void execute(CommandSender sender, Command command, String[] args) {
return;
}
OfflinePlayer friend = Bukkit.getOfflinePlayer(args[1]);
if(friend.getLastPlayed() == 0) {
if(friend.getLastPlayed() == 0 && !plugin.getConfig().getBoolean("addFriendNotExistingPlayers")) {
plugin.message(sender, "addfriend-notVisited", args[1]);
return;
}
Expand All @@ -101,7 +101,7 @@ public void execute(CommandSender sender, Command command, String[] args) {
if(sender.hasPermission("areashop.addfriend") && sender instanceof Player) {
if(region.isOwner((Player)sender)) {
OfflinePlayer friend = Bukkit.getOfflinePlayer(args[1]);
if(friend.getLastPlayed() == 0) {
if(friend.getLastPlayed() == 0 && !plugin.getConfig().getBoolean("addFriendNotExistingPlayers")) {
plugin.message(sender, "addfriend-notVisited", args[1]);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ public void execute(CommandSender sender, Command command, String[] args) {
return;
}
@SuppressWarnings("deprecation")
OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]);
if(player == null || player.getLastPlayed() == 0) {
plugin.message(sender, "setlandlord-didNotPlayBefore", args[1]); // Using args[1] instead of playername because that could return nothing if not played before
}
OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]);
GeneralRegion region = null;
if(args.length < 3) {
if (sender instanceof Player) {
Expand All @@ -69,14 +66,14 @@ public void execute(CommandSender sender, Command command, String[] args) {
region = plugin.getFileManager().getRegion(args[2]);
}
if(region == null) {
plugin.message(player, "setlandlord-noRegion", args[2]);
plugin.message(sender, "setlandlord-noRegion", args[2]);
return;
}
region.setLandlord(player.getUniqueId(), args[1]);
String playerName = player.getName();
if(playerName.isEmpty()) {
if(playerName == null || playerName.isEmpty()) {
playerName = args[1];
}
region.setLandlord(player.getUniqueId(), playerName);
plugin.message(sender, "setlandlord-success", playerName, region.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;

/**
* Checks for placement of signs for this plugin
* @author NLThijs48
*/
public final class PlayerLoginListener implements Listener {
public final class PlayerLoginLogoutListener implements Listener {
AreaShop plugin;

/**
* Constructor
* @param plugin The AreaShop plugin
*/
public PlayerLoginListener(AreaShop plugin) {
public PlayerLoginLogoutListener(AreaShop plugin) {
this.plugin = plugin;
}

Expand Down Expand Up @@ -112,6 +114,29 @@ public void run() {
}
}.runTaskTimer(plugin, 22, 1); // Wait a bit before starting to prevent a lot of stress on the server when a player joins (a lot of plugins already do stuff then)
}

// Active time updates
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerLogout(PlayerQuitEvent event) {
updateLastActive(event.getPlayer());
}

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerKick(PlayerKickEvent event) {
updateLastActive(event.getPlayer());
}

/**
* Update the last active time for all regions the player is owner off
* @param player The player to update the active times for
*/
public void updateLastActive(Player player) {
for(GeneralRegion region : plugin.getFileManager().getRegions()) {
if(region.isOwner(player)) {
region.updateLastActiveTime();
}
}
}
}


Expand Down
Loading

0 comments on commit f76c1b8

Please sign in to comment.