-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from FTBTeam/1.20.1/dev
1.20.1/dev
- Loading branch information
Showing
19 changed files
with
403 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
common/src/main/java/dev/ftb/mods/ftbultimine/CooldownTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dev.ftb.mods.ftbultimine; | ||
|
||
import dev.ftb.mods.ftbultimine.config.FTBUltimineServerConfig; | ||
import dev.ftb.mods.ftbultimine.net.SyncUltimineTimePacket; | ||
import dev.ftb.mods.ftbultimine.net.SyncUltimineTimePacket.TimeType; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.util.Mth; | ||
import net.minecraft.world.entity.player.Player; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class CooldownTracker { | ||
private static final CooldownTracker clientInstance = new CooldownTracker(); | ||
private static final CooldownTracker serverInstance = new CooldownTracker(); | ||
|
||
private final Map<UUID,Long> lastUltimineTime = new HashMap<>(); | ||
|
||
// client can't just look at the server config (even though it's sync'd) since we could be using Ranks... | ||
// so this get sync'd when the server config changes, or when we get a Ranks event indicating a change | ||
private static long ultimineCooldownClient; | ||
|
||
public static long getLastUltimineTime(Player player) { | ||
CooldownTracker instance = player.level().isClientSide ? clientInstance : serverInstance; | ||
return instance.lastUltimineTime.getOrDefault(player.getUUID(), 0L); | ||
} | ||
|
||
public static void setLastUltimineTime(Player player, long when) { | ||
CooldownTracker instance = player.level().isClientSide ? clientInstance : serverInstance; | ||
instance.lastUltimineTime.put(player.getUUID(), when); | ||
if (player instanceof ServerPlayer sp) { | ||
new SyncUltimineTimePacket(when, TimeType.LAST_USED).sendTo(sp); | ||
} | ||
} | ||
|
||
/** | ||
* Get the remaining cooldown time for the player as a proportion of total time | ||
* @param player the player to check | ||
* @return a value in the range 0f -> 1f | ||
*/ | ||
public static float getCooldownRemaining(Player player) { | ||
long coolDown = getUltimineCooldown(player); | ||
if (coolDown == 0L) { | ||
return 1f; | ||
} | ||
long tickDelta = (System.currentTimeMillis() - getLastUltimineTime(player)) / 50; // ms -> ticks | ||
return Mth.clamp((float)tickDelta / coolDown, 0f, 1f); | ||
} | ||
|
||
public static boolean isOnCooldown(Player player) { | ||
long coolDown = getUltimineCooldown(player); | ||
if (coolDown == 0L) { | ||
return false; | ||
} | ||
long coolDownMs = coolDown * 50; // ticks -> ms | ||
return coolDownMs > 0L && System.currentTimeMillis() - getLastUltimineTime(player) < coolDownMs; | ||
} | ||
|
||
private static long getUltimineCooldown(Player player) { | ||
return player instanceof ServerPlayer sp ? | ||
FTBUltimineServerConfig.getUltimineCooldown(sp) : | ||
ultimineCooldownClient; | ||
} | ||
|
||
public static void setClientCooldownTime(long cooldown) { | ||
ultimineCooldownClient = cooldown; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.