Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package de.fgtech.pomo4ka.AuthMe.InventoryCache;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.util.Scanner;
  6.  
  7. import org.bukkit.inventory.ItemStack;
  8.  
  9. import de.fgtech.pomo4ka.AuthMe.Parameters.Settings;
  10. import java.util.ArrayList;
  11.  
  12. public class FlatfileCache {
  13.  
  14.     public FlatfileCache() {
  15.         final File folder = new File(Settings.CACHE_FOLDER);
  16.         if (!folder.exists()) {
  17.             folder.mkdirs();
  18.         }
  19.     }
  20.  
  21.     public void createCache(String playername, InventoryArmour invarm) {
  22.         final File file = new File(Settings.CACHE_FOLDER + "/" + playername
  23.                 + ".cache");
  24.  
  25.         if (file.exists()) {
  26.             return;
  27.         }
  28.  
  29.         FileWriter writer = null;
  30.         try {
  31.             file.createNewFile();
  32.  
  33.             writer = new FileWriter(file);
  34.  
  35.             ItemStack[] invstack = invarm.getInventory();
  36.  
  37.             for (int i = 0; i < invstack.length; i++) {
  38.  
  39.                 int itemid = 0;
  40.                 int amount = 0;
  41.                 int durability = 0;
  42.  
  43.                 if (invstack[i] != null) {
  44.                     itemid = invstack[i].getTypeId();
  45.                     amount = invstack[i].getAmount();
  46.                     durability = invstack[i].getDurability();
  47.                 }
  48.  
  49.                 writer.write("i" + ":" + itemid + ":" + amount + ":"
  50.                         + durability + "\r\n");
  51.                 writer.flush();
  52.             }
  53.  
  54.             ItemStack[] wearstack = invarm.getArmour();
  55.  
  56.             for (int i = 0; i < wearstack.length; i++) {
  57.                 int itemid = 0;
  58.                 int amount = 0;
  59.                 int durability = 0;
  60.  
  61.                 if (wearstack[i] != null) {
  62.                     itemid = wearstack[i].getTypeId();
  63.                     amount = wearstack[i].getAmount();
  64.                     durability = wearstack[i].getDurability();
  65.                 }
  66.  
  67.                 writer.write("w" + ":" + itemid + ":" + amount + ":"
  68.                         + durability + "\r\n");
  69.                 writer.flush();
  70.             }
  71.  
  72.             writer.close();
  73.         } catch (final Exception e) {
  74.             e.printStackTrace();
  75.         }
  76.     }
  77.  
  78.     public InventoryArmour readCache(String playername) {
  79.         final File file = new File(Settings.CACHE_FOLDER + "/" + playername
  80.                 + ".cache");
  81.  
  82.         ArrayList<ItemStack> stacki = new ArrayList<ItemStack>();
  83.         ArrayList<ItemStack> stacka = new ArrayList<ItemStack>();
  84.  
  85.         if (!file.exists()) {
  86.             return new InventoryArmour(stacki.toArray(new ItemStack[0]), stacka.toArray(new ItemStack[0]));
  87.         }
  88.  
  89.         Scanner reader = null;
  90.         try {
  91.             reader = new Scanner(file);
  92.  
  93.             while (reader.hasNextLine()) {
  94.                 final String line = reader.nextLine();
  95.  
  96.                 if (!line.contains(":")) {
  97.                     continue;
  98.                 }
  99.  
  100.                 final String[] in = line.split(":");
  101.  
  102.                 if (in.length != 4) {
  103.                     continue;
  104.                 }
  105.                 if (!in[0].equals("i") && !in[0].equals("w")) {
  106.                     continue;
  107.                 }
  108.  
  109.                 if (in[0].equals("i")) {
  110.                     stacki.add(new ItemStack(Integer.parseInt(in[1]),
  111.                             Integer.parseInt(in[2]), Short.parseShort((in[3]))));
  112.                 } else {
  113.                     stacka.add(new ItemStack(Integer.parseInt(in[1]),
  114.                             Integer.parseInt(in[2]), Short.parseShort((in[3]))));
  115.                 }
  116.  
  117.             }
  118.         } catch (final Exception e) {
  119.             e.printStackTrace();
  120.         } finally {
  121.             if (reader != null) {
  122.                 reader.close();
  123.             }
  124.         }
  125.         return new InventoryArmour(stacki.toArray(new ItemStack[0]), stacka.toArray(new ItemStack[0]));
  126.     }
  127.  
  128.     public void removeCache(String playername) {
  129.         final File file = new File(Settings.CACHE_FOLDER + "/" + playername
  130.                 + ".cache");
  131.  
  132.         if (file.exists()) {
  133.             file.delete();
  134.         }
  135.     }
  136.  
  137.     public boolean doesCacheExist(String playername) {
  138.         final File file = new File(Settings.CACHE_FOLDER + "/" + playername
  139.                 + ".cache");
  140.  
  141.         if (file.exists()) {
  142.             return true;
  143.         }
  144.         return false;
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement