Apply global sign changes to game sign

This commit is contained in:
Daniel Saukel 2018-05-29 22:23:08 +02:00
parent 7c736d8c90
commit 2f89637046
5 changed files with 285 additions and 524 deletions

View File

@ -17,204 +17,99 @@
package de.erethon.dungeonsxl.global; package de.erethon.dungeonsxl.global;
import de.erethon.caliburn.category.Category; import de.erethon.caliburn.category.Category;
import de.erethon.caliburn.item.VanillaItem;
import de.erethon.commons.chat.MessageUtil; import de.erethon.commons.chat.MessageUtil;
import de.erethon.commons.misc.BlockUtil; import de.erethon.commons.misc.NumberUtil;
import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.config.DMessage; import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.dungeon.Dungeon;
import de.erethon.dungeonsxl.game.Game; import de.erethon.dungeonsxl.game.Game;
import de.erethon.dungeonsxl.player.DGroup; import de.erethon.dungeonsxl.player.DGroup;
import de.erethon.dungeonsxl.world.DResourceWorld;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.material.Attachable;
/** /**
* Basically a GroupSign, but to form a game of multiple groups. * Basically a GroupSign, but to form a game of multiple groups.
* *
* @author Frank Baumann, Milan Albrecht, Daniel Saukel * @author Frank Baumann, Milan Albrecht, Daniel Saukel
*/ */
public class GameSign extends GlobalProtection { public class GameSign extends JoinSign {
private Game[] games; public static final String GAME_SIGN_TAG = "Game";
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) { private Game game;
super(startSign.getWorld(), id);
this.startSign = startSign; public GameSign(int id, Block startSign, String identifier, int maxGroupsPerGame) {
games = new Game[maxGames]; super(id, startSign, identifier, maxGroupsPerGame);
dungeon = DungeonsXL.getInstance().getDungeons().getByName(identifier);
if (dungeon == null) {
DResourceWorld resource = DungeonsXL.getInstance().getDWorlds().getResourceByName(identifier);
if (resource != null) {
dungeon = new Dungeon(resource);
}
}
this.maxGroupsPerGame = maxGroupsPerGame;
verticalSigns = (int) Math.ceil((float) (1 + maxGroupsPerGame) / 4);
int[] direction = getDirection(this.startSign.getData());
directionX = direction[0];
directionZ = direction[1];
update();
} }
/** /**
* @return the games * @return
* the attached game
*/ */
public Game[] getGames() { public Game getGame() {
return games; return game;
} }
/** /**
* @param games * @param game
* the games to set * the game to set
*/ */
public void setGames(Game[] games) { public void setGame(Game game) {
this.games = games; this.game = game;
}
/**
* @return the dungeon
*/
public Dungeon getDungeon() {
return dungeon;
}
/**
* @param dungeon
* the dungeon to set
*/
public void setDungeon(Dungeon dungeon) {
this.dungeon = dungeon;
}
/**
* @return the maximum player count per group
*/
public int getMaxGroupsPerGame() {
return maxGroupsPerGame;
}
/**
* @param maxGroupsPerGame
* the maximum player count per group to set
*/
public void setMaxGroupsPerGame(int maxGroupsPerGame) {
this.maxGroupsPerGame = maxGroupsPerGame;
} }
/** /**
* Update this game sign to show the game(s) correctly. * Update this game sign to show the game(s) correctly.
*/ */
public void update() {
int i = 0;
for (Game game : games) {
if (!(startSign.getRelative(i * directionX, 0, i * directionZ).getState() instanceof Sign)) {
i++;
continue;
}
Sign sign = (Sign) startSign.getRelative(i * directionX, 0, i * directionZ).getState();
// Reset Signs
sign.setLine(0, "");
sign.setLine(1, "");
sign.setLine(2, "");
sign.setLine(3, "");
int yy = -1;
while (sign.getBlock().getRelative(0, yy, 0).getState() instanceof Sign) {
Sign subsign = (Sign) sign.getBlock().getRelative(0, yy, 0).getState();
subsign.setLine(0, "");
subsign.setLine(1, "");
subsign.setLine(2, "");
subsign.setLine(3, "");
subsign.update();
yy--;
}
// Set Signs
if (game != null && game.getDGroups().size() > 0) {
if (game.getDGroups().get(0).isPlaying()) {
sign.setLine(0, DMessage.SIGN_GLOBAL_IS_PLAYING.getMessage());
} else if (game.getDGroups().size() >= maxGroupsPerGame) {
sign.setLine(0, DMessage.SIGN_GLOBAL_FULL.getMessage());
} else {
sign.setLine(0, DMessage.SIGN_GLOBAL_JOIN_GAME.getMessage());
}
int j = 1;
Sign rowSign = sign;
for (DGroup dGroup : game.getDGroups()) {
if (j > 3) {
j = 0;
rowSign = (Sign) sign.getBlock().getRelative(0, -1, 0).getState();
}
if (rowSign != null) {
rowSign.setLine(j, dGroup.getName());
}
j++;
rowSign.update();
}
} else {
sign.setLine(0, DMessage.SIGN_GLOBAL_NEW_GAME.getMessage());
}
sign.update();
i++;
}
}
@Override @Override
public Set<Block> getBlocks() { public void update() {
if (blocks == null) { if (!(startSign.getState() instanceof Sign)) {
blocks = new HashSet<>(); return;
int i = games.length;
do {
i--;
blocks.add(startSign.getRelative(i * directionX, 0, i * directionZ));
} while (i >= 0);
HashSet<Block> toAdd = new HashSet<>();
for (Block block : blocks) {
i = verticalSigns;
do {
i--;
Block beneath = block.getLocation().add(0, -1 * i, 0).getBlock();
toAdd.add(beneath);
toAdd.add(BlockUtil.getAttachedBlock(beneath));
} while (i >= 0);
}
blocks.addAll(toAdd);
} }
return blocks; super.update();
Sign sign = (Sign) startSign.getState();
if (game == null || game.getDGroups().isEmpty()) {
sign.setLine(0, DMessage.SIGN_GLOBAL_NEW_GAME.getMessage());
sign.update();
return;
}
if (game.getDGroups().get(0).isPlaying()) {
sign.setLine(0, DMessage.SIGN_GLOBAL_IS_PLAYING.getMessage());
} else if (game.getDGroups().size() >= maxElements) {
sign.setLine(0, DMessage.SIGN_GLOBAL_FULL.getMessage());
} else {
sign.setLine(0, DMessage.SIGN_GLOBAL_JOIN_GAME.getMessage());
}
int j = 1;
Sign rowSign = sign;
for (DGroup dGroup : game.getDGroups()) {
if (j > 3) {
j = 0;
rowSign = (Sign) sign.getBlock().getRelative(0, -1, 0).getState();
}
if (rowSign != null) {
rowSign.setLine(j, dGroup.getName());
}
j++;
rowSign.update();
}
sign.update();
} }
@Override @Override
@ -225,8 +120,48 @@ public class GameSign extends GlobalProtection {
config.set(preString + ".y", startSign.getY()); config.set(preString + ".y", startSign.getY());
config.set(preString + ".z", startSign.getZ()); config.set(preString + ".z", startSign.getZ());
config.set(preString + ".dungeon", dungeon.getName()); config.set(preString + ".dungeon", dungeon.getName());
config.set(preString + ".maxGames", games.length); config.set(preString + ".maxGroupsPerGame", maxElements);
config.set(preString + ".maxGroupsPerGame", maxGroupsPerGame); }
public boolean onPlayerInteract(Block block, Player player) {
if (DungeonsXL.getInstance().getDWorlds().getGameWorlds().size() >= DungeonsXL.getInstance().getMainConfig().getMaxInstances()) {
MessageUtil.sendMessage(player, DMessage.ERROR_TOO_MANY_INSTANCES.getMessage());
return true;
}
DGroup dGroup = DGroup.getByPlayer(player);
if (dGroup == null) {
MessageUtil.sendMessage(player, DMessage.ERROR_JOIN_GROUP.getMessage());
return true;
}
if (!dGroup.getCaptain().equals(player)) {
MessageUtil.sendMessage(player, DMessage.ERROR_NOT_CAPTAIN.getMessage());
return true;
}
if (Game.getByDGroup(dGroup) != null) {
MessageUtil.sendMessage(player, DMessage.ERROR_LEAVE_GAME.getMessage());
return true;
}
Block topBlock = block.getRelative(0, startSign.getY() - block.getY(), 0);
if (!(topBlock.getState() instanceof Sign)) {
return true;
}
Sign topSign = (Sign) topBlock.getState();
if (topSign.getLine(0).equals(DMessage.SIGN_GLOBAL_NEW_GAME.getMessage())) {
game = new Game(dGroup);
dGroup.setDungeon(dungeon);
update();
} else if (topSign.getLine(0).equals(DMessage.SIGN_GLOBAL_JOIN_GAME.getMessage())) {
game.addDGroup(dGroup);
update();
}
return true;
} }
/* Statics */ /* Statics */
@ -239,53 +174,12 @@ public class GameSign extends GlobalProtection {
return null; return null;
} }
int x = block.getX(), y = block.getY(), z = block.getZ();
for (GlobalProtection protection : DungeonsXL.getInstance().getGlobalProtections().getProtections(GameSign.class)) { for (GlobalProtection protection : DungeonsXL.getInstance().getGlobalProtections().getProtections(GameSign.class)) {
GameSign gameSign = (GameSign) protection; GameSign gameSign = (GameSign) protection;
Block start = gameSign.startSign;
int sx1 = gameSign.startSign.getX(), sy1 = gameSign.startSign.getY(), sz1 = gameSign.startSign.getZ(); if (start == block || (start.getX() == block.getX() && start.getZ() == block.getZ() && (start.getY() >= block.getY() && start.getY() - gameSign.verticalSigns <= block.getY()))) {
int sx2 = sx1 + (gameSign.games.length - 1) * gameSign.directionX; return gameSign;
int sy2 = sy1 - gameSign.verticalSigns + 1;
int sz2 = sz1 + (gameSign.games.length - 1) * gameSign.directionZ;
if (sx1 > sx2) {
if (x < sx2 || x > sx1) {
continue;
}
} else if (sx1 < sx2) {
if (x > sx2 || x < sx1) {
continue;
}
} else if (x != sx1) {
continue;
} }
if (sy1 > sy2) {
if (y < sy2 || y > sy1) {
continue;
}
} else if (y != sy1) {
continue;
}
if (sz1 > sz2) {
if (z < sz2 || z > sz1) {
continue;
}
} else if (sz1 < sz2) {
if (z > sz2 || z < sz1) {
continue;
}
} else if (z != sz1) {
continue;
}
return gameSign;
} }
return null; return null;
@ -298,205 +192,47 @@ public class GameSign extends GlobalProtection {
public static GameSign getByGame(Game game) { public static GameSign getByGame(Game game) {
for (GlobalProtection protection : DungeonsXL.getInstance().getGlobalProtections().getProtections(GameSign.class)) { for (GlobalProtection protection : DungeonsXL.getInstance().getGlobalProtections().getProtections(GameSign.class)) {
GameSign gameSign = (GameSign) protection; GameSign gameSign = (GameSign) protection;
if (gameSign.game == game) {
for (Game signGame : gameSign.games) { return gameSign;
if (signGame == game) {
return gameSign;
}
} }
} }
return null; return null;
} }
/* SUBJECT TO CHANGE*/ public static GameSign tryToCreate(SignChangeEvent event) {
@Deprecated if (!event.getLine(0).equalsIgnoreCase(SIGN_TAG)) {
public static GameSign tryToCreate(Block startSign, String mapName, int maxGames, int maxGroupsPerGame) { return null;
}
if (!event.getLine(1).equalsIgnoreCase(GAME_SIGN_TAG)) {
return null;
}
String identifier = event.getLine(2);
int maxGroupsPerGame = NumberUtil.parseInt(event.getLine(3), 1);
return tryToCreate(event.getBlock(), identifier, maxGroupsPerGame);
}
public static GameSign tryToCreate(Block startSign, String identifier, int maxGroupsPerGame) {
World world = startSign.getWorld(); World world = startSign.getWorld();
int direction = startSign.getData(); BlockFace facing = ((Attachable) startSign.getState().getData()).getAttachedFace().getOppositeFace();
int x = startSign.getX(), y = startSign.getY(), z = startSign.getZ(); int x = startSign.getX(), y = startSign.getY(), z = startSign.getZ();
int verticalSigns = (int) Math.ceil((float) (1 + maxGroupsPerGame) / 4); int verticalSigns = (int) Math.ceil((float) (1 + maxGroupsPerGame) / 4);
while (verticalSigns > 1) {
Block block = world.getBlockAt(x, y - verticalSigns + 1, z);
block.setType(VanillaItem.WALL_SIGN.getMaterial(), false);
org.bukkit.material.Sign signData = new org.bukkit.material.Sign(VanillaItem.WALL_SIGN.getMaterial());
signData.setFacingDirection(facing);
org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState();
sign.setData(signData);
sign.update(true, false);
CopyOnWriteArrayList<Block> changeBlocks = new CopyOnWriteArrayList<>(); verticalSigns--;
int xx, yy, zz;
switch (direction) {
case 2:
zz = z;
for (yy = y; yy > y - verticalSigns; yy--) {
for (xx = x; xx > x - maxGames; xx--) {
Block block = world.getBlockAt(xx, yy, zz);
if (block.getType() != Material.AIR && block.getType() != Material.WALL_SIGN) {
return null;
}
if (block.getRelative(0, 0, 1).getType() == Material.AIR) {
return null;
}
changeBlocks.add(block);
}
}
break;
case 3:
zz = z;
for (yy = y; yy > y - verticalSigns; yy--) {
for (xx = x; xx < x + maxGames; xx++) {
Block block = world.getBlockAt(xx, yy, zz);
if (block.getType() != Material.AIR && block.getType() != Material.WALL_SIGN) {
return null;
}
if (block.getRelative(0, 0, -1).getType() == Material.AIR) {
return null;
}
changeBlocks.add(block);
}
}
break;
case 4:
xx = x;
for (yy = y; yy > y - verticalSigns; yy--) {
for (zz = z; zz < z + maxGames; zz++) {
Block block = world.getBlockAt(xx, yy, zz);
if (block.getType() != Material.AIR && block.getType() != Material.WALL_SIGN) {
return null;
}
if (block.getRelative(1, 0, 0).getType() == Material.AIR) {
return null;
}
changeBlocks.add(block);
}
}
break;
case 5:
xx = x;
for (yy = y; yy > y - verticalSigns; yy--) {
for (zz = z; zz > z - maxGames; zz--) {
Block block = world.getBlockAt(xx, yy, zz);
if (block.getType() != Material.AIR && block.getType() != Material.WALL_SIGN) {
return null;
}
if (block.getRelative(-1, 0, 0).getType() == Material.AIR) {
return null;
}
changeBlocks.add(block);
}
}
break;
} }
GameSign sign = new GameSign(DungeonsXL.getInstance().getGlobalProtections().generateId(GameSign.class, world), startSign, identifier, maxGroupsPerGame);
for (Block block : changeBlocks) {
block.setTypeIdAndData(68, startSign.getData(), true);
}
GameSign sign = new GameSign(DungeonsXL.getInstance().getGlobalProtections().generateId(GameSign.class, world), startSign, mapName, maxGames, maxGroupsPerGame);
return sign; return sign;
} }
@Deprecated
public static boolean playerInteract(Block block, Player player) {
int x = block.getX(), y = block.getY(), z = block.getZ();
GameSign gameSign = getByBlock(block);
if (gameSign == null) {
return false;
}
if (DungeonsXL.getInstance().getDWorlds().getGameWorlds().size() >= DungeonsXL.getInstance().getMainConfig().getMaxInstances()) {
MessageUtil.sendMessage(player, DMessage.ERROR_TOO_MANY_INSTANCES.getMessage());
return true;
}
DGroup dGroup = DGroup.getByPlayer(player);
if (dGroup == null) {
MessageUtil.sendMessage(player, DMessage.ERROR_JOIN_GROUP.getMessage());
return true;
}
if (!dGroup.getCaptain().equals(player)) {
MessageUtil.sendMessage(player, DMessage.ERROR_NOT_CAPTAIN.getMessage());
return true;
}
if (Game.getByDGroup(dGroup) != null) {
MessageUtil.sendMessage(player, DMessage.ERROR_LEAVE_GAME.getMessage());
return true;
}
int sx1 = gameSign.startSign.getX(), sy1 = gameSign.startSign.getY(), sz1 = gameSign.startSign.getZ();
Block topBlock = block.getRelative(0, sy1 - y, 0);
int column;
if (gameSign.directionX != 0) {
column = Math.abs(x - sx1);
} else {
column = Math.abs(z - sz1);
}
if (!(topBlock.getState() instanceof Sign)) {
return true;
}
Sign topSign = (Sign) topBlock.getState();
if (topSign.getLine(0).equals(DMessage.SIGN_GLOBAL_NEW_GAME.getMessage())) {
Game game = new Game(dGroup);
dGroup.setDungeon(gameSign.dungeon);
gameSign.games[column] = game;
gameSign.update();
} else if (topSign.getLine(0).equals(DMessage.SIGN_GLOBAL_JOIN_GAME.getMessage())) {
gameSign.games[column].addDGroup(dGroup);
gameSign.update();
}
return true;
}
@Deprecated
public static int[] getDirection(byte data) {
int[] direction = new int[2];
switch (data) {
case 2:
direction[0] = -1;
break;
case 3:
direction[0] = 1;
break;
case 4:
direction[1] = 1;
break;
case 5:
direction[1] = -1;
break;
}
return direction;
}
} }

View File

@ -188,11 +188,10 @@ public class GlobalProtectionCache {
preString = "protections.gameSigns." + world.getName() + "." + id + "."; preString = "protections.gameSigns." + world.getName() + "." + id + ".";
if (data.contains(preString)) { if (data.contains(preString)) {
String mapName = data.getString(preString + ".dungeon"); String mapName = data.getString(preString + ".dungeon");
int maxGames = data.getInt(preString + ".maxGames");
int maxGroupsPerGame = data.getInt(preString + ".maxGroupsPerGame"); int maxGroupsPerGame = data.getInt(preString + ".maxGroupsPerGame");
Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z")); Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z"));
new GameSign(id, startSign, mapName, maxGames, maxGroupsPerGame); new GameSign(id, startSign, mapName, maxGroupsPerGame);
} }
} while (data.contains(preString)); } while (data.contains(preString));

View File

@ -18,7 +18,6 @@ package de.erethon.dungeonsxl.global;
import de.erethon.caliburn.category.Category; import de.erethon.caliburn.category.Category;
import de.erethon.caliburn.item.VanillaItem; import de.erethon.caliburn.item.VanillaItem;
import de.erethon.commons.misc.NumberUtil;
import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.config.DMessage; import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DGlobalPlayer; import de.erethon.dungeonsxl.player.DGlobalPlayer;
@ -212,7 +211,8 @@ public class GlobalProtectionListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
} }
if (GameSign.playerInteract(clickedBlock, player)) { GameSign gameSign = GameSign.getByBlock(clickedBlock);
if (gameSign != null && gameSign.onPlayerInteract(clickedBlock, player)) {
event.setCancelled(true); event.setCancelled(true);
} }
@ -245,20 +245,9 @@ public class GlobalProtectionListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
} }
} else if (lines[1].equalsIgnoreCase("Game")) { } else if (lines[1].equalsIgnoreCase(GameSign.GAME_SIGN_TAG)) {
if (GameSign.tryToCreate(event) != null) {
String dungeonName = lines[2]; event.setCancelled(true);
String[] data = lines[3].split("\\,");
if (data.length >= 2 && data.length <= 3) {
int maxObjects = NumberUtil.parseInt(data[0]);
int maxMembersPerObject = NumberUtil.parseInt(data[1]);
if (maxObjects > 0 && maxMembersPerObject > 0) {
if (GameSign.tryToCreate(event.getBlock(), dungeonName, maxObjects, maxMembersPerObject) != null) {
event.setCancelled(true);
}
}
} }
} else if (lines[1].equalsIgnoreCase(LeaveSign.LEAVE_SIGN_TAG)) { } else if (lines[1].equalsIgnoreCase(LeaveSign.LEAVE_SIGN_TAG)) {

View File

@ -19,15 +19,10 @@ package de.erethon.dungeonsxl.global;
import de.erethon.caliburn.category.Category; import de.erethon.caliburn.category.Category;
import de.erethon.caliburn.item.VanillaItem; import de.erethon.caliburn.item.VanillaItem;
import de.erethon.commons.chat.MessageUtil; import de.erethon.commons.chat.MessageUtil;
import de.erethon.commons.misc.BlockUtil;
import de.erethon.commons.misc.NumberUtil; import de.erethon.commons.misc.NumberUtil;
import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.config.DMessage; import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.dungeon.Dungeon;
import de.erethon.dungeonsxl.player.DGroup; import de.erethon.dungeonsxl.player.DGroup;
import de.erethon.dungeonsxl.world.DResourceWorld;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
@ -42,34 +37,16 @@ import org.bukkit.material.Attachable;
* *
* @author Frank Baumann, Milan Albrecht, Daniel Saukel * @author Frank Baumann, Milan Albrecht, Daniel Saukel
*/ */
public class GroupSign extends GlobalProtection { public class GroupSign extends JoinSign {
public static final String GROUP_SIGN_TAG = "Group"; public static final String GROUP_SIGN_TAG = "Group";
private String groupName; private String groupName;
private DGroup group; private DGroup group;
private Dungeon dungeon;
private int maxPlayersPerGroup;
private Block startSign;
private int verticalSigns;
private Set<Block> blocks;
public GroupSign(int id, Block startSign, String identifier, int maxPlayersPerGroup, String groupName) { public GroupSign(int id, Block startSign, String identifier, int maxPlayersPerGroup, String groupName) {
super(startSign.getWorld(), id); super(id, startSign, identifier, maxPlayersPerGroup);
this.startSign = startSign;
dungeon = DungeonsXL.getInstance().getDungeons().getByName(identifier);
if (dungeon == null) {
DResourceWorld resource = DungeonsXL.getInstance().getDWorlds().getResourceByName(identifier);
if (resource != null) {
dungeon = new Dungeon(resource);
}
}
this.maxPlayersPerGroup = maxPlayersPerGroup;
verticalSigns = (int) Math.ceil((float) (1 + maxPlayersPerGroup) / 4);
this.groupName = groupName; this.groupName = groupName;
update();
} }
/** /**
@ -88,65 +65,18 @@ public class GroupSign extends GlobalProtection {
this.group = group; this.group = group;
} }
/**
* @return
* the dungeon
*/
public Dungeon getDungeon() {
return dungeon;
}
/**
* @param dungeon
* the dungeon to set
*/
public void setDungeon(Dungeon dungeon) {
this.dungeon = dungeon;
}
/**
* @return
* the maximum player count per group
*/
public int getMaxPlayersPerGroup() {
return maxPlayersPerGroup;
}
/**
* @param maxPlayersPerGroup
* the maximum player count per group to set
*/
public void setMaxPlayersPerGroup(int maxPlayersPerGroup) {
this.maxPlayersPerGroup = maxPlayersPerGroup;
}
/** /**
* Update this group sign to show the group(s) correctly. * Update this group sign to show the group(s) correctly.
*/ */
@Override
public void update() { public void update() {
if (!(startSign.getState() instanceof Sign)) { if (!(startSign.getState() instanceof Sign)) {
return; return;
} }
super.update();
Sign sign = (Sign) startSign.getState(); Sign sign = (Sign) startSign.getState();
sign.setLine(0, "");
sign.setLine(1, "");
sign.setLine(2, "");
sign.setLine(3, "");
int y = -1 * verticalSigns;
while (startSign.getRelative(0, y, 0).getState() instanceof Sign && y != 0) {
Sign subsign = (Sign) sign.getBlock().getRelative(0, y, 0).getState();
subsign.setLine(0, "");
subsign.setLine(1, "");
subsign.setLine(2, "");
subsign.setLine(3, "");
subsign.update();
y++;
}
// Set Signs
if (group == null) { if (group == null) {
sign.setLine(0, DMessage.SIGN_GLOBAL_NEW_GROUP.getMessage()); sign.setLine(0, DMessage.SIGN_GLOBAL_NEW_GROUP.getMessage());
sign.update(); sign.update();
@ -156,7 +86,7 @@ public class GroupSign extends GlobalProtection {
if (group.isPlaying()) { if (group.isPlaying()) {
sign.setLine(0, DMessage.SIGN_GLOBAL_IS_PLAYING.getMessage()); sign.setLine(0, DMessage.SIGN_GLOBAL_IS_PLAYING.getMessage());
} else if (group.getPlayers().size() >= maxPlayersPerGroup) { } else if (group.getPlayers().size() >= maxElements) {
sign.setLine(0, DMessage.SIGN_GLOBAL_FULL.getMessage()); sign.setLine(0, DMessage.SIGN_GLOBAL_FULL.getMessage());
} else { } else {
@ -183,35 +113,6 @@ public class GroupSign extends GlobalProtection {
sign.update(); sign.update();
} }
@Override
public Set<Block> getBlocks() {
if (blocks == null) {
blocks = new HashSet<>();
HashSet<Block> toAdd = new HashSet<>();
int y = -1 * verticalSigns;
while (y != 1) {
blocks.add(startSign.getRelative(0, y, 0));
y++;
}
for (Block block : blocks) {
int i = verticalSigns;
do {
i--;
Block beneath = block.getLocation().add(0, -1 * i, 0).getBlock();
toAdd.add(beneath);
toAdd.add(BlockUtil.getAttachedBlock(beneath));
} while (i >= 0);
}
blocks.addAll(toAdd);
}
return blocks;
}
@Override @Override
public void save(FileConfiguration config) { public void save(FileConfiguration config) {
String preString = "protections.groupSigns." + getWorld().getName() + "." + getId(); String preString = "protections.groupSigns." + getWorld().getName() + "." + getId();
@ -221,20 +122,16 @@ public class GroupSign extends GlobalProtection {
config.set(preString + ".z", startSign.getZ()); config.set(preString + ".z", startSign.getZ());
config.set(preString + ".dungeon", dungeon.getName()); config.set(preString + ".dungeon", dungeon.getName());
config.set(preString + ".groupName", groupName); config.set(preString + ".groupName", groupName);
config.set(preString + ".maxPlayersPerGroup", maxPlayersPerGroup); config.set(preString + ".maxPlayersPerGroup", maxElements);
} }
public boolean onPlayerInteract(Block block, Player player) { public boolean onPlayerInteract(Block block, Player player) {
int y = block.getY();
if (DGroup.getByPlayer(player) != null) { if (DGroup.getByPlayer(player) != null) {
MessageUtil.sendMessage(player, DMessage.ERROR_LEAVE_GROUP.getMessage()); MessageUtil.sendMessage(player, DMessage.ERROR_LEAVE_GROUP.getMessage());
return true; return true;
} }
int sy1 = startSign.getY(); Block topBlock = block.getRelative(0, startSign.getY() - block.getY(), 0);
Block topBlock = block.getRelative(0, sy1 - y, 0);
if (!(topBlock.getState() instanceof Sign)) { if (!(topBlock.getState() instanceof Sign)) {
return true; return true;
} }
@ -308,7 +205,7 @@ public class GroupSign extends GlobalProtection {
int verticalSigns = (int) Math.ceil((float) (1 + maxPlayersPerGroup) / 4); int verticalSigns = (int) Math.ceil((float) (1 + maxPlayersPerGroup) / 4);
while (verticalSigns > 1) { while (verticalSigns > 1) {
Block block = world.getBlockAt(x, y - verticalSigns, z); Block block = world.getBlockAt(x, y - verticalSigns + 1, z);
block.setType(VanillaItem.WALL_SIGN.getMaterial(), false); block.setType(VanillaItem.WALL_SIGN.getMaterial(), false);
org.bukkit.material.Sign signData = new org.bukkit.material.Sign(VanillaItem.WALL_SIGN.getMaterial()); org.bukkit.material.Sign signData = new org.bukkit.material.Sign(VanillaItem.WALL_SIGN.getMaterial());
signData.setFacingDirection(facing); signData.setFacingDirection(facing);

View File

@ -0,0 +1,140 @@
/*
* Copyright (C) 2012-2018 Frank Baumann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.global;
import de.erethon.commons.misc.BlockUtil;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.dungeon.Dungeon;
import de.erethon.dungeonsxl.world.DResourceWorld;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.configuration.file.FileConfiguration;
/**
* @author Daniel Saukel
*/
public class JoinSign extends GlobalProtection {
protected Dungeon dungeon;
protected int maxElements;
protected Block startSign;
protected int verticalSigns;
protected Set<Block> blocks;
protected JoinSign(int id, Block startSign, String identifier, int maxElements) {
super(startSign.getWorld(), id);
this.startSign = startSign;
dungeon = DungeonsXL.getInstance().getDungeons().getByName(identifier);
if (dungeon == null) {
DResourceWorld resource = DungeonsXL.getInstance().getDWorlds().getResourceByName(identifier);
if (resource != null) {
dungeon = new Dungeon(resource);
}
}
verticalSigns = (int) Math.ceil((float) (1 + maxElements) / 4);
this.maxElements = maxElements;
update();
}
/**
* @return
* the dungeon
*/
public Dungeon getDungeon() {
return dungeon;
}
/**
* @param dungeon
* the dungeon to set
*/
public void setDungeon(Dungeon dungeon) {
this.dungeon = dungeon;
}
/**
* @return
* the maximum element count per sign
*/
public int getMaxElements() {
return maxElements;
}
/**
* @param maxElements
* the maximum element count per sign
*/
public void setMaxElements(int maxElements) {
this.maxElements = maxElements;
}
@Override
public Set<Block> getBlocks() {
if (blocks == null) {
blocks = new HashSet<>();
HashSet<Block> toAdd = new HashSet<>();
int y = -1 * verticalSigns;
while (y != 1) {
blocks.add(startSign.getRelative(0, y, 0));
y++;
}
for (Block block : blocks) {
int i = verticalSigns;
do {
i--;
Block beneath = block.getLocation().add(0, -1 * i, 0).getBlock();
toAdd.add(beneath);
toAdd.add(BlockUtil.getAttachedBlock(beneath));
} while (i >= 0);
}
blocks.addAll(toAdd);
}
return blocks;
}
/**
* Clears signs
*/
public void update() {
int y = -1 * verticalSigns;
while (startSign.getRelative(0, y + 1, 0).getState() instanceof Sign && y != 0) {
Sign subsign = (Sign) startSign.getRelative(0, y + 1, 0).getState();
subsign.setLine(0, "");
subsign.setLine(1, "");
subsign.setLine(2, "");
subsign.setLine(3, "");
subsign.update();
y++;
}
}
@Override
public void save(FileConfiguration config) {
}
}