Improved code readability in Clipboard

This commit is contained in:
Florian CUNY 2019-04-28 13:50:15 +02:00
parent 8a885eac7f
commit 854d06eef2

View File

@ -1,10 +1,5 @@
package world.bentobox.bentobox.blueprints;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@ -27,12 +22,18 @@ 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.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 {
@ -45,18 +46,16 @@ public class Clipboard {
private static final String LINES = "lines";
private YamlConfiguration blockConfig;
private Location pos1;
private Location pos2;
private Location origin;
private @Nullable Location pos1;
private @Nullable Location pos2;
private @Nullable Location origin;
public Clipboard(String contents) throws InvalidConfigurationException {
super();
set(contents);
}
public Clipboard(YamlConfiguration config) {
super();
blockConfig = config;
this.blockConfig = config;
}
public Clipboard() {
@ -64,9 +63,9 @@ public class Clipboard {
}
/**
* Copy the blocks between pos1 and pos2 to the clipboard
* Copy the blocks between pos1 and pos2 into the clipboard
* @param user - user
* @return true if successful, false if pos1 or pos2 are undefined or something is already in the clipboard
* @return true if successful, false if pos1 or pos2 are undefined.
*/
public boolean copy(User user, boolean copyAir) {
if (pos1 == null || pos2 == null) {
@ -112,42 +111,42 @@ public class Clipboard {
String pos = x + "," + y + "," + z;
// Position defines the section
ConfigurationSection s = blockConfig.createSection(BLOCKS_YAML_PREFIX + "." + pos);
ConfigurationSection blocksSection = 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;
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) {
en.set(COLOR, c.getColor().name());
entitySection.set(COLOR, c.getColor().name());
}
}
if (e instanceof Tameable && ((Tameable)e).isTamed()) {
en.set("tamed", true);
if (entity instanceof Tameable && ((Tameable)entity).isTamed()) {
entitySection.set("tamed", true);
}
if (e instanceof ChestedHorse && ((ChestedHorse)e).isCarryingChest()) {
en.set("chest", true);
if (entity instanceof ChestedHorse && ((ChestedHorse)entity).isCarryingChest()) {
entitySection.set("chest", true);
}
if (e instanceof Ageable) {
en.set("adult", ((Ageable)e).isAdult());
if (entity instanceof Ageable) {
entitySection.set("adult", ((Ageable)entity).isAdult());
}
if (e instanceof AbstractHorse) {
AbstractHorse horse = (AbstractHorse)e;
en.set("domestication", horse.getDomestication());
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) {
en.set("inventory." + index, i);
entitySection.set("inventory." + index, i);
}
}
}
if (e instanceof Horse) {
Horse horse = (Horse)e;
en.set("style", horse.getStyle().name());
if (entity instanceof Horse) {
Horse horse = (Horse)entity;
entitySection.set("style", horse.getStyle().name());
}
}
@ -157,26 +156,26 @@ public class Clipboard {
}
// Block state
BlockState bs = block.getState();
BlockState blockState = block.getState();
// Set block data
if (bs.getData() instanceof Attachable) {
ConfigurationSection a = blockConfig.createSection(ATTACHED_YAML_PREFIX + pos);
a.set("bd", block.getBlockData().getAsString());
if (blockState.getData() instanceof Attachable) {
ConfigurationSection attachedSection = blockConfig.createSection(ATTACHED_YAML_PREFIX + pos);
attachedSection.set("bd", block.getBlockData().getAsString());
// Placeholder for attachment
s.set("bd", "minecraft:air");
blocksSection.set("bd", "minecraft:air");
// Signs
if (bs instanceof Sign) {
Sign sign = (Sign)bs;
a.set(LINES, Arrays.asList(sign.getLines()));
if (blockState instanceof Sign) {
Sign sign = (Sign)blockState;
attachedSection.set(LINES, Arrays.asList(sign.getLines()));
}
return true;
} else {
s.set("bd", block.getBlockData().getAsString());
blocksSection.set("bd", block.getBlockData().getAsString());
// Signs
if (bs instanceof Sign) {
Sign sign = (Sign)bs;
s.set(LINES, Arrays.asList(sign.getLines()));
if (blockState instanceof Sign) {
Sign sign = (Sign)blockState;
blocksSection.set(LINES, Arrays.asList(sign.getLines()));
}
}
@ -185,25 +184,25 @@ public class Clipboard {
}
// Chests
if (bs instanceof InventoryHolder) {
InventoryHolder ih = (InventoryHolder)bs;
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) {
s.set("inventory." + index, i);
blocksSection.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());
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;
}
@ -218,18 +217,21 @@ public class Clipboard {
/**
* @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;
}
@ -253,7 +255,7 @@ public class Clipboard {
/**
* Set the clipboard contents from a YAML configuration
* @param set the blockConfig
* @param blockConfig the blockConfig
*/
public Clipboard set(YamlConfiguration blockConfig) {
this.blockConfig = blockConfig;
@ -265,14 +267,14 @@ public class Clipboard {
/**
* @param origin the origin to set
*/
public void setOrigin(Location origin) {
public void setOrigin(@Nullable Location origin) {
this.origin = origin;
}
/**
* @param pos1 the pos1 to set
*/
public void setPos1(Location pos1) {
public void setPos1(@Nullable Location pos1) {
origin = null;
this.pos1 = pos1;
}
@ -280,7 +282,7 @@ public class Clipboard {
/**
* @param pos2 the pos2 to set
*/
public void setPos2(Location pos2) {
public void setPos2(@Nullable Location pos2) {
origin = null;
this.pos2 = pos2;
}
@ -294,5 +296,4 @@ public class Clipboard {
public String toString() {
return blockConfig.saveToString();
}
}