Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.16 KB | None | 0 0
  1. //
  2. // Decompiled by Procyon v0.5.30
  3. //
  4.  
  5. package net.mcdecimation.mod.looting;
  6.  
  7. import com.google.common.base.Splitter;
  8. import com.google.common.primitives.Ints;
  9. import com.google.common.reflect.TypeToken;
  10. import com.google.gson.Gson;
  11. import net.mcdecimation.mod.util.ItemStacks;
  12. import net.minecraft.block.Block;
  13.  
  14. import java.io.BufferedReader;
  15. import java.io.IOException;
  16. import java.io.Reader;
  17. import java.io.UncheckedIOException;
  18. import java.lang.reflect.Type;
  19. import java.nio.charset.StandardCharsets;
  20. import java.nio.file.Files;
  21. import java.nio.file.LinkOption;
  22. import java.nio.file.Path;
  23. import java.util.HashMap;
  24. import java.util.List;
  25.  
  26. public class BlockLootManager
  27. {
  28.     private static final Splitter AT_SPLITTER;
  29.     private final HashMap<BlockMetaPair, LootCollection> collections;
  30.    
  31.     public BlockLootManager() {
  32.         this.collections = new HashMap<BlockMetaPair, LootCollection>();
  33.     }
  34.    
  35.     public void read(final Path path) throws IOException {
  36.         this.collections.clear();
  37.         if (!Files.exists(path, new LinkOption[0])) {
  38.             return;
  39.         }
  40.         if (!Files.isRegularFile(path, new LinkOption[0])) {
  41.             throw new IOException("Invalid config file, not regular.");
  42.         }
  43.         try (final BufferedReader in = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
  44.             final Type type = new TypeToken<List<DeserializedLootEntry>>() {}.getType();
  45.             final List<DeserializedLootEntry> entries = (List<DeserializedLootEntry>)new Gson().fromJson((Reader)in, type);
  46.             for (final DeserializedLootEntry entry : entries) {
  47.                 BlockMetaPair pair;
  48.                 if (entry.block.contains("@")) {
  49.                     final List<String> split = (List<String>)BlockLootManager.AT_SPLITTER.splitToList((CharSequence)entry.block);
  50.                     if (split.size() != 2) {
  51.                         throw new IOException("Invalid block definition: " + entry.block);
  52.                     }
  53.                     final Integer meta = Ints.tryParse((String)split.get(1));
  54.                     if (meta == null || meta < 0 || meta >= 16) {
  55.                         throw new IOException("Invalid metadata: " + split.get(1));
  56.                     }
  57.                     final Block block = this.tryGetBlock(split.get(0));
  58.                     pair = new BlockMetaPair(block, (byte)(Object)meta);
  59.                 }
  60.                 else {
  61.                     pair = new BlockMetaPair(this.tryGetBlock(entry.block), (byte)(-1));
  62.                 }
  63.                 LootEntry[] stacks;
  64.                 try {
  65.                     final UncheckedIOException ex = null;
  66.                     stacks = entry.loot.stream().map(definition -> {
  67.                         try {
  68.                             return ItemStacks.parse(definition);
  69.                         }
  70.                         catch (ItemStacks.InvalidStackDefinition e) {
  71.                             new UncheckedIOException(new IOException(e));
  72.                             throw ex;
  73.                         }
  74.                     }).map(s -> new LootEntry(s, IntRange.of(1, s.stackSize))).toArray(LootEntry[]::new);
  75.                 }
  76.                 catch (UncheckedIOException e2) {
  77.                     throw e2.getCause();
  78.                 }
  79.                 final LootCollection collection = new LootCollection(IntRange.of(1, entry.stacks), stacks);
  80.                 this.collections.put(pair, collection);
  81.             }
  82.         }
  83.     }
  84.    
  85.     public LootCollection getCollection(final Block block, final int meta) {
  86.         LootCollection collection = this.collections.get(new BlockMetaPair(block, (byte)meta));
  87.         if (collection == null) {
  88.             collection = this.collections.get(new BlockMetaPair(block, (byte)(-1)));
  89.         }
  90.         return collection;
  91.     }
  92.    
  93.     private Block tryGetBlock(final String name) throws IOException {
  94.         final Block block = Block.getBlockFromName(name);
  95.         if (block == null) {
  96.             throw new IOException("Invalid or unknown block name: " + name);
  97.         }
  98.         return block;
  99.     }
  100.    
  101.     static {
  102.         AT_SPLITTER = Splitter.on('@');
  103.     }
  104.    
  105.     private static final class BlockMetaPair
  106.     {
  107.         final Block block;
  108.         final byte meta;
  109.        
  110.         BlockMetaPair(final Block block, final byte meta) {
  111.             this.block = block;
  112.             this.meta = meta;
  113.         }
  114.        
  115.         @Override
  116.         public boolean equals(final Object o) {
  117.             if (this == o) {
  118.                 return true;
  119.             }
  120.             if (o == null || this.getClass() != o.getClass()) {
  121.                 return false;
  122.             }
  123.             final BlockMetaPair that = (BlockMetaPair)o;
  124.             return this.meta == that.meta && this.block == that.block;
  125.         }
  126.        
  127.         @Override
  128.         public int hashCode() {
  129.             int result = System.identityHashCode(this.block);
  130.             result = 31 * result + this.meta;
  131.             return result;
  132.         }
  133.     }
  134.    
  135.     private static final class DeserializedLootEntry
  136.     {
  137.         String block;
  138.         int stacks;
  139.         List<String> loot;
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement