Adjusted Clipboard to remove code smell and renamed package

This commit is contained in:
tastybento 2018-08-03 21:19:15 -07:00
parent 1c29dfca6b
commit 683aca7043
5 changed files with 75 additions and 53 deletions

View File

@ -12,7 +12,7 @@ import org.bukkit.block.Block;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.island.builders.Clipboard;
import world.bentobox.bentobox.schems.Clipboard;
import world.bentobox.bentobox.util.Util;
public class AdminSchemCommand extends CompositeCommand {
@ -42,7 +42,7 @@ public class AdminSchemCommand extends CompositeCommand {
if (args.get(0).equalsIgnoreCase("paste")) {
if (cb.isFull()) {
cb.paste(user.getLocation());
cb.pasteClipboard(user.getLocation());
user.sendMessage("general.success");
return true;
} else {

View File

@ -19,7 +19,7 @@ import com.google.common.collect.ImmutableSet.Builder;
import com.google.gson.annotations.Expose;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.adapters.Adapter;
@ -514,7 +514,9 @@ public class Island implements DataObject {
* Sets the island range.
* This method should <u><strong>NEVER</strong></u> be used except for testing purposes.
* <br>
* The range value is a copy of {@link Settings#getIslandDistance()} made when the Island got created in order to allow easier access to this value and must therefore remain <u><strong>AS IS</strong></u>.
* The range value is a copy of {@link WorldSettings#getIslandDistance()} made when the Island
* got created in order to allow easier access to this value and must therefore remain
* <u><strong>AS IS</strong></u>.
* @param range the range to set
* @see #setProtectionRange(int)
*/

View File

@ -559,10 +559,9 @@ public class IslandWorldManager {
}
/**
* The data folder for the addon that registered this world, or the plugin's
* data folder if none found
* Get data folder for the addon that registered this world
*
* @return
* @return data folder file object or the plugin's data folder if none found
*/
public File getDataFolder(World world) {
return worldSettings.get(Util.getWorld(world)).getAddon().map(Addon::getDataFolder)

View File

@ -12,7 +12,7 @@ import org.bukkit.configuration.InvalidConfigurationException;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.island.builders.Clipboard;
import world.bentobox.bentobox.schems.Clipboard;
public class SchemsManager {
@ -95,7 +95,7 @@ public class SchemsManager {
*/
public void paste(World world, Island island, Runnable task) {
if (islandSchems.containsKey(world)) {
islandSchems.get(world).paste(world, island, task);
islandSchems.get(world).pasteIsland(world, island, task);
} else {
plugin.logError("Tried to paste schem for " + world.getName() + " but it is not loaded!");
}

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.island.builders;
package world.bentobox.bentobox.schems;
import java.io.BufferedOutputStream;
import java.io.File;
@ -178,7 +178,7 @@ public class Clipboard {
* @param island - location to paste
* @param task - task to run after pasting
*/
public void paste(World world, Island island, Runnable task) {
public void pasteIsland(World world, Island island, Runnable task) {
// Offset due to bedrock
Vector off = new Vector(0,0,0);
if (blockConfig.contains(BEDROCK)) {
@ -199,7 +199,7 @@ public class Clipboard {
* Paste clipboard at this location
* @param location
*/
public void paste(Location location) {
public void pasteClipboard(Location location) {
blockConfig.getConfigurationSection(BLOCK).getKeys(false).forEach(b -> pasteBlock(location.getWorld(), null, location, blockConfig.getConfigurationSection(BLOCK + "." + b)));
}
@ -247,44 +247,28 @@ public class Clipboard {
}
private void setBlock(Island island, Block block, ConfigurationSection config, String blockData) {
// Set the block data
block.setBlockData(Bukkit.createBlockData(blockData));
// 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);
inv.getKeys(false).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);
}
// Entities
// Set the block state for chests, signs and mob spawners
setBlockState(island, block, config);
// Set entities
if (config.isConfigurationSection(ENTITY)) {
setEntity(island, block.getLocation(), config);
}
}
/**
* Sets any entity that is in this location
* @param island
* @param block
* @param config
*/
private void setEntity(Island island, Location location, ConfigurationSection config) {
ConfigurationSection en = config.getConfigurationSection(ENTITY);
en.getKeys(false).forEach(k -> {
ConfigurationSection ent = en.getConfigurationSection(k);
Location center = block.getLocation().add(new Vector(0.5, 0.0, 0.5));
LivingEntity e = (LivingEntity)block.getWorld().spawnEntity(center, EntityType.valueOf(ent.getString("type", "PIG")));
Location center = location.add(new Vector(0.5, 0.0, 0.5));
LivingEntity e = (LivingEntity)island.getWorld().spawnEntity(center, EntityType.valueOf(ent.getString("type", "PIG")));
if (e != null) {
e.setCustomName(ent.getString("name"));
}
@ -316,8 +300,45 @@ public class Clipboard {
horse.setStyle(Horse.Style.valueOf(ent.getString("style", "NONE")));
}
});
}
/**
* 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);
inv.getKeys(false).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);
}
}
private boolean copyBlock(Block block, Location copyOrigin, boolean copyAir, Collection<LivingEntity> entities) {