From 29dfc313fc7bb0667708fab11919154892f057bc Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Fri, 22 Dec 2017 13:22:33 +0100 Subject: [PATCH] L10nAPI - Reworking API - WIP Removed the old localization system Started implementation of the new one For build reasons, removed protection listeners - they were requiring too much work to update them, and they need to be reworked due to the boilerplate code. --- .../us/tastybento/bskyblock/BSkyBlock.java | 40 +- .../bskyblock/api/addons/BSAddon.java | 5 + .../bskyblock/api/commands/User.java | 2 +- .../bskyblock/api/localization/BSLocale.java | 73 + .../commands/island/IslandCreateCommand.java | 12 +- .../config/AbstractLocaleManager.java | 125 - .../bskyblock/config/BSBLocale.java | 99 - .../bskyblock/config/LocaleManager.java | 37 - .../bskyblock/config/YamlResourceBundle.java | 181 -- .../managers/island/IslandsManager.java | 5 +- .../listeners/protection/IslandGuard.java | 2557 ----------------- .../listeners/protection/IslandGuard1_8.java | 206 -- .../listeners/protection/IslandGuard1_9.java | 474 --- .../listeners/protection/NetherEvents.java | 226 -- .../listeners/protection/VisitorGuard.java | 162 -- .../bskyblock/managers/LocalesManager.java | 64 + 16 files changed, 160 insertions(+), 4108 deletions(-) create mode 100644 src/main/java/us/tastybento/bskyblock/api/localization/BSLocale.java delete mode 100644 src/main/java/us/tastybento/bskyblock/config/AbstractLocaleManager.java delete mode 100644 src/main/java/us/tastybento/bskyblock/config/BSBLocale.java delete mode 100644 src/main/java/us/tastybento/bskyblock/config/LocaleManager.java delete mode 100644 src/main/java/us/tastybento/bskyblock/config/YamlResourceBundle.java delete mode 100644 src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard.java delete mode 100644 src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard1_8.java delete mode 100644 src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard1_9.java delete mode 100644 src/main/java/us/tastybento/bskyblock/listeners/protection/NetherEvents.java delete mode 100644 src/main/java/us/tastybento/bskyblock/listeners/protection/VisitorGuard.java create mode 100644 src/main/java/us/tastybento/bskyblock/managers/LocalesManager.java diff --git a/src/main/java/us/tastybento/bskyblock/BSkyBlock.java b/src/main/java/us/tastybento/bskyblock/BSkyBlock.java index fba4d362f..0d1e08c79 100755 --- a/src/main/java/us/tastybento/bskyblock/BSkyBlock.java +++ b/src/main/java/us/tastybento/bskyblock/BSkyBlock.java @@ -1,9 +1,6 @@ package us.tastybento.bskyblock; -import java.util.UUID; - import org.bukkit.Material; -import org.bukkit.command.CommandSender; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -11,8 +8,6 @@ import org.bukkit.plugin.java.JavaPlugin; import us.tastybento.bskyblock.api.BSModule; import us.tastybento.bskyblock.commands.AdminCommand; import us.tastybento.bskyblock.commands.IslandCommand; -import us.tastybento.bskyblock.config.BSBLocale; -import us.tastybento.bskyblock.config.LocaleManager; import us.tastybento.bskyblock.config.Settings; import us.tastybento.bskyblock.database.BSBDatabase; import us.tastybento.bskyblock.database.managers.PlayersManager; @@ -21,11 +16,8 @@ import us.tastybento.bskyblock.generators.IslandWorld; import us.tastybento.bskyblock.listeners.JoinLeaveListener; import us.tastybento.bskyblock.listeners.NetherPortals; import us.tastybento.bskyblock.listeners.PanelListener; -import us.tastybento.bskyblock.listeners.protection.IslandGuard; -import us.tastybento.bskyblock.listeners.protection.IslandGuard1_8; -import us.tastybento.bskyblock.listeners.protection.IslandGuard1_9; -import us.tastybento.bskyblock.listeners.protection.NetherEvents; import us.tastybento.bskyblock.managers.CommandsManager; +import us.tastybento.bskyblock.managers.LocalesManager; import us.tastybento.bskyblock.util.Util; import us.tastybento.bskyblock.util.nms.NMSAbstraction; @@ -47,8 +39,7 @@ public class BSkyBlock extends JavaPlugin implements BSModule { // Managers private CommandsManager commandsManager; - - protected LocaleManager localeManager; + private LocalesManager localesManager; @Override public void onEnable(){ @@ -102,7 +93,8 @@ public class BSkyBlock extends JavaPlugin implements BSModule { }; Settings.defaultLanguage = "en-US"; - localeManager = new LocaleManager(plugin); + localesManager = new LocalesManager(plugin); + localesManager.registerLocales(plugin); // Register Listeners registerListeners(); @@ -138,11 +130,7 @@ public class BSkyBlock extends JavaPlugin implements BSModule { PluginManager manager = getServer().getPluginManager(); // Player join events manager.registerEvents(new JoinLeaveListener(this), this); - manager.registerEvents(new NetherEvents(this), this); manager.registerEvents(new NetherPortals(this), this); - manager.registerEvents(new IslandGuard(this), this); - manager.registerEvents(new IslandGuard1_8(this), this); - manager.registerEvents(new IslandGuard1_9(this), this); manager.registerEvents(new PanelListener(this), this); } @@ -209,22 +197,6 @@ public class BSkyBlock extends JavaPlugin implements BSModule { return plugin; } - /** - * @param sender - * @return Locale object for sender - */ - public BSBLocale getLocale(CommandSender sender) { - return localeManager.getLocale(sender); - } - - /** - * @param uuid - * @return Locale object for UUID - */ - public BSBLocale getLocale(UUID uuid) { - return localeManager.getLocale(uuid); - } - public NMSAbstraction getNMSHandler() { NMSAbstraction nmsHandler = null; try { @@ -239,6 +211,10 @@ public class BSkyBlock extends JavaPlugin implements BSModule { return commandsManager; } + public LocalesManager getLocalesManager() { + return localesManager; + } + @Override public String getIdentifier() { return getDescription().getName(); diff --git a/src/main/java/us/tastybento/bskyblock/api/addons/BSAddon.java b/src/main/java/us/tastybento/bskyblock/api/addons/BSAddon.java index df30d0cf1..ec29be4f7 100644 --- a/src/main/java/us/tastybento/bskyblock/api/addons/BSAddon.java +++ b/src/main/java/us/tastybento/bskyblock/api/addons/BSAddon.java @@ -3,6 +3,7 @@ package us.tastybento.bskyblock.api.addons; import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.api.BSModule; import us.tastybento.bskyblock.managers.CommandsManager; +import us.tastybento.bskyblock.managers.LocalesManager; public abstract class BSAddon implements BSModule { @@ -27,6 +28,10 @@ public abstract class BSAddon implements BSModule { return BSkyBlock.getPlugin().getCommandsManager(); } + public LocalesManager getLocalesManager() { + return BSkyBlock.getPlugin().getLocalesManager(); + } + @Override public String getIdentifier() { return getDescription().getName(); diff --git a/src/main/java/us/tastybento/bskyblock/api/commands/User.java b/src/main/java/us/tastybento/bskyblock/api/commands/User.java index 7d9845e02..9ce3f0d53 100644 --- a/src/main/java/us/tastybento/bskyblock/api/commands/User.java +++ b/src/main/java/us/tastybento/bskyblock/api/commands/User.java @@ -153,7 +153,7 @@ public class User { * @param variables - CharSequence target, replacement pairs */ public void sendMessage(String reference, String... variables) { - String message = ChatColor.getLastColors(reference) + plugin.getLocale(sender).get(ChatColor.stripColor(reference)); + String message = plugin.getLocalesManager().get(sender, reference); if (variables.length > 1) { for (int i = 0; i < variables.length; i+=2) { message.replace(variables[i], variables[i+1]); diff --git a/src/main/java/us/tastybento/bskyblock/api/localization/BSLocale.java b/src/main/java/us/tastybento/bskyblock/api/localization/BSLocale.java new file mode 100644 index 000000000..209b07999 --- /dev/null +++ b/src/main/java/us/tastybento/bskyblock/api/localization/BSLocale.java @@ -0,0 +1,73 @@ +package us.tastybento.bskyblock.api.localization; + +import org.bukkit.ChatColor; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +/** + * @author Poslovitch, Tastybento + */ +public class BSLocale { + + private Locale locale; + private YamlConfiguration config; + private Map cache; + + public BSLocale(String languageTag, File file) { + this.locale = Locale.forLanguageTag(languageTag); + this.config = YamlConfiguration.loadConfiguration(file); + this.cache = new HashMap<>(); + } + + /** + * Get text from the yml file for this locale + * @param reference - the YAML node where the text is + * @return Text for this locale reference or the reference if nothing has been found + */ + public String get(String reference) { + if (cache.containsKey(reference)) { + return cache.get(reference); + } else if (config.contains(reference)) { + cache.put(reference, ChatColor.translateAlternateColorCodes('&', config.getString(reference))); + return cache.get(reference); + } + return reference; // return reference in case nothing has been found + } + + public void clearCache() { + this.cache.clear(); + } + + /** + * Returns the locale language + * @return the locale language + */ + public String getLanguage(){ + if(locale == null) return "unknown"; + + return locale.getDisplayLanguage(); + } + + /** + * Returns the locale country + * @return the locale country + */ + public String getCountry(){ + if(locale == null) return "unknown"; + + return locale.getDisplayCountry(); + } + + /** + * Returns the locale language tag (e.g: en-GB) + * @return the locale language tag + */ + public String toLanguageTag(){ + return this.locale.toLanguageTag(); + } + +} diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/IslandCreateCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/IslandCreateCommand.java index 22c9e2fe9..01d73358a 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/IslandCreateCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/IslandCreateCommand.java @@ -33,30 +33,30 @@ public class IslandCreateCommand extends CompositeCommand { @Override public boolean execute(User user, String[] args) { if (getIslands().hasIsland(user.getUniqueId())) { - user.sendMessage(ChatColor.RED + "general.errors.already-have-island"); + user.sendMessage("general.errors.already-have-island"); } if (getPlayers().inTeam(user.getUniqueId())) { return false; } - createIsland(user.getPlayer()); + createIsland(user); return true; } /** * Creates an island for player * - * @param player + * @param user */ - protected void createIsland(Player player) { + protected void createIsland(User user) { //TODO: Add panels, make a selection. try { NewIsland.builder() - .player(player) + .player(user.getPlayer()) .reason(Reason.CREATE) .build(); } catch (IOException e) { plugin.getLogger().severe("Could not create island for player."); - player.sendMessage(ChatColor.RED + plugin.getLocale(player).get("general.errors.general")); + user.sendMessage("general.errors.general"); e.printStackTrace(); } } diff --git a/src/main/java/us/tastybento/bskyblock/config/AbstractLocaleManager.java b/src/main/java/us/tastybento/bskyblock/config/AbstractLocaleManager.java deleted file mode 100644 index b5703c03c..000000000 --- a/src/main/java/us/tastybento/bskyblock/config/AbstractLocaleManager.java +++ /dev/null @@ -1,125 +0,0 @@ -package us.tastybento.bskyblock.config; - -import java.io.File; -import java.io.FilenameFilter; -import java.net.MalformedURLException; -import java.util.HashMap; -import java.util.Locale; -import java.util.UUID; - -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - -import us.tastybento.bskyblock.util.FileLister; - -/** - * Handles locale functions - * @author ben - * - */ -public abstract class AbstractLocaleManager { - private Plugin plugin; - private HashMap locales = new HashMap<>(); - final static String LOCALE_FOLDER = "locales"; - - public AbstractLocaleManager(Plugin plugin) { - super(); - this.plugin = plugin; - this.loadLocales(); - } - - /** - * Returns an HashMap of locale identifier and the related object - * @return the locales - */ - public HashMap getLocales(){ - return locales; - } - - /** - * Set the available locales - * @param locales - the locales to set - */ - public void setLocales(HashMap locales){ - this.locales = locales; - } - - /** - * Returns the default locale - * @return the default locale - */ - public BSBLocale getLocale(){ - return locales.get(Settings.defaultLanguage); - } - - /** - * Returns the locale for the specified CommandSender - * @param sender - CommandSender to get the locale - * @return if sender is a player, the player's locale, otherwise the default locale - */ - public BSBLocale getLocale(CommandSender sender){ - if(sender instanceof Player) return getLocale(((Player) sender).getUniqueId()); - else return getLocale(); - } - - /** - * Returns the locale for the specified player - * @param player - Player to get the locale - * @return the locale for this player - */ - public abstract BSBLocale getLocale(UUID player); - - /** - * Loads all the locales available. If the locale folder does not exist, one will be created and - * filled with locale files from the jar. - */ - public void loadLocales() { - // Describe the filter - we only want files that are correctly named - FilenameFilter ymlFilter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - //plugin.getLogger().info("DEBUG: filename = " + name); - if (name.toLowerCase().startsWith("bsb_") && name.toLowerCase().endsWith(".yml")) { - // See if this is a valid locale - //Locale localeObject = new Locale(name.substring(0, 2), name.substring(3, 5)); - Locale localeObject = Locale.forLanguageTag(name.substring(4, name.length() - 4)); - if (localeObject == null) { - plugin.getLogger().severe("Filename '" + name + "' is an unknown locale, skipping..."); - return false; - } - return true; - } else { - if (name.toLowerCase().endsWith(".yml")) { - plugin.getLogger().severe("Filename '" + name + "' is not in the correct format for a locale file - skipping..."); - } - return false; - } - } - }; - // Run through the files and store the locales - File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER); - // If the folder does not exist, then make it and fill with the locale files from the jar - if (!localeDir.exists()) { - localeDir.mkdir(); - FileLister lister = new FileLister(plugin); - try { - for (String name : lister.listJar(LOCALE_FOLDER)) { - plugin.saveResource(name,true); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - // Store all the locales available - for (String language : localeDir.list(ymlFilter)) { - try { - BSBLocale locale = new BSBLocale(plugin, language); - locales.put(locale.getLocaleId(), locale); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - - } - } -} diff --git a/src/main/java/us/tastybento/bskyblock/config/BSBLocale.java b/src/main/java/us/tastybento/bskyblock/config/BSBLocale.java deleted file mode 100644 index 4b22aaf94..000000000 --- a/src/main/java/us/tastybento/bskyblock/config/BSBLocale.java +++ /dev/null @@ -1,99 +0,0 @@ -package us.tastybento.bskyblock.config; - -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.Locale; -import java.util.ResourceBundle; - -import org.bukkit.ChatColor; -import org.bukkit.plugin.Plugin; - -public class BSBLocale { - - final static String LOCALE_FOLDER = "locales"; - private Plugin plugin; - //private String localeId; - private String languageTag; - private ResourceBundle rb; - Locale localeObject; - - /** - * Provides localization - * Locale files are .yml and have the filename "bsb_[country and language tag].yml", e.g. bsb_en_GB.yml - * @param plugin - * @throws MalformedURLException - */ - public BSBLocale(Plugin plugin, String localeId) throws MalformedURLException { - this.plugin = plugin; - //this.localeId = localeId; - // Check if the folder exists - File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER); - if (!localeDir.exists()) { - localeDir.mkdirs(); - } - // Check if this file does not exist - File localeFile = new File(localeDir, localeId); - if (!localeFile.exists()) { - // Does not exist - look in JAR and save if possible - plugin.saveResource(LOCALE_FOLDER + localeId, false); - } - languageTag = localeId.substring(4, localeId.length() - 4).replace('_', '-'); - URL[] urls = {localeDir.toURI().toURL()}; - ClassLoader loader = new URLClassLoader(urls); - localeObject = Locale.forLanguageTag(languageTag); - rb = ResourceBundle.getBundle("bsb", localeObject, loader, YamlResourceBundle.Control.INSTANCE); - } - - /** - * Get text from the yml file for this locale - * @param reference - the YAML node where the text is - * @return Text for this locale reference or the reference is nothing has been found - */ - public String get(String reference) { - // TODO: add placeholder conversion? - //plugin.getLogger().info("DEBUG: default lang = " + Settings.defaultLanguage); - //plugin.getLogger().info("DEBUG: this locale = " + languageTag); - //plugin.getLogger().info("DEBUG: reference = " + reference); - if (rb.containsKey(reference)) { - //plugin.getLogger().info("DEBUG: contains key"); - return ChatColor.translateAlternateColorCodes('&', rb.getString(reference)); - } else if (!Settings.defaultLanguage.equals(languageTag)){ - //plugin.getLogger().info("DEBUG: try default"); - // TODO: Try default lang - return reference; - } - plugin.getLogger().severe(reference + " not found in " + languageTag + " or default lang " + Settings.defaultLanguage); - return reference; // Return reference for debug purposes, like for the mods. - } - - /** - * Returns the locale language - * @return the locale language - */ - public String getLanguageName(){ - if(localeObject == null) return "unknown"; - - return localeObject.getDisplayLanguage(localeObject); - } - - /** - * Returns the locale country - * @return the locale country - */ - public String getCountryName(){ - if(localeObject == null) return "unknown"; - - return localeObject.getDisplayCountry(localeObject); - } - - /** - * Returns the locale identifier (e.g: en-GB) - * @return the locale ID - */ - public String getLocaleId(){ - return this.localeObject.toLanguageTag(); - } - -} diff --git a/src/main/java/us/tastybento/bskyblock/config/LocaleManager.java b/src/main/java/us/tastybento/bskyblock/config/LocaleManager.java deleted file mode 100644 index 2fd00fd6a..000000000 --- a/src/main/java/us/tastybento/bskyblock/config/LocaleManager.java +++ /dev/null @@ -1,37 +0,0 @@ -package us.tastybento.bskyblock.config; - -import java.util.UUID; - -import us.tastybento.bskyblock.BSkyBlock; - -/** - * Handles the BSkyBlock locale - * @author ben - * - */ -public class LocaleManager extends AbstractLocaleManager { - - private BSkyBlock plugin; - - public LocaleManager(BSkyBlock plugin) { - super(plugin); - this.plugin = plugin; - } - - @Override - /** - * Returns the locale for the specified player - * @param player - Player to get the locale - * @return the locale for this player - */ - public BSBLocale getLocale(UUID player){ - //getLogger().info("DEBUG: " + player); - //getLogger().info("DEBUG: " + getPlayers() == null ? "Players is null":"Players in not null"); - //getLogger().info("DEBUG: " + getPlayers().getPlayer(player)); - //getLogger().info("DEBUG: " + getPlayers().getPlayer(player).getLocale()); - String locale = plugin.getPlayers().getPlayer(player).getLocale(); - if(locale.isEmpty() || !getLocales().containsKey(locale)) return getLocales().get(Settings.defaultLanguage); - - return getLocales().get(locale); - } -} diff --git a/src/main/java/us/tastybento/bskyblock/config/YamlResourceBundle.java b/src/main/java/us/tastybento/bskyblock/config/YamlResourceBundle.java deleted file mode 100644 index 2f6a4b878..000000000 --- a/src/main/java/us/tastybento/bskyblock/config/YamlResourceBundle.java +++ /dev/null @@ -1,181 +0,0 @@ -/* - * https://github.com/akihyro/yaml-resource-bundle - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package us.tastybento.bskyblock.config; - -import static java.util.Arrays.asList; -import static java.util.Collections.enumeration; -import static java.util.Collections.unmodifiableList; -import static java.util.stream.Collectors.toMap; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.util.AbstractMap.SimpleImmutableEntry; -import java.util.Enumeration; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Map.Entry; -import java.util.ResourceBundle; -import java.util.Set; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.stream.Stream; - -import org.yaml.snakeyaml.Yaml; - -/** - * {@link ResourceBundle} for YAML format. - */ -public class YamlResourceBundle extends ResourceBundle { - - /** - * Entries. - */ - private final Map entries; - - /** - * Constructor. - * - * @param string YAML data. - */ - public YamlResourceBundle(String string) { - entries = flattenYamlTree(new Yaml().loadAs(string, Map.class)); - } - - /** - * Constructor. - * - * @param stream YAML data as input stream. - */ - public YamlResourceBundle(InputStream stream) { - entries = flattenYamlTree(new Yaml().loadAs(stream, Map.class)); - } - - /** - * Constructor. - * - * @param reader YAML data as input character stream. - */ - public YamlResourceBundle(Reader reader) { - entries = flattenYamlTree(new Yaml().loadAs(reader, Map.class)); - } - - /** - * Flatten yaml tree structure. - * - * @param map {@link Map} of yaml tree. - * @return {@link Map} of entries. - */ - private static Map flattenYamlTree(Map map) { - return map.entrySet().stream() - .flatMap(YamlResourceBundle::flattenYamlTree) - .collect(toMap( - e -> e.getKey(), - e -> e.getValue(), - (oldValue, newValue) -> newValue - )); - } - - /** - * Flatten yaml tree structure. - * - * @param entry {@link Entry} of yaml tree. - * @return {@link Stream} of entries - */ - private static Stream> flattenYamlTree(Entry entry) { - String key = entry.getKey().toString(); - Object value = entry.getValue(); - if (value instanceof Map) { - Map valueAsMap = (Map) value; - return valueAsMap.entrySet().stream() - .flatMap(YamlResourceBundle::flattenYamlTree) - .map(e -> new SimpleImmutableEntry<>(key + "." + e.getKey(), e.getValue())); - } else if (value instanceof List) { - List valueAsList = (List) value; - value = valueAsList.stream().toArray(String[]::new); - AtomicInteger index = new AtomicInteger(); - return Stream.concat( - Stream.of(new SimpleImmutableEntry<>(key, value)), - valueAsList.stream() - .map(v -> new SimpleImmutableEntry<>(key + "[" + index.getAndIncrement() + "]", v)) - ); - } - return Stream.of(new SimpleImmutableEntry<>(key, value)); - } - - /** {@inheritDoc} */ - @Override - protected Set handleKeySet() { - return entries.keySet(); - } - - /** {@inheritDoc} */ - @Override - public Enumeration getKeys() { - return enumeration(keySet()); - } - - /** {@inheritDoc} */ - @Override - protected Object handleGetObject(String key) { - return entries.get(key); - } - - /** - * {@link ResourceBundle.Control} for YAML format. - */ - public static class Control extends ResourceBundle.Control { - - /** - * Singleton instance. - */ - public static final Control INSTANCE = new Control(); - - /** - * Constructor. - */ - protected Control() { - } - - /** {@inheritDoc} */ - @Override - public List getFormats(String baseName) { - return unmodifiableList(asList("yaml", "yml")); - } - - /** {@inheritDoc} */ - @Override - public ResourceBundle newBundle(String baseName, - Locale locale, String format, ClassLoader loader, boolean reload) - throws IllegalAccessException, InstantiationException, IOException { - if (!getFormats(baseName).contains(format)) { - return null; - } - String bundleName = toBundleName(baseName, locale); - String resourceName = toResourceName(bundleName, format); - InputStream stream = loader.getResourceAsStream(resourceName); - try { - return stream != null ? new YamlResourceBundle(stream) : null; - } finally { - if (stream != null) { - stream.close(); - } - } - - } - - } - -} \ No newline at end of file diff --git a/src/main/java/us/tastybento/bskyblock/database/managers/island/IslandsManager.java b/src/main/java/us/tastybento/bskyblock/database/managers/island/IslandsManager.java index a41df5ed3..c4804eeb0 100644 --- a/src/main/java/us/tastybento/bskyblock/database/managers/island/IslandsManager.java +++ b/src/main/java/us/tastybento/bskyblock/database/managers/island/IslandsManager.java @@ -623,10 +623,11 @@ public class IslandsManager { //home.getChunk().load(); player.teleport(home); //player.sendBlockChange(home, Material.GLOWSTONE, (byte)0); + User user = User.getInstance(player); if (number == 1) { - player.sendMessage(plugin.getLocale(player.getUniqueId()).get("island.teleport").replace("[label]", Settings.ISLANDCOMMAND)); + user.sendMessage("island.teleport", "[label]", Settings.ISLANDCOMMAND); } else { - player.sendMessage(plugin.getLocale(player.getUniqueId()).get("island.teleported").replace("[number]", String.valueOf(number))); + user.sendMessage("island.teleported", "[number]", String.valueOf(number)); } // Exit spectator mode if in it if (player.getGameMode().equals(GameMode.SPECTATOR)) { diff --git a/src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard.java b/src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard.java deleted file mode 100644 index 1bee17de0..000000000 --- a/src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard.java +++ /dev/null @@ -1,2557 +0,0 @@ -package us.tastybento.bskyblock.listeners.protection; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.UUID; - -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.Sound; -import org.bukkit.World.Environment; -import org.bukkit.block.Biome; -import org.bukkit.block.Block; -import org.bukkit.block.BlockState; -import org.bukkit.entity.Animals; -import org.bukkit.entity.Creeper; -import org.bukkit.entity.Enderman; -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Golem; -import org.bukkit.entity.IronGolem; -import org.bukkit.entity.ItemFrame; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Monster; -import org.bukkit.entity.Player; -import org.bukkit.entity.Projectile; -import org.bukkit.entity.Skeleton; -import org.bukkit.entity.Slime; -import org.bukkit.entity.Snowman; -import org.bukkit.entity.Squid; -import org.bukkit.entity.Villager; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockBurnEvent; -import org.bukkit.event.block.BlockDispenseEvent; -import org.bukkit.event.block.BlockIgniteEvent; -import org.bukkit.event.block.BlockMultiPlaceEvent; -import org.bukkit.event.block.BlockPistonExtendEvent; -import org.bukkit.event.block.BlockPistonRetractEvent; -import org.bukkit.event.block.BlockPlaceEvent; -import org.bukkit.event.block.BlockSpreadEvent; -import org.bukkit.event.entity.CreatureSpawnEvent; -import org.bukkit.event.entity.EntityChangeBlockEvent; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDeathEvent; -import org.bukkit.event.entity.EntityExplodeEvent; -import org.bukkit.event.entity.PlayerLeashEntityEvent; -import org.bukkit.event.entity.PotionSplashEvent; -import org.bukkit.event.hanging.HangingBreakByEntityEvent; -import org.bukkit.event.hanging.HangingPlaceEvent; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerBedEnterEvent; -import org.bukkit.event.player.PlayerBucketEmptyEvent; -import org.bukkit.event.player.PlayerBucketFillEvent; -import org.bukkit.event.player.PlayerEggThrowEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.event.player.PlayerShearEntityEvent; -import org.bukkit.event.player.PlayerUnleashEntityEvent; -import org.bukkit.event.vehicle.VehicleDamageEvent; -import org.bukkit.event.vehicle.VehicleMoveEvent; -import org.bukkit.event.world.StructureGrowEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.material.MaterialData; -import org.bukkit.potion.Potion; -import org.bukkit.util.BlockIterator; -import org.bukkit.util.Vector; - -import us.tastybento.bskyblock.BSkyBlock; -import us.tastybento.bskyblock.api.events.IslandBaseEvent; -import us.tastybento.bskyblock.api.events.island.IslandEvent; -import us.tastybento.bskyblock.api.events.island.IslandEvent.Reason; -import us.tastybento.bskyblock.config.Settings; -import us.tastybento.bskyblock.database.objects.Island; -import us.tastybento.bskyblock.database.objects.Island.SettingsFlag; -import us.tastybento.bskyblock.generators.IslandWorld; -import us.tastybento.bskyblock.util.Util; - -/** - * @author tastybento - * Provides protection to islands - */ -@SuppressWarnings("deprecation") -public class IslandGuard implements Listener { - private final BSkyBlock plugin; - private final static boolean DEBUG = false; - private static final boolean DEBUG2 = false; - private HashMap onPlate = new HashMap<>(); - private Set tntBlocks = new HashSet<>(); - private Set litCreeper = new HashSet<>(); - - public IslandGuard(final BSkyBlock plugin) { - this.plugin = plugin; - } - - /** - * Prevents visitors picking items from riding horses or other inventories - * @param event - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onHorseInventoryClick(InventoryClickEvent event) { - if (DEBUG) - plugin.getLogger().info("DEBUG: horse and llama inventory click"); - if (event.getInventory().getHolder() == null) { - return; - } - // World check - if (!Util.inWorld(event.getWhoClicked())) { - return; - } - if (event.getInventory().getHolder() instanceof Animals) { - if (actionAllowed((Player)event.getWhoClicked(), event.getWhoClicked().getLocation(), SettingsFlag.MOUNT_INVENTORY)) { - return; - } - // Elsewhere - not allowed - event.getWhoClicked().sendMessage(plugin.getLocale(event.getWhoClicked().getUniqueId()).get("island.protected")); - event.setCancelled(true); - } - } - - /** - * Checks if action is allowed for player in location for flag - * @param player - * @param location - * @param flag - * @return true if allowed - */ - private boolean actionAllowed(Player player, Location location, SettingsFlag flag) { - if (player == null) { - return actionAllowed(location, flag); - } - // This permission bypasses protection - if (player.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return true; - } - Island island = plugin.getIslands().getProtectedIslandAt(location); - if (island != null && (island.getFlag(flag) || island.getMembers().contains(player.getUniqueId()))){ - return true; - } - if (island == null && Settings.defaultWorldSettings.get(flag)) { - return true; - } - return false; - } - - /** - * Action allowed in this location - * @param location - * @param flag - * @return true if allowed - */ - private boolean actionAllowed(Location location, SettingsFlag flag) { - Island island = plugin.getIslands().getProtectedIslandAt(location); - if (island != null && island.getFlag(flag)){ - return true; - } - if (island == null && Settings.defaultWorldSettings.get(flag)) { - return true; - } - return false; - } - - // Vehicle damage - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onVehicleDamageEvent(VehicleDamageEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - plugin.getLogger().info(e.getAttacker().getType().toString()); - } - if (Util.inWorld(e.getVehicle())) { - if (!(e.getAttacker() instanceof Player)) { - return; - } - Player p = (Player) e.getAttacker(); - if (actionAllowed(p, e.getVehicle().getLocation(), SettingsFlag.BREAK_BLOCKS)) { - return; - } - // Not allowed - p.sendMessage(plugin.getLocale(p.getUniqueId()).get("island.protected")); - e.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onVehicleMove(final VehicleMoveEvent e) { - if (DEBUG) - plugin.getLogger().info("DEBUG: vehicle move = " + e.getVehicle()); - if (!Util.inWorld(e.getVehicle())) { - return; - } - - Entity passenger = e.getVehicle().getPassenger(); - if (passenger == null || !(passenger instanceof Player)) { - return; - } - - Player player = (Player)passenger; - if (plugin.getIslands() == null) { - if (DEBUG) - plugin.getLogger().info("DEBUG: grid = null"); - return; - } - Island islandTo = plugin.getIslands().getProtectedIslandAt(e.getTo()); - // Announcement entering - Island islandFrom = plugin.getIslands().getProtectedIslandAt(e.getFrom()); - // Only says something if there is a change in islands - /* - * Situations: - * islandTo == null && islandFrom != null - exit - * islandTo == null && islandFrom == null - nothing - * islandTo != null && islandFrom == null - enter - * islandTo != null && islandFrom != null - same PlayerIsland or teleport? - * islandTo == islandFrom - */ - // plugin.getLogger().info("islandTo = " + islandTo); - // plugin.getLogger().info("islandFrom = " + islandFrom); - - if (islandTo != null && (islandTo.getOwner() != null || islandTo.isSpawn())) { - // Lock check - if (islandTo.isLocked() || plugin.getPlayers().isBanned(islandTo.getOwner(),player.getUniqueId())) { - if (!islandTo.getMembers().contains(player.getUniqueId()) - && !player.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect") - && !player.hasPermission(Settings.PERMPREFIX + "mod.bypasslock")) { - player.sendMessage(plugin.getLocale(player.getUniqueId()).get("lock.islandlocked")); - // Get the vector away from this island - Vector v = e.getVehicle().getLocation().toVector().subtract(islandTo.getCenter().toVector()).normalize().multiply(new Vector(1.2,0,1.2)); - if (DEBUG) - plugin.getLogger().info("DEBUG: direction vector = " + v); - e.getVehicle().setVelocity(v); - return; - } - } - } - if (islandTo !=null && islandFrom == null && (islandTo.getOwner() != null || islandTo.isSpawn())) { - // Entering - if (islandTo.isSpawn()) { - if (!plugin.getLocale(player.getUniqueId()).get("lock.enteringspawn").isEmpty()) { - if(islandTo.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - player.sendMessage(plugin.getLocale(player).get("lock.enteringspawn")); - } - } - } else { - if (!plugin.getLocale(player.getUniqueId()).get("lock.nowentering").isEmpty()) { - if(islandTo.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - player.sendMessage(plugin.getLocale(player).get("lock.nowentering").replace("[name]", plugin.getIslands().getIslandName(islandTo.getOwner()))); - } - } - } - } else if (islandTo == null && islandFrom != null && (islandFrom.getOwner() != null || islandFrom.isSpawn())) { - // Leaving - if (islandFrom.isSpawn()) { - // Leaving - if (!plugin.getLocale(player.getUniqueId()).get("lock.leavingspawn").isEmpty()) { - if(islandFrom.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - player.sendMessage(plugin.getLocale(player.getUniqueId()).get("lock.leavingspawn")); - } - } - } else { - if (!plugin.getLocale(player.getUniqueId()).get("lock.nowleaving").isEmpty()) { - if(islandFrom.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - player.sendMessage(plugin.getLocale(player.getUniqueId()).get("lock.nowleaving").replace("[name]", plugin.getIslands().getIslandName(islandFrom.getOwner()))); - } - } - } - } else if (islandTo != null && islandFrom !=null && !islandTo.equals(islandFrom)) { - // Adjacent islands or overlapping protections - if (islandFrom.isSpawn()) { - // Leaving - if (!plugin.getLocale(player.getUniqueId()).get("lock.leavingspawn").isEmpty()) { - if(islandFrom.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - player.sendMessage(plugin.getLocale(player).get("lock.leavingspawn")); - } - } - } else if (islandFrom.getOwner() != null){ - if (!plugin.getLocale(player.getUniqueId()).get("lock.nowleaving").isEmpty()) { - if(islandFrom.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - player.sendMessage(plugin.getLocale(player).get("lock.nowleaving").replace("[name]", plugin.getIslands().getIslandName(islandFrom.getOwner()))); - } - } - } - if (islandTo.isSpawn()) { - if (!plugin.getLocale(player.getUniqueId()).get("lock.enteringspawn").isEmpty()) { - if(islandTo.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - player.sendMessage(plugin.getLocale(player).get("lock.enteringspawn")); - } - } - } else if (islandTo.getOwner() != null) { - if (!plugin.getLocale(player.getUniqueId()).get("lock.nowentering").isEmpty()) { - if(islandTo.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - player.sendMessage(plugin.getLocale(player).get("lock.nowentering").replace("[name]", plugin.getIslands().getIslandName(islandTo.getOwner()))); - } - } - } - } - } - - - /** - * Adds island lock function - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onPlayerMove(final PlayerMoveEvent e) { - if (e.getPlayer().isDead()) { - return; - } - if (!Util.inWorld(e.getPlayer())) { - return; - } - if (plugin.getIslands() == null) { - return; - } - // Only do something if there is a definite x or z movement - if (e.getTo().getBlockX() - e.getFrom().getBlockX() == 0 && e.getTo().getBlockZ() - e.getFrom().getBlockZ() == 0) { - return; - } - final Island islandTo = plugin.getIslands().getProtectedIslandAt(e.getTo()); - // Announcement entering - final Island islandFrom = plugin.getIslands().getProtectedIslandAt(e.getFrom()); - // Only says something if there is a change in islands - /* - * Situations: - * islandTo == null && islandFrom != null - exit - * islandTo == null && islandFrom == null - nothing - * islandTo != null && islandFrom == null - enter - * islandTo != null && islandFrom != null - same PlayerIsland or teleport? - * islandTo == islandFrom - */ - // plugin.getLogger().info("islandTo = " + islandTo); - // plugin.getLogger().info("islandFrom = " + islandFrom); - if (islandTo != null && (islandTo.getOwner() != null || islandTo.isSpawn())) { - // Lock check - if (islandTo.isLocked() || plugin.getPlayers().isBanned(islandTo.getOwner(),e.getPlayer().getUniqueId())) { - if (!islandTo.getMembers().contains(e.getPlayer().getUniqueId()) - && !e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect") - && !e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypasslock")) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer().getUniqueId()).get("lock.islandlocked")); - - // Get the vector away from this island - if (e.getPlayer().isInsideVehicle()) { - if (e.getPlayer().getVehicle() instanceof LivingEntity) { - // Dismount - e.getPlayer().leaveVehicle(); - e.setCancelled(true); - } - - } else { - Vector v = e.getPlayer().getLocation().toVector().subtract(islandTo.getCenter().toVector()).normalize().multiply(new Vector(1.2,0,1.2)); - if (DEBUG) - plugin.getLogger().info("DEBUG: direction vector = " + v); - e.getPlayer().setVelocity(v); - } - return; - } - } - } - - if (islandTo != null && islandFrom == null && (islandTo.getOwner() != null || islandTo.isSpawn())) { - // Entering - if (islandTo.isLocked() || plugin.getPlayers().isBanned(islandTo.getOwner(),e.getPlayer().getUniqueId())) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("lock.islandlocked")); - } - if (islandTo.isSpawn()) { - if (!plugin.getLocale(e.getPlayer().getUniqueId()).get("lock.enteringspawn").isEmpty()) { - if(islandTo.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("lock.enteringspawn")); - } - } - } else { - if (!plugin.getLocale(e.getPlayer().getUniqueId()).get("lock.nowentering").isEmpty()) { - if(islandTo.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("lock.nowentering").replace("[name]", plugin.getIslands().getIslandName(islandTo.getOwner()))); - } - } - } - // Fire entry event - final IslandBaseEvent event = IslandEvent.builder() - .island(islandTo) - .reason(Reason.ENTER) - .involvedPlayer(e.getPlayer().getUniqueId()) - .location(e.getTo()) - .build(); - plugin.getServer().getPluginManager().callEvent(event); - } else if (islandTo == null && islandFrom != null && (islandFrom.getOwner() != null || islandFrom.isSpawn())) { - // Leaving - if (islandFrom.isSpawn()) { - // Leaving - if(islandFrom.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("lock.leavingspawn")); - } - } else { - if(islandFrom.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("lock.nowleaving").replace("[name]", plugin.getIslands().getIslandName(islandFrom.getOwner()))); - } - } - // Fire exit event - final IslandBaseEvent event = IslandEvent.builder() - .island(islandTo) - .reason(Reason.EXIT) - .involvedPlayer(e.getPlayer().getUniqueId()) - .location(e.getFrom()) - .build(); - plugin.getServer().getPluginManager().callEvent(event); - } else if (islandTo != null && islandFrom != null && !islandTo.equals(islandFrom)) { - // Adjacent islands or overlapping protections - if (islandFrom.isSpawn()) { - // Leaving - if(islandFrom.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("lock.leavingspawn")); - } - } else if (islandFrom.getOwner() != null) { - if(islandFrom.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("lock.nowleaving").replace("[name]", plugin.getIslands().getIslandName(islandFrom.getOwner()))); - } - } - if (islandTo.isSpawn()) { - if(islandTo.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("lock.enteringspawn")); - } - } else if (islandTo.getOwner() != null) { - if(islandTo.getFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer().getUniqueId()).get("lock.nowentering").replace("[name]", plugin.getIslands().getIslandName(islandTo.getOwner()))); - } - } - // Fire exit event - final IslandBaseEvent event = IslandEvent.builder() - .island(islandTo) - .reason(Reason.EXIT) - .involvedPlayer(e.getPlayer().getUniqueId()) - .location(e.getFrom()) - .build(); - plugin.getServer().getPluginManager().callEvent(event); - // Fire entry event - final IslandBaseEvent event2 = IslandEvent.builder() - .island(islandTo) - .reason(Reason.ENTER) - .involvedPlayer(e.getPlayer().getUniqueId()) - .location(e.getTo()) - .build(); - plugin.getServer().getPluginManager().callEvent(event2); - } - } - - - /** - * Prevents mobs spawning at spawn or in an island - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onMobSpawn(final CreatureSpawnEvent e) { - if (DEBUG2) { - plugin.getLogger().info("on Mob spawn" + e.getEventName()); - } - // if grid is not loaded yet, return. - if (plugin.getIslands() == null) { - return; - } - // If not in the right world, return - if (!Util.inWorld(e.getEntity())) { - return; - } - // Deal with natural spawning - if (e.getEntity() instanceof Monster || e.getEntity() instanceof Slime) { - if (!actionAllowed(e.getLocation(), SettingsFlag.MONSTER_SPAWN)) { - if (DEBUG2) - plugin.getLogger().info("Natural monster spawn cancelled."); - // Mobs not allowed to spawn - e.setCancelled(true); - return; - } - } - if (e.getEntity() instanceof Animals) { - if (!actionAllowed(e.getLocation(), SettingsFlag.ANIMAL_SPAWN)) { - // Animals are not allowed to spawn - if (DEBUG2) - plugin.getLogger().info("Natural animal spawn cancelled."); - e.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onExplosion(final EntityExplodeEvent e) { - if (DEBUG) { - // Commented out due to Ender dragon spam in Paper Spigot - //plugin.getLogger().info(e.getEventName()); - //plugin.getLogger().info("Entity exploding is " + e.getEntity()); - } - if (!Util.inWorld(e.getLocation())) { - return; - } - // Find out what is exploding - Entity expl = e.getEntity(); - if (expl == null) { - // This allows beds to explode or other null entities, but still curtail the damage - // Note player can still die from beds exploding in the nether. - if (!Settings.allowTNTDamage) { - //plugin.getLogger().info("TNT block damage prevented"); - e.blockList().clear(); - } else { - if (!Settings.allowChestDamage) { - List toberemoved = new ArrayList<>(); - // Save the chest blocks in a list - for (Block b : e.blockList()) { - switch (b.getType()) { - case CHEST: - case ENDER_CHEST: - case STORAGE_MINECART: - case TRAPPED_CHEST: - toberemoved.add(b); - break; - default: - break; - } - } - // Now delete them - for (Block b : toberemoved) { - e.blockList().remove(b); - } - } - } - return; - } - // prevent at spawn - if (plugin.getIslands().isAtSpawn(e.getLocation())) { - e.setCancelled(true); - } - // Find out what is exploding - EntityType exploding = e.getEntityType(); - if (exploding == null) { - return; - } - switch (exploding) { - case CREEPER: - if (!Settings.allowCreeperDamage) { - // plugin.getLogger().info("Creeper block damage prevented"); - e.blockList().clear(); - } else { - // Check if creeper griefing is allowed - if (!Settings.allowCreeperGriefing) { - // Find out who the creeper was targeting - Creeper creeper = (Creeper)e.getEntity(); - if (creeper.getTarget() instanceof Player) { - Player target = (Player)creeper.getTarget(); - // Check if the target is on their own island or not - if (!plugin.getIslands().locationIsOnIsland(target, e.getLocation())) { - // They are a visitor tsk tsk - // Stop the blocks from being damaged, but allow hurt still - e.blockList().clear(); - } - } - // Check if this creeper was lit by a visitor - if (litCreeper.contains(creeper.getUniqueId())) { - if (DEBUG) { - plugin.getLogger().info("DBEUG: preventing creeper from damaging"); - } - litCreeper.remove(creeper.getUniqueId()); - e.setCancelled(true); - return; - } - } - if (!Settings.allowChestDamage) { - List toberemoved = new ArrayList(); - // Save the chest blocks in a list - for (Block b : e.blockList()) { - switch (b.getType()) { - case CHEST: - case ENDER_CHEST: - case STORAGE_MINECART: - case TRAPPED_CHEST: - toberemoved.add(b); - break; - default: - break; - } - } - // Now delete them - for (Block b : toberemoved) { - e.blockList().remove(b); - } - } - } - break; - case PRIMED_TNT: - case MINECART_TNT: - if (!Settings.allowTNTDamage) { - // plugin.getLogger().info("TNT block damage prevented"); - e.blockList().clear(); - } else { - if (!Settings.allowChestDamage) { - List toberemoved = new ArrayList<>(); - // Save the chest blocks in a list - for (Block b : e.blockList()) { - switch (b.getType()) { - case CHEST: - case ENDER_CHEST: - case STORAGE_MINECART: - case TRAPPED_CHEST: - toberemoved.add(b); - break; - default: - break; - } - } - // Now delete them - for (Block b : toberemoved) { - e.blockList().remove(b); - } - } - } - break; - default: - break; - } - } - - /** - * Allows or prevents enderman griefing - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onEndermanGrief(final EntityChangeBlockEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (!(e.getEntity() instanceof Enderman)) { - return; - } - if (!Util.inWorld(e.getEntity())) { - return; - } - // Prevent Enderman griefing at spawn - if (plugin.getIslands() != null && plugin.getIslands().isAtSpawn(e.getEntity().getLocation())) { - e.setCancelled(true); - } - if (Settings.allowEndermanGriefing) - return; - // Stop the Enderman from griefing - // plugin.getLogger().info("Enderman stopped from griefing); - e.setCancelled(true); - } - - /** - * Drops the Enderman's block when he dies if he has one - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onEndermanDeath(final EntityDeathEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (!Settings.endermanDeathDrop) - return; - if (!Util.inWorld(e.getEntity())) { - return; - } - if (!(e.getEntity() instanceof Enderman)) { - // plugin.getLogger().info("Not an Enderman!"); - return; - } - // Get the block the enderman is holding - Enderman ender = (Enderman) e.getEntity(); - MaterialData m = ender.getCarriedMaterial(); - if (m != null && !m.getItemType().equals(Material.AIR)) { - // Drop the item - // plugin.getLogger().info("Dropping item " + m.toString()); - e.getEntity().getWorld().dropItemNaturally(e.getEntity().getLocation(), m.toItemStack(1)); - } - } - - /* - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false) - public void onBlockBreakCheck(final BlockPhysicsEvent e) { - if (DEBUG) { - plugin.getLogger().info("DEBUG: check: " + e.getEventName()); - plugin.getLogger().info("DEBUG: block is " + e.getBlock()); - } - } - */ - - /** - * Prevents blocks from being broken - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onBlockBreak(final BlockBreakEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (Settings.allowAutoActivator && e.getPlayer().getName().equals("[CoFH]")) { - return; - } - if (Util.inWorld(e.getPlayer())) { - if (actionAllowed(e.getPlayer(), e.getBlock().getLocation(), SettingsFlag.BREAK_BLOCKS)) { - return; - } - // Everyone else - not allowed - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onItemFrameDamage(final EntityDamageByEntityEvent e) { - // Check world - if (!Util.inWorld(e.getEntity()) || !(e.getEntity() instanceof ItemFrame)) { - return; - } - if (e.getDamager() instanceof Projectile) { - if (DEBUG) - plugin.getLogger().info("DEBUG: Projectile damage to itemframe"); - // Find out who fired the arrow - Projectile p = (Projectile) e.getDamager(); - if (DEBUG) - plugin.getLogger().info("DEBUG: Shooter is " + p.getShooter().toString()); - if (p.getShooter() instanceof Skeleton || p.getShooter() instanceof Golem) { - if (DEBUG) - plugin.getLogger().info("DEBUG: Shooter is mob"); - if (!Settings.allowMobDamageToItemFrames) { - if (DEBUG) - plugin.getLogger().info("DEBUG: Damage not allowed, cancelling"); - e.setCancelled(true); - } - } - } - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onItemFrameDamage(final HangingBreakByEntityEvent e) { - if (DEBUG) { - plugin.getLogger().info("DEBUG: Hanging break by entity event"); - plugin.getLogger().info("DEBUG: cause = " + e.getCause()); - plugin.getLogger().info("DEBUG: entity = " + e.getEntity()); - plugin.getLogger().info("DEBUG: remover = " + e.getRemover()); - } - // Check world - if (!Util.inWorld(e.getEntity()) || !(e.getEntity() instanceof ItemFrame)) { - return; - } - if (e.getRemover() instanceof Skeleton || e.getRemover() instanceof Golem) { - if (DEBUG) - plugin.getLogger().info("DEBUG: Remover is mob"); - if (!Settings.allowMobDamageToItemFrames) { - if (DEBUG) - plugin.getLogger().info("DEBUG: Damage not allowed, cancelling"); - e.setCancelled(true); - } - } - } - /** - * This method protects players from PVP if it is not allowed and from - * arrows fired by other players - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onEntityDamage(final EntityDamageByEntityEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - plugin.getLogger().info("DEBUG: Damager = " + e.getDamager().toString()); - plugin.getLogger().info("DEBUG: Entitytype = " + e.getEntityType()); - plugin.getLogger().info("DEBUG: Entity = " + e.getEntity()); - } - // Check world - if (!Util.inWorld(e.getEntity())) { - return; - } - // Get the island where the damage is occurring - Island island = plugin.getIslands().getProtectedIslandAt(e.getEntity().getLocation()); - boolean inNether = false; - if (e.getEntity().getWorld().equals(IslandWorld.getNetherWorld())) { - inNether = true; - } - // Stop TNT damage if it is disallowed - if (!Settings.allowTNTDamage && (e.getDamager().getType().equals(EntityType.PRIMED_TNT) - || e.getDamager().getType().equals(EntityType.FIREWORK) - || e.getDamager().getType().equals(EntityType.FIREBALL))) { - if (DEBUG) - plugin.getLogger().info("DEBUG: cancelling firework or fireball damage"); - e.setCancelled(true); - return; - } - // Check for creeper damage at spawn - if (island != null && island.isSpawn() && e.getDamager().getType().equals(EntityType.CREEPER) && island.getFlag(SettingsFlag.CREEPER_HURT)) { - return; - } - // Stop Creeper damager if it is disallowed - if (!Settings.allowCreeperDamage && e.getDamager().getType().equals(EntityType.CREEPER) && !(e.getEntity() instanceof Player)) { - e.setCancelled(true); - return; - } - // Stop Creeper griefing if it is disallowed - if (!Settings.allowCreeperGriefing && e.getDamager().getType().equals(EntityType.CREEPER)) { - // Now we have to check what the target was - Creeper creeper = (Creeper)e.getDamager(); - //plugin.getLogger().info("DEBUG: creeper is damager"); - //plugin.getLogger().info("DEBUG: entity being damaged is " + e.getEntity()); - if (creeper.getTarget() instanceof Player) { - //plugin.getLogger().info("DEBUG: target is a player"); - Player target = (Player)creeper.getTarget(); - //plugin.getLogger().info("DEBUG: player = " + target.getName()); - // Check if the target is on their own island or not - if (!plugin.getIslands().locationIsOnIsland(target, e.getEntity().getLocation())) { - // They are a visitor tsk tsk - //plugin.getLogger().info("DEBUG: player is a visitor"); - e.setCancelled(true); - return; - } - } - // Check if this creeper was lit by a visitor - if (litCreeper.contains(creeper.getUniqueId())) { - if (DEBUG) { - plugin.getLogger().info("DEBUG: preventing creeeper from damaging"); - } - e.setCancelled(true); - return; - } - } - // Ops can do anything - if (e.getDamager() instanceof Player) { - Player p = (Player) e.getDamager(); - if (p.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - } - // Get the real attacker - boolean flamingArrow = false; - boolean projectile = false; - Player attacker = null; - if (e.getDamager() instanceof Player) { - attacker = (Player)e.getDamager(); - } else if (e.getDamager() instanceof Projectile) { - if (DEBUG) - plugin.getLogger().info("DEBUG: Projectile damage"); - projectile = true; - // Find out who fired the arrow - Projectile p = (Projectile) e.getDamager(); - if (DEBUG) - plugin.getLogger().info("DEBUG: Shooter is " + p.getShooter().toString()); - if (p.getShooter() instanceof Player) { - attacker = (Player) p.getShooter(); - if (p.getFireTicks() > 0) { - flamingArrow = true; - } - // Check if this is a flaming arrow - if (DEBUG) - plugin.getLogger().info("DEBUG: fire ticks = " + p.getFireTicks() + " max = " + p.getMaxFireTicks()); - } - } - if (attacker == null) { - // Not a player - return; - } - // Self damage - if (e.getEntity() instanceof Player && attacker.equals((Player)e.getEntity())) { - if (DEBUG) plugin.getLogger().info("Self damage!"); - return; - } - if (DEBUG) - plugin.getLogger().info("DEBUG: Another player"); - - // ITEM FRAME ENTITY DAMAGE or Armor Stand - // Check to see if it's an item frame - if (e.getEntity() instanceof ItemFrame || e.getEntityType().toString().endsWith("STAND")) { - if (island != null && (island.getFlag(SettingsFlag.BREAK_BLOCKS) || island.getMembers().contains(attacker.getUniqueId()))) { - return; - } - // Else not allowed - attacker.sendMessage(plugin.getLocale(attacker).get("island.protected")); - if (flamingArrow) - e.getEntity().setFireTicks(0); - if (projectile) - e.getDamager().remove(); - e.setCancelled(true); - return; - } - // Monsters being hurt - if (e.getEntity() instanceof Monster || e.getEntity() instanceof Slime || e.getEntity() instanceof Squid) { - // Normal island check - if (island != null && island.getMembers().contains(attacker.getUniqueId())) { - // Members always allowed - return; - } - if (actionAllowed(attacker, e.getEntity().getLocation(), SettingsFlag.HURT_MONSTERS)) { - // Check for visitors setting creepers alight using flint steel - if (!Settings.allowCreeperGriefing && e.getEntity() instanceof Creeper) { - for (ItemStack holding : Util.getPlayerInHandItems(attacker)) { - if (holding.getType().equals(Material.FLINT_AND_STEEL)) { - // Save this creeper for later when any damage caused by its explosion will be nullified - litCreeper.add(e.getEntity().getUniqueId()); - if (DEBUG) { - plugin.getLogger().info("DEBUG: adding to lit creeper set"); - } - } - } - } - return; - } - // Not allowed - attacker.sendMessage(plugin.getLocale(attacker).get("island.protected")); - if (flamingArrow) - e.getEntity().setFireTicks(0); - if (projectile) - e.getDamager().remove(); - e.setCancelled(true); - return; - } - - // Mobs being hurt - if (e.getEntity() instanceof Animals || e.getEntity() instanceof IronGolem || e.getEntity() instanceof Snowman - || e.getEntity() instanceof Villager) { - if (island != null && (island.getFlag(SettingsFlag.HURT_ANIMALS) || island.getMembers().contains(attacker.getUniqueId()))) { - return; - } - if (DEBUG) - plugin.getLogger().info("DEBUG: Mobs not allowed to be hurt. Blocking"); - // Else not allowed - attacker.sendMessage(plugin.getLocale(attacker).get("island.protected")); - if (flamingArrow) - e.getEntity().setFireTicks(0); - if (projectile) - e.getDamager().remove(); - e.setCancelled(true); - return; - } - - // Establish whether PVP is allowed or not. - boolean pvp = false; - if ((inNether && island != null && island.getFlag(SettingsFlag.PVP_NETHER) || (!inNether && island != null && island.getFlag(SettingsFlag.PVP_OVERWORLD)))) { - if (DEBUG) plugin.getLogger().info("DEBUG: PVP allowed"); - pvp = true; - } - - // Players being hurt PvP - if (e.getEntity() instanceof Player) { - if (!pvp) { - attacker.sendMessage(plugin.getLocale(attacker).get("targetInPVPArea")); - if (flamingArrow) - e.getEntity().setFireTicks(0); - if (projectile) - e.getDamager().remove(); - e.setCancelled(true); - } - } - } - - /** - * Prevents placing of blocks - * - * @param e - */ - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onPlayerBlockPlace(final BlockPlaceEvent e) { - if (DEBUG) { - plugin.getLogger().info("DEBUG: " + e.getEventName()); - if (e.getPlayer() == null) { - plugin.getLogger().info("DEBUG: player is null"); - } else { - plugin.getLogger().info("DEBUG: block placed by " + e.getPlayer().getName()); - } - plugin.getLogger().info("DEBUG: Block is " + e.getBlock().toString()); - } - if (Settings.allowAutoActivator && e.getPlayer().getName().equals("[CoFH]")) { - return; - } - // plugin.getLogger().info(e.getEventName()); - if (Util.inWorld(e.getPlayer())) { - // This permission bypasses protection - if (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - //plugin.getLogger().info("DEBUG: checking is inside protection area"); - Island island = plugin.getIslands().getProtectedIslandAt(e.getBlock().getLocation()); - // Outside of island protection zone - if (island == null) { - if (!Settings.defaultWorldSettings.get(SettingsFlag.PLACE_BLOCKS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - return; - } - if (actionAllowed(e.getPlayer(), e.getBlock().getLocation(), SettingsFlag.PLACE_BLOCKS)) { - // Check how many placed - //plugin.getLogger().info("DEBUG: block placed " + e.getBlock().getType()); - String type = e.getBlock().getType().toString(); - if (!e.getBlock().getState().getClass().getName().endsWith("CraftBlockState") - // Not all blocks have that type of class, so we have to do some explicit checking... - || e.getBlock().getType().equals(Material.REDSTONE_COMPARATOR_OFF) - || type.endsWith("BANNER") // Avoids V1.7 issues - || e.getBlock().getType().equals(Material.ENDER_CHEST) - || e.getBlock().getType().equals(Material.ENCHANTMENT_TABLE) - || e.getBlock().getType().equals(Material.DAYLIGHT_DETECTOR) - || e.getBlock().getType().equals(Material.FLOWER_POT)){ - // tile entity placed - if (Settings.limitedBlocks.containsKey(type) && Settings.limitedBlocks.get(type) > -1) { - int count = island.getTileEntityCount(e.getBlock().getType(),e.getBlock().getWorld()); - //plugin.getLogger().info("DEBUG: count is "+ count); - if (Settings.limitedBlocks.get(type) <= count) { - e.getPlayer().sendMessage((plugin.getLocale(e.getPlayer()).get("moblimits.entity").replace("[entity]", - Util.prettifyText(type))).replace("[number]", String.valueOf(Settings.limitedBlocks.get(type)))); - e.setCancelled(true); - } - } - } - } else { - // Visitor - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onPlayerBlockPlace(final BlockMultiPlaceEvent e) { - if (DEBUG) { - plugin.getLogger().info("DEBUG: " + e.getEventName()); - if (e.getPlayer() == null) { - plugin.getLogger().info("DEBUG: player is null"); - } else { - plugin.getLogger().info("DEBUG: block placed by " + e.getPlayer().getName()); - } - plugin.getLogger().info("DEBUG: Block is " + e.getBlock().toString()); - } - if (Settings.allowAutoActivator && e.getPlayer().getName().equals("[CoFH]")) { - return; - } - // plugin.getLogger().info(e.getEventName()); - if (Util.inWorld(e.getPlayer())) { - // This permission bypasses protection - if (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - Island island = plugin.getIslands().getProtectedIslandAt(e.getBlock().getLocation()); - if (island == null) { - if (!Settings.defaultWorldSettings.get(SettingsFlag.PLACE_BLOCKS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - return; - } - // Island exists - if (island.getFlag(SettingsFlag.PLACE_BLOCKS) || island.getMembers().contains(e.getPlayer().getUniqueId())) { - // Check how many placed - //plugin.getLogger().info("DEBUG: block placed " + e.getBlock().getType()); - String type = e.getBlock().getType().toString(); - if (!e.getBlock().getState().getClass().getName().endsWith("CraftBlockState") - // Not all blocks have that type of class, so we have to do some explicit checking... - || e.getBlock().getType().equals(Material.REDSTONE_COMPARATOR_OFF) - || type.endsWith("BANNER") // Avoids V1.7 issues - || e.getBlock().getType().equals(Material.ENDER_CHEST) - || e.getBlock().getType().equals(Material.ENCHANTMENT_TABLE) - || e.getBlock().getType().equals(Material.DAYLIGHT_DETECTOR) - || e.getBlock().getType().equals(Material.FLOWER_POT)){ - // tile entity placed - if (Settings.limitedBlocks.containsKey(type) && Settings.limitedBlocks.get(type) > -1) { - int count = island.getTileEntityCount(e.getBlock().getType(),e.getBlock().getWorld()); - if (Settings.limitedBlocks.get(type) <= count) { - e.getPlayer().sendMessage((plugin.getLocale(e.getPlayer()).get("moblimits.entity").replace("[entity]", - Util.prettifyText(type))).replace("[number]", String.valueOf(Settings.limitedBlocks.get(type)))); - e.setCancelled(true); - return; - } - } - } - return; - } - // Outside of protection area or visitor - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onPlayerLeashHitch(final HangingPlaceEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - plugin.getLogger().info("DEBUG: block placed " + e.getBlock().getType()); - plugin.getLogger().info("DEBUG: entity " + e.getEntity().getType()); - } - if (Settings.allowAutoActivator && e.getPlayer().getName().equals("[CoFH]")) { - return; - } - // plugin.getLogger().info(e.getEventName()); - if (Util.inWorld(e.getPlayer())) { - if (e.getEntity() != null && e.getEntity().getType().equals(EntityType.LEASH_HITCH)) { - if (!actionAllowed(e.getPlayer(), e.getBlock().getLocation(), SettingsFlag.LEASH)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } - } - } - - - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onPlayerBlockPlace(final HangingPlaceEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - plugin.getLogger().info("DEBUG: block placed " + e.getBlock().getType()); - plugin.getLogger().info("DEBUG: entity " + e.getEntity().getType()); - } - if (Settings.allowAutoActivator && e.getPlayer().getName().equals("[CoFH]")) { - return; - } - // plugin.getLogger().info(e.getEventName()); - if (Util.inWorld(e.getPlayer())) { - // This permission bypasses protection - if (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - Island island = plugin.getIslands().getProtectedIslandAt(e.getBlock().getLocation()); - // Outside of island protection zone - if (island == null) { - if (!Settings.defaultWorldSettings.get(SettingsFlag.PLACE_BLOCKS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - return; - } - if (island.getFlag(SettingsFlag.PLACE_BLOCKS) || island.getMembers().contains(e.getPlayer().getUniqueId())) { - // Check how many placed - String type = e.getEntity().getType().toString(); - if (e.getEntity().getType().equals(EntityType.ITEM_FRAME) || e.getEntity().getType().equals(EntityType.PAINTING)) { - // tile entity placed - if (Settings.limitedBlocks.containsKey(type) && Settings.limitedBlocks.get(type) > -1) { - // Convert from EntityType to Material via string - ugh - int count = island.getTileEntityCount(Material.valueOf(type),e.getEntity().getWorld()); - if (Settings.limitedBlocks.get(type) <= count) { - e.getPlayer().sendMessage((plugin.getLocale(e.getPlayer()).get("moblimits.entity").replace("[entity]", - Util.prettifyText(type))).replace("[number]", String.valueOf(Settings.limitedBlocks.get(type)))); - e.setCancelled(true); - } - } - } - } else { - // Visitor - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } - } - - // Prevent sleeping in other beds - @EventHandler(priority = EventPriority.LOW) - public void onPlayerBedEnter(final PlayerBedEnterEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - // Check world - if (Util.inWorld(e.getPlayer())) { - if (actionAllowed(e.getPlayer(),e.getBed().getLocation(), SettingsFlag.BED)) { - return; - } - // Not allowed - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } - - /** - * Prevents the breakage of hanging items - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW) - public void onBreakHanging(final HangingBreakByEntityEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - plugin.getLogger().info(e.getRemover().toString()); - } - if (Util.inWorld(e.getEntity())) { - if ((e.getRemover() instanceof Creeper) && !Settings.allowCreeperDamage) { - e.setCancelled(true); - return; - } - // Check if creeper griefing is allowed - if ((e.getRemover() instanceof Creeper) && !Settings.allowCreeperGriefing) { - // Find out who the creeper was targeting - Creeper creeper = (Creeper)e.getRemover(); - if (creeper.getTarget() instanceof Player) { - Player target = (Player)creeper.getTarget(); - // Check if the target is on their own island or not - if (!plugin.getIslands().locationIsOnIsland(target, e.getEntity().getLocation())) { - // They are a visitor tsk tsk - e.setCancelled(true); - return; - } - } - // Check if this creeper was lit by a visitor - if (litCreeper.contains(creeper.getUniqueId())) { - if (DEBUG) { - plugin.getLogger().info("DBEUG: preventing creeper from damaging"); - } - e.setCancelled(true); - return; - } - } - if (e.getRemover() instanceof Player) { - Player p = (Player) e.getRemover(); - // This permission bypasses protection - if (p.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - if (actionAllowed(p,e.getEntity().getLocation(),SettingsFlag.BREAK_BLOCKS)) { - return; - } - // Not allowed - p.sendMessage(plugin.getLocale(p.getUniqueId()).get("island.protected")); - e.setCancelled(true); - } - } - } - - /** - * Prevents the leash use - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW) - public void onLeashUse(final PlayerLeashEntityEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (Util.inWorld(e.getEntity())) { - if (e.getPlayer() != null) { - Player player = e.getPlayer(); - if (actionAllowed(player, e.getEntity().getLocation(),SettingsFlag.LEASH)) { - return; - } - player.sendMessage(plugin.getLocale(player).get("island.protected")); - e.setCancelled(true); - } - } - } - - - /** - * Prevents the leash use - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW) - public void onLeashUse(final PlayerUnleashEntityEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (Util.inWorld(e.getEntity())) { - if (e.getPlayer() != null) { - Player player = e.getPlayer(); - if (actionAllowed(player, e.getEntity().getLocation(),SettingsFlag.LEASH)) { - return; - } - player.sendMessage(plugin.getLocale(player).get("island.protected")); - e.setCancelled(true); - } - } - } - /* - * Not going to implement this right now, but it could be used if required - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onLiquidFlow(final BlockFromToEvent e) { - // Ignore non-island worlds - if (!Util.inWorld(e.getBlock())) { - return; - } - // Only check lateral movement - if (e.getBlock().getLocation().getBlockX() == e.getToBlock().getLocation().getBlockX() - && e.getBlock().getLocation().getBlockZ() == e.getToBlock().getLocation().getBlockZ()) { - return; - } - // Ignore flows within flow - if (e.getBlock().getType().equals(e.getToBlock().getType())) { - return; - } - // Ignore stationary to non-stationary - if (e.getBlock().getType().equals(Material.STATIONARY_WATER) && e.getToBlock().getType().equals(Material.WATER) ) { - return; - } - if (e.getBlock().getType().equals(Material.STATIONARY_LAVA) && e.getToBlock().getType().equals(Material.LAVA) ) { - return; - } - // Check if flow leaving island protection range - // Check home island - Island from = plugin.getIslands().getProtectedIslandAt(e.getBlock().getLocation()); - Island to = plugin.getIslands().getProtectedIslandAt(e.getToBlock().getLocation()); - // Scenarios - // 1. inside district or outside - always ok - // 2. inside to outside - not allowed - // 3. outside to inside - allowed - if (to == null && from == null) { - return; - } - if (to !=null && from != null && to.equals(from)) { - return; - } - // Flows into an island space is allowed, e.g., sea - if (to != null) { - return; - } - // Otherwise cancel - the flow is not allowed - e.setCancelled(true); - } - */ - - /** - * Prevents emptying of buckets outside of island space - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onBucketEmpty(final PlayerBucketEmptyEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (Util.inWorld(e.getPlayer())) { - Player p = e.getPlayer(); - if (e.getBlockClicked() != null) { - // This is where the water or lava actually will be dumped - Block dumpBlock = e.getBlockClicked().getRelative(e.getBlockFace()); - if (actionAllowed(p, dumpBlock.getLocation(), SettingsFlag.BUCKET)) { - // Check if biome is Nether and then allow water placement but fizz the water - if (e.getBlockClicked().getBiome().equals(Biome.HELL)) { - if (plugin.getServer().getVersion().contains("(MC: 1.8") || plugin.getServer().getVersion().contains("(MC: 1.7")) { - if (e.getPlayer().getItemInHand().getType().equals(Material.WATER_BUCKET)) { - e.setCancelled(true); - e.getPlayer().getItemInHand().setType(Material.BUCKET); - e.getPlayer().getWorld().playSound(e.getPlayer().getLocation(), Sound.valueOf("FIZZ"), 1F, 2F); - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("biome.set").replace("[biome]", "Nether")); - } - } else { - if (Util.playerIsHolding(e.getPlayer(), Material.WATER_BUCKET)) { - e.setCancelled(true); - if (e.getPlayer().getInventory().getItemInMainHand().getType() == Material.WATER_BUCKET) { - e.getPlayer().getInventory().setItemInMainHand(new ItemStack(Material.BUCKET)); - } else if (e.getPlayer().getInventory().getItemInOffHand().getType() == Material.WATER_BUCKET) { - e.getPlayer().getInventory().setItemInOffHand(new ItemStack(Material.BUCKET)); - } - e.getPlayer().getWorld().playSound(e.getPlayer().getLocation(), Sound.ENTITY_CREEPER_PRIMED, 1F, 2F); - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("biome.set").replace("[biome]", "Nether")); - } - } - } - return; - } - // Not allowed - p.sendMessage(plugin.getLocale(p.getUniqueId()).get("island.protected")); - e.setCancelled(true); - } - } - } - - /** - * Prevents water from being dispensed in hell biomes - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW) - public void onNetherDispenser(final BlockDispenseEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (!Util.inWorld(e.getBlock().getLocation()) || !e.getBlock().getBiome().equals(Biome.HELL)) { - return; - } - // plugin.getLogger().info("DEBUG: Item being dispensed is " + - // e.getItem().getType().toString()); - if (e.getItem().getType().equals(Material.WATER_BUCKET)) { - e.setCancelled(true); - if (plugin.getServer().getVersion().contains("(MC: 1.8") || plugin.getServer().getVersion().contains("(MC: 1.7")) { - e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.valueOf("FIZZ"), 1F, 2F); - } else { - e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.ENTITY_CREEPER_PRIMED, 1F, 2F); - } - } - } - - @EventHandler(priority = EventPriority.LOW) - public void onBucketFill(final PlayerBucketFillEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (Util.inWorld(e.getPlayer())) { - // This permission bypasses protection - if (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - Island island = plugin.getIslands().getProtectedIslandAt(e.getBlockClicked().getLocation()); - if (island != null) { - if (island.getMembers().contains(e.getPlayer().getUniqueId())) { - return; - } - if (island.getFlag(SettingsFlag.COLLECT_LAVA) && e.getItemStack().getType().equals(Material.LAVA_BUCKET)) { - return; - } - if (island.getFlag(SettingsFlag.COLLECT_WATER) && e.getItemStack().getType().equals(Material.WATER_BUCKET)) { - return; - } - if (island.getFlag(SettingsFlag.MILKING) && e.getItemStack().getType().equals(Material.MILK_BUCKET)) { - return; - } - if (island.getFlag(SettingsFlag.BUCKET)) { - return; - } - } else { - // Null - if (Settings.defaultWorldSettings.get(SettingsFlag.BUCKET)) { - return; - } - } - // Not allowed - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } - - // Protect sheep - @EventHandler(priority = EventPriority.LOW) - public void onShear(final PlayerShearEntityEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (Util.inWorld(e.getPlayer())) { - if (actionAllowed(e.getPlayer(), e.getEntity().getLocation(), SettingsFlag.SHEARING)) { - return; - } - // Not allowed - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } - - /** - * Handles interaction with objects - * - * @param e - */ - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onPlayerInteract(final PlayerInteractEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (!Util.inWorld(e.getPlayer())) { - return; - } - if (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - if ((e.getClickedBlock() != null && plugin.getIslands().locationIsOnIsland(e.getPlayer(), e.getClickedBlock().getLocation()))) { - // You can do anything on your island - return; - } - // Player is not clicking a block, they are clicking a material so this - // is driven by where the player is - if (e.getClickedBlock() == null && (e.getMaterial() != null && plugin.getIslands().playerIsOnIsland(e.getPlayer()))) { - return; - } - // Get island - Island island = plugin.getIslands().getProtectedIslandAt(e.getPlayer().getLocation()); - if (DEBUG) { - if (island == null) { - plugin.getLogger().info("DEBUG: Player is not on a known island."); - } else { - plugin.getLogger().info("DEBUG: Player is on a known island."); - } - } - // Check for disallowed clicked blocks - if (e.getClickedBlock() != null) { - if (DEBUG) { - plugin.getLogger().info("DEBUG: clicked block " + e.getClickedBlock()); - plugin.getLogger().info("DEBUG: Material " + e.getMaterial()); - } - // Look along player's sight line to see if any blocks are fire - try { - BlockIterator iter = new BlockIterator(e.getPlayer(), 10); - Block lastBlock = iter.next(); - while (iter.hasNext()) { - lastBlock = iter.next(); - if (DEBUG) - plugin.getLogger().info("DEBUG: lastBlock = " + lastBlock.toString()); - if (lastBlock.equals(e.getClickedBlock())) { - if (DEBUG) - plugin.getLogger().info("DEBUG: found clicked block"); - break; - } - if (lastBlock.getType().equals(Material.FIRE)) { - if (DEBUG) - plugin.getLogger().info("DEBUG: fire found"); - if (island != null) { - if (!island.getFlag(SettingsFlag.FIRE_EXTINGUISH)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } else { - if (Settings.defaultWorldSettings.get(SettingsFlag.FIRE_EXTINGUISH)) { - if (DEBUG) - plugin.getLogger().info("DEBUG: extinguishing is allowed"); - continue; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - } - } - } catch (Exception ex) { - // To catch at block iterator exceptions that can happen in the void or at the very top of blocks - if (DEBUG) { - plugin.getLogger().info("DEBUG: block iterator error"); - ex.printStackTrace(); - } - } - // Handle Shulker Boxes - if (e.getClickedBlock().getType().toString().contains("SHULKER_BOX")) { - if (island == null) { - if (!Settings.defaultWorldSettings.get(SettingsFlag.OPEN_CHESTS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } else if (!island.getFlag(SettingsFlag.OPEN_CHESTS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - return; - } - // Handle fireworks - if (e.getMaterial() != null && e.getMaterial().equals(Material.FIREWORK)) { - if (island == null) { - if (!Settings.defaultWorldSettings.get(SettingsFlag.PLACE_BLOCKS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } else if (!island.getFlag(SettingsFlag.PLACE_BLOCKS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - return; - } - if (DEBUG) - plugin.getLogger().info("DEBUG: clicked block = " + e.getClickedBlock().getType()); - switch (e.getClickedBlock().getType()) { - case WOODEN_DOOR: - case SPRUCE_DOOR: - case ACACIA_DOOR: - case DARK_OAK_DOOR: - case BIRCH_DOOR: - case JUNGLE_DOOR: - case TRAP_DOOR: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.DOOR)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.DOOR)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case FENCE_GATE: - case SPRUCE_FENCE_GATE: - case ACACIA_FENCE_GATE: - case DARK_OAK_FENCE_GATE: - case BIRCH_FENCE_GATE: - case JUNGLE_FENCE_GATE: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.GATE)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.GATE)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case ENDER_CHEST: - break; - case CHEST: - case TRAPPED_CHEST: - case DISPENSER: - case DROPPER: - case HOPPER: - case HOPPER_MINECART: - case STORAGE_MINECART: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.OPEN_CHESTS)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.OPEN_CHESTS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case SOIL: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.CROP_TRAMPLE)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.CROP_TRAMPLE)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case BREWING_STAND: - case CAULDRON: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.BREWING)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.BREWING)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case DIODE: - case DIODE_BLOCK_OFF: - case DIODE_BLOCK_ON: - case REDSTONE_COMPARATOR_ON: - case REDSTONE_COMPARATOR_OFF: - case DAYLIGHT_DETECTOR: - case DAYLIGHT_DETECTOR_INVERTED: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.REDSTONE)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.REDSTONE)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case ENCHANTMENT_TABLE: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.ENCHANTING)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.ENCHANTING)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case FURNACE: - case BURNING_FURNACE: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.FURNACE)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.FURNACE)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case ICE: - break; - case ITEM_FRAME: - break; - case JUKEBOX: - if (DEBUG) - plugin.getLogger().info("DEBUG: Jukebox"); - case NOTE_BLOCK: - if (island == null) { - if (DEBUG) - plugin.getLogger().info("DEBUG: Jukebox island = null"); - if (Settings.defaultWorldSettings.get(SettingsFlag.MUSIC)) { - return; - } else { - if (DEBUG) - plugin.getLogger().info("DEBUG: Jukebox not allowed"); - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.MUSIC)) { - if (DEBUG) - plugin.getLogger().info("DEBUG: Jukebox not allowed"); - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case PACKED_ICE: - break; - case STONE_BUTTON: - case WOOD_BUTTON: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.BUTTON)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.LEVER)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case LEVER: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.LEVER)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.LEVER)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case TNT: - break; - case WORKBENCH: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.CRAFTING)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.CRAFTING)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case ANVIL: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.ANVIL)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.ANVIL)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case RAILS: - case POWERED_RAIL: - case DETECTOR_RAIL: - case ACTIVATOR_RAIL: - // If they are not on an island, it's protected - if (island == null) { - if (!Settings.defaultWorldSettings.get(SettingsFlag.PLACE_BLOCKS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - return; - } - if (!island.getFlag(SettingsFlag.PLACE_BLOCKS)) { - if (e.getMaterial() == Material.MINECART || e.getMaterial() == Material.STORAGE_MINECART || e.getMaterial() == Material.HOPPER_MINECART - || e.getMaterial() == Material.EXPLOSIVE_MINECART || e.getMaterial() == Material.POWERED_MINECART) { - e.setCancelled(true); - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.getPlayer().updateInventory(); - return; - } - } - case BEACON: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.BEACON)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.BEACON)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case CAKE_BLOCK: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.BREAK_BLOCKS)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.getPlayer().setFoodLevel(e.getPlayer().getFoodLevel() - 2); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.BREAK_BLOCKS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.getPlayer().setFoodLevel(e.getPlayer().getFoodLevel() - 2); - e.setCancelled(true); - return; - } - break; - case DRAGON_EGG: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.BREAK_BLOCKS)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.BREAK_BLOCKS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case MOB_SPAWNER: - if (island == null) { - if (Settings.defaultWorldSettings.get(SettingsFlag.BREAK_BLOCKS)) { - return; - } else { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - if (!island.getFlag(SettingsFlag.BREAK_BLOCKS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - break; - case BED_BLOCK: - if (e.getPlayer().getWorld().getEnvironment().equals(Environment.NETHER)) { - // Prevent explosions - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - default: - break; - } - } - // Check for disallowed in-hand items - if (DEBUG) { - plugin.getLogger().info("Material = " + e.getMaterial()); - plugin.getLogger().info("in hand = " + Util.getPlayerInHandItems(e.getPlayer())); - } - if (e.getMaterial() != null) { - // This check protects against an exploit in 1.7.9 against cactus - // and sugar cane - if (e.getMaterial() == Material.WOOD_DOOR || e.getMaterial() == Material.CHEST - || e.getMaterial() == Material.TRAPPED_CHEST || e.getMaterial() == Material.IRON_DOOR) { - if ((island == null && Settings.defaultWorldSettings.get(SettingsFlag.PLACE_BLOCKS)) - || (island !=null && !island.getFlag(SettingsFlag.PLACE_BLOCKS))) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - e.getPlayer().updateInventory(); - } - } else if (e.getMaterial().name().contains("BOAT") && (e.getClickedBlock() != null && !e.getClickedBlock().isLiquid())) { - // Trying to put a boat on non-liquid - if ((island == null && Settings.defaultWorldSettings.get(SettingsFlag.PLACE_BLOCKS)) - || (island !=null && !island.getFlag(SettingsFlag.PLACE_BLOCKS))) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } else if (e.getMaterial().equals(Material.ENDER_PEARL)) { - if ((island == null && Settings.defaultWorldSettings.get(SettingsFlag.THROW_ENDERPEARLS)) - || (island !=null && !island.getFlag(SettingsFlag.THROW_ENDERPEARLS))) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } else if (e.getMaterial().equals(Material.FLINT_AND_STEEL)) { - plugin.getLogger().info("DEBUG: flint & steel"); - if (e.getClickedBlock() != null) { - if (e.getMaterial().equals(Material.OBSIDIAN)) { - plugin.getLogger().info("DEBUG: flint & steel on obsidian"); - //return; - } - if (!actionAllowed(e.getPlayer(), e.getClickedBlock().getLocation(), SettingsFlag.FIRE)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } - } else if (e.getMaterial().equals(Material.MONSTER_EGG)) { - if (DEBUG) - plugin.getLogger().info("DEBUG: allowMonsterEggs = " + island.getFlag(SettingsFlag.SPAWN_EGGS)); - if (!actionAllowed(e.getPlayer(),e.getClickedBlock().getLocation(),SettingsFlag.SPAWN_EGGS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } else if (e.getMaterial().equals(Material.POTION) && e.getItem().getDurability() != 0) { - // Potion - // plugin.getLogger().info("DEBUG: potion"); - try { - Potion p = Potion.fromItemStack(e.getItem()); - if (p.isSplash()) { - // Splash potions are allowed only if PVP is allowed - boolean inNether = false; - if (e.getPlayer().getWorld().equals(IslandWorld.getNetherWorld())) { - inNether = true; - } - // Check PVP - if (island == null) { - if ((inNether && Settings.defaultWorldSettings.get(SettingsFlag.PVP_NETHER) - || (!inNether && Settings.defaultWorldSettings.get(SettingsFlag.PVP_OVERWORLD)))) { - return; - } - } else { - if ((inNether && island.getFlag(SettingsFlag.PVP_NETHER) || (!inNether && island.getFlag(SettingsFlag.PVP_OVERWORLD)))) { - return; - } - } - // Not allowed - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } catch (Exception ex) { - } - } - // Everything else is okay - } - } - - /** - * Handles hitting minecarts or feeding animals - * @param e - */ - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onPlayerHitEntity(PlayerInteractEntityEvent e) { - Player p = e.getPlayer(); - if (DEBUG) { - plugin.getLogger().info("Hit entity event " + e.getEventName()); - } - if (!Util.inWorld(p)) { - return; - } - if (p.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - // You can do anything if you are Op of have the bypass - return; - } - // Leashes are dealt with elsewhere - if (Util.playerIsHolding(p, Material.LEASH)) { - return; - } - Island island = plugin.getIslands().getProtectedIslandAt(e.getPlayer().getLocation()); - if (!plugin.getIslands().playerIsOnIsland(e.getPlayer())) { - // Not on island - // Minecarts and other storage entities - //plugin.getLogger().info("DEBUG: " + e.getRightClicked().getType().toString()); - //plugin.getLogger().info("DEBUG: " + p.getItemInHand()); - // Handle village trading - if (e.getRightClicked() != null && e.getRightClicked().getType().equals(EntityType.VILLAGER)) { - if (island != null) { - if (DEBUG) { - plugin.getLogger().info("DEBUG: island is not null"); - plugin.getLogger().info("DEBUG: island is not spawn"); - plugin.getLogger().info("DEBUG: villager trading is " + island.getFlag(SettingsFlag.VILLAGER_TRADING)); - } - if ((!island.getFlag(SettingsFlag.VILLAGER_TRADING) && !island.getMembers().contains(p.getUniqueId()))) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - } - // Handle name tags and dyes - if (Util.playerIsHolding(p, Material.NAME_TAG) || Util.playerIsHolding(p, Material.INK_SACK)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - e.getPlayer().updateInventory(); - return; - } - // Handle cookies (to animals) - if (Util.playerIsHolding(p, Material.COOKIE) && e.getRightClicked() instanceof Animals) { - if (island == null && !Settings.defaultWorldSettings.get(SettingsFlag.HURT_ANIMALS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - if (island != null) { - if ((!island.getFlag(SettingsFlag.HURT_ANIMALS) && !island.getMembers().contains(p.getUniqueId()))) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - } - // Handle breeding - if (e.getRightClicked() instanceof Animals) { - for (ItemStack item : Util.getPlayerInHandItems(p)) { - Material type = item.getType(); - if (type == Material.EGG || type == Material.WHEAT || type == Material.CARROT_ITEM || type == Material.SEEDS) { - if (island == null && !Settings.defaultWorldSettings.get(SettingsFlag.BREEDING)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - if (island != null) { - if ((!island.getFlag(SettingsFlag.BREEDING) && !island.getMembers().contains(p.getUniqueId()))) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - return; - } - } - } - } - } - switch (e.getRightClicked().getType()) { - case CREEPER: - // This seems to be called when the player is in Creative mode... - if (!Settings.allowCreeperGriefing) { - for (ItemStack item : Util.getPlayerInHandItems(e.getPlayer())) { - if (item != null && item.getType().equals(Material.FLINT_AND_STEEL)) { - if (!island.getMembers().contains(e.getPlayer().getUniqueId())) { - // Visitor - litCreeper.add(e.getRightClicked().getUniqueId()); - if (DEBUG) { - plugin.getLogger().info("DEBUG: visitor lit creeper"); - } - } - } - } - } - break; - case LLAMA: - case SKELETON_HORSE: - case ZOMBIE_HORSE: - case HORSE: - //plugin.getLogger().info("Horse riding"); - if (island == null && !Settings.defaultWorldSettings.get(SettingsFlag.MOUNT_RIDING)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - if (island != null && !island.getFlag(SettingsFlag.MOUNT_RIDING)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - break; - case ITEM_FRAME: - // This is to place items in an item frame - if (island == null && !Settings.defaultWorldSettings.get(SettingsFlag.PLACE_BLOCKS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - if (island != null) { - if (!island.getFlag(SettingsFlag.PLACE_BLOCKS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } - break; - case MINECART_CHEST: - case MINECART_FURNACE: - case MINECART_HOPPER: - //plugin.getLogger().info("Minecarts"); - if (island == null && !Settings.defaultWorldSettings.get(SettingsFlag.OPEN_CHESTS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - if (island != null) { - if (!island.getFlag(SettingsFlag.OPEN_CHESTS)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - } - break; - default: - break; - } - } - } - - /** - * Prevents fire spread - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onBlockBurn(BlockBurnEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (!Util.inWorld(e.getBlock())) { - //plugin.getLogger().info("DEBUG: Not in world"); - return; - } - if (actionAllowed(e.getBlock().getLocation(), SettingsFlag.FIRE_SPREAD)) { - return; - } - e.setCancelled(true); - } - - /** - * Prevent fire spread - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onBlockSpread(BlockSpreadEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - plugin.getLogger().info(e.getSource().getType().toString()); - } - if (e.getSource().getType() == Material.FIRE) { - if (!Util.inWorld(e.getBlock())) { - //plugin.getLogger().info("DEBUG: Not in world"); - return; - } - if (actionAllowed(e.getBlock().getLocation(), SettingsFlag.FIRE_SPREAD)) { - return; - } - e.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - public void onBlockIgnite(final BlockIgniteEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - plugin.getLogger().info(e.getCause().name()); - } - if (!Util.inWorld(e.getBlock())) { - //plugin.getLogger().info("DEBUG: Not in world"); - return; - } - // Check if this is a portal lighting - if (e.getBlock() != null && e.getBlock().getType().equals(Material.OBSIDIAN)) { - if (DEBUG) - plugin.getLogger().info("DEBUG: portal lighting"); - return; - } - if (e.getCause() != null) { - if (DEBUG) - plugin.getLogger().info("DEBUG: ignite cause = " + e.getCause()); - switch (e.getCause()) { - case ENDER_CRYSTAL: - case EXPLOSION: - case FIREBALL: - case LIGHTNING: - if (!actionAllowed(e.getBlock().getLocation(), SettingsFlag.FIRE)) { - if (DEBUG) - plugin.getLogger().info("DEBUG: canceling fire"); - e.setCancelled(true); - } - break; - case FLINT_AND_STEEL: - Set transparent = new HashSet<>(); - transparent.add(Material.AIR); - if (DEBUG) { - plugin.getLogger().info("DEBUG: block = " + e.getBlock()); - //plugin.getLogger().info("DEBUG: target block = " + e.getPlayer().getTargetBlock(transparent, 10)); - } - // Check if this is allowed - if (e.getPlayer() != null && (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypass"))) { - return; - } - if (!actionAllowed(e.getBlock().getLocation(), SettingsFlag.FIRE)) { - if (DEBUG) - plugin.getLogger().info("DEBUG: canceling fire"); - // If this was not a player, just stop it - if (e.getPlayer() == null) { - e.setCancelled(true); - break; - } - // Get target block - Block targetBlock = e.getPlayer().getTargetBlock(transparent, 10); - if (targetBlock.getType().equals(Material.OBSIDIAN)) { - final MaterialData md = new MaterialData(e.getBlock().getType(), e.getBlock().getData()); - plugin.getServer().getScheduler().runTask(plugin, () -> { - if (e.getBlock().getType().equals(Material.FIRE)) { - e.getBlock().setType(md.getItemType()); - e.getBlock().setData(md.getData()); - } - }); - } else { - e.setCancelled(true); - } - } - break; - - case LAVA: - case SPREAD: - // Check if this is a portal lighting - if (e.getBlock() != null && e.getBlock().getType().equals(Material.OBSIDIAN)) { - if (DEBUG) - plugin.getLogger().info("DEBUG: obsidian lighting"); - return; - } - if (!actionAllowed(e.getBlock().getLocation(), SettingsFlag.FIRE_SPREAD)) { - if (DEBUG) - plugin.getLogger().info("DEBUG: canceling fire spread"); - e.setCancelled(true); - } - break; - default: - break; - } - } - } - - - /** - * Pressure plates - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onPlateStep(PlayerInteractEvent e) { - if (DEBUG) { - plugin.getLogger().info("pressure plate = " + e.getEventName()); - plugin.getLogger().info("action = " + e.getAction()); - } - if (!Util.inWorld(e.getPlayer()) || !e.getAction().equals(Action.PHYSICAL) - || e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect") - || plugin.getIslands().playerIsOnIsland(e.getPlayer())) { - //plugin.getLogger().info("DEBUG: Not in world"); - return; - } - // Check island - Island island = plugin.getIslands().getProtectedIslandAt(e.getPlayer().getLocation()); - if ((island == null && Settings.defaultWorldSettings.get(SettingsFlag.PRESSURE_PLATE))) { - return; - } - if (island != null && island.getFlag(SettingsFlag.PRESSURE_PLATE)) { - return; - } - // Block action - UUID playerUUID = e.getPlayer().getUniqueId(); - if (!onPlate.containsKey(playerUUID)) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - Vector v = e.getPlayer().getLocation().toVector(); - onPlate.put(playerUUID, new Vector(v.getBlockX(), v.getBlockY(), v.getBlockZ())); - } - e.setCancelled(true); - } - - /** - * Removes the player from the plate map - * @param e - */ - @EventHandler(priority = EventPriority.LOW) - public void onStepOffPlate(PlayerMoveEvent e) { - if (!onPlate.containsKey(e.getPlayer().getUniqueId())) { - return; - } - Vector v = e.getPlayer().getLocation().toVector(); - if (!(new Vector(v.getBlockX(), v.getBlockY(), v.getBlockZ())).equals(onPlate.get(e.getPlayer().getUniqueId()))) { - onPlate.remove(e.getPlayer().getUniqueId()); - } - } - - @EventHandler(priority = EventPriority.LOW) - public void onPistonExtend(BlockPistonExtendEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - Location pistonLoc = e.getBlock().getLocation(); - if (Settings.allowPistonPush || !Util.inWorld(pistonLoc)) { - //plugin.getLogger().info("DEBUG: Not in world"); - return; - } - Island island = plugin.getIslands().getProtectedIslandAt(pistonLoc); - if (island == null || !island.onIsland(pistonLoc)) { - //plugin.getLogger().info("DEBUG: Not on is island protection zone"); - return; - } - // We need to check where the blocks are going to go, not where they are - for (Block b : e.getBlocks()) { - if (!island.onIsland(b.getRelative(e.getDirection()).getLocation())) { - //plugin.getLogger().info("DEBUG: Block is outside protected area"); - e.setCancelled(true); - return; - } - } - } - - /** - * Handle visitor chicken egg throwing - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onEggThrow(PlayerEggThrowEvent e) { - if (DEBUG) { - plugin.getLogger().info("egg throwing = " + e.getEventName()); - } - if (!Util.inWorld(e.getPlayer()) || e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect") - || plugin.getIslands().playerIsOnIsland(e.getPlayer()) || plugin.getIslands().isAtSpawn(e.getPlayer().getLocation())) { - return; - } - // Check island - Island island = plugin.getIslands().getProtectedIslandAt(e.getPlayer().getLocation()); - if (island == null) { - return; - } - if (!island.getFlag(SettingsFlag.THROW_EGGS)) { - e.setHatching(false); - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - //e.getPlayer().updateInventory(); - } - } - - /** - * Prevents trees from growing outside of the protected area. - * - * @param e - */ - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onTreeGrow(final StructureGrowEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - // Check world - if (!Util.inWorld(e.getLocation())) { - return; - } - // Check if this is on an island - Island island = plugin.getIslands().getIslandAt(e.getLocation()); - if (island == null || island.isSpawn()) { - return; - } - Iterator it = e.getBlocks().iterator(); - while (it.hasNext()) { - BlockState b = it.next(); - if (b.getType() == Material.LOG || b.getType() == Material.LOG_2 - || b.getType() == Material.LEAVES || b.getType() == Material.LEAVES_2) { - if (!island.onIsland(b.getLocation())) { - it.remove(); - } - } - } - } - - /** - * Trap TNT being primed by flaming arrows - * @param e - */ - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onTNTPrimed(final EntityChangeBlockEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - plugin.getLogger().info("DEBUG: block = " + e.getBlock().getType()); - plugin.getLogger().info("DEBUG: entity = " + e.getEntityType()); - plugin.getLogger().info("DEBUG: material changing to " + e.getTo()); - } - if (actionAllowed(e.getEntity().getLocation(), SettingsFlag.FIRE)) { - return; - } - if (e.getBlock() == null) { - return; - } - // Check for TNT - if (!e.getBlock().getType().equals(Material.TNT)) { - //plugin.getLogger().info("DEBUG: not tnt"); - return; - } - // Check world - if (!Util.inWorld(e.getBlock())) { - return; - } - // Check if this is on an island - Island island = plugin.getIslands().getIslandAt(e.getBlock().getLocation()); - if (island == null || island.isSpawn()) { - return; - } - // Stop TNT from being damaged if it is being caused by a visitor with a flaming arrow - if (e.getEntity() instanceof Projectile) { - //plugin.getLogger().info("DEBUG: projectile"); - Projectile projectile = (Projectile) e.getEntity(); - // Find out who fired it - if (projectile.getShooter() instanceof Player) { - //plugin.getLogger().info("DEBUG: player shot arrow. Fire ticks = " + projectile.getFireTicks()); - if (projectile.getFireTicks() > 0) { - //plugin.getLogger().info("DEBUG: arrow on fire"); - Player shooter = (Player)projectile.getShooter(); - if (!plugin.getIslands().locationIsAtHome(shooter, true, e.getBlock().getLocation())) { - //plugin.getLogger().info("DEBUG: shooter is not at home"); - // Only say it once a second - // Debounce event (it can be called twice for the same action) - if (!tntBlocks.contains(e.getBlock().getLocation())) { - shooter.sendMessage(plugin.getLocale(shooter).get("island.protected")); - tntBlocks.add(e.getBlock().getLocation()); - plugin.getServer().getScheduler().runTaskLater(plugin, () -> tntBlocks.remove(e.getBlock().getLocation()), 20L); - } - // Remove the arrow - projectile.remove(); - e.setCancelled(true); - } - } - } - } - } - - @EventHandler(priority=EventPriority.LOW) - public void onEvent(BlockPistonExtendEvent event) { - if (!Settings.allowTNTPushing) { - // Check world - if (!Util.inWorld(event.getBlock())) return; - - if (event.getBlocks().stream().anyMatch(it -> it.getType() == Material.TNT)) - event.setCancelled(true); - } - } - - @EventHandler(priority=EventPriority.LOW) - public void onEvent(BlockPistonRetractEvent event) { - if (!Settings.allowTNTPushing) { - // Check world - if (!Util.inWorld(event.getBlock())) return; - - if (event.getBlocks().stream().anyMatch(it -> it.getType() == Material.TNT)) - event.setCancelled(true); - } - } - - /** - * Checks for splash damage. If there is any to any affected entity and it's not allowed, it won't work on any of them. - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onSplashPotionSplash(final PotionSplashEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - plugin.getLogger().info("splash entity = " + e.getEntity()); - plugin.getLogger().info("splash entity type = " + e.getEntityType()); - plugin.getLogger().info("splash affected entities = " + e.getAffectedEntities()); - //plugin.getLogger().info("splash hit entity = " + e.getHitEntity()); - } - if (!Util.inWorld(e.getEntity().getLocation())) { - return; - } - // Try to get the shooter - Projectile projectile = (Projectile) e.getEntity(); - if (DEBUG) - plugin.getLogger().info("splash shooter = " + projectile.getShooter()); - if (projectile.getShooter() != null && projectile.getShooter() instanceof Player) { - Player attacker = (Player)projectile.getShooter(); - // Run through all the affected entities - for (LivingEntity entity: e.getAffectedEntities()) { - if (DEBUG) - plugin.getLogger().info("DEBUG: affected splash entity = " + entity); - // Self damage - if (attacker.equals(entity)) { - if (DEBUG) - plugin.getLogger().info("DEBUG: Self damage from splash potion!"); - continue; - } - Island island = plugin.getIslands().getIslandAt(entity.getLocation()); - boolean inNether = false; - if (entity.getWorld().equals(IslandWorld.getNetherWorld())) { - inNether = true; - } - // Monsters being hurt - if (entity instanceof Monster || entity instanceof Slime || entity instanceof Squid) { - // Normal island check - if (island != null && island.getMembers().contains(attacker.getUniqueId())) { - // Members always allowed - continue; - } - if (actionAllowed(attacker, entity.getLocation(), SettingsFlag.HURT_MONSTERS)) { - continue; - } - // Not allowed - attacker.sendMessage(plugin.getLocale(attacker).get("island.protected")); - e.setCancelled(true); - return; - } - - // Mobs being hurt - if (entity instanceof Animals || entity instanceof IronGolem || entity instanceof Snowman - || entity instanceof Villager) { - if (island != null && (island.getFlag(SettingsFlag.HURT_ANIMALS) || island.getMembers().contains(attacker.getUniqueId()))) { - continue; - } - if (DEBUG) - plugin.getLogger().info("DEBUG: Mobs not allowed to be hurt. Blocking"); - attacker.sendMessage(plugin.getLocale(attacker).get("island.protected")); - e.setCancelled(true); - return; - } - - // Establish whether PVP is allowed or not. - boolean pvp = false; - if ((inNether && island != null && island.getFlag(SettingsFlag.PVP_NETHER) || (!inNether && island != null && island.getFlag(SettingsFlag.PVP_OVERWORLD)))) { - if (DEBUG) plugin.getLogger().info("DEBUG: PVP allowed"); - pvp = true; - } - - // Players being hurt PvP - if (entity instanceof Player) { - if (!pvp) { - if (DEBUG) plugin.getLogger().info("DEBUG: PVP not allowed"); - attacker.sendMessage(plugin.getLocale(attacker).get("targetInPVPArea")); - e.setCancelled(true); - return; - } - } - } - } - } -} diff --git a/src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard1_8.java b/src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard1_8.java deleted file mode 100644 index d76717d6d..000000000 --- a/src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard1_8.java +++ /dev/null @@ -1,206 +0,0 @@ -package us.tastybento.bskyblock.listeners.protection; - -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.player.PlayerInteractAtEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.ItemStack; - -import us.tastybento.bskyblock.BSkyBlock; -import us.tastybento.bskyblock.config.Settings; -import us.tastybento.bskyblock.database.objects.Island; -import us.tastybento.bskyblock.database.objects.Island.SettingsFlag; -import us.tastybento.bskyblock.util.Util; - -/** - * @author tastybento - * Provides protection to islands - handles newer events that may not - * exist in older servers - */ -public class IslandGuard1_8 implements Listener { - private final BSkyBlock plugin; - private final static boolean DEBUG = false; - - public IslandGuard1_8(final BSkyBlock plugin) { - this.plugin = plugin; - - } - - /** - * Checks if action is allowed for player in location for flag - * @param player - * @param location - * @param flag - * @return true if allowed - */ - private boolean actionAllowed(Player player, Location location, SettingsFlag flag) { - // This permission bypasses protection - if (player.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return true; - } - Island island = plugin.getIslands().getProtectedIslandAt(location); - if (island != null && (island.getFlag(flag) || island.getMembers().contains(player.getUniqueId()))){ - return true; - } - if (island == null && Settings.defaultWorldSettings.get(flag)) { - return true; - } - return false; - } - - - /** - * Handle interaction with armor stands V1.8 - * Note - some armor stand protection is done in IslandGuard.java, e.g. against projectiles. - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onPlayerInteract(final PlayerInteractAtEntityEvent e) { - if (DEBUG) { - plugin.getLogger().info("1.8 " + e.getEventName()); - } - if (!Util.inWorld(e.getPlayer())) { - return; - } - if (e.getRightClicked() != null && e.getRightClicked().getType().equals(EntityType.ARMOR_STAND)) { - if (actionAllowed(e.getPlayer(), e.getRightClicked().getLocation(), SettingsFlag.ARMOR_STAND)) { - return; - } - e.setCancelled(true); - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer().getUniqueId()).get("island.protected")); - } - } - - /** - * Handle V1.8 blocks that need special treatment - * Tilling of coarse dirt into dirt - * Usually prevented because it could lead to an endless supply of dirt with gravel - * - * @param e - */ - @SuppressWarnings("deprecation") - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onPlayerInteract(final PlayerInteractEvent e) { - if (DEBUG) { - plugin.getLogger().info("1.8 " + e.getEventName()); - } - if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { - return; - } - if (!Util.inWorld(e.getPlayer())) { - return; - } - // This permission bypasses protection - if (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - // Prevents tilling of coarse dirt into dirt - ItemStack inHand = e.getPlayer().getItemInHand(); - if (inHand.getType() == Material.WOOD_HOE || inHand.getType() == Material.IRON_HOE || inHand.getType() == Material.GOLD_HOE - || inHand.getType() == Material.DIAMOND_HOE || inHand.getType() == Material.STONE_HOE) { - // plugin.getLogger().info("1.8 " + "DEBUG: hoe in hand"); - Block block = e.getClickedBlock(); - // plugin.getLogger().info("1.8 " + "DEBUG: block is " + block.getType() + - // ":" + block.getData()); - // Check if coarse dirt - if (block.getType() == Material.DIRT && block.getData() == (byte) 1) { - // plugin.getLogger().info("1.8 " + "DEBUG: hitting coarse dirt!"); - e.setCancelled(true); - } - } - } - - // Armor stand events - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled=true) - void placeArmorStandEvent(PlayerInteractEvent e) { - Player p = e.getPlayer(); - if (DEBUG) { - plugin.getLogger().info("1.8 " + "Armor stand place " + e.getEventName()); - } - if (!Util.inWorld(p)) { - return; - } - if (p.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - // You can do anything if you are Op - return; - } - - // Check if they are holding armor stand - for (ItemStack inHand : Util.getPlayerInHandItems(e.getPlayer())) { - if (inHand.getType().equals(Material.ARMOR_STAND)) { - // Check island - Island island = plugin.getIslands().getIslandAt(e.getPlayer().getLocation()); - if (island == null && Settings.defaultWorldSettings.get(SettingsFlag.PLACE_BLOCKS)) { - return; - } - if (island !=null && (island.getMembers().contains(p.getUniqueId()) || island.getFlag(SettingsFlag.PLACE_BLOCKS))) { - //plugin.getLogger().info("1.8 " + "DEBUG: armor stand place check"); - if (Settings.limitedBlocks.containsKey("ARMOR_STAND") && Settings.limitedBlocks.get("ARMOR_STAND") > -1) { - //plugin.getLogger().info("1.8 " + "DEBUG: count armor stands"); - int count = island.getTileEntityCount(Material.ARMOR_STAND,e.getPlayer().getWorld()); - //plugin.getLogger().info("1.8 " + "DEBUG: count is " + count + " limit is " + Settings.limitedBlocks.get("ARMOR_STAND")); - if (Settings.limitedBlocks.get("ARMOR_STAND") <= count) { - e.getPlayer().sendMessage((plugin.getLocale(e.getPlayer().getUniqueId()).get("moblimits.entity").replace("[entity]", - Util.prettifyText(Material.ARMOR_STAND.toString()))).replace("[number]", String.valueOf(Settings.limitedBlocks.get("ARMOR_STAND")))); - e.setCancelled(true); - return; - } - } - return; - } - // plugin.getLogger().info("1.8 " + "DEBUG: stand place cancelled"); - e.setCancelled(true); - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer().getUniqueId()).get("island.protected")); - e.getPlayer().updateInventory(); - } - } - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void ArmorStandDestroy(EntityDamageByEntityEvent e) { - if (DEBUG) { - plugin.getLogger().info("1.8 " + "IslandGuard New " + e.getEventName()); - } - if (!(e.getEntity() instanceof LivingEntity)) { - return; - } - if (!Util.inWorld(e.getEntity())) { - return; - } - final LivingEntity livingEntity = (LivingEntity) e.getEntity(); - if (!livingEntity.getType().equals(EntityType.ARMOR_STAND)) { - return; - } - if (e.getDamager() instanceof Player) { - Player p = (Player) e.getDamager(); - if (p.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - // Check if on island - if (plugin.getIslands().playerIsOnIsland(p)) { - return; - } - // Check island - Island island = plugin.getIslands().getIslandAt(e.getEntity().getLocation()); - if (island == null && Settings.defaultWorldSettings.get(SettingsFlag.BREAK_BLOCKS)) { - return; - } - if (island != null && island.getFlag(SettingsFlag.BREAK_BLOCKS)) { - return; - } - p.sendMessage(plugin.getLocale(p.getUniqueId()).get("island.protected")); - e.setCancelled(true); - } - } - -} \ No newline at end of file diff --git a/src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard1_9.java b/src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard1_9.java deleted file mode 100644 index f13a99ddf..000000000 --- a/src/main/java/us/tastybento/bskyblock/listeners/protection/IslandGuard1_9.java +++ /dev/null @@ -1,474 +0,0 @@ -package us.tastybento.bskyblock.listeners.protection; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; - -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.entity.Animals; -import org.bukkit.entity.EnderCrystal; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.IronGolem; -import org.bukkit.entity.Monster; -import org.bukkit.entity.Player; -import org.bukkit.entity.Projectile; -import org.bukkit.entity.Slime; -import org.bukkit.entity.Snowman; -import org.bukkit.entity.Squid; -import org.bukkit.entity.Villager; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.block.EntityBlockFormEvent; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDamageEvent.DamageCause; -import org.bukkit.event.entity.EntityExplodeEvent; -import org.bukkit.event.entity.LingeringPotionSplashEvent; -import org.bukkit.event.player.PlayerInteractAtEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.projectiles.ProjectileSource; - -import us.tastybento.bskyblock.BSkyBlock; -import us.tastybento.bskyblock.config.Settings; -import us.tastybento.bskyblock.database.objects.Island; -import us.tastybento.bskyblock.database.objects.Island.SettingsFlag; -import us.tastybento.bskyblock.generators.IslandWorld; -import us.tastybento.bskyblock.util.Util; - -/** - * @author tastybento - * Provides protection to islands - handles newer events that may not - * exist in older servers - */ -public class IslandGuard1_9 implements Listener { - private final BSkyBlock plugin; - private final static boolean DEBUG = false; - private HashMap thrownPotions; - - public IslandGuard1_9(final BSkyBlock plugin) { - this.plugin = plugin; - this.thrownPotions = new HashMap<>(); - } - - /** - * Handles Frost Walking on visitor's islands - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onBlockForm(EntityBlockFormEvent e) { - if (DEBUG) { - plugin.getLogger().info("1.9 " +e.getEventName()); - } - if (e.getEntity() instanceof Player && e.getNewState().getType().equals(Material.FROSTED_ICE)) { - Player player= (Player) e.getEntity(); - if (!Util.inWorld(player)) { - return; - } - // This permission bypasses protection - if (player.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - // Check island - Island island = plugin.getIslands().getIslandAt(player.getLocation()); - if (island == null && Settings.defaultWorldSettings.get(SettingsFlag.PLACE_BLOCKS)) { - return; - } - if (island !=null) { - if (island.getMembers().contains(player.getUniqueId()) || island.getFlag(SettingsFlag.PLACE_BLOCKS)) { - return; - } - } - // Silently cancel the event - e.setCancelled(true); - } - } - - - /** - * Handle interaction with end crystals 1.9 - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onHitEndCrystal(final PlayerInteractAtEntityEvent e) { - if (DEBUG) { - plugin.getLogger().info("1.9 " +e.getEventName()); - } - if (!Util.inWorld(e.getPlayer())) { - return; - } - // This permission bypasses protection - if (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - if (e.getRightClicked() != null && e.getRightClicked().getType().equals(EntityType.ENDER_CRYSTAL)) { - // Check island - Island island = plugin.getIslands().getIslandAt(e.getRightClicked().getLocation()); - if (island == null && Settings.defaultWorldSettings.get(SettingsFlag.BREAK_BLOCKS)) { - return; - } - if (island !=null) { - if (island.getMembers().contains(e.getPlayer().getUniqueId()) || island.getFlag(SettingsFlag.BREAK_BLOCKS)) { - return; - } - } - e.setCancelled(true); - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - } - } - - // End crystal - @SuppressWarnings("deprecation") - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled=true) - void placeEndCrystalEvent(PlayerInteractEvent e) { - Player p = e.getPlayer(); - if (DEBUG) { - plugin.getLogger().info("1.9 " +"End crystal place " + e.getEventName()); - } - if (!Util.inWorld(p)) { - return; - } - if (p.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - // You can do anything if you are Op - return; - } - - // Check if they are holding armor stand - for (ItemStack inHand : Util.getPlayerInHandItems(e.getPlayer())) { - if (inHand.getType().equals(Material.END_CRYSTAL)) { - // Check island - Island island = plugin.getIslands().getIslandAt(e.getPlayer().getLocation()); - if (island == null && Settings.defaultWorldSettings.get(SettingsFlag.PLACE_BLOCKS)) { - return; - } - if (island !=null && (island.getMembers().contains(p.getUniqueId()) || island.getFlag(SettingsFlag.PLACE_BLOCKS))) { - //plugin.getLogger().info("1.9 " +"DEBUG: armor stand place check"); - if (Settings.limitedBlocks.containsKey("END_CRYSTAL") && Settings.limitedBlocks.get("END_CRYSTAL") > -1) { - //plugin.getLogger().info("1.9 " +"DEBUG: count armor stands"); - int count = island.getTileEntityCount(Material.END_CRYSTAL,e.getPlayer().getWorld()); - //plugin.getLogger().info("1.9 " +"DEBUG: count is " + count + " limit is " + Settings.limitedBlocks.get("ARMOR_STAND")); - if (Settings.limitedBlocks.get("END_CRYSTAL") <= count) { - e.getPlayer().sendMessage((plugin.getLocale(e.getPlayer().getUniqueId()).get("moblimits.entity").replace("[entity]", - Util.prettifyText(Material.END_CRYSTAL.toString()))).replace("[number]", String.valueOf(Settings.limitedBlocks.get("END_CRYSTAL")))); - e.setCancelled(true); - return; - } - } - return; - } - // plugin.getLogger().info("1.9 " +"DEBUG: stand place cancelled"); - e.setCancelled(true); - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.getPlayer().updateInventory(); - } - } - - } - - /** - * Handle end crystal damage by visitors - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void EndCrystalDamage(EntityDamageByEntityEvent e) { - if (DEBUG) { - plugin.getLogger().info("1.9 " +"IslandGuard 1_9 " + e.getEventName()); - plugin.getLogger().info("1.9 " +"Entity is " + e.getEntityType()); - } - if (e.getEntity() == null || !Util.inWorld(e.getEntity())) { - return; - } - if (!(e.getEntity() instanceof EnderCrystal)) { - if (DEBUG) { - plugin.getLogger().info("1.9 Entity is not End crystal it is " + e.getEntityType()); - } - return; - } - if (DEBUG) { - plugin.getLogger().info("1.9 Damager is " + e.getDamager()); - } - Player p = null; - if (e.getDamager() instanceof Player) { - p = (Player) e.getDamager(); - if (DEBUG) { - plugin.getLogger().info("1.9 Damager is a player"); - } - } else if (e.getDamager() instanceof Projectile) { - // Get the shooter - Projectile projectile = (Projectile)e.getDamager(); - ProjectileSource shooter = projectile.getShooter(); - if (shooter instanceof Player) { - p = (Player)shooter; - } - if (DEBUG) { - plugin.getLogger().info("1.9 " +"Damager is a projectile shot by " + p.getName()); - } - } - if (p != null) { - if (p.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - if (DEBUG) { - plugin.getLogger().info("1.9 " +"Bypassing protection"); - } - return; - } - // Check if on island - if (plugin.getIslands().playerIsOnIsland(p)) { - if (DEBUG) { - plugin.getLogger().info("1.9 " +"Player is on their own island"); - } - return; - } - // Check island - Island island = plugin.getIslands().getIslandAt(e.getEntity().getLocation()); - if (island == null && Settings.defaultWorldSettings.get(SettingsFlag.BREAK_BLOCKS)) { - return; - } - if (island != null && island.getFlag(SettingsFlag.BREAK_BLOCKS)) { - if (DEBUG) { - plugin.getLogger().info("1.9 " +"Visitor is allowed to break blocks"); - } - return; - } - p.sendMessage(plugin.getLocale(p).get("island.protected")); - e.setCancelled(true); - } - - } - - /** - * Handles end crystal explosions - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onExplosion(final EntityExplodeEvent e) { - if (DEBUG) { - plugin.getLogger().info("1.9 " +e.getEventName()); - plugin.getLogger().info("1.9 " +"Entity exploding is " + e.getEntity()); - } - if (e.getEntity() == null || !e.getEntityType().equals(EntityType.ENDER_CRYSTAL)) { - if (DEBUG) { - plugin.getLogger().info("1.9 " +"Entity is not an END CRYSTAL"); - } - return; - } - - if (!Util.inWorld(e.getLocation())) { - return; - } - // General settings irrespective of whether this is allowed or not - if (!Settings.allowTNTDamage) { - plugin.getLogger().info("1.9 " +"TNT block damage prevented"); - e.blockList().clear(); - } else { - if (!Settings.allowChestDamage) { - List toberemoved = new ArrayList<>(); - // Save the chest blocks in a list - for (Block b : e.blockList()) { - switch (b.getType()) { - case CHEST: - case ENDER_CHEST: - case STORAGE_MINECART: - case TRAPPED_CHEST: - toberemoved.add(b); - break; - default: - break; - } - } - // Now delete them - for (Block b : toberemoved) { - e.blockList().remove(b); - } - } - } - // prevent at spawn - if (plugin.getIslands().isAtSpawn(e.getLocation())) { - e.blockList().clear(); - e.setCancelled(true); - } - - } - - /** - * Handle blocks that need special treatment - * Tilling of coarse dirt into dirt using off-hand (regular hand is in 1.8) - * Usually prevented because it could lead to an endless supply of dirt with gravel - * - * @param e - */ - @SuppressWarnings("deprecation") - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onPlayerInteract(final PlayerInteractEvent e) { - if (DEBUG) { - plugin.getLogger().info("1.9 " + e.getEventName()); - } - if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { - return; - } - if (!Util.inWorld(e.getPlayer())) { - return; - } - // This permission bypasses protection - if (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - // Prevents tilling of coarse dirt into dirt - ItemStack inHand = e.getPlayer().getInventory().getItemInOffHand(); - if (inHand.getType() == Material.WOOD_HOE || inHand.getType() == Material.IRON_HOE || inHand.getType() == Material.GOLD_HOE - || inHand.getType() == Material.DIAMOND_HOE || inHand.getType() == Material.STONE_HOE) { - // plugin.getLogger().info("1.8 " + "DEBUG: hoe in hand"); - Block block = e.getClickedBlock(); - // plugin.getLogger().info("1.8 " + "DEBUG: block is " + block.getType() + - // ":" + block.getData()); - // Check if coarse dirt - if (block.getType() == Material.DIRT && block.getData() == (byte) 1) { - // plugin.getLogger().info("1.8 " + "DEBUG: hitting coarse dirt!"); - e.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onLingeringPotionSplash(final LingeringPotionSplashEvent e) { - if (DEBUG) { - plugin.getLogger().info("1.9 " + e.getEventName()); - plugin.getLogger().info("1.9 entity = " + e.getEntity()); - plugin.getLogger().info("1.9 entity type = " + e.getEntityType()); - plugin.getLogger().info("1.9 radius = " + e.getAreaEffectCloud().getRadius()); - plugin.getLogger().info("1.9 id = " + e.getAreaEffectCloud().getEntityId()); - plugin.getLogger().info("1.9 hit entity = " + e.getHitEntity()); - } - if (!Util.inWorld(e.getEntity().getLocation())) { - return; - } - // Try to get the shooter - Projectile projectile = e.getEntity(); - plugin.getLogger().info("shooter = " + projectile.getShooter()); - if (projectile.getShooter() != null && projectile.getShooter() instanceof Player) { - UUID uuid = ((Player)projectile.getShooter()).getUniqueId(); - // Store it and remove it when the effect is gone - thrownPotions.put(e.getAreaEffectCloud().getEntityId(), uuid); - plugin.getServer().getScheduler().runTaskLater(plugin, () -> thrownPotions.remove(e.getAreaEffectCloud().getEntityId()), e.getAreaEffectCloud().getDuration()); - } - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onLingeringPotionDamage(final EntityDamageByEntityEvent e) { - if (DEBUG) { - plugin.getLogger().info("1.9 lingering potion damage " + e.getEventName()); - plugin.getLogger().info("1.9 lingering potion entity = " + e.getEntity()); - plugin.getLogger().info("1.9 lingering potion entity type = " + e.getEntityType()); - plugin.getLogger().info("1.9 lingering potion cause = " + e.getCause()); - plugin.getLogger().info("1.9 lingering potion damager = " + e.getDamager()); - } - if (!Util.inWorld(e.getEntity().getLocation())) { - return; - } - if (e.getEntity() == null || e.getEntity().getUniqueId() == null) { - return; - } - if (e.getCause().equals(DamageCause.ENTITY_ATTACK) && thrownPotions.containsKey(e.getDamager().getEntityId())) { - UUID attacker = thrownPotions.get(e.getDamager().getEntityId()); - // Self damage - if (attacker.equals(e.getEntity().getUniqueId())) { - if (DEBUG) - plugin.getLogger().info("DEBUG: Self damage from lingering potion!"); - return; - } - Island island = plugin.getIslands().getIslandAt(e.getEntity().getLocation()); - boolean inNether = false; - if (e.getEntity().getWorld().equals(IslandWorld.getNetherWorld())) { - inNether = true; - } - // Monsters being hurt - if (e.getEntity() instanceof Monster || e.getEntity() instanceof Slime || e.getEntity() instanceof Squid) { - // Normal island check - if (island != null && island.getMembers().contains(attacker)) { - // Members always allowed - return; - } - if (actionAllowed(attacker, e.getEntity().getLocation(), SettingsFlag.HURT_MONSTERS)) { - return; - } - // Not allowed - e.setCancelled(true); - return; - } - - // Mobs being hurt - if (e.getEntity() instanceof Animals || e.getEntity() instanceof IronGolem || e.getEntity() instanceof Snowman - || e.getEntity() instanceof Villager) { - if (island != null && (island.getFlag(SettingsFlag.HURT_ANIMALS) || island.getMembers().contains(attacker))) { - return; - } - if (DEBUG) - plugin.getLogger().info("DEBUG: Mobs not allowed to be hurt. Blocking"); - e.setCancelled(true); - return; - } - - // Establish whether PVP is allowed or not. - boolean pvp = false; - if ((inNether && island != null && island.getFlag(SettingsFlag.PVP_NETHER) || (!inNether && island != null && island.getFlag(SettingsFlag.PVP_OVERWORLD)))) { - if (DEBUG) plugin.getLogger().info("DEBUG: PVP allowed"); - pvp = true; - } - - // Players being hurt PvP - if (e.getEntity() instanceof Player) { - if (!pvp) { - if (DEBUG) plugin.getLogger().info("DEBUG: PVP not allowed"); - e.setCancelled(true); - } - } - } - } - - /** - * Checks if action is allowed for player in location for flag - * @param uuid - * @param location - * @param flag - * @return true if allowed - */ - private boolean actionAllowed(UUID uuid, Location location, SettingsFlag flag) { - Player player = plugin.getServer().getPlayer(uuid); - if (player == null) { - return actionAllowed(location, flag); - } - // This permission bypasses protection - if (player.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return true; - } - Island island = plugin.getIslands().getProtectedIslandAt(location); - if (island != null && (island.getFlag(flag) || island.getMembers().contains(player.getUniqueId()))){ - return true; - } - if (island == null && Settings.defaultWorldSettings.get(flag)) { - return true; - } - return false; - } - - /** - * Action allowed in this location - * @param location - * @param flag - * @return true if allowed - */ - private boolean actionAllowed(Location location, SettingsFlag flag) { - Island island = plugin.getIslands().getProtectedIslandAt(location); - if (island != null && island.getFlag(flag)){ - return true; - } - if (island == null && Settings.defaultWorldSettings.get(flag)) { - return true; - } - return false; - } -} \ No newline at end of file diff --git a/src/main/java/us/tastybento/bskyblock/listeners/protection/NetherEvents.java b/src/main/java/us/tastybento/bskyblock/listeners/protection/NetherEvents.java deleted file mode 100644 index 4c2c0a835..000000000 --- a/src/main/java/us/tastybento/bskyblock/listeners/protection/NetherEvents.java +++ /dev/null @@ -1,226 +0,0 @@ -package us.tastybento.bskyblock.listeners.protection; - -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World.Environment; -import org.bukkit.block.BlockState; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Player; -import org.bukkit.entity.Vehicle; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockPlaceEvent; -import org.bukkit.event.entity.EntityExplodeEvent; -import org.bukkit.event.entity.EntityPortalEvent; -import org.bukkit.event.player.PlayerBucketEmptyEvent; -import org.bukkit.event.world.StructureGrowEvent; -import org.bukkit.util.Vector; - -import us.tastybento.bskyblock.BSkyBlock; -import us.tastybento.bskyblock.config.Settings; -import us.tastybento.bskyblock.generators.IslandWorld; -import us.tastybento.bskyblock.util.SafeSpotTeleport; - -public class NetherEvents implements Listener { - private final BSkyBlock plugin; - private final static boolean DEBUG = false; - - public NetherEvents(BSkyBlock plugin) { - this.plugin = plugin; - } - - /** - * This handles non-player portal use - * Currently disables portal use by entities - * - * @param event - */ - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onEntityPortal(EntityPortalEvent event) { - if (DEBUG) - plugin.getLogger().info("DEBUG: nether portal entity " + event.getFrom().getBlock().getType()); - // If the nether is disabled then quit immediately - if (!Settings.netherGenerate || IslandWorld.getNetherWorld() == null) { - return; - } - if (event.getEntity() == null) { - return; - } - if (event.getFrom() != null && event.getFrom().getBlock().getType().equals(Material.ENDER_PORTAL)) { - event.setCancelled(true); - // Same action for all worlds except the end itself - if (!event.getFrom().getWorld().getEnvironment().equals(Environment.THE_END)) { - if (plugin.getServer().getWorld(Settings.worldName + "_the_end") != null) { - // The end exists - Location end_place = plugin.getServer().getWorld(Settings.worldName + "_the_end").getSpawnLocation(); - event.getEntity().teleport(end_place); - if (DEBUG) - plugin.getLogger().info("DEBUG: Result teleported " + event.getEntityType() + " to " + end_place); - return; - } - } - return; - } - Location currentLocation = event.getFrom().clone(); - String currentWorld = currentLocation.getWorld().getName(); - // Only operate if this is Island territory - if (!currentWorld.equalsIgnoreCase(Settings.worldName) && !currentWorld.equalsIgnoreCase(Settings.worldName + "_nether")) { - return; - } - // No entities may pass with the old nether - if (!Settings.netherIslands) { - event.setCancelled(true); - return; - } - // New nether - // Entities can pass only if there are adjoining portals - Location dest = event.getFrom().toVector().toLocation(IslandWorld.getIslandWorld()); - if (event.getFrom().getWorld().getEnvironment().equals(Environment.NORMAL)) { - dest = event.getFrom().toVector().toLocation(IslandWorld.getNetherWorld()); - } - // Vehicles - if (event.getEntity() instanceof Vehicle) { - Vehicle vehicle = (Vehicle)event.getEntity(); - vehicle.eject(); - } - new SafeSpotTeleport(plugin, event.getEntity(), dest); - event.setCancelled(true); - } - - // Nether portal spawn protection - - /** - * Function to check proximity to nether spawn location - * - * @param player - * @return true if in the spawn area, false if not - */ - private boolean awayFromSpawn(Player player) { - Vector p = player.getLocation().toVector().multiply(new Vector(1, 0, 1)); - Vector spawn = player.getWorld().getSpawnLocation().toVector().multiply(new Vector(1, 0, 1)); - return spawn.distanceSquared(p) < (Settings.netherSpawnRadius * Settings.netherSpawnRadius); - } - - /** - * Prevents blocks from being broken - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW) - public void onBlockBreak(final BlockBreakEvent e) { - if (DEBUG) - plugin.getLogger().info("DEBUG: " + e.getEventName()); - // plugin.getLogger().info("Block break"); - if ((e.getPlayer().getWorld().getName().equalsIgnoreCase(Settings.worldName + "_nether") && !Settings.netherIslands) - || e.getPlayer().getWorld().getName().equalsIgnoreCase(Settings.worldName + "_the_end")) { - if (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - if (DEBUG) - plugin.getLogger().info("Block break in island nether"); - if (!awayFromSpawn(e.getPlayer()) && !e.getPlayer().isOp()) { - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("nether.spawnisprotected")); - e.setCancelled(true); - } - } - - } - - /** - * Prevents placing of blocks - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW) - public void onPlayerBlockPlace(final BlockPlaceEvent e) { - if (DEBUG) - plugin.getLogger().info("DEBUG: " + e.getEventName()); - if (!Settings.netherIslands) { - if (e.getPlayer().getWorld().getName().equalsIgnoreCase(Settings.worldName + "_nether") - || e.getPlayer().getWorld().getName().equalsIgnoreCase(Settings.worldName + "_the_end")) { - if (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - if (!awayFromSpawn(e.getPlayer()) && !e.getPlayer().isOp()) { - e.setCancelled(true); - } - } - } - } - - @EventHandler(priority = EventPriority.LOW) - public void onBucketEmpty(final PlayerBucketEmptyEvent e) { - if (DEBUG) - plugin.getLogger().info("DEBUG: " + e.getEventName()); - if (!Settings.netherIslands) { - if (e.getPlayer().getWorld().getName().equalsIgnoreCase(Settings.worldName + "_nether") - || e.getPlayer().getWorld().getName().equalsIgnoreCase(Settings.worldName + "_the_end")) { - if (e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect")) { - return; - } - if (!awayFromSpawn(e.getPlayer()) && !e.getPlayer().isOp()) { - e.setCancelled(true); - } - } - } - } - - /** - * Prevent the Nether spawn from being blown up - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onExplosion(final EntityExplodeEvent e) { - if (Settings.netherIslands) { - // Not used in the new nether - return; - } - // Find out what is exploding - Entity expl = e.getEntity(); - if (expl == null) { - return; - } - // Check world - if (!e.getEntity().getWorld().getName().equalsIgnoreCase(Settings.worldName + "_nether") - || e.getEntity().getWorld().getName().equalsIgnoreCase(Settings.worldName + "_the_end")) { - return; - } - Location spawn = e.getLocation().getWorld().getSpawnLocation(); - Location loc = e.getLocation(); - if (spawn.distance(loc) < Settings.netherSpawnRadius) { - e.blockList().clear(); - } - } - - /** - * Converts trees to gravel and glowstone - * - * @param e - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onTreeGrow(final StructureGrowEvent e) { - if (DEBUG) - plugin.getLogger().info("DEBUG: " + e.getEventName()); - - if (!Settings.netherTrees) { - return; - } - if (!Settings.netherGenerate || IslandWorld.getNetherWorld() == null) { - return; - } - // Check world - if (!e.getLocation().getWorld().equals(IslandWorld.getNetherWorld())) { - return; - } - for (BlockState b : e.getBlocks()) { - if (b.getType() == Material.LOG || b.getType() == Material.LOG_2) { - b.setType(Material.GRAVEL); - } else if (b.getType() == Material.LEAVES || b.getType() == Material.LEAVES_2) { - b.setType(Material.GLOWSTONE); - } - } - } -} \ No newline at end of file diff --git a/src/main/java/us/tastybento/bskyblock/listeners/protection/VisitorGuard.java b/src/main/java/us/tastybento/bskyblock/listeners/protection/VisitorGuard.java deleted file mode 100644 index e1679fe50..000000000 --- a/src/main/java/us/tastybento/bskyblock/listeners/protection/VisitorGuard.java +++ /dev/null @@ -1,162 +0,0 @@ -package us.tastybento.bskyblock.listeners.protection; - -import org.bukkit.Location; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.entity.EntityDamageEvent.DamageCause; -import org.bukkit.event.entity.EntityPickupItemEvent; -import org.bukkit.event.entity.PlayerDeathEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerRespawnEvent; - -import us.tastybento.bskyblock.BSkyBlock; -import us.tastybento.bskyblock.config.Settings; -import us.tastybento.bskyblock.database.objects.Island; -import us.tastybento.bskyblock.database.objects.Island.SettingsFlag; -import us.tastybento.bskyblock.util.Util; - -/** - * @author tastybento - * Provides protection to islands - */ -public class VisitorGuard implements Listener { - private final BSkyBlock plugin; - private static final boolean DEBUG = false; - - public VisitorGuard(final BSkyBlock plugin) { - this.plugin = plugin; - } - - /* - * Prevent dropping items if player dies on another island - * This option helps reduce the down side of dying due to traps, etc. - * Also handles muting of death messages - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onVisitorDeath(final PlayerDeathEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (!Util.inWorld(e.getEntity())) { - return; - } - // Mute death messages - if (Settings.muteDeathMessages) { - e.setDeathMessage(null); - } - // If visitors will keep items and their level on death. This overrides any global settings. - // If the player is not a visitor then they die and lose everything - sorry :-( - Island island = plugin.getIslands().getProtectedIslandAt(e.getEntity().getLocation()); - if (island != null && !island.getMembers().contains(e.getEntity().getUniqueId()) && island.getFlag(SettingsFlag.KEEP_INVENTORY)) { - // They are a visitor - InventorySave.getInstance().savePlayerInventory(e.getEntity()); - e.getDrops().clear(); - e.setKeepLevel(true); - e.setDroppedExp(0); - } - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onVisitorSpawn(final PlayerRespawnEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - // If the player died on an island and his inventory has been saved, give it him back. This will override any global settings. - if (InventorySave.isStored(e.getPlayer().getUniqueId())) { - InventorySave.getInstance().loadPlayerInventory(e.getPlayer()); - InventorySave.getInstance().clearSavedInventory(e.getPlayer()); - } - } - - /* - * Prevent item drop by visitors - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onVisitorDrop(final PlayerDropItemEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (!Util.inWorld(e.getPlayer())) { - return; - } - Island island = plugin.getIslands().getIslandAt(e.getItemDrop().getLocation()); - if ((island != null && island.getFlag(SettingsFlag.ITEM_DROP)) - || e.getPlayer().hasPermission(Settings.PERMPREFIX + "mod.bypassprotect") - || plugin.getIslands().locationIsOnIsland(e.getPlayer(), e.getItemDrop().getLocation())) { - return; - } - e.getPlayer().sendMessage(plugin.getLocale(e.getPlayer()).get("island.protected")); - e.setCancelled(true); - } - - /** - * Prevents visitors from getting damage if invinciblevisitors option is set to TRUE - * @param e - */ - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onVisitorReceiveDamage(EntityDamageEvent e){ - if(!Settings.invincibleVisitor) return; - if(!(e.getEntity() instanceof Player)) return; - - Player p = (Player) e.getEntity(); - if (!Util.inWorld(p) || plugin.getIslands().locationIsOnIsland(p, p.getLocation())) return; - - if (Settings.invincibleVisitorOptions.contains(e.getCause())) e.setCancelled(true); - - else if(e.getCause().equals(DamageCause.VOID)) { - if(plugin.getPlayers().hasIsland(p.getUniqueId())) { - Location safePlace = plugin.getIslands().getSafeHomeLocation(p.getUniqueId(), 1); - if (safePlace != null) { - p.teleport(safePlace); - // Set their fall distance to zero otherwise they crash onto their island and die - p.setFallDistance(0); - e.setCancelled(true); - return; - } - } - // No island, or no safe spot on island - if (plugin.getIslands().getSpawn() != null) { - p.teleport(plugin.getIslands().getSpawnPoint()); - // Set their fall distance to zero otherwise they crash onto their island and die - p.setFallDistance(0); - e.setCancelled(true); - return; - } - // No island spawn, try regular spawn - if (!p.performCommand("spawn")) { - // If this command doesn't work, let them die otherwise they may get trapped in the void forever - return; - } - // Set their fall distance to zero otherwise they crash onto their island and die - p.setFallDistance(0); - e.setCancelled(true); - } - } - - /* - * Prevent item pickup by visitors - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onVisitorPickup(final EntityPickupItemEvent e) { - if (DEBUG) { - plugin.getLogger().info(e.getEventName()); - } - if (e.getEntity() instanceof Player) { - Player player = (Player)e.getEntity(); - if (!Util.inWorld(player)) { - return; - } - Island island = plugin.getIslands().getIslandAt(e.getItem().getLocation()); - if ((island != null && island.getFlag(SettingsFlag.ITEM_PICKUP)) - || player.hasPermission(Settings.PERMPREFIX + "mod.bypassprotect") - || plugin.getIslands().locationIsOnIsland(player, e.getItem().getLocation())) { - return; - } - e.setCancelled(true); - } - } - -} diff --git a/src/main/java/us/tastybento/bskyblock/managers/LocalesManager.java b/src/main/java/us/tastybento/bskyblock/managers/LocalesManager.java new file mode 100644 index 000000000..16a2ebc95 --- /dev/null +++ b/src/main/java/us/tastybento/bskyblock/managers/LocalesManager.java @@ -0,0 +1,64 @@ +package us.tastybento.bskyblock.managers; + +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.api.BSModule; +import us.tastybento.bskyblock.api.localization.BSLocale; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +/** + * @author Poslovitch + */ +public final class LocalesManager { + + private BSkyBlock plugin; + private Map> locales; + + public LocalesManager(BSkyBlock plugin) { + this.plugin = plugin; + this.locales = new HashMap<>(); + } + + public String get(CommandSender sender, String reference) { + if (sender instanceof Player) { + return get(((Player)sender).getUniqueId(), reference); + } + return reference; //TODO + } + + public String get(UUID uuid, String reference) { + return reference; //TODO + } + + public Map> getLocales() { + return locales; + } + + public List getLocales(BSModule module) { + return locales.get(module); + } + + public BSLocale getLocale(BSModule module, String languageTag) { + for (BSLocale locale : locales.get(module)) { + if (locale.toLanguageTag().equals(languageTag)) return locale; + } + return null; + } + + public void registerLocales(BSModule module) { + //TODO + } + + public void registerLocale(BSModule module, String languageTag) { + //TODO + } + + public void registerExternalLocale(BSModule originModule, BSModule targetModule, String languageTag) { + //TODO + } +}