Add timeToNextPlayAfterStart game rule; resolves #241; resolves #367

This commit is contained in:
Daniel Saukel 2018-04-24 23:48:26 +02:00
parent f92a004fcb
commit 11ac5d5071
5 changed files with 124 additions and 39 deletions

View File

@ -67,7 +67,8 @@ public class GameRuleProvider {
/* Timer */
DEFAULT_VALUES.timeLastPlayed = 0;
DEFAULT_VALUES.timeToNextPlay = 0;
DEFAULT_VALUES.timeToNextPlayAfterStart = 0;
DEFAULT_VALUES.timeToNextPlayAfterFinish = 0;
DEFAULT_VALUES.timeToNextLoot = 0;
DEFAULT_VALUES.timeToNextWave = 10;
DEFAULT_VALUES.timeToFinish = -1;
@ -122,7 +123,8 @@ public class GameRuleProvider {
/* Timer */
protected Integer timeLastPlayed;
protected Integer timeToNextPlay;
protected Integer timeToNextPlayAfterStart;
protected Integer timeToNextPlayAfterFinish;
protected Integer timeToNextLoot;
protected Integer timeToNextWave;
protected Integer timeToFinish;
@ -334,10 +336,17 @@ public class GameRuleProvider {
}
/**
* @return the time until a player can play again
* @return the time until a player can play again after he started the dungeon the last time
*/
public int getTimeToNextPlay() {
return timeToNextPlay;
public int getTimeToNextPlayAfterStart() {
return timeToNextPlayAfterStart;
}
/**
* @return the time until a player can play again after he finished the dungeon the last time
*/
public int getTimeToNextPlayAfterFinish() {
return timeToNextPlayAfterFinish;
}
/**
@ -732,8 +741,12 @@ public class GameRuleProvider {
timeLastPlayed = defaultValues.timeLastPlayed;
}
if (timeToNextPlay == null) {
timeToNextPlay = defaultValues.timeToNextPlay;
if (timeToNextPlayAfterStart == null) {
timeToNextPlayAfterStart = defaultValues.timeToNextPlayAfterStart;
}
if (timeToNextPlayAfterFinish == null) {
timeToNextPlayAfterFinish = defaultValues.timeToNextPlayAfterFinish;
}
if (timeToNextLoot == null) {

View File

@ -20,6 +20,7 @@ import de.erethon.commons.chat.MessageUtil;
import de.erethon.commons.player.PlayerUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.DMessage;
import io.github.dre2n.dungeonsxl.dungeon.Dungeon;
import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerKickEvent;
import io.github.dre2n.dungeonsxl.event.dplayer.instance.DInstancePlayerUpdateEvent;
import io.github.dre2n.dungeonsxl.event.dplayer.instance.game.DGamePlayerDeathEvent;
@ -510,7 +511,7 @@ public class DGamePlayer extends DInstancePlayer {
reward.giveTo(getPlayer());
}
getData().logTimeLastPlayed(getDGroup().getDungeon().getName());
getData().logTimeLastFinished(getDGroup().getDungeonName());
// Tutorial Permissions
if (game.isTutorial() && plugin.getPermissionProvider().hasGroupSupport()) {
@ -598,8 +599,13 @@ public class DGamePlayer extends DInstancePlayer {
GameRuleProvider rules = game.getRules();
if (!checkTime(game)) {
MessageUtil.sendMessage(player, DMessage.ERROR_COOLDOWN.getMessage(String.valueOf(rules.getTimeToNextPlay())));
if (!checkTimeAfterStart(game)) {
MessageUtil.sendMessage(player, DMessage.ERROR_COOLDOWN.getMessage(String.valueOf(rules.getTimeToNextPlayAfterStart())));
return false;
}
if (!checkTimeAfterFinish(game)) {
MessageUtil.sendMessage(player, DMessage.ERROR_COOLDOWN.getMessage(String.valueOf(rules.getTimeToNextPlayAfterStart())));
return false;
}
@ -632,7 +638,7 @@ public class DGamePlayer extends DInstancePlayer {
if (new File(DungeonsXL.MAPS, dungeonName).isDirectory()) {
if (played.equalsIgnoreCase(dungeonName) || played.equalsIgnoreCase("any")) {
Long time = getData().getTimeLastPlayed(dungeonName);
Long time = getData().getTimeLastFinished(dungeonName);
if (time != -1) {
if (rules.getFinishedAll().contains(played)) {
numOfNeeded++;
@ -669,20 +675,22 @@ public class DGamePlayer extends DInstancePlayer {
return true;
}
public boolean checkTime(Game game) {
public boolean checkTimeAfterStart(Game game) {
return checkTime(game.getDungeon(), game.getRules().getTimeToNextPlayAfterStart(), getData().getTimeLastStarted(game.getDungeon().getName()));
}
public boolean checkTimeAfterFinish(Game game) {
return checkTime(game.getDungeon(), game.getRules().getTimeToNextPlayAfterFinish(), getData().getTimeLastFinished(game.getDungeon().getName()));
}
public boolean checkTime(Dungeon dungeon, int requirement, long dataTime) {
if (DPermission.hasPermission(player, DPermission.IGNORE_TIME_LIMIT)) {
return true;
}
GameRuleProvider rules = game.getRules();
if (rules.getTimeToNextPlay() != 0) {
// read PlayerConfig
long time = getData().getTimeLastPlayed(game.getDungeon().getName());
if (time != -1) {
if (time + rules.getTimeToNextPlay() * 1000 * 60 * 60 > System.currentTimeMillis()) {
return false;
}
if (requirement != -1) {
if (requirement + dataTime * 1000 * 60 * 60 > System.currentTimeMillis()) {
return false;
}
}
return true;

View File

@ -817,6 +817,7 @@ public class DGroup {
if (dPlayer == null) {
continue;
}
dPlayer.getData().logTimeLastStarted(getDungeonName());
dPlayer.respawn();
@ -890,7 +891,7 @@ public class DGroup {
}
for (DGamePlayer dPlayer : getDGamePlayers()) {
if (!dPlayer.checkTime(game)) {
if (!dPlayer.checkTimeAfterStart(game) || !dPlayer.checkTimeAfterFinish(game)) {
return false;
}
}

View File

@ -47,7 +47,7 @@ public class DPlayerData extends DREConfig {
boolean is1_9 = Internals.andHigher(Internals.v1_9_R1).contains(CompatibilityHandler.getInstance().getInternals());
public static final int CONFIG_VERSION = 2;
public static final int CONFIG_VERSION = 3;
public static final String PREFIX_STATE_PERSISTENCE = "savePlayer.";
public static final String PREFIX_STATS = "stats.";
@ -67,7 +67,8 @@ public class DPlayerData extends DREConfig {
private Collection<PotionEffect> oldPotionEffects;
// Stats
private Map<String, Long> timeLastPlayed = new HashMap<>();
private Map<String, Long> timeLastStarted = new HashMap<>();
private Map<String, Long> timeLastFinished = new HashMap<>();
public DPlayerData(File file) {
super(file, CONFIG_VERSION);
@ -266,11 +267,43 @@ public class DPlayerData extends DREConfig {
oldPotionEffects = potionEffects;
}
/**
* @return a map of the player's started dungeons with dates.
*/
public Map<String, Long> getTimeLastStarted() {
return timeLastStarted;
}
/**
* @param dungeon
* the dungeon to check
* @return the time when the player started the dungeon for the last time
*/
public long getTimeLastStarted(String dungeon) {
Long time = timeLastStarted.get(dungeon.toLowerCase());
if (time == null) {
return -1;
} else {
return time;
}
}
/**
* @param dungeon
* the started dungeon
* @param time
* the time when the dungeon was started
*/
public void setTimeLastStarted(String dungeon, long time) {
timeLastStarted.put(dungeon.toLowerCase(), time);
save();
}
/**
* @return a map of the player's finished dungeons with dates.
*/
public Map<String, Long> getTimeLastPlayed() {
return timeLastPlayed;
public Map<String, Long> getTimeLastFinished() {
return timeLastFinished;
}
/**
@ -278,8 +311,8 @@ public class DPlayerData extends DREConfig {
* the dungeon to check
* @return the time when the player finished the dungeon for the last time
*/
public long getTimeLastPlayed(String dungeon) {
Long time = timeLastPlayed.get(dungeon.toLowerCase());
public long getTimeLastFinished(String dungeon) {
Long time = timeLastFinished.get(dungeon.toLowerCase());
if (time == null) {
return -1;
} else {
@ -293,25 +326,38 @@ public class DPlayerData extends DREConfig {
* @param time
* the time when the dungeon was finished
*/
public void setTimeLastPlayed(String dungeon, long time) {
timeLastPlayed.put(dungeon.toLowerCase(), time);
public void setTimeLastFinished(String dungeon, long time) {
timeLastFinished.put(dungeon.toLowerCase(), time);
save();
}
/* Actions */
/**
* @param dungeon
* the started dungeon
*/
public void logTimeLastStarted(String dungeon) {
timeLastStarted.put(dungeon.toLowerCase(), System.currentTimeMillis());
save();
}
/**
* @param dungeon
* the finished dungeon
*/
public void logTimeLastPlayed(String dungeon) {
timeLastPlayed.put(dungeon.toLowerCase(), System.currentTimeMillis());
public void logTimeLastFinished(String dungeon) {
timeLastFinished.put(dungeon.toLowerCase(), System.currentTimeMillis());
save();
}
@Override
public void initialize() {
if (!config.contains(PREFIX_STATS + "timeLastPlayed")) {
config.createSection(PREFIX_STATS + "timeLastPlayed");
if (!config.contains(PREFIX_STATS + "timeLastStarted")) {
config.createSection(PREFIX_STATS + "timeLastStarted");
}
if (!config.contains(PREFIX_STATS + "timeLastFinished")) {
config.createSection(PREFIX_STATS + "timeLastFinished");
}
if (!file.exists()) {
@ -327,9 +373,15 @@ public class DPlayerData extends DREConfig {
@Override
public void load() {
if (config.isConfigurationSection(PREFIX_STATS + "timeLastPlayed")) {
for (String key : config.getConfigurationSection(PREFIX_STATS + "timeLastPlayed").getKeys(false)) {
timeLastPlayed.put(key, config.getLong(PREFIX_STATS + "timeLastPlayed." + key));
if (config.isConfigurationSection(PREFIX_STATS + "timeLastStarted")) {
for (String key : config.getConfigurationSection(PREFIX_STATS + "timeLastStarted").getKeys(false)) {
timeLastStarted.put(key, config.getLong(PREFIX_STATS + "timeLastStarted." + key));
}
}
if (config.isConfigurationSection(PREFIX_STATS + "timeLastFinished")) {
for (String key : config.getConfigurationSection(PREFIX_STATS + "timeLastFinished").getKeys(false)) {
timeLastFinished.put(key, config.getLong(PREFIX_STATS + "timeLastFinished." + key));
}
}
@ -363,7 +415,8 @@ public class DPlayerData extends DREConfig {
@Override
public void save() {
config.set(PREFIX_STATS + "timeLastPlayed", timeLastPlayed);
config.set(PREFIX_STATS + "timeLastStarted", timeLastStarted);
config.set(PREFIX_STATS + "timeLastFinished", timeLastFinished);
super.save();
}

View File

@ -106,6 +106,7 @@ public class WorldConfig extends GameRuleProvider {
}
/* Keep Inventory */
// DEPRECATED
if (configFile.contains("keepInventory")) {
if (!configFile.contains("keepInventoryOnEnter")) {
keepInventoryOnEnter = configFile.getBoolean("keepInventory");
@ -221,8 +222,17 @@ public class WorldConfig extends GameRuleProvider {
}
/* Times */
if (configFile.contains("timeToNextPlayAfterStart")) {
timeToNextPlayAfterStart = configFile.getInt("timeToNextPlayAfterStart");
}
// DEPRECATED
if (configFile.contains("timeToNextPlay")) {
timeToNextPlay = configFile.getInt("timeToNextPlay");
timeToNextPlayAfterFinish = configFile.getInt("timeToNextPlay");
}
if (configFile.contains("timeToNextPlayAfterFinish")) {
timeToNextPlayAfterFinish = configFile.getInt("timeToNextPlayAfterFinish");
}
if (configFile.contains("timeToNextLoot")) {