PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.0
  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. package buildcraft.lib.recipe;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import javax.annotation.Nonnull;
  10. import gnu.trove.map.hash.TCharObjectHashMap;
  11. import net.minecraft.block.Block;
  12. import net.minecraft.item.Item;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraftforge.fml.common.registry.ForgeRegistries;
  15. import net.minecraftforge.oredict.ShapedOreRecipe;
  16. import buildcraft.lib.misc.StackUtil;
  17. public class RecipeBuilderShaped {
  18. @Nonnull
  19. private ItemStack result = StackUtil.EMPTY;
  20. private final List<String> shape = new ArrayList<>();
  21. private final TCharObjectHashMap<Object> objects = new TCharObjectHashMap<>();
  22. public RecipeBuilderShaped add(String row) {
  23. if (shape.size() > 0 && shape.get(0).length() != row.length()) {
  24. throw new IllegalArgumentException("Badly sized row! (Other rows = " + shape.get(0).length() + ", given row = " + row.length() + ")");
  25. }
  26. shape.add(row);
  27. return this;
  28. }
  29. public RecipeBuilderShaped map(char c, Object... values) {
  30. boolean put = false;
  31. for (Object v : values) {
  32. if (v != null && v != StackUtil.EMPTY) {
  33. if (v instanceof Item//
  34. || v instanceof Block//
  35. || v instanceof ItemStack//
  36. || v instanceof String) {
  37. if (!put) {
  38. objects.put(c, v);
  39. put = true;
  40. }
  41. } else {
  42. throw new IllegalArgumentException("Invalid " + v.getClass());
  43. }
  44. }
  45. }
  46. if (!put) {
  47. throw new IllegalArgumentException("Didn't find a non-null value!");
  48. }
  49. return this;
  50. }
  51. public RecipeBuilderShaped setResult(@Nonnull ItemStack result) {
  52. this.result = result;
  53. return this;
  54. }
  55. public Object[] createRecipeObjectArray() {
  56. Object[] objs = new Object[shape.size() + objects.size() * 2];
  57. int offset = 0;
  58. for (String s : shape) {
  59. objs[offset++] = s;
  60. }
  61. for (char c : objects.keys()) {
  62. objs[offset++] = c;
  63. objs[offset++] = objects.get(c);
  64. }
  65. return objs;
  66. }
  67. public Object[] createRecipeObjectArrayNBT() {
  68. Object[] objs = new Object[shape.size() + objects.size() * 2];
  69. Object[] original = createRecipeObjectArray();
  70. for (int i = 0; i < objs.length; i++) {
  71. Object o = original[i];
  72. if (o instanceof ItemStack) {
  73. o = new IngredientNBTBC((ItemStack) o);
  74. }
  75. objs[i] = o;
  76. }
  77. return objs;
  78. }
  79. public ShapedOreRecipe buildRotated() {
  80. int fromRows = shape.size();
  81. int toRows = shape.get(0).length();
  82. StringBuilder[] strings = new StringBuilder[toRows];
  83. for (int toRow = 0; toRow < toRows; toRow++) {
  84. strings[toRow] = new StringBuilder();
  85. }
  86. for (String toAdd : shape) {
  87. for (int toRow = 0; toRow < toRows; toRow++) {
  88. strings[toRow].append(toAdd.charAt(toRow));
  89. }
  90. }
  91. Object[] objs = new Object[toRows + objects.size() * 2];
  92. int offset = 0;
  93. for (StringBuilder string : strings) {
  94. objs[offset++] = string.toString();
  95. }
  96. for (char c : objects.keys()) {
  97. objs[offset++] = c;
  98. objs[offset++] = objects.get(c);
  99. }
  100. return new ShapedOreRecipe(result.getItem().getRegistryName(), result, objs);
  101. }
  102. private void ensureValid() {
  103. if (result.isEmpty()) {
  104. throw new IllegalStateException("Result hasn't been set yet!");
  105. }
  106. }
  107. public void register() {
  108. ensureValid();
  109. ForgeRegistries.RECIPES.register(new ShapedOreRecipe(result.getItem().getRegistryName(), result, createRecipeObjectArray()));
  110. }
  111. public void registerNbtAware() {
  112. ensureValid();
  113. ForgeRegistries.RECIPES.register(new ShapedOreRecipe(result.getItem().getRegistryName(), result, createRecipeObjectArrayNBT()).setRegistryName(result.getItem().getRegistryName()));
  114. }
  115. public void registerRotated() {
  116. ensureValid();
  117. ForgeRegistries.RECIPES.register(buildRotated().setRegistryName(result.getItem().getRegistryName()));
  118. }
  119. }