Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Decompiled by Procyon v0.5.30
- //
- package net.mcdecimation.mod.looting;
- import com.google.common.base.Splitter;
- import com.google.common.primitives.Ints;
- import com.google.common.reflect.TypeToken;
- import com.google.gson.Gson;
- import net.mcdecimation.mod.util.ItemStacks;
- import net.minecraft.block.Block;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.Reader;
- import java.io.UncheckedIOException;
- import java.lang.reflect.Type;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.LinkOption;
- import java.nio.file.Path;
- import java.util.HashMap;
- import java.util.List;
- public class BlockLootManager
- {
- private static final Splitter AT_SPLITTER;
- private final HashMap<BlockMetaPair, LootCollection> collections;
- public BlockLootManager() {
- this.collections = new HashMap<BlockMetaPair, LootCollection>();
- }
- public void read(final Path path) throws IOException {
- this.collections.clear();
- if (!Files.exists(path, new LinkOption[0])) {
- return;
- }
- if (!Files.isRegularFile(path, new LinkOption[0])) {
- throw new IOException("Invalid config file, not regular.");
- }
- try (final BufferedReader in = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
- final Type type = new TypeToken<List<DeserializedLootEntry>>() {}.getType();
- final List<DeserializedLootEntry> entries = (List<DeserializedLootEntry>)new Gson().fromJson((Reader)in, type);
- for (final DeserializedLootEntry entry : entries) {
- BlockMetaPair pair;
- if (entry.block.contains("@")) {
- final List<String> split = (List<String>)BlockLootManager.AT_SPLITTER.splitToList((CharSequence)entry.block);
- if (split.size() != 2) {
- throw new IOException("Invalid block definition: " + entry.block);
- }
- final Integer meta = Ints.tryParse((String)split.get(1));
- if (meta == null || meta < 0 || meta >= 16) {
- throw new IOException("Invalid metadata: " + split.get(1));
- }
- final Block block = this.tryGetBlock(split.get(0));
- pair = new BlockMetaPair(block, (byte)(Object)meta);
- }
- else {
- pair = new BlockMetaPair(this.tryGetBlock(entry.block), (byte)(-1));
- }
- LootEntry[] stacks;
- try {
- final UncheckedIOException ex = null;
- stacks = entry.loot.stream().map(definition -> {
- try {
- return ItemStacks.parse(definition);
- }
- catch (ItemStacks.InvalidStackDefinition e) {
- new UncheckedIOException(new IOException(e));
- throw ex;
- }
- }).map(s -> new LootEntry(s, IntRange.of(1, s.stackSize))).toArray(LootEntry[]::new);
- }
- catch (UncheckedIOException e2) {
- throw e2.getCause();
- }
- final LootCollection collection = new LootCollection(IntRange.of(1, entry.stacks), stacks);
- this.collections.put(pair, collection);
- }
- }
- }
- public LootCollection getCollection(final Block block, final int meta) {
- LootCollection collection = this.collections.get(new BlockMetaPair(block, (byte)meta));
- if (collection == null) {
- collection = this.collections.get(new BlockMetaPair(block, (byte)(-1)));
- }
- return collection;
- }
- private Block tryGetBlock(final String name) throws IOException {
- final Block block = Block.getBlockFromName(name);
- if (block == null) {
- throw new IOException("Invalid or unknown block name: " + name);
- }
- return block;
- }
- static {
- AT_SPLITTER = Splitter.on('@');
- }
- private static final class BlockMetaPair
- {
- final Block block;
- final byte meta;
- BlockMetaPair(final Block block, final byte meta) {
- this.block = block;
- this.meta = meta;
- }
- @Override
- public boolean equals(final Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || this.getClass() != o.getClass()) {
- return false;
- }
- final BlockMetaPair that = (BlockMetaPair)o;
- return this.meta == that.meta && this.block == that.block;
- }
- @Override
- public int hashCode() {
- int result = System.identityHashCode(this.block);
- result = 31 * result + this.meta;
- return result;
- }
- }
- private static final class DeserializedLootEntry
- {
- String block;
- int stacks;
- List<String> loot;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement