Repackage utils

This commit is contained in:
Daniel Mills
2021-07-16 02:11:37 -04:00
parent b9b30f9f53
commit da53a7d469
471 changed files with 1043 additions and 601 deletions

View File

@@ -0,0 +1,144 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util;
import lombok.NonNull;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* @author Missionary (missionarymc@gmail.com)
* @since 3/23/2018
*/
public class Board {
private static final String[] CACHED_ENTRIES = new String[C.values().length];
private static final Function<String, String> APPLY_COLOR_TRANSLATION = s -> C.translateAlternateColorCodes('&', s);
static {
IntStream.range(0, 15).forEach(i -> CACHED_ENTRIES[i] = C.values()[i].toString() + C.RESET);
}
private final Player player;
private final Objective objective;
private final Team team;
@Setter
private BoardSettings boardSettings;
private boolean ready;
@SuppressWarnings("deprecation")
public Board(@NonNull final Player player, final BoardSettings boardSettings) {
this.player = player;
this.boardSettings = boardSettings;
this.objective = this.getScoreboard().getObjective("board") == null ? this.getScoreboard().registerNewObjective("board", "dummy") : this.getScoreboard().getObjective("board");
this.objective.setDisplaySlot(DisplaySlot.SIDEBAR);
this.team = this.getScoreboard().getTeam("board") == null ? this.getScoreboard().registerNewTeam("board") : this.getScoreboard().getTeam("board");
this.team.setAllowFriendlyFire(true);
this.team.setCanSeeFriendlyInvisibles(false);
this.team.setPrefix("");
this.team.setSuffix("");
this.ready = true;
}
public Scoreboard getScoreboard() {
return (player != null) ? player.getScoreboard() : null;
}
public void remove() {
this.resetScoreboard();
}
public void update() {
// Checking if we are ready to start updating the Scoreboard.
if (!ready) {
return;
}
// Making sure the player is connected.
if (!player.isOnline()) {
remove();
return;
}
// Making sure the Scoreboard Provider is set.
if (boardSettings == null) {
return;
}
// Getting their Scoreboard display from the Scoreboard Provider.
final List<String> entries = boardSettings.getBoardProvider().getLines(player).stream().map(APPLY_COLOR_TRANSLATION).collect(Collectors.toList());
if (boardSettings.getScoreDirection() == ScoreDirection.UP) {
Collections.reverse(entries);
}
// Setting the Scoreboard title
String title = boardSettings.getBoardProvider().getTitle(player);
if (title.length() > 32) {
Bukkit.getLogger().warning("The title " + title + " is over 32 characters in length, substringing to prevent errors.");
title = title.substring(0, 32);
}
objective.setDisplayName(C.translateAlternateColorCodes('&', title));
// Clearing previous Scoreboard values if entry sizes don't match.
if (this.getScoreboard().getEntries().size() != entries.size())
this.getScoreboard().getEntries().forEach(this::removeEntry);
// Setting Scoreboard lines.
for (int i = 0; i < entries.size(); i++) {
String str = entries.get(i);
BoardEntry entry = BoardEntry.translateToEntry(str);
Team team = getScoreboard().getTeam(CACHED_ENTRIES[i]);
if (team == null) {
team = this.getScoreboard().registerNewTeam(CACHED_ENTRIES[i]);
team.addEntry(team.getName());
}
team.setPrefix(entry.getPrefix());
team.setSuffix(entry.getSuffix());
switch (boardSettings.getScoreDirection()) {
case UP -> objective.getScore(team.getName()).setScore(1 + i);
case DOWN -> objective.getScore(team.getName()).setScore(15 - i);
}
}
}
public void removeEntry(String id) {
this.getScoreboard().resetScores(id);
}
public void resetScoreboard() {
ready = false;
player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
}
}

View File

@@ -0,0 +1,58 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util;
import lombok.Getter;
import org.apache.commons.lang.StringUtils;
/**
* @author Missionary (missionarymc@gmail.com)
* @since 3/29/2018
*/
@SuppressWarnings("ClassCanBeRecord")
public class BoardEntry {
@Getter
private final String prefix, suffix;
private BoardEntry(final String prefix, final String suffix) {
this.prefix = prefix;
this.suffix = suffix;
}
public static BoardEntry translateToEntry(String input) {
if (input.isEmpty()) {
return new BoardEntry("", "");
}
if (input.length() <= 16) {
return new BoardEntry(input, "");
} else {
String prefix = input.substring(0, 16);
String suffix = "";
if (prefix.endsWith("\u00a7")) {
prefix = prefix.substring(0, prefix.length() - 1);
suffix = "\u00a7" + suffix;
}
suffix = StringUtils.left(C.getLastColors(prefix) + suffix + input.substring(16), 16);
return new BoardEntry(prefix, suffix);
}
}
}

View File

@@ -0,0 +1,96 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class BoardManager {
private final JavaPlugin plugin;
private BoardSettings boardSettings;
private final Map<UUID, Board> scoreboards;
private final BukkitTask updateTask;
public BoardManager(JavaPlugin plugin, BoardSettings boardSettings) {
this.plugin = plugin;
this.boardSettings = boardSettings;
this.scoreboards = new ConcurrentHashMap<>();
this.updateTask = new BoardUpdateTask(this).runTaskTimer(plugin, 2L, 2L);
plugin.getServer().getOnlinePlayers().forEach(this::setup);
}
public void setBoardSettings(BoardSettings boardSettings) {
this.boardSettings = boardSettings;
scoreboards.values().forEach(board -> board.setBoardSettings(boardSettings));
}
public boolean hasBoard(Player player) {
return scoreboards.containsKey(player.getUniqueId());
}
public Optional<Board> getBoard(Player player) {
return Optional.ofNullable(scoreboards.get(player.getUniqueId()));
}
public void setup(Player player) {
Optional.ofNullable(scoreboards.remove(player.getUniqueId())).ifPresent(Board::resetScoreboard);
if (player.getScoreboard().equals(Bukkit.getScoreboardManager().getMainScoreboard())) {
player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());
}
scoreboards.put(player.getUniqueId(), new Board(player, boardSettings));
}
public void remove(Player player) {
Optional.ofNullable(scoreboards.remove(player.getUniqueId())).ifPresent(Board::remove);
}
public Map<UUID, Board> getScoreboards() {
return Collections.unmodifiableMap(scoreboards);
}
public void onDisable() {
updateTask.cancel();
plugin.getServer().getOnlinePlayers().forEach(this::remove);
scoreboards.clear();
}
}

View File

@@ -0,0 +1,32 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util;
import org.bukkit.entity.Player;
import java.util.List;
public interface BoardProvider {
String getTitle(Player player);
List<String> getLines(Player player);
}

View File

@@ -0,0 +1,34 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util;
import lombok.Builder;
import lombok.Getter;
@SuppressWarnings("ClassCanBeRecord")
@Getter
@Builder
public class BoardSettings {
private final BoardProvider boardProvider;
private final ScoreDirection scoreDirection;
}

View File

@@ -0,0 +1,43 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util;
import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.UUID;
import java.util.function.Predicate;
/**
* @author Missionary (missionarymc@gmail.com)
* @since 5/31/2018
*/
@RequiredArgsConstructor
public class BoardUpdateTask extends BukkitRunnable {
private static final Predicate<UUID> PLAYER_IS_ONLINE = uuid -> Bukkit.getPlayer(uuid) != null;
private final BoardManager boardManager;
@Override
public void run() {
boardManager.getScoreboards().entrySet().stream().filter(entrySet -> PLAYER_IS_ONLINE.test(entrySet.getKey())).forEach(entrySet -> entrySet.getValue().update());
}
}

View File

@@ -0,0 +1,32 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util;
/**
* @author Missionary (missionarymc@gmail.com)
* @since 5/31/2018
*/
public enum ScoreDirection {
UP,
DOWN
}