#177: Code improvements dungeon vs. dungeonName

This commit is contained in:
Daniel Saukel 2016-12-24 02:55:44 +01:00
parent b8bf2659d7
commit fe97b932d7
10 changed files with 117 additions and 170 deletions

View File

@ -76,7 +76,7 @@ public class EnterCommand extends BRCommand {
}
if (joining == null) {
joining = new DGroup(captain, game.getWorld().getName(), game.getDungeon() != null);
joining = new DGroup(captain, game.getDungeon());
}
if (joining.getCaptain() != captain && !DPermissions.hasPermission(sender, DPermissions.BYPASS)) {

View File

@ -93,14 +93,16 @@ public class RenameCommand extends BRCommand {
boolean changed = false;
for (GlobalProtection protection : plugin.getGlobalProtections().getProtections()) {
if (protection instanceof GroupSign) {
if (((GroupSign) protection).getMapName().equals(args[1])) {
((GroupSign) protection).setMapName(args[2]);
Dungeon dungeon = ((GroupSign) protection).getDungeon();
if (dungeon.getName().equals(args[1])) {
dungeon.setName(args[2]);
changed = true;
}
} else if (protection instanceof GameSign) {
if (((GameSign) protection).getMapName().equals(args[1])) {
((GameSign) protection).setMapName(args[2]);
Dungeon dungeon = ((GameSign) protection).getDungeon();
if (dungeon.getName().equals(args[1])) {
dungeon.setName(args[2]);
changed = true;
}
}

View File

@ -41,8 +41,9 @@ public class Dungeon {
* Real dungeon
*/
public Dungeon(File file) {
this.name = file.getName().replaceAll(".yml", "");
this.config = new DungeonConfig(file);
name = file.getName().replaceAll(".yml", "");
config = new DungeonConfig(file);
map = config.getStartFloor();
}
/**
@ -60,6 +61,14 @@ public class Dungeon {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the config
*/
@ -106,6 +115,11 @@ public class Dungeon {
* @return false if there are setup errors
*/
public boolean isSetupCorrect() {
for (DResourceWorld resource : DungeonsXL.getInstance().getDWorlds().getResources()) {
if (resource.getName().equals(name)) {
return false;
}
}
return config.getStartFloor() != null && config.getEndFloor() != null;
}

View File

@ -203,8 +203,8 @@ public class DPortal extends GlobalProtection {
}
}
if (target == null && dGroup.getMapName() != null) {
DResourceWorld resource = plugin.getDWorlds().getResourceByName(dGroup.getMapName());
if (target == null && dGroup.getDungeon() != null) {
DResourceWorld resource = dGroup.getDungeon().getMap();
if (resource != null) {
target = resource.instantiateAsGameWorld();
dGroup.setGameWorld(target);

View File

@ -21,7 +21,9 @@ import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.dungeonsxl.config.DMessages;
import io.github.dre2n.dungeonsxl.dungeon.Dungeon;
import io.github.dre2n.dungeonsxl.game.Game;
import static io.github.dre2n.dungeonsxl.global.GlobalProtection.plugin;
import io.github.dre2n.dungeonsxl.player.DGroup;
import io.github.dre2n.dungeonsxl.world.DResourceWorld;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
@ -48,31 +50,24 @@ public class GameSign extends GlobalProtection {
// Variables
private Game[] games;
private boolean multiFloor;
private String dungeonName;
private String mapName;
private Dungeon dungeon;
private int maxGroupsPerGame;
private Block startSign;
private int directionX = 0, directionZ = 0;
private int verticalSigns;
private Set<Block> blocks;
public GameSign(int id, Block startSign, String identifier, int maxGames, int maxGroupsPerGame, boolean multiFloor) {
public GameSign(int id, Block startSign, String identifier, int maxGames, int maxGroupsPerGame) {
super(startSign.getWorld(), id);
this.startSign = startSign;
games = new Game[maxGames];
this.setMultiFloor(multiFloor);
if (multiFloor) {
dungeonName = identifier;
Dungeon dungeon = plugin.getDungeons().getByName(identifier);
if (dungeon != null) {
mapName = dungeon.getConfig().getStartFloor().getName();
} else {
mapName = "invalid";
dungeon = plugin.getDungeons().getByName(identifier);
if (dungeon == null) {
DResourceWorld resource = plugin.getDWorlds().getResourceByName(identifier);
if (resource != null) {
dungeon = new Dungeon(resource);
}
} else {
mapName = identifier;
}
this.maxGroupsPerGame = maxGroupsPerGame;
verticalSigns = (int) Math.ceil((float) (1 + maxGroupsPerGame) / 4);
@ -100,48 +95,18 @@ public class GameSign extends GlobalProtection {
}
/**
* @return the multiFloor
* @return the dungeon
*/
public boolean isMultiFloor() {
return multiFloor;
public Dungeon getDungeon() {
return dungeon;
}
/**
* @param multiFloor
* the multiFloor to set
* @param dungeon
* the dungeon to set
*/
public void setMultiFloor(boolean multiFloor) {
this.multiFloor = multiFloor;
}
/**
* @return the dungeonName
*/
public String getDungeonName() {
return dungeonName;
}
/**
* @param dungeonName
* the dungeonName to set
*/
public void setDungeonName(String dungeonName) {
this.dungeonName = dungeonName;
}
/**
* @return the mapName
*/
public String getMapName() {
return mapName;
}
/**
* @param mapName
* the mapName to set
*/
public void setMapName(String mapName) {
this.mapName = mapName;
public void setDungeon(Dungeon dungeon) {
this.dungeon = dungeon;
}
/**
@ -266,17 +231,9 @@ public class GameSign extends GlobalProtection {
config.set(preString + ".x", startSign.getX());
config.set(preString + ".y", startSign.getY());
config.set(preString + ".z", startSign.getZ());
if (isMultiFloor()) {
config.set(preString + ".dungeon", dungeonName);
} else {
config.set(preString + ".dungeon", mapName);
}
config.set(preString + ".dungeon", dungeon.getName());
config.set(preString + ".maxGames", games.length);
config.set(preString + ".maxGroupsPerGame", maxGroupsPerGame);
config.set(preString + ".multiFloor", isMultiFloor());
}
/* Statics */
@ -361,7 +318,7 @@ public class GameSign extends GlobalProtection {
/* SUBJECT TO CHANGE*/
@Deprecated
public static GameSign tryToCreate(Block startSign, String mapName, int maxGames, int maxGroupsPerGame, boolean multiFloor) {
public static GameSign tryToCreate(Block startSign, String mapName, int maxGames, int maxGroupsPerGame) {
World world = startSign.getWorld();
int direction = startSign.getData();
int x = startSign.getX(), y = startSign.getY(), z = startSign.getZ();
@ -457,7 +414,7 @@ public class GameSign extends GlobalProtection {
block.setTypeIdAndData(68, startSign.getData(), true);
}
GameSign sign = new GameSign(protections.generateId(GameSign.class, world), startSign, mapName, maxGames, maxGroupsPerGame, multiFloor);
GameSign sign = new GameSign(protections.generateId(GameSign.class, world), startSign, mapName, maxGames, maxGroupsPerGame);
return sign;
}
@ -513,7 +470,7 @@ public class GameSign extends GlobalProtection {
if (topSign.getLine(0).equals(NEW_GAME)) {
Game game = new Game(dGroup);
dGroup.setDungeon(gameSign.dungeonName == null ? gameSign.mapName : gameSign.dungeonName);
dGroup.setDungeon(gameSign.dungeon);
gameSign.games[column] = game;
gameSign.update();

View File

@ -171,13 +171,9 @@ public class GlobalProtections {
String mapName = data.getString(preString + ".dungeon");
int maxGames = data.getInt(preString + ".maxGames");
int maxGroupsPerGame = data.getInt(preString + ".maxGroupsPerGame");
boolean multiFloor = false;
if (data.contains(preString + ".multiFloor")) {
multiFloor = data.getBoolean(preString + ".multiFloor");
}
Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z"));
new GameSign(id, startSign, mapName, maxGames, maxGroupsPerGame, multiFloor);
new GameSign(id, startSign, mapName, maxGames, maxGroupsPerGame);
}
} while (data.contains(preString));
@ -195,13 +191,9 @@ public class GlobalProtections {
String mapName = data.getString(preString + ".dungeon");
int maxGroups = data.getInt(preString + ".maxGroups");
int maxPlayersPerGroup = data.getInt(preString + ".maxPlayersPerGroup");
boolean multiFloor = false;
if (data.contains(preString + ".multiFloor")) {
multiFloor = data.getBoolean(preString + ".multiFloor");
}
Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z"));
new GroupSign(id, startSign, mapName, maxGroups, maxPlayersPerGroup, multiFloor);
new GroupSign(id, startSign, mapName, maxGroups, maxPlayersPerGroup);
}
} while (data.contains(preString));
}

View File

@ -21,6 +21,7 @@ import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.dungeonsxl.config.DMessages;
import io.github.dre2n.dungeonsxl.dungeon.Dungeon;
import io.github.dre2n.dungeonsxl.player.DGroup;
import io.github.dre2n.dungeonsxl.world.DResourceWorld;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
@ -47,31 +48,24 @@ public class GroupSign extends GlobalProtection {
// Variables
private DGroup[] dGroups;
private boolean multiFloor;
private String dungeonName;
private String mapName;
private Dungeon dungeon;
private int maxPlayersPerGroup;
private Block startSign;
private int directionX = 0, directionZ = 0;
private int verticalSigns;
private Set<Block> blocks;
public GroupSign(int id, Block startSign, String identifier, int maxGroups, int maxPlayersPerGroup, boolean multiFloor) {
public GroupSign(int id, Block startSign, String identifier, int maxGroups, int maxPlayersPerGroup) {
super(startSign.getWorld(), id);
this.startSign = startSign;
dGroups = new DGroup[maxGroups];
this.setMultiFloor(multiFloor);
if (multiFloor) {
dungeonName = identifier;
Dungeon dungeon = plugin.getDungeons().getByName(identifier);
if (dungeon != null) {
mapName = dungeon.getConfig().getStartFloor().getName();
} else {
mapName = "invalid";
dungeon = plugin.getDungeons().getByName(identifier);
if (dungeon == null) {
DResourceWorld resource = plugin.getDWorlds().getResourceByName(identifier);
if (resource != null) {
dungeon = new Dungeon(resource);
}
} else {
mapName = identifier;
}
this.maxPlayersPerGroup = maxPlayersPerGroup;
verticalSigns = (int) Math.ceil((float) (1 + maxPlayersPerGroup) / 4);
@ -99,48 +93,18 @@ public class GroupSign extends GlobalProtection {
}
/**
* @return the multiFloor
* @return the dungeon
*/
public boolean isMultiFloor() {
return multiFloor;
public Dungeon getDungeon() {
return dungeon;
}
/**
* @param multiFloor
* the multiFloor to set
* @param dungeon
* the dungeon to set
*/
public void setMultiFloor(boolean multiFloor) {
this.multiFloor = multiFloor;
}
/**
* @return the dungeonName
*/
public String getDungeonName() {
return dungeonName;
}
/**
* @param dungeonName
* the dungeonName to set
*/
public void setDungeonName(String dungeonName) {
this.dungeonName = dungeonName;
}
/**
* @return the mapName
*/
public String getMapName() {
return mapName;
}
/**
* @param mapName
* the mapName to set
*/
public void setMapName(String mapName) {
this.mapName = mapName;
public void setDungeon(Dungeon dungeon) {
this.dungeon = dungeon;
}
/**
@ -265,17 +229,9 @@ public class GroupSign extends GlobalProtection {
config.set(preString + ".x", startSign.getX());
config.set(preString + ".y", startSign.getY());
config.set(preString + ".z", startSign.getZ());
if (isMultiFloor()) {
config.set(preString + ".dungeon", dungeonName);
} else {
config.set(preString + ".dungeon", mapName);
}
config.set(preString + ".dungeon", dungeon.getName());
config.set(preString + ".maxGroups", dGroups.length);
config.set(preString + ".maxPlayersPerGroup", maxPlayersPerGroup);
config.set(preString + ".multiFloor", isMultiFloor());
}
/* Statics */
@ -342,7 +298,7 @@ public class GroupSign extends GlobalProtection {
/* SUBJECT TO CHANGE */
@Deprecated
public static GroupSign tryToCreate(Block startSign, String mapName, int maxGroups, int maxPlayersPerGroup, boolean multiFloor) {
public static GroupSign tryToCreate(Block startSign, String mapName, int maxGroups, int maxPlayersPerGroup) {
World world = startSign.getWorld();
int direction = startSign.getData();
int x = startSign.getX(), y = startSign.getY(), z = startSign.getZ();
@ -438,7 +394,7 @@ public class GroupSign extends GlobalProtection {
block.setTypeIdAndData(68, startSign.getData(), true);
}
GroupSign sign = new GroupSign(protections.generateId(GroupSign.class, world), startSign, mapName, maxGroups, maxPlayersPerGroup, multiFloor);
GroupSign sign = new GroupSign(protections.generateId(GroupSign.class, world), startSign, mapName, maxGroups, maxPlayersPerGroup);
return sign;
}
@ -476,12 +432,7 @@ public class GroupSign extends GlobalProtection {
Sign topSign = (Sign) topBlock.getState();
if (topSign.getLine(0).equals(NEW_GROUP)) {
if (groupSign.isMultiFloor()) {
groupSign.dGroups[column] = new DGroup(player, groupSign.dungeonName, groupSign.isMultiFloor());
} else {
groupSign.dGroups[column] = new DGroup(player, groupSign.mapName, groupSign.isMultiFloor());
}
groupSign.dGroups[column] = new DGroup(player, groupSign.dungeon);
groupSign.update();
} else if (topSign.getLine(0).equals(JOIN_GROUP)) {

View File

@ -66,27 +66,29 @@ public class BlockListener implements Listener {
}
@EventHandler
public void onBreakWithSignOnIt(BlockBreakEvent event){
public void onBreakWithSignOnIt(BlockBreakEvent event) {
Block block = event.getBlock();
Player player = event.getPlayer();
Block blockAbove = block.getRelative(BlockFace.UP);
//get the above block and return if there is nothing
if(blockAbove == null)
return;
if (blockAbove == null) {
return;
}
//return if above block is not a sign
if(blockAbove.getType() != Material.SIGN_POST && blockAbove.getType() != Material.WALL_SIGN)
return;
if (blockAbove.getType() != Material.SIGN_POST && blockAbove.getType() != Material.WALL_SIGN) {
return;
}
//let onBreak() method to handle the sign
BlockBreakEvent bbe = new BlockBreakEvent(blockAbove, player);
onBreak(bbe);
//follow the onBreak()
event.setCancelled(bbe.isCancelled());
}
@EventHandler(priority = EventPriority.HIGH)
public void onBreak(BlockBreakEvent event) {
Block block = event.getBlock();
@ -156,21 +158,15 @@ public class BlockListener implements Listener {
if (data.length >= 2 && data.length <= 3) {
int maxObjects = NumberUtil.parseInt(data[0]);
int maxMembersPerObject = NumberUtil.parseInt(data[1]);
boolean multiFloor = false;
if (data.length == 3) {
if (data[2].equals("+")) {
multiFloor = true;
}
}
if (maxObjects > 0 && maxMembersPerObject > 0) {
if (lines[1].equalsIgnoreCase("Game")) {
if (GameSign.tryToCreate(event.getBlock(), dungeonName, maxObjects, maxMembersPerObject, multiFloor) != null) {
if (GameSign.tryToCreate(event.getBlock(), dungeonName, maxObjects, maxMembersPerObject) != null) {
event.setCancelled(true);
}
} else if (lines[1].equalsIgnoreCase("Group")) {
if (GroupSign.tryToCreate(event.getBlock(), dungeonName, maxObjects, maxMembersPerObject, multiFloor) != null) {
if (GroupSign.tryToCreate(event.getBlock(), dungeonName, maxObjects, maxMembersPerObject) != null) {
event.setCancelled(true);
}
}

View File

@ -85,14 +85,17 @@ public class DGroup {
floorCount = 0;
}
@Deprecated
public DGroup(Player player, String identifier, boolean multiFloor) {
this("Group_" + plugin.getDGroups().size(), player, identifier, multiFloor);
}
@Deprecated
public DGroup(String name, Player player, String identifier, boolean multiFloor) {
this(name, player, new ArrayList<Player>(), identifier, multiFloor);
}
@Deprecated
public DGroup(String name, Player captain, List<Player> players, String identifier, boolean multiFloor) {
plugin.getDGroups().add(this);
this.name = name;
@ -126,6 +129,38 @@ public class DGroup {
floorCount = 0;
}
public DGroup(Player player, Dungeon dungeon) {
this("Group_" + plugin.getDGroups().size(), player, dungeon);
}
@Deprecated
public DGroup(String name, Player player, Dungeon dungeon) {
this(name, player, new ArrayList<Player>(), dungeon);
}
public DGroup(String name, Player captain, List<Player> players, Dungeon dungeon) {
plugin.getDGroups().add(this);
this.name = name;
DPlayerJoinDGroupEvent event = new DPlayerJoinDGroupEvent(plugin.getDPlayers().getByPlayer(captain), true, this);
plugin.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
this.captain = captain.getUniqueId();
this.players.add(captain.getUniqueId());
}
for (Player player : players) {
if (!this.players.contains(player.getUniqueId())) {
addPlayer(player);
}
}
this.dungeon = dungeon;
playing = false;
floorCount = 0;
}
// Getters and setters
/**
* @return the name; formatted
@ -688,7 +723,7 @@ public class DGroup {
MessageUtil.sendTitleMessage(player, title, subtitle, rules.getTitleFadeIn(), rules.getTitleShow(), rules.getTitleFadeOut());
} else if (getDungeonName() != null) {
} else if (!getDungeonName().equals(getMapName())) {
MessageUtil.sendTitleMessage(player, "&b&l" + getDungeonName().replaceAll("_", " "), "&4&l" + getMapName().replaceAll("_", " "));
} else {

View File

@ -36,7 +36,7 @@
<dependency>
<groupId>io.github.dre2n</groupId>
<artifactId>commons</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>io.github.dre2n</groupId>