Skip to content

Commit

Permalink
Support servers with broken getFirstJoin implementation - Release 1.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceDTRH committed May 8, 2022
1 parent 3a8163e commit 5f9f63f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>xyz.alicedtrh</groupId>
<artifactId>safetyblanket</artifactId>
<version>1.2.3-SNAPSHOT</version>
<version>1.2.4-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Safetyblanket</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public class SafetyBlanketConfig {

@Comment("Should this plugin check for updates on server start? (Default: True)")
public static boolean CHECK_FOR_UPDATES = true;

@Comment("Use manual method to store player age data (Only use this if your users are marked as new on every join)")
@Comment("If you use this, all users will be marked as new once and after that the plugin will keep track of player age")
@Comment("(Default: false)")
public static boolean MANUAL_PLAYER_AGE_STORAGE = false;
}
6 changes: 6 additions & 0 deletions src/main/java/xyz/alicedtrh/safetyblanket/Safetyblanket.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public final class Safetyblanket extends JavaPlugin {
static int blankets = 0;

public static final @NotNull NamespacedKey HAS_NEW_PLAYER_EFFECTS = NamespacedKey.minecraft("xyz.alicedtrh.safetyblanket.newplayer");
public static final @NotNull NamespacedKey PLAYER_AGE = NamespacedKey.minecraft("xyz.alicedtrh.safetyblanket.playerage");

public static Logger log() {
return Safetyblanket.getPlugin(Safetyblanket.class).getLogger();
Expand Down Expand Up @@ -73,4 +74,9 @@ public void onCommandEnableSafetyBlanket(CommandSender sender, Player player, In
sender.sendMessage(String.format("%s has been given test blanket.", player.getName()));
}

@CommandHook("timeuntilregular")
public void onCommandTimeUntilRegular(CommandSender sender, Player player) {
sender.sendMessage(String.valueOf(new SafetyblanketEvents().timeUntilRegular(player, TimeUnit.TICKS, 0)));
}

}
17 changes: 14 additions & 3 deletions src/main/java/xyz/alicedtrh/safetyblanket/SafetyblanketEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,12 @@ public void onPlayerJoinEvent(@NotNull PlayerJoinEvent event) {
return;
}

if (!event.getPlayer().getPersistentDataContainer().has(Safetyblanket.PLAYER_AGE)) {
event.getPlayer().getPersistentDataContainer().set(Safetyblanket.PLAYER_AGE, PersistentDataType.LONG, event.getPlayer().getLastLogin());
}

if (!isPlayerNew(event.getPlayer())) {
if(event.getPlayer().getPersistentDataContainer().has(Safetyblanket.HAS_NEW_PLAYER_EFFECTS)) {
if (event.getPlayer().getPersistentDataContainer().has(Safetyblanket.HAS_NEW_PLAYER_EFFECTS)) {
new ExpireSafetyBlanketTask(event.getPlayer()).runTask(plugin);
}
return;
Expand Down Expand Up @@ -208,7 +212,7 @@ protected void addSafetyBlanket(@NotNull Player player, int override) {
* Returns whether the player is still considered new to the server.
*/
private static boolean isPlayerNew(@NotNull Player player) {
return (System.currentTimeMillis() - player.getFirstPlayed()) < NEW_PLAYER_DURATION;
return (System.currentTimeMillis() - getFirstPlayed(player)) < NEW_PLAYER_DURATION;
}

/**
Expand All @@ -218,7 +222,7 @@ protected int timeUntilRegular(@NotNull Player player, @NotNull TimeUnit timeUni
int time_in_millis;
try {
time_in_millis = Math.toIntExact(
NEW_PLAYER_DURATION - (System.currentTimeMillis() - player.getFirstPlayed())
NEW_PLAYER_DURATION - (System.currentTimeMillis() - getFirstPlayed(player))
);
} catch (ArithmeticException e) {
time_in_millis = 0;
Expand Down Expand Up @@ -249,5 +253,12 @@ protected int timeUntilRegular(@NotNull Player player, @NotNull TimeUnit timeUni

}

protected static long getFirstPlayed(Player player) {
if (MANUAL_PLAYER_AGE_STORAGE) {
return player.getPersistentDataContainer().getOrDefault(Safetyblanket.PLAYER_AGE, PersistentDataType.LONG, player.getLastLogin());
}
return player.getFirstPlayed();
}


}
6 changes: 6 additions & 0 deletions src/main/resources/command.rdcml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ enableuserblanket player:player int:ticks {
permission safetyblanket.admin
help Give user a temporary test blanket.
hook enableuserblanket
}

timeuntilregular player:player {
permission safetyblanket.user
help Check how many ticks the player has left until they are no longer a new player
hook timeuntilregular
}

0 comments on commit 5f9f63f

Please sign in to comment.