YTMango

Custom JEI Categories [KubeJS Additions v4.2.0+]

Apr 14th, 2024 (edited)
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.77 KB | Source Code | 0 0
  1. /**
  2.  * @title Custom JEI Categories [KubeJS Additions v4.2.0+]
  3.  * @author pietheniceguy
  4.  * @source https://discord.com/channels/303440391124942858/1229177615978594334
  5.  */
  6.  
  7. JEIAddedEvents.registerCategories((event) => {
  8.   // Register a new CustomCategory with the id "kubejsadditions:painful_blocks".
  9.   event.custom("kubejsadditions:painful_blocks", (category) => {
  10.     const {
  11.       jeiHelpers,
  12.       jeiHelpers: { guiHelper },
  13.     } = category;
  14.  
  15.     // Set the title of the category to "Painful Blocks".
  16.     category
  17.       .title("Painful Blocks")
  18.       // Set the background of the category to a blank 100x50 drawable canvas.
  19.       .background(guiHelper.createBlankDrawable(100, 50))
  20.       // Set the icon of the category to a cactus item.
  21.       .icon(guiHelper.createDrawableItemStack(Item.of("minecraft:cactus")))
  22.       // Set the callback function that will verify if a recipe is a valid recipe for this category.
  23.       .isRecipeHandled((recipe) => {
  24.         return global["verifyRecipe"](jeiHelpers, recipe);
  25.       })
  26.       // Set the callback function that will allow JEI to index this recipe and determine
  27.       // what the inputs and outputs of each recipe are.
  28.       .handleLookup((builder, recipe, focuses) => {
  29.         global["handleLookup"](jeiHelpers, builder, recipe, focuses);
  30.       })
  31.       // Set the callback function for rendering additional detials to the screen.
  32.       .setDrawHandler((recipe, recipeSlotsView, guiGraphics, mouseX, mouseY) => {
  33.         global["renderPainfulBlocks"](jeiHelpers, recipe, recipeSlotsView, guiGraphics, mouseX, mouseY);
  34.       });
  35.   });
  36. });
  37.  
  38. // This function will be used by JEI to verify if a custom recipe is apart of this category.
  39. global["verifyRecipe"] = (jeiHelpers, recipe) => {
  40.   // The data we give later on in the recipes will be stored in the `data` field.
  41.   // Whatever you pass in, is whatever you'll get out.
  42.   // The possibilities are endless, as you are only restricted to what you can store on
  43.   // the KubeJS client side.
  44.  
  45.   // IMPORTANT: Always return true or false. If you do not, it could crash the game or cause
  46.   // JEI to not work properly.
  47.   return !!(
  48.     recipe?.data?.name !== undefined &&
  49.     recipe?.data?.type !== undefined &&
  50.     recipe?.data?.description !== undefined
  51.   );
  52. };
  53.  
  54. // JEI needs to understand what sort of information is held within
  55. // the recipe. This is where you can define different types of slots,
  56. // where they should go, if they are input, output, catalysts, or neither.
  57. // Depending on the slot type, it will effect if the recipe appears in
  58. // in the recipe lookup.
  59. // Refer to the JEI API for more information on how to use this.
  60. global["handleLookup"] = (jeiHelpers, builder, recipe, focuses) => {
  61.   switch (recipe.data.type) {
  62.     case "item":
  63.     case "block":
  64.       // Add an input slot to the recipe that is 35 pixels from the left and 20 pixels from the top.
  65.       // Name the slot "input" so that if we want to reference it in the draw handler, we can.
  66.       builder.addSlot("INPUT", 35, 20).addItemStack(Item.of(recipe.data.name)).setSlotName("input");
  67.       // Add an invisible output slot so that if you look at how the item is made, it shows this recipe.
  68.       builder.addInvisibleIngredients("OUTPUT").addItemStack(Item.of(recipe.data.name));
  69.       break;
  70.     case "fluid":
  71.       // Add an input slot to the recipe that is 35 pixels from the left and 20 pixels from the top.
  72.       // This one is slightly different as we are adding a fluid to the slot instead of an Item.
  73.       // you can chain these as much as you'd like and add as many different ingredients
  74.       // as you'd like.
  75.       builder.addSlot("INPUT", 35, 20).addFluidStack(recipe.data.name, 1000).setSlotName("input");
  76.       // Add an invisible output slot so that if you look at how the item is made, it shows this recipe.
  77.       builder.addInvisibleIngredients("OUTPUT").addFluidStack(recipe.data.name, 1000);
  78.       break;
  79.   }
  80. };
  81.  
  82. // We tap into the Minecraft GuiGraphics class to draw the text description above the input slot.
  83. global["renderPainfulBlocks"] = (jeiHelpers, recipe, recipeSlotsView, guiGraphics, mouseX, mouseY) => {
  84.   // By using the Client binding we can get the Minecraft font.
  85.   // Next we can draw the description of the recipe above the input slot.
  86.   // The first argument is the font, the second is the FormattedText, the third is the x position,
  87.   // the fourth is the y position, the fifth is the width of the text, and I have no clue what the last argument does.
  88.   // Probably z-index if I had to guess.
  89.   guiGraphics.drawWordWrap(Client.font, Text.of(recipe.data.description), 0, 10, 100, 0);
  90. };
  91.  
  92. // Here we can hook into the JEI recipe registration event to add some recipes to our
  93. // newly created category.
  94. JEIAddedEvents.registerRecipes((event) => {
  95.   // This utilized the new recipe category you made above here.
  96.   // All that's left is to add as many different recipes as your heart desires.
  97.   // There are no limitations to what you can store as a "recipe". Nothing that you don't control
  98.   // cares about the format of the data.
  99.   // If you want it to be an object, an array, a string, a number, a boolean, or even a function,
  100.   // It does not matter. It's all up to you. Go wild.
  101.   // Just make sure to update how you render it in the category definition.
  102.   event
  103.     .custom("kubejsadditions:painful_blocks")
  104.     .add({ name: "minecraft:cactus", type: "block", description: "It is kind of prickly." })
  105.     .add({ name: "minecraft:stick", type: "item", description: "It could be sharp!" })
  106.     .add({ name: "minecraft:lava", type: "fluid", description: "It is very hot." })
  107.     .add({ name: "minecraft:sugar", type: "item", description: "It causes diabetes." });
  108.   // .add([])
  109.   // .add("")
  110.   // .add(true)
  111.   // .add(50)
  112.   // .add(12.4)
  113.   // .add(()=> Item.of('steak'))
  114. });
Tags: kubejs
Add Comment
Please, Sign In to add comment