From 00323510bba9d29066dc14baa53b4a89d804f07d Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 10 Feb 2017 17:59:08 +0100 Subject: [PATCH] Only show shops to players who point on the shop (configurable) --- .../de/epiceric/shopchest/config/Config.java | 4 ++ .../epiceric/shopchest/utils/ShopUtils.java | 60 +++++++++++++++++-- src/main/resources/config.yml | 5 ++ 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/config/Config.java b/src/main/java/de/epiceric/shopchest/config/Config.java index cc38ef9..f12e261 100644 --- a/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/src/main/java/de/epiceric/shopchest/config/Config.java @@ -119,6 +119,9 @@ public class Config { /** Whether players are allowed to sell/buy broken items **/ public boolean allow_broken_items; + /** Whether only the shops a player points on should be shown to him **/ + public boolean only_show_shops_in_sight; + /** *

Whether shops should automatically be removed from the database if an error occurred while loading

* (e.g. when no chest is found at a shop's location) @@ -337,6 +340,7 @@ public class Config { enable_towny_integration = plugin.getConfig().getBoolean("enable-towny-integration"); enable_vendor_messages = plugin.getConfig().getBoolean("enable-vendor-messages"); explosion_protection = plugin.getConfig().getBoolean("explosion-protection"); + only_show_shops_in_sight = plugin.getConfig().getBoolean("only-show-shops-in-sight"); exclude_admin_shops = plugin.getConfig().getBoolean("shop-limits.exclude-admin-shops"); append_potion_level_to_item_name = plugin.getConfig().getBoolean("append-potion-level-to-item-name"); show_shop_items = plugin.getConfig().getBoolean("show-shop-items"); diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java index 1a336ed..dc9abc1 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java @@ -3,19 +3,22 @@ package de.epiceric.shopchest.utils; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.shop.Shop; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.block.Chest; import org.bukkit.block.DoubleChest; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryHolder; import org.bukkit.permissions.PermissionAttachmentInfo; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; public class ShopUtils { @@ -227,13 +230,62 @@ public class ShopUtils { * @param location Location of the player */ public void updateShops(Player player, Location location) { - for (Shop shop : getShops()) { - updateShop(shop, player, location); + if (plugin.getShopChestConfig().only_show_shops_in_sight) { + HashSet transparent = new HashSet<>(); + transparent.add(Material.AIR); + List sight = player.getLineOfSight(transparent, (int) plugin.getShopChestConfig().maximal_distance); + + ArrayList shopsInSight = new ArrayList<>(); + + for (Block block : sight) { + if (block.getType() == Material.CHEST || block.getType() == Material.TRAPPED_CHEST) { + if (isShop(block.getLocation())) { + Shop shop = getShop(block.getLocation()); + shopsInSight.add(shop); + + if (shop.getHologram() != null && !shop.getHologram().isVisible(player)) { + shop.getHologram().showPlayer(player); + } + } + } else { + Block below = block.getRelative(BlockFace.DOWN); + if (isShop(below.getLocation())) { + Shop shop = getShop(below.getLocation()); + shopsInSight.add(shop); + + if (shop.getHologram() != null && !shop.getHologram().isVisible(player)) { + shop.getHologram().showPlayer(player); + } + } + } + } + + double itemDistSqr = Math.pow(plugin.getShopChestConfig().maximal_item_distance, 2); + + for (Shop shop : getShops()) { + if (shop.getItem() != null && shop.getLocation().getWorld().getName().equals(player.getWorld().getName())) { + if (shop.getLocation().distanceSquared(player.getEyeLocation()) <= itemDistSqr) { + shop.getItem().setVisible(player, true); + } else { + shop.getItem().setVisible(player, false); + } + } + + if (!shopsInSight.contains(shop)) { + if (shop.getHologram() != null) { + shop.getHologram().hidePlayer(player); + } + } + } + } else { + for (Shop shop : getShops()) { + updateShop(shop, player, location); + } } } /** - * Update hologram and item of the shop for a player + * Update hologram and item of the shop for a player based on their distance to each other * @param shop Shop to update * @param player Player to show the update * @param location Location of the player diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6359ad4..2b74a56 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -45,6 +45,11 @@ enable-towny-integration: true # buy or sell something from/to his shop or if his shop is out of stock enable-vendor-messages: true +# Set whether only the shops a player points on should be shown to him. +# If set to false, every shop near the player (with the specified +# distance) will be shown to him. +only-show-shops-in-sight: true + # Set whether the buy- and sell price should be arranged below each other. # The first line will be the buy price with the message # "message.hologram.only-buy", the second line will be the sell price