Weather game rules; resolves #237

This commit is contained in:
Daniel Saukel 2018-04-24 22:51:53 +02:00
parent 7bbee750b4
commit f92a004fcb
5 changed files with 124 additions and 8 deletions

View File

@ -46,13 +46,16 @@ public class GameRuleProvider {
DEFAULT_VALUES.keepInventoryOnDeath = true;
DEFAULT_VALUES.lobbyDisabled = false;
/* World interaction */
/* World */
DEFAULT_VALUES.gameMode = GameMode.SURVIVAL;
DEFAULT_VALUES.breakBlocks = false;
DEFAULT_VALUES.breakPlacedBlocks = false;
DEFAULT_VALUES.breakWhitelist = null;
DEFAULT_VALUES.placeBlocks = false;
DEFAULT_VALUES.placeWhitelist = null;
DEFAULT_VALUES.rain = null;
DEFAULT_VALUES.thunder = null;
DEFAULT_VALUES.time = null;
/* Fighting */
DEFAULT_VALUES.playerVersusPlayer = false;
@ -98,13 +101,16 @@ public class GameRuleProvider {
protected Boolean keepInventoryOnDeath;
protected Boolean lobbyDisabled;
/* World interaction */
/* World */
protected GameMode gameMode;
protected Boolean breakBlocks;
protected Boolean breakPlacedBlocks;
protected Map<Material, HashSet<Material>> breakWhitelist;
protected Boolean placeBlocks;
protected Set<Material> placeWhitelist;
protected Boolean rain;
protected Boolean thunder;
protected Long time;
/* Fighting */
protected Boolean playerVersusPlayer;
@ -183,7 +189,7 @@ public class GameRuleProvider {
return lobbyDisabled;
}
// World interaction
// World
/**
* @return the gameMode
*/
@ -226,6 +232,56 @@ public class GameRuleProvider {
return placeWhitelist;
}
/**
* @return
* if it's raining permanently in this dungeon,
* null if random
*/
public Boolean isRaining() {
return rain;
}
/**
* @param rain
* set if it's raining permanently in this dungeon
*/
public void setRaining(Boolean rain) {
this.rain = rain;
}
/**
* @return
* You've been... THUNDERSTRUCK!
*/
public Boolean isThundering() {
return thunder;
}
/**
* @param thunder
* You've been... THUNDERSTRUCK!
*/
public void setThundering(Boolean thunder) {
this.thunder = thunder;
}
/**
* @return
* the locked day time in this dungeon,
* null if not locked
*/
public Long getTime() {
return time;
}
/**
* @param time
* the locked day time to set
*/
public void setTime(Long time) {
this.time = time;
}
// Fight
/**
* @return if players may attack each other
@ -609,7 +665,7 @@ public class GameRuleProvider {
lobbyDisabled = defaultValues.lobbyDisabled;
}
/* World interaction */
/* World */
if (gameMode == null) {
gameMode = defaultValues.gameMode;
}
@ -634,6 +690,18 @@ public class GameRuleProvider {
placeWhitelist = defaultValues.placeWhitelist;
}
if (rain == null) {
rain = defaultValues.rain;
}
if (thunder == null) {
thunder = defaultValues.thunder;
}
if (time == null) {
time = defaultValues.time;
}
/* Fighting */
if (playerVersusPlayer == null) {
playerVersusPlayer = defaultValues.playerVersusPlayer;

View File

@ -767,6 +767,7 @@ public class DGroup {
}
game.fetchRules();
GameRuleProvider rules = game.getRules();
gameWorld.setWeather(rules);
color = plugin.getMainConfig().getGroupColorPriority().get(game.getDGroups().indexOf(this));

View File

@ -18,6 +18,7 @@ package io.github.dre2n.dungeonsxl.world;
import de.erethon.commons.chat.MessageUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.game.GameRuleProvider;
import io.github.dre2n.dungeonsxl.player.DGamePlayer;
import java.io.File;
import org.bukkit.Location;
@ -126,6 +127,31 @@ public abstract class DInstanceWorld {
}
}
/**
* @param rules
* sets up the time and weather to match the rules
*/
public void setWeather(GameRuleProvider rules) {
if (world == null) {
return;
}
if (rules.isThundering() != null) {
if (rules.isThundering()) {
world.setThundering(true);
world.setStorm(true);
world.setThunderDuration(Integer.MAX_VALUE);
} else {
world.setThundering(false);
world.setStorm(false);
}
}
if (rules.getTime() != null) {
world.setTime(rules.getTime());
}
}
/* Abstracts */
/**
* Deletes this instance.

View File

@ -16,6 +16,7 @@
*/
package io.github.dre2n.dungeonsxl.world;
import io.github.dre2n.dungeonsxl.game.Game;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.LivingEntity;
@ -141,8 +142,16 @@ public class DWorldListener implements Listener {
@EventHandler
public void onWeatherChange(WeatherChangeEvent event) {
if (dWorlds.getInstanceByWorld(event.getWorld()) != null) {
if (event.toWeatherState()) {
DInstanceWorld dWorld = dWorlds.getInstanceByWorld(event.getWorld());
if (dWorld instanceof DEditWorld && event.toWeatherState()) {
event.setCancelled(true);
} else if (dWorld instanceof DGameWorld) {
Game game = Game.getByGameWorld((DGameWorld) dWorld);
Boolean raining = game.getRules().isRaining();
if (raining == null) {
return;
}
if ((raining && !event.toWeatherState()) || (!raining && event.toWeatherState())) {
event.setCancelled(true);
}
}

View File

@ -134,7 +134,7 @@ public class WorldConfig extends GameRuleProvider {
keepInventoryOnDeath = configFile.getBoolean("keepInventoryOnDeath");
}
/* World interaction */
/* World */
if (configFile.contains("gameMode")) {
if (EnumUtil.isValidEnum(GameMode.class, configFile.getString("gameMode").toUpperCase())) {
gameMode = GameMode.valueOf(configFile.getString("gameMode"));
@ -184,6 +184,18 @@ public class WorldConfig extends GameRuleProvider {
}
}
if (configFile.contains("rain")) {
rain = configFile.getBoolean("rain");
}
if (configFile.contains("thunder")) {
thunder = configFile.getBoolean("thunder");
}
if (configFile.contains("time")) {
time = configFile.getLong("time");
}
/* PvP */
if (configFile.contains("playerVersusPlayer")) {
playerVersusPlayer = configFile.getBoolean("playerVersusPlayer");