Skip to content

Commit

Permalink
add cooldown config for visit command
Browse files Browse the repository at this point in the history
  • Loading branch information
MelanX committed Apr 10, 2024
1 parent 28f1bca commit cb6487e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Expand Up @@ -7,6 +7,7 @@
import de.melanx.skyblockbuilder.data.SkyblockSavedData;
import de.melanx.skyblockbuilder.data.Team;
import de.melanx.skyblockbuilder.events.SkyblockHooks;
import de.melanx.skyblockbuilder.util.RandomUtility;
import de.melanx.skyblockbuilder.util.WorldUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
Expand Down Expand Up @@ -37,6 +38,12 @@ private static int visit(CommandSourceStack source, String name) throws CommandS
return 0;
}

if (!player.hasPermissions(2) && !data.getOrCreateMetaInfo(player).canVisit(level.getGameTime())) {
source.sendFailure(Component.translatable("skyblockbuilder.command.error.cooldown",
RandomUtility.formattedCooldown(PermissionsConfig.Teleports.visitCooldown - (level.getGameTime() - data.getOrCreateMetaInfo(player).getLastVisitTeleport()))));
return 0;
}

if (!player.hasPermissions(2) && !PermissionsConfig.Teleports.teleportationDimensions.test(player.level().dimension().location())) {
source.sendFailure(Component.translatable("skyblockbuilder.command.error.teleportation_not_allowed_dimension"));
return 0;
Expand Down Expand Up @@ -72,6 +79,7 @@ private static int visit(CommandSourceStack source, String name) throws CommandS
}

WorldUtil.teleportToIsland(player, team);
data.getOrCreateMetaInfo(player).setLastVisitTeleport(level.getGameTime());
source.sendSuccess(() -> Component.translatable("skyblockbuilder.command.success.visit_team", name).withStyle(ChatFormatting.GOLD), true);
return 1;
}
Expand Down
Expand Up @@ -29,6 +29,9 @@ public static class Teleports {
@Config("Should players be able to visit other island? [default: true]")
public static boolean allowVisits = true;

@Config("Cooldown in ticks for visiting other islands. [default: 3600 = 3min]")
public static int visitCooldown = 3600;

@Config("Should players be able to teleport to their home island? [default: true]")
public static boolean home = true;

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/de/melanx/skyblockbuilder/data/SkyMeta.java
Expand Up @@ -22,6 +22,7 @@ public class SkyMeta {
private UUID teamId = SkyblockSavedData.SPAWN_ID;
private long lastHomeTeleport;
private long lastSpawnTeleport;
private long lastVisitTeleport;

public static SkyMeta get(SkyblockSavedData data, @Nonnull CompoundTag nbt) {
return new SkyMeta(data, null).load(nbt);
Expand Down Expand Up @@ -115,6 +116,21 @@ public boolean canTeleportSpawn(long gameTime) {
return (this.lastSpawnTeleport == 0 ? PermissionsConfig.Teleports.spawnCooldown : gameTime) - this.getLastSpawnTeleport() >= PermissionsConfig.Teleports.spawnCooldown;
}

public long getLastVisitTeleport() {
return this.lastVisitTeleport;
}

public void setLastVisitTeleport(long gameTime) {
this.lastVisitTeleport = gameTime;
if (this.data != null) {
this.data.setDirtySilently();
}
}

public boolean canVisit(long gameTime) {
return (this.lastVisitTeleport == 0 ? PermissionsConfig.Teleports.visitCooldown : gameTime) - this.getLastVisitTeleport() >= PermissionsConfig.Teleports.visitCooldown;
}

public SkyMeta load(@Nonnull CompoundTag nbt) {
this.owner = nbt.getUUID("OwnerId");
this.teamId = nbt.getUUID("TeamId");
Expand All @@ -131,6 +147,7 @@ public SkyMeta load(@Nonnull CompoundTag nbt) {

this.lastHomeTeleport = nbt.getLong("LastHomeTeleport");
this.lastSpawnTeleport = nbt.getLong("LastSpawnTeleport");
this.lastVisitTeleport = nbt.getLong("LastVisitTeleport");

return this;
}
Expand All @@ -155,6 +172,7 @@ public CompoundTag save() {
nbt.put("Invitations", invitationTeams);
nbt.putLong("LastHomeTeleport", this.lastHomeTeleport);
nbt.putLong("LastSpawnTeleport", this.lastSpawnTeleport);
nbt.putLong("LastVisitTeleport", this.lastVisitTeleport);
return nbt;
}
}

0 comments on commit cb6487e

Please sign in to comment.