Skip to content

Commit

Permalink
#94: Use ResourceWorld instead of String to store floors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sataniel98 committed Jun 24, 2016
1 parent 141a35f commit 24fae9d
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Announcer(String name, FileConfiguration config) {

Dungeon dungeon = plugin.getDungeons().getByName(identifier);
if (dungeon != null) {
mapName = dungeon.getConfig().getStartFloor();
mapName = dungeon.getConfig().getStartFloor().getName();
}

} else {
Expand Down Expand Up @@ -137,7 +137,7 @@ public Announcer(String name, List<String> description, List<String> worlds, Str

Dungeon dungeon = plugin.getDungeons().getByName(identifier);
if (dungeon != null) {
mapName = dungeon.getConfig().getStartFloor();
mapName = dungeon.getConfig().getStartFloor().getName();
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.github.dre2n.dungeonsxl.player.DGamePlayer;
import io.github.dre2n.dungeonsxl.player.DGroup;
import io.github.dre2n.dungeonsxl.player.DPermissions;
import io.github.dre2n.dungeonsxl.world.EditWorld;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -73,7 +72,7 @@ public void onExecute(String[] args, CommandSender sender) {
Dungeon dungeon = plugin.getDungeons().getByName(args[2]);
if (dungeon != null) {
multiFloor = true;
mapName = dungeon.getConfig().getStartFloor();
mapName = dungeon.getConfig().getStartFloor().getName();
} else {
displayHelp(player);
return;
Expand Down Expand Up @@ -108,7 +107,7 @@ public void onExecute(String[] args, CommandSender sender) {
DungeonConfig config = dungeon.getConfig();

if (config != null) {
dGroup.setMapName(config.getStartFloor());
dGroup.setMapName(config.getStartFloor().getName());
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/github/dre2n/dungeonsxl/config/DMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.github.dre2n.dungeonsxl.config;

import io.github.dre2n.commons.config.Messages;
import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
Expand Down Expand Up @@ -185,6 +186,7 @@ public enum DMessages implements Messages {
this.message = message;
}

/* Getters and setters */
@Override
public String getIdentifier() {
return identifier;
Expand All @@ -205,6 +207,14 @@ public void setMessage(String message) {
this.message = message;
}

/* Actions */
/**
* Sends the message to the console.
*/
public void debug() {
MessageUtil.log(DungeonsXL.getInstance(), getMessage());
}

/* Statics */
/**
* @param identifer
Expand Down
72 changes: 50 additions & 22 deletions src/main/java/io/github/dre2n/dungeonsxl/config/DungeonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package io.github.dre2n.dungeonsxl.config;

import io.github.dre2n.commons.config.BRConfig;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.world.ResourceWorld;
import io.github.dre2n.dungeonsxl.world.Worlds;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -26,11 +29,13 @@
*/
public class DungeonConfig extends BRConfig {

Worlds worlds = DungeonsXL.getInstance().getWorlds();

public static final int CONFIG_VERSION = 1;

private String startFloor;
private String endFloor;
private List<String> floors = new ArrayList<>();
private ResourceWorld startFloor;
private ResourceWorld endFloor;
private List<ResourceWorld> floors = new ArrayList<>();
private int floorCount;
private boolean removeWhenPlayed;
private WorldConfig overrideValues;
Expand All @@ -48,54 +53,54 @@ public DungeonConfig(File file) {
/**
* @return the startFloor
*/
public String getStartFloor() {
public ResourceWorld getStartFloor() {
return startFloor;
}

/**
* @param startFloor
* the startFloor to set
*/
public void setStartFloor(String startFloor) {
public void setStartFloor(ResourceWorld startFloor) {
this.startFloor = startFloor;
}

/**
* @return the endFloor
*/
public String getEndFloor() {
public ResourceWorld getEndFloor() {
return endFloor;
}

/**
* @param endFloor
* the endFloor to set
*/
public void setEndFloor(String endFloor) {
public void setEndFloor(ResourceWorld endFloor) {
this.endFloor = endFloor;
}

/**
* @return the floors
*/
public List<String> getFloors() {
public List<ResourceWorld> getFloors() {
return floors;
}

/**
* @param gameWorld
* the gameWorld to add
* @param resource
* the resource to add
*/
public void addFloor(String gameWorld) {
floors.add(gameWorld);
public void addFloor(ResourceWorld resource) {
floors.add(resource);
}

/**
* @param gameWorld
* the gameWorld to remove
* @param resource
* the resource to remove
*/
public void removeFloor(String gameWorld) {
floors.remove(gameWorld);
public void removeFloor(ResourceWorld resource) {
floors.remove(resource);
}

/**
Expand Down Expand Up @@ -165,18 +170,36 @@ public void setDefaultValues(WorldConfig worldConfig) {
defaultValues = worldConfig;
}

/**
* @param resource
* the ResourceWorld to check
* @return true if the floor is either in the list or the start / end floor.
*/
public boolean containsFloor(ResourceWorld resource) {
return floors.contains(resource) || startFloor.equals(resource) || endFloor.equals(resource);
}

/**
* @param mapName
* the name of the map to check
* @return true if the floor is either in the list or the start / end floor.
*/
public boolean containsFloor(String mapName) {
return containsFloor(worlds.getResourceByName(mapName));
}

@Override
public void initialize() {
if (!config.contains("floors")) {
config.set("floors", floors);
config.createSection("floors");
}

if (!config.contains("startFloor")) {
config.set("startFloor", startFloor);
config.set("startFloor", startFloor.getName());
}

if (!config.contains("endFloor")) {
config.set("endFloor", endFloor);
config.set("endFloor", endFloor.getName());
}

if (!config.contains("floorCount")) {
Expand All @@ -201,15 +224,20 @@ public void initialize() {
@Override
public void load() {
if (config.contains("floors")) {
floors = config.getStringList("floors");
for (String floor : config.getStringList("floors")) {
ResourceWorld resource = worlds.getResourceByName(floor);
if (resource != null) {
floors.add(resource);
}
}
}

if (config.contains("startFloor")) {
startFloor = config.getString("startFloor");
startFloor = worlds.getResourceByName(config.getString("startFloor"));
}

if (config.contains("endFloor")) {
endFloor = config.getString("endFloor");
endFloor = worlds.getResourceByName(config.getString("endFloor"));
}

if (config.contains("floorCount")) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/github/dre2n/dungeonsxl/dungeon/Dungeon.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ public boolean isMultiFloor() {
return config != null;
}

/**
* @return false if there are setup errors
*/
public boolean isSetupCorrect() {
return config.getStartFloor() == null || config.getEndFloor() == null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ public Dungeons() {
}

for (File file : folder.listFiles()) {
dungeons.add(new Dungeon(file));
Dungeon dungeon = new Dungeon(file);

if (dungeon.isSetupCorrect()) {
dungeons.add(dungeon);

} else {
// debug
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/github/dre2n/dungeonsxl/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ public void fetchRules() {
*
* @return the unplayed floors
*/
public List<String> getUnplayedFloors() {
List<String> unplayedFloors = new ArrayList<>();
public List<ResourceWorld> getUnplayedFloors() {
List<ResourceWorld> unplayedFloors = new ArrayList<>();
for (DGroup dGroup : dGroups) {
if (dGroup.getUnplayedFloors().size() < unplayedFloors.size()) {
unplayedFloors = dGroup.getUnplayedFloors();
Expand Down Expand Up @@ -376,7 +376,7 @@ public void finishWave(final double mobCountIncreaseRate, final boolean teleport
}

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

new BukkitRunnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public GameSign(int id, Block startSign, String identifier, int maxGames, int ma
dungeonName = identifier;
Dungeon dungeon = plugin.getDungeons().getByName(identifier);
if (dungeon != null) {
mapName = dungeon.getConfig().getStartFloor();
mapName = dungeon.getConfig().getStartFloor().getName();
} else {
mapName = "invalid";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public GroupSign(int id, Block startSign, String identifier, int maxGroups, int
dungeonName = identifier;
Dungeon dungeon = plugin.getDungeons().getByName(identifier);
if (dungeon != null) {
mapName = dungeon.getConfig().getStartFloor();
mapName = dungeon.getConfig().getStartFloor().getName();
} else {
mapName = "invalid";
}
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/io/github/dre2n/dungeonsxl/player/DGamePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ public void respawn() {
* @param specifiedFloor
* the name of the next floor
*/
public void finishFloor(String specifiedFloor) {
public void finishFloor(ResourceWorld specifiedFloor) {
MessageUtil.sendMessage(getPlayer(), DMessages.PLAYER_FINISHED_DUNGEON.getMessage());
finished = true;

Expand Down Expand Up @@ -681,29 +681,28 @@ public void finishFloor(String specifiedFloor) {

DungeonConfig dConfig = dGroup.getDungeon().getConfig();
int random = NumberUtil.generateRandomInt(0, dConfig.getFloors().size());
String newFloor = dGroup.getUnplayedFloors().get(random);
ResourceWorld newFloor = dGroup.getUnplayedFloors().get(random);
if (dConfig.getFloorCount() == dGroup.getFloorCount() - 1) {
newFloor = dConfig.getEndFloor();

} else if (specifiedFloor != null) {
newFloor = specifiedFloor;
}

DGroupFinishFloorEvent event = new DGroupFinishFloorEvent(dGroup, dGroup.getGameWorld(), newFloor);
/*DGroupFinishFloorEvent event = new DGroupFinishFloorEvent(dGroup, dGroup.getGameWorld(), newFloor);
if (event.isCancelled()) {
return;
}

*/
Game game = dGroup.getGameWorld().getGame();

dGroup.removeUnplayedFloor(dGroup.getMapName());
dGroup.setMapName(newFloor);
dGroup.removeUnplayedFloor(dGroup.getGameWorld().getResource(), false);
dGroup.setMapName(newFloor.getName());

ResourceWorld resource = plugin.getWorlds().getResourceByName(newFloor);
GameWorld gameWorld = null;
if (resource != null) {
gameWorld = resource.instantiateAsGameWorld();
if (newFloor != null) {
gameWorld = newFloor.instantiateAsGameWorld();
}
dGroup.setGameWorld(gameWorld);

Expand Down
Loading

0 comments on commit 24fae9d

Please sign in to comment.