PageRenderTime 30ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/common/buildcraft/lib/recipe/RecipeBuilderShaped.java

https://github.com/2xsaiko/BuildCraft
Java | 137 lines | 114 code | 18 blank | 5 comment | 25 complexity | 2d4224ca587cd555f1d0fb87ac6ac70c MD5 | raw file
  1/*
  2 * Copyright (c) 2017 SpaceToad and the BuildCraft team
  3 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
  4 * distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/
  5 */
  6
  7package buildcraft.lib.recipe;
  8
  9import java.util.ArrayList;
 10import java.util.List;
 11
 12import javax.annotation.Nonnull;
 13
 14import gnu.trove.map.hash.TCharObjectHashMap;
 15
 16import net.minecraft.block.Block;
 17import net.minecraft.item.Item;
 18import net.minecraft.item.ItemStack;
 19
 20import net.minecraftforge.fml.common.registry.ForgeRegistries;
 21import net.minecraftforge.oredict.ShapedOreRecipe;
 22
 23import buildcraft.lib.misc.StackUtil;
 24
 25public class RecipeBuilderShaped {
 26    @Nonnull
 27    private ItemStack result = StackUtil.EMPTY;
 28    private final List<String> shape = new ArrayList<>();
 29    private final TCharObjectHashMap<Object> objects = new TCharObjectHashMap<>();
 30
 31    public RecipeBuilderShaped add(String row) {
 32        if (shape.size() > 0 && shape.get(0).length() != row.length()) {
 33            throw new IllegalArgumentException("Badly sized row! (Other rows = " + shape.get(0).length() + ", given row = " + row.length() + ")");
 34        }
 35        shape.add(row);
 36        return this;
 37    }
 38
 39    public RecipeBuilderShaped map(char c, Object... values) {
 40        boolean put = false;
 41        for (Object v : values) {
 42            if (v != null && v != StackUtil.EMPTY) {
 43                if (v instanceof Item//
 44                    || v instanceof Block//
 45                    || v instanceof ItemStack//
 46                    || v instanceof String) {
 47                    if (!put) {
 48                        objects.put(c, v);
 49                        put = true;
 50                    }
 51                } else {
 52                    throw new IllegalArgumentException("Invalid " + v.getClass());
 53                }
 54            }
 55        }
 56        if (!put) {
 57            throw new IllegalArgumentException("Didn't find a non-null value!");
 58        }
 59        return this;
 60    }
 61
 62    public RecipeBuilderShaped setResult(@Nonnull ItemStack result) {
 63        this.result = result;
 64        return this;
 65    }
 66
 67    public Object[] createRecipeObjectArray() {
 68        Object[] objs = new Object[shape.size() + objects.size() * 2];
 69        int offset = 0;
 70        for (String s : shape) {
 71            objs[offset++] = s;
 72        }
 73        for (char c : objects.keys()) {
 74            objs[offset++] = c;
 75            objs[offset++] = objects.get(c);
 76        }
 77        return objs;
 78    }
 79
 80    public Object[] createRecipeObjectArrayNBT() {
 81        Object[] objs = new Object[shape.size() + objects.size() * 2];
 82        Object[] original = createRecipeObjectArray();
 83        for (int i = 0; i < objs.length; i++) {
 84            Object o = original[i];
 85            if (o instanceof ItemStack) {
 86                o = new IngredientNBTBC((ItemStack) o);
 87            }
 88            objs[i] = o;
 89        }
 90        return objs;
 91    }
 92
 93    public ShapedOreRecipe buildRotated() {
 94        int fromRows = shape.size();
 95        int toRows = shape.get(0).length();
 96        StringBuilder[] strings = new StringBuilder[toRows];
 97        for (int toRow = 0; toRow < toRows; toRow++) {
 98            strings[toRow] = new StringBuilder();
 99        }
100        for (String toAdd : shape) {
101            for (int toRow = 0; toRow < toRows; toRow++) {
102                strings[toRow].append(toAdd.charAt(toRow));
103            }
104        }
105        Object[] objs = new Object[toRows + objects.size() * 2];
106        int offset = 0;
107        for (StringBuilder string : strings) {
108            objs[offset++] = string.toString();
109        }
110        for (char c : objects.keys()) {
111            objs[offset++] = c;
112            objs[offset++] = objects.get(c);
113        }
114        return new ShapedOreRecipe(result.getItem().getRegistryName(), result, objs);
115    }
116
117    private void ensureValid() {
118        if (result.isEmpty()) {
119            throw new IllegalStateException("Result hasn't been set yet!");
120        }
121    }
122
123    public void register() {
124        ensureValid();
125        ForgeRegistries.RECIPES.register(new ShapedOreRecipe(result.getItem().getRegistryName(), result, createRecipeObjectArray()));
126    }
127
128    public void registerNbtAware() {
129        ensureValid();
130        ForgeRegistries.RECIPES.register(new ShapedOreRecipe(result.getItem().getRegistryName(), result, createRecipeObjectArrayNBT()).setRegistryName(result.getItem().getRegistryName()));
131    }
132
133    public void registerRotated() {
134        ensureValid();
135        ForgeRegistries.RECIPES.register(buildRotated().setRegistryName(result.getItem().getRegistryName()));
136    }
137}