Skip to content

Commit

Permalink
Merge pull request #70 from CallVDois/add-remaining-time
Browse files Browse the repository at this point in the history
Add remaining time
  • Loading branch information
needkg committed Aug 28, 2024
2 parents d9c38f5 + 88c3fa3 commit 25eb2fc
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 51 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<groupId>org.callv2</groupId>
<artifactId>DayNightPvP</artifactId>
<name>DayNightPvP</name>
<version>1.2.6.7</version>
<version>1.2.7.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<java.version>17</java.version>
<shade.base>org.callv2.daynightpvp</shade.base>
<spigot.prefix>${project.name}</spigot.prefix>
<spigot.main>${project.groupId}.daynightpvp.DayNightPvP</spigot.main>
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/callv2/daynightpvp/commands/DnpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public DnpCommand(LangFile langFile, ConfigFile configFile, RunnableHandler runn

private void registerSubCommands() {
subCommands.put("reload", new ReloadSubCommand(langFile, new PluginServices(loseMoneyOnDeath, runnableHandler)));
subCommands.put("addworld", new AddWorldSubCommand(langFile, configFile));
subCommands.put("delworld", new DelWorldSubCommand(langFile, configFile));
subCommands.put("addworld", new AddWorldSubCommand(langFile, configFile, new PluginServices(loseMoneyOnDeath, runnableHandler)));
subCommands.put("delworld", new DelWorldSubCommand(langFile, configFile, new PluginServices(loseMoneyOnDeath, runnableHandler)));
}

@Override
Expand All @@ -48,10 +48,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
if (args.length == 0) {

PlayerUtils.sendMessage(sender, "");
PlayerUtils.sendMessage(sender, "§7§l* §a§lCommands§7:");
PlayerUtils.sendMessage(sender, "");
PlayerUtils.sendMessage(sender, "§7§l* §7/§9dnp §8-> §7Show all available commands.");
PlayerUtils.sendMessage(sender, "§7§l* §7/§9dnp reload §8-> §7Reload the plugin.");
PlayerUtils.sendMessage(sender,"§7§l* §a§lCommands§7:");
PlayerUtils.sendMessage(sender,"");
PlayerUtils.sendMessage(sender,"§7§l* §7/§9dnp §8-> §7Show all available commands.");
PlayerUtils.sendMessage(sender,"§7§l* §7/§9dnp reload §8-> §7Reload the plugin.");
PlayerUtils.sendMessage(sender,"§7§l* §7/§9dnp addworld §8-> §7Adds a world to the configuration file.");
PlayerUtils.sendMessage(sender,"§7§l* §7/§9dnp delworld §8-> §7Deletes a world from the configuration file.");
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.callv2.daynightpvp.commands.ISubCommand;
import org.callv2.daynightpvp.files.ConfigFile;
import org.callv2.daynightpvp.files.LangFile;
import org.callv2.daynightpvp.services.PluginServices;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -15,10 +16,12 @@ public class AddWorldSubCommand implements ISubCommand {

private final LangFile langFile;
private final ConfigFile configFile;
private final PluginServices pluginServices;

public AddWorldSubCommand(LangFile langFile, ConfigFile configFile) {
public AddWorldSubCommand(LangFile langFile, ConfigFile configFile, PluginServices pluginServices) {
this.langFile = langFile;
this.configFile = configFile;
this.pluginServices = pluginServices;
}

@Override
Expand All @@ -27,6 +30,7 @@ public void executeCommand(CommandSender sender, Command cmd, String commandLabe
if (Bukkit.getWorlds().contains(Bukkit.getWorld(args[1]))) {
if (!configFile.contains("worlds." + args[1])) {
addWorldToConfig(args[1]);
pluginServices.reloadPlugin();
sender.sendMessage(langFile.getFeedbackAddedWorld().replace("{0}", args[1]));
return;
}
Expand All @@ -44,7 +48,7 @@ public List<String> tabComplete(CommandSender sender, Command command, String al
List<String> suggestions = new ArrayList<>();

if (args.size() == 1) {
String prefix = args.get(0).toLowerCase(); // Obtenha o prefixo digitado
String prefix = args.get(0).toLowerCase();
for (World world : Bukkit.getWorlds()) {
if (World.Environment.NORMAL == world.getEnvironment()) {
String worldName = world.getName().toLowerCase();
Expand All @@ -63,6 +67,7 @@ private void addWorldToConfig(String worldName) {
configFile.setValue("worlds." + worldName + ".day-night-duration.night-duration", 600);
configFile.setValue("worlds." + worldName + ".automatic-pvp.enabled", true);
configFile.setValue("worlds." + worldName + ".automatic-pvp.day-end", 12000);
configFile.setValue("worlds." + worldName + ".boss-bar.time-remaining", false);
configFile.setValue("worlds." + worldName + ".automatic-difficulty.enabled", false);
configFile.setValue("worlds." + worldName + ".automatic-difficulty.day", "NORMAL");
configFile.setValue("worlds." + worldName + ".automatic-difficulty.night", "HARD");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.callv2.daynightpvp.commands.ISubCommand;
import org.callv2.daynightpvp.files.ConfigFile;
import org.callv2.daynightpvp.files.LangFile;
import org.callv2.daynightpvp.services.PluginServices;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -13,17 +14,20 @@ public class DelWorldSubCommand implements ISubCommand {

private final LangFile langFile;
private final ConfigFile configFile;
private final PluginServices pluginServices;

public DelWorldSubCommand(LangFile langFile, ConfigFile configFile) {
public DelWorldSubCommand(LangFile langFile, ConfigFile configFile, PluginServices pluginServices) {
this.langFile = langFile;
this.configFile = configFile;
this.pluginServices = pluginServices;
}

@Override
public void executeCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (args.length == 2) {
if (configFile.contains("worlds." + args[1])) {
removeWorldFromConfig(args[1]);
pluginServices.reloadPlugin();
sender.sendMessage(langFile.getFeedbackDeletedWorld().replace("{0}", args[1]));
return;
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/callv2/daynightpvp/files/ConfigFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void createFile() {
}

private void verifyFileVersion() {
int latestFileVersion = 18;
int latestFileVersion = 20;
if (latestFileVersion != getVersion()) {
File outdatedFile = new File(DayNightPvP.getInstance().getDataFolder(), "config.yml.old");
if (outdatedFile.exists()) {
Expand Down Expand Up @@ -244,11 +244,11 @@ public boolean getDayNightDurationEnabled(String worldName) {
}

public int getDayNightDurationDayDuration(String worldName) {
return getInt("worlds." + worldName + ".day-night-duration.day-duration", 600, 1, 2147483647);
return getInt("worlds." + worldName + ".day-night-duration.day-duration", 600, 1, 86400);
}

public int getDayNightDurationNightDuration(String worldName) {
return getInt("worlds." + worldName + ".day-night-duration.night-duration", 600, 1, 2147483647);
return getInt("worlds." + worldName + ".day-night-duration.night-duration", 600, 1, 86400);
}

public boolean getAutomaticPvpEnabled(String worldName) {
Expand All @@ -259,6 +259,10 @@ public int getAutomaticPvpDayEnd(String worldName) {
return getInt("worlds." + worldName + ".automatic-pvp.day-end", 12000, 1, 24000);
}

public boolean getTimeRemainingBossBarEnabled(String worldName) {
return getBoolean("worlds." + worldName + ".boss-bar.time-remaining", false);
}

public boolean getAutomaticDifficultyEnabled(String worldName) {
return getBoolean("worlds." + worldName + ".automatic-difficulty.enabled", false);
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/callv2/daynightpvp/files/LangFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void createFile() {
}

private void verifyFileVersion() {
int latestFileVersion = 15;
int latestFileVersion = 16;
if (latestFileVersion != getVersion()) {
File outdatedFile = new File(DayNightPvP.getInstance().getDataFolder(), "lang/" + configFile.getLanguage() + ".yml.old");
if (outdatedFile.exists()) {
Expand Down Expand Up @@ -166,6 +166,14 @@ public String getFeedbackNonExistentCommand() {
return formatMessage("feedback-non-existent-command");
}

public String getFeedbackBossbarSunset() {
return formatMessage("feedback-boss-bar-sunset");
}

public String getFeedbackBossbarSunrise() {
return formatMessage("feedback-boss-bar-sunrise");
}

public String getFeedbackError() {
return formatMessage("feedback-error");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,23 @@ public PvpStatusPlaceholder(LangFile langFile, ConfigFile configFile) {

@Override
public @NotNull String getVersion() {
return DayNightPvP.getInstance().getDescription().getVersion();
return "GENERIC";
}

@Override
public boolean canRegister() {
return true;
}

@Override
public boolean persist() {
return true; // This is required or else PlaceholderAPI will unregister the Expansion on reload
return true;
}

@Override
public String onPlaceholderRequest(Player player, String params) {
public String onPlaceholderRequest(Player player, @NotNull String params) {

if (params.equalsIgnoreCase("current_world_pvpstatus")) {
if (params.equalsIgnoreCase("pvp_status_current_world")) {
boolean pvpStatus;

World world = player.getWorld();
Expand All @@ -57,9 +62,9 @@ public String onPlaceholderRequest(Player player, String params) {
return pvpStatus ? langFile.getPlaceholderPvpEnabled() : langFile.getPlaceholderPvpDisabled();
}

if (params.startsWith("pvpstatus_")) {
if (params.startsWith("pvp_status_world")) {
boolean pvpStatus;
String worldName = params.substring("pvpstatus_".length());
String worldName = params.substring("pvp_status_world:".length());
World world = Bukkit.getWorld(worldName);
if (world != null) {
if (SearchUtils.worldExistsInWorldListSetString(configFile.getWorlds(), world.getName())) {
Expand All @@ -69,6 +74,7 @@ public String onPlaceholderRequest(Player player, String params) {
}
}
}

return langFile.getFeedbackError();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.callv2.daynightpvp.runnables;

import org.bukkit.Bukkit;
import org.bukkit.Difficulty;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable;
import org.callv2.daynightpvp.files.LangFile;
import org.callv2.daynightpvp.utils.ConsoleUtils;
import org.callv2.daynightpvp.utils.PlayerUtils;

import java.util.ArrayList;
import java.util.List;

public class AutomaticPvp extends BukkitRunnable {
public class AutomaticPvp implements Runnable {

public static List<World> worldsPvpOff = new ArrayList<>();
public static List<World> worldsPvpOn = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.callv2.daynightpvp.runnables;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable;

public class CustomTimeDuration extends BukkitRunnable {
public class CustomTimeDuration implements Runnable {

private final double dayTickIncrement;
private final double nightTickIncrement;
private final long dayTicks;
private final World world;
private double tickAccumulator;

public CustomTimeDuration(
long dayTicks,
Expand All @@ -28,11 +29,23 @@ public CustomTimeDuration(
@Override
public void run() {
long time = world.getTime();
double increment;

// Determina o incremento de tempo com base na hora do dia
if (time < dayTicks) {
world.setTime((long) (time + dayTickIncrement));
increment = dayTickIncrement;
} else {
world.setTime((long) (time + nightTickIncrement));
increment = nightTickIncrement;
}
}

// Adiciona o incremento ao acumulador
tickAccumulator += increment;

// Só avança o tempo quando o acumulador excede 1 tick
if (tickAccumulator >= 1.0) {
long ticksToAdvance = (long) tickAccumulator;
world.setTime(time + ticksToAdvance);
tickAccumulator -= ticksToAdvance; // Reduz o acumulador
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.callv2.daynightpvp.runnables;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BossBar;
import org.callv2.daynightpvp.files.LangFile;

public class RemainingTimeBossBar implements Runnable {

private final LangFile langFile;
private final BossBar bossBar;
private final World world;
private final boolean customDayNightDurationEnabled;
private final int dayDurationTicks;
private final int nightDurationTicks;
private final int dayEnd;

public RemainingTimeBossBar(LangFile langFile, BossBar bossbar, World world, boolean customDayNightDurationEnabled, int dayDurationSeconds, int nightDurationSeconds, int dayEnd) {
this.langFile = langFile;
this.bossBar = bossbar;
this.world = world;
this.customDayNightDurationEnabled = customDayNightDurationEnabled;
this.dayDurationTicks = defineDurationTicks(dayDurationSeconds);
this.nightDurationTicks = defineDurationTicks(nightDurationSeconds);
this.dayEnd = dayEnd;
}

@Override
public void run() {

updateBossBar();

Bukkit.getServer().getOnlinePlayers().forEach(bossBar::removePlayer);
world.getPlayers().forEach(bossBar::addPlayer);
}

private void updateBossBar() {
long time = world.getTime();

long remainingTicks;
double progress;
String title;

if (time >= 0 && time < dayEnd) {
remainingTicks = dayEnd - time;
progress = (double) time / dayEnd;
title = langFile.getFeedbackBossbarSunset().replace("{0}", formatTime(remainingTicks, dayDurationTicks));
bossBar.setColor(BarColor.YELLOW);
} else {
remainingTicks = 24000 - time;
progress = (double) (time - dayEnd) / (24000 - dayEnd);
title = langFile.getFeedbackBossbarSunrise().replace("{0}", formatTime(remainingTicks, nightDurationTicks));
bossBar.setColor(BarColor.PURPLE);
}

bossBar.setProgress(progress);
bossBar.setTitle(title);
}

private String formatTime(long remainingTicks, long totalTicks) {
// Converte ticks para segundos
long remainingSeconds = (long) (remainingTicks * (totalTicks / 12000.0) / 20);

// Calcula horas, minutos e segundos
long hours = remainingSeconds / 3600;
long minutes = (remainingSeconds % 3600) / 60;
long seconds = remainingSeconds % 60;

// Formata a string com horas, minutos e segundos
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}

private int defineDurationTicks(int seconds) {
if (customDayNightDurationEnabled) {
return seconds * 20;
} else {
return 12000;
}
}
}
Loading

0 comments on commit 25eb2fc

Please sign in to comment.