From 38a2e833919124b8288ce44a327f860a92dbc7b3 Mon Sep 17 00:00:00 2001 From: theone1000 Date: Fri, 18 Oct 2019 19:40:59 -0600 Subject: [PATCH] more things including island stuff. --- .../java/com/songoda/skyblock/SkyBlock.java | 14 +- .../com/songoda/skyblock/ban/BanManager.java | 13 +- .../songoda/skyblock/biome/BiomeManager.java | 9 +- .../command/commands/admin/ReloadCommand.java | 33 +- .../admin/SetAlwaysLoadedCommand.java | 36 +- .../confirmation/ConfirmationTask.java | 15 +- .../skyblock/cooldown/CooldownManager.java | 237 ++- .../skyblock/cooldown/CooldownTask.java | 22 +- .../skyblock/cooldown/CooldownType.java | 16 +- .../skyblock/invite/InviteManager.java | 15 +- .../com/songoda/skyblock/island/Island.java | 58 +- .../songoda/skyblock/island/IslandLevel.java | 92 +- .../skyblock/island/IslandManager.java | 7 +- .../songoda/skyblock/island/IslandRole.java | 16 +- .../leaderboard/leaderheads/TopBank.java | 2 +- .../leaderboard/leaderheads/TopLevel.java | 2 +- .../leaderboard/leaderheads/TopVotes.java | 6 +- .../skyblock/limit/EnumLimitation.java | 77 + .../songoda/skyblock/limit/LimitManager.java | 115 -- .../songoda/skyblock/limit/Limitation.java | 13 + .../limit/LimitationInstanceHandler.java | 42 + .../skyblock/limit/impl/BlockLimitation.java | 106 ++ .../skyblock/limit/impl/EntityLimitaton.java | 55 + .../com/songoda/skyblock/listeners/Block.java | 24 +- .../songoda/skyblock/listeners/Entity.java | 1382 +++++++++-------- .../songoda/skyblock/listeners/Interact.java | 29 +- .../skyblock/utils/structure/Area.java | 6 +- .../utils/structure/SchematicUtil.java | 6 +- .../utils/structure/StructureUtil.java | 2 - src/main/resources/language.yml | 2 + src/main/resources/limits.yml | 7 +- 31 files changed, 1321 insertions(+), 1138 deletions(-) create mode 100644 src/main/java/com/songoda/skyblock/limit/EnumLimitation.java delete mode 100644 src/main/java/com/songoda/skyblock/limit/LimitManager.java create mode 100644 src/main/java/com/songoda/skyblock/limit/Limitation.java create mode 100644 src/main/java/com/songoda/skyblock/limit/LimitationInstanceHandler.java create mode 100644 src/main/java/com/songoda/skyblock/limit/impl/BlockLimitation.java create mode 100644 src/main/java/com/songoda/skyblock/limit/impl/EntityLimitaton.java diff --git a/src/main/java/com/songoda/skyblock/SkyBlock.java b/src/main/java/com/songoda/skyblock/SkyBlock.java index af0ff5eb..f8d7a38e 100644 --- a/src/main/java/com/songoda/skyblock/SkyBlock.java +++ b/src/main/java/com/songoda/skyblock/SkyBlock.java @@ -15,7 +15,7 @@ import com.songoda.skyblock.invite.InviteManager; import com.songoda.skyblock.island.IslandManager; import com.songoda.skyblock.leaderboard.LeaderboardManager; import com.songoda.skyblock.levelling.LevellingManager; -import com.songoda.skyblock.limit.LimitManager; +import com.songoda.skyblock.limit.LimitationInstanceHandler; import com.songoda.skyblock.listeners.*; import com.songoda.skyblock.menus.Rollback; import com.songoda.skyblock.menus.admin.Creator; @@ -61,7 +61,6 @@ public class SkyBlock extends JavaPlugin { private UpgradeManager upgradeManager; private PlayerDataManager playerDataManager; private CooldownManager cooldownManager; - private LimitManager limitManager; private ScoreboardManager scoreboardManager; private InviteManager inviteManager; private BiomeManager biomeManager; @@ -76,6 +75,7 @@ public class SkyBlock extends JavaPlugin { private MessageManager messageManager; private EconomyManager economyManager; private HologramManager hologramManager; + private LimitationInstanceHandler limitationHandler; public static SkyBlock getInstance() { return instance; @@ -101,7 +101,7 @@ public class SkyBlock extends JavaPlugin { upgradeManager = new UpgradeManager(this); playerDataManager = new PlayerDataManager(this); cooldownManager = new CooldownManager(this); - limitManager = new LimitManager(this); + limitationHandler = new LimitationInstanceHandler(); if (fileManager.getConfig(new File(getDataFolder(), "config.yml")).getFileConfiguration() .getBoolean("Island.Scoreboard.Enable")) { @@ -261,10 +261,6 @@ public class SkyBlock extends JavaPlugin { return cooldownManager; } - public LimitManager getLimitManager() { - return limitManager; - } - public ScoreboardManager getScoreboardManager() { return scoreboardManager; } @@ -329,6 +325,10 @@ public class SkyBlock extends JavaPlugin { return stackableManager; } + public LimitationInstanceHandler getLimitationHandler() { + return limitationHandler; + } + @Override public ChunkGenerator getDefaultWorldGenerator(String worldName, String id) { return new VoidGenerator(); diff --git a/src/main/java/com/songoda/skyblock/ban/BanManager.java b/src/main/java/com/songoda/skyblock/ban/BanManager.java index fca4dd24..608a0a88 100644 --- a/src/main/java/com/songoda/skyblock/ban/BanManager.java +++ b/src/main/java/com/songoda/skyblock/ban/BanManager.java @@ -31,8 +31,7 @@ public class BanManager { public void onDisable() { Map banIslands = getIslands(); - for (UUID banIslandList : banIslands.keySet()) { - Ban ban = banIslands.get(banIslandList); + for (Ban ban : banIslands.values()) { ban.save(); } } @@ -96,11 +95,7 @@ public class BanManager { } public Ban getIsland(UUID islandOwnerUUID) { - if (hasIsland(islandOwnerUUID)) { - return banStorage.get(islandOwnerUUID); - } - - return null; + return banStorage.get(islandOwnerUUID); } public Map getIslands() { @@ -116,9 +111,7 @@ public class BanManager { } public void removeIsland(UUID islandOwnerUUID) { - if (hasIsland(islandOwnerUUID)) { - banStorage.remove(islandOwnerUUID); - } + banStorage.remove(islandOwnerUUID); } public void unloadIsland(UUID islandOwnerUUID) { diff --git a/src/main/java/com/songoda/skyblock/biome/BiomeManager.java b/src/main/java/com/songoda/skyblock/biome/BiomeManager.java index 7bc60c10..a4651dd0 100644 --- a/src/main/java/com/songoda/skyblock/biome/BiomeManager.java +++ b/src/main/java/com/songoda/skyblock/biome/BiomeManager.java @@ -11,8 +11,8 @@ import org.bukkit.block.Biome; import org.bukkit.entity.Player; import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; public class BiomeManager { @@ -23,7 +23,7 @@ public class BiomeManager { } public void setBiome(Island island, Biome biome) { - List chunks = new ArrayList<>(); + Set chunks = new HashSet<>(); Location location = island.getLocation(IslandWorld.Normal, IslandEnvironment.Island); int radius = (int) Math.ceil(island.getRadius()); @@ -31,8 +31,7 @@ public class BiomeManager { for (int z = location.getBlockZ() - radius; z < location.getBlockZ() + radius; z++) { location.getWorld().setBiome(x, z, biome); Chunk chunk = location.getWorld().getChunkAt(x >> 4, z >> 4); - if (!chunks.contains(chunk)) - chunks.add(chunk); + chunks.add(chunk); } } diff --git a/src/main/java/com/songoda/skyblock/command/commands/admin/ReloadCommand.java b/src/main/java/com/songoda/skyblock/command/commands/admin/ReloadCommand.java index d5e3a33d..9f731479 100644 --- a/src/main/java/com/songoda/skyblock/command/commands/admin/ReloadCommand.java +++ b/src/main/java/com/songoda/skyblock/command/commands/admin/ReloadCommand.java @@ -1,25 +1,25 @@ package com.songoda.skyblock.command.commands.admin; -import com.songoda.skyblock.command.SubCommand; -import com.songoda.skyblock.config.FileManager; -import com.songoda.skyblock.config.FileManager.Config; -import com.songoda.skyblock.generator.GeneratorManager; -import com.songoda.skyblock.hologram.HologramManager; -import com.songoda.skyblock.leaderboard.LeaderboardManager; -import com.songoda.skyblock.levelling.LevellingManager; -import com.songoda.skyblock.limit.LimitManager; -import com.songoda.skyblock.message.MessageManager; -import com.songoda.skyblock.scoreboard.ScoreboardManager; -import com.songoda.skyblock.sound.SoundManager; -import com.songoda.skyblock.utils.version.Sounds; +import java.io.File; +import java.util.Map; + import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; -import java.io.File; -import java.util.Map; +import com.songoda.skyblock.command.SubCommand; +import com.songoda.skyblock.config.FileManager; +import com.songoda.skyblock.config.FileManager.Config; +import com.songoda.skyblock.generator.GeneratorManager; +import com.songoda.skyblock.leaderboard.LeaderboardManager; +import com.songoda.skyblock.levelling.LevellingManager; +import com.songoda.skyblock.limit.LimitationInstanceHandler; +import com.songoda.skyblock.message.MessageManager; +import com.songoda.skyblock.scoreboard.ScoreboardManager; +import com.songoda.skyblock.sound.SoundManager; +import com.songoda.skyblock.utils.version.Sounds; public class ReloadCommand extends SubCommand { @@ -35,10 +35,9 @@ public class ReloadCommand extends SubCommand { public void onCommand(CommandSender sender, String[] args) { LeaderboardManager leaderboardManager = skyblock.getLeaderboardManager(); - HologramManager hologramManager = skyblock.getHologramManager(); MessageManager messageManager = skyblock.getMessageManager(); SoundManager soundManager = skyblock.getSoundManager(); - LimitManager limitManager = skyblock.getLimitManager(); + LimitationInstanceHandler limitHandler = skyblock.getLimitationHandler(); FileManager fileManager = skyblock.getFileManager(); messageManager.sendMessage(sender, "&cPlease note that this command is not supported and may " + @@ -96,7 +95,7 @@ public class ReloadCommand extends SubCommand { Bukkit.getScheduler().runTask(skyblock, () -> skyblock.getHologramManager().resetHologram()); }); - limitManager.reload(); + limitHandler.reloadAll(); messageManager.sendMessage(sender, configLoad.getString("Command.Island.Admin.Reload.Reloaded.Message")); soundManager.playSound(sender, Sounds.ANVIL_USE.bukkitSound(), 1.0F, 1.0F); diff --git a/src/main/java/com/songoda/skyblock/command/commands/admin/SetAlwaysLoadedCommand.java b/src/main/java/com/songoda/skyblock/command/commands/admin/SetAlwaysLoadedCommand.java index ac7146ef..227c8a72 100644 --- a/src/main/java/com/songoda/skyblock/command/commands/admin/SetAlwaysLoadedCommand.java +++ b/src/main/java/com/songoda/skyblock/command/commands/admin/SetAlwaysLoadedCommand.java @@ -1,5 +1,14 @@ package com.songoda.skyblock.command.commands.admin; +import java.io.File; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; + import com.songoda.skyblock.command.SubCommand; import com.songoda.skyblock.config.FileManager; import com.songoda.skyblock.config.FileManager.Config; @@ -10,16 +19,6 @@ import com.songoda.skyblock.playerdata.PlayerDataManager; import com.songoda.skyblock.sound.SoundManager; import com.songoda.skyblock.utils.player.OfflinePlayer; import com.songoda.skyblock.utils.version.Sounds; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.command.ConsoleCommandSender; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; - -import java.io.File; -import java.io.IOException; -import java.util.UUID; public class SetAlwaysLoadedCommand extends SubCommand { @@ -34,14 +33,21 @@ public class SetAlwaysLoadedCommand extends SubCommand { } public void onCommand(CommandSender sender, String[] args) { - PlayerDataManager playerDataManager = skyblock.getPlayerDataManager(); - MessageManager messageManager = skyblock.getMessageManager(); - IslandManager islandManager = skyblock.getIslandManager(); - SoundManager soundManager = skyblock.getSoundManager(); - FileManager fileManager = skyblock.getFileManager(); + FileManager fileManager = skyblock.getFileManager(); Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")); FileConfiguration configLoad = config.getFileConfiguration(); + MessageManager messageManager = skyblock.getMessageManager(); + + if (args.length == 0) { + messageManager.sendMessage(sender, + configLoad.getString("Command.Island.Admin.SetAlwaysLoaded.No-Player-Input.Message")); + return; + } + + PlayerDataManager playerDataManager = skyblock.getPlayerDataManager(); + IslandManager islandManager = skyblock.getIslandManager(); + SoundManager soundManager = skyblock.getSoundManager(); if (args.length == 1) { Player targetPlayer = Bukkit.getServer().getPlayer(args[0]); diff --git a/src/main/java/com/songoda/skyblock/confirmation/ConfirmationTask.java b/src/main/java/com/songoda/skyblock/confirmation/ConfirmationTask.java index 782e29e0..2d6994fa 100644 --- a/src/main/java/com/songoda/skyblock/confirmation/ConfirmationTask.java +++ b/src/main/java/com/songoda/skyblock/confirmation/ConfirmationTask.java @@ -1,10 +1,9 @@ package com.songoda.skyblock.confirmation; +import org.bukkit.scheduler.BukkitRunnable; + import com.songoda.skyblock.playerdata.PlayerData; import com.songoda.skyblock.playerdata.PlayerDataManager; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; public class ConfirmationTask extends BukkitRunnable { @@ -16,13 +15,9 @@ public class ConfirmationTask extends BukkitRunnable { @Override public void run() { - for (Player all : Bukkit.getOnlinePlayers()) { - if (playerDataManager.hasPlayerData(all)) { - PlayerData playerData = playerDataManager.getPlayerData(all); - - if (playerData.getConfirmationTime() > 0) { - playerData.setConfirmationTime(playerData.getConfirmationTime() - 1); - } + for (PlayerData playerData : playerDataManager.getPlayerData().values()) { + if (playerData.getConfirmationTime() > 0) { + playerData.setConfirmationTime(playerData.getConfirmationTime() - 1); } } } diff --git a/src/main/java/com/songoda/skyblock/cooldown/CooldownManager.java b/src/main/java/com/songoda/skyblock/cooldown/CooldownManager.java index 63e5747d..9418e52d 100644 --- a/src/main/java/com/songoda/skyblock/cooldown/CooldownManager.java +++ b/src/main/java/com/songoda/skyblock/cooldown/CooldownManager.java @@ -13,7 +13,9 @@ import org.bukkit.entity.Player; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; +import java.util.Collections; +import java.util.EnumMap; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -21,14 +23,14 @@ public class CooldownManager { private final SkyBlock skyblock; - private Map> cooldownStorage = new HashMap<>(); + private Map> cooldownStorage = new EnumMap<>(CooldownType.class); public CooldownManager(SkyBlock skyblock) { this.skyblock = skyblock; IslandManager islandManager = skyblock.getIslandManager(); - for (CooldownType cooldownTypeList : CooldownType.values()) { + for (CooldownType cooldownTypeList : CooldownType.getTypes()) { List cooldownPlayers = new ArrayList<>(); for (Player all : Bukkit.getOnlinePlayers()) { @@ -60,7 +62,7 @@ public class CooldownManager { } public void onDisable() { - for (CooldownType cooldownTypeList : CooldownType.values()) { + for (CooldownType cooldownTypeList : CooldownType.getTypes()) { setCooldownPlayer(cooldownTypeList); saveCooldownPlayer(cooldownTypeList); } @@ -69,23 +71,19 @@ public class CooldownManager { public CooldownPlayer loadCooldownPlayer(CooldownType cooldownType, OfflinePlayer player) { if (cooldownType == CooldownType.Biome || cooldownType == CooldownType.Creation) { Config config = skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/player-data"), - player.getUniqueId().toString() + ".yml")); + .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/player-data"), player.getUniqueId().toString() + ".yml")); FileConfiguration configLoad = config.getFileConfiguration(); if (configLoad.getString("Island." + cooldownType.name() + ".Cooldown") != null) { - return new CooldownPlayer(player.getUniqueId(), - new Cooldown(configLoad.getInt("Island." + cooldownType.name() + ".Cooldown"))); + return new CooldownPlayer(player.getUniqueId(), new Cooldown(configLoad.getInt("Island." + cooldownType.name() + ".Cooldown"))); } } else if (cooldownType == CooldownType.Levelling || cooldownType == CooldownType.Ownership) { Config config = skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/island-data"), - player.getUniqueId().toString() + ".yml")); + .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/island-data"), player.getUniqueId().toString() + ".yml")); FileConfiguration configLoad = config.getFileConfiguration(); if (configLoad.getString(cooldownType.name() + ".Cooldown") != null) { - return new CooldownPlayer(player.getUniqueId(), - new Cooldown(configLoad.getInt(cooldownType.name() + ".Cooldown"))); + return new CooldownPlayer(player.getUniqueId(), new Cooldown(configLoad.getInt(cooldownType.name() + ".Cooldown"))); } } @@ -95,79 +93,73 @@ public class CooldownManager { public void createPlayer(CooldownType cooldownType, OfflinePlayer player) { FileManager fileManager = skyblock.getFileManager(); - if (cooldownStorage.containsKey(cooldownType)) { - int time = 0; + List cooldowns = cooldownStorage.get(cooldownType); - if (cooldownType == CooldownType.Biome || cooldownType == CooldownType.Creation) { - time = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration() - .getInt("Island." + cooldownType.name() + ".Cooldown.Time"); + if (cooldowns == null) return; - Config config = fileManager - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/player-data"), - player.getUniqueId().toString() + ".yml")); - File configFile = config.getFile(); - FileConfiguration configLoad = config.getFileConfiguration(); + int time = 0; - configLoad.set("Island." + cooldownType.name() + ".Cooldown", time); + if (cooldownType == CooldownType.Biome || cooldownType == CooldownType.Creation) { + time = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration() + .getInt("Island." + cooldownType.name() + ".Cooldown.Time"); - try { - configLoad.save(configFile); - } catch (IOException e) { - e.printStackTrace(); - } - } else if (cooldownType == CooldownType.Levelling || cooldownType == CooldownType.Ownership) { - time = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration() - .getInt("Island." + cooldownType.name() + ".Cooldown.Time"); + Config config = fileManager + .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/player-data"), player.getUniqueId().toString() + ".yml")); + File configFile = config.getFile(); + FileConfiguration configLoad = config.getFileConfiguration(); - Config config = skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/island-data"), - player.getUniqueId().toString() + ".yml")); - File configFile = config.getFile(); - FileConfiguration configLoad = config.getFileConfiguration(); + configLoad.set("Island." + cooldownType.name() + ".Cooldown", time); - configLoad.set(cooldownType.name() + ".Cooldown", time); - - try { - configLoad.save(configFile); - } catch (IOException e) { - e.printStackTrace(); - } + try { + configLoad.save(configFile); + } catch (IOException e) { + e.printStackTrace(); } + } else if (cooldownType == CooldownType.Levelling || cooldownType == CooldownType.Ownership) { + time = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration() + .getInt("Island." + cooldownType.name() + ".Cooldown.Time"); - cooldownStorage.get(cooldownType).add(new CooldownPlayer(player.getUniqueId(), new Cooldown(time))); + Config config = skyblock.getFileManager() + .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/island-data"), player.getUniqueId().toString() + ".yml")); + File configFile = config.getFile(); + FileConfiguration configLoad = config.getFileConfiguration(); + + configLoad.set(cooldownType.name() + ".Cooldown", time); + + try { + configLoad.save(configFile); + } catch (IOException e) { + e.printStackTrace(); + } } + + cooldowns.add(new CooldownPlayer(player.getUniqueId(), new Cooldown(time))); } public void deletePlayer(CooldownType cooldownType, OfflinePlayer player) { - if (cooldownStorage.containsKey(cooldownType)) { - for (CooldownPlayer cooldownPlayerList : cooldownStorage.get(cooldownType)) { - if (cooldownPlayerList.getUUID().equals(player.getUniqueId())) { - if (cooldownType == CooldownType.Biome || cooldownType == CooldownType.Creation) { - skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/player-data"), - player.getUniqueId().toString() + ".yml")) - .getFileConfiguration().set("Island." + cooldownType.name() + ".Cooldown", null); - } else if (cooldownType == CooldownType.Levelling || cooldownType == CooldownType.Ownership) { - skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/island-data"), - player.getUniqueId().toString() + ".yml")) - .getFileConfiguration().set(cooldownType.name() + ".Cooldown", null); - } - - cooldownStorage.get(cooldownType).remove(cooldownPlayerList); - - break; + for (Iterator it = getCooldownPlayersOrEmptyList(cooldownType).iterator(); it.hasNext();) { + if (it.next().getUUID().equals(player.getUniqueId())) { + if (cooldownType == CooldownType.Biome || cooldownType == CooldownType.Creation) { + skyblock.getFileManager() + .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/player-data"), + player.getUniqueId().toString() + ".yml")) + .getFileConfiguration().set("Island." + cooldownType.name() + ".Cooldown", null); + } else if (cooldownType == CooldownType.Levelling || cooldownType == CooldownType.Ownership) { + skyblock.getFileManager().getConfig( + new File(new File(skyblock.getDataFolder().toString() + "/island-data"), player.getUniqueId().toString() + ".yml")) + .getFileConfiguration().set(cooldownType.name() + ".Cooldown", null); } + it.remove(); + break; } } } public boolean hasPlayer(CooldownType cooldownType, OfflinePlayer player) { - if (cooldownStorage.containsKey(cooldownType)) { - for (CooldownPlayer cooldownPlayerList : cooldownStorage.get(cooldownType)) { - if (cooldownPlayerList.getUUID().equals(player.getUniqueId())) { - return true; - } + + for (CooldownPlayer cooldownPlayerList : getCooldownPlayersOrEmptyList(cooldownType)) { + if (cooldownPlayerList.getUUID().equals(player.getUniqueId())) { + return true; } } @@ -175,72 +167,55 @@ public class CooldownManager { } public void transferPlayer(CooldownType cooldownType, OfflinePlayer player1, OfflinePlayer player2) { - if (cooldownStorage.containsKey(cooldownType)) { - for (CooldownPlayer cooldownPlayerList : cooldownStorage.get(cooldownType)) { - if (cooldownPlayerList.getUUID().equals(player1.getUniqueId())) { - cooldownPlayerList.setUUID(player2.getUniqueId()); - - break; - } + for (CooldownPlayer cooldownPlayerList : getCooldownPlayersOrEmptyList(cooldownType)) { + if (cooldownPlayerList.getUUID().equals(player1.getUniqueId())) { + cooldownPlayerList.setUUID(player2.getUniqueId()); + break; } } } public void removeCooldownPlayer(CooldownType cooldownType, CooldownPlayer cooldownPlayer) { - if (cooldownStorage.containsKey(cooldownType)) { - cooldownStorage.get(cooldownType).remove(cooldownPlayer); - } + getCooldownPlayersOrEmptyList(cooldownType).remove(cooldownPlayer); } public void removeCooldownPlayer(CooldownType cooldownType, OfflinePlayer player) { - if (cooldownStorage.containsKey(cooldownType)) { - for (CooldownPlayer cooldownPlayerList : cooldownStorage.get(cooldownType)) { - if (cooldownPlayerList.getUUID().equals(player.getUniqueId())) { - cooldownStorage.get(cooldownType).remove(cooldownPlayerList); - - break; - } + for (Iterator it = getCooldownPlayersOrEmptyList(cooldownType).iterator(); it.hasNext();) { + if (it.next().getUUID().equals(player.getUniqueId())) { + it.remove(); + break; } } } public void setCooldownPlayer(CooldownType cooldownType) { - if (cooldownStorage.containsKey(cooldownType)) { - for (CooldownPlayer cooldownPlayerList : cooldownStorage.get(cooldownType)) { - setCooldownPlayer(cooldownType, Bukkit.getServer().getOfflinePlayer(cooldownPlayerList.getUUID())); - } + for (CooldownPlayer cooldownPlayerList : getCooldownPlayersOrEmptyList(cooldownType)) { + setCooldownPlayer(cooldownType, Bukkit.getServer().getOfflinePlayer(cooldownPlayerList.getUUID())); } } public void setCooldownPlayer(CooldownType cooldownType, OfflinePlayer player) { - if (cooldownStorage.containsKey(cooldownType)) { - for (CooldownPlayer cooldownPlayerList : cooldownStorage.get(cooldownType)) { - if (cooldownPlayerList.getUUID().equals(player.getUniqueId())) { - if (cooldownType == CooldownType.Biome || cooldownType == CooldownType.Creation) { - skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/player-data"), - player.getUniqueId().toString() + ".yml")) - .getFileConfiguration().set("Island." + cooldownType + ".Cooldown", - cooldownPlayerList.getCooldown().getTime()); - } else if (cooldownType == CooldownType.Levelling || cooldownType == CooldownType.Ownership) { - skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/island-data"), - player.getUniqueId().toString() + ".yml")) - .getFileConfiguration() - .set(cooldownType.name() + ".Cooldown", cooldownPlayerList.getCooldown().getTime()); - } - - break; + for (CooldownPlayer cooldownPlayerList : getCooldownPlayersOrEmptyList(cooldownType)) { + if (cooldownPlayerList.getUUID().equals(player.getUniqueId())) { + if (cooldownType == CooldownType.Biome || cooldownType == CooldownType.Creation) { + skyblock.getFileManager() + .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/player-data"), + player.getUniqueId().toString() + ".yml")) + .getFileConfiguration().set("Island." + cooldownType + ".Cooldown", cooldownPlayerList.getCooldown().getTime()); + } else if (cooldownType == CooldownType.Levelling || cooldownType == CooldownType.Ownership) { + skyblock.getFileManager() + .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/island-data"), + player.getUniqueId().toString() + ".yml")) + .getFileConfiguration().set(cooldownType.name() + ".Cooldown", cooldownPlayerList.getCooldown().getTime()); } + break; } } } public void saveCooldownPlayer(CooldownType cooldownType) { - if (cooldownStorage.containsKey(cooldownType)) { - for (CooldownPlayer cooldownPlayerList : cooldownStorage.get(cooldownType)) { - saveCooldownPlayer(cooldownType, Bukkit.getServer().getOfflinePlayer(cooldownPlayerList.getUUID())); - } + for (CooldownPlayer cooldownPlayerList : getCooldownPlayersOrEmptyList(cooldownType)) { + saveCooldownPlayer(cooldownType, Bukkit.getServer().getOfflinePlayer(cooldownPlayerList.getUUID())); } } @@ -249,12 +224,10 @@ public class CooldownManager { if (cooldownType == CooldownType.Biome || cooldownType == CooldownType.Creation) { config = skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/player-data"), - player.getUniqueId().toString() + ".yml")); + .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/player-data"), player.getUniqueId().toString() + ".yml")); } else if (cooldownType == CooldownType.Levelling || cooldownType == CooldownType.Ownership) { config = skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/island-data"), - player.getUniqueId().toString() + ".yml")); + .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/island-data"), player.getUniqueId().toString() + ".yml")); } if (config != null) { @@ -267,19 +240,20 @@ public class CooldownManager { } public void addCooldownPlayer(CooldownType cooldownType, CooldownPlayer cooldownPlayer) { - if (cooldownType != null && cooldownPlayer != null) { - if (cooldownStorage.containsKey(cooldownType)) { - cooldownStorage.get(cooldownType).add(cooldownPlayer); - } - } + + if (cooldownType == null || cooldownPlayer == null) return; + + List cooldowns = cooldownStorage.get(cooldownType); + + if (cooldowns == null) return; + + cooldowns.add(cooldownPlayer); } public CooldownPlayer getCooldownPlayer(CooldownType cooldownType, OfflinePlayer player) { - if (cooldownStorage.containsKey(cooldownType)) { - for (CooldownPlayer cooldownPlayerList : cooldownStorage.get(cooldownType)) { - if (cooldownPlayerList.getUUID().equals(player.getUniqueId())) { - return cooldownPlayerList; - } + for (CooldownPlayer cooldownPlayerList : getCooldownPlayersOrEmptyList(cooldownType)) { + if (cooldownPlayerList.getUUID().equals(player.getUniqueId())) { + return cooldownPlayerList; } } @@ -287,14 +261,21 @@ public class CooldownManager { } public List getCooldownPlayers(CooldownType cooldownType) { - if (cooldownStorage.containsKey(cooldownType)) { - return cooldownStorage.get(cooldownType); - } + return cooldownStorage.get(cooldownType); + } - return null; + /** + * Convenience method. This method functions the same as + * {@link CooldownManager#getCooldownPlayers(CooldownType)} but returns a + * {@link Collections#emptyList()} when no value is present in the map under the + * key, cooldownType. + */ + public List getCooldownPlayersOrEmptyList(CooldownType cooldownType) { + return cooldownStorage.getOrDefault(cooldownType, Collections.emptyList()); } public boolean hasCooldownType(CooldownType cooldownType) { return cooldownStorage.containsKey(cooldownType); } + } diff --git a/src/main/java/com/songoda/skyblock/cooldown/CooldownTask.java b/src/main/java/com/songoda/skyblock/cooldown/CooldownTask.java index 757b0e29..d589492c 100644 --- a/src/main/java/com/songoda/skyblock/cooldown/CooldownTask.java +++ b/src/main/java/com/songoda/skyblock/cooldown/CooldownTask.java @@ -15,22 +15,22 @@ public class CooldownTask extends BukkitRunnable { @Override public void run() { - for (CooldownType cooldownTypeList : CooldownType.values()) { - if (cooldownManager.hasCooldownType(cooldownTypeList)) { - List cooldownPlayers = cooldownManager.getCooldownPlayers(cooldownTypeList); + for (CooldownType cooldownType : CooldownType.getTypes()) { + List cooldownPlayers = cooldownManager.getCooldownPlayers(cooldownType); - for (int i = 0; i < cooldownPlayers.size(); i++) { - CooldownPlayer cooldownPlayer = cooldownPlayers.get(i); - Cooldown cooldown = cooldownPlayer.getCooldown(); + if (cooldownPlayers == null) return; - cooldown.setTime(cooldown.getTime() - 1); + for (int i = 0; i < cooldownPlayers.size(); i++) { + CooldownPlayer cooldownPlayer = cooldownPlayers.get(i); + Cooldown cooldown = cooldownPlayer.getCooldown(); - if (cooldown.getTime() <= 0) { - cooldownManager.deletePlayer(cooldownTypeList, - Bukkit.getServer().getOfflinePlayer(cooldownPlayer.getUUID())); - } + cooldown.setTime(cooldown.getTime() - 1); + + if (cooldown.getTime() <= 0) { + cooldownManager.deletePlayer(cooldownType, Bukkit.getServer().getOfflinePlayer(cooldownPlayer.getUUID())); } } + } } } diff --git a/src/main/java/com/songoda/skyblock/cooldown/CooldownType.java b/src/main/java/com/songoda/skyblock/cooldown/CooldownType.java index 65379d21..f96010b4 100644 --- a/src/main/java/com/songoda/skyblock/cooldown/CooldownType.java +++ b/src/main/java/com/songoda/skyblock/cooldown/CooldownType.java @@ -1,7 +1,21 @@ package com.songoda.skyblock.cooldown; +import java.util.Collections; +import java.util.EnumSet; +import java.util.Set; + public enum CooldownType { - Biome, Creation, Levelling, Ownership, Teleport + Biome, + Creation, + Levelling, + Ownership, + Teleport; + + private static final Set types = Collections.unmodifiableSet(EnumSet.allOf(CooldownType.class)); + + public static Set getTypes() { + return types; + } } diff --git a/src/main/java/com/songoda/skyblock/invite/InviteManager.java b/src/main/java/com/songoda/skyblock/invite/InviteManager.java index 5e2cb94c..97b4b2c8 100644 --- a/src/main/java/com/songoda/skyblock/invite/InviteManager.java +++ b/src/main/java/com/songoda/skyblock/invite/InviteManager.java @@ -18,22 +18,17 @@ public class InviteManager { public Invite createInvite(Player player, Player sender, UUID owner, int time) { Invite invite = new Invite(player, sender, owner, time); inviteStorage.put(player.getUniqueId(), invite); - return invite; } public void removeInvite(UUID uuid) { - if (hasInvite(uuid)) { - inviteStorage.remove(uuid); - } + inviteStorage.remove(uuid); } public void tranfer(UUID uuid1, UUID uuid2) { Map islandInvites = getInvites(); - for (UUID islandInviteList : islandInvites.keySet()) { - Invite invite = islandInvites.get(islandInviteList); - + for (Invite invite : islandInvites.values()) { if (invite.getOwnerUUID().equals(uuid1)) { invite.setOwnerUUID(uuid2); } @@ -45,11 +40,7 @@ public class InviteManager { } public Invite getInvite(UUID uuid) { - if (hasInvite(uuid)) { - return inviteStorage.get(uuid); - } - - return null; + return inviteStorage.get(uuid); } public boolean hasInvite(UUID uuid) { diff --git a/src/main/java/com/songoda/skyblock/island/Island.java b/src/main/java/com/songoda/skyblock/island/Island.java index 06a88f81..d9f3f47d 100644 --- a/src/main/java/com/songoda/skyblock/island/Island.java +++ b/src/main/java/com/songoda/skyblock/island/Island.java @@ -27,6 +27,7 @@ import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.*; +import java.util.Map.Entry; public class Island { @@ -104,7 +105,7 @@ public class Island { if (playerDataConfigLoad.getString("Island.Owner") == null || !playerDataConfigLoad.getString("Island.Owner").equals(ownerUUID.toString())) { - members.remove(member); + members.remove(i); } } @@ -122,7 +123,7 @@ public class Island { if (playerDataConfigLoad.getString("Island.Owner") == null || !playerDataConfigLoad.getString("Island.Owner").equals(ownerUUID.toString())) { - operators.remove(operator); + operators.remove(i); } } @@ -131,24 +132,21 @@ public class Island { Config settingsDataConfig = null; - if (fileManager.isFileExist(new File(skyblock.getDataFolder().toString() + "/setting-data", - getOwnerUUID().toString() + ".yml"))) { - settingsDataConfig = fileManager.getConfig(new File( - skyblock.getDataFolder().toString() + "/setting-data", getOwnerUUID().toString() + ".yml")); + File settingDataFile = new File(skyblock.getDataFolder().toString() + "/setting-data", getOwnerUUID().toString() + ".yml"); + + if (fileManager.isFileExist(settingDataFile)) { + settingsDataConfig = fileManager.getConfig(settingDataFile); } - for (IslandRole roleList : IslandRole.values()) { + for (IslandRole roleList : IslandRole.getRoles()) { List settings = new ArrayList<>(); - - for (String settingList : defaultSettingsConfig.getFileConfiguration() - .getConfigurationSection("Settings." + roleList.name()).getKeys(false)) { - if (settingsDataConfig == null || settingsDataConfig.getFileConfiguration() - .getString("Settings." + roleList.name() + "." + settingList) == null) { - settings.add(new IslandSetting(settingList, defaultSettingsConfig.getFileConfiguration() - .getBoolean("Settings." + roleList.name() + "." + settingList))); + + for (String settingList : defaultSettingsConfig.getFileConfiguration().getConfigurationSection("Settings." + roleList.name()).getKeys(false)) { + if (settingsDataConfig == null || settingsDataConfig.getFileConfiguration().getString("Settings." + roleList.name() + "." + settingList) == null) { + settings.add( + new IslandSetting(settingList, defaultSettingsConfig.getFileConfiguration().getBoolean("Settings." + roleList.name() + "." + settingList))); } else { - settings.add(new IslandSetting(settingList, settingsDataConfig.getFileConfiguration() - .getBoolean("Settings." + roleList.name() + "." + settingList))); + settings.add(new IslandSetting(settingList, settingsDataConfig.getFileConfiguration().getBoolean("Settings." + roleList.name() + "." + settingList))); } } @@ -167,13 +165,13 @@ public class Island { configLoad.set("Weather.Weather", mainConfigLoad.getString("Island.Weather.Default.Weather").toUpperCase()); configLoad.set("Ownership.Original", ownerUUID.toString()); - for (IslandRole roleList : IslandRole.values()) { - List settings = new ArrayList<>(); + for (IslandRole roleList : IslandRole.getRoles()) { - for (String settingList : defaultSettingsConfig.getFileConfiguration() - .getConfigurationSection("Settings." + roleList.name()).getKeys(false)) { - settings.add(new IslandSetting(settingList, defaultSettingsConfig.getFileConfiguration() - .getBoolean("Settings." + roleList.name() + "." + settingList))); + Set keys = defaultSettingsConfig.getFileConfiguration().getConfigurationSection("Settings." + roleList.name()).getKeys(false); + List settings = new ArrayList<>(keys.size()); + + for (String settingList : keys) { + settings.add(new IslandSetting(settingList, defaultSettingsConfig.getFileConfiguration().getBoolean("Settings." + roleList.name() + "." + settingList))); } islandSettings.put(roleList, settings); @@ -227,12 +225,8 @@ public class Island { } public UUID getOriginalOwnerUUID() { - return UUID - .fromString( - skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/island-data"), - ownerUUID.toString() + ".yml")) - .getFileConfiguration().getString("Ownership.Original")); + return UUID.fromString(skyblock.getFileManager().getConfig(new File(new File(skyblock.getDataFolder().toString() + "/island-data"), ownerUUID.toString() + ".yml")) + .getFileConfiguration().getString("Ownership.Original")); } public int getSize() { @@ -772,9 +766,9 @@ public class Island { .getConfig(new File(skyblock.getDataFolder().toString() + "/setting-data", ownerUUID.toString() + ".yml")); FileConfiguration configLoad = config.getFileConfiguration(); - for (IslandRole roleList : islandSettings.keySet()) { - for (IslandSetting settingList : islandSettings.get(roleList)) { - configLoad.set("Settings." + roleList + "." + settingList.getName(), settingList.getStatus()); + for (Entry> entry : islandSettings.entrySet()) { + for (IslandSetting setting : entry.getValue()) { + configLoad.set("Settings." + entry.getKey() + "." + setting.getName(), setting.getStatus()); } } @@ -790,7 +784,7 @@ public class Island { .getConfig(new File(skyblock.getDataFolder().toString() + "/coop-data", ownerUUID.toString() + ".yml")); configLoad = config.getFileConfiguration(); - List coopPlayersAsString = new ArrayList<>(); + List coopPlayersAsString = new ArrayList<>(coopPlayers.size()); for (Map.Entry entry : coopPlayers.entrySet()) { if (entry.getValue() == IslandCoop.TEMP) continue; diff --git a/src/main/java/com/songoda/skyblock/island/IslandLevel.java b/src/main/java/com/songoda/skyblock/island/IslandLevel.java index 80f175ae..dba05c80 100644 --- a/src/main/java/com/songoda/skyblock/island/IslandLevel.java +++ b/src/main/java/com/songoda/skyblock/island/IslandLevel.java @@ -2,12 +2,16 @@ package com.songoda.skyblock.island; import com.songoda.skyblock.SkyBlock; import com.songoda.skyblock.config.FileManager.Config; + +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; import java.util.UUID; public class IslandLevel { @@ -25,18 +29,25 @@ public class IslandLevel { this.skyblock = skyblock; this.ownerUUID = ownerUUID; - Config config = skyblock.getFileManager().getConfig( - new File(new File(skyblock.getDataFolder().toString() + "/level-data"), ownerUUID.toString() + ".yml")); + Config config = skyblock.getFileManager().getConfig(new File(new File(skyblock.getDataFolder().toString() + "/level-data"), ownerUUID.toString() + ".yml")); FileConfiguration configLoad = config.getFileConfiguration(); - Map materials = new HashMap<>(); + Map materials; - if (configLoad.getString("Levelling.Materials") != null) { - for (String materialList : configLoad.getConfigurationSection("Levelling.Materials").getKeys(false)) { - if (configLoad.getString("Levelling.Materials." + materialList + ".Amount") != null) { - materials.put(materialList, configLoad.getLong("Levelling.Materials." + materialList + ".Amount")); - } + ConfigurationSection materialSection = configLoad.getConfigurationSection("Leveling.Materials"); + + if (materialSection != null) { + Set keys = materialSection.getKeys(false); + materials = new HashMap<>(keys.size() * 2); + + for (String material : materialSection.getKeys(false)) { + + final ConfigurationSection current = materialSection.getConfigurationSection(material); + + if (current.isSet("Amount")) materials.put(material, current.getLong("Amount")); } + } else { + materials = new HashMap<>(); } this.materials = materials; @@ -50,18 +61,18 @@ public class IslandLevel { Config config = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "levelling.yml")); FileConfiguration configLoad = config.getFileConfiguration(); + ConfigurationSection materialSection = configLoad.getConfigurationSection("Materials"); + + if (materialSection == null) return 0; + long pointsEarned = 0; - for (String materialList : this.materials.keySet()) { - long materialAmount = this.materials.get(materialList); + for (Entry entry : this.materials.entrySet()) { + ConfigurationSection current = materialSection.getConfigurationSection(entry.getKey()); - if (configLoad.getString("Materials." + materialList + ".Points") != null) { - long pointsRequired = config.getFileConfiguration().getLong("Materials." + materialList + ".Points"); + long pointsRequired = current.getLong("Points", 0); - if (pointsRequired != 0) { - pointsEarned = pointsEarned + (materialAmount * pointsRequired); - } - } + if (pointsRequired != 0) pointsEarned = pointsEarned + (entry.getValue() * pointsRequired); } return pointsEarned; @@ -71,26 +82,25 @@ public class IslandLevel { Config config = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "levelling.yml")); FileConfiguration configLoad = config.getFileConfiguration(); - long pointsEarned = 0; + ConfigurationSection materialSection = configLoad.getConfigurationSection("Materials"); - if (this.materials.containsKey(material)) { - long materialAmount = this.materials.get(material); + if (materialSection == null) return 0; - if (configLoad.getString("Materials." + material + ".Points") != null) { - long pointsRequired = configLoad.getLong("Materials." + material + ".Points"); + ConfigurationSection current = materialSection.getConfigurationSection(material); - if (pointsRequired != 0) { - pointsEarned = materialAmount * pointsRequired; - } - } - } + if (current == null) return 0; - return pointsEarned; + Long boxedAmount = this.materials.get(material); + + if (boxedAmount == null) return 0; + + long pointsRequired = current.getLong("Points"); + + return pointsRequired == 0 ? 0 : boxedAmount * pointsRequired; } public long getLevel() { - long division = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")) - .getFileConfiguration().getLong("Island.Levelling.Division"); + long division = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration().getLong("Island.Levelling.Division"); if (division == 0) { division = 1; @@ -100,27 +110,19 @@ public class IslandLevel { } public void setMaterialAmount(String material, long amount) { - skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/level-data"), - ownerUUID.toString() + ".yml")) - .getFileConfiguration().set("Levelling.Materials." + material + ".Amount", amount); + skyblock.getFileManager().getConfig(new File(new File(skyblock.getDataFolder().toString() + "/level-data"), ownerUUID.toString() + ".yml")).getFileConfiguration() + .set("Levelling.Materials." + material + ".Amount", amount); this.materials.put(material, amount); } public long getMaterialAmount(String material) { - if (this.materials.containsKey(material)) { - return this.materials.get(material); - } - - return 0; + return this.materials.getOrDefault(material, 0l); } public void removeMaterial(String material) { - skyblock.getFileManager() - .getConfig(new File(new File(skyblock.getDataFolder().toString() + "/level-data"), - ownerUUID.toString() + ".yml")) - .getFileConfiguration().set("Levelling.Materials." + material, null); + skyblock.getFileManager().getConfig(new File(new File(skyblock.getDataFolder().toString() + "/level-data"), ownerUUID.toString() + ".yml")).getFileConfiguration() + .set("Levelling.Materials." + material, null); this.materials.remove(material); } @@ -138,8 +140,7 @@ public class IslandLevel { } public void setMaterials(Map materials) { - Config config = skyblock.getFileManager().getConfig( - new File(new File(skyblock.getDataFolder().toString() + "/level-data"), ownerUUID.toString() + ".yml")); + Config config = skyblock.getFileManager().getConfig(new File(new File(skyblock.getDataFolder().toString() + "/level-data"), ownerUUID.toString() + ".yml")); FileConfiguration configLoad = config.getFileConfiguration(); configLoad.set("Levelling.Materials", null); @@ -168,8 +169,7 @@ public class IslandLevel { } public void save() { - Config config = skyblock.getFileManager().getConfig( - new File(new File(skyblock.getDataFolder().toString() + "/level-data"), ownerUUID.toString() + ".yml")); + Config config = skyblock.getFileManager().getConfig(new File(new File(skyblock.getDataFolder().toString() + "/level-data"), ownerUUID.toString() + ".yml")); File configFile = config.getFile(); FileConfiguration configLoad = config.getFileConfiguration(); diff --git a/src/main/java/com/songoda/skyblock/island/IslandManager.java b/src/main/java/com/songoda/skyblock/island/IslandManager.java index 89765bc8..7682c017 100644 --- a/src/main/java/com/songoda/skyblock/island/IslandManager.java +++ b/src/main/java/com/songoda/skyblock/island/IslandManager.java @@ -615,6 +615,7 @@ public class IslandManager { if (island.isDeleted()) return; island.save(); + int islandMembers = island.getRole(IslandRole.Member).size() + island.getRole(IslandRole.Operator).size() + 1, islandVisitors = getVisitorsAtIsland(island).size(); @@ -627,7 +628,7 @@ public class IslandManager { if (island.hasRole(IslandRole.Member, all.getUniqueId()) || island.hasRole(IslandRole.Operator, all.getUniqueId()) - || island.hasRole(IslandRole.Owner, all.getUniqueId())) { + || island.hasRole(IslandRole.Owner, all.getUniqueId()) || island.getCoopType(all.getUniqueId()) == IslandCoop.NORMAL) { if (scoreboardManager != null) { try { if (islandMembers == 1 && islandVisitors == 0) { @@ -1291,7 +1292,9 @@ public class IslandManager { for (UUID coopPlayerAtIslandList : getCoopPlayersAtIsland(island)) { Player targetPlayer = Bukkit.getServer().getPlayer(coopPlayerAtIslandList); - + + if(island.getCoopType(coopPlayerAtIslandList) == IslandCoop.NORMAL) continue; + if (targetPlayer != null) { LocationUtil.teleportPlayerToSpawn(targetPlayer); diff --git a/src/main/java/com/songoda/skyblock/island/IslandRole.java b/src/main/java/com/songoda/skyblock/island/IslandRole.java index d33c4bc9..84b40259 100644 --- a/src/main/java/com/songoda/skyblock/island/IslandRole.java +++ b/src/main/java/com/songoda/skyblock/island/IslandRole.java @@ -1,7 +1,21 @@ package com.songoda.skyblock.island; +import java.util.Collections; +import java.util.EnumSet; +import java.util.Set; + public enum IslandRole { - Coop, Visitor, Member, Operator, Owner + Coop, + Visitor, + Member, + Operator, + Owner; + + private static final Set roles = Collections.unmodifiableSet(EnumSet.allOf(IslandRole.class)); + + public static Set getRoles() { + return roles; + } } diff --git a/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopBank.java b/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopBank.java index 1c190bc8..773b200d 100644 --- a/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopBank.java +++ b/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopBank.java @@ -27,9 +27,9 @@ public class TopBank extends DataCollector { @Override public List> requestAll() { - Map topLevels = new HashMap<>(); List leaderboards = skyblock.getLeaderboardManager().getLeaderboard(Type.Bank); + Map topLevels = new HashMap<>(leaderboards.size()); for (Leaderboard leaderboard : leaderboards) { Visit visit = leaderboard.getVisit(); diff --git a/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopLevel.java b/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopLevel.java index 93325596..19d110d5 100644 --- a/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopLevel.java +++ b/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopLevel.java @@ -26,9 +26,9 @@ public class TopLevel extends DataCollector { @Override public List> requestAll() { - Map topLevels = new HashMap<>(); List leaderboards = skyblock.getLeaderboardManager().getLeaderboard(Leaderboard.Type.Level); + Map topLevels = new HashMap<>(leaderboards.size()); for (int i = 0; i < leaderboards.size(); i++) { Leaderboard leaderboard = leaderboards.get(i); diff --git a/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopVotes.java b/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopVotes.java index 0b152305..64c388ec 100644 --- a/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopVotes.java +++ b/src/main/java/com/songoda/skyblock/leaderboard/leaderheads/TopVotes.java @@ -27,10 +27,10 @@ public class TopVotes extends DataCollector { @Override public List> requestAll() { - Map topLevels = new HashMap<>(); - + List leaderboards = skyblock.getLeaderboardManager().getLeaderboard(Type.Votes); - + Map topLevels = new HashMap<>(leaderboards.size()); + for (int i = 0; i < leaderboards.size(); i++) { Leaderboard leaderboard = leaderboards.get(i); Visit visit = leaderboard.getVisit(); diff --git a/src/main/java/com/songoda/skyblock/limit/EnumLimitation.java b/src/main/java/com/songoda/skyblock/limit/EnumLimitation.java new file mode 100644 index 00000000..7a0b6781 --- /dev/null +++ b/src/main/java/com/songoda/skyblock/limit/EnumLimitation.java @@ -0,0 +1,77 @@ +package com.songoda.skyblock.limit; + +import java.util.EnumMap; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import org.bukkit.configuration.ConfigurationSection; + +public abstract class EnumLimitation> implements Limitation { + + private long defaultLimit; + private final Class type; + private final Map map; + + public EnumLimitation(Class type) { + this.defaultLimit = -1; + this.map = new EnumMap<>(type); + this.type = type; + } + + public boolean isBeingTracked(Enum type) { + return map.containsKey(type) || getDefault() >= 0; + } + + protected Map getMap() { + return map; + } + + public long getDefault() { + return defaultLimit; + } + + public boolean hasTooMuch(long currentAmount, Enum type) { + final long cached = map.getOrDefault(type, getDefault()); + + if (cached <= -1) return false; + + return currentAmount > cached; + } + + @Override + public void reload(ConfigurationSection loadFrom) { + unload(); + + if (loadFrom == null) return; + + final Set keys = loadFrom.getKeys(false); + + removeAndLoadDefaultLimit(loadFrom, keys); + + for (String key : keys) { + + final String enumName = key.toUpperCase(Locale.ENGLISH); + + try { + map.put(Enum.valueOf(type, enumName), loadFrom.getLong(key)); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("Incorrect enum constant '" + enumName + "' in " + loadFrom.getCurrentPath(), e); + } + + } + + } + + @Override + public void unload() { + map.clear(); + defaultLimit = -1; + } + + protected void removeAndLoadDefaultLimit(ConfigurationSection loadFrom, Set keys) { + keys.remove("DefaultLimit"); + defaultLimit = loadFrom.getInt("DefaultLimit", -1); + } + +} diff --git a/src/main/java/com/songoda/skyblock/limit/LimitManager.java b/src/main/java/com/songoda/skyblock/limit/LimitManager.java deleted file mode 100644 index e99fe007..00000000 --- a/src/main/java/com/songoda/skyblock/limit/LimitManager.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.songoda.skyblock.limit; - -import com.songoda.skyblock.SkyBlock; -import com.songoda.skyblock.island.Island; -import com.songoda.skyblock.island.IslandManager; -import com.songoda.skyblock.utils.version.Materials; -import org.bukkit.block.Block; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.permissions.PermissionAttachmentInfo; - -import java.io.File; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - -public class LimitManager { - - private final SkyBlock skyblock; - private Map blockLimits; - - public LimitManager(SkyBlock skyblock) { - this.skyblock = skyblock; - this.blockLimits = new HashMap<>(); - - this.reload(); - } - - public void reload() { - this.blockLimits.clear(); - - FileConfiguration limitsConfig = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "limits.yml")).getFileConfiguration(); - ConfigurationSection blockLimitSection = limitsConfig.getConfigurationSection("block"); - - if (blockLimitSection != null) { - for (String materialString : blockLimitSection.getKeys(false)) { - Materials material = Materials.fromString(materialString); - if (material != null) { - long limit = blockLimitSection.getLong(materialString); - this.blockLimits.put(material, limit); - } - } - } - } - - /** - * Gets the max number of a type of block a player can place - * - * @param player The player to check - * @param block The block to check - * @return The max number of the type of block the player can place - */ - public long getBlockLimit(Player player, Block block) { - if (player == null || block == null) - return -1; - - if (player.hasPermission("fabledskyblock.limit.block.*")) - return -1; - - Materials material = Materials.getMaterials(block.getType(), block.getData()); - if (material == null) - return -1; - - long limit = -1; - if (this.blockLimits.containsKey(material)) - limit = Math.max(limit, this.blockLimits.get(material)); - - Set permissions = player.getEffectivePermissions() - .stream() - .filter(x -> x.getPermission().toLowerCase().startsWith("fabledskyblock.limit.block." + material.name().toLowerCase())) - .collect(Collectors.toSet()); - - for (PermissionAttachmentInfo permission : permissions) { - try { - String permString = permission.getPermission(); - String numberString = permString.substring(permString.lastIndexOf(".") + 1); - if (numberString.equals("*")) - return -1; - - limit = Math.max(limit, Integer.parseInt(numberString)); - } catch (Exception ignored) { - } - } - - return limit; - } - - /** - * Checks if a player has exceeded the number of blocks they can place - * - * @param player The player to check - * @param block The block to check - * @return true if the player has exceeded the block limit, otherwise false - */ - public boolean isBlockLimitExceeded(Player player, Block block) { - IslandManager islandManager = this.skyblock.getIslandManager(); - - long limit = this.getBlockLimit(player, block); - if (limit == -1) - return false; - - Island island = islandManager.getIslandAtLocation(block.getLocation()); - long totalPlaced; - if (block.getType() == Materials.SPAWNER.parseMaterial()) { - totalPlaced = island.getLevel().getMaterials().entrySet().stream().filter(x -> x.getKey().contains("SPAWNER")).mapToLong(Map.Entry::getValue).sum(); - } else { - totalPlaced = island.getLevel().getMaterialAmount(Materials.getMaterials(block.getType(), block.getData()).name()); - } - - return limit < totalPlaced + 1; - } - -} diff --git a/src/main/java/com/songoda/skyblock/limit/Limitation.java b/src/main/java/com/songoda/skyblock/limit/Limitation.java new file mode 100644 index 00000000..3d554ca0 --- /dev/null +++ b/src/main/java/com/songoda/skyblock/limit/Limitation.java @@ -0,0 +1,13 @@ +package com.songoda.skyblock.limit; + +import org.bukkit.configuration.ConfigurationSection; + +public interface Limitation { + + void unload(); + + void reload(ConfigurationSection loadFrom); + + String getSectionName(); + +} diff --git a/src/main/java/com/songoda/skyblock/limit/LimitationInstanceHandler.java b/src/main/java/com/songoda/skyblock/limit/LimitationInstanceHandler.java new file mode 100644 index 00000000..555478ea --- /dev/null +++ b/src/main/java/com/songoda/skyblock/limit/LimitationInstanceHandler.java @@ -0,0 +1,42 @@ +package com.songoda.skyblock.limit; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.configuration.Configuration; + +import com.songoda.skyblock.SkyBlock; +import com.songoda.skyblock.limit.impl.BlockLimitation; +import com.songoda.skyblock.limit.impl.EntityLimitaton; + +public final class LimitationInstanceHandler { + + private final Map, Limitation> instances; + + public LimitationInstanceHandler() { + this.instances = new HashMap<>(); + registerInstance(new EntityLimitaton()); + registerInstance(new BlockLimitation()); + reloadAll(); + } + + public T getInstance(Class type) { + return type.cast(instances.get(type)); + } + + public void registerInstance(Limitation instance) { + instances.put(instance.getClass(), instance); + } + + public void reloadAll() { + final SkyBlock instance = SkyBlock.getInstance(); + final Configuration config = instance.getFileManager().getConfig(new File(instance.getDataFolder(), "limits.yml")).getFileConfiguration(); + + for (Limitation limit : instances.values()) { + limit.reload(config.getConfigurationSection(limit.getSectionName())); + } + + } + +} diff --git a/src/main/java/com/songoda/skyblock/limit/impl/BlockLimitation.java b/src/main/java/com/songoda/skyblock/limit/impl/BlockLimitation.java new file mode 100644 index 00000000..00333724 --- /dev/null +++ b/src/main/java/com/songoda/skyblock/limit/impl/BlockLimitation.java @@ -0,0 +1,106 @@ +package com.songoda.skyblock.limit.impl; + +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import org.bukkit.block.Block; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; +import org.bukkit.permissions.PermissionAttachmentInfo; + +import com.songoda.skyblock.SkyBlock; +import com.songoda.skyblock.island.Island; +import com.songoda.skyblock.island.IslandManager; +import com.songoda.skyblock.limit.EnumLimitation; +import com.songoda.skyblock.utils.version.Materials; + +public final class BlockLimitation extends EnumLimitation { + + public BlockLimitation() { + super(Materials.class); + } + + @Override + public String getSectionName() { + return "block"; + } + + @Override + public boolean hasTooMuch(long currentAmount, Enum type) { + throw new UnsupportedOperationException("Not implemented. Use getBlockLimit and isBlockLimitExceeded instead."); + } + + @Override + public void reload(ConfigurationSection loadFrom) { + unload(); + + if (loadFrom == null) return; + + final Set keys = loadFrom.getKeys(false); + + removeAndLoadDefaultLimit(loadFrom, keys); + + for (String key : keys) { + final String enumName = key.toUpperCase(Locale.ENGLISH); + final Materials type = Materials.fromString(enumName); + + if (type == null) throw new IllegalArgumentException("Unable to parse Materials from '" + enumName + "' in " + loadFrom.getCurrentPath()); + + getMap().put(type, loadFrom.getLong(key)); + } + + } + + @SuppressWarnings("deprecation") + public long getBlockLimit(Player player, Block block) { + if (player == null || block == null) return -1; + + if (player.hasPermission("fabledskyblock.limit.block.*")) return -1; + + final Materials material = Materials.getMaterials(block.getType(), block.getData()); + + if (material == null) return -1; + + long limit = getMap().getOrDefault(material, getDefault()); + + final String name = material.name().toLowerCase(); + + Set permissions = player.getEffectivePermissions().stream() + .filter(x -> x.getPermission().toLowerCase().startsWith("fabledskyblock.limit.block." + name)).collect(Collectors.toSet()); + + for (PermissionAttachmentInfo permission : permissions) { + try { + String permString = permission.getPermission(); + String numberString = permString.substring(permString.lastIndexOf(".") + 1); + if (numberString.equals("*")) return -1; + + limit = Math.max(limit, Integer.parseInt(numberString)); + } catch (Exception ignored) { + } + } + + return limit; + } + + @SuppressWarnings("deprecation") + public boolean isBlockLimitExceeded(Player player, Block block, long limit) { + + if (limit == -1) return false; + + final IslandManager islandManager = SkyBlock.getInstance().getIslandManager(); + final Island island = islandManager.getIslandAtLocation(block.getLocation()); + final long totalPlaced; + + if (block.getType() == Materials.SPAWNER.parseMaterial()) { + totalPlaced = island.getLevel().getMaterials().entrySet().stream().filter(x -> x.getKey().contains("SPAWNER")) + .mapToLong(Map.Entry::getValue).sum(); + } else { + totalPlaced = island.getLevel().getMaterialAmount(Materials.getMaterials(block.getType(), block.getData()).name()); + } + + return limit < totalPlaced + 1; + } + +} diff --git a/src/main/java/com/songoda/skyblock/limit/impl/EntityLimitaton.java b/src/main/java/com/songoda/skyblock/limit/impl/EntityLimitaton.java new file mode 100644 index 00000000..984e8b2e --- /dev/null +++ b/src/main/java/com/songoda/skyblock/limit/impl/EntityLimitaton.java @@ -0,0 +1,55 @@ +package com.songoda.skyblock.limit.impl; + +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; + +import com.songoda.skyblock.island.Island; +import com.songoda.skyblock.island.IslandEnvironment; +import com.songoda.skyblock.island.IslandWorld; +import com.songoda.skyblock.limit.EnumLimitation; + +public final class EntityLimitaton extends EnumLimitation { + + public EntityLimitaton() { + super(EntityType.class); + } + + public long getEntityCount(Island island, IslandWorld islandWorld, EntityType type) { + final Location islandLocation = island.getLocation(islandWorld, IslandEnvironment.Island); + final World world = islandLocation.getWorld(); + + final Location minLocation = new Location(world, islandLocation.getBlockX() - island.getRadius(), 0, + islandLocation.getBlockZ() - island.getRadius()); + final Location maxLocation = new Location(world, islandLocation.getBlockX() + island.getRadius(), world.getMaxHeight(), + islandLocation.getBlockZ() + island.getRadius()); + + final int minX = Math.min(maxLocation.getBlockX(), minLocation.getBlockX()); + final int minZ = Math.min(maxLocation.getBlockZ(), minLocation.getBlockZ()); + + final int maxX = Math.max(maxLocation.getBlockX(), minLocation.getBlockX()); + final int maxZ = Math.max(maxLocation.getBlockZ(), minLocation.getBlockZ()); + + int count = 0; + + for (int x = minX; x < maxX + 16; x += 16) { + for (int z = minZ; z < maxZ + 16; z += 16) { + final Chunk chunk = world.getChunkAt(x >> 4, z >> 4); + + for (Entity ent : chunk.getEntities()) { + if (ent.getType() == type) count++; + } + + } + } + return count; + } + + @Override + public String getSectionName() { + return "entity"; + } + +} diff --git a/src/main/java/com/songoda/skyblock/listeners/Block.java b/src/main/java/com/songoda/skyblock/listeners/Block.java index 0b19c20f..91ef9931 100644 --- a/src/main/java/com/songoda/skyblock/listeners/Block.java +++ b/src/main/java/com/songoda/skyblock/listeners/Block.java @@ -43,7 +43,7 @@ import com.songoda.skyblock.island.IslandManager; import com.songoda.skyblock.island.IslandRole; import com.songoda.skyblock.island.IslandWorld; import com.songoda.skyblock.levelling.LevellingManager; -import com.songoda.skyblock.limit.LimitManager; +import com.songoda.skyblock.limit.impl.BlockLimitation; import com.songoda.skyblock.stackable.Stackable; import com.songoda.skyblock.stackable.StackableManager; import com.songoda.skyblock.utils.NumberUtil; @@ -74,14 +74,17 @@ public class Block implements Listener { if (!worldManager.isIslandWorld(block.getWorld())) return; IslandWorld world = worldManager.getIslandWorld(block.getWorld()); - Island island = islandManager.getIslandAtLocation(block.getLocation()); + + Location blockLocation = block.getLocation(); + + Island island = islandManager.getIslandAtLocation(blockLocation); if (island == null) { event.setCancelled(true); return; } - if (!islandManager.hasPermission(player, block.getLocation(), "Destroy")) { + if (!islandManager.hasPermission(player, blockLocation, "Destroy")) { event.setCancelled(true); skyblock.getMessageManager().sendMessage(player, skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) @@ -91,7 +94,7 @@ public class Block implements Listener { } if (stackableManager != null - && stackableManager.isStacked(block.getLocation())) { + && stackableManager.isStacked(blockLocation)) { Stackable stackable = stackableManager.getStack(block.getLocation(), block.getType()); if (stackable != null) { Material material = block.getType(); @@ -99,7 +102,7 @@ public class Block implements Listener { int droppedAmount = 0; if (event.getPlayer().isSneaking()) { - Location dropLoc = event.getBlock().getLocation().add(0.5, 0.5, 0.5); + Location dropLoc = blockLocation.clone().add(0.5, 0.5, 0.5); int count = stackable.getSize(); droppedAmount = count; while (count > 64) { @@ -110,7 +113,7 @@ public class Block implements Listener { block.setType(Material.AIR); stackable.setSize(0); } else { - block.getWorld().dropItemNaturally(block.getLocation().add(.5, 1, .5), new ItemStack(material, 1, data)); + block.getWorld().dropItemNaturally(blockLocation.clone().add(.5, 1, .5), new ItemStack(material, 1, data)); stackable.takeOne(); droppedAmount = 1; } @@ -247,15 +250,18 @@ public class Block implements Listener { } } - LimitManager limitManager = skyblock.getLimitManager(); - if (limitManager.isBlockLimitExceeded(player, block)) { + BlockLimitation limits = skyblock.getLimitationHandler().getInstance(BlockLimitation.class); + + long limit = limits.getBlockLimit(player, block); + + if (limits.isBlockLimitExceeded(player, block, limit)) { Materials material = Materials.getMaterials(block.getType(), block.getData()); skyblock.getMessageManager().sendMessage(player, skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) .getFileConfiguration().getString("Island.Limit.Block.Exceeded.Message") .replace("%type", WordUtils.capitalizeFully(material.name().replace("_", " "))) - .replace("%limit", NumberUtil.formatNumber(limitManager.getBlockLimit(player, block)))); + .replace("%limit", NumberUtil.formatNumber(limit))); skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); event.setCancelled(true); diff --git a/src/main/java/com/songoda/skyblock/listeners/Entity.java b/src/main/java/com/songoda/skyblock/listeners/Entity.java index 4967ae3c..064c60af 100644 --- a/src/main/java/com/songoda/skyblock/listeners/Entity.java +++ b/src/main/java/com/songoda/skyblock/listeners/Entity.java @@ -3,6 +3,7 @@ package com.songoda.skyblock.listeners; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.EnumSet; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -55,7 +56,6 @@ import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitRunnable; -import com.google.common.collect.Sets; import com.songoda.skyblock.SkyBlock; import com.songoda.skyblock.config.FileManager; import com.songoda.skyblock.config.FileManager.Config; @@ -65,6 +65,7 @@ import com.songoda.skyblock.island.IslandLevel; import com.songoda.skyblock.island.IslandManager; import com.songoda.skyblock.island.IslandRole; import com.songoda.skyblock.island.IslandWorld; +import com.songoda.skyblock.limit.impl.EntityLimitaton; import com.songoda.skyblock.message.MessageManager; import com.songoda.skyblock.sound.SoundManager; import com.songoda.skyblock.stackable.StackableManager; @@ -77,691 +78,698 @@ import com.songoda.skyblock.world.WorldManager; public class Entity implements Listener { - private final SkyBlock skyblock; - - private Set preventFireTicks = new HashSet<>(); - - public Entity(SkyBlock skyblock) { - this.skyblock = skyblock; - } - - @EventHandler - public void onEntityDamage(EntityDamageEvent event) { - if (!(event.getEntity() instanceof Player)) { - return; - } - - Player player = (Player) event.getEntity(); - - FileManager fileManager = skyblock.getFileManager(); - - if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { - if (event.getCause() != null) { - if (event.getCause() == DamageCause.VOID) { - return; - } else if (event.getCause() == DamageCause.ENTITY_ATTACK) { - EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) event; - - if (entityDamageByEntityEvent.getDamager() != null - && entityDamageByEntityEvent.getDamager() instanceof Player) { - return; - } - } else { - if (NMSUtil.getVersionNumber() > 11) { - if (event.getCause() == DamageCause.valueOf("ENTITY_SWEEP_ATTACK")) { - EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) event; - - if (entityDamageByEntityEvent.getDamager() != null - && entityDamageByEntityEvent.getDamager() instanceof Player) { - return; - } - } - } - } - } - - Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")); - FileConfiguration configLoad = config.getFileConfiguration(); - - if (configLoad.getBoolean("Island.Settings.Damage.Enable")) { - if (!skyblock.getIslandManager().hasSetting(player.getLocation(), IslandRole.Owner, "Damage")) { - event.setCancelled(true); - } - } else if (!configLoad.getBoolean("Island.Damage.Enable")) { - event.setCancelled(true); - } - } - - // Fix a bug in minecraft where arrows with flame still apply fire ticks even if the shot entity isn't damaged - if (preventFireTicks.contains(player.getUniqueId()) && event.getCause() == DamageCause.FIRE_TICK) { - player.setFireTicks(0); - event.setCancelled(true); - } - } - - @EventHandler - public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { - MessageManager messageManager = skyblock.getMessageManager(); - IslandManager islandManager = skyblock.getIslandManager(); - SoundManager soundManager = skyblock.getSoundManager(); - FileManager fileManager = skyblock.getFileManager(); - - if (event.getDamager() instanceof Player) { - Player player = (Player) event.getDamager(); - org.bukkit.entity.Entity entity = event.getEntity(); - - if (skyblock.getWorldManager().isIslandWorld(entity.getWorld())) { - if (entity instanceof Player) { - Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")); - FileConfiguration configLoad = config.getFileConfiguration(); - - if (configLoad.getBoolean("Island.Settings.PvP.Enable")) { - if (!islandManager.hasSetting(entity.getLocation(), IslandRole.Owner, "PvP")) { - event.setCancelled(true); - } - } else if (!configLoad.getBoolean("Island.PvP.Enable")) { - event.setCancelled(true); - } - } else if (entity instanceof ArmorStand) { - if (!islandManager.hasPermission(player, entity.getLocation(), "Destroy")) { - event.setCancelled(true); - - messageManager.sendMessage(player, - fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")) - .getFileConfiguration().getString("Island.Settings.Permission.Message")); - soundManager.playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); - } - } else { - if (!islandManager.hasPermission(player, entity.getLocation(), "MobHurting")) { - event.setCancelled(true); - - messageManager.sendMessage(player, - fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")) - .getFileConfiguration().getString("Island.Settings.Permission.Message")); - soundManager.playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); - - return; - } - } - } - - return; - } - - if (event.getDamager() instanceof Projectile && ((Projectile) event.getDamager()).getShooter() instanceof Player) { - Player player = (Player) ((Projectile) event.getDamager()).getShooter(); - org.bukkit.entity.Entity entity = event.getEntity(); - - if (skyblock.getWorldManager().isIslandWorld(entity.getWorld())) { - if (event.getEntity() instanceof Player) { - Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")); - FileConfiguration configLoad = config.getFileConfiguration(); - - if (entity.getType() == EntityType.ITEM_FRAME && !islandManager.hasPermission(player, entity.getLocation(), "HangingDestroy")) { - event.setCancelled(true); - return; - } - - if (configLoad.getBoolean("Island.Settings.PvP.Enable")) { - if (!islandManager.hasSetting(entity.getLocation(), IslandRole.Owner, "PvP")) { - event.setCancelled(true); - } - } else if (!configLoad.getBoolean("Island.PvP.Enable")) { - event.setCancelled(true); - } - } else { - if (!islandManager.hasPermission(player, entity.getLocation(), "MobHurting")) { - event.setCancelled(true); - - messageManager.sendMessage(player, - fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")) - .getFileConfiguration().getString("Island.Settings.Permission.Message")); - soundManager.playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); - - return; - } - } - } - } else if (event.getEntity() instanceof Player) { - Player player = (Player) event.getEntity(); - - if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { - Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")); - FileConfiguration configLoad = config.getFileConfiguration(); - - if (configLoad.getBoolean("Island.Settings.Damage.Enable")) { - if (!islandManager.hasSetting(player.getLocation(), IslandRole.Owner, "Damage") - || (event.getDamager() instanceof TNTPrimed && !islandManager - .hasSetting(player.getLocation(), IslandRole.Owner, "Explosions"))) { - event.setCancelled(true); - } - } else if (!configLoad.getBoolean("Island.Damage.Enable")) { - event.setCancelled(true); - } - } - } else if (event.getDamager() instanceof TNTPrimed) { - org.bukkit.entity.Entity entity = event.getEntity(); - - if (skyblock.getWorldManager().isIslandWorld(entity.getWorld())) { - if (!islandManager.hasSetting(entity.getLocation(), IslandRole.Owner, "Explosions")) { - event.setCancelled(true); - } - } - } - - // Fix a bug in minecraft where arrows with flame still apply fire ticks even if the shot entity isn't damaged - if (event.isCancelled() && event.getEntity() != null && event.getDamager() instanceof Arrow && ((Arrow) event.getDamager()).getShooter() instanceof Player) { - Arrow arrow = (Arrow) event.getDamager(); - if (arrow.getFireTicks() != 0) { - preventFireTicks.add(event.getEntity().getUniqueId()); - new BukkitRunnable() { - public void run() { - preventFireTicks.remove(event.getEntity().getUniqueId()); - } - }.runTaskLater(SkyBlock.getInstance(), 5L); - } - } - } - - @EventHandler - public void onPlayerShearEntity(PlayerShearEntityEvent event) { - Player player = event.getPlayer(); - - if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { - if (!skyblock.getIslandManager().hasPermission(player, event.getEntity().getLocation(), "Shearing")) { - event.setCancelled(true); - - skyblock.getMessageManager().sendMessage(player, - skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) - .getFileConfiguration().getString("Island.Settings.Permission.Message")); - skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); - } - } - } - - /** - * Checks that an entity is not targeting another entity on different islands. - * @author LimeGlass - */ - @EventHandler - public void onEntityTarget(EntityTargetEvent event) { - org.bukkit.entity.Entity entity = event.getEntity(); - WorldManager worldManager = skyblock.getWorldManager(); - if (!worldManager.isIslandWorld(entity.getWorld())) - return; - - org.bukkit.entity.Entity target = event.getTarget(); - // Somehow the target can be null, thanks Spigot. - if (target == null) - return; - - IslandManager islandManager = skyblock.getIslandManager(); - Island entityIsland = islandManager.getIslandAtLocation(entity.getLocation()); - Island targetIsland = islandManager.getIslandAtLocation(target.getLocation()); - // Event not related to Skyblock islands. - if (entityIsland == null && targetIsland == null) - return; - // One entity is on an island, and the other isn't. - if (entityIsland == null || targetIsland == null) { - event.setCancelled(true); - return; - } - // Both entities are on different islands. - if (!entityIsland.getIslandUUID().equals(targetIsland.getIslandUUID())) { - event.setCancelled(true); - return; - } - } - - @EventHandler - public void onStackableInteract(PlayerArmorStandManipulateEvent event) { - Player player = event.getPlayer(); - if (!skyblock.getIslandManager().hasPermission(player, event.getRightClicked().getLocation(), "ArmorStandUse")) { - event.setCancelled(true); - - skyblock.getMessageManager().sendMessage(player, - skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) - .getFileConfiguration().getString("Island.Settings.Permission.Message")); - skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); - } - - if (NMSUtil.getVersionNumber() != 8) - return; - - StackableManager stackableManager = SkyBlock.getInstance().getStackableManager(); - if (stackableManager == null) - return; - - ArmorStand armorStand = event.getRightClicked(); - for (Location stackLocation : stackableManager.getStacks().keySet()) { - if (stackLocation.getWorld().equals(armorStand.getWorld()) && armorStand.getLocation().distanceSquared(stackLocation) <= 1.5) { - event.setCancelled(true); - return; - } - } - } - - @EventHandler - public void onHangingPlace(HangingPlaceEvent event) { - Player player = event.getPlayer(); - - if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { - if (!skyblock.getIslandManager().hasPermission(player, event.getEntity().getLocation(), - "EntityPlacement")) { - event.setCancelled(true); - - skyblock.getMessageManager().sendMessage(player, - skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) - .getFileConfiguration().getString("Island.Settings.Permission.Message")); - skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); - } - } - } - - @EventHandler - public void onHangingBreak(HangingBreakEvent event) { - Hanging hanging = event.getEntity(); - - if (event.getCause() != RemoveCause.EXPLOSION) { - return; - } - - if (skyblock.getWorldManager().isIslandWorld(hanging.getWorld())) { - if (!skyblock.getIslandManager().hasSetting(hanging.getLocation(), IslandRole.Owner, "Explosions")) { - event.setCancelled(true); - } - } - } - - @EventHandler - public void onHangingBreak(HangingBreakByEntityEvent event) { - Hanging hanging = event.getEntity(); - - if (!(event.getRemover() instanceof Player)) { - return; - } - - Player player = (Player) event.getRemover(); - - if (skyblock.getWorldManager().isIslandWorld(hanging.getWorld())) { - if (!skyblock.getIslandManager().hasPermission(player, hanging.getLocation(), "HangingDestroy")) { - event.setCancelled(true); - - skyblock.getMessageManager().sendMessage(player, - skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) - .getFileConfiguration().getString("Island.Settings.Permission.Message")); - skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); - } - } - } - - @EventHandler - public void onHangingInteract(PlayerInteractEntityEvent event) { - if (!(event.getRightClicked() instanceof Hanging)) { - return; - } - - Player player = event.getPlayer(); - Hanging hanging = (Hanging) event.getRightClicked(); - - if (skyblock.getWorldManager().isIslandWorld(hanging.getWorld())) { - if (!skyblock.getIslandManager().hasPermission(player, hanging.getLocation(), "HangingDestroy")) { - event.setCancelled(true); - - skyblock.getMessageManager().sendMessage(player, - skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) - .getFileConfiguration().getString("Island.Settings.Permission.Message")); - skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); - } - } - } - - @EventHandler - public void onHangingRemoveItem(EntityDamageByEntityEvent event) { - if (!(event.getDamager() instanceof Player && event.getEntity() instanceof Hanging)) { - return; - } - - Player player = (Player) event.getDamager(); - Hanging hanging = (Hanging) event.getEntity(); - - if (skyblock.getWorldManager().isIslandWorld(hanging.getWorld())) { - if (!skyblock.getIslandManager().hasPermission(player, hanging.getLocation(), "HangingDestroy")) { - event.setCancelled(true); - - skyblock.getMessageManager().sendMessage(player, - skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) - .getFileConfiguration().getString("Island.Settings.Permission.Message")); - skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); - } - } - } - - @EventHandler - public void onEntityTaming(EntityTameEvent event) { - if (!(event.getOwner() instanceof Player)) { - return; - } - - Player player = (Player) event.getOwner(); - - if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { - if (!skyblock.getIslandManager().hasPermission(player, event.getEntity().getLocation(), "MobTaming")) { - event.setCancelled(true); - - skyblock.getMessageManager().sendMessage(player, - skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) - .getFileConfiguration().getString("Island.Settings.Permission.Message")); - skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); - } - } - } - - @EventHandler - @SuppressWarnings("deprecation") - public void onEntityChangeBlock(EntityChangeBlockEvent event) { - org.bukkit.entity.Entity entity = event.getEntity(); - - if (entity instanceof Player) { - return; - } - - IslandManager islandManager = skyblock.getIslandManager(); - WorldManager worldManager = skyblock.getWorldManager(); - - Island island = islandManager.getIslandAtLocation(event.getBlock().getLocation()); - - if (island == null || !skyblock.getWorldManager().isIslandWorld(entity.getWorld())) return; - - if (event.isCancelled()) return; - - Config config = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")); - FileConfiguration configLoad = config.getFileConfiguration(); - - IslandWorld world = worldManager.getIslandWorld(event.getBlock().getWorld()); - - org.bukkit.block.Block block = event.getBlock(); - - // Check spawn block falling, this can be a bit glitchy, but it's better than nothing - if ((LocationUtil.isLocationLocation(block.getLocation(), island.getLocation(world, IslandEnvironment.Main).clone().subtract(0, 1, 0)) - || LocationUtil.isLocationLocation(block.getLocation(), island.getLocation(world, IslandEnvironment.Visitor).clone().subtract(0, 1, 0))) && - skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration().getBoolean("Island.Spawn.Protection")) { - event.setCancelled(true); - return; - } - - if ((event.getEntityType() == EntityType.FALLING_BLOCK) && LocationUtil.isLocationLocation(event.getBlock().getLocation(), - island.getLocation(world, IslandEnvironment.Main) - .clone()) - && configLoad.getBoolean("Island.Spawn.Protection")) { - FallingBlock fallingBlock = (FallingBlock) event.getEntity(); - if (fallingBlock.getDropItem()) { - if (NMSUtil.getVersionNumber() > 12) { - fallingBlock.getWorld().dropItemNaturally(fallingBlock.getLocation(), new ItemStack(fallingBlock.getBlockData().getMaterial(), 1)); - } else { - try { - Method getBlockDataMethod = FallingBlock.class.getMethod("getBlockData"); - byte data = (byte) getBlockDataMethod.invoke(fallingBlock); - if (fallingBlock.getMaterial().name().endsWith("ANVIL")) { - data = (byte) Math.ceil(data / 4.0); - } - fallingBlock.getWorld().dropItemNaturally(fallingBlock.getLocation(), new ItemStack(fallingBlock.getMaterial(), 1, data)); - } catch (Exception ignored) { - } - } - } - event.setCancelled(true); - } - - if (entity instanceof FallingBlock) - return; - - // Check entities interacting with spawn - if (LocationUtil.isLocationAffectingIslandSpawn(block.getLocation(), island, world) && - skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration().getBoolean("Island.Spawn.Protection")) { - event.setCancelled(true); - return; - } - - if (!islandManager.hasSetting(entity.getLocation(), IslandRole.Owner, "MobGriefing")) { - event.setCancelled(true); - } - - if (!skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")) - .getFileConfiguration().getBoolean("Island.Block.Level.Enable")) return; - - Materials materials = Materials.getMaterials(block.getType(), block.getData()); - - if (materials != null) { - IslandLevel level = island.getLevel(); - - if (level.hasMaterial(materials.name())) { - long materialAmount = level.getMaterialAmount(materials.name()); - - if (materialAmount - 1 <= 0) { - level.removeMaterial(materials.name()); - } else { - level.setMaterialAmount(materials.name(), materialAmount - 1); - } - } - } - - if (event.getTo() != null && event.getTo() != Material.AIR) { - materials = null; - - if (NMSUtil.getVersionNumber() > 12) { - materials = Materials.fromString(event.getTo().name()); - } else { - try { - materials = Materials.requestMaterials(event.getTo().name(), - (byte) event.getClass().getMethod("getData").invoke(event)); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException - | NoSuchMethodException | SecurityException e) { - e.printStackTrace(); - } - } - - if (materials != null) { - long materialAmount = 0; - IslandLevel level = island.getLevel(); - - if (level.hasMaterial(materials.name())) { - materialAmount = level.getMaterialAmount(materials.name()); - } - - level.setMaterialAmount(materials.name(), materialAmount + 1); - } - } - - } - - @EventHandler - public void onEntityExplode(EntityExplodeEvent event) { - org.bukkit.entity.Entity entity = event.getEntity(); - - WorldManager worldManager = skyblock.getWorldManager(); - IslandManager islandManager = skyblock.getIslandManager(); - - if (skyblock.getWorldManager().isIslandWorld(entity.getWorld())) { - if (!islandManager.hasSetting(entity.getLocation(), IslandRole.Owner, "Explosions")) { - event.setCancelled(true); - } - - if (!event.isCancelled()) { - Island island = islandManager.getIslandAtLocation(entity.getLocation()); - - if (island != null) { - if (skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")) - .getFileConfiguration().getBoolean("Island.Block.Level.Enable")) { - for (org.bukkit.block.Block blockList : event.blockList()) { - @SuppressWarnings("deprecation") - Materials materials = Materials.getMaterials(blockList.getType(), blockList.getData()); - - if (materials != null) { - IslandLevel level = island.getLevel(); - - if (level.hasMaterial(materials.name())) { - long materialAmount = level.getMaterialAmount(materials.name()); - - if (materialAmount - 1 <= 0) { - level.removeMaterial(materials.name()); - } else { - level.setMaterialAmount(materials.name(), materialAmount - 1); - } - } - } - } - } - - if (SkyBlock.getInstance().getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration().getBoolean("Island.Spawn.Protection")) { - IslandWorld world = worldManager.getIslandWorld(event.getEntity().getWorld()); - for (org.bukkit.block.Block block : event.blockList()) { - if (LocationUtil.isLocationLocation(block.getLocation(), - island.getLocation(world, IslandEnvironment.Main) - .clone() - .subtract(0.0D, 1.0D, 0.0D))) { - event.blockList().remove(block); - break; - } - } - } - } - } - } - } - - @EventHandler(priority = EventPriority.MONITOR) - public void onEntityDeath(EntityDeathEvent event) { - LivingEntity livingEntity = event.getEntity(); - - // Certain entities shouldn't drop twice the amount - if (livingEntity instanceof Player - || livingEntity instanceof ArmorStand - || livingEntity instanceof Horse) { - return; - } - - if (NMSUtil.getVersionNumber() > 9) { - if (livingEntity instanceof Donkey || livingEntity instanceof Mule || livingEntity instanceof ElderGuardian) - return; - } - - if (NMSUtil.getVersionNumber() > 10) { - if (livingEntity instanceof Evoker) - return; - } - - if (NMSUtil.getVersionNumber() > 13) { - if (livingEntity instanceof Ravager || livingEntity instanceof Illager) - return; - } - - if (livingEntity.hasMetadata("SkyBlock")) - return; - - IslandManager islandManager = skyblock.getIslandManager(); - - if (skyblock.getWorldManager().isIslandWorld(livingEntity.getWorld())) { - Island island = islandManager.getIslandAtLocation(livingEntity.getLocation()); - - if (island != null) { - List upgrades = skyblock.getUpgradeManager().getUpgrades(Upgrade.Type.Drops); - - if (upgrades != null && upgrades.size() > 0 && upgrades.get(0).isEnabled() && island.isUpgrade(Upgrade.Type.Drops)) { - Set dontMultiply = new HashSet<>(); - - if (NMSUtil.getVersionNumber() > 8) { - EntityEquipment equipment = livingEntity.getEquipment(); - if (equipment != null) { - for (ItemStack item : event.getDrops()) { - if (item.equals(equipment.getHelmet()) - || item.equals(equipment.getChestplate()) - || item.equals(equipment.getLeggings()) - || item.equals(equipment.getBoots()) - || item.equals(equipment.getItemInMainHand()) - || item.equals(equipment.getItemInOffHand())) { - dontMultiply.add(item); - } - } - } - - if (livingEntity instanceof Pig) { - Pig pig = (Pig) livingEntity; - if (pig.hasSaddle()) - dontMultiply.add(new ItemStack(Material.SADDLE, 1)); - } - } - - for (ItemStack is : event.getDrops()) - if (!dontMultiply.contains(is)) - livingEntity.getWorld().dropItemNaturally(livingEntity.getLocation(), is); - } - } - } - } - - @EventHandler - public void onEntityTargetLivingEntity(EntityTargetLivingEntityEvent event) { - if (!(event.getTarget() instanceof Player)) { - return; - } - - if (!(event.getEntity() instanceof ExperienceOrb)) { - return; - } - - Player player = (Player) event.getTarget(); - - if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { - if (!skyblock.getIslandManager().hasPermission(player, "ExperienceOrbPickup")) { - event.setTarget(null); - event.setCancelled(true); - } - } - } - - @SuppressWarnings("deprecation") - @EventHandler - public void onCreatureSpawn(CreatureSpawnEvent event) { - LivingEntity entity = event.getEntity(); - if (entity instanceof ArmorStand) - return; - if (entity.hasMetadata("SkyBlock")) - return; - - SpawnReason reason = event.getSpawnReason(); - Set reasons = Sets.newHashSet(SpawnReason.NATURAL, SpawnReason.JOCKEY, SpawnReason.MOUNT, getSpawnReason("RAID"), getSpawnReason("PATROL")); - // Check that the reason of this event is not any of these above. - if (!reasons.stream().filter(r -> r != null).anyMatch(r -> r == reason)) - return; - - if (!skyblock.getWorldManager().isIslandWorld(entity.getWorld())) - return; - if (skyblock.getIslandManager().hasSetting(entity.getLocation(), IslandRole.Owner, "NaturalMobSpawning")) - return; - if (event.getSpawnReason() != SpawnReason.JOCKEY && event.getSpawnReason() != SpawnReason.MOUNT) { - entity.remove(); // Older versions ignore the event being cancelled, so this fixes that issue. - return; - } - Bukkit.getScheduler().scheduleSyncDelayedTask(skyblock, () -> { - if (NMSUtil.getVersionNumber() > 10) { // getPassengers() was added in 1.11 - for (org.bukkit.entity.Entity passenger : entity.getPassengers()) - passenger.remove(); - } else { - if (entity.getPassenger() != null) - entity.getPassenger().remove(); - } - entity.remove(); - }); - event.setCancelled(true); // For other plugin API reasons. - } - - private SpawnReason getSpawnReason(String reason) { - try { - return SpawnReason.valueOf(reason); - } catch (Exception e) { - return null; - } - } + private final SkyBlock skyblock; + + private Set preventFireTicks = new HashSet<>(); + + public Entity(SkyBlock skyblock) { + this.skyblock = skyblock; + } + + @EventHandler + public void onEntityDamage(EntityDamageEvent event) { + if (!(event.getEntity() instanceof Player)) { + return; + } + + Player player = (Player) event.getEntity(); + + FileManager fileManager = skyblock.getFileManager(); + + if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { + if (event.getCause() != null) { + if (event.getCause() == DamageCause.VOID) { + return; + } else if (event.getCause() == DamageCause.ENTITY_ATTACK) { + EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) event; + + if (entityDamageByEntityEvent.getDamager() != null && entityDamageByEntityEvent.getDamager() instanceof Player) { + return; + } + } else { + if (NMSUtil.getVersionNumber() > 11) { + if (event.getCause() == DamageCause.valueOf("ENTITY_SWEEP_ATTACK")) { + EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) event; + + if (entityDamageByEntityEvent.getDamager() != null && entityDamageByEntityEvent.getDamager() instanceof Player) { + return; + } + } + } + } + } + + Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")); + FileConfiguration configLoad = config.getFileConfiguration(); + + if (configLoad.getBoolean("Island.Settings.Damage.Enable")) { + if (!skyblock.getIslandManager().hasSetting(player.getLocation(), IslandRole.Owner, "Damage")) { + event.setCancelled(true); + } + } else if (!configLoad.getBoolean("Island.Damage.Enable")) { + event.setCancelled(true); + } + } + + // Fix a bug in minecraft where arrows with flame still apply fire ticks even if + // the shot entity isn't damaged + if (preventFireTicks.contains(player.getUniqueId()) && event.getCause() == DamageCause.FIRE_TICK) { + player.setFireTicks(0); + event.setCancelled(true); + } + } + + @EventHandler + public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { + MessageManager messageManager = skyblock.getMessageManager(); + IslandManager islandManager = skyblock.getIslandManager(); + SoundManager soundManager = skyblock.getSoundManager(); + FileManager fileManager = skyblock.getFileManager(); + + if (event.getDamager() instanceof Player) { + Player player = (Player) event.getDamager(); + org.bukkit.entity.Entity entity = event.getEntity(); + + if (skyblock.getWorldManager().isIslandWorld(entity.getWorld())) { + if (entity instanceof Player) { + Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")); + FileConfiguration configLoad = config.getFileConfiguration(); + + if (configLoad.getBoolean("Island.Settings.PvP.Enable")) { + if (!islandManager.hasSetting(entity.getLocation(), IslandRole.Owner, "PvP")) { + event.setCancelled(true); + } + } else if (!configLoad.getBoolean("Island.PvP.Enable")) { + event.setCancelled(true); + } + } else if (entity instanceof ArmorStand) { + if (!islandManager.hasPermission(player, entity.getLocation(), "Destroy")) { + event.setCancelled(true); + + messageManager.sendMessage(player, fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")) + .getFileConfiguration().getString("Island.Settings.Permission.Message")); + soundManager.playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); + } + } else { + if (!islandManager.hasPermission(player, entity.getLocation(), "MobHurting")) { + event.setCancelled(true); + + messageManager.sendMessage(player, fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")) + .getFileConfiguration().getString("Island.Settings.Permission.Message")); + soundManager.playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); + + return; + } + } + } + + return; + } + + if (event.getDamager() instanceof Projectile && ((Projectile) event.getDamager()).getShooter() instanceof Player) { + Player player = (Player) ((Projectile) event.getDamager()).getShooter(); + org.bukkit.entity.Entity entity = event.getEntity(); + + if (skyblock.getWorldManager().isIslandWorld(entity.getWorld())) { + if (event.getEntity() instanceof Player) { + Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")); + FileConfiguration configLoad = config.getFileConfiguration(); + + if (entity.getType() == EntityType.ITEM_FRAME && !islandManager.hasPermission(player, entity.getLocation(), "HangingDestroy")) { + event.setCancelled(true); + return; + } + + if (configLoad.getBoolean("Island.Settings.PvP.Enable")) { + if (!islandManager.hasSetting(entity.getLocation(), IslandRole.Owner, "PvP")) { + event.setCancelled(true); + } + } else if (!configLoad.getBoolean("Island.PvP.Enable")) { + event.setCancelled(true); + } + } else { + if (!islandManager.hasPermission(player, entity.getLocation(), "MobHurting")) { + event.setCancelled(true); + + messageManager.sendMessage(player, fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")) + .getFileConfiguration().getString("Island.Settings.Permission.Message")); + soundManager.playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); + + return; + } + } + } + } else if (event.getEntity() instanceof Player) { + Player player = (Player) event.getEntity(); + + if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { + Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")); + FileConfiguration configLoad = config.getFileConfiguration(); + + if (configLoad.getBoolean("Island.Settings.Damage.Enable")) { + if (!islandManager.hasSetting(player.getLocation(), IslandRole.Owner, "Damage") || (event.getDamager() instanceof TNTPrimed + && !islandManager.hasSetting(player.getLocation(), IslandRole.Owner, "Explosions"))) { + event.setCancelled(true); + } + } else if (!configLoad.getBoolean("Island.Damage.Enable")) { + event.setCancelled(true); + } + } + } else if (event.getDamager() instanceof TNTPrimed) { + org.bukkit.entity.Entity entity = event.getEntity(); + + if (skyblock.getWorldManager().isIslandWorld(entity.getWorld())) { + if (!islandManager.hasSetting(entity.getLocation(), IslandRole.Owner, "Explosions")) { + event.setCancelled(true); + } + } + } + + // Fix a bug in minecraft where arrows with flame still apply fire ticks even if + // the shot entity isn't damaged + if (event.isCancelled() && event.getEntity() != null && event.getDamager() instanceof Arrow + && ((Arrow) event.getDamager()).getShooter() instanceof Player) { + Arrow arrow = (Arrow) event.getDamager(); + if (arrow.getFireTicks() != 0) { + preventFireTicks.add(event.getEntity().getUniqueId()); + new BukkitRunnable() { + public void run() { + preventFireTicks.remove(event.getEntity().getUniqueId()); + } + }.runTaskLater(SkyBlock.getInstance(), 5L); + } + } + } + + @EventHandler + public void onPlayerShearEntity(PlayerShearEntityEvent event) { + Player player = event.getPlayer(); + + if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { + if (!skyblock.getIslandManager().hasPermission(player, event.getEntity().getLocation(), "Shearing")) { + event.setCancelled(true); + + skyblock.getMessageManager().sendMessage(player, + skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")).getFileConfiguration() + .getString("Island.Settings.Permission.Message")); + skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); + } + } + } + + /** + * Checks that an entity is not targeting another entity on different islands. + * + * @author LimeGlass + */ + @EventHandler + public void onEntityTarget(EntityTargetEvent event) { + org.bukkit.entity.Entity entity = event.getEntity(); + WorldManager worldManager = skyblock.getWorldManager(); + if (!worldManager.isIslandWorld(entity.getWorld())) return; + + org.bukkit.entity.Entity target = event.getTarget(); + // Somehow the target can be null, thanks Spigot. + if (target == null) return; + + IslandManager islandManager = skyblock.getIslandManager(); + Island entityIsland = islandManager.getIslandAtLocation(entity.getLocation()); + Island targetIsland = islandManager.getIslandAtLocation(target.getLocation()); + // Event not related to Skyblock islands. + if (entityIsland == null && targetIsland == null) return; + // One entity is on an island, and the other isn't. + if (entityIsland == null || targetIsland == null) { + event.setCancelled(true); + return; + } + // Both entities are on different islands. + if (!entityIsland.getIslandUUID().equals(targetIsland.getIslandUUID())) { + event.setCancelled(true); + return; + } + } + + @EventHandler + public void onStackableInteract(PlayerArmorStandManipulateEvent event) { + Player player = event.getPlayer(); + if (!skyblock.getIslandManager().hasPermission(player, event.getRightClicked().getLocation(), "ArmorStandUse")) { + event.setCancelled(true); + + skyblock.getMessageManager().sendMessage(player, skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) + .getFileConfiguration().getString("Island.Settings.Permission.Message")); + skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); + } + + if (NMSUtil.getVersionNumber() != 8) return; + + StackableManager stackableManager = SkyBlock.getInstance().getStackableManager(); + if (stackableManager == null) return; + + ArmorStand armorStand = event.getRightClicked(); + for (Location stackLocation : stackableManager.getStacks().keySet()) { + if (stackLocation.getWorld().equals(armorStand.getWorld()) && armorStand.getLocation().distanceSquared(stackLocation) <= 1.5) { + event.setCancelled(true); + return; + } + } + } + + @EventHandler + public void onHangingPlace(HangingPlaceEvent event) { + Player player = event.getPlayer(); + + if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { + if (!skyblock.getIslandManager().hasPermission(player, event.getEntity().getLocation(), "EntityPlacement")) { + event.setCancelled(true); + + skyblock.getMessageManager().sendMessage(player, + skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")).getFileConfiguration() + .getString("Island.Settings.Permission.Message")); + skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); + } + } + } + + @EventHandler + public void onHangingBreak(HangingBreakEvent event) { + Hanging hanging = event.getEntity(); + + if (event.getCause() != RemoveCause.EXPLOSION) { + return; + } + + if (skyblock.getWorldManager().isIslandWorld(hanging.getWorld())) { + if (!skyblock.getIslandManager().hasSetting(hanging.getLocation(), IslandRole.Owner, "Explosions")) { + event.setCancelled(true); + } + } + } + + @EventHandler + public void onHangingBreak(HangingBreakByEntityEvent event) { + Hanging hanging = event.getEntity(); + + if (!(event.getRemover() instanceof Player)) { + return; + } + + Player player = (Player) event.getRemover(); + + if (skyblock.getWorldManager().isIslandWorld(hanging.getWorld())) { + if (!skyblock.getIslandManager().hasPermission(player, hanging.getLocation(), "HangingDestroy")) { + event.setCancelled(true); + + skyblock.getMessageManager().sendMessage(player, + skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")).getFileConfiguration() + .getString("Island.Settings.Permission.Message")); + skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); + } + } + } + + @EventHandler + public void onHangingInteract(PlayerInteractEntityEvent event) { + if (!(event.getRightClicked() instanceof Hanging)) { + return; + } + + Player player = event.getPlayer(); + Hanging hanging = (Hanging) event.getRightClicked(); + + if (skyblock.getWorldManager().isIslandWorld(hanging.getWorld())) { + if (!skyblock.getIslandManager().hasPermission(player, hanging.getLocation(), "HangingDestroy")) { + event.setCancelled(true); + + skyblock.getMessageManager().sendMessage(player, + skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")).getFileConfiguration() + .getString("Island.Settings.Permission.Message")); + skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); + } + } + } + + @EventHandler + public void onHangingRemoveItem(EntityDamageByEntityEvent event) { + if (!(event.getDamager() instanceof Player && event.getEntity() instanceof Hanging)) { + return; + } + + Player player = (Player) event.getDamager(); + Hanging hanging = (Hanging) event.getEntity(); + + if (skyblock.getWorldManager().isIslandWorld(hanging.getWorld())) { + if (!skyblock.getIslandManager().hasPermission(player, hanging.getLocation(), "HangingDestroy")) { + event.setCancelled(true); + + skyblock.getMessageManager().sendMessage(player, + skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")).getFileConfiguration() + .getString("Island.Settings.Permission.Message")); + skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); + } + } + } + + @EventHandler + public void onEntityTaming(EntityTameEvent event) { + if (!(event.getOwner() instanceof Player)) { + return; + } + + Player player = (Player) event.getOwner(); + + if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { + if (!skyblock.getIslandManager().hasPermission(player, event.getEntity().getLocation(), "MobTaming")) { + event.setCancelled(true); + + skyblock.getMessageManager().sendMessage(player, + skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")).getFileConfiguration() + .getString("Island.Settings.Permission.Message")); + skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); + } + } + } + + @EventHandler + @SuppressWarnings("deprecation") + public void onEntityChangeBlock(EntityChangeBlockEvent event) { + org.bukkit.entity.Entity entity = event.getEntity(); + + if (entity instanceof Player) { + return; + } + + IslandManager islandManager = skyblock.getIslandManager(); + WorldManager worldManager = skyblock.getWorldManager(); + + Island island = islandManager.getIslandAtLocation(event.getBlock().getLocation()); + + if (island == null || !skyblock.getWorldManager().isIslandWorld(entity.getWorld())) return; + + if (event.isCancelled()) return; + + Config config = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")); + FileConfiguration configLoad = config.getFileConfiguration(); + + IslandWorld world = worldManager.getIslandWorld(event.getBlock().getWorld()); + + org.bukkit.block.Block block = event.getBlock(); + + // Check spawn block falling, this can be a bit glitchy, but it's better than + // nothing + if ((LocationUtil.isLocationLocation(block.getLocation(), island.getLocation(world, IslandEnvironment.Main).clone().subtract(0, 1, 0)) + || LocationUtil.isLocationLocation(block.getLocation(), + island.getLocation(world, IslandEnvironment.Visitor).clone().subtract(0, 1, 0))) + && skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration() + .getBoolean("Island.Spawn.Protection")) { + event.setCancelled(true); + return; + } + + if ((event.getEntityType() == EntityType.FALLING_BLOCK) + && LocationUtil.isLocationLocation(event.getBlock().getLocation(), island.getLocation(world, IslandEnvironment.Main).clone()) + && configLoad.getBoolean("Island.Spawn.Protection")) { + FallingBlock fallingBlock = (FallingBlock) event.getEntity(); + if (fallingBlock.getDropItem()) { + if (NMSUtil.getVersionNumber() > 12) { + fallingBlock.getWorld().dropItemNaturally(fallingBlock.getLocation(), + new ItemStack(fallingBlock.getBlockData().getMaterial(), 1)); + } else { + try { + Method getBlockDataMethod = FallingBlock.class.getMethod("getBlockData"); + byte data = (byte) getBlockDataMethod.invoke(fallingBlock); + if (fallingBlock.getMaterial().name().endsWith("ANVIL")) { + data = (byte) Math.ceil(data / 4.0); + } + fallingBlock.getWorld().dropItemNaturally(fallingBlock.getLocation(), new ItemStack(fallingBlock.getMaterial(), 1, data)); + } catch (Exception ignored) { + } + } + } + event.setCancelled(true); + } + + if (entity instanceof FallingBlock) return; + + // Check entities interacting with spawn + if (LocationUtil.isLocationAffectingIslandSpawn(block.getLocation(), island, world) && skyblock.getFileManager() + .getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration().getBoolean("Island.Spawn.Protection")) { + event.setCancelled(true); + return; + } + + if (!islandManager.hasSetting(entity.getLocation(), IslandRole.Owner, "MobGriefing")) { + event.setCancelled(true); + } + + if (!skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration() + .getBoolean("Island.Block.Level.Enable")) + return; + + Materials materials = Materials.getMaterials(block.getType(), block.getData()); + + if (materials != null) { + IslandLevel level = island.getLevel(); + + if (level.hasMaterial(materials.name())) { + long materialAmount = level.getMaterialAmount(materials.name()); + + if (materialAmount - 1 <= 0) { + level.removeMaterial(materials.name()); + } else { + level.setMaterialAmount(materials.name(), materialAmount - 1); + } + } + } + + if (event.getTo() != null && event.getTo() != Material.AIR) { + materials = null; + + if (NMSUtil.getVersionNumber() > 12) { + materials = Materials.fromString(event.getTo().name()); + } else { + try { + materials = Materials.requestMaterials(event.getTo().name(), (byte) event.getClass().getMethod("getData").invoke(event)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | SecurityException e) { + e.printStackTrace(); + } + } + + if (materials != null) { + long materialAmount = 0; + IslandLevel level = island.getLevel(); + + if (level.hasMaterial(materials.name())) { + materialAmount = level.getMaterialAmount(materials.name()); + } + + level.setMaterialAmount(materials.name(), materialAmount + 1); + } + } + + } + + @EventHandler + public void onEntityExplode(EntityExplodeEvent event) { + org.bukkit.entity.Entity entity = event.getEntity(); + + WorldManager worldManager = skyblock.getWorldManager(); + IslandManager islandManager = skyblock.getIslandManager(); + + if (skyblock.getWorldManager().isIslandWorld(entity.getWorld())) { + if (!islandManager.hasSetting(entity.getLocation(), IslandRole.Owner, "Explosions")) { + event.setCancelled(true); + } + + if (!event.isCancelled()) { + Island island = islandManager.getIslandAtLocation(entity.getLocation()); + + if (island != null) { + if (skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration() + .getBoolean("Island.Block.Level.Enable")) { + for (org.bukkit.block.Block blockList : event.blockList()) { + @SuppressWarnings("deprecation") + Materials materials = Materials.getMaterials(blockList.getType(), blockList.getData()); + + if (materials != null) { + IslandLevel level = island.getLevel(); + + if (level.hasMaterial(materials.name())) { + long materialAmount = level.getMaterialAmount(materials.name()); + + if (materialAmount - 1 <= 0) { + level.removeMaterial(materials.name()); + } else { + level.setMaterialAmount(materials.name(), materialAmount - 1); + } + } + } + } + } + + if (SkyBlock.getInstance().getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration() + .getBoolean("Island.Spawn.Protection")) { + IslandWorld world = worldManager.getIslandWorld(event.getEntity().getWorld()); + for (org.bukkit.block.Block block : event.blockList()) { + if (LocationUtil.isLocationLocation(block.getLocation(), + island.getLocation(world, IslandEnvironment.Main).clone().subtract(0.0D, 1.0D, 0.0D))) { + event.blockList().remove(block); + break; + } + } + } + } + } + } + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onEntityDeath(EntityDeathEvent event) { + LivingEntity livingEntity = event.getEntity(); + + // Certain entities shouldn't drop twice the amount + if (livingEntity instanceof Player || livingEntity instanceof ArmorStand || livingEntity instanceof Horse) { + return; + } + + if (NMSUtil.getVersionNumber() > 9) { + if (livingEntity instanceof Donkey || livingEntity instanceof Mule || livingEntity instanceof ElderGuardian) return; + } + + if (NMSUtil.getVersionNumber() > 10) { + if (livingEntity instanceof Evoker) return; + } + + if (NMSUtil.getVersionNumber() > 13) { + if (livingEntity instanceof Ravager || livingEntity instanceof Illager) return; + } + + if (livingEntity.hasMetadata("SkyBlock")) return; + + IslandManager islandManager = skyblock.getIslandManager(); + + if (skyblock.getWorldManager().isIslandWorld(livingEntity.getWorld())) { + Island island = islandManager.getIslandAtLocation(livingEntity.getLocation()); + + if (island != null) { + List upgrades = skyblock.getUpgradeManager().getUpgrades(Upgrade.Type.Drops); + + if (upgrades != null && upgrades.size() > 0 && upgrades.get(0).isEnabled() && island.isUpgrade(Upgrade.Type.Drops)) { + Set dontMultiply = new HashSet<>(); + + if (NMSUtil.getVersionNumber() > 8) { + EntityEquipment equipment = livingEntity.getEquipment(); + if (equipment != null) { + for (ItemStack item : event.getDrops()) { + if (item.equals(equipment.getHelmet()) || item.equals(equipment.getChestplate()) + || item.equals(equipment.getLeggings()) || item.equals(equipment.getBoots()) + || item.equals(equipment.getItemInMainHand()) || item.equals(equipment.getItemInOffHand())) { + dontMultiply.add(item); + } + } + } + + if (livingEntity instanceof Pig) { + Pig pig = (Pig) livingEntity; + if (pig.hasSaddle()) dontMultiply.add(new ItemStack(Material.SADDLE, 1)); + } + } + + for (ItemStack is : event.getDrops()) + if (!dontMultiply.contains(is)) livingEntity.getWorld().dropItemNaturally(livingEntity.getLocation(), is); + } + } + } + } + + @EventHandler + public void onEntityTargetLivingEntity(EntityTargetLivingEntityEvent event) { + if (!(event.getTarget() instanceof Player)) { + return; + } + + if (!(event.getEntity() instanceof ExperienceOrb)) { + return; + } + + Player player = (Player) event.getTarget(); + + if (skyblock.getWorldManager().isIslandWorld(player.getWorld())) { + if (!skyblock.getIslandManager().hasPermission(player, "ExperienceOrbPickup")) { + event.setTarget(null); + event.setCancelled(true); + } + } + } + + private static final Set CHECKED_REASONS; + + static { + CHECKED_REASONS = EnumSet.of(SpawnReason.NATURAL, SpawnReason.JOCKEY, SpawnReason.MOUNT); + + final SpawnReason raid = getSpawnReason("RAID"); + final SpawnReason patrol = getSpawnReason("PATROL"); + + if (patrol != null) CHECKED_REASONS.add(patrol); + if (raid != null) CHECKED_REASONS.add(raid); + + } + + @SuppressWarnings("deprecation") + @EventHandler + public void onCreatureSpawn(CreatureSpawnEvent event) { + LivingEntity entity = event.getEntity(); + if (entity instanceof ArmorStand) return; + if (entity.hasMetadata("SkyBlock")) return; + + Location entityLocation = entity.getLocation(); + + Island island = skyblock.getIslandManager().getIslandAtLocation(entityLocation); + + if (island == null) return; + + EntityLimitaton limits = skyblock.getLimitationHandler().getInstance(EntityLimitaton.class); + EntityType type = entity.getType(); + + if (limits.isBeingTracked(type)) { + long count = limits.getEntityCount(island, skyblock.getWorldManager().getIslandWorld(entityLocation.getWorld()), type); + + if (limits.hasTooMuch(count + 1, type)) { + entity.remove(); + event.setCancelled(true); + return; + } + + } + + SpawnReason spawnReason = event.getSpawnReason(); + + if (!CHECKED_REASONS.contains(spawnReason)) return; + + if (!skyblock.getWorldManager().isIslandWorld(entity.getWorld())) return; + if (skyblock.getIslandManager().hasSetting(entityLocation, IslandRole.Owner, "NaturalMobSpawning")) return; + if (spawnReason != SpawnReason.JOCKEY && spawnReason != SpawnReason.MOUNT) { + entity.remove(); // Older versions ignore the event being cancelled, so this fixes that issue. + return; + } + Bukkit.getScheduler().scheduleSyncDelayedTask(skyblock, () -> { + if (NMSUtil.getVersionNumber() > 10) { // getPassengers() was added in 1.11 + for (org.bukkit.entity.Entity passenger : entity.getPassengers()) + passenger.remove(); + } else { + if (entity.getPassenger() != null) entity.getPassenger().remove(); + } + entity.remove(); + }); + event.setCancelled(true); // For other plugin API reasons. + } + + private static SpawnReason getSpawnReason(String reason) { + try { + return SpawnReason.valueOf(reason); + } catch (Exception e) { + return null; + } + } } diff --git a/src/main/java/com/songoda/skyblock/listeners/Interact.java b/src/main/java/com/songoda/skyblock/listeners/Interact.java index cb231173..b1d0feb2 100644 --- a/src/main/java/com/songoda/skyblock/listeners/Interact.java +++ b/src/main/java/com/songoda/skyblock/listeners/Interact.java @@ -7,7 +7,7 @@ import com.songoda.skyblock.island.IslandLevel; import com.songoda.skyblock.island.IslandManager; import com.songoda.skyblock.island.IslandWorld; import com.songoda.skyblock.levelling.LevellingManager; -import com.songoda.skyblock.limit.LimitManager; +import com.songoda.skyblock.limit.impl.BlockLimitation; import com.songoda.skyblock.message.MessageManager; import com.songoda.skyblock.sound.SoundManager; import com.songoda.skyblock.stackable.Stackable; @@ -157,21 +157,24 @@ public class Interact implements Listener { return; } - LimitManager limitManager = skyblock.getLimitManager(); - if (limitManager.isBlockLimitExceeded(player, block)) { - Materials material = Materials.getMaterials(block.getType(), block.getData()); + BlockLimitation limits = skyblock.getLimitationHandler().getInstance(BlockLimitation.class); + + long limit = limits.getBlockLimit(player, block); + + if (limits.isBlockLimitExceeded(player, block, limit)) { + Materials material = Materials.getMaterials(block.getType(), block.getData()); - skyblock.getMessageManager().sendMessage(player, - skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) - .getFileConfiguration().getString("Island.Limit.Block.Exceeded.Message") - .replace("%type", WordUtils.capitalizeFully(material.name().replace("_", " "))) - .replace("%limit", NumberUtil.formatNumber(limitManager.getBlockLimit(player, block)))); - skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); + skyblock.getMessageManager().sendMessage(player, + skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")) + .getFileConfiguration().getString("Island.Limit.Block.Exceeded.Message") + .replace("%type", WordUtils.capitalizeFully(material.name().replace("_", " "))) + .replace("%limit", NumberUtil.formatNumber(limit))); + skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F); - event.setCancelled(true); - return; + event.setCancelled(true); + return; } - + Location location = event.getClickedBlock().getLocation(); if (stackableManager.isStacked(location)) { Stackable stackable = stackableManager.getStack(location, event.getMaterial()); diff --git a/src/main/java/com/songoda/skyblock/utils/structure/Area.java b/src/main/java/com/songoda/skyblock/utils/structure/Area.java index b7aebac1..6d9d728a 100644 --- a/src/main/java/com/songoda/skyblock/utils/structure/Area.java +++ b/src/main/java/com/songoda/skyblock/utils/structure/Area.java @@ -14,11 +14,7 @@ public class Area { } public Location getPosition(int position) { - if (positions.containsKey(position)) { - return positions.get(position); - } - - return null; + return positions.get(position); } public void setPosition(int position, Location location) { diff --git a/src/main/java/com/songoda/skyblock/utils/structure/SchematicUtil.java b/src/main/java/com/songoda/skyblock/utils/structure/SchematicUtil.java index 363b4aa1..697668a4 100644 --- a/src/main/java/com/songoda/skyblock/utils/structure/SchematicUtil.java +++ b/src/main/java/com/songoda/skyblock/utils/structure/SchematicUtil.java @@ -40,14 +40,14 @@ public class SchematicUtil { // TODO: Cache this later try { Class bukkitWorldClass = Class.forName("com.sk89q.worldedit.bukkit.BukkitWorld"); - Constructor bukkitWorldConstructor = bukkitWorldClass.getConstructor(World.class); + Constructor bukkitWorldConstructor = bukkitWorldClass.getConstructor(World.class); Class editSessionClass = Class.forName("com.sk89q.worldedit.EditSession"); Class localWorldClass = Class.forName("com.sk89q.worldedit.LocalWorld"); - Constructor editSessionConstructor = editSessionClass.getConstructor(localWorldClass, int.class); + Constructor editSessionConstructor = editSessionClass.getConstructor(localWorldClass, int.class); Class cuboidClipboardClass = Class.forName("com.sk89q.worldedit.CuboidClipboard"); Method loadSchematicMethod = cuboidClipboardClass.getMethod("loadSchematic", File.class); Class vectorClass = Class.forName("com.sk89q.worldedit.Vector"); - Constructor vectorConstructor = vectorClass.getConstructor(double.class, double.class, double.class); + Constructor vectorConstructor = vectorClass.getConstructor(double.class, double.class, double.class); Method pasteMethod = cuboidClipboardClass.getMethod("paste", editSessionClass, vectorClass, boolean.class); Object editSessionObj = editSessionConstructor.newInstance(bukkitWorldConstructor.newInstance(location.getWorld()), 999999999); diff --git a/src/main/java/com/songoda/skyblock/utils/structure/StructureUtil.java b/src/main/java/com/songoda/skyblock/utils/structure/StructureUtil.java index eac82b58..5f0336be 100644 --- a/src/main/java/com/songoda/skyblock/utils/structure/StructureUtil.java +++ b/src/main/java/com/songoda/skyblock/utils/structure/StructureUtil.java @@ -6,7 +6,6 @@ import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; import com.songoda.skyblock.SkyBlock; import com.songoda.skyblock.config.FileManager; -import com.songoda.skyblock.utils.Compression; import com.songoda.skyblock.utils.version.NMSUtil; import com.songoda.skyblock.utils.world.LocationUtil; import com.songoda.skyblock.utils.world.block.BlockData; @@ -26,7 +25,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; diff --git a/src/main/resources/language.yml b/src/main/resources/language.yml index ea4a8eb1..6503bf9a 100644 --- a/src/main/resources/language.yml +++ b/src/main/resources/language.yml @@ -242,6 +242,8 @@ Command: Message: '&bSkyBlock &8| &aInfo&8: &eThis island will unload.' Info: Message: '&f&oKeeps an island from unloading.' + No-Player-Input: + Message: '&bSkyBlock &8| &cError&8: &ePlease input a player.' Proxy: IsOn: Message: '&bSkyBlock &8| &aInfo&8: &eYou are now proxying %player.' diff --git a/src/main/resources/limits.yml b/src/main/resources/limits.yml index 8bd308b4..2d4c6861 100644 --- a/src/main/resources/limits.yml +++ b/src/main/resources/limits.yml @@ -1,3 +1,6 @@ block: - BEDROCK: 0 - END_PORTAL_FRAME: 12 + DefaultLimit: -1 + BEDROCK: 0 + END_PORTAL_FRAME: 12 +entity: + DefaultLimit: -1 \ No newline at end of file