Skip to content

Commit

Permalink
Improve api
Browse files Browse the repository at this point in the history
  • Loading branch information
BoomEaro committed Apr 25, 2024
1 parent 92e9109 commit 279a42a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
26 changes: 15 additions & 11 deletions src/main/java/ru/boomearo/board/objects/PlayerBoardImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ru.boomearo.board.objects.boards.*;
import ru.boomearo.board.tasks.UsedExecutor;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -335,21 +336,24 @@ private void handleBoard() {
}

// Сменяем страницу только если прошло время, иначе просто обновляем ее
if ((System.currentTimeMillis() - this.pageCreateTime) > thisPage.getTimeToChangePage()) {
Duration duration = thisPage.getTimeToChangePage();
if (duration != null) {
if ((System.currentTimeMillis() - this.pageCreateTime) > duration.toMillis()) {

// Убеждаемся что текущая страница не является следующей
if (this.pageIndex != nextPageIndex) {
// Если оказывается что в настройках игрока отключен авто скролл, то просто обновляем страницу.
// Иначе пытаемся открыть следующую страницу.
if (this.permanentView) {
update();
return;
}

toPage0(nextPageIndex, nextPage);

// Убеждаемся что текущая страница не является следующей
if (this.pageIndex != nextPageIndex) {
// Если оказывается что в настройках игрока отключен авто скролл, то просто обновляем страницу.
// Иначе пытаемся открыть следующую страницу.
if (this.permanentView) {
update();
return;
}

toPage0(nextPageIndex, nextPage);

update();
return;
}
}
update();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.ChatColor;
import ru.boomearo.board.objects.PlayerBoard;

import java.time.Duration;
import java.util.logging.Level;

@Getter
Expand All @@ -22,7 +23,12 @@ public AbstractHolder(AbstractPage page) {
}

public T getHolderResult() {
if ((System.currentTimeMillis() - this.cacheTime) > getMaxCacheTime()) {
Duration duration = getMaxCacheTime();
if (duration == null) {
return this.cache;
}

if ((System.currentTimeMillis() - this.cacheTime) > duration.toMillis()) {
this.cache = createHolderData(getSafeText());
this.cacheTime = System.currentTimeMillis();
}
Expand All @@ -40,8 +46,8 @@ protected String getSafeText() {
}
}

protected long getMaxCacheTime() {
return 1000;
protected Duration getMaxCacheTime() {
return Duration.ofSeconds(1);
}

protected abstract T createHolderData(String text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ru.boomearo.board.managers.BoardManager;
import ru.boomearo.board.objects.PlayerBoard;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -69,12 +70,12 @@ public boolean isVisibleToPlayer() {
}
}

public long getTimeToChangePage() {
public Duration getTimeToChangePage() {
try {
return getTimeToChange();
} catch (Exception e) {
this.playerBoard.getPlugin().getLogger().log(Level.SEVERE, "Failed to get time to change page for player " + this.playerBoard.getPlayer().getName(), e);
return 1;
return Duration.ofSeconds(1);
}
}

Expand Down Expand Up @@ -110,7 +111,7 @@ protected void onUpdate() {

}

protected abstract long getTimeToChange();
protected abstract Duration getTimeToChange();

protected abstract boolean isVisible();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.boomearo.board.objects.boards.defaults.pages;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -23,8 +24,8 @@ public DefaultPage(AbstractPageList pageList, String title, List<String> teams,
}

@Override
protected long getTimeToChange() {
return Integer.MAX_VALUE;
protected Duration getTimeToChange() {
return Duration.ofMillis(Long.MAX_VALUE);
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/ru/boomearo/board/utils/DataLength.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.boomearo.board.utils;

import lombok.Value;

@Value
public class DataLength {
int maxValueLength;
int maxTitleLength;

public DataLength(int maxValueLength, int maxTitleLength) {
this.maxValueLength = maxValueLength;
this.maxTitleLength = maxTitleLength;
}
}
15 changes: 3 additions & 12 deletions src/main/java/ru/boomearo/board/utils/StringLength.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package ru.boomearo.board.utils;

import lombok.Value;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import ru.boomearo.board.Board;

public final class StringLength {
@UtilityClass
public class StringLength {

private static DataLength MAX_DATA_LENGTH = new DataLength(16, 32);

Expand All @@ -27,14 +28,4 @@ public static DataLength getMaxDataLength() {
return MAX_DATA_LENGTH;
}

@Value
public static class DataLength {
int maxValueLength;
int maxTitleLength;

public DataLength(int maxValueLength, int maxTitleLength) {
this.maxValueLength = maxValueLength;
this.maxTitleLength = maxTitleLength;
}
}
}

0 comments on commit 279a42a

Please sign in to comment.