Skip to content

Commit

Permalink
[Change] 同期処理はプレホル適用のみにし、更新処理も人により分散する
Browse files Browse the repository at this point in the history
  • Loading branch information
Siroshun09 committed Aug 14, 2020
1 parent 8e3e4d7 commit d67d305
Show file tree
Hide file tree
Showing 16 changed files with 324 additions and 155 deletions.
18 changes: 4 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<repository>
<id>bintray-siroplugins-maven-repo</id>
<url>https://dl.bintray.com/siroplugins/maven-repo</url>
</repository>
<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/nexus/repository/public/</url>
</repository>
<repository>
<id>bintray-siroplugins-maven-repo</id>
<url>https://dl.bintray.com/siroplugins/maven-repo</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -62,12 +62,6 @@
<version>1.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.siroshun09.textlibs</groupId>
<artifactId>textlibs</artifactId>
<version>2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
Expand Down Expand Up @@ -133,10 +127,6 @@
<pattern>com.github.siroshun09</pattern>
<shadedPattern>${project.groupId}.lib</shadedPattern>
</relocation>
<relocation>
<pattern>me.lucko</pattern>
<shadedPattern>${project.groupId}.lib</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
Expand Down
40 changes: 9 additions & 31 deletions src/main/java/net/okocraft/scoreboard/BoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,12 @@ public class BoardManager {
private final ScoreboardPlugin plugin;
private final Board defBoard;
private final Set<DisplayedBoard> displayedBoards;
private final long interval;

BoardManager(@NotNull ScoreboardPlugin plugin) {
this.plugin = plugin;
this.displayedBoards = new HashSet<>();

defBoard = loadBoard(new BukkitConfig(plugin, "default.yml", true));

long minInterval = defBoard.getLines().stream()
.map(Line::getInterval)
.filter(i -> 0 < i).sorted()
.findFirst()
.orElse(1L);

if (defBoard.getTitle().shouldUpdate()) {
interval = Math.min(minInterval, defBoard.getTitle().getInterval());
} else {
interval = minInterval;
}

if (plugin.isUsingProtocolLib()) {
plugin.getLogger().info("We are using ProtocolLib.");
}

plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, this::update, interval, interval);
}

public void showAllDefault() {
Expand All @@ -63,14 +44,18 @@ public void showDefault(@NotNull Player player) {
display = new BukkitDisplayedBoard(plugin, defBoard, player);
}

display.scheduleUpdateTasks();
displayedBoards.add(display);
}

public void removeBoard(@NotNull Player player) {
displayedBoards.stream()
.filter(b -> b.getPlayer().equals(player))
.collect(Collectors.toSet())
.forEach(displayedBoards::remove);
Set<DisplayedBoard> playerBoards =
displayedBoards.stream().filter(b -> b.getPlayer().equals(player)).collect(Collectors.toSet());

for (DisplayedBoard board : playerBoards) {
board.cancelUpdateTasks();
displayedBoards.remove(board);
}

player.setScoreboard(plugin.getScoreboardManager().getMainScoreboard());
}
Expand All @@ -86,17 +71,10 @@ public void removeAll() {
displayedBoards.clear();
}

public void update() {
if (!plugin.getServer().getOnlinePlayers().isEmpty()) {
defBoard.update(interval);
displayedBoards.forEach(DisplayedBoard::update);
}
}

@NotNull
private Board loadBoard(@NotNull BukkitYaml yaml) {
if (!yaml.load()) {
throw new IllegalStateException("Could not load default board");
throw new IllegalStateException("Could not load " + yaml.getPath().getFileName().toString());
}

List<String> titleList = yaml.getStringList("title.list");
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/okocraft/scoreboard/ScoreboardPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,37 @@

import net.okocraft.scoreboard.listener.PlayerListener;
import net.okocraft.scoreboard.papi.PlaceholderAPIHooker;
import net.okocraft.scoreboard.task.AbstractUpdateTask;
import net.okocraft.scoreboard.util.LengthChecker;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.ScoreboardManager;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class ScoreboardPlugin extends JavaPlugin {

private ScoreboardManager scoreboardManager;
private BoardManager boardManager;
private PlayerListener listener;
private ExecutorService executor;
private ScheduledExecutorService scheduler;

@Override
public void onEnable() {
saveDefaultConfig();
reloadConfig();

executor = Executors.newCachedThreadPool();
scheduler = Executors.newSingleThreadScheduledExecutor();

if (isUsingProtocolLib()) {
getLogger().info("We are using ProtocolLib.");
}

try {
boardManager = new BoardManager(this);
Expand Down Expand Up @@ -54,6 +64,10 @@ public void onDisable() {
if (executor != null) {
executor.shutdownNow();
}

if (scheduler != null) {
scheduler.shutdownNow();
}
}

@NotNull
Expand Down Expand Up @@ -91,6 +105,12 @@ public ExecutorService getExecutor() {
return executor;
}

@NotNull
public ScheduledFuture<?> scheduleUpdateTask(@NotNull AbstractUpdateTask task, long tick) {
long interval = tick * 50;
return scheduler.scheduleWithFixedDelay(() -> executor.submit(task), interval, interval, TimeUnit.MILLISECONDS);
}

boolean isUsingProtocolLib() {
return getConfig().getBoolean("use-ProtocolLib", true) &&
getServer().getPluginManager().getPlugin("ProtocolLib") != null;
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/net/okocraft/scoreboard/board/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ public Board(@NotNull Line title, @NotNull List<Line> lines) {
this.lines = lines;
}

public void update(long increment) {
if (title.shouldUpdate()) {
title.update(increment);
}

lines.stream().filter(Line::shouldUpdate).forEach(l -> l.update(increment));
}

@NotNull
public Line getTitle() {
return title;
Expand Down
43 changes: 13 additions & 30 deletions src/main/java/net/okocraft/scoreboard/board/Line.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.okocraft.scoreboard.board;

import com.github.siroshun09.textlibs.util.Colorizer;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
Expand All @@ -13,46 +12,30 @@ public class Line {
private final long interval;
private final boolean shouldUpdate;

private String currentLine = "";
private int currentIndex = 0;
private long count = 0;

public Line(@NotNull List<String> lines, long interval) {
this.lines = List.copyOf(lines);
this.shouldUpdate = 0 < interval;
this.shouldUpdate = !lines.isEmpty() && 0 < interval;
this.interval = interval;

if (!lines.isEmpty()) {
currentLine = Colorizer.colorize(lines.get(0));
}
}

public boolean shouldUpdate() {
return shouldUpdate;
public boolean isEmpty() {
return lines.isEmpty();
}

public long getInterval() {
return interval;
public int getMaxIndex() {
return lines.size() - 1;
}

public void update(long increment) {
if (interval <= count) {
count = 0;

currentIndex++;

if (lines.size() <= currentIndex) {
currentIndex = 0;
}
@NotNull
public String get(int index) {
return lines.get(index);
}

currentLine = Colorizer.colorize(lines.get(currentIndex));
} else {
count += increment;
}
public boolean shouldUpdate() {
return shouldUpdate;
}

@NotNull
public String getCurrentLine() {
return currentLine;
public long getInterval() {
return interval;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,53 @@

import net.okocraft.scoreboard.ScoreboardPlugin;
import net.okocraft.scoreboard.display.line.DisplayedLine;
import net.okocraft.scoreboard.task.LineUpdateTask;
import net.okocraft.scoreboard.task.TitleUpdateTask;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;

public abstract class AbstractDisplayedBoard implements DisplayedBoard {

protected final ScoreboardPlugin plugin;
protected final Player player;

private final Set<ScheduledFuture<?>> updateTasks;

public AbstractDisplayedBoard(@NotNull ScoreboardPlugin plugin, @NotNull Player player) {
this.plugin = plugin;
this.player = player;

updateTasks = new HashSet<>();
}

@Override
@NotNull
public Player getPlayer() {
return player;
}

public void update() {
getTitle().update();
getLines().forEach(DisplayedLine::update);
@Override
public void scheduleUpdateTasks() {
if (getTitle().shouldUpdate()) {
updateTasks.add(plugin.scheduleUpdateTask(new TitleUpdateTask(plugin, this), getTitle().getInterval()));
}

plugin.getExecutor().submit(this::apply);
for (DisplayedLine line : getLines()) {
if (line.shouldUpdate()) {
updateTasks.add(plugin.scheduleUpdateTask(new LineUpdateTask(plugin, this, line), line.getInterval()));
}
}
}

protected abstract void apply();

@NotNull
protected abstract DisplayedLine getTitle();

@NotNull
protected abstract List<DisplayedLine> getLines();
@Override
public void cancelUpdateTasks() {
updateTasks.stream().filter(t -> !t.isCancelled()).forEach(t -> t.cancel(true));
}

@Override
public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,16 @@ public BukkitDisplayedBoard(@NotNull ScoreboardPlugin plugin, @NotNull Board boa
}

@Override
protected void apply() {
public void applyTitle() {
if (title.isChanged()) {
objective.setDisplayName(title.getCurrentLine());
}
}

for (int i = 0, l = lines.size(); i < l; i++) {
DisplayedLine line = lines.get(i);

if (!line.isChanged()) {
continue;
}

Team team = scoreboard.getTeam(String.valueOf(i));
@Override
public void applyLine(@NotNull DisplayedLine line) {
if (line.isChanged()) {
Team team = scoreboard.getTeam(line.getTeamName());

if (team != null) {
team.setPrefix(line.getCurrentLine());
Expand All @@ -77,13 +74,13 @@ protected void apply() {

@Override
@NotNull
protected DisplayedLine getTitle() {
public DisplayedLine getTitle() {
return title;
}

@Override
@NotNull
protected List<DisplayedLine> getLines() {
public List<DisplayedLine> getLines() {
return lines;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
package net.okocraft.scoreboard.display.board;

import net.okocraft.scoreboard.display.line.DisplayedLine;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public interface DisplayedBoard {

@NotNull
Player getPlayer();

void update();
@NotNull
DisplayedLine getTitle();

@NotNull
List<DisplayedLine> getLines();

void applyTitle();

void applyLine(@NotNull DisplayedLine line);

void scheduleUpdateTasks();

void cancelUpdateTasks();
}
Loading

0 comments on commit d67d305

Please sign in to comment.