Skip to content

Commit

Permalink
Direct play join signs. Resolves #451
Browse files Browse the repository at this point in the history
  • Loading branch information
Sataniel98 committed Sep 1, 2018
1 parent 9c50677 commit 7688d55
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 18 deletions.
24 changes: 18 additions & 6 deletions src/main/java/de/erethon/dungeonsxl/global/GameSign.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class GameSign extends JoinSign {

private Game game;

public GameSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxGroupsPerGame) {
super(plugin, id, startSign, identifier, maxGroupsPerGame);
public GameSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxGroupsPerGame, int startIfElementsAtLeast) {
super(plugin, id, startSign, identifier, maxGroupsPerGame, startIfElementsAtLeast);
}

/**
Expand Down Expand Up @@ -76,11 +76,17 @@ public void update() {
Sign sign = (Sign) startSign.getState();

if (game == null || game.getDGroups().isEmpty()) {
loadedWorld = false;
sign.setLine(0, DMessage.SIGN_GLOBAL_NEW_GAME.getMessage());
sign.update();
return;
}

if (game.getDGroups().size() >= startIfElementsAtLeast && startIfElementsAtLeast != -1) {
loadedWorld = true;
game.getDGroups().forEach(g -> g.teleport());
}

if (game.getDGroups().get(0).isPlaying()) {
sign.setLine(0, DMessage.SIGN_GLOBAL_IS_PLAYING.getMessage());

Expand Down Expand Up @@ -122,6 +128,7 @@ public void save(FileConfiguration config) {
config.set(preString + ".dungeon", dungeon.getName());
}
config.set(preString + ".maxGroupsPerGame", maxElements);
config.set(preString + ".startIfElementsAtLeast", startIfElementsAtLeast);
}

public void onPlayerInteract(Block block, Player player) {
Expand Down Expand Up @@ -209,12 +216,17 @@ public static GameSign tryToCreate(DungeonsXL plugin, SignChangeEvent event) {
}

String identifier = event.getLine(2);
int maxGroupsPerGame = NumberUtil.parseInt(event.getLine(3), 1);
String[] data = event.getLine(3).split(",");
int maxGroupsPerGame = NumberUtil.parseInt(data[0], 1);
int startIfElementsAtLeast = -1;
if (data.length > 1) {
startIfElementsAtLeast = NumberUtil.parseInt(data[1], -1);
}

return tryToCreate(plugin, event.getBlock(), identifier, maxGroupsPerGame);
return tryToCreate(plugin, event.getBlock(), identifier, maxGroupsPerGame, startIfElementsAtLeast);
}

public static GameSign tryToCreate(DungeonsXL plugin, Block startSign, String identifier, int maxGroupsPerGame) {
public static GameSign tryToCreate(DungeonsXL plugin, Block startSign, String identifier, int maxGroupsPerGame, int startIfElementsAtLeast) {
World world = startSign.getWorld();
BlockFace facing = ((Attachable) startSign.getState().getData()).getAttachedFace().getOppositeFace();
int x = startSign.getX(), y = startSign.getY(), z = startSign.getZ();
Expand All @@ -231,7 +243,7 @@ public static GameSign tryToCreate(DungeonsXL plugin, Block startSign, String id

verticalSigns--;
}
GameSign sign = new GameSign(plugin, plugin.getGlobalProtectionCache().generateId(GameSign.class, world), startSign, identifier, maxGroupsPerGame);
GameSign sign = new GameSign(plugin, plugin.getGlobalProtectionCache().generateId(GameSign.class, world), startSign, identifier, maxGroupsPerGame, startIfElementsAtLeast);

LWCUtil.removeProtection(startSign);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ public void loadAll() {
if (data.contains(preString)) {
String mapName = data.getString(preString + ".dungeon");
int maxGroupsPerGame = data.getInt(preString + ".maxGroupsPerGame");
int startIfElementsAtLeast = data.getInt(preString + ".startIfElementsAtLeast");
Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z"));

new GameSign(plugin, id, startSign, mapName, maxGroupsPerGame);
new GameSign(plugin, id, startSign, mapName, maxGroupsPerGame, startIfElementsAtLeast);
}

} while (data.contains(preString));
Expand All @@ -205,9 +206,10 @@ public void loadAll() {
String mapName = data.getString(preString + ".dungeon");
String groupName = data.getString(preString + ".groupName");
int maxPlayersPerGroup = data.getInt(preString + ".maxPlayersPerGroup");
int startIfElementsAtLeast = data.getInt(preString + ".startIfElementsAtLeast");
Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z"));

new GroupSign(plugin, id, startSign, mapName, maxPlayersPerGroup, groupName);
new GroupSign(plugin, id, startSign, mapName, maxPlayersPerGroup, startIfElementsAtLeast, groupName);
}
} while (data.contains(preString));
}
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/de/erethon/dungeonsxl/global/GroupSign.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class GroupSign extends JoinSign {
private String groupName;
private DGroup group;

public GroupSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxPlayersPerGroup, String groupName) {
super(plugin, id, startSign, identifier, maxPlayersPerGroup);
public GroupSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxPlayersPerGroup, int startIfElementsAtLeast, String groupName) {
super(plugin, id, startSign, identifier, maxPlayersPerGroup, startIfElementsAtLeast);
this.groupName = groupName;
}

Expand Down Expand Up @@ -77,11 +77,17 @@ public void update() {
Sign sign = (Sign) startSign.getState();

if (group == null) {
loadedWorld = false;
sign.setLine(0, DMessage.SIGN_GLOBAL_NEW_GROUP.getMessage());
sign.update();
return;
}

if (group.getPlayers().size() >= startIfElementsAtLeast && startIfElementsAtLeast != -1 && !loadedWorld) {
loadedWorld = true;
group.teleport();
}

if (group.isPlaying()) {
sign.setLine(0, DMessage.SIGN_GLOBAL_IS_PLAYING.getMessage());

Expand Down Expand Up @@ -124,6 +130,7 @@ public void save(FileConfiguration config) {
}
config.set(preString + ".groupName", groupName);
config.set(preString + ".maxPlayersPerGroup", maxElements);
config.set(preString + ".startIfElementsAtLeast", startIfElementsAtLeast);
}

public void onPlayerInteract(Block block, Player player) {
Expand Down Expand Up @@ -193,17 +200,21 @@ public static GroupSign tryToCreate(DungeonsXL plugin, SignChangeEvent event) {
String[] data = event.getLine(3).split(",");
int maxPlayersPerGroup = 1;
String groupName = null;
int startIfElementsAtLeast = -1;
if (data.length >= 1) {
maxPlayersPerGroup = NumberUtil.parseInt(data[0], 1);
}
if (data.length == 2) {
if (data.length >= 2) {
groupName = data[1];
}
if (data.length == 3) {
startIfElementsAtLeast = NumberUtil.parseInt(data[2], -1);
}

return tryToCreate(plugin, event.getBlock(), identifier, maxPlayersPerGroup, groupName);
return tryToCreate(plugin, event.getBlock(), identifier, maxPlayersPerGroup, startIfElementsAtLeast, groupName);
}

public static GroupSign tryToCreate(DungeonsXL plugin, Block startSign, String identifier, int maxPlayersPerGroup, String groupName) {
public static GroupSign tryToCreate(DungeonsXL plugin, Block startSign, String identifier, int maxPlayersPerGroup, int startIfElementsAtLeast, String groupName) {
World world = startSign.getWorld();
BlockFace facing = ((Attachable) startSign.getState().getData()).getAttachedFace().getOppositeFace();
int x = startSign.getX(), y = startSign.getY(), z = startSign.getZ();
Expand All @@ -220,7 +231,8 @@ public static GroupSign tryToCreate(DungeonsXL plugin, Block startSign, String i

verticalSigns--;
}
GroupSign sign = new GroupSign(plugin, plugin.getGlobalProtectionCache().generateId(GroupSign.class, world), startSign, identifier, maxPlayersPerGroup, groupName);
GroupSign sign = new GroupSign(plugin, plugin.getGlobalProtectionCache().generateId(GroupSign.class, world), startSign, identifier, maxPlayersPerGroup,
startIfElementsAtLeast, groupName);

LWCUtil.removeProtection(startSign);

Expand Down
36 changes: 32 additions & 4 deletions src/main/java/de/erethon/dungeonsxl/global/JoinSign.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public class JoinSign extends GlobalProtection {

protected Dungeon dungeon;
protected int maxElements;
protected int startIfElementsAtLeast = -1;
protected Block startSign;
protected int verticalSigns;
protected Set<Block> blocks;
protected boolean loadedWorld;

protected JoinSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxElements) {
protected JoinSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxElements, int startIfElementsAtLeast) {
super(plugin, startSign.getWorld(), id);

this.startSign = startSign;
Expand All @@ -52,6 +54,9 @@ protected JoinSign(DungeonsXL plugin, int id, Block startSign, String identifier
verticalSigns = (int) Math.ceil((float) (1 + maxElements) / 4);

this.maxElements = maxElements;
if (startIfElementsAtLeast > 0 && startIfElementsAtLeast <= maxElements) {
this.startIfElementsAtLeast = startIfElementsAtLeast;
}

update();
}
Expand All @@ -78,10 +83,33 @@ public int getMaxElements() {
}

/**
* @param maxElements the maximum element count per sign
* @param amount the maximum element count per sign
*/
public void setMaxElements(int maxElements) {
this.maxElements = maxElements;
public void setMaxElements(int amount) {
maxElements = amount;
}

/**
* Returns the minimum amount of elements required to start the dungeon countdown
*
* @return the minimum amount of elements required to start the dungeon countdown;<br>
* -1 if the dungeon is not instantiated only through the sign.
*/
public int getStartIfElementsAtLeastAmount() {
return startIfElementsAtLeast;
}

/**
* Sets the minimum amount of elements required to start the dungeon countdown
*
* @param amount the amount to set
*/
public void setStartIfElementsAtLeastAmount(int amount) {
if ((amount > 0 || amount == -1) && amount < maxElements) {
startIfElementsAtLeast = amount;
} else {
throw new IllegalArgumentException("startIfElementsAtLeastAmount is < 0 or < maxElements");
}
}

@Override
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/de/erethon/dungeonsxl/player/DGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;

Expand Down Expand Up @@ -609,6 +610,65 @@ public boolean isFinished() {
}

/* Actions */
public boolean teleport() {
if (dungeon == null || dungeon.getMap() == null) {
sendMessage(DMessage.ERROR_DUNGEON_NOT_EXIST.getMessage());
return false;
}

DGameWorld target = dungeon.getMap().instantiateAsGameWorld(false);
Game game = Game.getByDGroup(this);

if (target == null && game != null) {
target = game.getWorld();
}

if (target == null) {
if (game != null) {
for (DGroup otherTeam : game.getDGroups()) {
if (otherTeam.getGameWorld() != null) {
target = otherTeam.getGameWorld();
break;
}
}
}
}

if (target == null && dungeon != null) {
DResourceWorld resource = dungeon.getMap();
if (resource != null) {
target = resource.instantiateAsGameWorld(false);
if (target == null) {
sendMessage(DMessage.ERROR_TOO_MANY_INSTANCES.getMessage());
return false;
}
gameWorld = target;
}
}

if (target == null) {
sendMessage(DMessage.ERROR_DUNGEON_NOT_EXIST.getMessage());
return false;
}

if (game == null) {
game = new Game(plugin, this, target);

} else {
game.setWorld(target);
gameWorld = target;
}

for (OfflinePlayer offline : players.getOfflinePlayers()) {
if (!offline.isOnline()) {
players.remove(offline);
}
Player player = offline.getPlayer();
new DGamePlayer(plugin, player, target);
}
return true;
}

/**
* The group finishs the dungeon.
*/
Expand Down

0 comments on commit 7688d55

Please sign in to comment.