Implement itemcase shop variables.
This commit is contained in:
		@@ -33,6 +33,7 @@ import org.bukkit.event.block.BlockBreakEvent;
 | 
			
		||||
import org.bukkit.event.block.BlockPlaceEvent;
 | 
			
		||||
import org.bukkit.event.entity.EntityPickupItemEvent;
 | 
			
		||||
import org.bukkit.event.entity.ItemDespawnEvent;
 | 
			
		||||
import org.bukkit.inventory.Inventory;
 | 
			
		||||
import org.bukkit.inventory.ItemStack;
 | 
			
		||||
import org.bukkit.inventory.meta.ItemMeta;
 | 
			
		||||
import org.bukkit.metadata.FixedMetadataValue;
 | 
			
		||||
@@ -46,6 +47,24 @@ import org.bukkit.util.Vector;
 | 
			
		||||
 */
 | 
			
		||||
public final class Itemcase {
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Types of Itemcase.
 | 
			
		||||
     */
 | 
			
		||||
    public static enum Type {
 | 
			
		||||
        SHOWCASE,
 | 
			
		||||
        SHOP_BUY,
 | 
			
		||||
        SHOP_SELL,
 | 
			
		||||
        SHOP_MULTI
 | 
			
		||||
    };
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Storage types.
 | 
			
		||||
     */
 | 
			
		||||
    public static enum StorageType {
 | 
			
		||||
        FINITE,
 | 
			
		||||
        INFINITE
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * The ItemStack that this itemcase is showing.
 | 
			
		||||
     */
 | 
			
		||||
@@ -76,16 +95,45 @@ public final class Itemcase {
 | 
			
		||||
     */
 | 
			
		||||
    private Item displayItem;
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * This itemcase's Type.
 | 
			
		||||
     */
 | 
			
		||||
    private Type type = Type.SHOWCASE;
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * The storage type of this itemcase.
 | 
			
		||||
     */
 | 
			
		||||
    private StorageType storageType = StorageType.FINITE;
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * The storage of this itemcase.
 | 
			
		||||
     */
 | 
			
		||||
    private Inventory storage;
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * The buy price of this itemcase.
 | 
			
		||||
     */
 | 
			
		||||
    private double buyPrice = 0;
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * The sell price of this itemcase.
 | 
			
		||||
     */
 | 
			
		||||
    private double sellPrice = 0;
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructor.
 | 
			
		||||
     * 
 | 
			
		||||
     * @param type The type of Itemcase.
 | 
			
		||||
     * @param itemStack The ItemStack to be displayed.
 | 
			
		||||
     * @param location The location of the itemcase.
 | 
			
		||||
     * @param owner The owner of this itemcase.
 | 
			
		||||
     */
 | 
			
		||||
    public Itemcase(ItemStack itemStack, Location location,
 | 
			
		||||
    public Itemcase(Type type, ItemStack itemStack, Location location,
 | 
			
		||||
            OfflinePlayer owner) {
 | 
			
		||||
        
 | 
			
		||||
        // Set type.
 | 
			
		||||
        this.type = type;
 | 
			
		||||
        
 | 
			
		||||
        // Set item stack and ensure stack size is 1.
 | 
			
		||||
        this.itemStack = itemStack.clone();
 | 
			
		||||
        this.itemStack.setAmount(1);
 | 
			
		||||
@@ -235,6 +283,96 @@ public final class Itemcase {
 | 
			
		||||
        return this.displayItem;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @param type Type.
 | 
			
		||||
     */
 | 
			
		||||
    public void setType(Type type) {
 | 
			
		||||
        
 | 
			
		||||
        // Set type.
 | 
			
		||||
        this.type = type;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return Type.
 | 
			
		||||
     */
 | 
			
		||||
    public Type getType() {
 | 
			
		||||
        
 | 
			
		||||
        // Return type.
 | 
			
		||||
        return this.type;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @param storageType StorageType.
 | 
			
		||||
     */
 | 
			
		||||
    public void setStorageType(StorageType storageType) {
 | 
			
		||||
        
 | 
			
		||||
        // Set storage type.
 | 
			
		||||
        this.storageType= storageType;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return Storage Type.
 | 
			
		||||
     */
 | 
			
		||||
    public StorageType getStorageType() {
 | 
			
		||||
        
 | 
			
		||||
        // Return storage type.
 | 
			
		||||
        return this.storageType;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @param inventory Inventory.
 | 
			
		||||
     */
 | 
			
		||||
    public void setStorage(Inventory inventory) {
 | 
			
		||||
        
 | 
			
		||||
        // Set inventory.
 | 
			
		||||
        this.storage = inventory;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return Inventory.
 | 
			
		||||
     */
 | 
			
		||||
    public Inventory getStorage() {
 | 
			
		||||
        
 | 
			
		||||
        // Return inventory.
 | 
			
		||||
        return this.storage;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @param buyPrice Buy price.
 | 
			
		||||
     */
 | 
			
		||||
    public void setBuyPrice(double buyPrice) {
 | 
			
		||||
        
 | 
			
		||||
        // Set price.
 | 
			
		||||
        this.buyPrice = buyPrice;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return Buy price.
 | 
			
		||||
     */
 | 
			
		||||
    public double getBuyPrice() {
 | 
			
		||||
        
 | 
			
		||||
        // Return price.
 | 
			
		||||
        return this.buyPrice;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @param buyPrice Sell price.
 | 
			
		||||
     */
 | 
			
		||||
    public void setSellPrice(double sellPrice) {
 | 
			
		||||
        
 | 
			
		||||
        // Set price.
 | 
			
		||||
        this.sellPrice = sellPrice;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return Sell price.
 | 
			
		||||
     */
 | 
			
		||||
    public double getSellPrice() {
 | 
			
		||||
        
 | 
			
		||||
        // Return price.
 | 
			
		||||
        return this.sellPrice;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return The default location to spawn the display item.
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -17,21 +17,24 @@ package com.gmail.bleedobsidian.itemcase.configurations;
 | 
			
		||||
import com.gmail.bleedobsidian.itemcase.ConfigurationFile;
 | 
			
		||||
import com.gmail.bleedobsidian.itemcase.ItemCaseCore;
 | 
			
		||||
import com.gmail.bleedobsidian.itemcase.Itemcase;
 | 
			
		||||
import com.gmail.bleedobsidian.itemcase.Itemcase.StorageType;
 | 
			
		||||
import com.gmail.bleedobsidian.itemcase.Itemcase.Type;
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Map.Entry;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
import org.apache.commons.multiverse.io.FileUtils;
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.Location;
 | 
			
		||||
import org.bukkit.OfflinePlayer;
 | 
			
		||||
import org.bukkit.World;
 | 
			
		||||
import org.bukkit.configuration.ConfigurationSection;
 | 
			
		||||
import org.bukkit.inventory.Inventory;
 | 
			
		||||
import org.bukkit.inventory.ItemStack;
 | 
			
		||||
import org.bukkit.plugin.java.JavaPlugin;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A configuration file that holds all Itemcase saves for a specific world.
 | 
			
		||||
@@ -75,6 +78,9 @@ public class WorldFile extends ConfigurationFile {
 | 
			
		||||
        // Create a unique key for this itemcase based on location.
 | 
			
		||||
        String key = "itemcases." + blockX + "/" + blockY + "/" + blockZ + ".";
 | 
			
		||||
        
 | 
			
		||||
        // Set this Itemcase's type.
 | 
			
		||||
        this.file.set(key + "type", itemcase.getType().name());
 | 
			
		||||
        
 | 
			
		||||
        // Set this Itemcase's Owner.
 | 
			
		||||
        String uuid = itemcase.getOwner().getUniqueId().toString();
 | 
			
		||||
        this.file.set(key + "owner", uuid);
 | 
			
		||||
@@ -83,6 +89,45 @@ public class WorldFile extends ConfigurationFile {
 | 
			
		||||
        Map<String, Object> itemstack = itemcase.getItemStack().serialize();
 | 
			
		||||
        this.file.set(key + "itemstack", itemstack);
 | 
			
		||||
        
 | 
			
		||||
        // If itemcase is a shop.
 | 
			
		||||
        if(itemcase.getType() != Type.SHOWCASE) {
 | 
			
		||||
            
 | 
			
		||||
            // Set storage type.
 | 
			
		||||
            this.file.set(key + "shop.storage-type",
 | 
			
		||||
                    itemcase.getStorageType().name());
 | 
			
		||||
            
 | 
			
		||||
            // If shop has finite storage.
 | 
			
		||||
            if(itemcase.getStorageType() == StorageType.FINITE) {
 | 
			
		||||
                
 | 
			
		||||
                // Serialize inventory.
 | 
			
		||||
                Map<String, Object> inventory = 
 | 
			
		||||
                        this.serializeInventory(itemcase.getStorage());
 | 
			
		||||
                
 | 
			
		||||
                // Set inventory.
 | 
			
		||||
                this.file.set(key + "shop.storage", inventory);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            // If this itemcase buys.
 | 
			
		||||
            if(itemcase.getType() == Type.SHOP_BUY ||
 | 
			
		||||
                    itemcase.getType() == Type.SHOP_MULTI) {
 | 
			
		||||
                
 | 
			
		||||
                // Set buy price.
 | 
			
		||||
                this.file.set(key + "shop.buy-price", itemcase.getBuyPrice());
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            // If this itemcase sells.
 | 
			
		||||
            if(itemcase.getType() == Type.SHOP_SELL ||
 | 
			
		||||
                    itemcase.getType() == Type.SHOP_MULTI) {
 | 
			
		||||
                
 | 
			
		||||
                // Set sell price.
 | 
			
		||||
                this.file.set(key + "shop.sell-price", itemcase.getSellPrice());
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            
 | 
			
		||||
            // Set shop section to null.
 | 
			
		||||
            this.file.set(key + "shop", null);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Attempt to save to file.
 | 
			
		||||
        this.save(ItemCaseCore.instance);
 | 
			
		||||
    }
 | 
			
		||||
@@ -146,6 +191,10 @@ public class WorldFile extends ConfigurationFile {
 | 
			
		||||
            Location location = new Location(this.world, blockX, blockY,
 | 
			
		||||
                    blockZ);
 | 
			
		||||
            
 | 
			
		||||
            // Get type.
 | 
			
		||||
            Type type = Itemcase.Type.valueOf(
 | 
			
		||||
                    this.file.getString(key + ".type"));
 | 
			
		||||
            
 | 
			
		||||
            // Get owner.
 | 
			
		||||
            UUID uuid = UUID.fromString(this.file.getString(key + ".owner"));
 | 
			
		||||
            OfflinePlayer owner = Bukkit.getOfflinePlayer(uuid);
 | 
			
		||||
@@ -157,7 +206,52 @@ public class WorldFile extends ConfigurationFile {
 | 
			
		||||
            ItemStack itemstack = ItemStack.deserialize(itemstackMap);
 | 
			
		||||
            
 | 
			
		||||
            // Create itemcase object.
 | 
			
		||||
            Itemcase itemcase = new Itemcase(itemstack, location, owner);
 | 
			
		||||
            Itemcase itemcase = new Itemcase(type, itemstack, location, owner);
 | 
			
		||||
            
 | 
			
		||||
            // If itemcase is a shop.
 | 
			
		||||
            if(type != Type.SHOWCASE) {
 | 
			
		||||
                
 | 
			
		||||
                // Get storage type.
 | 
			
		||||
                StorageType storageType = StorageType.valueOf(
 | 
			
		||||
                        this.file.getString(key + "shop.storage-type"));
 | 
			
		||||
                
 | 
			
		||||
                // Set storage type.
 | 
			
		||||
                itemcase.setStorageType(storageType);
 | 
			
		||||
                
 | 
			
		||||
                // If itemcase has finite storage.
 | 
			
		||||
                if(storageType == StorageType.FINITE) {
 | 
			
		||||
                    
 | 
			
		||||
                    // Deserialse inventory.
 | 
			
		||||
                    Inventory inventory = this.deserializeInventory(
 | 
			
		||||
                            this.file.getConfigurationSection(
 | 
			
		||||
                                    key + "shop.storage").getValues(true));
 | 
			
		||||
                    
 | 
			
		||||
                    // Set inventory.
 | 
			
		||||
                    itemcase.setStorage(inventory);
 | 
			
		||||
                }
 | 
			
		||||
                
 | 
			
		||||
                // If this itemcase buys.
 | 
			
		||||
                if(type == Type.SHOP_BUY || type == Type.SHOP_MULTI) {
 | 
			
		||||
 | 
			
		||||
                    // Get buy price.
 | 
			
		||||
                    double buyPrice = 
 | 
			
		||||
                            this.file.getDouble(key + "shop.buy-price");
 | 
			
		||||
                    
 | 
			
		||||
                    // Set buy price.
 | 
			
		||||
                    itemcase.setBuyPrice(buyPrice);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                // If this itemcase sells.
 | 
			
		||||
                if(type == Type.SHOP_SELL || type == Type.SHOP_MULTI) {
 | 
			
		||||
 | 
			
		||||
                    // Get sell price.
 | 
			
		||||
                    double sellPrice = 
 | 
			
		||||
                            this.file.getDouble(key + "shop.sell-price");
 | 
			
		||||
                    
 | 
			
		||||
                    // Set sell price.
 | 
			
		||||
                    itemcase.setSellPrice(sellPrice);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            // Add to list.
 | 
			
		||||
            itemcases.add(itemcase);
 | 
			
		||||
@@ -184,4 +278,79 @@ public class WorldFile extends ConfigurationFile {
 | 
			
		||||
        // Delete directory.
 | 
			
		||||
        FileUtils.deleteDirectory(fileReference);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Serialize the given inventory.
 | 
			
		||||
     * 
 | 
			
		||||
     * @param inventory Inventory.
 | 
			
		||||
     * @return Map.
 | 
			
		||||
     */
 | 
			
		||||
    private Map<String, Object> serializeInventory(Inventory inventory) {
 | 
			
		||||
        
 | 
			
		||||
        // Create map.
 | 
			
		||||
        Map<String, Object> map = new HashMap();
 | 
			
		||||
        
 | 
			
		||||
        // Set size.
 | 
			
		||||
        map.put("size", inventory.getSize());
 | 
			
		||||
        
 | 
			
		||||
        // Set name.
 | 
			
		||||
        map.put("name", inventory.getName());
 | 
			
		||||
        
 | 
			
		||||
        // Loop through all content slots.
 | 
			
		||||
        for(int i = 0; i < inventory.getSize(); i++) {
 | 
			
		||||
            
 | 
			
		||||
            // Check if content slot has any items.
 | 
			
		||||
            if (inventory.getItem(i) != null) {
 | 
			
		||||
                
 | 
			
		||||
                // Serialize itemstack in content slot.
 | 
			
		||||
                map.put("" + i, inventory.getItem(i).serialize());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Return map.
 | 
			
		||||
        return map;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Deserialize inventory from given map.
 | 
			
		||||
     * 
 | 
			
		||||
     * @param map Map.
 | 
			
		||||
     * @return Inventory.
 | 
			
		||||
     */
 | 
			
		||||
    private Inventory deserializeInventory(Map<String, Object> map) {
 | 
			
		||||
        
 | 
			
		||||
        // Get size.
 | 
			
		||||
        int size = Integer.parseInt((String) map.get("size"));
 | 
			
		||||
        
 | 
			
		||||
        // Get name.
 | 
			
		||||
        String name = (String) map.get("name");
 | 
			
		||||
        
 | 
			
		||||
        // Create inventory.
 | 
			
		||||
        Inventory inventory = Bukkit.createInventory(null, size, name);
 | 
			
		||||
        
 | 
			
		||||
        // For every map entry.
 | 
			
		||||
        for(Entry<String, Object> entry : map.entrySet()) {
 | 
			
		||||
            
 | 
			
		||||
            // Check entry is parameter.
 | 
			
		||||
            if(entry.getKey().equals("size") ||
 | 
			
		||||
                    entry.getKey().equals("name")) {
 | 
			
		||||
                
 | 
			
		||||
                // Skip.
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            // Get slot.
 | 
			
		||||
            int slot = Integer.parseInt(entry.getKey());
 | 
			
		||||
            
 | 
			
		||||
            // Deserialize item.
 | 
			
		||||
            ItemStack item = ItemStack.deserialize(
 | 
			
		||||
                    (Map<String, Object>) entry.getValue());
 | 
			
		||||
            
 | 
			
		||||
            // Set item in slot.
 | 
			
		||||
            inventory.setItem(slot, item);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Return inventory.
 | 
			
		||||
        return inventory;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -22,8 +22,6 @@ import java.io.IOException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map.Entry;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.Location;
 | 
			
		||||
import org.bukkit.OfflinePlayer;
 | 
			
		||||
@@ -116,7 +114,8 @@ public final class ItemcaseManager {
 | 
			
		||||
            OfflinePlayer owner) {
 | 
			
		||||
        
 | 
			
		||||
        // Create new itemcase instance.
 | 
			
		||||
        Itemcase itemcase = new Itemcase(itemStack, location, owner);
 | 
			
		||||
        Itemcase itemcase = new Itemcase(Itemcase.Type.SHOWCASE, itemStack,
 | 
			
		||||
                location, owner);
 | 
			
		||||
        
 | 
			
		||||
        // Add itemcase to the list.
 | 
			
		||||
        this.itemcases.add(itemcase);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user