diff --git a/src/main/java/com/alttd/playershops/api/ShopHandler.java b/src/main/java/com/alttd/playershops/api/ShopHandler.java new file mode 100644 index 0000000..e0d8685 --- /dev/null +++ b/src/main/java/com/alttd/playershops/api/ShopHandler.java @@ -0,0 +1,72 @@ +package com.alttd.playershops.api; + +import com.alttd.playershops.shop.AbstractShop; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.UUID; + +public interface ShopHandler { // TODO finish docs + + /** + * Get the shop at a given location + * + * @param location Location of the shop + * @return Shop at the given location or null if no shop is found there + */ + AbstractShop getShop(Location location); + + /** + * Checks whether there is a shop at a given location + * @param location Location to check + * @return Whether there is a shop at the given location + */ + boolean isShop(Location location); + + /** + * Get a collection of all loaded shops + * + * This collection is safe to use for looping over and removing shops. + * + * @return Read-only collection of all shops + */ + Collection getShops(); + + /** + * Add a player's custom shop limit + * + * @param uuid The uuid linked to this player + * @param limit The new maximum amount of shops this player can have + */ + void addPlayerLimit(UUID uuid, int limit); + + /** + * Get the maximum amount of shops this player is allowed to have + * + * @param uuid The uuid linked to this player + * @return The limit of shops for this player + */ + int getShopLimit(UUID uuid); + + /** + * Checks wether this block is valid to be used as a shop + * + * @param block the block to check + * @return true if this block can be a shop + */ + boolean isShopMaterial(Block block); + + /** + * Get all the shops owned by a player + * + * @param uuid The uuid linked to this player + * @return List of shops this player owns + */ + List getShops(UUID uuid); + + ArrayList getShopMaterials(); +} diff --git a/src/main/java/com/alttd/playershops/handler/ShopHandler.java b/src/main/java/com/alttd/playershops/handler/ShopHandler.java index b7bbd0a..8cdacb7 100644 --- a/src/main/java/com/alttd/playershops/handler/ShopHandler.java +++ b/src/main/java/com/alttd/playershops/handler/ShopHandler.java @@ -2,22 +2,97 @@ package com.alttd.playershops.handler; import com.alttd.playershops.PlayerShops; import com.alttd.playershops.config.Config; +import com.alttd.playershops.shop.AbstractShop; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import lombok.Getter; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Tag; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; -import java.util.UUID; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; -public class ShopHandler { +public class ShopHandler implements com.alttd.playershops.api.ShopHandler { private final PlayerShops plugin; @Getter private final Object2IntMap shopBuildLimits; + @Getter + private final Map shopLocation; + @Getter + private final ArrayList shopMaterials; public ShopHandler(PlayerShops instance) { plugin = instance; + shopLocation = new ConcurrentHashMap<>(); shopBuildLimits = new Object2IntOpenHashMap<>(); shopBuildLimits.defaultReturnValue(Config.shopLimit); + shopMaterials = new ArrayList<>(); // TODO move into parent method where materials are loaded in. + } + + public AbstractShop getShop(Location location) { + Location newLocation = new Location(location.getWorld(), location.getBlockX(), location.getBlockY(), location.getBlockZ()); + + return shopLocation.get(newLocation); + } + + public boolean isShop(Location location) { + return getShop(location) != null; + } + + public Collection getShops() { + return Collections.unmodifiableCollection(shopLocation.values()); + } + + public void addPlayerLimit(UUID uuid, int limit) { + shopBuildLimits.put(uuid, limit); + } + + public int getShopLimit(UUID uuid) { + return shopBuildLimits.getInt(uuid); + } + + public void removeShops() { + shopLocation.clear(); + } + + public boolean isShopMaterial(Block block) { + if (Tag.SHULKER_BOXES.isTagged(block.getType())) { + return true; + } + return shopMaterials.contains(block.getType()); + } + + public List getShops(UUID uuid) { + List shops = new ArrayList<>(); + for (AbstractShop shop : shopLocation.values()) { + if (shop.getOwnerUUID().equals(uuid)) + shops.add(shop); + } + return shops; + } + + public AbstractShop getShopBySignLocation(Location signLocation) { + for (AbstractShop shop : shopLocation.values()) { + if (shop.getSignLocation().equals(signLocation)) + return shop; + } + return null; + } + + public AbstractShop getShopNearBlock(Block block) { + BlockFace[] faces = {BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}; + for (BlockFace face : faces) { + if (this.isShopMaterial(block.getRelative(face))) { + Block blockRelative = block.getRelative(face); + if (isShop(blockRelative.getLocation())) + return getShop(blockRelative.getLocation()); + } + } + return null; } }