Skip to content

Commit

Permalink
Save player teleport cooldowns (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts authored Dec 22, 2024
1 parent 3641be4 commit dab1417
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 99 deletions.
6 changes: 4 additions & 2 deletions src/main/java/serverutils/command/tp/CmdBack.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package serverutils.command.tp;

import static serverutils.data.TeleportType.BACK;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
Expand Down Expand Up @@ -41,7 +43,7 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
throw ServerUtilities.error(sender, "serverutilities.lang.warps.no_pos_found");
}

data.checkTeleportCooldown(sender, ServerUtilitiesPlayerData.Timer.BACK);
data.checkTeleportCooldown(sender, BACK);

Task task = new Task() {

Expand All @@ -55,6 +57,6 @@ public void execute(Universe universe) {
}
};

ServerUtilitiesPlayerData.Timer.BACK.teleport(player, playerMP -> lastTeleportLog.teleporter(), task);
data.teleport(lastTeleportLog.teleporter(), BACK, task);
}
}
5 changes: 3 additions & 2 deletions src/main/java/serverutils/command/tp/CmdHome.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import serverutils.ServerUtilities;
import serverutils.ServerUtilitiesPermissions;
import serverutils.data.ServerUtilitiesPlayerData;
import serverutils.data.TeleportType;
import serverutils.lib.command.CmdBase;
import serverutils.lib.command.CommandUtils;
import serverutils.lib.data.ForgePlayer;
Expand Down Expand Up @@ -113,7 +114,7 @@ public void processCommand(ICommandSender sender, String[] args0) throws Command
}

Task task = new NotifyTask(-1, player, TELEPORT.createNotification("serverutilities.lang.warps.tp", args[0]));
data.checkTeleportCooldown(sender, ServerUtilitiesPlayerData.Timer.HOME);
ServerUtilitiesPlayerData.Timer.HOME.teleport(player, playerMP -> pos.teleporter(), task);
data.checkTeleportCooldown(sender, TeleportType.HOME);
data.teleport(pos.teleporter(), TeleportType.HOME, task);
}
}
13 changes: 6 additions & 7 deletions src/main/java/serverutils/command/tp/CmdRTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import serverutils.ServerUtilitiesConfig;
import serverutils.data.ClaimedChunks;
import serverutils.data.ServerUtilitiesPlayerData;
import serverutils.data.TeleportType;
import serverutils.lib.command.CmdBase;
import serverutils.lib.command.CommandUtils;
import serverutils.lib.math.ChunkDimPos;
Expand All @@ -34,14 +35,12 @@ public CmdRTP() {
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
EntityPlayerMP player = getCommandSenderAsPlayer(sender);
ServerUtilitiesPlayerData data = ServerUtilitiesPlayerData.get(CommandUtils.getForgePlayer(player));
data.checkTeleportCooldown(sender, ServerUtilitiesPlayerData.Timer.RTP);
ServerUtilitiesPlayerData.Timer.RTP.teleport(
data.checkTeleportCooldown(sender, TeleportType.RTP);
TeleporterDimPos pos = findBlockPos(
player.mcServer.worldServerForDimension(ServerUtilitiesConfig.world.spawn_dimension),
player,
playerMP -> findBlockPos(
playerMP.mcServer.worldServerForDimension(ServerUtilitiesConfig.world.spawn_dimension),
player,
0),
null);
0);
data.teleport(pos, TeleportType.RTP, null);
}

private TeleporterDimPos findBlockPos(World world, EntityPlayerMP player, int depth) {
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/serverutils/command/tp/CmdSpawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;

import serverutils.ServerUtilitiesConfig;
import serverutils.data.ServerUtilitiesPlayerData;
import serverutils.data.TeleportType;
import serverutils.lib.command.CmdBase;
import serverutils.lib.command.CommandUtils;
import serverutils.lib.math.BlockDimPos;
import serverutils.lib.math.TeleporterDimPos;

public class CmdSpawn extends CmdBase {

Expand All @@ -22,16 +25,18 @@ public CmdSpawn() {
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
EntityPlayerMP player = getCommandSenderAsPlayer(sender);
ServerUtilitiesPlayerData data = ServerUtilitiesPlayerData.get(CommandUtils.getForgePlayer(player));
data.checkTeleportCooldown(sender, ServerUtilitiesPlayerData.Timer.SPAWN);
ServerUtilitiesPlayerData.Timer.SPAWN.teleport(player, playerMP -> {
World w = playerMP.mcServer.worldServerForDimension(ServerUtilitiesConfig.world.spawn_dimension);
ChunkCoordinates spawnpoint = w.getSpawnPoint();
data.checkTeleportCooldown(sender, TeleportType.SPAWN);
data.teleport(getSpawnTeleporter(), TeleportType.SPAWN, null);
}

private TeleporterDimPos getSpawnTeleporter() {
World w = DimensionManager.getWorld(ServerUtilitiesConfig.world.spawn_dimension);
ChunkCoordinates spawnpoint = w.getSpawnPoint();

while (w.getBlock(spawnpoint.posX, spawnpoint.posY, spawnpoint.posZ).isNormalCube()) {
spawnpoint.posY += 2;
}
while (w.getBlock(spawnpoint.posX, spawnpoint.posY, spawnpoint.posZ).isNormalCube()) {
spawnpoint.posY += 2;
}

return new BlockDimPos(spawnpoint, ServerUtilitiesConfig.world.spawn_dimension).teleporter();
}, null);
return new BlockDimPos(spawnpoint, ServerUtilitiesConfig.world.spawn_dimension).teleporter();
}
}
3 changes: 2 additions & 1 deletion src/main/java/serverutils/command/tp/CmdTPA.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import serverutils.ServerUtilities;
import serverutils.data.ServerUtilitiesPlayerData;
import serverutils.data.TeleportType;
import serverutils.lib.command.CmdBase;
import serverutils.lib.command.CommandUtils;
import serverutils.lib.data.Universe;
Expand All @@ -33,7 +34,7 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
checkArgs(sender, args, 1);
ServerUtilitiesPlayerData self = ServerUtilitiesPlayerData.get(CommandUtils.getForgePlayer(sender));

self.checkTeleportCooldown(sender, ServerUtilitiesPlayerData.Timer.TPA);
self.checkTeleportCooldown(sender, TeleportType.TPA);

ServerUtilitiesPlayerData other = ServerUtilitiesPlayerData.get(CommandUtils.getForgePlayer(sender, args[0]));

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/serverutils/command/tp/CmdTPAccept.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import serverutils.ServerUtilities;
import serverutils.ServerUtilitiesPermissions;
import serverutils.data.ServerUtilitiesPlayerData;
import serverutils.data.TeleportType;
import serverutils.lib.command.CmdBase;
import serverutils.lib.command.CommandUtils;
import serverutils.lib.math.TeleporterDimPos;
Expand Down Expand Up @@ -72,8 +73,6 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
otherName,
selfName)));
other.player.getPlayer().addChatMessage(component);

ServerUtilitiesPlayerData.Timer.TPA
.teleport(other.player.getPlayer(), playerMP -> TeleporterDimPos.of(selfPlayer), null);
other.teleport(TeleporterDimPos.of(selfPlayer), TeleportType.TPA, null);
}
}
5 changes: 3 additions & 2 deletions src/main/java/serverutils/command/tp/CmdWarp.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import serverutils.ServerUtilitiesPermissions;
import serverutils.data.ServerUtilitiesPlayerData;
import serverutils.data.ServerUtilitiesUniverseData;
import serverutils.data.TeleportType;
import serverutils.lib.command.CmdBase;
import serverutils.lib.command.CommandUtils;
import serverutils.lib.math.BlockDimPos;
Expand Down Expand Up @@ -67,14 +68,14 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
}

ServerUtilitiesPlayerData data = ServerUtilitiesPlayerData.get(CommandUtils.getForgePlayer(player));
data.checkTeleportCooldown(sender, ServerUtilitiesPlayerData.Timer.WARP);
data.checkTeleportCooldown(sender, TeleportType.WARP);

if (player.dimension != p.dim
&& !PermissionAPI.hasPermission(player, ServerUtilitiesPermissions.WARPS_CROSS_DIM)) {
throw ServerUtilities.error(sender, "serverutilities.lang.warps.cross_dim");
}

Task task = new NotifyTask(-1, player, TELEPORT.createNotification("serverutilities.lang.warps.tp", args[0]));
ServerUtilitiesPlayerData.Timer.WARP.teleport(player, playerMP -> p.teleporter(), task);
data.teleport(p.teleporter(), TeleportType.WARP, task);
}
}
69 changes: 24 additions & 45 deletions src/main/java/serverutils/data/ServerUtilitiesPlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.function.Function;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -45,45 +44,6 @@ public class ServerUtilitiesPlayerData extends PlayerData {
public static final String TAG_MUTED = "muted";
public static final String TAG_LAST_CHUNK = "serveru_lchunk";

public enum Timer {

HOME(TeleportType.HOME),
WARP(TeleportType.WARP),
BACK(TeleportType.BACK),
SPAWN(TeleportType.SPAWN),
TPA(TeleportType.TPA),
RTP(TeleportType.RTP),
VANILLA_TP(TeleportType.VANILLA_TP);

public static final Timer[] VALUES = values();

private final String cooldown;
private final String warmup;
private final TeleportType teleportType;

Timer(TeleportType teleportType) {
this.teleportType = teleportType;
this.cooldown = teleportType.getCooldownPermission();
this.warmup = teleportType.getWarmupPermission();
}

public void teleport(EntityPlayerMP player, Function<EntityPlayerMP, TeleporterDimPos> pos,
@Nullable Task extraTask) {
Universe universe = Universe.get();
int seconds = (int) RankConfigAPI.get(player, warmup).getTimer().seconds();

if (seconds > 0) {
IChatComponent component = StringUtils.color(
ServerUtilities.lang("stand_still", seconds).appendText(" [" + seconds + "]"),
EnumChatFormatting.GOLD);
TELEPORT_WARMUP.createNotification(component).setVanilla(true).send(player);
universe.scheduleTask(new TeleportTask(teleportType, player, this, seconds, pos, extraTask));
} else {
new TeleportTask(teleportType, player, this, 0, pos, extraTask).execute(universe);
}
}
}

public static ServerUtilitiesPlayerData get(ForgePlayer player) {
return player.getData().get(ServerUtilities.MOD_ID);
}
Expand All @@ -97,15 +57,13 @@ public static ServerUtilitiesPlayerData get(ForgePlayer player) {
private IChatComponent cachedNameForChat;

private BlockDimPos lastSafePos;
public final long[] lastTeleport;
public final BlockDimPosStorage homes;
private final TeleportTracker teleportTracker;

public ServerUtilitiesPlayerData(ForgePlayer player) {
super(player);
homes = new BlockDimPosStorage();
tpaRequestsFrom = new HashSet<>();
lastTeleport = new long[Timer.VALUES.length];
teleportTracker = new TeleportTracker();
}

Expand Down Expand Up @@ -191,10 +149,13 @@ public BlockDimPos getLastSafePos() {
return lastSafePos;
}

public void checkTeleportCooldown(ICommandSender sender, Timer timer) throws CommandException {
long cooldown = lastTeleport[timer.ordinal()] + player.getRankConfig(timer.cooldown).getTimer().millis()
- System.currentTimeMillis();
public void checkTeleportCooldown(ICommandSender sender, TeleportType teleportType) throws CommandException {
String cooldownPermission = teleportType.getCooldownPermission();
if (cooldownPermission == null) return;

long cooldown = teleportTracker.getLastTeleportTime(teleportType)
+ player.getRankConfig(cooldownPermission).getTimer().millis()
- System.currentTimeMillis();
if (cooldown > 0) {
throw ServerUtilities.error(sender, "cant_use_now_cooldown", StringUtils.getTimeString(cooldown));
}
Expand Down Expand Up @@ -266,6 +227,24 @@ public IChatComponent getNameForChat() {
return cachedNameForChat.createCopy();
}

public void teleport(TeleporterDimPos pos, TeleportType teleportType, @Nullable Task extraTask) {
EntityPlayerMP player = this.player.getPlayer();
Universe universe = Universe.get();
String warmup = teleportType.getWarmupPermission();
int seconds = warmup == null ? 0 : (int) RankConfigAPI.get(player, warmup).getTimer().seconds();

if (seconds > 0) {
IChatComponent component = StringUtils.color(
ServerUtilities.lang(player, "stand_still", seconds).appendText(" [" + seconds + "]"),
EnumChatFormatting.GOLD);
TELEPORT_WARMUP.createNotification(component).setVanilla(true).send(player);

universe.scheduleTask(new TeleportTask(teleportType, player, seconds, pos, extraTask));
} else {
new TeleportTask(teleportType, player, 0, pos, extraTask).execute(universe);
}
}

public TeleportLog getLastTeleportLog() {
return teleportTracker.getLastAvailableLog(player.getProfile());
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/serverutils/data/TeleportLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TeleportLog implements INBTSerializable<NBTTagCompound>, Comparable
private static final String NBT_KEY_TELEPORT_TYPE = "teleport_type";
private static final String NBT_KEY_CREATED_AT = "created_at";

private static Comparator<TeleportLog> comparator = Comparator
private static final Comparator<TeleportLog> comparator = Comparator
.comparing((log) -> log == null ? null : log.getCreatedAt(), Comparator.nullsFirst(Long::compareTo));

public TeleportType teleportType;
Expand Down Expand Up @@ -55,6 +55,7 @@ public void deserializeNBT(NBTTagCompound nbt) {
posZ = nbt.getInteger(NBT_KEY_Z);
dimension = nbt.getInteger(NBT_KEY_DIMENSION);
teleportType = nbt.getInteger(NBT_KEY_TELEPORT_TYPE);
this.createdAt = nbt.getLong(NBT_KEY_CREATED_AT);
this.from = new BlockDimPos(posX, posY, posZ, dimension);
this.teleportType = TeleportType.values()[teleportType];
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/serverutils/data/TeleportTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public TeleportLog getLastLog() {
return logs[0];
}

public long getLastTeleportTime(TeleportType teleportType) {
TeleportLog log = logs[teleportType.ordinal()];
return log == null ? -1 : log.getCreatedAt();
}

public void clearLog(TeleportType teleportType) {
logs[teleportType.ordinal()] = null;
}
Expand All @@ -73,7 +78,10 @@ public NBTTagCompound serializeNBT() {
@Override
public void deserializeNBT(NBTTagCompound nbt) {
for (int i = 0; i < logs.length; i++) {
logs[i] = new TeleportLog(nbt.getCompoundTag(String.valueOf(i)));
String key = String.valueOf(i);
if (nbt.hasKey(key)) {
logs[i] = new TeleportLog(nbt.getCompoundTag(key));
}
}
}

Expand Down
Loading

0 comments on commit dab1417

Please sign in to comment.