Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WorkbenchRecipeShaped implements IRecipe<IInventory> {
- public static final int recipeWidth = 4;
- public static final int recipeHeight = 4;
- private final NonNullList<Ingredient> recipeItems;
- private final ItemStack result;
- private final ResourceLocation recipeId;
- public WorkbenchRecipeShaped(ResourceLocation recipeId, NonNullList<Ingredient> recipeItemsIn, ItemStack result) {
- this.recipeId = recipeId;
- this.recipeItems = recipeItemsIn;
- this.result = result;
- }
- @Override public boolean matches(IInventory inv, World worldIn) {
- for (int i = 0 ; i < recipeWidth ; i++) {
- for (int j = 0 ; j < recipeHeight ; j++) {
- Ingredient ing = recipeItems.get(j + i * recipeWidth);
- if (!ing.test(inv.getStackInSlot(j + i * recipeWidth + 1))) return false;
- }
- }
- return true;
- }
- @Override public ItemStack getCraftingResult(IInventory inv) {
- return this.result.copy();
- }
- @Override public boolean canFit(int width, int height) {
- return width * height <= recipeHeight * recipeHeight;
- }
- @Override public ItemStack getRecipeOutput() {
- return this.result;
- }
- @Override public ResourceLocation getId() {
- return this.recipeId;
- }
- @Override public ItemStack getIcon() {
- return new ItemStack(ModBlocks.CELESTIAL_WORKBENCH_ITEM.get());
- }
- @Override public IRecipeSerializer<?> getSerializer() {
- return ModRecipes.WORKBENCH_RECIPE_SHAPED_SERIALIZER.get();
- }
- @Override public IRecipeType<?> getType() {
- return ModRecipes.WORKBENCH_RECIPE_SHAPED_RECIPE_TYPE;
- }
- private static NonNullList<Ingredient> deserializeIngredients(String[] pattern, Map<String, Ingredient> keys, int patternWidth, int patternHeight) {
- NonNullList<Ingredient> nonnulllist = NonNullList.withSize(patternWidth * patternHeight, Ingredient.EMPTY);
- Set<String> set = Sets.newHashSet(keys.keySet());
- set.remove(" ");
- for (int i = 0 ; i < pattern.length ; ++i) {
- for (int j = 0 ; j < pattern[i].length() ; ++j) {
- String s = pattern[i].substring(j, j + 1);
- Ingredient ingredient = keys.get(s);
- if (ingredient == null) {
- throw new JsonSyntaxException("Pattern references symbol '" + s + "' but it's not defined in the key");
- }
- set.remove(s);
- nonnulllist.set(j + patternWidth * i, ingredient);
- }
- }
- if (!set.isEmpty()) {
- throw new JsonSyntaxException("Key defines symbols that aren't used in pattern: " + set);
- }
- else {
- return nonnulllist;
- }
- }
- @VisibleForTesting
- static String[] shrink(String... toShrink) {
- int i = Integer.MAX_VALUE;
- int j = 0;
- int k = 0;
- int l = 0;
- for (int i1 = 0 ; i1 < toShrink.length ; ++i1) {
- String s = toShrink[i1];
- i = Math.min(i, firstNonSpace(s));
- int j1 = lastNonSpace(s);
- j = Math.max(j, j1);
- if (j1 < 0) {
- if (k == i1) {
- ++k;
- }
- ++l;
- }
- else {
- l = 0;
- }
- }
- if (toShrink.length == l) {
- return new String[0];
- }
- else {
- String[] astring = new String[toShrink.length - l - k];
- for (int k1 = 0 ; k1 < astring.length ; ++k1) {
- astring[k1] = toShrink[k1 + k].substring(i, j + 1);
- }
- return astring;
- }
- }
- private static int firstNonSpace(String str) {
- int i;
- for (i = 0; i < str.length() && str.charAt(i) == ' ' ; ++i) {
- }
- return i;
- }
- private static int lastNonSpace(String str) {
- int i;
- for (i = str.length() - 1; i >= 0 && str.charAt(i) == ' ' ; --i) {
- }
- return i;
- }
- private static String[] patternFromJson(JsonArray jsonArr) {
- String[] astring = new String[jsonArr.size()];
- for (int i = 0 ; i < astring.length ; ++i) {
- String s = JSONUtils.getString(jsonArr.get(i), "pattern[" + i + "]");
- if (i > 0 && astring[0].length() != s.length()) {
- throw new JsonSyntaxException("Invalid pattern: each row must be the same width");
- }
- astring[i] = s;
- }
- return astring;
- }
- private static Map<String, Ingredient> deserializeKey(JsonObject json) {
- Map<String, Ingredient> map = Maps.newHashMap();
- for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
- if (entry.getKey().length() != 1) {
- throw new JsonSyntaxException("Invalid key entry: '" + (String) entry.getKey() + "' is an invalid symbol (must be 1 character only).");
- }
- if (" ".equals(entry.getKey())) {
- throw new JsonSyntaxException("Invalid key entry: ' ' is a reserved symbol.");
- }
- map.put(entry.getKey(), Ingredient.deserialize(entry.getValue()));
- }
- map.put(" ", Ingredient.EMPTY);
- return map;
- }
- public static ItemStack deserializeItem(JsonObject object) {
- String s = JSONUtils.getString(object, "item");
- Item item = Registry.ITEM.getOptional(new ResourceLocation(s)).orElseThrow(() -> {
- return new JsonSyntaxException("Unknown item '" + s + "'");
- });
- if (object.has("data")) {
- throw new JsonParseException("Disallowed data tag found");
- }
- else {
- int i = JSONUtils.getInt(object, "count", 1);
- return net.minecraftforge.common.crafting.CraftingHelper.getItemStack(object, true);
- }
- }
- public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<WorkbenchRecipeShaped> {
- private static final int width = WorkbenchRecipeShaped.recipeWidth;
- private static final int height = WorkbenchRecipeShaped.recipeHeight;
- @Override public WorkbenchRecipeShaped read(ResourceLocation recipeId, JsonObject json) {
- Map<String, Ingredient> map = WorkbenchRecipeShaped.deserializeKey(JSONUtils.getJsonObject(json, "key"));
- String[] pattern = WorkbenchRecipeShaped.shrink(WorkbenchRecipeShaped.patternFromJson(JSONUtils.getJsonArray(json, "pattern")));
- NonNullList<Ingredient> inputs = WorkbenchRecipeShaped.deserializeIngredients(pattern, map, width, height);
- ItemStack output = WorkbenchRecipeShaped.deserializeItem(JSONUtils.getJsonObject(json, "result"));
- return new WorkbenchRecipeShaped(recipeId, inputs, output);
- }
- @Nullable @Override public WorkbenchRecipeShaped read(ResourceLocation recipeId, PacketBuffer buffer) {
- NonNullList<Ingredient> inputs = NonNullList.withSize(width * height, Ingredient.EMPTY);
- for (int i = 0 ; i < inputs.size() ; i++) {
- inputs.set(i, Ingredient.read(buffer));
- }
- ItemStack result = buffer.readItemStack();
- return new WorkbenchRecipeShaped(recipeId, inputs, result);
- }
- @Override public void write(PacketBuffer buffer, WorkbenchRecipeShaped recipe) {
- buffer.writeVarInt(width);
- buffer.writeVarInt(height);
- for (Ingredient ing : recipe.getIngredients())
- ing.write(buffer);
- buffer.writeItemStack(recipe.result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment