9
0
mirror of https://gitlab.com/SamB440/rpgregions-2.git synced 2025-12-19 14:59:19 +00:00

fix: %rpgregions_region_timed% showing incorrect percentage, fix: time incrementing standing still

This commit is contained in:
SamB440
2022-06-06 17:18:27 +01:00
parent 2be1fe3cb8
commit e9dfa07a73
4 changed files with 44 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
package net.islandearth.rpgregions.managers.data.account; package net.islandearth.rpgregions.managers.data.account;
import net.islandearth.rpgregions.managers.data.region.Discovery; import net.islandearth.rpgregions.managers.data.region.Discovery;
import net.islandearth.rpgregions.utils.TimeEntry;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@@ -14,7 +15,7 @@ public class RPGRegionsAccount {
private final UUID uuid; private final UUID uuid;
private final Map<String, Discovery> discoveredRegions; private final Map<String, Discovery> discoveredRegions;
private final List<AccountCooldown> cooldowns; private final List<AccountCooldown> cooldowns;
private final Map<String, Long> secondsInRegion; private final Map<String, TimeEntry> secondsInRegion;
public RPGRegionsAccount(UUID uuid, Map<String, Discovery> discoveredRegions) { public RPGRegionsAccount(UUID uuid, Map<String, Discovery> discoveredRegions) {
this.uuid = uuid; this.uuid = uuid;
@@ -39,12 +40,12 @@ public class RPGRegionsAccount {
return cooldowns; return cooldowns;
} }
public Optional<Long> getStartTimeInRegion(String region) { public Optional<TimeEntry> getTimeEntryInRegion(String region) {
return Optional.ofNullable(secondsInRegion.getOrDefault(region, null)); return Optional.ofNullable(secondsInRegion.getOrDefault(region, null));
} }
public void addStartTimeInRegion(String region, long time) { public void addTimeEntryInRegion(String region, long time) {
secondsInRegion.put(region, time); secondsInRegion.put(region, new TimeEntry(time));
} }
public void removeStartTimeInRegion(String region) { public void removeStartTimeInRegion(String region) {

View File

@@ -0,0 +1,27 @@
package net.islandearth.rpgregions.utils;
public class TimeEntry {
private long start, latestEntry;
public TimeEntry(long start) {
this.start = start;
this.latestEntry = start;
}
public long getStart() {
return start;
}
public long getLatestEntry() {
return latestEntry;
}
public void setStart(long start) {
this.start = start;
}
public void setLatestEntry(long latestEntry) {
this.latestEntry = latestEntry;
}
}

View File

@@ -6,6 +6,7 @@ import net.islandearth.rpgregions.managers.data.account.RPGRegionsAccount;
import net.islandearth.rpgregions.managers.data.region.ConfiguredRegion; import net.islandearth.rpgregions.managers.data.region.ConfiguredRegion;
import net.islandearth.rpgregions.thread.Blocking; import net.islandearth.rpgregions.thread.Blocking;
import net.islandearth.rpgregions.translation.Translations; import net.islandearth.rpgregions.translation.Translations;
import net.islandearth.rpgregions.utils.TimeEntry;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Optional; import java.util.Optional;
@@ -103,11 +104,11 @@ public class PlaceholderRegionHook extends PlaceholderExpansion implements Block
return region.get().getCustomName(); return region.get().getCustomName();
} }
final Optional<Long> startTimeInRegion = account.getStartTimeInRegion(region.get().getId()); final Optional<TimeEntry> timeEntry = account.getTimeEntryInRegion(region.get().getId());
if (startTimeInRegion.isEmpty()) return region.get().getCustomName(); if (timeEntry.isEmpty()) return region.get().getCustomName();
final double entry = startTimeInRegion.get(); final double entry = timeEntry.get().getStart();
final double time = TimeUnit.MILLISECONDS.toSeconds((long) (System.currentTimeMillis() - entry)); final double time = TimeUnit.MILLISECONDS.toSeconds((long) (timeEntry.get().getLatestEntry() - entry));
final double secondsInsideToDiscover = configuredRegion.getSecondsInsideToDiscover(); final double secondsInsideToDiscover = configuredRegion.getSecondsInsideToDiscover();
final double percent = (time / secondsInsideToDiscover) * 100; final double percent = (time / secondsInsideToDiscover) * 100;
return Translations.DISCOVERING_AREA_PLACEHOLDER.get(player, (int) percent, region.get().getId()).get(0); return Translations.DISCOVERING_AREA_PLACEHOLDER.get(player, (int) percent, region.get().getId()).get(0);

View File

@@ -9,6 +9,7 @@ import net.islandearth.rpgregions.managers.data.region.Discovery;
import net.islandearth.rpgregions.managers.data.region.WorldDiscovery; import net.islandearth.rpgregions.managers.data.region.WorldDiscovery;
import net.islandearth.rpgregions.translation.Translations; import net.islandearth.rpgregions.translation.Translations;
import net.islandearth.rpgregions.utils.RegenUtils; import net.islandearth.rpgregions.utils.RegenUtils;
import net.islandearth.rpgregions.utils.TimeEntry;
import net.islandearth.rpgregions.utils.TitleAnimator; import net.islandearth.rpgregions.utils.TitleAnimator;
import net.islandearth.rpgregions.utils.XSound; import net.islandearth.rpgregions.utils.XSound;
import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ChatMessageType;
@@ -72,12 +73,14 @@ public class RegionListener implements Listener {
if (!has && configuredRegion.isDiscoverable() && prioritised) { if (!has && configuredRegion.isDiscoverable() && prioritised) {
if (configuredRegion.isTimedRegion()) { if (configuredRegion.isTimedRegion()) {
final long currentTimeMillis = System.currentTimeMillis(); final long currentTimeMillis = System.currentTimeMillis();
if (account.getStartTimeInRegion(region).isEmpty()) { if (account.getTimeEntryInRegion(region).isEmpty()) {
account.addStartTimeInRegion(region, currentTimeMillis); account.addTimeEntryInRegion(region, currentTimeMillis);
} }
final long entry = account.getStartTimeInRegion(region).get(); final TimeEntry entry = account.getTimeEntryInRegion(region).get();
long time = TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis - entry); long time = TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis - entry.getStart());
entry.setLatestEntry(currentTimeMillis);
final int secondsInsideToDiscover = configuredRegion.getSecondsInsideToDiscover(); final int secondsInsideToDiscover = configuredRegion.getSecondsInsideToDiscover();
if (time < secondsInsideToDiscover) { if (time < secondsInsideToDiscover) {
plugin.debug(String.format("Unable to discover region for %s because they have not reached the time requirement (c: %d, e: %d, t: %d).", player.getName(), secondsInsideToDiscover, entry, time)); plugin.debug(String.format("Unable to discover region for %s because they have not reached the time requirement (c: %d, e: %d, t: %d).", player.getName(), secondsInsideToDiscover, entry, time));