Schems: sign placeholders for names and spawn location

This commit is contained in:
tastybento 2018-07-02 11:21:10 -07:00
parent afa21d25ed
commit d725a3e1d8
10 changed files with 340 additions and 721 deletions

View File

@ -5,7 +5,7 @@ package us.tastybento.bskyblock.api.localization;
* @author Poslovitch
*/
public class TextVariables {
private TextVariables() {}
public static final String NAME = "[name]";
@ -14,4 +14,5 @@ public class TextVariables {
public static final String RANK = "[rank]";
public static final String LABEL = "[label]";
public static final String PERMISSION = "[permission]";
public static final String SPAWN_HERE = "[spawn_here]";
}

View File

@ -11,6 +11,7 @@ import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.util.Vector;
@ -101,7 +102,7 @@ public class Island implements DataObject {
@Expose
private int levelHandicap;
@Expose
private Location spawnPoint;
private Map<Environment, Location> spawnPoint = new HashMap<>();
public Island() {}
public Island(Location location, UUID owner, int protectionRange) {
@ -169,7 +170,7 @@ public class Island implements DataObject {
return createdDate;
}
/**
* Gets the Island Guard flag's setting. If this is a protection flag, the this will be the
* Gets the Island Guard flag's setting. If this is a protection flag, the this will be the
* rank needed to bypass this flag. If it is a Settings flag, any non-zero value means the
* setting is allowed.
* @param flag - flag
@ -288,10 +289,6 @@ public class Island implements DataObject {
return members.getOrDefault(user.getUniqueId(), RanksManager.VISITOR_RANK);
}
public Location getSpawnPoint() {
return spawnPoint;
}
/**
* @param material - Material
* @return count of how many tile entities of type mat are on the island at last count. Counts are done when a player places
@ -620,9 +617,21 @@ public class Island implements DataObject {
//TODO default flags
}
public void setSpawnPoint(Location location) {
spawnPoint = location;
/**
* Get the default spawn location for this island. Note that this may only be valid
* after the initial pasting because the player can change the island after that point
* @return the spawnPoint
*/
public Map<Environment, Location> getSpawnPoint() {
return spawnPoint;
}
/**
* Set when island is pasted
* @param spawnPoint the spawnPoint to set
*/
public void setSpawnPoint(Map<Environment, Location> spawnPoint) {
this.spawnPoint = spawnPoint;
}
@Override
@ -699,10 +708,10 @@ public class Island implements DataObject {
members.forEach((u, i) -> {
if (owner.equals(u)) {
user.sendMessage("commands.admin.info.team-owner-format", "[name]", plugin.getPlayers().getName(u)
, "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i)));
, "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i)));
} else if (i > RanksManager.VISITOR_RANK){
user.sendMessage("commands.admin.info.team-member-format", "[name]", plugin.getPlayers().getName(u)
, "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i)));
, "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i)));
}
});
}
@ -731,5 +740,23 @@ public class Island implements DataObject {
}
}
/**
* Set the spawn location for this island type
* @param islandType - island type
* @param l - location
*/
public void setSpawnPoint(Environment islandType, Location l) {
spawnPoint.put(islandType, l);
}
/**
* Get the spawn point for this island type
* @param islandType - island type
* @return - location or null if one does not exist
*/
public Location getSpawnPoint(Environment islandType) {
return spawnPoint.get(islandType);
}
}

View File

@ -10,7 +10,9 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
@ -82,6 +84,9 @@ public class Clipboard {
private BSkyBlock plugin;
private boolean copied;
// Pasted items
private Map<Location, List<String>> signs = new HashMap<>();
private File schemFolder;
public Clipboard(BSkyBlock plugin) {
@ -186,61 +191,61 @@ public class Clipboard {
* @param location - location to paste
*/
public void paste(Location location) {
signs.clear();
blockConfig.getConfigurationSection(BLOCK).getKeys(false).forEach(b -> pasteBlock(location, blockConfig.getConfigurationSection(BLOCK + "." + b)));
}
private void pasteBlock(Location location, ConfigurationSection s) {
String[] pos = s.getName().split(",");
private void pasteBlock(Location location, ConfigurationSection config) {
String[] pos = config.getName().split(",");
int x = location.getBlockX() + Integer.valueOf(pos[0]);
int y = location.getBlockY() + Integer.valueOf(pos[1]);
int z = location.getBlockZ() + Integer.valueOf(pos[2]);
Material m = Material.getMaterial(s.getString("type", "AIR"));
// Default type is air
Material material = Material.getMaterial(config.getString("type", "AIR"));
Block block = location.getWorld().getBlockAt(x, y, z);
if (s.getBoolean(ATTACHED)) {
plugin.getServer().getScheduler().runTask(plugin, () -> setBlock(block, s, m));
if (config.getBoolean(ATTACHED)) {
plugin.getServer().getScheduler().runTask(plugin, () -> setBlock(block, config, material));
} else {
setBlock(block, s, m);
setBlock(block, config, material);
}
}
@SuppressWarnings("deprecation")
private void setBlock(Block block, ConfigurationSection s, Material m) {
private void setBlock(Block block, ConfigurationSection config, Material material) {
// Block state
if (s.getBoolean(ATTACHED) && m.toString().contains("TORCH")) {
TorchDir d = TorchDir.valueOf(s.getString(FACING));
if (config.getBoolean(ATTACHED) && material.toString().contains("TORCH")) {
TorchDir d = TorchDir.valueOf(config.getString(FACING));
// The block below has to be set to something solid for this to work
Block rel = block.getRelative(BlockFace.DOWN);
Material rm = rel.getType();
Byte data = rel.getData();
if (rel.isEmpty() || rel.isLiquid()) {
rel.setType(Material.STONE);
block.setType(m);
block.setType(material);
block.setData((byte)d.ordinal());
// Set the block back to what it was
rel.setType(rm);
rel.setData(data);
} else {
block.setType(m);
block.setType(material);
block.setData((byte)d.ordinal());
}
return;
}
block.setType(m, false);
BlockState bs = block.getState();
byte data = (byte)s.getInt("data");
// Set the block type
block.setType(material, false);
// Set the block data
byte data = (byte)config.getInt("data");
block.setData(data);
// Get the block state
BlockState bs = block.getState();
// Material Data
MaterialData md = bs.getData();
if (md instanceof Openable) {
Openable open = (Openable)md;
open.setOpen(s.getBoolean("open"));
open.setOpen(config.getBoolean("open"));
}
if (md instanceof Directional) {
@ -248,37 +253,40 @@ public class Clipboard {
if (md instanceof Stairs) {
//facing.setFacingDirection(BlockFace.valueOf(s.getString(FACING)).getOppositeFace());
Stairs stairs = (Stairs)md;
stairs.setInverted(s.getBoolean("inverted"));
stairs.setFacingDirection(BlockFace.valueOf(s.getString(FACING, "NORTH")));
stairs.setInverted(config.getBoolean("inverted"));
stairs.setFacingDirection(BlockFace.valueOf(config.getString(FACING, "NORTH")));
} else {
facing.setFacingDirection(BlockFace.valueOf(s.getString(FACING, "NORTH")));
facing.setFacingDirection(BlockFace.valueOf(config.getString(FACING, "NORTH")));
}
}
if (md instanceof Lever) {
Lever r = (Lever)md;
r.setPowered(s.getBoolean(POWERED));
r.setPowered(config.getBoolean(POWERED));
}
if (md instanceof Button) {
Button r = (Button)md;
r.setPowered(s.getBoolean(POWERED));
r.setPowered(config.getBoolean(POWERED));
}
// Block data
if (bs instanceof Sign) {
Sign sign = (Sign)bs;
List<String> lines = s.getStringList("lines");
List<String> lines = config.getStringList("lines");
for (int i =0 ; i < lines.size(); i++) {
sign.setLine(i, lines.get(i));
}
sign.update();
// Log the sign
signs.put(block.getLocation(), lines);
}
if (bs instanceof Banner) {
Banner banner = (Banner)bs;
DyeColor baseColor = DyeColor.valueOf(s.getString("baseColor", "RED"));
DyeColor baseColor = DyeColor.valueOf(config.getString("baseColor", "RED"));
banner.setBaseColor(baseColor);
int i = 0;
ConfigurationSection pat = s.getConfigurationSection("pattern");
ConfigurationSection pat = config.getConfigurationSection("pattern");
if (pat != null) {
for (String pattern : pat.getKeys(false)) {
banner.setPattern(i, new Pattern(DyeColor.valueOf(pat.getString(pattern, "GREEN"))
@ -290,30 +298,27 @@ public class Clipboard {
}
if (bs instanceof CreatureSpawner) {
CreatureSpawner spawner = ((CreatureSpawner) bs);
spawner.setSpawnedType(EntityType.valueOf(s.getString("spawnedType", "PIG")));
spawner.setMaxNearbyEntities(s.getInt("maxNearbyEntities", 16));
spawner.setMaxSpawnDelay(s.getInt("maxSpawnDelay", 2*60*20));
spawner.setMinSpawnDelay(s.getInt("minSpawnDelay", 5*20));
spawner.setSpawnedType(EntityType.valueOf(config.getString("spawnedType", "PIG")));
spawner.setMaxNearbyEntities(config.getInt("maxNearbyEntities", 16));
spawner.setMaxSpawnDelay(config.getInt("maxSpawnDelay", 2*60*20));
spawner.setMinSpawnDelay(config.getInt("minSpawnDelay", 5*20));
spawner.setDelay(s.getInt("delay", -1));
spawner.setRequiredPlayerRange(s.getInt("requiredPlayerRange", 16));
spawner.setSpawnRange(s.getInt("spawnRange", 4));
spawner.setDelay(config.getInt("delay", -1));
spawner.setRequiredPlayerRange(config.getInt("requiredPlayerRange", 16));
spawner.setSpawnRange(config.getInt("spawnRange", 4));
bs.update(true, false);
}
if (bs instanceof InventoryHolder) {
bs.update(true, false);
Inventory ih = ((InventoryHolder)bs).getInventory();
ConfigurationSection inv = s.getConfigurationSection("inventory");
ConfigurationSection inv = config.getConfigurationSection("inventory");
inv.getKeys(false).forEach(i -> ih.setItem(Integer.valueOf(i), (ItemStack)inv.get(i)));
}
// Entities
if (s.isConfigurationSection("entity")) {
ConfigurationSection e = s.getConfigurationSection("entity");
if (config.isConfigurationSection("entity")) {
ConfigurationSection e = config.getConfigurationSection("entity");
e.getKeys(false).forEach(k -> {
Location center = block.getLocation().add(new Vector(0.5, 0.0, 0.5));
LivingEntity ent = (LivingEntity)block.getWorld().spawnEntity(center, EntityType.valueOf(e.getString(k + ".type", "PIG")));
@ -433,6 +438,13 @@ public class Clipboard {
return blockConfig;
}
/**
* @return the signs
*/
public Map<Location, List<String>> getSigns() {
plugin.log("DEBUG: signs " + signs.size());
return signs;
}
private void unzip(final String zipFilePath) throws IOException {
Path path = Paths.get(zipFilePath);
if (!(path.toFile().exists())) {

View File

@ -1,61 +1,106 @@
package us.tastybento.bskyblock.island.builders;
import java.io.File;
import java.io.IOException;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.TreeType;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.World.Environment;
import org.bukkit.block.Sign;
import org.bukkit.entity.EntityType;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Chest;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.Constants.GameType;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.localization.TextVariables;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.util.Util;
/**
* Fired when a team event happens.
* Generates islands
*
* @author tastybento
* @since 1.0
*/
public class IslandBuilder {
public enum IslandType {
ISLAND,
NETHER,
END
}
private Island island;
private World world;
private IslandType type = IslandType.ISLAND;
private List<ItemStack> chestItems;
private Environment type = Environment.NORMAL;
private UUID playerUUID;
private String playerName;
private BSkyBlock plugin;
private Map<Environment, Clipboard> islandSchems = new EnumMap<>(Environment.class);
private Location spawnPoint;
private Runnable task;
//TODO support companions?
public IslandBuilder(Island island) {
public IslandBuilder(BSkyBlock plugin, Island island) {
this.plugin = plugin;
this.island = island;
world = island.getWorld();
loadIslands();
}
private void loadIslands() {
File schems = new File(plugin.getDataFolder(), "schems");
if (!schems.exists()) {
if (!schems.mkdirs()) {
plugin.logError("Could not make schems folder!");
} else {
copySchems(schems);
}
}
try {
Clipboard cb = new Clipboard(plugin);
cb.load("island");
islandSchems.put(Environment.NORMAL, cb);
} catch (IOException | InvalidConfigurationException e) {
plugin.logError("Could not load default island");
}
if (plugin.getSettings().isNetherGenerate() && plugin.getSettings().isNetherIslands()) {
try {
Clipboard cbn = new Clipboard(plugin);
cbn.load("nether-island");
islandSchems.put(Environment.NETHER, cbn);
} catch (IOException | InvalidConfigurationException e) {
plugin.logError("Could not load default nether island");
}
}
if (plugin.getSettings().isEndGenerate() && plugin.getSettings().isEndIslands()) {
try {
Clipboard cbe = new Clipboard(plugin);
cbe.load("end-island");
islandSchems.put(Environment.THE_END, cbe);
} catch (IOException | InvalidConfigurationException e) {
plugin.logError("Could not load default end island");
}
}
plugin.log("Loaded " + islandSchems.size() + " islands");
}
/**
* Copies schems from the jar file
* @param schems2 - file containing schem
*/
private void copySchems(File schems2) {
plugin.saveResource("schems/island.schem", false);
plugin.saveResource("schems/nether-island.schem", false);
plugin.saveResource("schems/end-island.schem", false);
}
/**
* @param type the type to set
*/
public IslandBuilder setType(IslandType type) {
public IslandBuilder setType(Environment type) {
this.type = type;
return this;
}
@ -70,417 +115,72 @@ public class IslandBuilder {
}
/**
* @param list the default chestItems to set
* The task to run when the island is built
* @param task
* @return IslandBuilder
*/
public IslandBuilder setChestItems(List<ItemStack> list) {
chestItems = list;
public IslandBuilder run(Runnable task) {
this.task = task;
return this;
}
public void build() {
plugin.log("Pasting island to " + type);
Location loc = island.getCenter();
// Switch on island type
switch (type) {
case ISLAND:
world = island.getWorld();
if (Constants.GAMETYPE == GameType.ACIDISLAND) {
generateAcidIslandBlocks();
} else {
generateIslandBlocks();
}
break;
case NETHER:
world = Bukkit.getWorld(island.getWorld().getName() + "_nether");
if (world == null) {
return;
}
generateNetherBlocks();
break;
case END:
world = Bukkit.getWorld(island.getWorld().getName() + "_the_end");
if (world == null) {
return;
}
generateEndBlocks();
break;
case NETHER:
world = Bukkit.getWorld(island.getWorld().getName() + "_nether");
if (world == null) {
return;
}
loc = island.getCenter().toVector().toLocation(world);
break;
case THE_END:
world = Bukkit.getWorld(island.getWorld().getName() + "_the_end");
if (world == null) {
return;
}
loc = island.getCenter().toVector().toLocation(world);
break;
default:
break;
}
islandSchems.get(type).paste(loc);
// Do other stuff
// Handle signs - signs are attachable, so they are not there until 1 tick after pasting
if (playerUUID != null) {
Bukkit.getScheduler().runTaskLater(plugin, () -> islandSchems.get(type).getSigns().forEach(this::writeSign), 2L);
}
if (task != null) {
Bukkit.getScheduler().runTaskLater(plugin, task, 3L);
}
}
private void writeSign(Location loc, List<String> lines) {
Sign sign = (Sign) loc.getBlock().getState();
org.bukkit.material.Sign s = (org.bukkit.material.Sign) sign.getData();
lines.forEach(plugin::log);
// Handle spawn sign
if (!lines.isEmpty() && lines.get(0).equalsIgnoreCase(TextVariables.SPAWN_HERE)) {
loc.getBlock().setType(Material.AIR);
// Orient to face same direction as sign
spawnPoint = new Location(loc.getWorld(), loc.getBlockX() + 0.5D, loc.getBlockY(),
loc.getBlockZ() + 0.5D, Util.blockFaceToFloat(s.getFacing().getOppositeFace()), 30F);
return;
}
// Sub in player's name
for (int i = 0 ; i < lines.size(); i++) {
sign.setLine(i, lines.get(i).replace(TextVariables.NAME, playerName));
}
sign.update();
}
/**
* Creates the AcidIsland default island block by block
* @return the spawnPoint
*/
private void generateAcidIslandBlocks() {
// AcidIsland
// Build island layer by layer
// Start from the base
// half sandstone; half sand
int x = island.getCenter().getBlockX();
int z = island.getCenter().getBlockZ();
int islandHeight = island.getCenter().getBlockY();
int y = 0;
for (int x_space = x - 4; x_space <= x + 4; x_space++) {
for (int z_space = z - 4; z_space <= z + 4; z_space++) {
Block b = world.getBlockAt(x_space, y, z_space);
b.setType(Material.BEDROCK);
}
}
for (y = 1; y < islandHeight + 5; y++) {
for (int x_space = x - 4; x_space <= x + 4; x_space++) {
for (int z_space = z - 4; z_space <= z + 4; z_space++) {
Block b = world.getBlockAt(x_space, y, z_space);
if (y < (islandHeight / 2)) {
b.setType(Material.SANDSTONE);
} else {
b.setType(Material.SAND);
}
}
}
}
// Then cut off the corners to make it round-ish
for (y = 0; y < islandHeight + 5; y++) {
for (int x_space = x - 4; x_space <= x + 4; x_space += 8) {
for (int z_space = z - 4; z_space <= z + 4; z_space += 8) {
Block b = world.getBlockAt(x_space, y, z_space);
b.setType(Material.STATIONARY_WATER);
}
}
}
// Add some grass
for (y = islandHeight + 4; y < islandHeight + 5; y++) {
for (int x_space = x - 2; x_space <= x + 2; x_space++) {
for (int z_space = z - 2; z_space <= z + 2; z_space++) {
Block blockToChange = world.getBlockAt(x_space, y, z_space);
blockToChange.setType(Material.GRASS);
}
}
}
// Place bedrock - MUST be there (ensures island are not
// overwritten
Block b = world.getBlockAt(x, islandHeight, z);
b.setType(Material.BEDROCK);
// Then add some more dirt in the classic shape
y = islandHeight + 3;
for (int x_space = x - 2; x_space <= x + 2; x_space++) {
for (int z_space = z - 2; z_space <= z + 2; z_space++) {
b = world.getBlockAt(x_space, y, z_space);
b.setType(Material.DIRT);
}
}
b = world.getBlockAt(x - 3, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x + 3, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z - 3);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z + 3);
b.setType(Material.DIRT);
y = islandHeight + 2;
for (int x_space = x - 1; x_space <= x + 1; x_space++) {
for (int z_space = z - 1; z_space <= z + 1; z_space++) {
b = world.getBlockAt(x_space, y, z_space);
b.setType(Material.DIRT);
}
}
b = world.getBlockAt(x - 2, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x + 2, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z - 2);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z + 2);
b.setType(Material.DIRT);
y = islandHeight + 1;
b = world.getBlockAt(x - 1, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x + 1, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z - 1);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z + 1);
b.setType(Material.DIRT);
// Add island items
y = islandHeight;
// Add tree (natural)
Location treeLoc = new Location(world, x, y + 5D, z);
world.generateTree(treeLoc, TreeType.ACACIA);
// Place a helpful sign in front of player
placeSign(x, islandHeight + 5, z + 3);
// Place the chest - no need to use the safe spawn function
// because we
// know what this island looks like
placeChest(x, islandHeight + 5, z + 1);
}
private void generateIslandBlocks() {
// Skyblock
// Build island layer by layer
// Start from the base
// half sandstone; half sand
int x = island.getCenter().getBlockX();
int z = island.getCenter().getBlockZ();
int islandHeight = island.getCenter().getBlockY();
World world = island.getCenter().getWorld();
int y;
// Add some grass
for (y = islandHeight + 4; y < islandHeight + 5; y++) {
for (int x_space = x - 3; x_space <= x + 3; x_space++) {
for (int z_space = z - 3; z_space <= z + 3; z_space++) {
world.getBlockAt(x_space, y, z_space).setType(Material.GRASS);
}
}
}
// Then cut off the corners to make it round-ish
for (int x_space = x - 3; x_space <= x + 3; x_space += 6) {
for (int z_space = z - 3; z_space <= z + 3; z_space += 6) {
world.getBlockAt(x_space, y-1, z_space).setType(Material.AIR);
}
}
// Place bedrock - MUST be there (ensures island are not
// overwritten
Block b = world.getBlockAt(x, islandHeight, z);
b.setType(Material.BEDROCK);
// Then add some more dirt in the classic shape
y = islandHeight + 3;
for (int x_space = x - 2; x_space <= x + 2; x_space++) {
for (int z_space = z - 2; z_space <= z + 2; z_space++) {
b = world.getBlockAt(x_space, y, z_space);
b.setType(Material.DIRT);
}
}
b = world.getBlockAt(x - 3, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x + 3, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z - 3);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z + 3);
b.setType(Material.DIRT);
y = islandHeight + 2;
for (int x_space = x - 1; x_space <= x + 1; x_space++) {
for (int z_space = z - 1; z_space <= z + 1; z_space++) {
b = world.getBlockAt(x_space, y, z_space);
b.setType(Material.DIRT);
}
}
b = world.getBlockAt(x - 2, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x + 2, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z - 2);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z + 2);
b.setType(Material.DIRT);
y = islandHeight + 1;
b = world.getBlockAt(x - 1, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x + 1, y, z);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z - 1);
b.setType(Material.DIRT);
b = world.getBlockAt(x, y, z + 1);
b.setType(Material.DIRT);
// Add island items
y = islandHeight;
// Add tree (natural)
Location treeLoc = new Location(world, x, y + 5D, z);
world.generateTree(treeLoc, TreeType.TREE);
// Place a helpful sign in front of player
placeSign(x, islandHeight + 5, z + 3);
// Place the chest - no need to use the safe spawn function
// because we
// know what this island looks like
placeChest(x, islandHeight + 5, z + 1);
}
private void generateNetherBlocks() {
// Nether block
int x = island.getCenter().getBlockX();
int z = island.getCenter().getBlockZ();
int islandHeight = island.getCenter().getBlockY();
int y;
for (y = islandHeight + 4; y < islandHeight + 5; y++) {
for (int x_space = x - 3; x_space <= x + 3; x_space++) {
for (int z_space = z - 3; z_space <= z + 3; z_space++) {
world.getBlockAt(x_space, y, z_space).setType(Material.NETHER_BRICK);
}
}
}
// Then cut off the corners to make it round-ish
for (int x_space = x - 3; x_space <= x + 3; x_space += 6) {
for (int z_space = z - 3; z_space <= z + 3; z_space += 6) {
world.getBlockAt(x_space, y-1, z_space).setType(Material.AIR);
}
}
// Place bedrock - MUST be there (ensures island are not
// overwritten
Block b = world.getBlockAt(x, islandHeight, z);
b.setType(Material.BEDROCK);
// Then add some more dirt in the classic shape
y = islandHeight + 3;
for (int x_space = x - 2; x_space <= x + 2; x_space++) {
for (int z_space = z - 2; z_space <= z + 2; z_space++) {
b = world.getBlockAt(x_space, y, z_space);
b.setType(Material.NETHERRACK);
}
}
b = world.getBlockAt(x - 3, y, z);
b.setType(Material.SOUL_SAND);
b = world.getBlockAt(x + 3, y, z);
b.setType(Material.SOUL_SAND);
b = world.getBlockAt(x, y, z - 3);
b.setType(Material.SOUL_SAND);
b = world.getBlockAt(x, y, z + 3);
b.setType(Material.SOUL_SAND);
y = islandHeight + 2;
for (int x_space = x - 1; x_space <= x + 1; x_space++) {
for (int z_space = z - 1; z_space <= z + 1; z_space++) {
b = world.getBlockAt(x_space, y, z_space);
b.setType(Material.GRAVEL);
}
}
b = world.getBlockAt(x - 2, y, z);
b.setType(Material.QUARTZ_ORE);
b = world.getBlockAt(x + 2, y, z);
b.setType(Material.QUARTZ_ORE);
b = world.getBlockAt(x, y, z - 2);
b.setType(Material.QUARTZ_ORE);
b = world.getBlockAt(x, y, z + 2);
b.setType(Material.QUARTZ_ORE);
y = islandHeight + 1;
b = world.getBlockAt(x - 1, y, z);
b.setType(Material.MAGMA);
b = world.getBlockAt(x + 1, y, z);
b.setType(Material.MAGMA);
b = world.getBlockAt(x, y, z - 1);
b.setType(Material.MAGMA);
b = world.getBlockAt(x, y, z + 1);
b.setType(Material.MAGMA);
// Place a helpful sign in front of player
placeSign(x, islandHeight + 5, z + 3);
// Place the chest - no need to use the safe spawn function
// because we know what this island looks like
placeChest(x, islandHeight + 5, z + 1);
}
private void generateEndBlocks() {
// Nether block
int x = island.getCenter().getBlockX();
int z = island.getCenter().getBlockZ();
int islandHeight = island.getCenter().getBlockY();
int y;
// Add some grass
for (y = islandHeight + 4; y < islandHeight + 5; y++) {
for (int x_space = x - 3; x_space <= x + 3; x_space++) {
for (int z_space = z - 3; z_space <= z + 3; z_space++) {
world.getBlockAt(x_space, y, z_space).setType(Material.END_BRICKS);
}
}
}
// Then cut off the corners to make it round-ish
for (int x_space = x - 3; x_space <= x + 3; x_space += 6) {
for (int z_space = z - 3; z_space <= z + 3; z_space += 6) {
world.getBlockAt(x_space, y-1, z_space).setType(Material.AIR);
}
}
// Place bedrock - MUST be there (ensures island are not
// overwritten
Block b = world.getBlockAt(x, islandHeight, z);
b.setType(Material.BEDROCK);
// Then add some more dirt in the classic shape
y = islandHeight + 3;
for (int x_space = x - 2; x_space <= x + 2; x_space++) {
for (int z_space = z - 2; z_space <= z + 2; z_space++) {
b = world.getBlockAt(x_space, y, z_space);
b.setType(Material.ENDER_STONE);
}
}
b = world.getBlockAt(x - 3, y, z);
b.setType(Material.OBSIDIAN);
b = world.getBlockAt(x + 3, y, z);
b.setType(Material.OBSIDIAN);
b = world.getBlockAt(x, y, z - 3);
b.setType(Material.OBSIDIAN);
b = world.getBlockAt(x, y, z + 3);
b.setType(Material.OBSIDIAN);
y = islandHeight + 2;
for (int x_space = x - 1; x_space <= x + 1; x_space++) {
for (int z_space = z - 1; z_space <= z + 1; z_space++) {
b = world.getBlockAt(x_space, y, z_space);
b.setType(Material.ENDER_STONE);
}
}
b = world.getBlockAt(x - 2, y, z);
b.setType(Material.ENDER_STONE);
b = world.getBlockAt(x + 2, y, z);
b.setType(Material.ENDER_STONE);
b = world.getBlockAt(x, y, z - 2);
b.setType(Material.ENDER_STONE);
b = world.getBlockAt(x, y, z + 2);
b.setType(Material.ENDER_STONE);
y = islandHeight + 1;
b = world.getBlockAt(x - 1, y, z);
b.setType(Material.ENDER_STONE);
b = world.getBlockAt(x + 1, y, z);
b.setType(Material.ENDER_STONE);
b = world.getBlockAt(x, y, z - 1);
b.setType(Material.ENDER_STONE);
b = world.getBlockAt(x, y, z + 1);
b.setType(Material.ENDER_STONE);
// Add island items
y = islandHeight;
// Spawn an ender crystal
world.spawnEntity(new Location(world, x, y + 5D, z), EntityType.ENDER_CRYSTAL);
// Place a helpful sign in front of player
placeSign(x, islandHeight + 5, z + 3);
// Place the chest - no need to use the safe spawn function
// because we know what this island looks like
placeChest(x, islandHeight + 5, z + 1);
}
private void placeSign(int x, int y, int z) {
Block blockToChange = world.getBlockAt(x, y, z);
blockToChange.setType(Material.SIGN_POST);
if (playerUUID != null) {
Sign sign = (Sign) blockToChange.getState();
User user = User.getInstance(playerUUID);
// Sets the lines of the sign
sign.setLine(0, user.getTranslation("new-island.sign.line0", TextVariables.NAME, playerName));
sign.setLine(1, user.getTranslation("new-island.sign.line1", TextVariables.NAME, playerName));
sign.setLine(2, user.getTranslation("new-island.sign.line2", TextVariables.NAME, playerName));
sign.setLine(3, user.getTranslation("new-island.sign.line3", TextVariables.NAME, playerName));
((org.bukkit.material.Sign) sign.getData()).setFacingDirection(BlockFace.NORTH);
sign.update();
}
}
private void placeChest(int x, int y, int z) {
// Fill the chest and orient it correctly
Block blockToChange = world.getBlockAt(x, y, z);
blockToChange.setType(Material.CHEST);
BlockState state = blockToChange.getState();
Chest chest = new Chest(BlockFace.SOUTH);
state.setData(chest);
state.update();
if (!chestItems.isEmpty()) {
InventoryHolder chestBlock = (InventoryHolder) state;
for (ItemStack item: chestItems) {
chestBlock.getInventory().addItem(item);
}
}
public Optional<Location> getSpawnPoint() {
return Optional.ofNullable(spawnPoint);
}
}

View File

@ -1,196 +0,0 @@
package us.tastybento.bskyblock.island.builders;
import java.io.File;
import java.io.IOException;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Chest;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.localization.TextVariables;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
/**
* Generates islands
*
* @author tastybento
* @since 1.0
*/
public class IslandBuilderNew {
public enum IslandType {
ISLAND,
NETHER,
END
}
private Island island;
private World world;
private IslandType type = IslandType.ISLAND;
private List<ItemStack> chestItems;
private UUID playerUUID;
private String playerName;
private BSkyBlock plugin;
private Map<IslandType, Clipboard> islandSchems = new EnumMap<>(IslandType.class);
//TODO support companions?
public IslandBuilderNew(BSkyBlock plugin, Island island) {
this.plugin = plugin;
this.island = island;
world = island.getWorld();
loadIslands();
}
private void loadIslands() {
File schems = new File(plugin.getDataFolder(), "schems");
if (!schems.exists()) {
if (!schems.mkdirs()) {
plugin.logError("Could not make schems folder!");
} else {
copySchems(schems);
}
}
try {
Clipboard cb = new Clipboard(plugin);
cb.load("island");
islandSchems.put(IslandType.ISLAND, cb);
} catch (IOException | InvalidConfigurationException e) {
plugin.logError("Could not load default island");
}
if (plugin.getSettings().isNetherGenerate() && plugin.getSettings().isNetherIslands()) {
try {
Clipboard cbn = new Clipboard(plugin);
cbn.load("nether-island");
islandSchems.put(IslandType.NETHER, cbn);
} catch (IOException | InvalidConfigurationException e) {
plugin.logError("Could not load default nether island");
}
}
if (plugin.getSettings().isEndGenerate() && plugin.getSettings().isEndIslands()) {
try {
Clipboard cbe = new Clipboard(plugin);
cbe.load("end-island");
islandSchems.put(IslandType.END, cbe);
} catch (IOException | InvalidConfigurationException e) {
plugin.logError("Could not load default end island");
}
}
plugin.log("Loaded " + islandSchems.size() + " islands");
}
/**
* Copies schems from the jar file
* @param schems2 - file containing schem
*/
private void copySchems(File schems2) {
plugin.saveResource("schems/island.schem", false);
plugin.saveResource("schems/nether-island.schem", false);
plugin.saveResource("schems/end-island.schem", false);
}
/**
* @param type the type to set
*/
public IslandBuilderNew setType(IslandType type) {
this.type = type;
return this;
}
/**
* @param player - the player the player to set
*/
public IslandBuilderNew setPlayer(Player player) {
playerUUID = player.getUniqueId();
playerName = player.getName();
return this;
}
/**
* @param list the default chestItems to set
*/
public IslandBuilderNew setChestItems(List<ItemStack> list) {
chestItems = list;
return this;
}
public void build() {
plugin.log("Pasting island to " + type);
Location loc = island.getCenter();
// Switch on island type
switch (type) {
case NETHER:
world = Bukkit.getWorld(island.getWorld().getName() + "_nether");
if (world == null) {
return;
}
loc = island.getCenter().toVector().toLocation(world);
break;
case END:
world = Bukkit.getWorld(island.getWorld().getName() + "_the_end");
if (world == null) {
return;
}
loc = island.getCenter().toVector().toLocation(world);
break;
default:
break;
}
plugin.log("Pasting island to " + loc);
islandSchems.get(type).paste(loc);
// Do other stuff
}
private void placeSign(int x, int y, int z) {
Block blockToChange = world.getBlockAt(x, y, z);
blockToChange.setType(Material.SIGN_POST);
if (playerUUID != null) {
Sign sign = (Sign) blockToChange.getState();
User user = User.getInstance(playerUUID);
// Sets the lines of the sign
sign.setLine(0, user.getTranslation("new-island.sign.line0", TextVariables.NAME, playerName));
sign.setLine(1, user.getTranslation("new-island.sign.line1", TextVariables.NAME, playerName));
sign.setLine(2, user.getTranslation("new-island.sign.line2", TextVariables.NAME, playerName));
sign.setLine(3, user.getTranslation("new-island.sign.line3", TextVariables.NAME, playerName));
((org.bukkit.material.Sign) sign.getData()).setFacingDirection(BlockFace.NORTH);
sign.update();
}
}
private void placeChest(int x, int y, int z) {
// Fill the chest and orient it correctly
Block blockToChange = world.getBlockAt(x, y, z);
blockToChange.setType(Material.CHEST);
BlockState state = blockToChange.getState();
Chest chest = new Chest(BlockFace.SOUTH);
state.setData(chest);
state.update();
if (!chestItems.isEmpty()) {
InventoryHolder chestBlock = (InventoryHolder) state;
for (ItemStack item: chestItems) {
chestBlock.getInventory().addItem(item);
}
}
}
}

View File

@ -348,6 +348,7 @@ public class IslandsManager {
public Location getSafeHomeLocation(World world, User user, int number) {
// Try the numbered home location first
Location l = plugin.getPlayers().getHomeLocation(world, user, number);
if (l == null) {
// Get the default home, which may be null too, but that's okay
number = 1;
@ -433,7 +434,7 @@ public class IslandsManager {
* @return the spawnPoint or null if spawn does not exist
*/
public Location getSpawnPoint(World world) {
return spawn.containsKey(world) ? spawn.get(world).getSpawnPoint() : null;
return spawn.containsKey(world) ? spawn.get(world).getSpawnPoint(world.getEnvironment()) : null;
}
/**
@ -480,7 +481,7 @@ public class IslandsManager {
/**
* Teleport player to a home location. If one cannot be found a search is done to
* find a safe place.
*
*
* @param world - world to check
* @param player - the player
* @param number - a number - home location to do to
@ -504,7 +505,7 @@ public class IslandsManager {
/**
* Teleport player to a home location. If one cannot be found a search is done to
* find a safe place.
*
*
* @param world - world to check
* @param player - the player
* @param number - a number - home location to do to
@ -548,7 +549,7 @@ public class IslandsManager {
// If this is a new island, then run commands and do resets
if (newIsland) {
// TODO add command running
// Remove money inventory etc.
if (plugin.getIWM().isOnJoinResetEnderChest(world)) {
user.getPlayer().getEnderChest().clear();
@ -723,7 +724,7 @@ public class IslandsManager {
// Move player to spawn
if (spawn.containsKey(island.getWorld())) {
// go to island spawn
player.teleport(spawn.get(island.getWorld()).getSpawnPoint());
player.teleport(spawn.get(island.getWorld()).getSpawnPoint(island.getWorld().getEnvironment()));
} else {
if (!player.performCommand(Constants.SPAWNCOMMAND)) {
plugin.logWarning("During island deletion player " + player.getName() + " could not be sent to spawn so was dropped, sorry.");

View File

@ -4,6 +4,7 @@ import java.io.IOException;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.events.IslandBaseEvent;
@ -11,7 +12,7 @@ import us.tastybento.bskyblock.api.events.island.IslandEvent;
import us.tastybento.bskyblock.api.events.island.IslandEvent.Reason;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.island.builders.IslandBuilderNew;
import us.tastybento.bskyblock.island.builders.IslandBuilder;
/**
* Create and paste a new island
@ -117,40 +118,67 @@ public class NewIsland {
.island(island)
.location(island.getCenter())
.build();
if (!event.isCancelled()) {
// Create island
IslandBuilderNew ib = new IslandBuilderNew(plugin, island)
.setPlayer(user.getPlayer())
.setChestItems(plugin.getSettings().getChestItems())
.setType(IslandBuilderNew.IslandType.ISLAND);
ib.build();
if (plugin.getSettings().isNetherGenerate() && plugin.getSettings().isNetherIslands() && plugin.getIWM().getNetherWorld() != null) {
ib.setType(IslandBuilderNew.IslandType.NETHER).build();
}
if (plugin.getSettings().isEndGenerate() && plugin.getSettings().isEndIslands() && plugin.getIWM().getEndWorld() != null) {
ib.setType(IslandBuilderNew.IslandType.END).build();
}
// Teleport player to their island
plugin.getIslands().homeTeleport(world, user.getPlayer(), true);
// Fire exit event
Reason reasonDone = Reason.CREATED;
switch (reason) {
case CREATE:
reasonDone = Reason.CREATED;
break;
case RESET:
reasonDone = Reason.RESETTED;
break;
default:
break;
}
IslandEvent.builder()
.involvedPlayer(user.getUniqueId())
.reason(reasonDone)
.island(island)
.location(island.getCenter())
.build();
if (event.isCancelled()) {
return;
}
// Create island
IslandBuilder ib = new IslandBuilder(plugin, island)
.setPlayer(user.getPlayer())
.setType(Environment.NORMAL);
ib.run(() -> {
// Set initial spawn point if one exists
ib.getSpawnPoint().ifPresent(l -> {
plugin.getPlayers().setHomeLocation(user, l, 1);
island.setSpawnPoint(Environment.NORMAL, l);
});
// Teleport player after this island is built
plugin.getIslands().homeTeleport(world, user.getPlayer(), true);
});
// Build it
ib.build();
// Make nether island
if (plugin.getSettings().isNetherGenerate() && plugin.getSettings().isNetherIslands() && plugin.getIWM().getNetherWorld() != null) {
IslandBuilder ib_nether = new IslandBuilder(plugin, island)
.setPlayer(user.getPlayer())
.setType(Environment.NETHER);
ib_nether.run(() -> ib_nether.getSpawnPoint().ifPresent(l -> island.setSpawnPoint(Environment.NETHER, l)));
// Build it
ib_nether.build();
}
// Make end island
if (plugin.getSettings().isEndGenerate() && plugin.getSettings().isEndIslands() && plugin.getIWM().getEndWorld() != null) {
IslandBuilder ib_end = new IslandBuilder(plugin, island)
.setPlayer(user.getPlayer())
.setType(Environment.THE_END);
ib_end.run(() -> {
// Set initial spawn point if one exists
ib_end.getSpawnPoint().ifPresent(l -> island.setSpawnPoint(Environment.NETHER, l));
});
// Build it
ib_end.build();
}
// Fire exit event
Reason reasonDone = Reason.CREATED;
switch (reason) {
case CREATE:
reasonDone = Reason.CREATED;
break;
case RESET:
reasonDone = Reason.RESETTED;
break;
default:
break;
}
IslandEvent.builder()
.involvedPlayer(user.getUniqueId())
.reason(reasonDone)
.island(island)
.location(island.getCenter())
.build();
}
/**

View File

@ -13,6 +13,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.util.Vector;
@ -243,5 +244,49 @@ public class Util {
return world.getEnvironment().equals(Environment.NORMAL) ? world : Bukkit.getWorld(world.getName().replaceAll(NETHER, "").replaceAll(THE_END, ""));
}
/**
* Converts block face direction to radial degrees. Returns 0 if block face
* is not radial.
*
* @param face
* @return degrees
*/
public static float blockFaceToFloat(BlockFace face) {
switch (face) {
case EAST:
return 90F;
case EAST_NORTH_EAST:
return 67.5F;
case EAST_SOUTH_EAST:
return 0F;
case NORTH:
return 0F;
case NORTH_EAST:
return 45F;
case NORTH_NORTH_EAST:
return 22.5F;
case NORTH_NORTH_WEST:
return 337.5F;
case NORTH_WEST:
return 315F;
case SOUTH:
return 180F;
case SOUTH_EAST:
return 135F;
case SOUTH_SOUTH_EAST:
return 157.5F;
case SOUTH_SOUTH_WEST:
return 202.5F;
case SOUTH_WEST:
return 225F;
case WEST:
return 270F;
case WEST_NORTH_WEST:
return 292.5F;
case WEST_SOUTH_WEST:
return 247.5F;
default:
return 0F;
}
}
}

View File

@ -16,6 +16,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.plugin.PluginManager;
import org.junit.BeforeClass;
@ -39,7 +40,7 @@ import us.tastybento.bskyblock.util.Util;
@RunWith(PowerMockRunner.class)
@PrepareForTest( { BSkyBlock.class, Util.class })
public class MySQLDatabaseHandlerTest {
private static MySQLDatabaseHandler<Island> handler;
private static Island instance;
private static String UNIQUE_ID = "xyz";
@ -66,14 +67,14 @@ public class MySQLDatabaseHandlerTest {
Bukkit.setServer(server);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
Settings settings = mock(Settings.class);
when(plugin.getSettings()).thenReturn(settings);
when(settings.getDeathsMax()).thenReturn(10);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
dbConn = mock(MySQLDatabaseConnecter.class);
Connection connection = mock(Connection.class);
@ -88,7 +89,7 @@ public class MySQLDatabaseHandlerTest {
instance = new Island();
instance.setUniqueId(UNIQUE_ID);
handler = new MySQLDatabaseHandler<>(plugin, Island.class, dbConn);
PowerMockito.mockStatic(Util.class);
when(Util.sameWorld(Mockito.any(), Mockito.any())).thenReturn(true);
@ -120,11 +121,11 @@ public class MySQLDatabaseHandlerTest {
players.setPlayerName("name");
players.setPlayerUUID(UUID.randomUUID());
players.setResetsLeft(3);
MySQLDatabaseHandler<Players> h = new MySQLDatabaseHandler<>(plugin, Players.class, dbConn);
h.saveObject(players);
Island island = new Island();
island.setUniqueId(UNIQUE_ID);
island.setCenter(location);
@ -149,12 +150,12 @@ public class MySQLDatabaseHandlerTest {
island.setPurgeProtected(true);
island.setRange(100);
island.setSpawn(true);
island.setSpawnPoint(location);
island.setSpawnPoint(Environment.NORMAL, location);
island.setWorld(world);
MySQLDatabaseHandler<Island> ih = new MySQLDatabaseHandler<>(plugin, Island.class, dbConn);
ih.saveObject(island);
}
}

View File

@ -600,7 +600,7 @@ public class IslandsManagerTest {
Island island = mock(Island.class);
when(island.getWorld()).thenReturn(world);
// Make a spawn position on the island
when(island.getSpawnPoint()).thenReturn(location);
when(island.getSpawnPoint(Mockito.any())).thenReturn(location);
// Set the spawn island
im.setSpawn(island);
assertEquals(location,im.getSpawnPoint(world));