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; 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.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@ -27,12 +22,18 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Attachable; import org.bukkit.material.Attachable;
import org.bukkit.material.Colorable; import org.bukkit.material.Colorable;
import org.bukkit.util.BoundingBox; import org.bukkit.util.BoundingBox;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; 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 * @author tastybento
* @since 1.5.0
*/ */
public class Clipboard { public class Clipboard {
@ -45,18 +46,16 @@ public class Clipboard {
private static final String LINES = "lines"; private static final String LINES = "lines";
private YamlConfiguration blockConfig; private YamlConfiguration blockConfig;
private Location pos1; private @Nullable Location pos1;
private Location pos2; private @Nullable Location pos2;
private Location origin; private @Nullable Location origin;
public Clipboard(String contents) throws InvalidConfigurationException { public Clipboard(String contents) throws InvalidConfigurationException {
super();
set(contents); set(contents);
} }
public Clipboard(YamlConfiguration config) { public Clipboard(YamlConfiguration config) {
super(); this.blockConfig = config;
blockConfig = config;
} }
public Clipboard() { 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 * @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) { public boolean copy(User user, boolean copyAir) {
if (pos1 == null || pos2 == null) { if (pos1 == null || pos2 == null) {
@ -112,42 +111,42 @@ public class Clipboard {
String pos = x + "," + y + "," + z; String pos = x + "," + y + "," + z;
// Position defines the section // Position defines the section
ConfigurationSection s = blockConfig.createSection(BLOCKS_YAML_PREFIX + "." + pos); ConfigurationSection blocksSection = blockConfig.createSection(BLOCKS_YAML_PREFIX + "." + pos);
// Set entities // Set entities
for (LivingEntity e: entities) { for (LivingEntity entity: entities) {
ConfigurationSection en = blockConfig.createSection(ENTITIES_YAML_PREFIX + pos + "." + e.getUniqueId()); ConfigurationSection entitySection = blockConfig.createSection(ENTITIES_YAML_PREFIX + pos + "." + entity.getUniqueId());
en.set("type", e.getType().name()); entitySection.set("type", entity.getType().name());
en.set("name", e.getCustomName()); entitySection.set("name", entity.getCustomName());
if (e instanceof Colorable) { if (entity instanceof Colorable) {
Colorable c = (Colorable)e; Colorable c = (Colorable)entity;
if (c.getColor() != null) { if (c.getColor() != null) {
en.set(COLOR, c.getColor().name()); entitySection.set(COLOR, c.getColor().name());
} }
} }
if (e instanceof Tameable && ((Tameable)e).isTamed()) { if (entity instanceof Tameable && ((Tameable)entity).isTamed()) {
en.set("tamed", true); entitySection.set("tamed", true);
} }
if (e instanceof ChestedHorse && ((ChestedHorse)e).isCarryingChest()) { if (entity instanceof ChestedHorse && ((ChestedHorse)entity).isCarryingChest()) {
en.set("chest", true); entitySection.set("chest", true);
} }
if (e instanceof Ageable) { if (entity instanceof Ageable) {
en.set("adult", ((Ageable)e).isAdult()); entitySection.set("adult", ((Ageable)entity).isAdult());
} }
if (e instanceof AbstractHorse) { if (entity instanceof AbstractHorse) {
AbstractHorse horse = (AbstractHorse)e; AbstractHorse horse = (AbstractHorse)entity;
en.set("domestication", horse.getDomestication()); entitySection.set("domestication", horse.getDomestication());
for (int index = 0; index < horse.getInventory().getSize(); index++) { for (int index = 0; index < horse.getInventory().getSize(); index++) {
ItemStack i = horse.getInventory().getItem(index); ItemStack i = horse.getInventory().getItem(index);
if (i != null) { if (i != null) {
en.set("inventory." + index, i); entitySection.set("inventory." + index, i);
} }
} }
} }
if (e instanceof Horse) { if (entity instanceof Horse) {
Horse horse = (Horse)e; Horse horse = (Horse)entity;
en.set("style", horse.getStyle().name()); entitySection.set("style", horse.getStyle().name());
} }
} }
@ -157,26 +156,26 @@ public class Clipboard {
} }
// Block state // Block state
BlockState bs = block.getState(); BlockState blockState = block.getState();
// Set block data // Set block data
if (bs.getData() instanceof Attachable) { if (blockState.getData() instanceof Attachable) {
ConfigurationSection a = blockConfig.createSection(ATTACHED_YAML_PREFIX + pos); ConfigurationSection attachedSection = blockConfig.createSection(ATTACHED_YAML_PREFIX + pos);
a.set("bd", block.getBlockData().getAsString()); attachedSection.set("bd", block.getBlockData().getAsString());
// Placeholder for attachment // Placeholder for attachment
s.set("bd", "minecraft:air"); blocksSection.set("bd", "minecraft:air");
// Signs // Signs
if (bs instanceof Sign) { if (blockState instanceof Sign) {
Sign sign = (Sign)bs; Sign sign = (Sign)blockState;
a.set(LINES, Arrays.asList(sign.getLines())); attachedSection.set(LINES, Arrays.asList(sign.getLines()));
} }
return true; return true;
} else { } else {
s.set("bd", block.getBlockData().getAsString()); blocksSection.set("bd", block.getBlockData().getAsString());
// Signs // Signs
if (bs instanceof Sign) { if (blockState instanceof Sign) {
Sign sign = (Sign)bs; Sign sign = (Sign)blockState;
s.set(LINES, Arrays.asList(sign.getLines())); blocksSection.set(LINES, Arrays.asList(sign.getLines()));
} }
} }
@ -185,25 +184,25 @@ public class Clipboard {
} }
// Chests // Chests
if (bs instanceof InventoryHolder) { if (blockState instanceof InventoryHolder) {
InventoryHolder ih = (InventoryHolder)bs; InventoryHolder ih = (InventoryHolder)blockState;
for (int index = 0; index < ih.getInventory().getSize(); index++) { for (int index = 0; index < ih.getInventory().getSize(); index++) {
ItemStack i = ih.getInventory().getItem(index); ItemStack i = ih.getInventory().getItem(index);
if (i != null) { if (i != null) {
s.set("inventory." + index, i); blocksSection.set("inventory." + index, i);
} }
} }
} }
if (bs instanceof CreatureSpawner) { if (blockState instanceof CreatureSpawner) {
CreatureSpawner spawner = (CreatureSpawner)bs; CreatureSpawner spawner = (CreatureSpawner)blockState;
s.set("spawnedType",spawner.getSpawnedType().name()); blocksSection.set("spawnedType",spawner.getSpawnedType().name());
s.set("delay", spawner.getDelay()); blocksSection.set("delay", spawner.getDelay());
s.set("maxNearbyEntities", spawner.getMaxNearbyEntities()); blocksSection.set("maxNearbyEntities", spawner.getMaxNearbyEntities());
s.set("maxSpawnDelay", spawner.getMaxSpawnDelay()); blocksSection.set("maxSpawnDelay", spawner.getMaxSpawnDelay());
s.set("minSpawnDelay", spawner.getMinSpawnDelay()); blocksSection.set("minSpawnDelay", spawner.getMinSpawnDelay());
s.set("requiredPlayerRange", spawner.getRequiredPlayerRange()); blocksSection.set("requiredPlayerRange", spawner.getRequiredPlayerRange());
s.set("spawnRange", spawner.getSpawnRange()); blocksSection.set("spawnRange", spawner.getSpawnRange());
} }
return true; return true;
} }
@ -218,18 +217,21 @@ public class Clipboard {
/** /**
* @return the origin * @return the origin
*/ */
@Nullable
public Location getOrigin() { public Location getOrigin() {
return origin; return origin;
} }
/** /**
* @return the pos1 * @return the pos1
*/ */
@Nullable
public Location getPos1() { public Location getPos1() {
return pos1; return pos1;
} }
/** /**
* @return the pos2 * @return the pos2
*/ */
@Nullable
public Location getPos2() { public Location getPos2() {
return pos2; return pos2;
} }
@ -253,7 +255,7 @@ public class Clipboard {
/** /**
* Set the clipboard contents from a YAML configuration * Set the clipboard contents from a YAML configuration
* @param set the blockConfig * @param blockConfig the blockConfig
*/ */
public Clipboard set(YamlConfiguration blockConfig) { public Clipboard set(YamlConfiguration blockConfig) {
this.blockConfig = blockConfig; this.blockConfig = blockConfig;
@ -265,14 +267,14 @@ public class Clipboard {
/** /**
* @param origin the origin to set * @param origin the origin to set
*/ */
public void setOrigin(Location origin) { public void setOrigin(@Nullable Location origin) {
this.origin = origin; this.origin = origin;
} }
/** /**
* @param pos1 the pos1 to set * @param pos1 the pos1 to set
*/ */
public void setPos1(Location pos1) { public void setPos1(@Nullable Location pos1) {
origin = null; origin = null;
this.pos1 = pos1; this.pos1 = pos1;
} }
@ -280,7 +282,7 @@ public class Clipboard {
/** /**
* @param pos2 the pos2 to set * @param pos2 the pos2 to set
*/ */
public void setPos2(Location pos2) { public void setPos2(@Nullable Location pos2) {
origin = null; origin = null;
this.pos2 = pos2; this.pos2 = pos2;
} }
@ -294,5 +296,4 @@ public class Clipboard {
public String toString() { public String toString() {
return blockConfig.saveToString(); return blockConfig.saveToString();
} }
} }