Fixed code smells and bugs, added flower pot protection

This commit is contained in:
tastybento 2018-07-11 22:49:20 -07:00
parent 60a0c486f3
commit 31455a033d
12 changed files with 47 additions and 74 deletions

View File

@ -211,6 +211,6 @@ public interface WorldSettings {
/** /**
* @return the visitorBannedCommands * @return the visitorBannedCommands
*/ */
public List<String> getVisitorBannedCommands(); List<String> getVisitorBannedCommands();
} }

View File

@ -44,7 +44,6 @@ public class BSBDatabase<T> {
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | ClassNotFoundException | IntrospectionException e) { | InvocationTargetException | ClassNotFoundException | IntrospectionException e) {
logger.severe(() -> "Could not load objects from database! Error: " + e.getMessage()); logger.severe(() -> "Could not load objects from database! Error: " + e.getMessage());
e.printStackTrace();
} }
return result; return result;
} }
@ -61,7 +60,6 @@ public class BSBDatabase<T> {
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| ClassNotFoundException | IntrospectionException e) { | ClassNotFoundException | IntrospectionException e) {
logger.severe(() -> "Could not load object from database! " + e.getMessage()); logger.severe(() -> "Could not load object from database! " + e.getMessage());
e.printStackTrace();
} }
return result; return result;
} }

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock.database.objects; package us.tastybento.bskyblock.database.objects;
import java.util.Date; import java.util.Date;
import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
@ -103,7 +104,7 @@ public class Island implements DataObject {
@Expose @Expose
private int levelHandicap; private int levelHandicap;
@Expose @Expose
private Map<Environment, Location> spawnPoint = new HashMap<>(); private Map<Environment, Location> spawnPoint = new EnumMap<>(Environment.class);
public Island() {} public Island() {}
public Island(Location location, UUID owner, int protectionRange) { public Island(Location location, UUID owner, int protectionRange) {

View File

@ -45,7 +45,7 @@ public class FlagSerializer2 implements AdapterInterface<Map<Flag, Integer>, Map
} }
Map<Flag, Integer> flags = (Map<Flag, Integer>)object; Map<Flag, Integer> flags = (Map<Flag, Integer>)object;
for (Entry<Flag, Integer> en: flags.entrySet()) { for (Entry<Flag, Integer> en: flags.entrySet()) {
result.put(en.getKey().getID(), en.getValue() < 0 ? false : true); result.put(en.getKey().getID(), en.getValue() >= 0);
} }
return result; return result;
} }

View File

@ -21,7 +21,6 @@ import org.bukkit.Bukkit;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Nameable;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Banner; import org.bukkit.block.Banner;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -83,6 +82,14 @@ public class Clipboard {
private static final String LOAD_ERROR = "Could not load schems file - does not exist : "; private static final String LOAD_ERROR = "Could not load schems file - does not exist : ";
private static final String BEDROCK = "bedrock";
private static final String INVENTORY = "inventory";
private static final String ENTITY = "entity";
private static final String COLOR = "color";
private YamlConfiguration blockConfig = new YamlConfiguration(); private YamlConfiguration blockConfig = new YamlConfiguration();
private Location pos1; private Location pos1;
private Location pos2; private Location pos2;
@ -199,8 +206,8 @@ public class Clipboard {
public void paste(World world, Island island, Runnable task) { public void paste(World world, Island island, Runnable task) {
// Offset due to bedrock // Offset due to bedrock
Vector off = new Vector(0,0,0); Vector off = new Vector(0,0,0);
if (blockConfig.contains("bedrock")) { if (blockConfig.contains(BEDROCK)) {
String[] offset = blockConfig.getString("bedrock").split(","); String[] offset = blockConfig.getString(BEDROCK).split(",");
off = new Vector(Integer.valueOf(offset[0]), Integer.valueOf(offset[1]), Integer.valueOf(offset[2])); off = new Vector(Integer.valueOf(offset[0]), Integer.valueOf(offset[1]), Integer.valueOf(offset[2]));
} }
// Calculate location for pasting // Calculate location for pasting
@ -302,7 +309,6 @@ public class Clipboard {
if (md instanceof Directional) { if (md instanceof Directional) {
Directional facing = (Directional)md; Directional facing = (Directional)md;
if (md instanceof Stairs) { if (md instanceof Stairs) {
//facing.setFacingDirection(BlockFace.valueOf(s.getString(FACING)).getOppositeFace());
Stairs stairs = (Stairs)md; Stairs stairs = (Stairs)md;
stairs.setInverted(config.getBoolean("inverted")); stairs.setInverted(config.getBoolean("inverted"));
stairs.setFacingDirection(BlockFace.valueOf(config.getString(FACING, "NORTH"))); stairs.setFacingDirection(BlockFace.valueOf(config.getString(FACING, "NORTH")));
@ -359,26 +365,24 @@ public class Clipboard {
if (bs instanceof InventoryHolder) { if (bs instanceof InventoryHolder) {
bs.update(true, false); bs.update(true, false);
Inventory ih = ((InventoryHolder)bs).getInventory(); Inventory ih = ((InventoryHolder)bs).getInventory();
if (config.isConfigurationSection("inventory")) { if (config.isConfigurationSection(INVENTORY)) {
ConfigurationSection inv = config.getConfigurationSection("inventory"); ConfigurationSection inv = config.getConfigurationSection(INVENTORY);
inv.getKeys(false).forEach(i -> ih.setItem(Integer.valueOf(i), (ItemStack)inv.get(i))); inv.getKeys(false).forEach(i -> ih.setItem(Integer.valueOf(i), (ItemStack)inv.get(i)));
} }
} }
// Entities // Entities
if (config.isConfigurationSection("entity")) { if (config.isConfigurationSection(ENTITY)) {
ConfigurationSection en = config.getConfigurationSection("entity"); ConfigurationSection en = config.getConfigurationSection(ENTITY);
en.getKeys(false).forEach(k -> { en.getKeys(false).forEach(k -> {
ConfigurationSection ent = en.getConfigurationSection(k); ConfigurationSection ent = en.getConfigurationSection(k);
Location center = block.getLocation().add(new Vector(0.5, 0.0, 0.5)); 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"))); LivingEntity e = (LivingEntity)block.getWorld().spawnEntity(center, EntityType.valueOf(ent.getString("type", "PIG")));
if (e instanceof Nameable) { if (e != null) {
e.setCustomName(ent.getString("name")); e.setCustomName(ent.getString("name"));
} }
if (e instanceof Colorable) { if (e instanceof Colorable && ent.contains(COLOR)) {
if (ent.contains("color")) { ((Colorable) e).setColor(DyeColor.valueOf(ent.getString(COLOR)));
((Colorable) e).setColor(DyeColor.valueOf(ent.getString("color")));
}
} }
if (e instanceof Tameable) { if (e instanceof Tameable) {
((Tameable)e).setTamed(ent.getBoolean("tamed")); ((Tameable)e).setTamed(ent.getBoolean("tamed"));
@ -396,7 +400,7 @@ public class Clipboard {
if (e instanceof AbstractHorse) { if (e instanceof AbstractHorse) {
AbstractHorse horse = (AbstractHorse)e; AbstractHorse horse = (AbstractHorse)e;
horse.setDomestication(ent.getInt("domestication")); horse.setDomestication(ent.getInt("domestication"));
ConfigurationSection inv = ent.getConfigurationSection("inventory"); ConfigurationSection inv = ent.getConfigurationSection(INVENTORY);
inv.getKeys(false).forEach(i -> horse.getInventory().setItem(Integer.valueOf(i), (ItemStack)inv.get(i))); inv.getKeys(false).forEach(i -> horse.getInventory().setItem(Integer.valueOf(i), (ItemStack)inv.get(i)));
} }
@ -427,12 +431,10 @@ public class Clipboard {
for (LivingEntity e: entities) { for (LivingEntity e: entities) {
ConfigurationSection en = s.createSection("entity." + e.getUniqueId()); ConfigurationSection en = s.createSection("entity." + e.getUniqueId());
en.set("type", e.getType().name()); en.set("type", e.getType().name());
if (e instanceof Nameable) { en.set("name", e.getCustomName());
en.set("name", e.getCustomName());
}
if (e instanceof Colorable) { if (e instanceof Colorable) {
Colorable c = (Colorable)e; Colorable c = (Colorable)e;
en.set("color", c.getColor().name()); en.set(COLOR, c.getColor().name());
} }
if (e instanceof Tameable && ((Tameable)e).isTamed()) { if (e instanceof Tameable && ((Tameable)e).isTamed()) {
en.set("tamed", true); en.set("tamed", true);
@ -471,7 +473,7 @@ public class Clipboard {
s.set("data", block.getData()); s.set("data", block.getData());
} }
if (block.getType().equals(Material.BEDROCK)) { if (block.getType().equals(Material.BEDROCK)) {
blockConfig.set("bedrock", x + "," + y + "," + z); blockConfig.set(BEDROCK, x + "," + y + "," + z);
} }
// Block state // Block state
@ -485,7 +487,6 @@ public class Clipboard {
} }
if (md instanceof Directional) { if (md instanceof Directional) {
if (md instanceof Stairs) { if (md instanceof Stairs) {
//facing.setFacingDirection(BlockFace.valueOf(s.getString(FACING)).getOppositeFace());
Stairs stairs = (Stairs)md; Stairs stairs = (Stairs)md;
s.set("inverted", stairs.isInverted()); s.set("inverted", stairs.isInverted());
s.set(FACING, stairs.getAscendingDirection().name()); s.set(FACING, stairs.getAscendingDirection().name());
@ -502,11 +503,11 @@ public class Clipboard {
} }
if (md instanceof Colorable) { if (md instanceof Colorable) {
Colorable c = (Colorable)md; Colorable c = (Colorable)md;
s.set("color", c.getColor().name()); s.set(COLOR, c.getColor().name());
} }
if (block.getType().equals(Material.CARPET)) { if (block.getType().equals(Material.CARPET)) {
DyeColor c = DyeColor.getByWoolData(block.getData()); DyeColor c = DyeColor.getByWoolData(block.getData());
s.set("color", c.name()); s.set(COLOR, c.name());
} }
if (md instanceof Redstone) { if (md instanceof Redstone) {
Redstone r = (Redstone)md; Redstone r = (Redstone)md;

View File

@ -1,6 +1,3 @@
/**
*
*/
package us.tastybento.bskyblock.listeners; package us.tastybento.bskyblock.listeners;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -22,7 +19,7 @@ public class BannedVisitorCommands implements Listener {
private BSkyBlock plugin; private BSkyBlock plugin;
/** /**
* @param plugin * @param plugin - plugin
*/ */
public BannedVisitorCommands(BSkyBlock plugin) { public BannedVisitorCommands(BSkyBlock plugin) {
this.plugin = plugin; this.plugin = plugin;

View File

@ -1,7 +1,5 @@
package us.tastybento.bskyblock.listeners; package us.tastybento.bskyblock.listeners;
import java.util.Objects;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@ -119,7 +117,7 @@ public class NetherPortals implements Listener {
if (plugin.getIWM().isEndGenerate(overWorld) && plugin.getIWM().isEndIslands(overWorld)) { if (plugin.getIWM().isEndGenerate(overWorld) && plugin.getIWM().isEndIslands(overWorld)) {
World endWorld = plugin.getIWM().getEndWorld(overWorld); World endWorld = plugin.getIWM().getEndWorld(overWorld);
// End exists and end islands are being used // End exists and end islands are being used
Location to = plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.THE_END)).filter(Objects::nonNull).orElse(e.getFrom().toVector().toLocation(endWorld)); Location to = plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.THE_END)).orElse(e.getFrom().toVector().toLocation(endWorld));
e.setCancelled(true); e.setCancelled(true);
new SafeTeleportBuilder(plugin) new SafeTeleportBuilder(plugin)
.entity(e.getPlayer()) .entity(e.getPlayer())
@ -179,7 +177,7 @@ public class NetherPortals implements Listener {
if (e.getFrom().getWorld().getEnvironment().equals(Environment.NETHER)) { if (e.getFrom().getWorld().getEnvironment().equals(Environment.NETHER)) {
// If this is from the island nether, then go to the same vector, otherwise try island home location // If this is from the island nether, then go to the same vector, otherwise try island home location
Location to = plugin.getIWM().isNetherIslands(overWorld) Location to = plugin.getIWM().isNetherIslands(overWorld)
? plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NORMAL)).filter(Objects::nonNull).orElse(e.getFrom().toVector().toLocation(overWorld)) ? plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NORMAL)).orElse(e.getFrom().toVector().toLocation(overWorld))
: plugin.getIslands().getIslandLocation(overWorld, e.getPlayer().getUniqueId()); : plugin.getIslands().getIslandLocation(overWorld, e.getPlayer().getUniqueId());
e.setCancelled(true); e.setCancelled(true);
@ -194,7 +192,7 @@ public class NetherPortals implements Listener {
World nether = plugin.getIWM().getNetherWorld(overWorld); World nether = plugin.getIWM().getNetherWorld(overWorld);
// If this is to island nether, then go to the same vector, otherwise try spawn // If this is to island nether, then go to the same vector, otherwise try spawn
Location to = (plugin.getIWM().isNetherIslands(overWorld) && plugin.getIWM().isNetherGenerate(overWorld)) Location to = (plugin.getIWM().isNetherIslands(overWorld) && plugin.getIWM().isNetherGenerate(overWorld))
? plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NETHER)).filter(Objects::nonNull).orElse(e.getFrom().toVector().toLocation(nether)) ? plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NETHER)).orElse(e.getFrom().toVector().toLocation(nether))
: nether.getSpawnLocation(); : nether.getSpawnLocation();
e.setCancelled(true); e.setCancelled(true);
// Else other worlds teleport to the nether // Else other worlds teleport to the nether

View File

@ -1,5 +1,7 @@
package us.tastybento.bskyblock.listeners.flags; package us.tastybento.bskyblock.listeners.flags;
import org.bukkit.Material;
import org.bukkit.block.FlowerPot;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
@ -124,40 +126,19 @@ public class BlockInteractionListener extends AbstractFlagListener {
case REDSTONE_COMPARATOR: case REDSTONE_COMPARATOR:
checkIsland(e, e.getClickedBlock().getLocation(), Flags.REDSTONE); checkIsland(e, e.getClickedBlock().getLocation(), Flags.REDSTONE);
break; break;
case ARMOR_STAND:
this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType());
break;
case BANNER:
this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType());
break;
case BED:
this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType());
break;
case BOAT:
case BOAT_ACACIA:
case BOAT_BIRCH:
case BOAT_DARK_OAK:
case BOAT_JUNGLE:
case BOAT_SPRUCE:
this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType());
break;
case DRAGON_EGG: case DRAGON_EGG:
checkIsland(e, e.getClickedBlock().getLocation(), Flags.BREAK_BLOCKS); checkIsland(e, e.getClickedBlock().getLocation(), Flags.BREAK_BLOCKS);
break; break;
case ENDER_PORTAL_FRAME: case ENDER_PORTAL_FRAME:
this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); checkIsland(e, e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS);
break; break;
case FLOWER_POT: case FLOWER_POT:
this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); FlowerPot pot = (FlowerPot) e.getClickedBlock().getState();
break; if (pot.getContents() == null || pot.getContents().getItemType().equals(Material.AIR)) {
case MOB_SPAWNER: checkIsland(e, e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS);
this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); } else {
break; checkIsland(e, e.getClickedBlock().getLocation(), Flags.BREAK_BLOCKS);
case OBSERVER: }
this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType());
break;
case POWERED_MINECART:
this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType());
break; break;
default: default:
break; break;

View File

@ -18,6 +18,7 @@ import org.bukkit.event.vehicle.VehicleDamageEvent;
import org.bukkit.util.BlockIterator; import org.bukkit.util.BlockIterator;
import us.tastybento.bskyblock.api.flags.AbstractFlagListener; import us.tastybento.bskyblock.api.flags.AbstractFlagListener;
import us.tastybento.bskyblock.api.localization.TextVariables;
import us.tastybento.bskyblock.api.user.User; import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
@ -100,14 +101,14 @@ public class BreakBlocksListener extends AbstractFlagListener {
getIslands().getIslandAt(e.getVehicle().getLocation()).ifPresent(x -> { getIslands().getIslandAt(e.getVehicle().getLocation()).ifPresent(x -> {
if (!x.isAllowed(user, Flags.BREAK_BLOCKS)) { if (!x.isAllowed(user, Flags.BREAK_BLOCKS)) {
e.setCancelled(true); e.setCancelled(true);
user.sendMessage("protection.protected"); user.notify("protection.protected", TextVariables.DESCRIPTION, user.getTranslation(Flags.BREAK_BLOCKS.getHintReference()));
} }
}); });
// The player is in the world, but not on an island, so general world settings apply // The player is in the world, but not on an island, so general world settings apply
if (!Flags.BREAK_BLOCKS.isSetForWorld(e.getVehicle().getWorld())) { if (!Flags.BREAK_BLOCKS.isSetForWorld(e.getVehicle().getWorld())) {
e.setCancelled(true); e.setCancelled(true);
user.sendMessage("protection.protected"); user.notify("protection.protected", TextVariables.DESCRIPTION, user.getTranslation(Flags.BREAK_BLOCKS.getHintReference()));
} }
} }
} }

View File

@ -704,7 +704,6 @@ public class IslandsManager {
handler.saveObject(island); handler.saveObject(island);
} catch (Exception e) { } catch (Exception e) {
plugin.logError("Could not save island to database when running async! " + e.getMessage()); plugin.logError("Could not save island to database when running async! " + e.getMessage());
e.printStackTrace();
} }
} }
midSave = false; midSave = false;

View File

@ -82,7 +82,6 @@ public class SchemsManager {
islandSchems.put(world, cb); islandSchems.put(world, cb);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
plugin.logError("Could not load " + name + " schem"); plugin.logError("Could not load " + name + " schem");
e.printStackTrace();
return false; return false;
} }
return true; return true;

View File

@ -190,9 +190,7 @@ public class NewIsland {
// Find a free spot // Find a free spot
Map<Result, Integer> result = new EnumMap<>(Result.class); Map<Result, Integer> result = new EnumMap<>(Result.class);
Result r = isIsland(last); Result r = isIsland(last);
while (!r.equals(Result.FREE) while (!r.equals(Result.FREE) && result.getOrDefault(Result.BLOCK_AT_CENTER, 0) < MAX_UNOWNED_ISLANDS) {
&& (result.getOrDefault(Result.BLOCK_AT_CENTER, 0) < MAX_UNOWNED_ISLANDS
&& result.getOrDefault(Result.BLOCK_AT_CENTER, 0) < MAX_UNOWNED_ISLANDS)) {
last = nextGridLocation(last); last = nextGridLocation(last);
result.merge(r, 1, (k,v) -> v++); result.merge(r, 1, (k,v) -> v++);
r = isIsland(last); r = isIsland(last);