Merge branch 'develop' of https://github.com/BentoBoxWorld/BentoBox.git into develop

This commit is contained in:
tastybento 2019-04-28 07:32:24 -07:00
commit 2ee03cd081
30 changed files with 855 additions and 817 deletions

View File

@ -25,13 +25,13 @@ public class Blueprint {
private List<String> description; //TODO
private World.Environment environment;
private Clipboard clipboard;
public Blueprint(@NonNull String name, @NonNull ZipFile zip) throws IOException {
this.name = name;
try (JsonReader reader = new Gson().newJsonReader(new InputStreamReader(zip.getInputStream(zip.getEntry("properties.json"))))) {
readProperties(reader);
}
//System.out.println(new Gson().toJson(this));
}
private void readProperties(@NonNull JsonReader reader) throws IOException {
@ -77,8 +77,4 @@ public class Blueprint {
public World.Environment getEnvironment() {
return environment;
}
public Clipboard getClipboard() {
return clipboard;
}
}

View File

@ -1,8 +0,0 @@
package world.bentobox.bentobox.api.blueprints;
/**
* @author tastybento, Poslovitch
* @since 1.5.0
*/
public class Clipboard {
}

View File

@ -1,12 +1,13 @@
package world.bentobox.bentobox.api.commands.admin.deaths;
import java.util.List;
import java.util.UUID;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import java.util.List;
import java.util.UUID;
/**
* @author Poslovitch
*/
@ -23,8 +24,8 @@ public class AdminDeathsResetCommand extends CompositeCommand {
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (args.isEmpty() || args.size() != 1) {
public boolean execute(User user, String label, @NonNull List<String> args) {
if (args.size() != 1) {
showHelp(this, user);
return false;
}
@ -35,7 +36,7 @@ public class AdminDeathsResetCommand extends CompositeCommand {
return false;
} else {
getPlayers().setDeaths(getWorld(), target, 0);
user.sendMessage("general.success");
user.sendMessage("commands.admin.deaths.reset.success", TextVariables.NAME, args.get(0));
return true;
}
}

View File

@ -1,14 +1,14 @@
package world.bentobox.bentobox.api.commands.admin.deaths;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.math.NumberUtils;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import java.util.List;
import java.util.UUID;
/**
* @author Poslovitch
*/
@ -25,8 +25,8 @@ public class AdminDeathsSetCommand extends CompositeCommand {
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (args.isEmpty() || args.size() != 2) {
public boolean execute(User user, String label, @NonNull List<String> args) {
if (args.size() != 2) {
showHelp(this, user);
return false;
}
@ -38,7 +38,7 @@ public class AdminDeathsSetCommand extends CompositeCommand {
user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(1));
} else {
getPlayers().setDeaths(getWorld(), target, Integer.valueOf(args.get(1)));
user.sendMessage("general.success");
user.sendMessage("commands.admin.deaths.set.success", TextVariables.NAME, args.get(0), TextVariables.NUMBER, args.get(1));
return true;
}

View File

@ -13,8 +13,8 @@ import org.bukkit.Particle;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.blueprints.Clipboard;
import world.bentobox.bentobox.managers.SchemsManager;
import world.bentobox.bentobox.schems.Clipboard;
public class AdminSchemCommand extends ConfirmableCommand {
// Clipboards

View File

@ -4,7 +4,7 @@ import java.util.List;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.blueprints.Clipboard;
public class AdminSchemCopyCommand extends CompositeCommand {
@ -27,7 +27,7 @@ public class AdminSchemCopyCommand extends CompositeCommand {
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
Clipboard clipboard = parent.getClipboards().computeIfAbsent(user.getUniqueId(), v -> new Clipboard());
boolean copyAir = (args.size() == 1 && args.get(0).equalsIgnoreCase("air"));
return clipboard.copy(user, copyAir);
}

View File

@ -6,7 +6,7 @@ import java.util.Optional;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.managers.ClipboardManager;
import world.bentobox.bentobox.util.Util;
public class AdminSchemLoadCommand extends CompositeCommand {
@ -30,9 +30,9 @@ public class AdminSchemLoadCommand extends CompositeCommand {
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
if (clipboard.load(user, args.get(0))) {
parent.getClipboards().put(user.getUniqueId(), clipboard);
ClipboardManager bp = new ClipboardManager(getPlugin(), parent.getSchemsFolder());
if (bp.load(user, args.get(0))) {
parent.getClipboards().put(user.getUniqueId(), bp.getClipboard());
return true;
}

View File

@ -8,7 +8,7 @@ import org.bukkit.block.Block;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.blueprints.Clipboard;
public class AdminSchemOriginCommand extends CompositeCommand {
@ -26,7 +26,7 @@ public class AdminSchemOriginCommand extends CompositeCommand {
public boolean execute(User user, String label, List<String> args) {
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
Clipboard clipboard = parent.getClipboards().computeIfAbsent(user.getUniqueId(), v -> new Clipboard());
if (clipboard.getPos1() == null || clipboard.getPos2() == null) {
user.sendMessage("commands.admin.schem.need-pos1-pos2");
return false;

View File

@ -4,7 +4,8 @@ import java.util.List;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.blueprints.Clipboard;
import world.bentobox.bentobox.blueprints.Paster;
public class AdminSchemPasteCommand extends CompositeCommand {
@ -21,11 +22,10 @@ public class AdminSchemPasteCommand extends CompositeCommand {
@Override
public boolean execute(User user, String label, List<String> args) {
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
Clipboard clipboard = parent.getClipboards().computeIfAbsent(user.getUniqueId(), v -> new Clipboard());
if (clipboard.isFull()) {
clipboard.pasteClipboard(user.getLocation());
user.sendMessage("general.success");
new Paster(getPlugin(), clipboard, user.getLocation(), () -> user.sendMessage("general.success"));
user.sendMessage("commands.admin.schem.paste.pasting");
return true;
}

View File

@ -4,7 +4,7 @@ import java.util.List;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.blueprints.Clipboard;
import world.bentobox.bentobox.util.Util;
public class AdminSchemPos1Command extends CompositeCommand {
@ -22,7 +22,7 @@ public class AdminSchemPos1Command extends CompositeCommand {
@Override
public boolean execute(User user, String label, List<String> args) {
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
Clipboard clipboard = parent.getClipboards().computeIfAbsent(user.getUniqueId(), v -> new Clipboard());
if (user.getLocation().equals(clipboard.getPos2())) {
user.sendMessage("commands.admin.schem.set-different-pos");

View File

@ -4,7 +4,7 @@ import java.util.List;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.blueprints.Clipboard;
import world.bentobox.bentobox.util.Util;
public class AdminSchemPos2Command extends CompositeCommand {
@ -22,7 +22,7 @@ public class AdminSchemPos2Command extends CompositeCommand {
@Override
public boolean execute(User user, String label, List<String> args) {
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
Clipboard clipboard = parent.getClipboards().computeIfAbsent(user.getUniqueId(), v -> new Clipboard());
if (user.getLocation().equals(clipboard.getPos1())) {
user.sendMessage("commands.admin.schem.set-different-pos");

View File

@ -5,7 +5,8 @@ import java.util.List;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.blueprints.Clipboard;
import world.bentobox.bentobox.managers.ClipboardManager;
public class AdminSchemSaveCommand extends ConfirmableCommand {
@ -27,7 +28,7 @@ public class AdminSchemSaveCommand extends ConfirmableCommand {
}
AdminSchemCommand parent = (AdminSchemCommand) getParent();
Clipboard clipboard = parent.getClipboards().getOrDefault(user.getUniqueId(), new Clipboard(getPlugin(), parent.getSchemsFolder()));
Clipboard clipboard = parent.getClipboards().computeIfAbsent(user.getUniqueId(), v -> new Clipboard());
if (clipboard.isFull()) {
// Check if file exists
@ -35,12 +36,12 @@ public class AdminSchemSaveCommand extends ConfirmableCommand {
if (newFile.exists()) {
this.askConfirmation(user, user.getTranslation("commands.admin.schem.file-exists"), () -> {
parent.hideClipboard(user);
clipboard.save(user, args.get(0));
new ClipboardManager(getPlugin(), parent.getSchemsFolder(), clipboard).save(user, args.get(0));
});
return false;
} else {
parent.hideClipboard(user);
return clipboard.save(user, args.get(0));
return new ClipboardManager(getPlugin(), parent.getSchemsFolder(), clipboard).save(user, args.get(0));
}
} else {
user.sendMessage("commands.admin.schem.copy-first");

View File

@ -1,10 +1,6 @@
package world.bentobox.bentobox.api.commands.admin.team;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.team.TeamEvent;
@ -12,6 +8,9 @@ import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import java.util.List;
import java.util.UUID;
public class AdminTeamAddCommand extends CompositeCommand {
public AdminTeamAddCommand(CompositeCommand parent) {
@ -68,7 +67,7 @@ public class AdminTeamAddCommand extends CompositeCommand {
Island teamIsland = getIslands().getIsland(getWorld(), ownerUUID);
if (teamIsland != null) {
getIslands().setJoinTeam(teamIsland, targetUUID);
user.sendMessage("general.success");
user.sendMessage("commands.admin.team.add.success", TextVariables.NAME, target.getName(), "[owner]", owner.getName());
IslandBaseEvent event = TeamEvent.builder()
.island(teamIsland)
.reason(TeamEvent.Reason.JOINED)
@ -81,8 +80,5 @@ public class AdminTeamAddCommand extends CompositeCommand {
user.sendMessage("general.errors.player-has-no-island");
return false;
}
}
}

View File

@ -1,10 +1,6 @@
package world.bentobox.bentobox.api.commands.admin.team;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.team.TeamEvent;
@ -12,6 +8,9 @@ import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import java.util.List;
import java.util.UUID;
public class AdminTeamDisbandCommand extends CompositeCommand {
public AdminTeamDisbandCommand(CompositeCommand parent) {
@ -66,7 +65,7 @@ public class AdminTeamDisbandCommand extends CompositeCommand {
Bukkit.getServer().getPluginManager().callEvent(event);
}
});
user.sendMessage("general.success");
user.sendMessage("commands.admin.team.disband.success", TextVariables.NAME, args.get(0));
return true;
}
}

View File

@ -1,10 +1,7 @@
package world.bentobox.bentobox.api.commands.admin.team;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.team.TeamEvent;
@ -12,11 +9,17 @@ import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import java.util.List;
import java.util.UUID;
/**
* Kicks the specified player from the island team.
* @author tastybento
*/
public class AdminTeamKickCommand extends CompositeCommand {
public AdminTeamKickCommand(CompositeCommand parent) {
super(parent, "kick");
}
@Override
@ -49,18 +52,23 @@ public class AdminTeamKickCommand extends CompositeCommand {
}
@Override
public boolean execute(User user, String label, List<String> args) {
public boolean execute(User user, String label, @NonNull List<String> args) {
UUID targetUUID = getPlayers().getUUID(args.get(0));
if (targetUUID.equals(getIslands().getOwner(getWorld(), targetUUID))) {
Island island = getIslands().getIsland(getWorld(), targetUUID);
if (targetUUID.equals(island.getOwner())) {
user.sendMessage("commands.admin.team.kick.cannot-kick-owner");
getIslands().getIsland(getWorld(), targetUUID).showMembers(user);
island.showMembers(user);
return false;
}
User.getInstance(targetUUID).sendMessage("commands.admin.team.kick.admin-kicked");
User target = User.getInstance(targetUUID);
target.sendMessage("commands.admin.team.kick.admin-kicked");
getIslands().removePlayer(getWorld(), targetUUID);
user.sendMessage("general.success");
user.sendMessage("commands.admin.team.kick.success", TextVariables.NAME, target.getName(), "[owner]", getPlayers().getName(island.getOwner()));
// Fire event so add-ons know
Island island = getIslands().getIsland(getWorld(), targetUUID);
IslandBaseEvent event = TeamEvent.builder()
.island(island)
.reason(TeamEvent.Reason.KICK)

View File

@ -1,10 +1,6 @@
package world.bentobox.bentobox.api.commands.admin.team;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.team.TeamEvent;
@ -12,6 +8,13 @@ import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import java.util.List;
import java.util.UUID;
/**
* Sets the owner of an island.
* @author tastybento
*/
public class AdminTeamSetownerCommand extends CompositeCommand {
public AdminTeamSetownerCommand(CompositeCommand parent) {
@ -43,12 +46,12 @@ public class AdminTeamSetownerCommand extends CompositeCommand {
return false;
}
if (getIslands().getOwner(getWorld(), targetUUID).equals(targetUUID)) {
user.sendMessage("commands.admin.team.setowner.already-owner");
user.sendMessage("commands.admin.team.setowner.already-owner", TextVariables.NAME, args.get(0));
return false;
}
// Make new owner
getIslands().setOwner(getWorld(), user, targetUUID);
user.sendMessage("general.success");
user.sendMessage("commands.admin.team.setowner.success", TextVariables.NAME, args.get(0));
// Fire event so add-ons know
Island island = getIslands().getIsland(getWorld(), targetUUID);
IslandBaseEvent event = TeamEvent.builder()

View File

@ -1,10 +1,6 @@
package world.bentobox.bentobox.api.commands.island.team;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
@ -13,6 +9,9 @@ import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import java.util.List;
import java.util.UUID;
public class IslandTeamKickCommand extends ConfirmableCommand {
@ -94,7 +93,7 @@ public class IslandTeamKickCommand extends ConfirmableCommand {
if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) {
getPlugin().getVault().ifPresent(vault -> vault.withdraw(target, vault.getBalance(target)));
}
user.sendMessage("general.success");
user.sendMessage("commands.island.team.kick.success", TextVariables.NAME, target.getName());
// Fire event
IslandBaseEvent e = TeamEvent.builder()
.island(oldIsland)

View File

@ -0,0 +1,301 @@
package world.bentobox.bentobox.blueprints;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.Sign;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.ChestedHorse;
import org.bukkit.entity.Horse;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Attachable;
import org.bukkit.material.Colorable;
import org.bukkit.util.BoundingBox;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author tastybento
* @since 1.5.0
*/
public class Clipboard {
// Commonly used texts along this class.
private static final String ATTACHED_YAML_PREFIX = "attached.";
private static final String ENTITIES_YAML_PREFIX = "entities.";
private static final String BLOCKS_YAML_PREFIX = "blocks.";
private static final String BEDROCK = "bedrock";
private static final String COLOR = "color";
private static final String LINES = "lines";
private @Nullable YamlConfiguration blockConfig;
private @Nullable Location pos1;
private @Nullable Location pos2;
private @Nullable Location origin;
public Clipboard(String contents) throws InvalidConfigurationException {
set(contents);
}
public Clipboard(@NonNull YamlConfiguration config) {
this.blockConfig = config;
}
public Clipboard() {
super();
}
/**
* Copy the blocks between pos1 and pos2 into the clipboard.
* This will erase any previously registered data from the clipboard.
* @param user - user
* @return true if successful, false if pos1 or pos2 are undefined.
*/
public boolean copy(User user, boolean copyAir) {
if (pos1 == null || pos2 == null) {
user.sendMessage("commands.admin.schem.need-pos1-pos2");
return false;
}
// World
World world = pos1.getWorld();
// Clear the clipboard
blockConfig = new YamlConfiguration();
int count = 0;
BoundingBox toCopy = BoundingBox.of(pos1, pos2);
for (int x = (int)toCopy.getMinX(); x <= toCopy.getMaxX(); x++) {
for (int y = (int)toCopy.getMinY(); y <= toCopy.getMaxY(); y++) {
for (int z = (int)toCopy.getMinZ(); z <= toCopy.getMaxZ(); z++) {
Block block = world.getBlockAt(x, y, z);
if (copyBlock(block, origin == null ? user.getLocation() : origin, copyAir, world.getLivingEntities().stream()
.filter(Objects::nonNull)
.filter(e -> !(e instanceof Player) && e.getLocation().getBlock().equals(block))
.collect(Collectors.toList()))) {
count ++;
}
}
}
}
blockConfig.set("size.xsize", toCopy.getWidthX());
blockConfig.set("size.ysize", toCopy.getHeight());
blockConfig.set("size.zsize", toCopy.getWidthZ());
user.sendMessage("commands.admin.schem.copied-blocks", TextVariables.NUMBER, String.valueOf(count));
return true;
}
private boolean copyBlock(Block block, Location copyOrigin, boolean copyAir, Collection<LivingEntity> entities) {
if (!copyAir && block.getType().equals(Material.AIR) && entities.isEmpty()) {
return false;
}
// Create position
int x = block.getLocation().getBlockX() - copyOrigin.getBlockX();
int y = block.getLocation().getBlockY() - copyOrigin.getBlockY();
int z = block.getLocation().getBlockZ() - copyOrigin.getBlockZ();
String pos = x + "," + y + "," + z;
// Position defines the section
ConfigurationSection blocksSection = blockConfig.createSection(BLOCKS_YAML_PREFIX + "." + pos);
// Set entities
for (LivingEntity entity: entities) {
ConfigurationSection entitySection = blockConfig.createSection(ENTITIES_YAML_PREFIX + pos + "." + entity.getUniqueId());
entitySection.set("type", entity.getType().name());
entitySection.set("name", entity.getCustomName());
if (entity instanceof Colorable) {
Colorable c = (Colorable)entity;
if (c.getColor() != null) {
entitySection.set(COLOR, c.getColor().name());
}
}
if (entity instanceof Tameable && ((Tameable)entity).isTamed()) {
entitySection.set("tamed", true);
}
if (entity instanceof ChestedHorse && ((ChestedHorse)entity).isCarryingChest()) {
entitySection.set("chest", true);
}
if (entity instanceof Ageable) {
entitySection.set("adult", ((Ageable)entity).isAdult());
}
if (entity instanceof AbstractHorse) {
AbstractHorse horse = (AbstractHorse)entity;
entitySection.set("domestication", horse.getDomestication());
for (int index = 0; index < horse.getInventory().getSize(); index++) {
ItemStack i = horse.getInventory().getItem(index);
if (i != null) {
entitySection.set("inventory." + index, i);
}
}
}
if (entity instanceof Horse) {
Horse horse = (Horse)entity;
entitySection.set("style", horse.getStyle().name());
}
}
// Return if this is just air block
if (!copyAir && block.getType().equals(Material.AIR) && !entities.isEmpty()) {
return true;
}
// Block state
BlockState blockState = block.getState();
// Set block data
if (blockState.getData() instanceof Attachable) {
ConfigurationSection attachedSection = blockConfig.createSection(ATTACHED_YAML_PREFIX + pos);
attachedSection.set("bd", block.getBlockData().getAsString());
// Placeholder for attachment
blocksSection.set("bd", "minecraft:air");
// Signs
if (blockState instanceof Sign) {
Sign sign = (Sign)blockState;
attachedSection.set(LINES, Arrays.asList(sign.getLines()));
}
return true;
} else {
blocksSection.set("bd", block.getBlockData().getAsString());
// Signs
if (blockState instanceof Sign) {
Sign sign = (Sign)blockState;
blocksSection.set(LINES, Arrays.asList(sign.getLines()));
}
}
if (block.getType().equals(Material.BEDROCK)) {
blockConfig.set(BEDROCK, x + "," + y + "," + z);
}
// Chests
if (blockState instanceof InventoryHolder) {
InventoryHolder ih = (InventoryHolder)blockState;
for (int index = 0; index < ih.getInventory().getSize(); index++) {
ItemStack i = ih.getInventory().getItem(index);
if (i != null) {
blocksSection.set("inventory." + index, i);
}
}
}
if (blockState instanceof CreatureSpawner) {
CreatureSpawner spawner = (CreatureSpawner)blockState;
blocksSection.set("spawnedType",spawner.getSpawnedType().name());
blocksSection.set("delay", spawner.getDelay());
blocksSection.set("maxNearbyEntities", spawner.getMaxNearbyEntities());
blocksSection.set("maxSpawnDelay", spawner.getMaxSpawnDelay());
blocksSection.set("minSpawnDelay", spawner.getMinSpawnDelay());
blocksSection.set("requiredPlayerRange", spawner.getRequiredPlayerRange());
blocksSection.set("spawnRange", spawner.getSpawnRange());
}
return true;
}
/**
* @return the blockConfig
*/
@Nullable
public YamlConfiguration getBlockConfig() {
return blockConfig;
}
/**
* @return the origin
*/
@Nullable
public Location getOrigin() {
return origin;
}
/**
* @return the pos1
*/
@Nullable
public Location getPos1() {
return pos1;
}
/**
* @return the pos2
*/
@Nullable
public Location getPos2() {
return pos2;
}
public boolean isFull() {
return blockConfig != null;
}
/**
* Set the clipboard from a YAML string
* @param contents
* @return
* @throws InvalidConfigurationException
*/
public Clipboard set(String contents) throws InvalidConfigurationException {
this.blockConfig.loadFromString(contents);
setPos1(null);
setPos2(null);
return this;
}
/**
* Set the clipboard contents from a YAML configuration
* @param blockConfig the blockConfig
*/
public Clipboard set(@NonNull YamlConfiguration blockConfig) {
this.blockConfig = blockConfig;
setPos1(null);
setPos2(null);
return this;
}
/**
* @param origin the origin to set
*/
public void setOrigin(@Nullable Location origin) {
this.origin = origin;
}
/**
* @param pos1 the pos1 to set
*/
public void setPos1(@Nullable Location pos1) {
origin = null;
this.pos1 = pos1;
}
/**
* @param pos2 the pos2 to set
*/
public void setPos2(@Nullable Location pos2) {
origin = null;
this.pos2 = pos2;
}
/**
* Returns the clipboard as a String using {@link YamlConfiguration#saveToString()}.
* @return the clipboard as a String.
*/
@Override
public String toString() {
return blockConfig.saveToString();
}
}

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.schems;
package world.bentobox.bentobox.blueprints;
import java.util.Iterator;
import java.util.List;
@ -14,7 +14,6 @@ import org.bukkit.block.BlockState;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.Sign;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.ChestedHorse;
@ -28,6 +27,8 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Colorable;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.localization.TextVariables;
@ -50,48 +51,78 @@ public class Paster {
CANCEL
}
private BentoBox plugin;
// The minimum block position (x,y,z)
private Location pos1;
// The maximum block position (x,y,z)
private Location pos2;
// Speed of pasting
private int pasteSpeed;
private PasteState pasteState;
private BukkitTask pastingTask;
private static final String BEDROCK = "bedrock";
// Commonly used texts along this class.
private static final String ATTACHED_YAML_PREFIX = "attached.";
private static final String ENTITIES_YAML_PREFIX = "entities.";
private static final String BLOCKS_YAML_PREFIX = "blocks.";
private static final String INVENTORY = "inventory";
private static final String ENTITY = "entity";
private static final String COLOR = "color";
private static final String LINES = "lines";
private BentoBox plugin;
// The minimum block position (x,y,z)
private Location pos1;
// The maximum block position (x,y,z)
private Location pos2;
// Speed of pasting
private int pasteSpeed;
private PasteState pasteState;
private BukkitTask pastingTask;
/**
* Pastes a schem
* @param plugin - BentoBox instance
* @param clipboard - the clipboard that called this paster
* @param blockConfig - the schem Yaml File
* @param world - the world to paste into
* @param island - the island object
* @param loc - the location to paste to
* @param task - the task the run after pasting
* Paste a clipboard
* @param plugin - BentoBox
* @param clipboard - clipboard to paste
* @param location - location to paste to
*/
public Paster(final BentoBox plugin, Clipboard clipboard, final YamlConfiguration blockConfig, final World world, final Island island, final Location loc, final Runnable task) {
public Paster(@NonNull BentoBox plugin, @NonNull Clipboard clipboard, @NonNull Location location) {
this.plugin = plugin;
if (!blockConfig.contains(BLOCKS_YAML_PREFIX)) {
plugin.logError("Clipboard has no block data in it to paste!");
return;
paste(location.getWorld(), null, location, clipboard, null);
}
/**
* Paste a clipboard
* @param plugin - BentoBox
* @param clipboard - clipboard to paste
* @param location - location to paste to
* @param task - task to run after pasting
*/
public Paster(@NonNull BentoBox plugin, @NonNull Clipboard clipboard, @NonNull Location location, @Nullable Runnable task) {
this.plugin = plugin;
paste(location.getWorld(), null, location, clipboard, task);
}
/**
* Pastes a clipboard
* @param plugin - BentoBox
* @param clipboard - clipboard to paste
* @param world - world to paste to
* @param island - island related to this paste
* @param task - task to run after pasting
*/
public Paster(@NonNull BentoBox plugin, @NonNull Clipboard clipboard, @NonNull World world, @NonNull Island island, @Nullable Runnable task) {
this.plugin = plugin;
// Offset due to bedrock
Vector off = new Vector(0,0,0);
if (clipboard.getBlockConfig().contains(BEDROCK)) {
String[] offset = clipboard.getBlockConfig().getString(BEDROCK).split(",");
off = new Vector(Integer.valueOf(offset[0]), Integer.valueOf(offset[1]), Integer.valueOf(offset[2]));
}
// Calculate location for pasting
Location loc = island.getCenter().toVector().subtract(off).toLocation(world);
// Paste
paste(world, island, loc, clipboard, task);
}
private void paste(@NonNull World world, @Nullable Island island, @NonNull Location loc, @NonNull Clipboard clipboard, @Nullable Runnable task) {
// Iterators for the various schem sections
Iterator<String> it = blockConfig.getConfigurationSection(BLOCKS_YAML_PREFIX).getKeys(false).iterator();
Iterator<String> it2 = blockConfig.contains(ATTACHED_YAML_PREFIX) ? blockConfig.getConfigurationSection(ATTACHED_YAML_PREFIX).getKeys(false).iterator() : null;
Iterator<String> it3 = blockConfig.contains(ENTITIES_YAML_PREFIX) ? blockConfig.getConfigurationSection(ENTITIES_YAML_PREFIX).getKeys(false).iterator() : null;
Iterator<String> it = clipboard.getBlockConfig().getConfigurationSection(BLOCKS_YAML_PREFIX).getKeys(false).iterator();
Iterator<String> it2 = clipboard.getBlockConfig().contains(ATTACHED_YAML_PREFIX) ? clipboard.getBlockConfig().getConfigurationSection(ATTACHED_YAML_PREFIX).getKeys(false).iterator() : null;
Iterator<String> it3 = clipboard.getBlockConfig().contains(ENTITIES_YAML_PREFIX) ? clipboard.getBlockConfig().getConfigurationSection(ENTITIES_YAML_PREFIX).getKeys(false).iterator() : null;
// Initial state & speed
pasteState = PasteState.BLOCKS;
@ -100,15 +131,15 @@ public class Paster {
pastingTask = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
int count = 0;
while (pasteState.equals(PasteState.BLOCKS) && count < pasteSpeed && it.hasNext()) {
pasteBlock(world, island, loc, blockConfig.getConfigurationSection(BLOCKS_YAML_PREFIX + it.next()));
pasteBlock(world, island, loc, clipboard.getBlockConfig().getConfigurationSection(BLOCKS_YAML_PREFIX + it.next()));
count++;
}
while (it2 != null && pasteState.equals(PasteState.ATTACHMENTS) && count < pasteSpeed && it2.hasNext()) {
pasteBlock(world, island, loc, blockConfig.getConfigurationSection(ATTACHED_YAML_PREFIX + it2.next()));
pasteBlock(world, island, loc, clipboard.getBlockConfig().getConfigurationSection(ATTACHED_YAML_PREFIX + it2.next()));
count++;
}
while (it3 != null && pasteState.equals(PasteState.ENTITIES) && count < pasteSpeed && it3.hasNext()) {
pasteEntity(world, loc, blockConfig.getConfigurationSection(ENTITIES_YAML_PREFIX + it3.next()));
pasteEntity(world, loc, clipboard.getBlockConfig().getConfigurationSection(ENTITIES_YAML_PREFIX + it3.next()));
count++;
}
// STATE SHIFT
@ -168,40 +199,6 @@ public class Paster {
updatePos(world, x,y,z);
}
/**
* Tracks the minimum and maximum block positions
* @param world - world
* @param x - x
* @param y - y
* @param z - z
*/
private void updatePos(World world, int x, int y, int z) {
if (pos1 == null) {
pos1 = new Location(world, x, y, z);
}
if (pos2 == null) {
pos2 = new Location(world, x, y, z);
}
if (x < pos1.getBlockX()) {
pos1.setX(x);
}
if (x > pos2.getBlockX()) {
pos2.setX(x);
}
if (y < pos1.getBlockY()) {
pos1.setY(y);
}
if (y > pos2.getBlockY()) {
pos2.setY(y);
}
if (z < pos1.getBlockZ()) {
pos1.setZ(z);
}
if (z > pos2.getBlockZ()) {
pos2.setZ(z);
}
}
private void pasteEntity(World world, Location location, ConfigurationSection config) {
String[] pos = config.getName().split(",");
int x = location.getBlockX() + Integer.valueOf(pos[0]);
@ -217,6 +214,47 @@ public class Paster {
setBlockState(island, block, config);
}
/**
* Handles signs, chests and mob spawner blocks
* @param island - island
* @param block - block
* @param config - config
*/
private void setBlockState(Island island, Block block, ConfigurationSection config) {
// Get the block state
BlockState bs = block.getState();
// Signs
if (bs instanceof Sign) {
List<String> lines = config.getStringList(LINES);
writeSign(island, block, lines);
}
// Chests, in general
if (bs instanceof InventoryHolder) {
bs.update(true, false);
Inventory ih = ((InventoryHolder)bs).getInventory();
if (config.isConfigurationSection(INVENTORY)) {
ConfigurationSection inv = config.getConfigurationSection(INVENTORY);
// Double chests are pasted as two blocks so inventory is filled twice. This code stops over filling for the first block.
inv.getKeys(false).stream()
.filter(i -> Integer.valueOf(i) < ih.getSize())
.forEach(i -> ih.setItem(Integer.valueOf(i), (ItemStack)inv.get(i)));
}
}
// Mob spawners
if (bs instanceof CreatureSpawner) {
CreatureSpawner spawner = ((CreatureSpawner) bs);
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(config.getInt("delay", -1));
spawner.setRequiredPlayerRange(config.getInt("requiredPlayerRange", 16));
spawner.setSpawnRange(config.getInt("spawnRange", 4));
bs.update(true, false);
}
}
/**
* Sets any entity that is in this location
* @param location - location
@ -263,43 +301,36 @@ public class Paster {
}
/**
* Handles signs, chests and mob spawner blocks
* @param island - island
* @param block - block
* @param config - config
* Tracks the minimum and maximum block positions
* @param world - world
* @param x - x
* @param y - y
* @param z - z
*/
private void setBlockState(Island island, Block block, ConfigurationSection config) {
// Get the block state
BlockState bs = block.getState();
// Signs
if (bs instanceof Sign) {
List<String> lines = config.getStringList(LINES);
writeSign(island, block, lines);
private void updatePos(World world, int x, int y, int z) {
if (pos1 == null) {
pos1 = new Location(world, x, y, z);
}
// Chests, in general
if (bs instanceof InventoryHolder) {
bs.update(true, false);
Inventory ih = ((InventoryHolder)bs).getInventory();
if (config.isConfigurationSection(INVENTORY)) {
ConfigurationSection inv = config.getConfigurationSection(INVENTORY);
// Double chests are pasted as two blocks so inventory is filled twice. This code stops over filling for the first block.
inv.getKeys(false).stream()
.filter(i -> Integer.valueOf(i) < ih.getSize())
.forEach(i -> ih.setItem(Integer.valueOf(i), (ItemStack)inv.get(i)));
}
if (pos2 == null) {
pos2 = new Location(world, x, y, z);
}
// Mob spawners
if (bs instanceof CreatureSpawner) {
CreatureSpawner spawner = ((CreatureSpawner) bs);
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(config.getInt("delay", -1));
spawner.setRequiredPlayerRange(config.getInt("requiredPlayerRange", 16));
spawner.setSpawnRange(config.getInt("spawnRange", 4));
bs.update(true, false);
if (x < pos1.getBlockX()) {
pos1.setX(x);
}
if (x > pos2.getBlockX()) {
pos2.setX(x);
}
if (y < pos1.getBlockY()) {
pos1.setY(y);
}
if (y > pos2.getBlockY()) {
pos2.setY(y);
}
if (z < pos1.getBlockZ()) {
pos1.setZ(z);
}
if (z > pos2.getBlockZ()) {
pos2.setZ(z);
}
}

View File

@ -0,0 +1,208 @@
package world.bentobox.bentobox.managers;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.blueprints.Clipboard;
import world.bentobox.bentobox.blueprints.Paster;
import world.bentobox.bentobox.database.objects.Island;
/**
* @author tastybento
*/
public class ClipboardManager {
private static final String LOAD_ERROR = "Could not load schems file - does not exist : ";
private static void unzipFiles(final ZipInputStream zipInputStream, final Path unzipFilePath) throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(unzipFilePath.toAbsolutePath().toString()))) {
byte[] bytesIn = new byte[1024];
int read;
while ((read = zipInputStream.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
}
}
private BentoBox plugin;
private File schemFolder;
private Clipboard clipboard;
public ClipboardManager(BentoBox plugin, File schemFolder) {
this(plugin, schemFolder, null);
}
public ClipboardManager(BentoBox plugin, File schemFolder, Clipboard clipboard) {
super();
this.plugin = plugin;
if (!schemFolder.exists()) {
schemFolder.mkdirs();
}
this.schemFolder = schemFolder;
this.clipboard = clipboard;
}
/**
* @return the clipboard
*/
public Clipboard getClipboard() {
return clipboard;
}
/**
* Load a file to clipboard
* @param fileName - filename in schems folder
* @throws IOException - if there's a load error with unziping or name
* @throws InvalidConfigurationException - the YAML of the schem is at fault
*/
public void load(String fileName) throws IOException, InvalidConfigurationException {
File zipFile = new File(schemFolder, fileName + ".schem");
if (!zipFile.exists()) {
plugin.logError(LOAD_ERROR + zipFile.getName());
throw new IOException(LOAD_ERROR + zipFile.getName());
}
unzip(zipFile.getAbsolutePath());
File file = new File(schemFolder, fileName);
if (!file.exists()) {
plugin.logError(LOAD_ERROR + file.getName());
throw new IOException(LOAD_ERROR + file.getName());
}
YamlConfiguration blockConfig = new YamlConfiguration();
blockConfig.load(file);
clipboard = new Clipboard(blockConfig);
Files.delete(file.toPath());
}
/*
Load a file to clipboard
*/
/**
* @param user - user trying to load
* @param fileName - filename
* @return - <tt>true</tt> if load is successful, <tt>false</tt> if not
*/
public boolean load(User user, String fileName) {
try {
load(fileName);
} catch (IOException e1) {
user.sendMessage("commands.admin.schem.could-not-load");
plugin.logError("Could not load schems file: " + fileName + " " + e1.getMessage());
return false;
} catch (InvalidConfigurationException e1) {
user.sendMessage("commands.admin.schem.could-not-load");
plugin.logError("Could not load schems file - YAML error : " + fileName + " " + e1.getMessage());
return false;
}
user.sendMessage("general.success");
return true;
}
/**
* Paste clipboard to this location
* @param location - location
*/
public void pasteClipboard(Location location) {
if (clipboard != null) {
new Paster(plugin, clipboard, location);
} else {
plugin.logError("Clipboard has no block data in it to paste!");
}
}
/**
* Pastes the clipboard to island location.
* If pos1 and pos2 are not set already, they are automatically set to the pasted coordinates
* @param world - world in which to paste
* @param island - location to paste
* @param task - task to run after pasting
*/
public void pasteIsland(World world, Island island, Runnable task) {
new Paster(plugin, clipboard, world, island, task);
}
/**
* Save the clipboard to a file
* @param user - user who is copying
* @param newFile - filename
* @return - true if successful, false if error
*/
public boolean save(User user, String newFile) {
File file = new File(schemFolder, newFile);
try {
clipboard.getBlockConfig().save(file);
} catch (IOException e) {
user.sendMessage("commands.admin.schem.could-not-save", "[message]", "Could not save temp schems file.");
plugin.logError("Could not save temporary schems file: " + file.getName());
return false;
}
try {
zip(file);
} catch (IOException e) {
user.sendMessage("commands.admin.schem.could-not-save", "[message]", "Could not zip temp schems file.");
plugin.logError("Could not zip temporary schems file: " + file.getName());
return false;
}
user.sendMessage("general.success");
return true;
}
private void unzip(final String zipFilePath) throws IOException {
Path path = Paths.get(zipFilePath);
if (!(path.toFile().exists())) {
throw new IOException("No file exists!");
}
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath))) {
ZipEntry entry = zipInputStream.getNextEntry();
while (entry != null) {
Path filePath = Paths.get(path.getParent().toString(), entry.getName());
if (!entry.isDirectory()) {
unzipFiles(zipInputStream, filePath);
} else {
Files.createDirectories(filePath);
}
zipInputStream.closeEntry();
entry = zipInputStream.getNextEntry();
}
}
}
private void zip(File targetFile) throws IOException {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetFile.getAbsolutePath() + ".schem"))) {
zipOutputStream.putNextEntry(new ZipEntry(targetFile.getName()));
try (FileInputStream inputStream = new FileInputStream(targetFile)) {
final byte[] buffer = new byte[1024];
int length;
while((length = inputStream.read(buffer)) >= 0) {
zipOutputStream.write(buffer, 0, length);
}
try {
Files.delete(targetFile.toPath());
} catch (Exception e) {
plugin.logError(e.getMessage());
}
}
}
}
}

View File

@ -1,16 +1,5 @@
package world.bentobox.bentobox.managers;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.util.Util;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
@ -22,6 +11,19 @@ import java.util.Set;
import java.util.TreeMap;
import java.util.jar.JarFile;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.blueprints.Clipboard;
import world.bentobox.bentobox.blueprints.Paster;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
public class SchemsManager {
/**
@ -71,9 +73,9 @@ public class SchemsManager {
}
/**
* Get all the schems for this world
* Get all the blueprints for this world
* @param world world
* @return map of schems for this world or an empty map if there are none registered
* @return map of blueprints for this world or an empty map if there are none registered
*/
public Map<String, Clipboard> get(World world) {
return islandSchems.getOrDefault(world, new TreeMap<>(String.CASE_INSENSITIVE_ORDER));
@ -120,9 +122,9 @@ public class SchemsManager {
plugin.log("Loading " + name + ".schem for " + world.getName());
Map<String, Clipboard> schemList = islandSchems.getOrDefault(world, new TreeMap<>(String.CASE_INSENSITIVE_ORDER));
try {
Clipboard cb = new Clipboard(plugin, schems);
ClipboardManager cb = new ClipboardManager(plugin, schems);
cb.load(name);
schemList.put(name, cb);
schemList.put(name, cb.getClipboard());
islandSchems.put(world, schemList);
} catch (IOException | InvalidConfigurationException e) {
plugin.logError("Could not load " + name + " schem, skipping!");
@ -150,7 +152,7 @@ public class SchemsManager {
*/
public void paste(World world, Island island, String name, Runnable task) {
if (islandSchems.containsKey(world) && islandSchems.get(world).containsKey(name)) {
islandSchems.get(world).get(name).pasteIsland(world, island, task);
new Paster(plugin, islandSchems.get(world).get(name), world, island, task);
} else {
plugin.logError("Tried to paste schem '" + name + "' for " + world.getName() + " but the schem is not loaded!");
plugin.log("This might be due to an invalid schem format. Keep in mind that schems are not schematics.");

View File

@ -1,442 +0,0 @@
package world.bentobox.bentobox.schems;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.Sign;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.ChestedHorse;
import org.bukkit.entity.Horse;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Attachable;
import org.bukkit.material.Colorable;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
/**
* @author tastybento
*/
public class Clipboard {
// Commonly used texts along this class.
private static final String ATTACHED_YAML_PREFIX = "attached.";
private static final String ENTITIES_YAML_PREFIX = "entities.";
private static final String BLOCKS_YAML_PREFIX = "blocks.";
private static final String BEDROCK = "bedrock";
private static final String COLOR = "color";
private static final String LOAD_ERROR = "Could not load schems file - does not exist : ";
private static final String LINES = "lines";
private YamlConfiguration blockConfig = new YamlConfiguration();
private Location pos1;
private Location pos2;
private Location origin;
private BentoBox plugin;
private boolean copied;
private File schemFolder;
public Clipboard(BentoBox plugin, File schemFolder) {
super();
this.plugin = plugin;
if (!schemFolder.exists()) {
schemFolder.mkdirs();
}
this.schemFolder = schemFolder;
}
/**
* @return the pos1
*/
public Location getPos1() {
return pos1;
}
/**
* @param pos1 the pos1 to set
*/
public void setPos1(Location pos1) {
origin = null;
this.pos1 = pos1;
}
/**
* @return the pos2
*/
public Location getPos2() {
return pos2;
}
/**
* @param pos2 the pos2 to set
*/
public void setPos2(Location pos2) {
origin = null;
this.pos2 = pos2;
}
/**
* @return the origin
*/
public Location getOrigin() {
return origin;
}
/**
* @param origin the origin to set
*/
public void setOrigin(Location origin) {
this.origin = origin;
}
/**
* Copy the blocks between pos1 and pos2 to the clipboard
* @param user - user
* @return true if successful, false if pos1 or pos2 are undefined or something is already in the clipboard
*/
public boolean copy(User user, boolean copyAir) {
if (pos1 == null || pos2 == null) {
user.sendMessage("commands.admin.schem.need-pos1-pos2");
return false;
}
// World
World world = pos1.getWorld();
// Clear the clipboard
blockConfig = new YamlConfiguration();
int count = 0;
BoundingBox toCopy = BoundingBox.of(pos1, pos2);
for (int x = (int)toCopy.getMinX(); x <= toCopy.getMaxX(); x++) {
for (int y = (int)toCopy.getMinY(); y <= toCopy.getMaxY(); y++) {
for (int z = (int)toCopy.getMinZ(); z <= toCopy.getMaxZ(); z++) {
Block block = world.getBlockAt(x, y, z);
if (copyBlock(block, origin == null ? user.getLocation() : origin, copyAir, world.getLivingEntities().stream()
.filter(Objects::nonNull)
.filter(e -> !(e instanceof Player) && e.getLocation().getBlock().equals(block))
.collect(Collectors.toList()))) {
count ++;
}
}
}
}
blockConfig.set("size.xsize", toCopy.getWidthX());
blockConfig.set("size.ysize", toCopy.getHeight());
blockConfig.set("size.zsize", toCopy.getWidthZ());
user.sendMessage("commands.admin.schem.copied-blocks", TextVariables.NUMBER, String.valueOf(count));
copied = true;
return true;
}
/**
* Pastes the clipboard to island location.
* If pos1 and pos2 are not set already, they are automatically set to the pasted coordinates
* @param world - world in which to paste
* @param island - location to paste
* @param task - task to run after pasting
*/
public void pasteIsland(World world, Island island, Runnable task) {
// Offset due to bedrock
Vector off = new Vector(0,0,0);
if (blockConfig.contains(BEDROCK)) {
String[] offset = blockConfig.getString(BEDROCK).split(",");
off = new Vector(Integer.valueOf(offset[0]), Integer.valueOf(offset[1]), Integer.valueOf(offset[2]));
}
// Calculate location for pasting
Location loc = island.getCenter().toVector().subtract(off).toLocation(world);
// Paste
paste(world, island, loc, task);
}
private void paste(World world, Island island, Location loc, Runnable task) {
new Paster(plugin, this, blockConfig, world, island, loc, task);
}
/**
* Paste clipboard at this location
* @param location - location
*/
public void pasteClipboard(Location location) {
if (blockConfig.contains(BLOCKS_YAML_PREFIX)) {
paste(location.getWorld(), null, location, null);
} else {
plugin.logError("Clipboard has no block data in it to paste!");
}
}
private boolean copyBlock(Block block, Location copyOrigin, boolean copyAir, Collection<LivingEntity> entities) {
if (!copyAir && block.getType().equals(Material.AIR) && entities.isEmpty()) {
return false;
}
// Create position
int x = block.getLocation().getBlockX() - copyOrigin.getBlockX();
int y = block.getLocation().getBlockY() - copyOrigin.getBlockY();
int z = block.getLocation().getBlockZ() - copyOrigin.getBlockZ();
String pos = x + "," + y + "," + z;
// Position defines the section
ConfigurationSection s = blockConfig.createSection(BLOCKS_YAML_PREFIX + "." + pos);
// Set entities
for (LivingEntity e: entities) {
ConfigurationSection en = blockConfig.createSection(ENTITIES_YAML_PREFIX + pos + "." + e.getUniqueId());
en.set("type", e.getType().name());
en.set("name", e.getCustomName());
if (e instanceof Colorable) {
Colorable c = (Colorable)e;
if (c.getColor() != null) {
en.set(COLOR, c.getColor().name());
}
}
if (e instanceof Tameable && ((Tameable)e).isTamed()) {
en.set("tamed", true);
}
if (e instanceof ChestedHorse && ((ChestedHorse)e).isCarryingChest()) {
en.set("chest", true);
}
if (e instanceof Ageable) {
en.set("adult", ((Ageable)e).isAdult());
}
if (e instanceof AbstractHorse) {
AbstractHorse horse = (AbstractHorse)e;
en.set("domestication", horse.getDomestication());
for (int index = 0; index < horse.getInventory().getSize(); index++) {
ItemStack i = horse.getInventory().getItem(index);
if (i != null) {
en.set("inventory." + index, i);
}
}
}
if (e instanceof Horse) {
Horse horse = (Horse)e;
en.set("style", horse.getStyle().name());
}
}
// Return if this is just air block
if (!copyAir && block.getType().equals(Material.AIR) && !entities.isEmpty()) {
return true;
}
// Block state
BlockState bs = block.getState();
// Set block data
if (bs.getData() instanceof Attachable) {
ConfigurationSection a = blockConfig.createSection(ATTACHED_YAML_PREFIX + pos);
a.set("bd", block.getBlockData().getAsString());
// Placeholder for attachment
s.set("bd", "minecraft:air");
// Signs
if (bs instanceof Sign) {
Sign sign = (Sign)bs;
a.set(LINES, Arrays.asList(sign.getLines()));
}
return true;
} else {
s.set("bd", block.getBlockData().getAsString());
// Signs
if (bs instanceof Sign) {
Sign sign = (Sign)bs;
s.set(LINES, Arrays.asList(sign.getLines()));
}
}
if (block.getType().equals(Material.BEDROCK)) {
blockConfig.set(BEDROCK, x + "," + y + "," + z);
}
// Chests
if (bs instanceof InventoryHolder) {
InventoryHolder ih = (InventoryHolder)bs;
for (int index = 0; index < ih.getInventory().getSize(); index++) {
ItemStack i = ih.getInventory().getItem(index);
if (i != null) {
s.set("inventory." + index, i);
}
}
}
if (bs instanceof CreatureSpawner) {
CreatureSpawner spawner = (CreatureSpawner)bs;
s.set("spawnedType",spawner.getSpawnedType().name());
s.set("delay", spawner.getDelay());
s.set("maxNearbyEntities", spawner.getMaxNearbyEntities());
s.set("maxSpawnDelay", spawner.getMaxSpawnDelay());
s.set("minSpawnDelay", spawner.getMinSpawnDelay());
s.set("requiredPlayerRange", spawner.getRequiredPlayerRange());
s.set("spawnRange", spawner.getSpawnRange());
}
return true;
}
/**
* @return the blockConfig
*/
private YamlConfiguration getBlockConfig() {
return blockConfig;
}
private void unzip(final String zipFilePath) throws IOException {
Path path = Paths.get(zipFilePath);
if (!(path.toFile().exists())) {
throw new IOException("No file exists!");
}
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath))) {
ZipEntry entry = zipInputStream.getNextEntry();
while (entry != null) {
Path filePath = Paths.get(path.getParent().toString(), entry.getName());
if (!entry.isDirectory()) {
unzipFiles(zipInputStream, filePath);
} else {
Files.createDirectories(filePath);
}
zipInputStream.closeEntry();
entry = zipInputStream.getNextEntry();
}
}
}
private static void unzipFiles(final ZipInputStream zipInputStream, final Path unzipFilePath) throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(unzipFilePath.toAbsolutePath().toString()))) {
byte[] bytesIn = new byte[1024];
int read;
while ((read = zipInputStream.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
}
}
private void zip(File targetFile) throws IOException {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetFile.getAbsolutePath() + ".schem"))) {
zipOutputStream.putNextEntry(new ZipEntry(targetFile.getName()));
try (FileInputStream inputStream = new FileInputStream(targetFile)) {
final byte[] buffer = new byte[1024];
int length;
while((length = inputStream.read(buffer)) >= 0) {
zipOutputStream.write(buffer, 0, length);
}
try {
Files.delete(targetFile.toPath());
} catch (Exception e) {
plugin.logError(e.getMessage());
}
}
}
}
public boolean isFull() {
return copied;
}
/**
* Load a file to clipboard
* @param fileName - filename in schems folder
* @throws IOException - if there's a load error with unziping or name
* @throws InvalidConfigurationException - the YAML of the schem is at fault
*/
public void load(String fileName) throws IOException, InvalidConfigurationException {
File zipFile = new File(schemFolder, fileName + ".schem");
if (!zipFile.exists()) {
plugin.logError(LOAD_ERROR + zipFile.getName());
throw new IOException(LOAD_ERROR + zipFile.getName());
}
unzip(zipFile.getAbsolutePath());
File file = new File(schemFolder, fileName);
if (!file.exists()) {
plugin.logError(LOAD_ERROR + file.getName());
throw new IOException(LOAD_ERROR + file.getName());
}
blockConfig = new YamlConfiguration();
blockConfig.load(file);
copied = true;
Files.delete(file.toPath());
// Clear pos1 and 2
setPos1(null);
setPos2(null);
}
/*
Load a file to clipboard
*/
/**
* @param user - use trying to load
* @param fileName - filename
* @return - <tt>true</tt> if load is successful, <tt>false</tt> if not
*/
public boolean load(User user, String fileName) {
try {
load(fileName);
} catch (IOException e1) {
user.sendMessage("commands.admin.schem.could-not-load");
plugin.logError("Could not load schems file: " + fileName + " " + e1.getMessage());
return false;
} catch (InvalidConfigurationException e1) {
user.sendMessage("commands.admin.schem.could-not-load");
plugin.logError("Could not load schems file - YAML error : " + fileName + " " + e1.getMessage());
return false;
}
user.sendMessage("general.success");
return true;
}
/**
* Save the clipboard to a file
* @param user - user who is copying
* @param newFile - filename
* @return - true if successful, false if error
*/
public boolean save(User user, String newFile) {
File file = new File(schemFolder, newFile);
try {
getBlockConfig().save(file);
} catch (IOException e) {
user.sendMessage("commands.admin.schem.could-not-save", "[message]", "Could not save temp schems file.");
plugin.logError("Could not save temporary schems file: " + file.getName());
return false;
}
try {
zip(file);
} catch (IOException e) {
user.sendMessage("commands.admin.schem.could-not-save", "[message]", "Could not zip temp schems file.");
plugin.logError("Could not zip temporary schems file: " + file.getName());
return false;
}
user.sendMessage("general.success");
return true;
}
}

View File

@ -48,7 +48,6 @@ commands:
console: "Console"
admin:
help:
parameters: ""
description: "admin command"
resets:
description: "edit resets of the players"
@ -64,27 +63,31 @@ commands:
description: "add player to owner's team"
name-not-owner: "&c[name] is not the owner."
name-has-island: "&c[name] has an island. Unregister or delete them first!"
success: "&b[name]&a has been added to &b[owner]&a's island."
disband:
parameters: "<owner>"
description: "disband owner's team"
use-disband-owner: "&cNot owner! Use disband [owner]"
use-disband-owner: "&cNot owner! Use disband [owner]."
disbanded: "&cAdmin disbanded your team!"
success: "&b[name]&a's team has been disbanded."
kick:
parameters: "<team player>"
description: "kick a player from a team"
cannot-kick-owner: "&cYou cannot kick the owner. Kick members first."
not-in-team: "&cThis player is not in a team."
admin-kicked: "&cThe admin kicked you from the team."
success: "&b[name] &ahas been kicked from &b[owner]&a's island."
setowner:
parameters: "<player>"
description: "transfers island ownership to the player"
already-owner: "&cPlayer is already the owner of this island!"
already-owner: "&c[name] is already the owner of this island!"
success: "&b[name]&a is now the owner of this island."
range:
description: "Admin island range command"
display:
already-off: "&cIndicators are already off"
already-on: "&cIndicators are already on"
description: "Show/hide island range indicators"
description: "show/hide island range indicators"
hiding: "&2Hiding range indicators"
hint: |-
&cRed Barrier icons &fshow the current island protected range limit.
@ -93,17 +96,17 @@ commands:
showing: "&2Showing range indicators"
set:
parameters: "<player> <range>"
description: "Sets the island protected range"
description: "sets the island protected range"
invalid-value:
not-numeric: "&c[number] is not a whole number!"
too-low: "&cThe protection range must be greater than 1!"
too-high: "&cThe protection range should be equal or less than [number]!"
same-as-before: "&cThe protection range is already set to [number]!"
success: "&2Set island protection range to [number]"
too-low: "&cThe protection range must be greater than &b1&c!"
too-high: "&cThe protection range should be equal or less than &b[number]&c!"
same-as-before: "&cThe protection range is already set to &b[number]&c!"
success: "&aSet island protection range to &b[number]&a."
reset:
parameters: "<player>"
description: "Resets the island protected range to the world default"
success: "&2Reset island protection range to [number]"
description: "resets the island protected range to the world default"
success: "&aReset island protection range to &b[number]&a."
register:
parameters: "<player>"
description: "register player to unowned island you are on"
@ -203,21 +206,17 @@ commands:
parameters: "<schem name>"
description: "load schem into the clipboard"
list:
parameters: ""
description: "list available schems"
no-schems: "&cNo schems in schems folder!"
available-schems: "&aThese schems are available for loading:"
origin:
parameters: ""
description: "set the schem's origin to your position"
paste:
parameters: ""
description: "paste the clipboard to your location"
pasting: "&aPasting..."
pos1:
parameters: ""
description: "set 1st corner of cuboid clipboard"
pos2:
parameters: ""
description: "set 2nd corner of cuboid clipboard"
save:
parameters: "<schem name>"
@ -241,9 +240,11 @@ commands:
reset:
description: "resets deaths of the player"
parameters: "<player>"
success: "&aSuccessfully reset &b[name]&a's deaths to &b0&a."
set:
description: "sets deaths of the player"
parameters: "<player> <deaths>"
success: "&aSuccessfully set &b[name]&a's deaths to &b[number]&a."
bentobox:
description: "BentoBox admin command"
about:
@ -392,6 +393,7 @@ commands:
parameters: "<player>"
owner-kicked: "&cThe owner kicked you from the island!"
cannot-kick: "&cYou cannot kick yourself!"
success: "&b[name] &ahas been kicked from your island."
demote:
description: "demote a player on your island down a rank"
parameters: "<player>"

View File

@ -1,17 +1,5 @@
package world.bentobox.bentobox.api.commands.admin.team;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;
@ -25,9 +13,9 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager;
@ -36,6 +24,18 @@ import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author tastybento
*
@ -305,7 +305,7 @@ public class AdminTeamAddCommandTest {
// Success
assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(im).setJoinTeam(Mockito.eq(island), Mockito.eq(notUUID));
Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
Mockito.verify(user).sendMessage("commands.admin.team.add.success", TextVariables.NAME, name[1], "[owner]", name[0]);
}
}

View File

@ -1,19 +1,5 @@
package world.bentobox.bentobox.api.commands.admin.team;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;
@ -27,9 +13,9 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager;
@ -38,6 +24,20 @@ import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author tastybento
*
@ -123,10 +123,8 @@ public class AdminTeamDisbandCommandTest {
PluginManager pim = mock(PluginManager.class);
when(server.getPluginManager()).thenReturn(pim);
when(Bukkit.getServer()).thenReturn(server);
}
/**
* Test method for {@link AdminTeamDisbandCommand#execute(User, String, List)}.
*/
@ -190,6 +188,7 @@ public class AdminTeamDisbandCommandTest {
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(is);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(pm.getName(Mockito.any())).thenReturn(name[0]);
// Owner
when(im.getOwner(Mockito.any(), Mockito.eq(notUUID))).thenReturn(notUUID);
// Members
@ -202,7 +201,6 @@ public class AdminTeamDisbandCommandTest {
assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(im, Mockito.never()).setLeaveTeam(Mockito.any(), Mockito.eq(notUUID));
Mockito.verify(im).setLeaveTeam(Mockito.any(), Mockito.eq(uuid));
Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
Mockito.verify(user).sendMessage("commands.admin.team.disband.success", TextVariables.NAME, name[0]);
}
}

View File

@ -1,18 +1,5 @@
package world.bentobox.bentobox.api.commands.admin.team;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;
@ -26,9 +13,9 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager;
@ -37,6 +24,20 @@ import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author tastybento
*
@ -122,7 +123,6 @@ public class AdminTeamKickCommandTest {
PluginManager pim = mock(PluginManager.class);
when(server.getPluginManager()).thenReturn(pim);
when(Bukkit.getServer()).thenReturn(server);
}
@ -172,7 +172,7 @@ public class AdminTeamKickCommandTest {
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(im.getOwner(Mockito.any(), Mockito.eq(notUUID))).thenReturn(notUUID);
when(is.getOwner()).thenReturn(notUUID);
AdminTeamKickCommand itl = new AdminTeamKickCommand(ac);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
@ -188,15 +188,16 @@ public class AdminTeamKickCommandTest {
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(true);
Island is = mock(Island.class);
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(is);
String[] name = {"tastybento"};
String name = "tastybento";
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(pm.getName(Mockito.any())).thenReturn(name);
when(im.getOwner(Mockito.any(), Mockito.eq(notUUID))).thenReturn(uuid);
when(is.getOwner()).thenReturn(uuid);
AdminTeamKickCommand itl = new AdminTeamKickCommand(ac);
assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList(name)));
Mockito.verify(im).removePlayer(Mockito.any(), Mockito.eq(notUUID));
Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
Mockito.verify(user).sendMessage(Mockito.eq("commands.admin.team.kick.success"), Mockito.eq(TextVariables.NAME), Mockito.eq(name), Mockito.eq("[owner]"), Mockito.anyString());
}
}

View File

@ -1,18 +1,5 @@
package world.bentobox.bentobox.api.commands.admin.team;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;
@ -26,9 +13,9 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager;
@ -37,6 +24,19 @@ import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author tastybento
*
@ -167,12 +167,13 @@ public class AdminTeamSetownerCommandTest {
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(is);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(pm.getName(Mockito.any())).thenReturn(name[0]);
when(im.getOwner(Mockito.any(), Mockito.eq(notUUID))).thenReturn(notUUID);
AdminTeamSetownerCommand itl = new AdminTeamSetownerCommand(ac);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage("commands.admin.team.setowner.already-owner");
Mockito.verify(user).sendMessage("commands.admin.team.setowner.already-owner", TextVariables.NAME, name[0]);
}
/**
@ -188,6 +189,7 @@ public class AdminTeamSetownerCommandTest {
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(is);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(pm.getName(Mockito.any())).thenReturn(name[0]);
// Owner
when(im.getOwner(Mockito.any(), Mockito.eq(notUUID))).thenReturn(uuid);
when(pm.getName(Mockito.eq(uuid))).thenReturn("owner");
@ -201,7 +203,6 @@ public class AdminTeamSetownerCommandTest {
assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
// Add other verifications
Mockito.verify(im).setOwner(Mockito.any(), Mockito.eq(user), Mockito.eq(notUUID));
Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
Mockito.verify(user).sendMessage("commands.admin.team.setowner.success", TextVariables.NAME, name[0]);
}
}

View File

@ -37,6 +37,7 @@ import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.blueprints.Clipboard;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
@ -45,7 +46,6 @@ import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.SchemsManager;
import world.bentobox.bentobox.managers.island.NewIsland;
import world.bentobox.bentobox.managers.island.NewIsland.Builder;
import world.bentobox.bentobox.schems.Clipboard;
/**
* @author tastybento

View File

@ -1,17 +1,5 @@
package world.bentobox.bentobox.api.commands.island.team;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;
@ -27,10 +15,10 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
@ -38,6 +26,18 @@ import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author tastybento
*/
@ -209,6 +209,7 @@ public class IslandTeamKickCommandTest {
when(s.isKickConfirmation()).thenReturn(false);
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(pm.getName(notUUID)).thenReturn("poslovitch");
Set<UUID> members = new HashSet<>();
members.add(notUUID);
@ -217,7 +218,7 @@ public class IslandTeamKickCommandTest {
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch")));
Mockito.verify(im).removePlayer(Mockito.any(), Mockito.eq(notUUID));
Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
Mockito.verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch");
}
/**
@ -250,6 +251,7 @@ public class IslandTeamKickCommandTest {
Player targetPlayer = mock(Player.class);
when(targetPlayer.getUniqueId()).thenReturn(notUUID);
when(targetPlayer.isOnline()).thenReturn(true);
when(targetPlayer.getName()).thenReturn("poslovitch");
User.getInstance(targetPlayer);
// Target's inventory
PlayerInventory inv = mock(PlayerInventory.class);
@ -274,7 +276,7 @@ public class IslandTeamKickCommandTest {
// Verify
Mockito.verify(im).removePlayer(Mockito.any(), Mockito.eq(notUUID));
Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
Mockito.verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch");
Mockito.verify(enderChest).clear();
Mockito.verify(inv).clear();
}

View File

@ -1,9 +1,7 @@
package world.bentobox.bentobox.schems;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -51,7 +49,7 @@ import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.blueprints.Clipboard;
@SuppressWarnings("deprecation")
@RunWith(PowerMockRunner.class)
@ -173,15 +171,12 @@ public class ClipboardTest {
@Test
public void testClipboard() {
when(schemFolder.exists()).thenReturn(false);
new Clipboard(plugin, schemFolder);
Mockito.verify(schemFolder).mkdirs();
new Clipboard();
}
@Test
public void testSetGetPos1() {
Clipboard cb = new Clipboard(plugin, schemFolder);
Clipboard cb = new Clipboard();
assertNull(cb.getPos1());
cb.setPos1(loc);
assertEquals(loc, cb.getPos1());
@ -190,7 +185,7 @@ public class ClipboardTest {
@Test
public void testSetGetPos2() {
Clipboard cb = new Clipboard(plugin, schemFolder);
Clipboard cb = new Clipboard();
assertNull(cb.getPos2());
cb.setPos2(loc);
assertEquals(loc, cb.getPos2());
@ -198,7 +193,7 @@ public class ClipboardTest {
@Test
public void testSetGetOrigin() {
Clipboard cb = new Clipboard(plugin, schemFolder);
Clipboard cb = new Clipboard();
assertNull(cb.getOrigin());
cb.setOrigin(loc);
assertEquals(loc, cb.getOrigin());
@ -206,14 +201,14 @@ public class ClipboardTest {
@Test
public void testCopyNoPos1Pos2() {
Clipboard cb = new Clipboard(plugin, schemFolder);
Clipboard cb = new Clipboard();
cb.copy(user, false);
Mockito.verify(user).sendMessage(Mockito.eq("commands.admin.schem.need-pos1-pos2"));
}
@Test
public void testCopyNoPos2() {
Clipboard cb = new Clipboard(plugin, schemFolder);
Clipboard cb = new Clipboard();
cb.setPos1(loc);
cb.copy(user, false);
Mockito.verify(user).sendMessage(Mockito.eq("commands.admin.schem.need-pos1-pos2"));
@ -221,7 +216,7 @@ public class ClipboardTest {
@Test
public void testCopy() {
Clipboard cb = new Clipboard(plugin, schemFolder);
Clipboard cb = new Clipboard();
cb.setPos1(loc);
cb.setPos2(loc2);
cb.copy(user, false);
@ -235,7 +230,7 @@ public class ClipboardTest {
String[] lines = {"line1", "line2", "line3", "line4"};
when(bs.getLines()).thenReturn(lines);
when(block.getState()).thenReturn(bs);
Clipboard cb = new Clipboard(plugin, schemFolder);
Clipboard cb = new Clipboard();
cb.setPos1(loc);
cb.setPos2(loc2);
cb.copy(user, false);
@ -253,7 +248,7 @@ public class ClipboardTest {
when(inv.getContents()).thenReturn(contents);
when(bs.getInventory()).thenReturn(inv);
when(block.getState()).thenReturn(bs);
Clipboard cb = new Clipboard(plugin, schemFolder);
Clipboard cb = new Clipboard();
cb.setPos1(loc);
cb.setPos2(loc2);
cb.copy(user, false);
@ -268,7 +263,7 @@ public class ClipboardTest {
CreatureSpawner bs = mock(CreatureSpawner.class);
when(bs.getSpawnedType()).thenReturn(EntityType.CAVE_SPIDER);
when(block.getState()).thenReturn(bs);
Clipboard cb = new Clipboard(plugin, schemFolder);
Clipboard cb = new Clipboard();
cb.setPos1(loc);
cb.setPos2(loc2);
cb.copy(user, false);
@ -284,7 +279,7 @@ public class ClipboardTest {
when(block.getType()).thenReturn(Material.AIR);
BlockState bs = mock(BlockState.class);
when(block.getState()).thenReturn(bs);
Clipboard cb = new Clipboard(plugin, schemFolder);
Clipboard cb = new Clipboard();
cb.setPos1(loc);
cb.setPos2(loc2);
// Do not copy air
@ -294,60 +289,4 @@ public class ClipboardTest {
Mockito.verify(user).sendMessage("commands.admin.schem.copied-blocks", TextVariables.NUMBER, "8");
}
@Test
public void testPasteIslandNoData() {
Clipboard cb = new Clipboard(plugin, schemFolder);
Island island = mock(Island.class);
when(island.getCenter()).thenReturn(loc);
cb.pasteIsland(world, island, () -> {});
Mockito.verify(plugin).logError(Mockito.eq("Clipboard has no block data in it to paste!"));
// Verify the task is run
Mockito.verify(sched, Mockito.never()).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L));
}
@Test
public void testPasteIslandWithData() {
Clipboard cb = new Clipboard(plugin, schemFolder);
Island island = mock(Island.class);
when(island.getCenter()).thenReturn(loc);
cb.setPos1(loc);
cb.setPos2(loc2);
cb.copy(user, false);
cb.pasteIsland(world, island, () -> {});
// Verify the task is run
Mockito.verify(sched).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L));
}
@Test
public void testPasteClipboardNoData() {
Clipboard cb = new Clipboard(plugin, schemFolder);
cb.pasteClipboard(loc);
Mockito.verify(plugin).logError(Mockito.eq("Clipboard has no block data in it to paste!"));
}
@Test
public void testPasteClipboard() {
Clipboard cb = new Clipboard(plugin, schemFolder);
cb.setPos1(loc);
cb.setPos2(loc2);
cb.copy(user, false);
cb.pasteClipboard(loc);
// No verification possible on the new constructor
}
@Test
public void testIsFull() {
Clipboard cb = new Clipboard(plugin, schemFolder);
assertFalse(cb.isFull());
cb.setPos1(loc);
cb.setPos2(loc2);
cb.copy(user, false);
assertTrue(cb.isFull());
}
/*
* Will not test the file system methods
*/
}