đŸŦCustom storage

Create your custom storage

You can create your own storage system if the default ones do not suit you.

StorageManager manager = this.getProvider(StorageManager.class);
manager.setCustomStorage(new IStorage() {
	
	@Override
	public void updateTransaction(List<Transaction> transactions) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void updateItem(AuctionPlugin plugin, AuctionItem auctionItem, StorageType type) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void update(AuctionPlugin plugin, StorageType type) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void storeTransaction(Transaction transaction) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void saveTransactions(List<Transaction> transactions) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void saveItems(AuctionPlugin plugin, List<AuctionItem> auctionItems, StorageType type) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void saveItem(AuctionPlugin plugin, AuctionItem auctionItem, StorageType type) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void save(AuctionPlugin plugin) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void removeItem(AuctionPlugin plugin, AuctionItem auctionItem, StorageType type) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void load(StorageManager storageManager, AuctionPlugin plugin) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public List<Transaction> getTransactions() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public List<AuctionItem> getItems(AuctionPlugin plugin, StorageType type) {
		// TODO Auto-generated method stub
		return null;
	}
});

Here is an example of use with the JsonStorage class. This class is directly the one used in the plugin, if you have questions about the API you can come on discord.

package fr.maxlego08.zauctionhouse.storage.storages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import fr.maxlego08.zauctionhouse.api.AuctionItem;
import fr.maxlego08.zauctionhouse.api.AuctionPlugin;
import fr.maxlego08.zauctionhouse.api.enums.StorageType;
import fr.maxlego08.zauctionhouse.api.event.events.AuctionItemExpireEvent;
import fr.maxlego08.zauctionhouse.api.storage.IStorage;
import fr.maxlego08.zauctionhouse.api.storage.StorageManager;
import fr.maxlego08.zauctionhouse.api.transaction.Transaction;
import fr.maxlego08.zauctionhouse.api.utils.Config;
import fr.maxlego08.zauctionhouse.zcore.logger.LoggerManager;
import fr.maxlego08.zauctionhouse.zcore.utils.ZUtils;

public class JSonStorage extends ZUtils implements IStorage {

	private List<AuctionItem> items = new ArrayList<>();
	private List<AuctionItem> buyingItems = new ArrayList<>();
	private List<AuctionItem> expiredItems = new ArrayList<>();
	private List<Transaction> transactions = new ArrayList<>();

	private transient Map<UUID, Long> cooldowns = new HashMap<UUID, Long>();

	@Override
	public void load(StorageManager manager, AuctionPlugin plugin) {
		
		this.items.removeIf(e -> e == null);
		this.buyingItems.removeIf(e -> e == null);
		this.expiredItems.removeIf(e -> e == null);
		
		this.buyingItems.forEach(e -> {
			if (e == null){
				return;
			}

			if (e != null){
				e.setCanBuy(false);
			}
			e.setStorageType(StorageType.BUY);
		});
		this.expiredItems.forEach(e -> {

			if (e == null){
				return;
			}

			if (e != null){
				e.setCanBuy(false);
			}
			e.setStorageType(StorageType.EXPIRE);
		});
		this.items.forEach(e -> {
			if (e == null){
				return;
			}
			e.setStorageType(StorageType.STORAGE);
		});

		this.purgeTransactions(Config.removeTransactionAfterSeconds, () -> {
		});
	}

	@Override
	public void saveItem(AuctionPlugin plugin, AuctionItem auctionItem, StorageType type) {
		auctionItem.setStorageType(type);
		switch (type) {
		case BUY:
			buyingItems.add(auctionItem);
			break;
		case EXPIRE:
			expiredItems.add(auctionItem);
			break;
		case STORAGE:
			items.add(auctionItem);
			break;
		default:
			break;
		}
	}

	@Override
	public void removeItem(AuctionPlugin plugin, AuctionItem auctionItem, StorageType type) {
		switch (type) {
		case BUY:
			buyingItems.remove(auctionItem);
			break;
		case EXPIRE:
			expiredItems.remove(auctionItem);
			break;
		case STORAGE:
			items.remove(auctionItem);
			break;
		default:
			break;
		}
	}

	@Override
	public void saveItems(AuctionPlugin plugin, List<AuctionItem> i, StorageType type) {
		switch (type) {
		case BUY:
			buyingItems = i;
			break;
		case EXPIRE:
			expiredItems = i;
			break;
		case STORAGE:
			items = i;
			break;
		default:
			break;
		}
	}

	@Override
	public void update(AuctionPlugin plugin, StorageType type) {
		switch (type) {
		case BUY:
			update(plugin, buyingItems, type);
			break;
		case EXPIRE:
			update(plugin, expiredItems, type);
			break;
		case STORAGE:
			update(plugin, items, type);
			break;
		default:
			break;
		}
	}

	private void update(AuctionPlugin plugin, List<AuctionItem> items, StorageType type) {
		long currentMilliSecond = System.currentTimeMillis();
		Iterator<AuctionItem> iterator = items.iterator();
		while (iterator.hasNext()) {
			AuctionItem auctionItem = iterator.next();
			if (auctionItem == null)
				continue;

			if (System.currentTimeMillis() > auctionItem.getExpireAt() && auctionItem.getExpireAt() != -1) {

				Bukkit.getScheduler().runTask(plugin, () -> {
					AuctionItemExpireEvent event = new AuctionItemExpireEvent(auctionItem.getExpireAt(),
							currentMilliSecond, auctionItem, type);
					event.callEvent();
				});

				// Ajout de logger sur les informations ici !

				if (type.equals(StorageType.STORAGE)) {

					long expiredAt = Config.expireTime != -1 ? (System.currentTimeMillis() + (1000 * Config.expireTime))
							: Config.expireTime;
					auctionItem.setExpireAt(expiredAt);
					auctionItem.setCanBuy(false);
					saveItem(plugin, auctionItem, StorageType.EXPIRE);
					iterator.remove();

					String message = "x%s %s on sale by %s has just expired, it goes in the inventory of expired items.";
					LoggerManager.getInstance().log(message, auctionItem.getAmount(),
							getItemName(auctionItem.getItemStack()), auctionItem.getSeller().getName());
				} else {
					iterator.remove();

					if (type.equals(StorageType.BUY)) {

						String message = "x%s %s, bought by %s, has just expired, the item is definitively deleted.";
						LoggerManager.getInstance().log(message, auctionItem.getAmount(),
								getItemName(auctionItem.getItemStack()), auctionItem.getSeller().getName());

					} else {

						String message = "x%s %s, item of %s, has just expired, the item is definitively deleted.";
						LoggerManager.getInstance().log(message, auctionItem.getAmount(),
								getItemName(auctionItem.getItemStack()), auctionItem.getSeller().getName());

					}
				}
			}
		}
	}

	@Override
	public List<AuctionItem> getItems(AuctionPlugin plugin, StorageType type) {
		this.update(plugin, type);
		switch (type) {
		case BUY:
			return buyingItems;
		case EXPIRE:
			return expiredItems;
		case STORAGE:
			return items;
		default:
			return new ArrayList<>();
		}
	}

	@Override
	public void save(AuctionPlugin plugin) {

	}

	@Override
	public void storeTransaction(Transaction transaction, Consumer<Transaction> consumer) {
		this.transactions.add(transaction);
		if (consumer != null) {
			consumer.accept(transaction);
		}
	}

	@Override
	public void saveTransactions(List<Transaction> transactions) {
		this.transactions = transactions;
	}

	@Override
	public void updateItem(AuctionPlugin plugin, AuctionItem auctionItem, StorageType type) {
		this.removeItem(plugin, auctionItem, StorageType.STORAGE);
		this.saveItem(plugin, auctionItem, type);
	}

	@Override
	public List<Transaction> getTransactions() {
		return transactions;
	}

	@Override
	public void updateTransaction(List<Transaction> transactions) {
		// TODO Auto-generated method stub

	}

	@Override
	public void purgeTransactions(long seconds, Runnable runnable) {
		Iterator<Transaction> iterator = this.transactions.iterator();
		while (iterator.hasNext()) {
			Transaction transaction = iterator.next();
			long diff = Math.abs(System.currentTimeMillis() - transaction.getDate()) / 1000;
			if (diff > seconds) {
				iterator.remove();
			}
		}
		runnable.run();
	}

	/**
	 * @return the items
	 */
	public List<AuctionItem> getItems() {
		return items;
	}

	/**
	 * @return the buyingItems
	 */
	public List<AuctionItem> getBuyingItems() {
		return buyingItems;
	}

	/**
	 * @return the expiredItems
	 */
	public List<AuctionItem> getExpiredItems() {
		return expiredItems;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "JSonStorage [items=" + items + ", buyingItems=" + buyingItems + ", expiredItems=" + expiredItems
				+ ", transactions=" + transactions + "]";
	}

	@Override
	public void fetchClaimMoney(Player player, Consumer<List<Transaction>> object) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isCooldown(UUID uniqueId) {

		long expiredAt = this.cooldowns.getOrDefault(uniqueId, 0l);

		if (expiredAt > System.currentTimeMillis()) {
			return true;
		}

		this.cooldowns.put(uniqueId, System.currentTimeMillis() + (1000 * Config.cooldownClickMilliseconds));

		return false;
	}

	/**
	 * @return the cooldowns
	 */
	public Map<UUID, Long> getCooldowns() {
		return cooldowns;
	}

	/**
	 * @param items the items to set
	 */
	public void setItems(List<AuctionItem> items) {
		this.items = items;
	}

	/**
	 * @param buyingItems the buyingItems to set
	 */
	public void setBuyingItems(List<AuctionItem> buyingItems) {
		this.buyingItems = buyingItems;
	}

	/**
	 * @param expiredItems the expiredItems to set
	 */
	public void setExpiredItems(List<AuctionItem> expiredItems) {
		this.expiredItems = expiredItems;
	}

	/**
	 * @param transactions the transactions to set
	 */
	public void setTransactions(List<Transaction> transactions) {
		this.transactions = transactions;
	}

	/**
	 * @param cooldowns the cooldowns to set
	 */
	public void setCooldowns(Map<UUID, Long> cooldowns) {
		this.cooldowns = cooldowns;
	}
	
	

}

Last updated