Fixed some code smells

This commit is contained in:
Florian CUNY 2018-04-25 13:48:58 +02:00
parent 135f35131c
commit ecb630fac3
23 changed files with 88 additions and 146 deletions

View File

@ -63,7 +63,6 @@ public class BSkyBlock extends JavaPlugin {
settings = new Settings();
// Load settings from config.yml. This will check if there are any issues with it too.
try {
//settings.saveSettings();
settings = settings.loadSettings();
} catch (Exception e) {
getLogger().severe("Settings could not be loaded " + e.getMessage());
@ -282,8 +281,6 @@ public class BSkyBlock extends JavaPlugin {
return notifier;
}
/**
* @return the headGetter
*/

View File

@ -1,8 +1,8 @@
package us.tastybento.bskyblock;
/**
* All the plugin constants are here
* @author Tastybento
* Contains the plugin constants.
* @author tastybento
*/
public class Constants {
// ----------------- Constants -----------------
@ -10,17 +10,7 @@ public class Constants {
public enum GameType {
BSKYBLOCK, ACIDISLAND, BOTH
}
/*
public static final GameType GAMETYPE = GameType.ACIDISLAND;
// The spawn command (Essentials spawn for example)
public static final String SPAWNCOMMAND = "spawn";
// Permission prefix
public static final String PERMPREFIX = "acidisland.";
// The island command
public static final String ISLANDCOMMAND = "ai";
// Admin command
public static final String ADMINCOMMAND = "acid";
*/
public static final GameType GAMETYPE = GameType.BSKYBLOCK;
// Permission prefix
public static final String PERMPREFIX = "bskyblock.";

View File

@ -404,7 +404,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
if (cmd.hasSubCommmands()) {
// Check if subcommands are visible to this sender
for (CompositeCommand subCommand: cmd.getSubCommands().values()) {
if ((sender instanceof Player)) {
if (sender instanceof Player) {
// Player
if (subCommand.getPermission().isEmpty() || sender.hasPermission(subCommand.getPermission())) {
// Permission is okay
@ -419,7 +419,8 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
}
}
}
String lastArg = (args.length != 0 ? args[args.length - 1] : "");
String lastArg = args.length != 0 ? args[args.length - 1] : "";
return Util.tabLimit(options, lastArg);
}

View File

@ -9,7 +9,7 @@ public abstract class PremadeEvent extends Event {
@Override
public HandlerList getHandlers() {
return handlers;
return getHandlerList();
}
public static HandlerList getHandlerList() {

View File

@ -14,7 +14,7 @@ public class EntityDamageByAcidEvent extends IslandBaseEvent {
private final Entity entity;
private double damage;
public enum Acid { RAIN, WATER };
public enum Acid { RAIN, WATER }
private final Acid cause;
public EntityDamageByAcidEvent(Island island, Entity entity, double damage, Acid cause) {

View File

@ -28,7 +28,7 @@ public class IslandEvent {
RESETTED,
UNLOCK,
UNKNOWN
};
}
public static IslandEventBuilder builder() {
return new IslandEventBuilder();

View File

@ -26,7 +26,7 @@ public class TeamEvent {
DELETE,
UNKNOWN,
UNINVITE
};
}
public static TeamEventBuilder builder() {
return new TeamEventBuilder();

View File

@ -99,10 +99,8 @@ public class Flag implements Comparable<Flag> {
} else if (!id.equals(other.id)) {
return false;
}
if (type != other.type) {
return false;
}
return true;
return type == other.type;
}
public PanelItem toPanelItem(User user) {

View File

@ -28,7 +28,6 @@ public class PanelItem {
public PanelItem(ItemStack icon, String name, List<String> description, boolean glow, ClickHandler clickHandler, boolean playerHead) {
this.icon = icon;
this.playerHead = playerHead;
setMeta();
// Get the meta
meta = icon.getItemMeta();
@ -48,11 +47,6 @@ public class PanelItem {
icon.setItemMeta(meta);
}
private void setMeta() {
// TODO Auto-generated method stub
}
public ItemStack getItem() {
return icon;
}
@ -117,7 +111,6 @@ public class PanelItem {
public void setHead(ItemStack itemStack) {
this.icon = itemStack;
setMeta();
// Get the meta
meta = icon.getItemMeta();
// Create the final item

View File

@ -16,7 +16,7 @@ public class Notifier {
/**
* Time in seconds before {@link #notificationCache} removes the entry related to the player.
*/
private final int NOTIFICATION_DELAY = 5;
private static final int NOTIFICATION_DELAY = 5;
private final LoadingCache<User, Notification> notificationCache = CacheBuilder.newBuilder()
.expireAfterAccess(NOTIFICATION_DELAY, TimeUnit.SECONDS)

View File

@ -50,7 +50,7 @@ public abstract class AbstractDatabaseHandler<T> {
* Loads all the records in this table and returns a list of them
* @return list of <T>
*/
public abstract List<T> loadObjects() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, IntrospectionException;
public abstract List<T> loadObjects() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException;
/**
* Creates a <T> filled with values from the corresponding
@ -58,20 +58,20 @@ public abstract class AbstractDatabaseHandler<T> {
* @param uniqueId - unique ID
* @return <T>
*/
public abstract T loadObject(String uniqueId) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, IntrospectionException;
public abstract T loadObject(String uniqueId) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException;
/**
* Save T into the corresponding database
*
* @param instance that should be inserted into the database
*/
public abstract void saveObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException ;
public abstract void saveObject(T instance) throws IllegalAccessException, InvocationTargetException, IntrospectionException ;
/**
* Deletes the object with the unique id from the database
* @param instance
*/
public abstract void deleteObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException ;
public abstract void deleteObject(T instance) throws IllegalAccessException, InvocationTargetException, IntrospectionException ;
/**
* Checks if a key exists or not

View File

@ -21,12 +21,12 @@ public class ConfigHandler<T> extends FlatFileDatabaseHandler<T> {
super(plugin, type, databaseConnecter);
}
public void saveSettings(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
public void saveSettings(T instance) throws IllegalAccessException, InvocationTargetException, IntrospectionException {
configFlag = true;
saveObject(instance);
}
public T loadSettings(String uniqueId, T dbConfig) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, IntrospectionException {
public T loadSettings(String uniqueId, T dbConfig) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException {
if (dbConfig == null) {
return loadObject(uniqueId);
}

View File

@ -8,7 +8,7 @@ public class FlatFileDatabase extends BSBDbSetup{
@Override
public AbstractDatabaseHandler<?> getHandler(Class<?> type) {
return new FlatFileDatabaseHandler<>(BSkyBlock.getInstance(), type, new FlatFileDatabaseConnecter(BSkyBlock.getInstance(), null));
return new FlatFileDatabaseHandler<>(BSkyBlock.getInstance(), type, new FlatFileDatabaseConnecter(BSkyBlock.getInstance()));
}
/**
@ -17,7 +17,7 @@ public class FlatFileDatabase extends BSBDbSetup{
* @return - the config handler
*/
public AbstractDatabaseHandler<?> getConfig(Class<?> type) {
return new ConfigHandler<>(BSkyBlock.getInstance(), type, new FlatFileDatabaseConnecter(BSkyBlock.getInstance(), null));
return new ConfigHandler<>(BSkyBlock.getInstance(), type, new FlatFileDatabaseConnecter(BSkyBlock.getInstance()));
}
}

View File

@ -27,7 +27,7 @@ public class FlatFileDatabaseConnecter implements DatabaseConnecter {
private File dataFolder;
public FlatFileDatabaseConnecter(Plugin plugin, DatabaseConnectionSettingsImpl databaseConnectionSettingsImpl) {
public FlatFileDatabaseConnecter(Plugin plugin) {
this.plugin = plugin;
dataFolder = new File(plugin.getDataFolder(), DATABASE_FOLDER_NAME);
}

View File

@ -194,13 +194,6 @@ public class Island implements DataObject {
return levelHandicap;
}
/**
* @return true if the island is locked, otherwise false
*/
public boolean getLocked(){
return locked;
}
/**
* @return the members
*/
@ -294,13 +287,6 @@ public class Island implements DataObject {
return members.getOrDefault(user.getUniqueId(), RanksManager.VISITOR_RANK);
}
/**
* @return true if the island is the spawn otherwise false
*/
public boolean getSpawn(){
return spawn;
}
public Location getSpawnPoint() {
return spawnPoint;
}
@ -326,10 +312,8 @@ public class Island implements DataObject {
if (holder.getType().equals(Material.BURNING_FURNACE)) {
result++;
}
} else if (material.toString().endsWith("BANNER")) {
if (holder.getType().toString().endsWith("BANNER")) {
result++;
}
} else if (material.toString().endsWith("BANNER") && holder.getType().toString().endsWith("BANNER")) {
result++;
} else if (material.equals(Material.WALL_SIGN) || material.equals(Material.SIGN_POST)) {
if (holder.getType().equals(Material.WALL_SIGN) || holder.getType().equals(Material.SIGN_POST)) {
result++;
@ -519,9 +503,7 @@ public class Island implements DataObject {
* Resets the flags to their default as set in config.yml for this island
*/
public void setFlagsDefaults(){
/*for(SettingsFlag flag : SettingsFlag.values()){
this.flags.put(flag, Settings.defaultIslandSettings.get(flag));
}*/ //TODO default flags
//TODO default flags
}
/**
@ -650,9 +632,7 @@ public class Island implements DataObject {
* Resets the flags to their default as set in config.yml for the spawn
*/
public void setSpawnFlagsDefaults(){
/*for(SettingsFlag flag : SettingsFlag.values()){
this.flags.put(flag, Settings.defaultSpawnSettings.get(flag));
}*/ //TODO default flags
//TODO default flags
}
public void setSpawnPoint(Location location) {

View File

@ -232,7 +232,6 @@ public class Players implements DataObject {
Date kickedDate = new Date(kickedList.get(location));
Calendar coolDownTime = Calendar.getInstance();
coolDownTime.setTime(kickedDate);
// coolDownTime.add(Calendar.HOUR_OF_DAY, Settings.inviteWait);
coolDownTime.add(Calendar.MINUTE, getPlugin().getSettings().getInviteWait());
// Add the invite cooldown period
Calendar timeNow = Calendar.getInstance();

View File

@ -40,13 +40,13 @@ public class IslandBuilder {
private Island island;
private World world;
private IslandType type = IslandType.ISLAND;
//private List<String> companionNames = new ArrayList<>();
private List<ItemStack> chestItems;
//private List<Entity> companions = new ArrayList<>();
private UUID playerUUID;
private String playerName;
private BSkyBlock plugin;
//TODO support companions?
public IslandBuilder(BSkyBlock plugin, Island island) {
this.plugin = plugin;
this.island = island;
@ -208,8 +208,6 @@ public class IslandBuilder {
// Add tree (natural)
Location treeLoc = new Location(world, x, y + 5D, z);
world.generateTree(treeLoc, TreeType.ACACIA);
// Place the cow
//Location location = new Location(world, x, (islandHeight + 5), z - 2);
// Place a helpful sign in front of player
placeSign(x, islandHeight + 5, z + 3);
@ -295,8 +293,6 @@ public class IslandBuilder {
// Add tree (natural)
Location treeLoc = new Location(world, x, y + 5D, z);
world.generateTree(treeLoc, TreeType.TREE);
// Place the cow
//Location location = new Location(world, x, (islandHeight + 5), z - 2);
// Place a helpful sign in front of player
placeSign(x, islandHeight + 5, z + 3);
@ -373,18 +369,11 @@ public class IslandBuilder {
// Add island items
y = islandHeight;
// Add tree (natural)
Location treeLoc = new Location(world, x, y + 5D, z);
treeLoc.getBlock().getRelative(BlockFace.DOWN).setType(Material.DIRT);
world.generateTree(treeLoc, TreeType.TREE);
// Place the cow
//Location location = new Location(world, x, (islandHeight + 5), z - 2);
// Place a helpful sign in front of player
placeSign(x, islandHeight + 5, z + 3);
// Place the chest - no need to use the safe spawn function
// because we
// know what this island looks like
// because we know what this island looks like
placeChest(x, islandHeight + 5, z + 1);
}
@ -456,12 +445,8 @@ public class IslandBuilder {
// Add island items
y = islandHeight;
// Add tree (natural)
Location treeLoc = new Location(world, x, y + 5D, z);
world.spawnEntity(treeLoc, EntityType.ENDER_CRYSTAL);
//world.generateTree(treeLoc, TreeType.TREE);
// Place the cow
//Location location = new Location(world, x, (islandHeight + 5), z - 2);
// Spawn an ender crystal
world.spawnEntity(new Location(world, x, y + 5D, z), EntityType.ENDER_CRYSTAL);
// Place a helpful sign in front of player
placeSign(x, islandHeight + 5, z + 3);

View File

@ -16,6 +16,9 @@ import org.bukkit.event.player.PlayerInteractEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.user.User;
import java.util.ArrayList;
import java.util.List;
/**
* Enables changing of obsidian back into lava
* @author tastybento
@ -25,7 +28,6 @@ public class ObsidianToLava implements Listener {
private BSkyBlock plugin;
/**
* @param plugin
*/
@ -33,7 +35,6 @@ public class ObsidianToLava implements Listener {
this.plugin = plugin;
}
/**
* Enables changing of obsidian back into lava
*
@ -54,18 +55,14 @@ public class ObsidianToLava implements Listener {
if (plugin.getIslands().playerIsOnIsland(user)) {
// Look around to see if this is a lone obsidian block
Block b = e.getClickedBlock();
for (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 2; y++) {
for (int z = -2; z <= 2; z++) {
Material testBlock = b.getWorld().getBlockAt(b.getX() + x, b.getY() + y, b.getZ() + z).getType();
if ((x != 0 || y != 0 || z != 0) && testBlock.equals(Material.OBSIDIAN)) {
// Do nothing special
return false;
}
}
for (Block testBlock : getBlocksAround(b)) {
if (testBlock.getType().equals(Material.OBSIDIAN)) {
// Do nothing special
return false;
}
}
user.sendMessage("general.tips.changing-ob-to-lava");
e.getItem().setType(Material.LAVA_BUCKET);
e.getPlayer().getWorld().playSound(e.getPlayer().getLocation(), Sound.ITEM_BUCKET_FILL_LAVA, 1F, 1F);
@ -76,4 +73,20 @@ public class ObsidianToLava implements Listener {
return false;
}
private List<Block> getBlocksAround(Block b) {
List<Block> blocksAround = new ArrayList<>();
for (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 2; y++) {
for (int z = -2; z <= 2; z++) {
blocksAround.add(b.getWorld().getBlockAt(b.getX() + x, b.getY() + y, b.getZ() + z));
}
}
}
// Remove the block at x = 0, y = 0 and z = 0 (which is the base block)
blocksAround.remove(b);
return blocksAround;
}
}

View File

@ -68,7 +68,7 @@ public class FlyingMobEvents implements Listener {
* @param e - event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void mobSpawn(CreatureSpawnEvent e) {
public void onMobSpawn(CreatureSpawnEvent e) {
// Only cover withers in the island world
if (!Util.inWorld(e.getEntity())) {
return;
@ -81,7 +81,7 @@ public class FlyingMobEvents implements Listener {
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void mobExplosion(EntityExplodeEvent e) {
public void onMobExplosion(EntityExplodeEvent e) {
// Only cover in the island world
if (e.getEntity() == null || !Util.inWorld(e.getEntity())) {
return;
@ -100,7 +100,7 @@ public class FlyingMobEvents implements Listener {
* Deal with pre-explosions
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void witherExplode(ExplosionPrimeEvent e) {
public void onWitherExplode(ExplosionPrimeEvent e) {
// Only cover withers in the island world
if (!Util.inWorld(e.getEntity()) || e.getEntity() == null) {
return;
@ -115,7 +115,6 @@ public class FlyingMobEvents implements Listener {
e.setCancelled(true);
}
}
// Testing only e.setCancelled(true);
}
if (e.getEntityType() == EntityType.WITHER_SKULL) {
// Get shooter
@ -140,7 +139,7 @@ public class FlyingMobEvents implements Listener {
* @param e - event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void witherChangeBlocks(EntityChangeBlockEvent e) {
public void onWitherChangeBlocks(EntityChangeBlockEvent e) {
// Only cover withers in the island world
if (e.getEntityType() != EntityType.WITHER || !Util.inWorld(e.getEntity()) ) {
return;
@ -158,7 +157,7 @@ public class FlyingMobEvents implements Listener {
* Clean up the hashmap. It's probably not needed, but just in case.
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void mobDeath(EntityDeathEvent e) {
public void onMobDeath(EntityDeathEvent e) {
mobSpawnInfo.remove(e.getEntity());
}
}

View File

@ -117,12 +117,12 @@ public final class AddonsManager {
YamlConfiguration data = new YamlConfiguration();
data.load(reader);
// Load the addon
AddonClassLoader loader = new AddonClassLoader(this, data, f, this.getClass().getClassLoader());
AddonClassLoader addonClassLoader = new AddonClassLoader(this, data, f, this.getClass().getClassLoader());
// Add to the list of loaders
this.loader.add(loader);
loader.add(addonClassLoader);
// Get the addon itself
addon = loader.getAddon();
addon = addonClassLoader.getAddon();
// Initialize some settings
addon.setDataFolder(new File(f.getParent(), addon.getDescription().getName()));
addon.setAddonFile(f);
@ -163,12 +163,12 @@ public final class AddonsManager {
addons.forEach(addon -> {
addon.onDisable();
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build());
System.out.println("Disabling " + addon.getDescription().getName() + "...");
plugin.getLogger().info("Disabling " + addon.getDescription().getName() + "...");
});
loader.forEach(loader -> {
loader.forEach(l -> {
try {
loader.close();
l.close();
} catch (IOException e) {
// Do nothing
}
@ -187,7 +187,6 @@ public final class AddonsManager {
this.loader = loader;
}
/**
* Finds a class by name that has been loaded by this loader
* Code copied from Bukkit JavaPluginLoader
@ -200,9 +199,9 @@ public final class AddonsManager {
if (cachedClass != null) {
return cachedClass;
} else {
for (AddonClassLoader loader : loader) {
for (AddonClassLoader l : loader) {
try {
cachedClass = loader.findClass(name, false);
cachedClass = l.findClass(name, false);
} catch (ClassNotFoundException cnfe) {}
if (cachedClass != null) {
return cachedClass;
@ -237,7 +236,7 @@ public final class AddonsManager {
* @return a list of files
- if the file cannot be read
*/
public List<String> listJarYamlFiles(JarFile jar, String folderPath) throws IOException {
public List<String> listJarYamlFiles(JarFile jar, String folderPath) {
List<String> result = new ArrayList<>();
/**

View File

@ -727,14 +727,12 @@ public class IslandsManager {
homeTeleport(player);
} else {
// Move player to spawn
Island spawn = getSpawn();
if (spawn != null) {
// go to island spawn
player.teleport(plugin.getIslandWorldManager().getIslandWorld().getSpawnLocation());
} else {
if (!player.performCommand(Constants.SPAWNCOMMAND)) {
plugin.getLogger().warning(()->
"During island deletion player " + player.getName() + " could not be sent to spawn so was dropped, sorry.");
plugin.getLogger().warning(()-> "During island deletion player " + player.getName() + " could not be sent to spawn so was dropped, sorry.");
}
}
}

View File

@ -59,10 +59,7 @@ public class LocalesManager {
// Describe the filter - we only want files that are correctly named
FilenameFilter ymlFilter = (dir, name) -> {
// Files must be 9 chars long
if (name.toLowerCase().endsWith(".yml") && name.length() == 9) {
return true;
}
return false;
return name.toLowerCase().endsWith(".yml") && name.length() == 9;
};
// Run through the files and store the locales

View File

@ -74,29 +74,24 @@ public class SafeSpotTeleport {
checking = true;
// Start a recurring task until done or cancelled
task = plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() {
@Override
public void run() {
List<ChunkSnapshot> chunkSnapshot = new ArrayList<>();
if (checking) {
Iterator<Pair<Integer, Integer>> it = chunksToScan.iterator();
if (!it.hasNext()) {
// Nothing left
tidyUp(entity, failureMessage);
return;
}
// Add chunk snapshots to the list
while (it.hasNext() && chunkSnapshot.size() < MAX_CHUNKS) {
Pair<Integer, Integer> pair = it.next();
chunkSnapshot.add(location.getWorld().getChunkAt(pair.x, pair.z).getChunkSnapshot());
it.remove();
}
// Move to next step
checking = false;
checkChunks(chunkSnapshot);
task = plugin.getServer().getScheduler().runTaskTimer(plugin, () -> {
List<ChunkSnapshot> chunkSnapshot = new ArrayList<>();
if (checking) {
Iterator<Pair<Integer, Integer>> it = chunksToScan.iterator();
if (!it.hasNext()) {
// Nothing left
tidyUp(entity, failureMessage);
return;
}
// Add chunk snapshots to the list
while (it.hasNext() && chunkSnapshot.size() < MAX_CHUNKS) {
Pair<Integer, Integer> pair = it.next();
chunkSnapshot.add(location.getWorld().getChunkAt(pair.x, pair.z).getChunkSnapshot());
it.remove();
}
// Move to next step
checking = false;
checkChunks(chunkSnapshot);
}
}, 0L, SPEED);
}
@ -119,8 +114,6 @@ public class SafeSpotTeleport {
/**
* Gets a set of chunk coords that will be scanned.
* @param entity
* @param location - the location
* @return
*/
private List<Pair<Integer, Integer>> getChunksToScan() {