getShopIterator() {
- return new ShopIterator();
- }
-
- /**
- * Returns all shops in the whole database, include unloaded.
- *
- * Make sure you have caching this, because this need a while to get all shops
- *
- * @return All shop in the database
- */
- public Collection getAllShops() {
- //noinspection unchecked
- HashMap>> worldsMap =
- (HashMap>>) getShops().clone();
- Collection shops = new ArrayList<>();
- for (HashMap> shopMapData : worldsMap.values()) {
- for (HashMap shopData : shopMapData.values()) {
- shops.addAll(shopData.values());
- }
- }
- return shops;
- }
-
- /**
- * Returns a hashmap of World - Chunk - Shop
- *
- * @return a hashmap of World - Chunk - Shop
- */
- @NotNull
- public HashMap>> getShops() {
- return this.shops;
- }
-
- /**
- * Get all loaded shops.
- *
- * @return All loaded shops.
- */
- public @Nullable Set getLoadedShops() {
- return this.loadedShops;
- }
-
- /**
- * Get a players all shops.
- *
- * @param playerUUID The player's uuid.
- * @return The list have this player's all shops.
- */
- public @NotNull List getPlayerAllShops(@NotNull UUID playerUUID) {
- return getAllShops().stream()
- .filter(shop -> shop.getOwner().equals(playerUUID))
- .collect(Collectors.toList());
- }
-
- /**
- * Get the all shops in the world.
- *
- * @param world The world you want get the shops.
- * @return The list have this world all shops
- */
- public @NotNull List getShopsInWorld(@NotNull World world) {
- return getAllShops().stream()
- .filter(shop -> Objects.equals(shop.getLocation().getWorld(), world))
- .collect(Collectors.toList());
- }
-
- public class ShopIterator implements Iterator {
- private Iterator> chunks;
- private Iterator shops;
- private Iterator>> worlds;
-
- public ShopIterator() {
- //noinspection unchecked
- HashMap>> worldsMap =
- (HashMap>>) getShops().clone();
- worlds = worldsMap.values().iterator();
+ }
+ });
}
- /** Returns true if there is still more shops to iterate over. */
- @Override
- public boolean hasNext() {
- if (shops == null || !shops.hasNext()) {
- if (chunks == null || !chunks.hasNext()) {
- if (!worlds.hasNext()) {
- return false;
- } else {
- chunks = worlds.next().values().iterator();
- return hasNext();
- }
- } else {
- shops = chunks.next().values().iterator();
- return hasNext();
+ /**
+ * Loads the given shop into storage. This method is used for loading data from the database. Do
+ * not use this method to create a shop.
+ *
+ * @param world The world the shop is in
+ * @param shop The shop to load
+ */
+ public void loadShop(@NotNull String world, @NotNull Shop shop) {
+ this.addShop(world, shop);
+ }
+
+ /**
+ * Removes a shop from the world. Does NOT remove it from the database. * REQUIRES * the world to
+ * be loaded Call shop.onUnload by your self.
+ *
+ * @param shop The shop to remove
+ */
+ public void removeShop(@NotNull Shop shop) {
+ // shop.onUnload();
+ Location loc = shop.getLocation();
+ String world = Objects.requireNonNull(loc.getWorld()).getName();
+ HashMap> inWorld = this.getShops().get(world);
+ int x = (int) Math.floor((shop.getLocation().getBlockX()) / 16.0);
+ int z = (int) Math.floor((shop.getLocation().getBlockZ()) / 16.0);
+ ShopChunk shopChunk = new ShopChunk(world, x, z);
+ HashMap inChunk = inWorld.get(shopChunk);
+ if (inChunk == null) {
+ return;
}
- }
- return true;
+ inChunk.remove(loc);
+ // shop.onUnload();
}
- /** Fetches the next shop. Throws NoSuchElementException if there are no more shops. */
- @Override
- public @NotNull Shop next() {
- if (shops == null || !shops.hasNext()) {
- if (chunks == null || !chunks.hasNext()) {
- if (!worlds.hasNext()) {
- throw new NoSuchElementException("No more shops to iterate over!");
- }
- chunks = worlds.next().values().iterator();
- }
- shops = chunks.next().values().iterator();
- }
- if (!shops.hasNext()) {
- return this.next(); // Skip to the next one (Empty iterator?)
- }
- return shops.next();
+ /**
+ * @return Returns the HashMap. Info contains what their last question etc was.
+ */
+ public HashMap getActions() {
+ return this.actions;
}
- }
+
+ /**
+ * Returns a new shop iterator object, allowing iteration over shops easily, instead of sorting
+ * through a 3D hashmap.
+ *
+ * @return a new shop iterator object.
+ */
+ public Iterator getShopIterator() {
+ return new ShopIterator();
+ }
+
+ /**
+ * Returns all shops in the whole database, include unloaded.
+ *
+ * Make sure you have caching this, because this need a while to get all shops
+ *
+ * @return All shop in the database
+ */
+ public Collection getAllShops() {
+ //noinspection unchecked
+ HashMap>> worldsMap = (HashMap>>) getShops().clone();
+ Collection shops = new ArrayList<>();
+ for (HashMap> shopMapData : worldsMap.values()) {
+ for (HashMap shopData : shopMapData.values()) {
+ shops.addAll(shopData.values());
+ }
+ }
+ return shops;
+ }
+
+ /**
+ * Returns a hashmap of World - Chunk - Shop
+ *
+ * @return a hashmap of World - Chunk - Shop
+ */
+ @NotNull
+ public HashMap>> getShops() {
+ return this.shops;
+ }
+
+ /**
+ * Get all loaded shops.
+ *
+ * @return All loaded shops.
+ */
+ public @Nullable Set getLoadedShops() {
+ return this.loadedShops;
+ }
+
+ /**
+ * Get a players all shops.
+ *
+ * @param playerUUID The player's uuid.
+ * @return The list have this player's all shops.
+ */
+ public @NotNull List getPlayerAllShops(@NotNull UUID playerUUID) {
+ return getAllShops().stream().filter(shop -> shop.getOwner().equals(playerUUID)).collect(Collectors.toList());
+ }
+
+ /**
+ * Get the all shops in the world.
+ *
+ * @param world The world you want get the shops.
+ * @return The list have this world all shops
+ */
+ public @NotNull List getShopsInWorld(@NotNull World world) {
+ return getAllShops().stream().filter(shop -> Objects.equals(shop.getLocation().getWorld(), world)).collect(Collectors.toList());
+ }
+
+ public class ShopIterator implements Iterator {
+
+ private Iterator> chunks;
+
+ private Iterator shops;
+
+ private Iterator>> worlds;
+
+ public ShopIterator() {
+ //noinspection unchecked
+ HashMap>> worldsMap = (HashMap>>) getShops().clone();
+ worlds = worldsMap.values().iterator();
+ }
+
+ /**
+ * Returns true if there is still more shops to iterate over.
+ */
+ @Override
+ public boolean hasNext() {
+ if (shops == null || !shops.hasNext()) {
+ if (chunks == null || !chunks.hasNext()) {
+ if (!worlds.hasNext()) {
+ return false;
+ } else {
+ chunks = worlds.next().values().iterator();
+ return hasNext();
+ }
+ } else {
+ shops = chunks.next().values().iterator();
+ return hasNext();
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Fetches the next shop. Throws NoSuchElementException if there are no more shops.
+ */
+ @Override
+ public @NotNull Shop next() {
+ if (shops == null || !shops.hasNext()) {
+ if (chunks == null || !chunks.hasNext()) {
+ if (!worlds.hasNext()) {
+ throw new NoSuchElementException("No more shops to iterate over!");
+ }
+ chunks = worlds.next().values().iterator();
+ }
+ shops = chunks.next().values().iterator();
+ }
+ if (!shops.hasNext()) {
+ return this.next(); // Skip to the next one (Empty iterator?)
+ }
+ return shops.next();
+ }
+
+ }
+
}