mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-31 21:52:59 +00:00 
			
		
		
		
	Initial update to 1.12
- Convert most recipes to JSON - Add JSON factories for impostor and turtle recipes. - Several mappings changes - Migrate to Forge's new registry system
This commit is contained in:
		| @@ -5,18 +5,39 @@ | ||||
|  */ | ||||
|  | ||||
| package dan200.computercraft.shared.util; | ||||
|  | ||||
| import com.google.gson.JsonObject; | ||||
| import net.minecraft.inventory.InventoryCrafting; | ||||
| import net.minecraft.item.ItemStack; | ||||
| import net.minecraft.item.crafting.IRecipe; | ||||
| import net.minecraft.item.crafting.Ingredient; | ||||
| import net.minecraft.item.crafting.ShapedRecipes; | ||||
| import net.minecraft.util.JsonUtils; | ||||
| import net.minecraft.util.NonNullList; | ||||
| import net.minecraft.world.World; | ||||
| import net.minecraftforge.common.crafting.CraftingHelper; | ||||
| import net.minecraftforge.common.crafting.IRecipeFactory; | ||||
| import net.minecraftforge.common.crafting.JsonContext; | ||||
|  | ||||
| import javax.annotation.Nonnull; | ||||
|  | ||||
| public class ImpostorRecipe extends ShapedRecipes | ||||
| { | ||||
|     public ImpostorRecipe( int width, int height, ItemStack[] ingredients, @Nonnull ItemStack result ) | ||||
|     public ImpostorRecipe( @Nonnull String group, int width, int height, NonNullList<Ingredient> ingredients, @Nonnull ItemStack result ) | ||||
|     { | ||||
|         super( width, height, ingredients, result ); | ||||
|         super( group, width, height, ingredients, result ); | ||||
|     } | ||||
|      | ||||
|     public ImpostorRecipe( @Nonnull String group, int width, int height, ItemStack[] ingredients, @Nonnull ItemStack result ) | ||||
|     { | ||||
|         super( group, width, height, convert( ingredients ), result ); | ||||
|     } | ||||
|  | ||||
|     private static NonNullList<Ingredient> convert( ItemStack[] items ) | ||||
|     { | ||||
|         NonNullList<Ingredient> ingredients = NonNullList.withSize( items.length, Ingredient.EMPTY ); | ||||
|         for( int i = 0; i < items.length; i++ ) ingredients.set( i, Ingredient.fromStacks( items[ i ] ) ); | ||||
|         return ingredients; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -24,11 +45,23 @@ public class ImpostorRecipe extends ShapedRecipes | ||||
|     { | ||||
|         return false; | ||||
|     } | ||||
|      | ||||
|  | ||||
|     @Nonnull | ||||
|     @Override | ||||
|     public ItemStack getCraftingResult( @Nonnull InventoryCrafting _inventory ) | ||||
|     { | ||||
|         return ItemStack.EMPTY; | ||||
|     } | ||||
|  | ||||
|     public static class Factory implements IRecipeFactory | ||||
|     { | ||||
|         @Override | ||||
|         public IRecipe parse( JsonContext ctx, JsonObject json ) | ||||
|         { | ||||
|             String group = JsonUtils.getString( json, "group", "" ); | ||||
|             CraftingHelper.ShapedPrimer primer = RecipeUtil.getPrimer( ctx, json ); | ||||
|             ItemStack result = CraftingHelper.getItemStack( JsonUtils.getJsonObject( json, "result" ), ctx ); | ||||
|             return new ImpostorRecipe( group, primer.width, primer.height, primer.input, result ); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -6,20 +6,38 @@ | ||||
|  | ||||
| package dan200.computercraft.shared.util; | ||||
|  | ||||
| import com.google.gson.JsonObject; | ||||
| import net.minecraft.inventory.InventoryCrafting; | ||||
| import net.minecraft.item.ItemStack; | ||||
| import net.minecraft.item.crafting.IRecipe; | ||||
| import net.minecraft.item.crafting.Ingredient; | ||||
| import net.minecraft.item.crafting.ShapedRecipes; | ||||
| import net.minecraft.item.crafting.ShapelessRecipes; | ||||
| import net.minecraft.util.JsonUtils; | ||||
| import net.minecraft.util.NonNullList; | ||||
| import net.minecraft.world.World; | ||||
| import net.minecraftforge.common.crafting.IRecipeFactory; | ||||
| import net.minecraftforge.common.crafting.JsonContext; | ||||
|  | ||||
| import javax.annotation.Nonnull; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
|  | ||||
| public class ImpostorShapelessRecipe extends ShapelessRecipes | ||||
| { | ||||
|     public ImpostorShapelessRecipe( @Nonnull ItemStack result, ItemStack[] ingredients ) | ||||
|     public ImpostorShapelessRecipe( @Nonnull String group, @Nonnull ItemStack result, NonNullList<Ingredient> ingredients ) | ||||
|     { | ||||
|         super( result, new ArrayList<ItemStack>(Arrays.asList( ingredients ))); | ||||
|         super( group, result, ingredients ); | ||||
|     } | ||||
|  | ||||
|     public ImpostorShapelessRecipe( @Nonnull String group, @Nonnull ItemStack result, ItemStack[] ingredients ) | ||||
|     { | ||||
|         super( group, result, convert( ingredients ) ); | ||||
|     } | ||||
|  | ||||
|     private static NonNullList<Ingredient> convert( ItemStack[] items ) | ||||
|     { | ||||
|         NonNullList<Ingredient> ingredients = NonNullList.withSize( items.length, Ingredient.EMPTY ); | ||||
|         for( int i = 0; i < items.length; i++ ) ingredients.set( i, Ingredient.fromStacks( items[ i ] ) ); | ||||
|         return ingredients; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -30,8 +48,20 @@ public class ImpostorShapelessRecipe extends ShapelessRecipes | ||||
|  | ||||
|     @Nonnull | ||||
|     @Override | ||||
|     public ItemStack getCraftingResult( InventoryCrafting _inventory ) | ||||
|     public ItemStack getCraftingResult( InventoryCrafting inventory ) | ||||
|     { | ||||
|         return ItemStack.EMPTY; | ||||
|     } | ||||
|  | ||||
|     public static class Factory implements IRecipeFactory | ||||
|     { | ||||
|         @Override | ||||
|         public IRecipe parse( JsonContext context, JsonObject json ) | ||||
|         { | ||||
|             String group = JsonUtils.getString( json, "group", "" ); | ||||
|             NonNullList<Ingredient> ings = RecipeUtil.getIngredients( context, json ); | ||||
|             ItemStack itemstack = ShapedRecipes.deserializeItem( JsonUtils.getJsonObject( json, "result" ), true ); | ||||
|             return new ImpostorShapelessRecipe( group, itemstack, ings ); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
							
								
								
									
										105
									
								
								src/main/java/dan200/computercraft/shared/util/RecipeUtil.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										105
									
								
								src/main/java/dan200/computercraft/shared/util/RecipeUtil.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,105 @@ | ||||
| /* | ||||
|  * This file is part of ComputerCraft - http://www.computercraft.info | ||||
|  * Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission. | ||||
|  * Send enquiries to dratcliffe@gmail.com | ||||
|  */ | ||||
|  | ||||
| package dan200.computercraft.shared.util; | ||||
|  | ||||
| import com.google.common.collect.Maps; | ||||
| import com.google.common.collect.Sets; | ||||
| import com.google.gson.*; | ||||
| import net.minecraft.item.crafting.Ingredient; | ||||
| import net.minecraft.util.JsonUtils; | ||||
| import net.minecraft.util.NonNullList; | ||||
| import net.minecraftforge.common.crafting.CraftingHelper; | ||||
| import net.minecraftforge.common.crafting.JsonContext; | ||||
|  | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
|  | ||||
| public class RecipeUtil | ||||
| { | ||||
|     public static CraftingHelper.ShapedPrimer getPrimer( JsonContext context, JsonObject json ) | ||||
|     { | ||||
|         Map<Character, Ingredient> ingMap = Maps.newHashMap(); | ||||
|         for( Map.Entry<String, JsonElement> entry : JsonUtils.getJsonObject( json, "key" ).entrySet() ) | ||||
|         { | ||||
|             if( entry.getKey().length() != 1 ) | ||||
|             { | ||||
|                 throw new JsonSyntaxException( "Invalid key entry: '" + 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." ); | ||||
|             } | ||||
|  | ||||
|             ingMap.put( entry.getKey().charAt( 0 ), CraftingHelper.getIngredient( entry.getValue(), context ) ); | ||||
|         } | ||||
|  | ||||
|         ingMap.put( ' ', Ingredient.EMPTY ); | ||||
|  | ||||
|         JsonArray patternJ = JsonUtils.getJsonArray( json, "pattern" ); | ||||
|  | ||||
|         if( patternJ.size() == 0 ) | ||||
|             throw new JsonSyntaxException( "Invalid pattern: empty pattern not allowed" ); | ||||
|  | ||||
|         String[] pattern = new String[ patternJ.size() ]; | ||||
|         for( int x = 0; x < pattern.length; ++x ) | ||||
|         { | ||||
|             String line = JsonUtils.getString( patternJ.get( x ), "pattern[" + x + "]" ); | ||||
|             if( x > 0 && pattern[ 0 ].length() != line.length() ) | ||||
|             { | ||||
|                 throw new JsonSyntaxException( "Invalid pattern: each row must  be the same width" ); | ||||
|             } | ||||
|             pattern[ x ] = line; | ||||
|         } | ||||
|  | ||||
|         CraftingHelper.ShapedPrimer primer = new CraftingHelper.ShapedPrimer(); | ||||
|         primer.width = pattern[ 0 ].length(); | ||||
|         primer.height = pattern.length; | ||||
|         primer.mirrored = false; | ||||
|         primer.input = NonNullList.withSize( primer.width * primer.height, Ingredient.EMPTY ); | ||||
|  | ||||
|         Set<Character> keys = Sets.newHashSet( ingMap.keySet() ); | ||||
|         keys.remove( ' ' ); | ||||
|  | ||||
|         int x = 0; | ||||
|         for( String line : pattern ) | ||||
|         { | ||||
|             for( char chr : line.toCharArray() ) | ||||
|             { | ||||
|                 Ingredient ing = ingMap.get( chr ); | ||||
|                 if( ing == null ) | ||||
|                 { | ||||
|                     throw new JsonSyntaxException( "Pattern references symbol '" + chr + "' but it's not defined in the key" ); | ||||
|                 } | ||||
|                 primer.input.set( x++, ing ); | ||||
|                 keys.remove( chr ); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if( !keys.isEmpty() ) | ||||
|         { | ||||
|             throw new JsonSyntaxException( "Key defines symbols that aren't used in pattern: " + keys ); | ||||
|         } | ||||
|  | ||||
|         return primer; | ||||
|     } | ||||
|  | ||||
|     public static NonNullList<Ingredient> getIngredients( JsonContext context, JsonObject json ) | ||||
|     { | ||||
|         NonNullList<Ingredient> ings = NonNullList.create(); | ||||
|         for( JsonElement ele : JsonUtils.getJsonArray( json, "ingredients" ) ) | ||||
|         { | ||||
|             ings.add( CraftingHelper.getIngredient( ele, context ) ); | ||||
|         } | ||||
|  | ||||
|         if( ings.isEmpty() ) | ||||
|         { | ||||
|             throw new JsonParseException( "No ingredients for recipe" ); | ||||
|         } | ||||
|  | ||||
|         return ings; | ||||
|     } | ||||
| } | ||||
| @@ -36,27 +36,27 @@ public class WorldUtil | ||||
|  | ||||
|     public static Pair<Entity, Vec3d> rayTraceEntities( World world, Vec3d vecStart, Vec3d vecDir, double distance ) | ||||
|     { | ||||
|         Vec3d vecEnd = vecStart.addVector( vecDir.xCoord * distance, vecDir.yCoord * distance, vecDir.zCoord * distance ); | ||||
|         Vec3d vecEnd = vecStart.addVector( vecDir.x * distance, vecDir.y * distance, vecDir.z * distance ); | ||||
|  | ||||
|         // Raycast for blocks | ||||
|         RayTraceResult result = world.rayTraceBlocks( vecStart.addVector(0.0,0.0,0.0), vecEnd.addVector(0.0,0.0,0.0) ); | ||||
|         if( result != null && result.typeOfHit == RayTraceResult.Type.BLOCK ) | ||||
|         { | ||||
|             distance = vecStart.distanceTo( result.hitVec ); | ||||
|             vecEnd = vecStart.addVector( vecDir.xCoord * distance, vecDir.yCoord * distance, vecDir.zCoord * distance ); | ||||
|             vecEnd = vecStart.addVector( vecDir.x * distance, vecDir.y * distance, vecDir.z * distance ); | ||||
|         } | ||||
|  | ||||
|         // Check for entities | ||||
|         float xStretch = (Math.abs(vecDir.xCoord) > 0.25f) ? 0.0f : 1.0f; | ||||
|         float yStretch = (Math.abs(vecDir.yCoord) > 0.25f) ? 0.0f : 1.0f; | ||||
|         float zStretch = (Math.abs(vecDir.zCoord) > 0.25f) ? 0.0f : 1.0f; | ||||
|         float xStretch = (Math.abs(vecDir.x) > 0.25f) ? 0.0f : 1.0f; | ||||
|         float yStretch = (Math.abs(vecDir.y) > 0.25f) ? 0.0f : 1.0f; | ||||
|         float zStretch = (Math.abs(vecDir.z) > 0.25f) ? 0.0f : 1.0f; | ||||
|         AxisAlignedBB bigBox = new AxisAlignedBB( | ||||
|             Math.min(vecStart.xCoord, vecEnd.xCoord) - 0.375f * xStretch, | ||||
|             Math.min(vecStart.yCoord, vecEnd.yCoord) - 0.375f * yStretch, | ||||
|             Math.min(vecStart.zCoord, vecEnd.zCoord) - 0.375f * zStretch, | ||||
|             Math.max(vecStart.xCoord, vecEnd.xCoord) + 0.375f * xStretch, | ||||
|             Math.max(vecStart.yCoord, vecEnd.yCoord) + 0.375f * yStretch, | ||||
|             Math.max(vecStart.zCoord, vecEnd.zCoord) + 0.375f * zStretch | ||||
|             Math.min(vecStart.x, vecEnd.x) - 0.375f * xStretch, | ||||
|             Math.min(vecStart.y, vecEnd.y) - 0.375f * yStretch, | ||||
|             Math.min(vecStart.z, vecEnd.z) - 0.375f * zStretch, | ||||
|             Math.max(vecStart.x, vecEnd.x) + 0.375f * xStretch, | ||||
|             Math.max(vecStart.y, vecEnd.y) + 0.375f * yStretch, | ||||
|             Math.max(vecStart.z, vecEnd.z) + 0.375f * zStretch | ||||
|         ); | ||||
|  | ||||
|         Entity closest = null; | ||||
| @@ -79,7 +79,7 @@ public class WorldUtil | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             if( littleBox.isVecInside( vecStart ) ) | ||||
|             if( littleBox.contains( vecStart ) ) | ||||
|             { | ||||
|                 closest = entity; | ||||
|                 closestDist = 0.0f; | ||||
| @@ -96,7 +96,7 @@ public class WorldUtil | ||||
|                     closestDist = dist; | ||||
|                 } | ||||
|             } | ||||
|             else if( littleBox.intersectsWith( bigBox ) ) | ||||
|             else if( littleBox.intersects( bigBox ) ) | ||||
|             { | ||||
|                 if( closest == null ) | ||||
|                 { | ||||
| @@ -107,7 +107,7 @@ public class WorldUtil | ||||
|         } | ||||
|         if( closest != null && closestDist <= distance ) | ||||
|         { | ||||
|             Vec3d closestPos = vecStart.addVector( vecDir.xCoord * closestDist, vecDir.yCoord * closestDist, vecDir.zCoord * closestDist ); | ||||
|             Vec3d closestPos = vecStart.addVector( vecDir.x * closestDist, vecDir.y * closestDist, vecDir.z * closestDist ); | ||||
|             return Pair.of( closest, closestPos ); | ||||
|         } | ||||
|         return null; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 SquidDev
					SquidDev