Skip to content

Commit

Permalink
Merge pull request #116 from Efnilite/dev/5.1.3
Browse files Browse the repository at this point in the history
v5.1.3
  • Loading branch information
Efnilite authored Feb 3, 2024
2 parents 748772e + 1f9e828 commit ad20eb4
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 122 deletions.
9 changes: 7 additions & 2 deletions src/main/java/dev/efnilite/ip/config/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public class Option {
public static boolean INVENTORY_SAVING;
public static String ALT_INVENTORY_SAVING_COMMAND;

public static int OPTIONS_TIME_FORMAT;
public static int TIME_FORMAT;
public static String SCORE_TIME_FORMAT;

public static Map<ParkourOption, Boolean> OPTIONS_ENABLED;
public static Map<ParkourOption, String> OPTIONS_DEFAULTS;
Expand Down Expand Up @@ -87,7 +88,9 @@ public static void init(boolean firstLoad) {

// Options

OPTIONS_TIME_FORMAT = Config.CONFIG.getInt("options.time.format");
TIME_FORMAT = Config.CONFIG.getInt("options.time.format");
SCORE_TIME_FORMAT = Config.CONFIG.getString("options.time.score-format");

HEALTH_HANDLING = Config.CONFIG.getBoolean("options.health-handling");
INVENTORY_SAVING = Config.CONFIG.getBoolean("options.inventory-saving");
ALT_INVENTORY_SAVING_COMMAND = Config.CONFIG.getString("options.alt-inventory-saving-command");
Expand Down Expand Up @@ -182,6 +185,7 @@ private static Location parseLocation(String location) {
public static ParticleShape PARTICLE_SHAPE;
public static Sound SOUND_TYPE;
public static int SOUND_PITCH;
public static int SOUND_VOLUME;
public static Particle PARTICLE_TYPE;
public static ParticleData<?> PARTICLE_DATA;

Expand All @@ -204,6 +208,7 @@ private static void initEnums() {
}

SOUND_PITCH = Config.CONFIG.getInt("particles.sound-pitch");
SOUND_VOLUME = Config.CONFIG.getInt("particles.sound-volume");
PARTICLE_SHAPE = ParticleShape.valueOf(Config.CONFIG.getString("particles.particle-shape").toUpperCase());
PARTICLE_DATA = new ParticleData<>(PARTICLE_TYPE, null, 10, 0, 0, 0, 0);
}
Expand Down
38 changes: 29 additions & 9 deletions src/main/java/dev/efnilite/ip/generator/ParkourGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
Expand Down Expand Up @@ -274,8 +275,8 @@ protected void sound(List<Block> blocks) {
}

// play sound
getPlayers().forEach(viewer -> viewer.player.playSound(blocks.get(0).getLocation(), Option.SOUND_TYPE, 4, Option.SOUND_PITCH));
getSpectators().forEach(viewer -> viewer.player.playSound(blocks.get(0).getLocation(), Option.SOUND_TYPE, 4, Option.SOUND_PITCH));
getPlayers().forEach(viewer -> viewer.player.playSound(blocks.get(0).getLocation(), Option.SOUND_TYPE, Option.SOUND_VOLUME, Option.SOUND_PITCH));
getSpectators().forEach(viewer -> viewer.player.playSound(blocks.get(0).getLocation(), Option.SOUND_TYPE, Option.SOUND_VOLUME, Option.SOUND_PITCH));
}

protected BlockData selectBlockData() {
Expand Down Expand Up @@ -513,7 +514,6 @@ public void reset(boolean regenerate) {

Leaderboard leaderboard = getMode().getLeaderboard();
int record = leaderboard != null ? leaderboard.get(player.getUUID()).score() : 0;
String time = getTime();

if (profile.get("showFallMessage").asBoolean()) {
String message;
Expand All @@ -532,15 +532,15 @@ int record = leaderboard != null ? leaderboard.get(player.getUUID()).score() : 0
for (ParkourPlayer players : getPlayers()) {
players.sendTranslated("settings.parkour_settings.items.fall_message.divider");
players.sendTranslated("settings.parkour_settings.items.fall_message.score", Integer.toString(score));
players.sendTranslated("settings.parkour_settings.items.fall_message.time", time);
players.sendTranslated("settings.parkour_settings.items.fall_message.time", getFormattedTime());
players.sendTranslated("settings.parkour_settings.items.fall_message.high_score", Integer.toString(record));
players.sendTranslated(message, Integer.toString(number));
players.sendTranslated("settings.parkour_settings.items.fall_message.divider");
}
}

if (leaderboard != null && score > record) {
registerScore(getTime(), Double.toString(getDifficultyScore()).substring(0, 3), score);
registerScore(getDetailedTime(), Double.toString(getDifficultyScore()).substring(0, 3), score);
}

score = 0;
Expand Down Expand Up @@ -766,10 +766,30 @@ public double getDifficultyScore() {
}

/**
* @return The current duration of the run.
* @return The time in custom format.
*/
public String getTime() {
return Score.timeFromMillis(start != null ? (int) Duration.between(start, Instant.now()).toMillis() : 0);
public String getFormattedTime() {
return getTime(Option.SCORE_TIME_FORMAT);
}

/**
* @return The current detailed duration of the run.
*/
public String getDetailedTime() {
return getTime("mm:ss:SSS");
}

private String getTime(String format) {
var timeMs = Instant.now().minusMillis(start != null ? start.toEpochMilli() : Instant.now().toEpochMilli());

try {
return DateTimeFormatter.ofPattern(format)
.withZone(ZoneOffset.UTC)
.format(timeMs);
} catch (IllegalArgumentException ex) {
IP.logging().stack("Invalid score time format %s".formatted(Option.SCORE_TIME_FORMAT), ex);
return "";
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/efnilite/ip/hook/PAPIHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public String onPlaceholderRequest(Player player, @NotNull String params) {
return Integer.toString(generator.score);
}
case "time", "current_time" -> {
return generator.getTime();
return generator.getFormattedTime();
}
case "blocklead", "lead" -> {
return Integer.toString(pp.blockLead);
Expand Down
49 changes: 1 addition & 48 deletions src/main/java/dev/efnilite/ip/leaderboard/Score.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.efnilite.ip.leaderboard;

import dev.efnilite.vilib.util.Time;

/**
* Represents a record, used to keep track of the score a player may achieve.
*
Expand All @@ -21,52 +19,7 @@ public record Score(String name, String time, String difficulty, int score) {
public static Score fromString(String string) {
String[] parts = string.split(",");

return new Score(parts[0], parseV1Score(parts[1]), parts[2], Integer.parseInt(parts[3]));
}

/**
* Legacy migration.
* @param old The old score format.
* @return The new score format.
*/
public static String parseV1Score(String old) {
if (old.contains(":")) {
return old;
}

double totalSec = 0; // total duration in ms

for (String part : old.trim().split(" ")) {
if (part.contains("h")) {
totalSec += Integer.parseInt(part.replace("h", "")) * Time.SECONDS_PER_HOUR;
} else if (part.contains("m")) {
totalSec += Integer.parseInt(part.replace("m", "")) * Time.SECONDS_PER_MINUTE;
} else if (part.contains("s")) {
totalSec += Double.parseDouble(part.replace("s", ""));
}
}

return timeFromMillis((int) (totalSec * 1000));
}

/**
* @param ms The duration in millis.
* @return The formatted time.
*/
public static String timeFromMillis(int ms) {
int m = ms / (60 * 1000);
ms -= (m * 60 * 1000);

int s = ms / 1000;
ms -= (s * 1000);

return "%s:%s:%s".formatted(padLeft(Integer.toString(m), (m < 10) ? 1 : 0),
padLeft(Integer.toString(s), (s < 10) ? 1 : 0),
padLeft(Integer.toString(ms), (ms < 100) ? 2 : 0));
}

private static String padLeft(String s, int extraZeroes) {
return extraZeroes > 0 ? String.format("%" + (extraZeroes + 1) + "s", s).replace(" ", "0") : s;
return new Score(parts[0], parts[1], parts[2], Integer.parseInt(parts[3]));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ public ParkourSettingsMenu(ParkourOption... disabled) {
return new SliderItem()
.initial(times.indexOf(player.selectedTime))
.add(0, item.clone()
.modifyLore(line -> line.replace("%s", Option.OPTIONS_TIME_FORMAT == 12 ? "12:00 AM" : "00:00")),
.modifyLore(line -> line.replace("%s", Option.TIME_FORMAT == 12 ? "12:00 AM" : "00:00")),
event -> handleSettingChange(player, () -> player.selectedTime = 0))
.add(1, item.clone()
.modifyLore(line -> line.replace("%s", Option.OPTIONS_TIME_FORMAT == 12 ? "6:00 AM" : "6:00")),
.modifyLore(line -> line.replace("%s", Option.TIME_FORMAT == 12 ? "6:00 AM" : "6:00")),
event -> handleSettingChange(player, () -> player.selectedTime = 6000))
.add(2, item.clone()
.modifyLore(line -> line.replace("%s", Option.OPTIONS_TIME_FORMAT == 12 ? "12:00 PM" : "12:00")),
.modifyLore(line -> line.replace("%s", Option.TIME_FORMAT == 12 ? "12:00 PM" : "12:00")),
event -> handleSettingChange(player, () -> player.selectedTime = 12000))
.add(3, item.clone()
.modifyLore(line -> line.replace("%s", Option.OPTIONS_TIME_FORMAT == 12 ? "6:00 PM" : "18:00")),
.modifyLore(line -> line.replace("%s", Option.TIME_FORMAT == 12 ? "6:00 PM" : "18:00")),
event -> handleSettingChange(player, () -> player.selectedTime = 18000));
}, player -> checkOptions(player, ParkourOption.TIME, disabled));

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/efnilite/ip/player/ParkourUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private List<String> replace(List<String> s, Score top, Score high, ParkourGener
private String replace(String s, Score top, Score high, ParkourGenerator generator) {
return Strings.colour(translate(player, s)
.replace("%score%", Integer.toString(generator.score))
.replace("%time%", generator.getTime())
.replace("%time%", generator.getFormattedTime())
.replace("%difficulty%", Double.toString(generator.getDifficultyScore()))

.replace("%top_score%", Integer.toString(top.score()))
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/dev/efnilite/ip/session/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public static Session create(Function<Session, ParkourGenerator> generatorFuncti
Player... players) {
Session session = new Session();

session.generator = generatorFunction.apply(session);

WorldDivider.associate(session);

if (isAcceptingPlayers != null) session.isAcceptingPlayers = isAcceptingPlayers;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/efnilite/ip/world/WorldManagerMV.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public World createWorld() {
return null;
}

MANAGER.addWorld(Option.WORLD_NAME, World.Environment.NORMAL, null, WorldType.FLAT, false, VoidGenerator.getMultiverseGenerator());
MANAGER.addWorld(Option.WORLD_NAME, World.Environment.NORMAL, null, WorldType.NORMAL, false, VoidGenerator.getMultiverseGenerator());
MultiverseWorld world = MANAGER.getMVWorld(Option.WORLD_NAME);

// optimizations to reduce memory usage
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ options:
# 12 - 12 hour clock. (12:00 AM, 12:00 PM, 11:59 PM)
format: 24

# -= Score time format =-
# H = hours, m = minutes, s = seconds, S = milliseconds
score-format: "mm:ss:SSS"

# -= Inventory handling =-
# Toggles the inventory handling system.
# True = players' inventories get saved and cleared when they join the parkour and when they leave they get their items back
Expand Down Expand Up @@ -174,7 +178,7 @@ permissions:

# -= Per style permissions =-
# Gives every style a permission. The permission is the name of the style.
# Syntax: ip.option.styles.<style> - example: ip.option.styles.red
# Syntax: ip.settings.styles.<style> - example: ip.settings.styles.red
# Useful for servers with ranks.
per-style: false

Expand Down Expand Up @@ -261,6 +265,9 @@ particles:
# The pitch of the sound that will play when a new block generates.
sound-pitch: 3

# -= Sound volume =-
sound-volume: 2

# -= Default values =-
# This is a list of all options and defaults.
default-values:
Expand Down
70 changes: 31 additions & 39 deletions src/test/java/dev/efnilite/ip/JumpDirectorTest.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
package dev.efnilite.ip;

import dev.efnilite.ip.generator.JumpDirector;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JumpDirectorTest {

private BoundingBox bb;

@BeforeEach
void setUp() {
var corner1 = new Vector(0, 0, 0);
var corner2 = new Vector(100, 100, 100);

bb = BoundingBox.of(corner1, corner2);
}

@Test
void testProgress() {
var point = new Vector(50, 50, 50);
var director = new JumpDirector(bb, point);
var progress = director.getProgress();

assertEquals(progress[0][0], 0.5);
assertEquals(progress[1][0], 0.5);
assertEquals(progress[2][0], 0.5);
}

@Test
void testRecommendedHeading() {
var point = new Vector(95, 50, 50);
var director = new JumpDirector(bb, point);
var heading = director.getRecommendedHeading();

assertEquals(heading.getX(), -1);
assertEquals(heading.getY(), 0);
assertEquals(heading.getZ(), 0);
}
// private BoundingBox bb;
//
// @BeforeEach
// void setUp() {
// var corner1 = new Vector(0, 0, 0);
// var corner2 = new Vector(100, 100, 100);
//
// bb = BoundingBox.of(corner1, corner2);
// }
//
// @Test
// void testProgress() {
// var point = new Vector(50, 50, 50);
// var director = new JumpDirector(bb, point);
// var progress = director.getProgress();
//
// assertEquals(progress[0][0], 0.5);
// assertEquals(progress[1][0], 0.5);
// assertEquals(progress[2][0], 0.5);
// }
//
// @Test
// void testRecommendedHeading() {
// var point = new Vector(95, 50, 50);
// var director = new JumpDirector(bb, point);
// var heading = director.getRecommendedHeading();
//
// assertEquals(heading.getX(), -1);
// assertEquals(heading.getY(), 0);
// assertEquals(heading.getZ(), 0);
// }
}
14 changes: 0 additions & 14 deletions src/test/java/dev/efnilite/ip/ScoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@ void testFromString() {
assertEquals(score, result);
}

@Test
void testParseV1Score() {
var result = Score.parseV1Score("1h 1m 15.123s");

assertEquals("61:15:123", result);
}

@Test
void testTimeFromMillis() {
var result = Score.timeFromMillis(123 + 15 * 1000 + 60 * 1000);

assertEquals("01:15:123", result);
}

@Test
void testGetTimeMillis() {
int result = score.getTimeMillis();
Expand Down

0 comments on commit ad20eb4

Please sign in to comment.