Package com.kellerkindt.scs.storage

Source Code of com.kellerkindt.scs.storage.ShowCaseImport

/**
* ShowCaseStandalone
* Copyright (C) 2012 Kellerkindt <copyright at kellerkindt.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.kellerkindt.scs.storage;

/**
  * @author Sorklin <sorklin at gmail.com>
  */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;

import com.kellerkindt.scs.ShowCaseStandalone;
import com.kellerkindt.scs.interfaces.ShopHandler;
import com.kellerkindt.scs.interfaces.StorageHandler;
import com.kellerkindt.scs.shops.BuyShop;
import com.kellerkindt.scs.shops.DisplayShop;
import com.kellerkindt.scs.shops.SellShop;
import com.kellerkindt.scs.shops.Shop;
import com.kellerkindt.scs.utilities.Utilities;

public class ShowCaseImport implements StorageHandler {
 
  private static final String      PATH_TO_DATAFILE  = "showcases.csv";
 
    private ShowCaseStandalone       scs;
    private  File            datafile;
    private List<Shop>          shops  = new ArrayList<Shop>();
   
    public ShowCaseImport(ShowCaseStandalone instance) {
        this.scs     = instance;
        this.datafile  = new File(instance.getDataFolder(), PATH_TO_DATAFILE);
    }
   
    public boolean fileExists () {
      return datafile.exists();
    }
   
    /**
     * Taken from ShowCase by Narrowtux.
     */
     public void load() throws IOException {
       // first clear
       this.shops.clear();

        if (datafile.exists()) {
            FileInputStream input;
            try {
                input = new FileInputStream(datafile.getAbsoluteFile());
                InputStreamReader ir = new InputStreamReader(input);
                BufferedReader r = new BufferedReader(ir);
                String locline;
               
                int x, y, z;
                Material type;
                short matdata;
                String player;
                World world;
                String showtype;
                String data;
               
                while (true) {
                    locline = r.readLine();
                    if (locline == null) {
                        break;
                    }
                    String line[] = locline.split(",");
                    if (line.length == 9 || line.length == 10) {
                            x = Integer.valueOf(line[0]);
                            y = Integer.valueOf(line[1]);
                            z = Integer.valueOf(line[2]);
                            type = Material.getMaterial(Integer.valueOf(line[3]));
                            matdata = Short.valueOf(line[4]);
                            player = line[5];
                            world = scs.getServer().getWorld(line[6]);
                            showtype = line[7].toLowerCase();
                            data = line[line.length - 1];
                    } else {
                            continue;
                    }
                    Location loc = new Location(world, x, y, z);
                    Shop p = argumentsToShop(loc, type, matdata, player, showtype, data);
                    if(p != null)
                        shops.add(p);
                }
               
                r.close();
               
            } catch (Exception e) {
                    e.printStackTrace();
            }
        }
    }
   
    private Shop argumentsToShop (Location loc, Material mat, short matdata, String owner, String type, String data) {
           
        int amount = 1;
        int maxAmount = 1;
        ItemStack is;
        double price = 1.0;
       

        String[] args = data.split(";");
        Shop shop  = null;

        if(loc == null) {
            ShowCaseStandalone.slog(Level.INFO, "Showcase import: location is null, " + getClass().getName());
            return null;
        }
       

        if(owner.equals("")){
            ShowCaseStandalone.slog(Level.INFO, "Could not owner for shop, " + getClass().getName());
            return null;
        }
       

        try {
            is   = Utilities.getItemStackFromString(mat + ":" + matdata);
        } catch (IOException ioe) {
            ShowCaseStandalone.slog(Level.INFO, "Could not load materials for shop");
            ioe.printStackTrace();
            return null;
        }
       
       
        //Based on shop type, I'm going to set basic parameters, then try to
        //parse the data into those parameters.  If anything fails, I'll go
        //with the default parameters.
       
        price = 1.0;
        if (type.equalsIgnoreCase("infinite")) {
            //data = String.valueOf(price)
            //Try to parse, but ignore if I can't.
            if(data != "")
                try {
                    price = Double.parseDouble(data);
                } catch (NumberFormatException nfe) {}
           
            // new shop
            shop = new SellShop(UUID.randomUUID(), owner, loc, is);
            shop.setUnlimited(true);
           
           
        } else if (type.equalsIgnoreCase("finite")) {
            //data = itemAmount + ";" + pricePerItem; FINITE
            if(args.length > 1)
                try {
                    amount = Integer.parseInt(args[0]);
                    price = Double.parseDouble(args[1]);
                } catch (Exception e) {}
           
            // new shop
            shop = new SellShop(UUID.randomUUID(), owner, loc, is);
           
        } else if (type.equalsIgnoreCase("exchange")) {
            amount = 0;
            //data =  Exchange-type;Exchange-data;buy-amount;exchange-amount;exchange-rate-left;exchange-rate-right
            if(args.length >= 3)
                try {
                    maxAmount = Integer.parseInt(args[2]);
                } catch (Exception e) {}
           
            // new shop
            shop = new BuyShop(UUID.randomUUID(), owner, loc, is);
            ((BuyShop)shop).setMaxAmount(maxAmount);
           
        } else if (type.equalsIgnoreCase("basic") || type.equalsIgnoreCase("tutorial")) {
            amount = 0;
            price = 0.0;
            //No need to parse the data.
           
            // new shop
            shop = new DisplayShop(UUID.randomUUID(), owner, loc, is);
           
        } else {
            ShowCaseStandalone.slog(Level.INFO, "Could not get activity for shop, " + getClass().getName());
            return null;
        }


        // add the rest of the data
      shop.setAmount    (amount      );
      shop.setItemStack  (is        );
      shop.setPrice    (price      );
      shop.setOwner    (owner      );
      
        return shop;
    }

  /**
   * @see com.kellerkindt.scs.interfaces.StorageHandler#load(com.kellerkindt.scs.interfaces.ShopHandler)
   */
  @Override
  public void load(ShopHandler handler) throws IOException {
    load();
   
    handler.addAll(shops);
  }

  /**
   * @see com.kellerkindt.scs.interfaces.StorageHandler#save(com.kellerkindt.scs.interfaces.ShopHandler)
   */
  @Override
  public void save(ShopHandler handler) throws IOException {
    throw new IOException ("NOT SUPPORTED");
  }

  /**
   * @see com.kellerkindt.scs.interfaces.StorageHandler#flush()
   */
  @Override
  public void flush() throws IOException {
   
  }


}
TOP

Related Classes of com.kellerkindt.scs.storage.ShowCaseImport

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.