Skip to content

Commit

Permalink
Add enableuserblanket command to allow for testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceDTRH committed May 7, 2022
1 parent 5cf9daf commit 0676dec
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
14 changes: 11 additions & 3 deletions src/main/java/xyz/alicedtrh/safetyblanket/Safetyblanket.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void onEnable() {
int pluginId = 15141;
this.metrics = new Metrics(this, pluginId);

ConfigManager config = ConfigManager.create(this).target(SafetyBlanketConfig.class).saveDefaults().load();
/*ConfigManager config = */
ConfigManager.create(this).target(SafetyBlanketConfig.class).saveDefaults().load();
new CommandParser(this.getResource("command.rdcml")).parse().register("safetyblanket", this);
}

Expand All @@ -43,9 +44,16 @@ public void onDisable() {
}

@CommandHook("disableuserblanket")
public void onCommandSafetyBlanket(CommandSender sender, Player arg1) {
new ExpireSafetyBlanketTask(arg1).run();
public void onCommandDisableSafetyBlanket(CommandSender sender, Player player) {
new ExpireSafetyBlanketTask(player).run();
sender.sendMessage("Forcefully expired user blanket.");
}

@CommandHook("enableuserblanket")
public void onCommandEnableSafetyBlanket(CommandSender sender, Player player, Integer ticks) {
sender.sendMessage("Warning! This is only used for testing. When the user logs out and back in, the blanket will disappear.");
new SafetyblanketEvents().addSafetyBlanket(player, ticks);
sender.sendMessage(String.format("%s has been given test blanket.", player.getName()));
}

}
29 changes: 23 additions & 6 deletions src/main/java/xyz/alicedtrh/safetyblanket/SafetyblanketEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,31 @@ public void onPlayerJoinEvent(@NotNull PlayerJoinEvent event) {

/**
* Cover player in blanket
*
* @param player Player to give blanket
*/
private void addSafetyBlanket(@NotNull Player player) {
addSafetyBlanket(player, 0);
}

/**
* Cover player in blanket
*
* @param player Player to give blanket
* @param override When supplied, the amount of time the forced blanket should be active for
*/
protected void addSafetyBlanket(@NotNull Player player, int override) {
Safetyblanket.blankets++;

new ExpireSafetyBlanketTask(player).runTaskLater(plugin, timeUntilRegular(player, TimeUnit.TICKS));
new ExpireSafetyBlanketTask(player).runTaskLater(plugin, timeUntilRegular(player, TimeUnit.TICKS, override));

player.getPersistentDataContainer().set(Safetyblanket.HAS_NEW_PLAYER_EFFECTS, PersistentDataType.SHORT, (short) 1);
if (!player.hasPlayedBefore()) {
if (PREVENT_TARGETING) {
player.sendMessage("Enemies won't target you for " + timeUntilRegular(player, TimeUnit.MINUTES) + " minutes, unless you attack them first.");
player.sendMessage(String.format("Enemies won't target you for %d minutes, unless you attack them first.", timeUntilRegular(player, TimeUnit.MINUTES, override)));
}
if (REGEN_BOOST) {
player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, timeUntilRegular(player, TimeUnit.TICKS), 0, true, true, false));
player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, timeUntilRegular(player, TimeUnit.TICKS, override), 0, true, true, false));
}
}
if (REGEN_BOOST) {
Expand All @@ -186,7 +199,7 @@ private void addSafetyBlanket(@NotNull Player player) {
if (PREVENT_MOB_SPAWNS) {
player.setAffectsSpawning(false);
if (EARLY_MOB_SPAWN_DISABLE) {
new EarlyExpireSafetyBlanketTask(player).runTaskLater(plugin, (long) (timeUntilRegular(player, TimeUnit.TICKS) * EARLY_MOB_SPAWN_DISABLE_PERCENT));
new EarlyExpireSafetyBlanketTask(player).runTaskLater(plugin, (long) (timeUntilRegular(player, TimeUnit.TICKS, override) * EARLY_MOB_SPAWN_DISABLE_PERCENT));
}
}
}
Expand All @@ -201,7 +214,7 @@ private static boolean isPlayerNew(@NotNull Player player) {
/**
* The amount of time the player has left until they are no longer considered new to the server.
*/
private int timeUntilRegular(@NotNull Player player, @NotNull TimeUnit timeUnit) {
protected int timeUntilRegular(@NotNull Player player, @NotNull TimeUnit timeUnit, int override) {
int time_in_millis;
try {
time_in_millis = Math.toIntExact(
Expand All @@ -212,8 +225,12 @@ private int timeUntilRegular(@NotNull Player player, @NotNull TimeUnit timeUnit)
Safetyblanket.log().warning("Overflow when trying to calculate player time: " + e.getLocalizedMessage());
}

if (override > 0) {
time_in_millis = override * 20;
}

// If it's less than (or equal to) zero, the result will always be zero regardless of timeUnit.
if(time_in_millis <= 0) {
if (time_in_millis <= 0) {
return 0;
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/resources/command.rdcml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
disableuserblanket player:arg {
disableuserblanket player:player {
permission safetyblanket.admin
help This lets you forcefully disable the blanket for a user.
help Remove user blanket. (Restored after rejoin if user is still new)
hook disableuserblanket
}

enableuserblanket player:player int:ticks {
permission safetyblanket.admin
help Give user a temporary test blanket.
hook enableuserblanket
}

0 comments on commit 0676dec

Please sign in to comment.