Skip to content

Commit

Permalink
#61: Use GameRules instead of WorldConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Sataniel98 committed May 31, 2016
1 parent c94cc4c commit 6605555
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 70 deletions.
12 changes: 11 additions & 1 deletion src/main/java/io/github/dre2n/dungeonsxl/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

Expand Down Expand Up @@ -348,7 +349,7 @@ public void finishWave(final double mobCountIncreaseRate, final boolean teleport
}
}

int delay = world.getConfig().getTimeToNextWave();
int delay = rules.getTimeToNextWave();
sendMessage(plugin.getMessageConfig().getMessage(DMessages.GROUP_WAVE_FINISHED, String.valueOf(waveCount), String.valueOf(delay)));

new BukkitRunnable() {
Expand Down Expand Up @@ -410,4 +411,13 @@ public static Game getByGameWorld(GameWorld gameWorld) {
return null;
}

public static Game getByWorld(World world) {
GameWorld gameWorld = GameWorld.getByWorld(world);
if (gameWorld != null) {
return getByGameWorld(gameWorld);
} else {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.DMessages;
import io.github.dre2n.dungeonsxl.game.Game;
import io.github.dre2n.dungeonsxl.game.GamePlaceableBlock;
import io.github.dre2n.dungeonsxl.game.GameType;
import io.github.dre2n.dungeonsxl.game.GameTypeDefault;
Expand Down Expand Up @@ -107,10 +108,11 @@ public void onBreak(BlockBreakEvent event) {
}
}

if (gameWorld.getGame() != null) {
GameType gameType = gameWorld.getGame().getType();
if (gameType == GameTypeDefault.DEFAULT && gameWorld.getConfig() != null) {
event.setCancelled(!gameWorld.getConfig().canBuild());
Game game = gameWorld.getGame();
if (game != null) {
GameType gameType = game.getType();
if (gameType == GameTypeDefault.DEFAULT) {
event.setCancelled(!game.getRules().canBuild());

} else if (!gameType.canBuild()) {
event.setCancelled(true);
Expand All @@ -132,8 +134,11 @@ public void onPlace(BlockPlaceEvent event) {
return;
}

if (gameWorld.getConfig().canBuild() || GamePlaceableBlock.canBuildHere(block, block.getFace(event.getBlockAgainst()), event.getItemInHand().getType(), gameWorld)) {
return;
Game game = gameWorld.getGame();
if (game != null) {
if (game.getRules().canBuild() || GamePlaceableBlock.canBuildHere(block, block.getFace(event.getBlockAgainst()), event.getItemInHand().getType(), gameWorld)) {
return;
}
}

// Workaround for a bug that would allow 3-Block-high jumping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
package io.github.dre2n.dungeonsxl.listener;

import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.WorldConfig;
import io.github.dre2n.dungeonsxl.game.Game;
import io.github.dre2n.dungeonsxl.game.GameType;
import io.github.dre2n.dungeonsxl.game.GameTypeDefault;
import io.github.dre2n.dungeonsxl.mob.DMob;
import io.github.dre2n.dungeonsxl.player.DGamePlayer;
import io.github.dre2n.dungeonsxl.player.DGroup;
Expand Down Expand Up @@ -158,16 +155,8 @@ public void onDamageByEntity(EntityDamageByEntityEvent event) {
return;
}

WorldConfig config = gameWorld.getConfig();
GameType type = game.getType();

boolean pvp = config.isPlayerVersusPlayer();
boolean friendlyFire = config.isFriendlyFire();

if (type != GameTypeDefault.DEFAULT) {
pvp = type.isPlayerVersusPlayer();
friendlyFire = type.isFriendlyFire();
}
boolean pvp = game.getRules().isPlayerVersusPlayer();
boolean friendlyFire = game.getRules().isFriendlyFire();

Entity attackerEntity = event.getDamager();
Entity attackedEntity = event.getEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.DMessages;
import io.github.dre2n.dungeonsxl.config.WorldConfig;
import io.github.dre2n.dungeonsxl.event.dgroup.DGroupCreateEvent;
import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerDeathEvent;
import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerKickEvent;
import io.github.dre2n.dungeonsxl.game.Game;
import io.github.dre2n.dungeonsxl.global.DPortal;
import io.github.dre2n.dungeonsxl.global.GameSign;
import io.github.dre2n.dungeonsxl.global.GlobalProtection;
Expand Down Expand Up @@ -84,15 +84,18 @@ public class PlayerListener implements Listener {
@EventHandler(priority = EventPriority.HIGH)
public void onDeath(PlayerDeathEvent event) {
Player player = event.getEntity();
DGamePlayer dPlayer = DGamePlayer.getByPlayer(player);

GameWorld gameWorld = GameWorld.getByWorld(player.getLocation().getWorld());
if (gameWorld == null) {
return;
}

WorldConfig dConfig = gameWorld.getConfig();
Game game = Game.getByGameWorld(gameWorld);
if (game == null) {
return;
}

DGamePlayer dPlayer = DGamePlayer.getByPlayer(player);
if (dPlayer == null) {
return;
}
Expand All @@ -117,14 +120,12 @@ public void onDeath(PlayerDeathEvent event) {
if (dPlayer.getLives() != -1) {
MessageUtil.sendMessage(player, DMessages.PLAYER_DEATH.getMessage(String.valueOf(dPlayer.getLives())));

if (dConfig != null) {
if (dConfig.getKeepInventoryOnDeath()) {
dPlayer.setRespawnInventory(event.getEntity().getInventory().getContents());
dPlayer.setRespawnArmor(event.getEntity().getInventory().getArmorContents());
// Delete all drops
for (ItemStack item : event.getDrops()) {
item.setType(Material.AIR);
}
if (game.getRules().getKeepInventoryOnDeath()) {
dPlayer.setRespawnInventory(event.getEntity().getInventory().getContents());
dPlayer.setRespawnArmor(event.getEntity().getInventory().getArmorContents());
// Delete all drops
for (ItemStack item : event.getDrops()) {
item.setType(Material.AIR);
}
}
}
Expand All @@ -136,7 +137,7 @@ public void onDeath(PlayerDeathEvent event) {
if (!dPlayerKickEvent.isCancelled()) {
MessageUtil.broadcastMessage(DMessages.PLAYER_DEATH_KICK.getMessage(player.getName()));
dPlayer.leave();
if (gameWorld.getConfig().getKeepInventoryOnEscape()) {
if (game.getRules().getKeepInventoryOnEscape()) {
dPlayer.applyRespawnInventory();
}
}
Expand Down Expand Up @@ -347,9 +348,9 @@ public void onDropItem(PlayerDropItemEvent event) {
return;
}

GameWorld gameWorld = GameWorld.getByWorld(gamePlayer.getWorld());
Game game = Game.getByWorld(gamePlayer.getWorld());

for (Material material : gameWorld.getConfig().getSecureObjects()) {
for (Material material : game.getRules().getSecureObjects()) {
if (material == event.getItemDrop().getItemStack().getType()) {
event.setCancelled(true);
MessageUtil.sendMessage(player, DMessages.ERROR_DROP.getMessage());
Expand Down Expand Up @@ -471,9 +472,9 @@ public void onQuit(PlayerQuitEvent event) {
DGroup dGroup = DGroup.getByPlayer(player);

// Check GameWorld
GameWorld gameWorld = GameWorld.getByWorld(player.getWorld());
if (gameWorld != null) {
int timeUntilKickOfflinePlayer = gameWorld.getConfig().getTimeUntilKickOfflinePlayer();
Game game = Game.getByWorld(player.getWorld());
if (game != null) {
int timeUntilKickOfflinePlayer = game.getRules().getTimeUntilKickOfflinePlayer();

if (timeUntilKickOfflinePlayer == 0) {
dPlayer.leave();
Expand Down Expand Up @@ -587,7 +588,7 @@ public void onCommand(PlayerCommandPreprocessEvent event) {
String command = event.getMessage().toLowerCase();
ArrayList<String> commandWhitelist = new ArrayList<>();

GameWorld gameWorld = GameWorld.getByWorld(dPlayer.getWorld());
Game game = Game.getByWorld(dPlayer.getWorld());

if (dPlayer instanceof DEditPlayer) {
if (DPermissions.hasPermission(event.getPlayer(), DPermissions.CMD_EDIT)) {
Expand All @@ -597,9 +598,9 @@ public void onCommand(PlayerCommandPreprocessEvent event) {
commandWhitelist.addAll(plugin.getMainConfig().getEditCommandWhitelist());
}

} else if (gameWorld != null) {
if (gameWorld.getConfig() != null) {
commandWhitelist.addAll(gameWorld.getConfig().getGameCommandWhitelist());
} else if (game != null) {
if (game.getRules() != null) {
commandWhitelist.addAll(game.getRules().getGameCommandWhitelist());
}
}

Expand Down
28 changes: 14 additions & 14 deletions src/main/java/io/github/dre2n/dungeonsxl/player/DGamePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
import io.github.dre2n.commons.util.playerutil.PlayerUtil;
import io.github.dre2n.dungeonsxl.config.DMessages;
import io.github.dre2n.dungeonsxl.config.DungeonConfig;
import io.github.dre2n.dungeonsxl.config.WorldConfig;
import io.github.dre2n.dungeonsxl.event.dgroup.DGroupFinishDungeonEvent;
import io.github.dre2n.dungeonsxl.event.dgroup.DGroupFinishFloorEvent;
import io.github.dre2n.dungeonsxl.event.dgroup.DGroupRewardEvent;
import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerFinishEvent;
import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerKickEvent;
import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerUpdateEvent;
import io.github.dre2n.dungeonsxl.game.Game;
import io.github.dre2n.dungeonsxl.game.GameRules;
import io.github.dre2n.dungeonsxl.game.GameType;
import io.github.dre2n.dungeonsxl.game.GameTypeDefault;
import io.github.dre2n.dungeonsxl.mob.DMob;
Expand Down Expand Up @@ -83,18 +83,18 @@ public DGamePlayer(Player player, GameWorld gameWorld) {
public DGamePlayer(Player player, World world) {
super(player, world);

WorldConfig worldConfig = GameWorld.getByWorld(world).getConfig();
GameRules rules = Game.getByWorld(world).getRules();
player.setGameMode(GameMode.SURVIVAL);

if (!worldConfig.getKeepInventoryOnEnter()) {
if (!rules.getKeepInventoryOnEnter()) {
clearPlayerData();
}

if (worldConfig.isLobbyDisabled()) {
if (rules.isLobbyDisabled()) {
ready();
}

initialLives = worldConfig.getInitialLives();
initialLives = rules.getInitialLives();
lives = initialLives;

Location teleport = GameWorld.getByWorld(world).getLobbyLocation();
Expand Down Expand Up @@ -183,12 +183,12 @@ public DClass getDClass() {
* the dClass to set
*/
public void setDClass(String className) {
GameWorld gameWorld = GameWorld.getByWorld(getPlayer().getWorld());
if (gameWorld == null) {
Game game = Game.getByWorld(getPlayer().getWorld());
if (game == null) {
return;
}

DClass dClass = gameWorld.getConfig().getClass(className);
DClass dClass = game.getRules().getClass(className);
if (dClass != null) {
if (this.dClass != dClass) {
this.dClass = dClass;
Expand Down Expand Up @@ -359,19 +359,19 @@ public void setLives(int lives) {
public void leave() {
delete();

WorldConfig dConfig = GameWorld.getByWorld(getWorld()).getConfig();
GameRules rules = Game.getByWorld(getWorld()).getRules();
if (finished) {
getSavePlayer().reset(dConfig.getKeepInventoryOnFinish());
getSavePlayer().reset(rules.getKeepInventoryOnFinish());
} else {
getSavePlayer().reset(dConfig.getKeepInventoryOnEscape());
getSavePlayer().reset(rules.getKeepInventoryOnEscape());
}

GameWorld gameWorld = GameWorld.getByWorld(getWorld());
DGroup dGroup = DGroup.getByPlayer(getPlayer());

// Permission bridge
if (plugin.getPermissionProvider() != null) {
for (String permission : gameWorld.getConfig().getGamePermissions()) {
for (String permission : rules.getGamePermissions()) {
plugin.getPermissionProvider().playerRemoveTransient(getWorld().getName(), player, permission);
}
}
Expand All @@ -385,7 +385,7 @@ public void leave() {
if (game != null) {
if (finished) {
if (game.getType().hasRewards()) {
for (Reward reward : gameWorld.getConfig().getRewards()) {
for (Reward reward : rules.getRewards()) {
reward.giveTo(getPlayer());
}

Expand Down Expand Up @@ -515,7 +515,7 @@ public void respawn() {
}

// Respawn Items
if (GameWorld.getByWorld(getWorld()).getConfig().getKeepInventoryOnDeath()) {
if (Game.getByWorld(getWorld()).getRules().getKeepInventoryOnDeath()) {
applyRespawnInventory();
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.DMessages;
import io.github.dre2n.dungeonsxl.config.WorldConfig;
import io.github.dre2n.dungeonsxl.dungeon.Dungeon;
import io.github.dre2n.dungeonsxl.event.dgroup.DGroupDisbandEvent;
import io.github.dre2n.dungeonsxl.event.dgroup.DGroupStartFloorEvent;
import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerJoinDGroupEvent;
import io.github.dre2n.dungeonsxl.event.requirement.RequirementDemandEvent;
import io.github.dre2n.dungeonsxl.event.reward.RewardAdditionEvent;
import io.github.dre2n.dungeonsxl.game.Game;
import io.github.dre2n.dungeonsxl.game.GameRules;
import io.github.dre2n.dungeonsxl.game.GameType;
import io.github.dre2n.dungeonsxl.game.GameTypeDefault;
import io.github.dre2n.dungeonsxl.global.GroupSign;
Expand Down Expand Up @@ -521,9 +521,9 @@ public void startGame(Game game) {
}
}

WorldConfig config = gameWorld.getConfig();
if (config != null) {
for (Requirement requirement : config.getRequirements()) {
GameRules rules = gameWorld.getGame().getRules();
if (rules != null) {
for (Requirement requirement : rules.getRequirements()) {
RequirementDemandEvent requirementDemandEvent = new RequirementDemandEvent(requirement, player);
plugin.getServer().getPluginManager().callEvent(event);

Expand All @@ -536,22 +536,22 @@ public void startGame(Game game) {

GameType gameType = game.getType();
if (gameType == GameTypeDefault.DEFAULT) {
player.setGameMode(config.getGameMode());
if (config.isTimeIsRunning()) {
timeIsRunningTask = new TimeIsRunningTask(this, config.getTimeToFinish()).runTaskTimer(plugin, 20, 20);
player.setGameMode(rules.getGameMode());
if (rules.isTimeIsRunning()) {
timeIsRunningTask = new TimeIsRunningTask(this, rules.getTimeToFinish()).runTaskTimer(plugin, 20, 20);
}

} else {
player.setGameMode(gameType.getGameMode());
if (gameType.getShowTime()) {
timeIsRunningTask = new TimeIsRunningTask(this, config.getTimeToFinish()).runTaskTimer(plugin, 20, 20);
timeIsRunningTask = new TimeIsRunningTask(this, rules.getTimeToFinish()).runTaskTimer(plugin, 20, 20);
}
}
}

// Permission bridge
if (plugin.getPermissionProvider() != null) {
for (String permission : gameWorld.getConfig().getGamePermissions()) {
for (String permission : rules.getGamePermissions()) {
plugin.getPermissionProvider().playerRemoveTransient(gameWorld.getWorld().getName(), player, permission);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean check() {

@Override
public void onInit() {
if (getGameWorld().getConfig().isLobbyDisabled()) {
if (getGame().getRules().isLobbyDisabled()) {
getSign().getBlock().setType(Material.AIR);
return;
}
Expand All @@ -52,7 +52,7 @@ public void onInit() {
int directionZ = direction[1];

int xx = 0, zz = 0;
for (DClass dclass : getGameWorld().getConfig().getClasses()) {
for (DClass dclass : getGame().getRules().getClasses()) {

// Check existing signs
boolean isContinued = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void onInit() {
String lines[] = getSign().getLines();

if (!lines[1].isEmpty()) {
String msg = getGameWorld().getConfig().getMsg(NumberUtil.parseInt(lines[1]), true);
String msg = getGame().getRules().getMsg(NumberUtil.parseInt(lines[1]), true);
if (msg != null) {
this.msg = msg;
getSign().getBlock().setType(Material.AIR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void onInit() {
String lines[] = getSign().getLines();

if (!lines[1].isEmpty()) {
String msg = getGameWorld().getConfig().getMsg(NumberUtil.parseInt(lines[1]), true);
String msg = getGame().getRules().getMsg(NumberUtil.parseInt(lines[1]), true);
if (msg != null) {
this.msg = msg;
getSign().getBlock().setType(Material.AIR);
Expand Down
Loading

0 comments on commit 6605555

Please sign in to comment.