diff --git a/LICENSE b/LICENSE index 0e259d4..5f79ebe 100644 --- a/LICENSE +++ b/LICENSE @@ -1,121 +1,21 @@ -Creative Commons Legal Code - -CC0 1.0 Universal - - CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE - LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN - ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS - INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES - REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS - PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM - THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED - HEREUNDER. - -Statement of Purpose - -The laws of most jurisdictions throughout the world automatically confer -exclusive Copyright and Related Rights (defined below) upon the creator -and subsequent owner(s) (each and all, an "owner") of an original work of -authorship and/or a database (each, a "Work"). - -Certain owners wish to permanently relinquish those rights to a Work for -the purpose of contributing to a commons of creative, cultural and -scientific works ("Commons") that the public can reliably and without fear -of later claims of infringement build upon, modify, incorporate in other -works, reuse and redistribute as freely as possible in any form whatsoever -and for any purposes, including without limitation commercial purposes. -These owners may contribute to the Commons to promote the ideal of a free -culture and the further production of creative, cultural and scientific -works, or to gain reputation or greater distribution for their Work in -part through the use and efforts of others. - -For these and/or other purposes and motivations, and without any -expectation of additional consideration or compensation, the person -associating CC0 with a Work (the "Affirmer"), to the extent that he or she -is an owner of Copyright and Related Rights in the Work, voluntarily -elects to apply CC0 to the Work and publicly distribute the Work under its -terms, with knowledge of his or her Copyright and Related Rights in the -Work and the meaning and intended legal effect of CC0 on those rights. - -1. Copyright and Related Rights. A Work made available under CC0 may be -protected by copyright and related or neighboring rights ("Copyright and -Related Rights"). Copyright and Related Rights include, but are not -limited to, the following: - - i. the right to reproduce, adapt, distribute, perform, display, - communicate, and translate a Work; - ii. moral rights retained by the original author(s) and/or performer(s); -iii. publicity and privacy rights pertaining to a person's image or - likeness depicted in a Work; - iv. rights protecting against unfair competition in regards to a Work, - subject to the limitations in paragraph 4(a), below; - v. rights protecting the extraction, dissemination, use and reuse of data - in a Work; - vi. database rights (such as those arising under Directive 96/9/EC of the - European Parliament and of the Council of 11 March 1996 on the legal - protection of databases, and under any national implementation - thereof, including any amended or successor version of such - directive); and -vii. other similar, equivalent or corresponding rights throughout the - world based on applicable law or treaty, and any national - implementations thereof. - -2. Waiver. To the greatest extent permitted by, but not in contravention -of, applicable law, Affirmer hereby overtly, fully, permanently, -irrevocably and unconditionally waives, abandons, and surrenders all of -Affirmer's Copyright and Related Rights and associated claims and causes -of action, whether now known or unknown (including existing as well as -future claims and causes of action), in the Work (i) in all territories -worldwide, (ii) for the maximum duration provided by applicable law or -treaty (including future time extensions), (iii) in any current or future -medium and for any number of copies, and (iv) for any purpose whatsoever, -including without limitation commercial, advertising or promotional -purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each -member of the public at large and to the detriment of Affirmer's heirs and -successors, fully intending that such Waiver shall not be subject to -revocation, rescission, cancellation, termination, or any other legal or -equitable action to disrupt the quiet enjoyment of the Work by the public -as contemplated by Affirmer's express Statement of Purpose. - -3. Public License Fallback. Should any part of the Waiver for any reason -be judged legally invalid or ineffective under applicable law, then the -Waiver shall be preserved to the maximum extent permitted taking into -account Affirmer's express Statement of Purpose. In addition, to the -extent the Waiver is so judged Affirmer hereby grants to each affected -person a royalty-free, non transferable, non sublicensable, non exclusive, -irrevocable and unconditional license to exercise Affirmer's Copyright and -Related Rights in the Work (i) in all territories worldwide, (ii) for the -maximum duration provided by applicable law or treaty (including future -time extensions), (iii) in any current or future medium and for any number -of copies, and (iv) for any purpose whatsoever, including without -limitation commercial, advertising or promotional purposes (the -"License"). The License shall be deemed effective as of the date CC0 was -applied by Affirmer to the Work. Should any part of the License for any -reason be judged legally invalid or ineffective under applicable law, such -partial invalidity or ineffectiveness shall not invalidate the remainder -of the License, and in such case Affirmer hereby affirms that he or she -will not (i) exercise any of his or her remaining Copyright and Related -Rights in the Work or (ii) assert any associated claims and causes of -action with respect to the Work, in either case contrary to Affirmer's -express Statement of Purpose. - -4. Limitations and Disclaimers. - - a. No trademark or patent rights held by Affirmer are waived, abandoned, - surrendered, licensed or otherwise affected by this document. - b. Affirmer offers the Work as-is and makes no representations or - warranties of any kind concerning the Work, express, implied, - statutory or otherwise, including without limitation warranties of - title, merchantability, fitness for a particular purpose, non - infringement, or the absence of latent or other defects, accuracy, or - the present or absence of errors, whether or not discoverable, all to - the greatest extent permissible under applicable law. - c. Affirmer disclaims responsibility for clearing rights of other persons - that may apply to the Work or any use thereof, including without - limitation any person's Copyright and Related Rights in the Work. - Further, Affirmer disclaims responsibility for obtaining any necessary - consents, permissions or other rights required for any use of the - Work. - d. Affirmer understands and acknowledges that Creative Commons is not a - party to this document and has no duty or obligation with respect to - this CC0 or use of the Work. +MIT License + +Copyright (c) 2023 Griefed + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 0de36d9..a423e24 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,6 @@ [![](http://cf.way2muchnoise.eu/versions/415747.svg)](https://www.curseforge.com/minecraft/mc-mods/crumbs) --> -Assortment of blocks I want to use. - ## What does this mod do? This mod adds an assortment of blocks I would like to use in Minecraft. Mainly blocks inspired by the aesthetic of Half Life 2's Citadel, @@ -33,6 +31,6 @@ This guide will show how to import the MultiLoader Template into IntelliJ IDEA. 7. Assuming you were able to run the game in step 7 your workspace should now be set up. ## Development Guide -When using this template the majority of your mod is developed in the Common project. The Common project is compiled against the vanilla game and is used to hold code that is shared between the different loader-specific versions of your mod. The Common project has no knowledge or access to ModLoader specific code, apis, or concepts. Code that requires something from a specific loader must be done through the project that is specific to that loader, such as the Forge or Fabric project. +When using this template the majority of your mod is developed in the Common project. The Common project is compiled against the vanilla game and is used to hold code that is shared between the different loader-specific versions of your mod. The Common project has no knowledge or access to ModLoader specific code, apis, or concepts. Code that requires something from a specific loader must be done through the project that is specific to that loader, such as the Forge or Fabric project. Loader specific projects such as the Forge and Fabric project are used to load the Common project into the game. These projects also define code that is specific to that loader. Loader specific projects can access all of the code in the Common project. It is important to remember that the Common project can not access code from loader specific projects. diff --git a/buildSrc/src/main/java/de/griefed/generation/BlockAndItemCodeGenerator.java b/buildSrc/src/main/java/de/griefed/generation/BlockAndItemCodeGenerator.java index e532ba0..c7c46c0 100644 --- a/buildSrc/src/main/java/de/griefed/generation/BlockAndItemCodeGenerator.java +++ b/buildSrc/src/main/java/de/griefed/generation/BlockAndItemCodeGenerator.java @@ -32,13 +32,13 @@ public void apply(Project project) { String modName = gradleProperties.getProperty("mod_name"); BlockDefinitionParser parser = new BlockDefinitionParser(project, objectMapper); - CommonGeneration common = new CommonGeneration(project.findProject("common"), modName, parser, objectMapper); - FabricGeneration fabric = new FabricGeneration(project.findProject("fabric"), modName, parser, objectMapper); - ForgeGeneration forge = new ForgeGeneration(project.findProject("forge"), modName, parser, objectMapper); + CommonGeneration common = new CommonGeneration(project.findProject("Common"), modName, parser, objectMapper); + //FabricGeneration fabric = new FabricGeneration(project.findProject("fabric"), modName, parser, objectMapper); + //ForgeGeneration forge = new ForgeGeneration(project.findProject("forge"), modName, parser, objectMapper); common.run(); - fabric.run(); - forge.run(); + //fabric.run(); + //forge.run(); } catch (IOException e) { throw new RuntimeException("Error generating block and item code.", e); } diff --git a/buildSrc/src/main/java/de/griefed/generation/CommonGeneration.java b/buildSrc/src/main/java/de/griefed/generation/CommonGeneration.java index be4fe3c..5c7e220 100644 --- a/buildSrc/src/main/java/de/griefed/generation/CommonGeneration.java +++ b/buildSrc/src/main/java/de/griefed/generation/CommonGeneration.java @@ -1,19 +1,132 @@ package de.griefed.generation; import com.fasterxml.jackson.databind.ObjectMapper; +import de.griefed.generation.blocks.BlockDefinition; import de.griefed.generation.blocks.BlockDefinitionParser; import org.gradle.api.Project; import java.io.IOException; -public class CommonGeneration extends ModloaderGeneration { +public class CommonGeneration extends ModloaderGeneration implements CodeGeneration { protected CommonGeneration(Project project, String modName, BlockDefinitionParser parser, ObjectMapper objectMapper) { super(project, modName, parser, objectMapper); } + private final String modBlockClassTemplate = StringTemplates.COMMON_MODBLOCKS_CLASS + .replace("GROUP", getGroup()); + + private final String modItemsClassTemplate = StringTemplates.COMMON_ITEMS_CLASS + .replace("GROUP", getGroup()); + + private final String modloaderClassTemplate = StringTemplates.COMMON_MAIN_CLASS + .replace("GROUP", getGroup()) + .replace("MODID", getId()); + public void run() throws IOException { + createModBlockPackage(); + createItemPackage(); + createModBlocksClass(); + createModItemsClass(); + createModloaderClass(); updateTranslations(); createBlockFiles(); createItemFiles(); + updateModBlocksClass(); + updateModItemsClass(); + updateModloaderMain(); + } + + @Override + public void createModBlocksClass() throws IOException { + if (!getModBlocksClass().exists() && getModBlocksClass().createNewFile()) { + System.out.println("ModBlock-class created: " + getModBlocksClass().getAbsolutePath()); + writeToFile(getModBlocksClass(), modBlockClassTemplate); + } + } + + @Override + public void createModItemsClass() throws IOException { + if (!getModItemsClass().exists() && getModItemsClass().createNewFile()) { + System.out.println("ModItems-class created: " + getModItemsClass().getAbsolutePath()); + writeToFile(getModItemsClass(), modItemsClassTemplate); + } + } + + @Override + public void createModloaderClass() throws IOException { + if (!getModloaderClass().exists() && getModloaderClass().createNewFile()) { + System.out.println("ModItems-class created: " + getModloaderClass().getAbsolutePath()); + writeToFile(getModloaderClass(), modloaderClassTemplate); + } + } + + //public static final RegistryObject SILVERBLOCK = BLOCKS.register("silverblock", () -> new Block( + // BlockBehaviour.Properties.of(Material.METAL).sound(SoundType.STONE).strength(12f, 10f).requiresCorrectToolForDrops())); + //public static final RegistryObject SILVERBLOCK_ITEM = ITEMS.register("silverblock", () -> new BlockItem(SILVERBLOCK.get(), itemBuilder())); + @Override + public void updateModBlocksClass() throws IOException { + String modBlocks = readFromFile(getModBlocksClass()); + StringBuilder blocks = new StringBuilder(); + StringBuilder registration; + for (BlockDefinition block : getBlockDefinitionParser().getBlocks()) { + registration = new StringBuilder(); + //block + registration.append("\n\tpublic static final RegistryObject ").append(block.getId().toUpperCase()).append(" = BLOCKS.register(\"").append(block.getId()).append("_block\", () -> new Block(\n") + .append("\t\t\tBlockBehaviour.Properties.of(Material.").append(block.getMaterial()).append(").sound(SoundType.").append(block.getSoundType()).append(").strength(").append(block.getStrengthOne()).append("f, ").append(block.getStrengthTwo()).append("f)\n") + .append("\t\t\t\t\t").append(".lightLevel(state -> ").append(block.getLightLevel()).append(")").append(".explosionResistance(").append(block.getExplosionResistance()).append("f)\n\t\t\t\t\t"); + if (block.isRequiresCorrectTool()) { + registration.append(".requiresCorrectToolForDrops()"); + } + if (block.isInstabreak()) { + registration.append(".instabreak()"); + } + registration.append("));\n"); + + //block item + registration.append("\n\tpublic static final RegistryObject ").append(block.getId().toUpperCase()).append("_ITEM = ITEMS.register(\"").append(block.getId()).append("\", () -> new BlockItem(").append(block.getId().toUpperCase()).append(".get(), itemBuilder()));\n"); + + blocks.append(registration); + } + modBlocks = CODE_REPLACE + .matcher(modBlocks) + .replaceAll("/*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/" + + blocks + + "\t/*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/"); + writeToFile(getModBlocksClass(), modBlocks); + } + + + //public static final RegistryObject SILVERNUGGET = ITEMS.register("silvernugget", () -> new Item(itemBuilder().stacksTo(64).rarity(Rarity.COMMON))); + @Override + public void updateModItemsClass() throws IOException { + String modItems = readFromFile(getModItemsClass()); + StringBuilder items = new StringBuilder(); + StringBuilder registration; + /*for (ItemDefinition item : getItemDefinitionParser().getItems()) { + registration = new StringBuilder(); + registration.append("\n\tpublic static final RegistryObject ").append(item.getId().toUpperCase()).append(" = ITEMS.register(\"").append(item.getId()).append("\",\n") + .append("\t\t\t() -> new Item(new Item.Properties()));"); + registration.append("\n"); + items.append(registration); + }*/ + modItems = CODE_REPLACE + .matcher(modItems) + .replaceAll("/*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/\n\n" + + items + + "\t/*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/"); + writeToFile(getModItemsClass(), modItems); + } + + @Override + public void updateModloaderMain() throws IOException { + String modloaderMain = readFromFile(getModloaderClass()); + modloaderMain = CODE_REPLACE + .matcher(modloaderMain) + .replaceAll(""" + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + \t\tGeneratedModBlocks.loadClass(); + \t\tGeneratedModItems.loadClass(); + \t\t/*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/"""); + writeToFile(getModloaderClass(), modloaderMain); } } diff --git a/buildSrc/src/main/java/de/griefed/generation/FabricGeneration.java b/buildSrc/src/main/java/de/griefed/generation/FabricGeneration.java index 6eb7314..38494b3 100644 --- a/buildSrc/src/main/java/de/griefed/generation/FabricGeneration.java +++ b/buildSrc/src/main/java/de/griefed/generation/FabricGeneration.java @@ -32,14 +32,14 @@ public FabricGeneration(Project project, String modName, BlockDefinitionParser p @Override public void run() throws IOException { - createModBlockPackage(); - createItemPackage(); - createModBlocksClass(); - createModItemsClass(); - createModloaderClass(); - updateModBlocksClass(); - updateModItemsClass(); - updateModloaderMain(); +// createModBlockPackage(); +// createItemPackage(); +// createModBlocksClass(); +// createModItemsClass(); +// createModloaderClass(); +// updateModBlocksClass(); +// updateModItemsClass(); +// updateModloaderMain(); } @Override @@ -79,7 +79,7 @@ public void updateModBlocksClass() throws IOException { for (BlockDefinition block : getBlockDefinitionParser().getBlocks()) { registration = new StringBuilder(); registration.append("\n\tpublic static final Block ").append(block.getId().toUpperCase()).append("_BLOCK = registerBlock(\"").append(block.getId()).append("_block\",\n") - .append("\t\t\tnew Block(FabricBlockSettings.of(Material.").append(block.getMaterial()).append(").strength(").append(block.getStrength()).append("f).luminance(").append(block.getLightLevel()).append(")\n") + .append("\t\t\tnew Block(FabricBlockSettings.of(Material.").append(block.getMaterial()).append(").strength(").append(block.getStrengthOne()).append("f).luminance(").append(block.getLightLevel()).append(")\n") .append("\t\t\t\t\t.hardness(").append(block.getExplosionResistance()).append(")"); if (block.isRequiresCorrectTool()) { registration.append(".requiresTool()"); diff --git a/buildSrc/src/main/java/de/griefed/generation/ForgeGeneration.java b/buildSrc/src/main/java/de/griefed/generation/ForgeGeneration.java index a140d4d..b0a83da 100644 --- a/buildSrc/src/main/java/de/griefed/generation/ForgeGeneration.java +++ b/buildSrc/src/main/java/de/griefed/generation/ForgeGeneration.java @@ -28,14 +28,14 @@ public ForgeGeneration(Project project, String modName, BlockDefinitionParser pa @Override public void run() throws IOException { - createModBlockPackage(); - createItemPackage(); - createModBlocksClass(); - createModItemsClass(); - createModloaderClass(); - updateModBlocksClass(); - updateModItemsClass(); - updateModloaderMain(); +// createModBlockPackage(); +// createItemPackage(); +// createModBlocksClass(); +// createModItemsClass(); +// createModloaderClass(); +// updateModBlocksClass(); +// updateModItemsClass(); +// updateModloaderMain(); } @Override @@ -71,7 +71,7 @@ public void updateModBlocksClass() throws IOException { registration = new StringBuilder(); registration.append("\n\tpublic static final RegistryObject ").append(block.getId().toUpperCase()).append("_BLOCK = registerBlock(\"").append(block.getId()).append("_block\",\n") .append("\t\t\t() -> new Block(BlockBehaviour.Properties.of(Material.").append(block.getMaterial()).append(")\n") - .append("\t\t\t\t\t.strength(").append(block.getStrength()).append("f).lightLevel(state -> ").append(block.getLightLevel()).append(")").append(".explosionResistance(").append(block.getExplosionResistance()).append("f)\n\t\t\t\t\t"); + .append("\t\t\t\t\t.strength(").append(block.getStrengthOne()).append("f).lightLevel(state -> ").append(block.getLightLevel()).append(")").append(".explosionResistance(").append(block.getExplosionResistance()).append("f)\n\t\t\t\t\t"); if (block.isRequiresCorrectTool()) { registration.append(".requiresCorrectToolForDrops()"); } diff --git a/buildSrc/src/main/java/de/griefed/generation/ModloaderGeneration.java b/buildSrc/src/main/java/de/griefed/generation/ModloaderGeneration.java index 811321c..f76feff 100644 --- a/buildSrc/src/main/java/de/griefed/generation/ModloaderGeneration.java +++ b/buildSrc/src/main/java/de/griefed/generation/ModloaderGeneration.java @@ -46,10 +46,14 @@ protected ModloaderGeneration(Project project, String modName, BlockDefinitionPa this.modloader = project.getName(); this.id = group.substring(group.lastIndexOf(".") + 1); this.blocksGroup = new File(groupDirectory, "block"); - this.modBlocksClass = new File(blocksGroup, "ModBlocks.java"); + this.modBlocksClass = new File(blocksGroup, "GeneratedModBlocks.java"); this.itemGroup = new File(groupDirectory, "item"); - this.modItemsClass = new File(itemGroup, "ModItems.java"); - this.modloaderClass = new File(groupDirectory, subName + ".java"); + this.modItemsClass = new File(itemGroup, "GeneratedModItems.java"); + if (project.getName().equalsIgnoreCase("common")) { + this.modloaderClass = new File(groupDirectory,"CommonClass.java"); + } else { + this.modloaderClass = new File(groupDirectory, subName + ".java"); + } this.assetsDirectory = new File(project.getProjectDir(),"src/main/resources/assets/" + id); this.translationsFile = new File(assetsDirectory, "lang/en_us.json"); } @@ -166,7 +170,7 @@ public void createBlockFiles() throws IOException { blockTexturesDir.mkdirs(); String blockstatesTemp = "BLOCKID_block.json"; String blockModelTemp = "BLOCKID_block.json"; - String itemBlockModelTemp = "BLOCKID_block.json"; + String itemBlockModelTemp = "BLOCKID.json"; String blockTextureTemp = "BLOCKID_block.png"; File blockstate; File blockModel; diff --git a/buildSrc/src/main/java/de/griefed/generation/StringTemplates.java b/buildSrc/src/main/java/de/griefed/generation/StringTemplates.java index 3d7e8a7..324ae13 100644 --- a/buildSrc/src/main/java/de/griefed/generation/StringTemplates.java +++ b/buildSrc/src/main/java/de/griefed/generation/StringTemplates.java @@ -273,4 +273,117 @@ public void onInitialize() { } } """; + + public static final String COMMON_MODBLOCKS_CLASS = """ + package GROUP.block; + + import GROUP.Constants; + import GROUP.platform.Services; + import GROUP.registry.RegistrationProvider; + import GROUP.registry.RegistryObject; + import net.minecraft.core.Registry; + import net.minecraft.world.item.*; + import net.minecraft.world.level.block.Block; + import net.minecraft.world.level.block.SoundType; + import net.minecraft.world.level.block.state.BlockBehaviour; + import net.minecraft.world.level.material.Material; + + public class GeneratedModBlocks { + public static final RegistrationProvider BLOCKS = RegistrationProvider.get(Registry.BLOCK_REGISTRY, Constants.MOD_ID); + public static final RegistrationProvider ITEMS = RegistrationProvider.get(Registry.ITEM_REGISTRY, Constants.MOD_ID); + + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + + private static Item.Properties itemBuilder() { + return new Item.Properties().tab(Services.PLATFORM.getCreativeTab()); + } + + // Called in the mod initializer / constructor in order to make sure that items are registered + public static void loadClass() { + } + } + """; + + public static final String COMMON_ITEMS_CLASS = """ + package GROUP.item; + + import GROUP.Constants; + import GROUP.platform.Services; + import GROUP.registry.RegistrationProvider; + import GROUP.registry.RegistryObject; + import net.minecraft.core.Registry; + import net.minecraft.world.item.Item; + + public class GeneratedModItems { + public static final RegistrationProvider ITEMS = RegistrationProvider.get(Registry.ITEM_REGISTRY, Constants.MOD_ID); + + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + + private static Item.Properties itemBuilder() { + return new Item.Properties().tab(Services.PLATFORM.getCreativeTab()); + } + + // Called in the mod initializer / constructor in order to make sure that items are registered + public static void loadClass() { + } + } + + """; + + public static final String COMMON_MAIN_CLASS = """ + package GROUP; + + import GROUP.block.GeneratedModBlocks; + import GROUP.item.GeneratedModItems; + import GROUP.platform.Services; + import net.minecraft.core.Registry; + import net.minecraft.network.chat.Component; + import net.minecraft.world.food.FoodProperties; + import net.minecraft.world.item.ItemStack; + import net.minecraft.world.item.Items; + import net.minecraft.world.item.TooltipFlag; + + import java.util.List; + + // This class is part of the common project meaning it is shared between all supported loaders. Code written here can only + // import and access the vanilla codebase, libraries used by vanilla, and optionally third party libraries that provide + // common compatible binaries. This means common code can not directly use loader specific concepts such as Forge events + // however it will be compatible with all supported mod loaders. + public class CommonClass { + + // The loader specific projects are able to import and use any code from the common project. This allows you to + // write the majority of your code here and load it from your loader specific projects. This example has some + // code that gets invoked by the entry point of the loader specific projects. + public static void init() { + + Constants.LOG.info("Hello from Common init on {}! we are currently in a {} environment!", Services.PLATFORM.getPlatformName(), Services.PLATFORM.getEnvironmentName()); + Constants.LOG.info("The ID for diamonds is {}", Registry.ITEM.getKey(Items.DIAMOND)); + + // It is common for all supported loaders to provide a similar feature that can not be used directly in the + // common code. A popular way to get around this is using Java's built-in service loader feature to create + // your own abstraction layer. You can learn more about this in our provided services class. In this example + // we have an interface in the common code and use a loader specific implementation to delegate our call to + // the platform specific approach. + if (Services.PLATFORM.isModLoaded("MODID")) { + Constants.LOG.info("Hello to MODID"); + } + + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + } + + public static void onItemTooltip(ItemStack stack, TooltipFlag context, List tooltip) { + if (!stack.isEmpty()) { + final FoodProperties food = stack.getItem().getFoodProperties(); + + if (food != null) { + tooltip.add(Component.literal("Nutrition: " + food.getNutrition())); + tooltip.add(Component.literal("Saturation: " + food.getSaturationModifier())); + } + } + } + } + """; } diff --git a/buildSrc/src/main/java/de/griefed/generation/blocks/BlockDefinition.java b/buildSrc/src/main/java/de/griefed/generation/blocks/BlockDefinition.java index 1370a76..74427a2 100644 --- a/buildSrc/src/main/java/de/griefed/generation/blocks/BlockDefinition.java +++ b/buildSrc/src/main/java/de/griefed/generation/blocks/BlockDefinition.java @@ -5,7 +5,9 @@ public class BlockDefinition { private String id; private String translation; private String material; - private int strength; + private String soundType; + private int strengthOne; + private int strengthTwo; private int lightLevel; private int explosionResistance; private boolean requiresCorrectTool; @@ -23,8 +25,14 @@ public String getMaterial() { return material; } - public int getStrength() { - return strength; + public String getSoundType() { return soundType; } + + public int getStrengthOne() { + return strengthOne; + } + + public int getStrengthTwo() { + return strengthTwo; } public int getLightLevel() { diff --git a/common/src/main/java/de/griefed/addemall/CommonClass.java b/common/src/main/java/de/griefed/addemall/CommonClass.java index 1f1ef33..ec0f33a 100644 --- a/common/src/main/java/de/griefed/addemall/CommonClass.java +++ b/common/src/main/java/de/griefed/addemall/CommonClass.java @@ -1,5 +1,7 @@ package de.griefed.addemall; +import de.griefed.addemall.block.GeneratedModBlocks; +import de.griefed.addemall.item.GeneratedModItems; import de.griefed.addemall.platform.Services; import net.minecraft.core.Registry; import net.minecraft.network.chat.Component; @@ -30,9 +32,13 @@ public static void init() { // we have an interface in the common code and use a loader specific implementation to delegate our call to // the platform specific approach. if (Services.PLATFORM.isModLoaded("addemall")) { - Constants.LOG.info("Hello to addemall"); } + + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + GeneratedModBlocks.loadClass(); + GeneratedModItems.loadClass(); + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ } public static void onItemTooltip(ItemStack stack, TooltipFlag context, List tooltip) { @@ -45,4 +51,4 @@ public static void onItemTooltip(ItemStack stack, TooltipFlag context, List BLOCKS = RegistrationProvider.get(Registry.BLOCK_REGISTRY, Constants.MOD_ID); + public static final RegistrationProvider ITEMS = RegistrationProvider.get(Registry.ITEM_REGISTRY, Constants.MOD_ID); + + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + public static final RegistryObject TUTORIAL_STONE = BLOCKS.register("tutorial_stone_block", () -> new Block( + BlockBehaviour.Properties.of(Material.STONE).sound(SoundType.STONE).strength(12f, 10f) + .lightLevel(state -> 0).explosionResistance(8f) + .requiresCorrectToolForDrops())); + + public static final RegistryObject TUTORIAL_STONE_ITEM = ITEMS.register("tutorial_stone", () -> new BlockItem(TUTORIAL_STONE.get(), itemBuilder())); + + public static final RegistryObject TUTORIAL_WOOL = BLOCKS.register("tutorial_wool_block", () -> new Block( + BlockBehaviour.Properties.of(Material.WOOL).sound(SoundType.STONE).strength(8f, 6f) + .lightLevel(state -> 5).explosionResistance(6f) + )); + + public static final RegistryObject TUTORIAL_WOOL_ITEM = ITEMS.register("tutorial_wool", () -> new BlockItem(TUTORIAL_WOOL.get(), itemBuilder())); + + public static final RegistryObject TUTORIAL_SAND = BLOCKS.register("tutorial_sand_block", () -> new Block( + BlockBehaviour.Properties.of(Material.SAND).sound(SoundType.STONE).strength(4f, 2f) + .lightLevel(state -> 12).explosionResistance(2f) + .instabreak())); + + public static final RegistryObject TUTORIAL_SAND_ITEM = ITEMS.register("tutorial_sand", () -> new BlockItem(TUTORIAL_SAND.get(), itemBuilder())); + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + + private static Item.Properties itemBuilder() { + return new Item.Properties().tab(Services.PLATFORM.getCreativeTab()); + } + + // Called in the mod initializer / constructor in order to make sure that items are registered + public static void loadClass() { + } +} diff --git a/common/src/main/java/de/griefed/addemall/item/GeneratedModItems.java b/common/src/main/java/de/griefed/addemall/item/GeneratedModItems.java new file mode 100644 index 0000000..c69048f --- /dev/null +++ b/common/src/main/java/de/griefed/addemall/item/GeneratedModItems.java @@ -0,0 +1,25 @@ +package de.griefed.addemall.item; + +import de.griefed.addemall.Constants; +import de.griefed.addemall.platform.Services; +import de.griefed.addemall.registry.RegistrationProvider; +import de.griefed.addemall.registry.RegistryObject; +import net.minecraft.core.Registry; +import net.minecraft.world.item.Item; + +public class GeneratedModItems { + public static final RegistrationProvider ITEMS = RegistrationProvider.get(Registry.ITEM_REGISTRY, Constants.MOD_ID); + + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + + /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ + + private static Item.Properties itemBuilder() { + return new Item.Properties().tab(Services.PLATFORM.getCreativeTab()); + } + + // Called in the mod initializer / constructor in order to make sure that items are registered + public static void loadClass() { + } +} + diff --git a/common/src/main/java/de/griefed/addemall/platform/services/IPlatformHelper.java b/common/src/main/java/de/griefed/addemall/platform/services/IPlatformHelper.java index a0ed9e3..706d524 100644 --- a/common/src/main/java/de/griefed/addemall/platform/services/IPlatformHelper.java +++ b/common/src/main/java/de/griefed/addemall/platform/services/IPlatformHelper.java @@ -1,7 +1,14 @@ package de.griefed.addemall.platform.services; +import net.minecraft.world.item.CreativeModeTab; + public interface IPlatformHelper { + /** + * @return The Just Enough Crowns Creative Tab. + */ + CreativeModeTab getCreativeTab(); + /** * Gets the name of the current platform * diff --git a/common/src/main/java/de/griefed/addemall/registry/RegistrationProvider.java b/common/src/main/java/de/griefed/addemall/registry/RegistrationProvider.java new file mode 100644 index 0000000..075bdb4 --- /dev/null +++ b/common/src/main/java/de/griefed/addemall/registry/RegistrationProvider.java @@ -0,0 +1,113 @@ +package de.griefed.addemall.registry; + +import de.griefed.addemall.platform.Services; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceKey; + +import java.util.Collection; +import java.util.function.Supplier; + +/** + * Utility class for multiloader registration. + *

+ * Example usage: + *

{@code
+ * public static final RegistrationProvider PROVIDER = RegistrationProvider.get(Test.REGISTRY, "modid");
+ * public static final RegistryObject OBJECT = PROVIDER.register("object", () -> new Test());
+ *
+ * // The purpose of this method is to be called in the mod's constructor, in order to assure that the class is loaded, and that objects can be registered.
+ * public static void loadClass(){}
+ * }
+ * + * @param the type of the objects that this class registers + */ +public interface RegistrationProvider { + + /** + * Gets a provider for specified {@code modId} and {@code resourceKey}.
+ * It is recommended to store the resulted provider in a {@code static final} field to + * the {@link Factory#INSTANCE factory} creating multiple providers for the same resource key and mod id. + * + * @param resourceKey the {@link ResourceKey} of the registry of the provider + * @param modId the mod id that the provider will register objects for + * @param the type of the provider + * @return the provider + */ + static RegistrationProvider get(ResourceKey> resourceKey, String modId) { + return Factory.INSTANCE.create(resourceKey, modId); + } + + /** + * Gets a provider for specified {@code modId} and {@code registry}.
+ * It is recommended to store the resulted provider in a {@code static final} field to + * the {@link Factory#INSTANCE factory} creating multiple providers for the same resource key and mod id. + * + * @param registry the {@link Registry} of the provider + * @param modId the mod id that the provider will register objects for + * @param the type of the provider + * @return the provider + */ + static RegistrationProvider get(Registry registry, String modId) { + return Factory.INSTANCE.create(registry, modId); + } + + /** + * Registers an object. + * + * @param name the name of the object + * @param supplier a supplier of the object to register + * @param the type of the object + * @return a wrapper containing the lazy registered object. Calling {@link RegistryObject#get() get} too early + * on the wrapper might result in crashes! + */ + RegistryObject register(String name, Supplier supplier); + + /** + * Gets all the objects currently registered. + * + * @return an immutable view of all the objects currently registered + */ + Collection> getEntries(); + + /** + * Gets the mod id that this provider registers objects for. + * + * @return the mod id + */ + String getModId(); + + /** + * Factory class for {@link RegistrationProvider registration providers}.
+ * This class is loaded using {@link java.util.ServiceLoader Service Loaders}, and only one + * should exist per mod loader. + */ + interface Factory { + + /** + * The singleton instance of the {@link Factory}. This is different on each loader. + */ + Factory INSTANCE = Services.load(Factory.class); + + /** + * Creates a {@link RegistrationProvider}. + * + * @param resourceKey the {@link ResourceKey} of the registry to create this provider for + * @param modId the mod id for which the provider will register objects + * @param the type of the provider + * @return the provider + */ + RegistrationProvider create(ResourceKey> resourceKey, String modId); + + /** + * Creates a {@link RegistrationProvider}. + * + * @param registry the {@link Registry} to create this provider for + * @param modId the mod id for which the provider will register objects + * @param the type of the provider + * @return the provider + */ + default RegistrationProvider create(Registry registry, String modId) { + return create(registry.key(), modId); + } + } +} diff --git a/common/src/main/java/de/griefed/addemall/registry/RegistryObject.java b/common/src/main/java/de/griefed/addemall/registry/RegistryObject.java new file mode 100644 index 0000000..ac5b3d7 --- /dev/null +++ b/common/src/main/java/de/griefed/addemall/registry/RegistryObject.java @@ -0,0 +1,45 @@ +package de.griefed.addemall.registry; + +import net.minecraft.core.Holder; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; + +import java.util.function.Supplier; + +/** + * Represents a lazy wrapper for registry object. + * + * @param the type of the object + */ +public interface RegistryObject extends Supplier { + + /** + * Gets the {@link ResourceKey} of the registry of the object wrapped. + * + * @return the {@link ResourceKey} of the registry + */ + ResourceKey getResourceKey(); + + /** + * Gets the id of the object. + * + * @return the id of the object + */ + ResourceLocation getId(); + + /** + * Gets the object behind this wrapper. Calling this method too early + * might result in crashes. + * + * @return the object behind this wrapper + */ + @Override + T get(); + + /** + * Gets this object wrapped in a vanilla {@link Holder}. + * + * @return the holder + */ + Holder asHolder(); +} diff --git a/common/src/main/resources/assets/addemall/lang/en_us.json b/common/src/main/resources/assets/addemall/lang/en_us.json index ffebac1..2a5d80d 100644 --- a/common/src/main/resources/assets/addemall/lang/en_us.json +++ b/common/src/main/resources/assets/addemall/lang/en_us.json @@ -13,5 +13,7 @@ "block.addemall.tutorial_sand_block": "Tutorial Sand", -"DO.NOT.EDIT.MANUALLY.END": "END" +"DO.NOT.EDIT.MANUALLY.END": "END", + + "itemGroup.addemall.tab": "AddEmAll" } diff --git a/common/src/main/resources/assets/addemall/models/item/tutorial_sand.json b/common/src/main/resources/assets/addemall/models/item/tutorial_sand.json new file mode 100644 index 0000000..fb692ff --- /dev/null +++ b/common/src/main/resources/assets/addemall/models/item/tutorial_sand.json @@ -0,0 +1,3 @@ +{ + "parent": "addemall:block/tutorial_sand_block" +} diff --git a/common/src/main/resources/assets/addemall/models/item/tutorial_stone.json b/common/src/main/resources/assets/addemall/models/item/tutorial_stone.json new file mode 100644 index 0000000..c0842df --- /dev/null +++ b/common/src/main/resources/assets/addemall/models/item/tutorial_stone.json @@ -0,0 +1,3 @@ +{ + "parent": "addemall:block/tutorial_stone_block" +} diff --git a/common/src/main/resources/assets/addemall/models/item/tutorial_wool.json b/common/src/main/resources/assets/addemall/models/item/tutorial_wool.json new file mode 100644 index 0000000..485c224 --- /dev/null +++ b/common/src/main/resources/assets/addemall/models/item/tutorial_wool.json @@ -0,0 +1,3 @@ +{ + "parent": "addemall:block/tutorial_wool_block" +} diff --git a/definitions/blocks.json b/definitions/blocks.json index 303572f..f3cb4f8 100644 --- a/definitions/blocks.json +++ b/definitions/blocks.json @@ -3,7 +3,9 @@ "id": "tutorial_stone", "translation": "Tutorial Stone", "material": "STONE", - "strength": 6, + "soundType": "STONE", + "strengthOne": 12, + "strengthTwo": 10, "lightLevel": 0, "explosionResistance": 8, "requiresCorrectTool": true, @@ -13,7 +15,9 @@ "id": "tutorial_wool", "translation": "Tutorial Wool", "material": "WOOL", - "strength": 4, + "soundType": "STONE", + "strengthOne": 8, + "strengthTwo": 6, "lightLevel": 5, "explosionResistance": 6, "requiresCorrectTool": false, @@ -23,7 +27,9 @@ "id": "tutorial_sand", "translation": "Tutorial Sand", "material": "SAND", - "strength": 2, + "soundType": "STONE", + "strengthOne": 4, + "strengthTwo": 2, "lightLevel": 12, "explosionResistance": 2, "requiresCorrectTool": false, diff --git a/fabric/build.gradle b/fabric/build.gradle index 2b6f247..2db3bac 100644 --- a/fabric/build.gradle +++ b/fabric/build.gradle @@ -13,12 +13,12 @@ dependencies { modImplementation "net.fabricmc:fabric-loader:${fabric_loader_version}" modImplementation "net.fabricmc.fabric-api:fabric-api:${fabric_version}+${minecraft_version}" implementation group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.1' - implementation project(":common") + implementation project(":Common") } loom { - if (project(":common").file("src/main/resources/${mod_id}.accesswidener").exists()) { - accessWidenerPath.set(project(":common").file("src/main/resources/${mod_id}.accesswidener")) + if (project(":Common").file("src/main/resources/${mod_id}.accesswidener").exists()) { + accessWidenerPath.set(project(":Common").file("src/main/resources/${mod_id}.accesswidener")) } mixin { defaultRefmapName.set("${mod_id}.refmap.json") @@ -40,17 +40,17 @@ loom { } tasks.withType(JavaCompile).configureEach { - source(project(":common").sourceSets.main.allSource) + source(project(":Common").sourceSets.main.allSource) } tasks.withType(Javadoc).configureEach { - source(project(":common").sourceSets.main.allJava) + source(project(":Common").sourceSets.main.allJava) } tasks.named("sourcesJar", Jar) { - from(project(":common").sourceSets.main.allSource) + from(project(":Common").sourceSets.main.allSource) } processResources { - from project(":common").sourceSets.main.resources + from project(":Common").sourceSets.main.resources } publishing { diff --git a/fabric/src/main/java/de/griefed/addemall/AddEmAllFabric.java b/fabric/src/main/java/de/griefed/addemall/AddEmAllFabric.java index 554c8cd..bd52790 100644 --- a/fabric/src/main/java/de/griefed/addemall/AddEmAllFabric.java +++ b/fabric/src/main/java/de/griefed/addemall/AddEmAllFabric.java @@ -1,9 +1,12 @@ package de.griefed.addemall; import net.fabricmc.api.ModInitializer; -import de.griefed.addemall.block.ModBlocks; -import de.griefed.addemall.item.ModItems; import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback; +import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,17 +15,17 @@ public class AddEmAllFabric implements ModInitializer { public static final String MOD_ID = "addemall"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); + public static final CreativeModeTab TAB_ADDEMALL = FabricItemGroupBuilder.build( + new ResourceLocation(Constants.MOD_ID, "tab"), + () -> new ItemStack(Items.DIAMOND) + ); + @Override public void onInitialize() { Constants.LOG.info("Hello world!"); LOGGER.info("Hello Fabric World!"); CommonClass.init(); - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - ModItems.registerModItems(); - ModBlocks.registerModBlocks(); - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - ItemTooltipCallback.EVENT.register(CommonClass::onItemTooltip); } } diff --git a/fabric/src/main/java/de/griefed/addemall/FabricRegistrationFactory.java b/fabric/src/main/java/de/griefed/addemall/FabricRegistrationFactory.java new file mode 100644 index 0000000..e486252 --- /dev/null +++ b/fabric/src/main/java/de/griefed/addemall/FabricRegistrationFactory.java @@ -0,0 +1,92 @@ +package de.griefed.addemall; + +import de.griefed.addemall.registry.RegistrationProvider; +import de.griefed.addemall.registry.RegistryObject; +import net.minecraft.core.Holder; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.function.Supplier; + +public class FabricRegistrationFactory implements RegistrationProvider.Factory { + + @Override + public RegistrationProvider create(ResourceKey> resourceKey, String modId) { + return new Provider<>(modId, resourceKey); + } + + @Override + public RegistrationProvider create(Registry registry, String modId) { + return new Provider<>(modId, registry); + } + + private static class Provider implements RegistrationProvider { + private final String modId; + private final Registry registry; + + private final Set> entries = new HashSet<>(); + private final Set> entriesView = Collections.unmodifiableSet(entries); + + @SuppressWarnings({"unchecked"}) + private Provider(String modId, ResourceKey> key) { + this.modId = modId; + final var reg = Registry.REGISTRY.get(key.location()); + if (reg == null) { + throw new RuntimeException("Registry with name " + key.location() + " was not found!"); + } + registry = (Registry) reg; + } + + private Provider(String modId, Registry registry) { + this.modId = modId; + this.registry = registry; + } + + @Override + @SuppressWarnings("unchecked") + public RegistryObject register(String name, Supplier supplier) { + final var rl = new ResourceLocation(modId, name); + final var obj = Registry.register(registry, rl, supplier.get()); + final var ro = new RegistryObject() { + final ResourceKey key = ResourceKey.create((ResourceKey>) registry.key(), rl); + + @Override + public ResourceKey getResourceKey() { + return key; + } + + @Override + public ResourceLocation getId() { + return rl; + } + + @Override + public I get() { + return obj; + } + + @Override + public Holder asHolder() { + return (Holder) registry.getOrCreateHolder((ResourceKey) this.key); + } + }; + entries.add((RegistryObject) ro); + return ro; + } + + @Override + public Collection> getEntries() { + return entriesView; + } + + @Override + public String getModId() { + return modId; + } + } +} \ No newline at end of file diff --git a/fabric/src/main/java/de/griefed/addemall/block/ModBlocks.java b/fabric/src/main/java/de/griefed/addemall/block/ModBlocks.java deleted file mode 100644 index 1768cc2..0000000 --- a/fabric/src/main/java/de/griefed/addemall/block/ModBlocks.java +++ /dev/null @@ -1,58 +0,0 @@ -package de.griefed.addemall.block; - -import net.fabricmc.fabric.api.item.v1.FabricItemSettings; -import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; -import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; -import de.griefed.addemall.AddEmAllFabric; -import net.minecraft.block.Block; -import net.minecraft.block.Material; -import net.minecraft.core.Registry; -import net.minecraft.world.item.*; -import net.minecraft.item.BlockItem; -import net.minecraft.item.Item; -import net.minecraft.item.ItemGroup; -import net.minecraft.item.ItemGroups; -import net.minecraft.registry.Registries; -import net.minecraft.registry.Registry; -import net.minecraft.util.Identifier; -import net.minecraft.world.level.block.Block; - -public class ModBlocks { - - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - public static final Block TUTORIAL_STONE_BLOCK = registerBlock("tutorial_stone_block", - new Block(FabricBlockSettings.of(Material.STONE).strength(6f).luminance(0) - .hardness(8).requiresTool() - ), ItemGroups.BUILDING_BLOCKS); - - public static final Block TUTORIAL_WOOL_BLOCK = registerBlock("tutorial_wool_block", - new Block(FabricBlockSettings.of(Material.WOOL).strength(4f).luminance(5) - .hardness(6) - ), ItemGroups.BUILDING_BLOCKS); - - public static final Block TUTORIAL_SAND_BLOCK = registerBlock("tutorial_sand_block", - new Block(FabricBlockSettings.of(Material.SAND).strength(2f).luminance(12) - .hardness(2).breakInstantly() - ), ItemGroups.BUILDING_BLOCKS); - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - - private static Block registerBlockWithoutItem(String name, Block block) { - return Registry.register(Registry.BLOCK, new Identifier(AddEmAllFabric.MOD_ID, name), block); - } - - private static Block registerBlock(String name, Block block, ItemGroup tab) { - registerBlockItem(name, block, tab); - return Registry.register(Registry.BLOCK, new Identifier(AddEmAllFabric.MOD_ID, name), block); - } - - private static Item registerBlockItem(String name, Block block, ItemGroup tab) { - Item item = Registry.register(Registry.ITEM, new Identifier(AddEmAllFabric.MOD_ID, name), - new BlockItem(block, new FabricItemSettings())); - ItemGroupEvents.modifyEntriesEvent(tab).register(entries -> entries.add(item)); - return item; - } - - public static void registerModBlocks() { - AddEmAllFabric.LOGGER.debug("Registering ModBlocks for " + AddEmAllFabric.MOD_ID); - } -} diff --git a/fabric/src/main/java/de/griefed/addemall/item/ModItems.java b/fabric/src/main/java/de/griefed/addemall/item/ModItems.java deleted file mode 100644 index af60ce2..0000000 --- a/fabric/src/main/java/de/griefed/addemall/item/ModItems.java +++ /dev/null @@ -1,38 +0,0 @@ -package de.griefed.addemall.item; - -import net.fabricmc.fabric.api.item.v1.FabricItemSettings; -import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; -import de.griefed.addemall.AddEmAllFabric; -import net.minecraft.item.Item; -import net.minecraft.item.ItemGroup; -import net.minecraft.item.ItemGroups; -import net.minecraft.registry.Registries; -import net.minecraft.registry.Registry; -import net.minecraft.util.Identifier; - -public class ModItems { - - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - - private static Item registerItem(String name, Item item) { - return Registry.register(Registries.ITEM, new Identifier(AddEmAllFabric.MOD_ID, name), item); - } - - public static void addItemsToItemGroups() { - /*###GENERATED ADD CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST*/ - - /*###GENERATED ADD CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST*/ - } - - public static void addToItemGroup(ItemGroup group, Item item) { - ItemGroupEvents.modifyEntriesEvent(group).register(entries -> entries.add(item)); - } - - public static void registerModItems() { - TutorialMod.LOGGER.debug("Registering Mod Items for " + TutorialMod.MOD_ID); - - addItemsToItemGroups(); - } -} diff --git a/fabric/src/main/java/de/griefed/addemall/platform/FabricPlatformHelper.java b/fabric/src/main/java/de/griefed/addemall/platform/FabricPlatformHelper.java index a747573..a406746 100644 --- a/fabric/src/main/java/de/griefed/addemall/platform/FabricPlatformHelper.java +++ b/fabric/src/main/java/de/griefed/addemall/platform/FabricPlatformHelper.java @@ -1,10 +1,17 @@ package de.griefed.addemall.platform; +import de.griefed.addemall.AddEmAllFabric; import de.griefed.addemall.platform.services.IPlatformHelper; import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.world.item.CreativeModeTab; public class FabricPlatformHelper implements IPlatformHelper { + @Override + public CreativeModeTab getCreativeTab() { + return AddEmAllFabric.TAB_ADDEMALL; + } + @Override public String getPlatformName() { return "Fabric"; diff --git a/fabric/src/main/resources/META-INF/services/de.griefed.addemall.registry.RegistrationProvider$Factory b/fabric/src/main/resources/META-INF/services/de.griefed.addemall.registry.RegistrationProvider$Factory new file mode 100644 index 0000000..e8af1a8 --- /dev/null +++ b/fabric/src/main/resources/META-INF/services/de.griefed.addemall.registry.RegistrationProvider$Factory @@ -0,0 +1 @@ +de.griefed.addemall.FabricRegistrationFactory \ No newline at end of file diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json index 841241d..d31fe84 100644 --- a/fabric/src/main/resources/fabric.mod.json +++ b/fabric/src/main/resources/fabric.mod.json @@ -16,7 +16,7 @@ "environment": "*", "entrypoints": { "main": [ - "de.griefed.addemall.AddEmAll" + "de.griefed.addemall.AddEmAllFabric" ] }, "mixins": [ @@ -26,7 +26,7 @@ "depends": { "fabricloader": ">=0.14", "fabric": "*", - "minecraft": "1.20", + "minecraft": "1.19.2", "java": ">=17" }, "suggests": { diff --git a/forge/build.gradle b/forge/build.gradle index c27cc13..7e0477e 100644 --- a/forge/build.gradle +++ b/forge/build.gradle @@ -36,7 +36,7 @@ minecraft { mods { modClientRun { source sourceSets.main - source project(":common").sourceSets.main + source project(":Common").sourceSets.main } } } @@ -50,7 +50,7 @@ minecraft { mods { modServerRun { source sourceSets.main - source project(":common").sourceSets.main + source project(":Common").sourceSets.main } } } @@ -65,7 +65,7 @@ minecraft { mods { modDataRun { source sourceSets.main - source project(":common").sourceSets.main + source project(":Common").sourceSets.main } } } @@ -76,22 +76,22 @@ sourceSets.main.resources.srcDir 'src/generated/resources' dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" - compileOnly project(":common") + compileOnly project(":Common") annotationProcessor("org.spongepowered:mixin:0.8.5-SNAPSHOT:processor") } tasks.withType(JavaCompile).configureEach { - source(project(":common").sourceSets.main.allSource) + source(project(":Common").sourceSets.main.allSource) } tasks.withType(Javadoc).configureEach { - source(project(":common").sourceSets.main.allJava) + source(project(":Common").sourceSets.main.allJava) } tasks.named("sourcesJar", Jar) { - from(project(":common").sourceSets.main.allSource) + from(project(":Common").sourceSets.main.allSource) } processResources { - from project(":common").sourceSets.main.resources + from project(":Common").sourceSets.main.resources } jar.finalizedBy('reobfJar') diff --git a/forge/src/main/java/de/griefed/addemall/AddEmAllForge.java b/forge/src/main/java/de/griefed/addemall/AddEmAllForge.java index 64548f3..5685ef8 100644 --- a/forge/src/main/java/de/griefed/addemall/AddEmAllForge.java +++ b/forge/src/main/java/de/griefed/addemall/AddEmAllForge.java @@ -1,8 +1,9 @@ package de.griefed.addemall; import com.mojang.logging.LogUtils; -import de.griefed.addemall.block.ModBlocks; -import de.griefed.addemall.item.ModItems; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; @@ -19,6 +20,13 @@ public class AddEmAllForge { public static final String MOD_ID = "addemall"; private static final Logger LOGGER = LogUtils.getLogger(); + public static final CreativeModeTab TAB_ADDEMALL = new CreativeModeTab(Constants.MOD_ID + ".tab") { + @Override + public ItemStack makeIcon() { + return new ItemStack(Items.DIAMOND); + } + }; + // Very Important Comment public AddEmAllForge() { // Use Forge to bootstrap the Common mod. @@ -28,11 +36,6 @@ public AddEmAllForge() { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - ModItems.register(modEventBus); - ModBlocks.register(modEventBus); - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - modEventBus.addListener(this::commonSetup); MinecraftForge.EVENT_BUS.register(this); diff --git a/forge/src/main/java/de/griefed/addemall/ForgeRegistrationFactory.java b/forge/src/main/java/de/griefed/addemall/ForgeRegistrationFactory.java new file mode 100644 index 0000000..b040b10 --- /dev/null +++ b/forge/src/main/java/de/griefed/addemall/ForgeRegistrationFactory.java @@ -0,0 +1,87 @@ +package de.griefed.addemall; + +import de.griefed.addemall.registry.RegistrationProvider; +import de.griefed.addemall.registry.RegistryObject; +import net.minecraft.core.Holder; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.javafmlmod.FMLModContainer; +import net.minecraftforge.registries.DeferredRegister; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.function.Supplier; + +public class ForgeRegistrationFactory implements RegistrationProvider.Factory { + + @Override + public RegistrationProvider create(ResourceKey> resourceKey, String modId) { + final var containerOpt = ModList.get().getModContainerById(modId); + if (containerOpt.isEmpty()) + throw new NullPointerException("Cannot find mod container for id " + modId); + final var cont = containerOpt.get(); + if (cont instanceof FMLModContainer fmlModContainer) { + final var register = DeferredRegister.create(resourceKey, modId); + register.register(fmlModContainer.getEventBus()); + return new Provider<>(modId, register); + } else { + throw new ClassCastException("The container of the mod " + modId + " is not a FML one!"); + } + } + + private static class Provider implements RegistrationProvider { + private final String modId; + private final DeferredRegister registry; + + private final Set> entries = new HashSet<>(); + private final Set> entriesView = Collections.unmodifiableSet(entries); + + private Provider(String modId, DeferredRegister registry) { + this.modId = modId; + this.registry = registry; + } + + @Override + public String getModId() { + return modId; + } + + @Override + @SuppressWarnings("unchecked") + public RegistryObject register(String name, Supplier supplier) { + final var obj = registry.register(name, supplier); + final var ro = new RegistryObject() { + + @Override + public ResourceKey getResourceKey() { + return obj.getKey(); + } + + @Override + public ResourceLocation getId() { + return obj.getId(); + } + + @Override + public I get() { + return obj.get(); + } + + @Override + public Holder asHolder() { + return obj.getHolder().orElseThrow(); + } + }; + entries.add((RegistryObject) ro); + return ro; + } + + @Override + public Set> getEntries() { + return entriesView; + } + } +} \ No newline at end of file diff --git a/forge/src/main/java/de/griefed/addemall/block/ModBlocks.java b/forge/src/main/java/de/griefed/addemall/block/ModBlocks.java deleted file mode 100644 index 85f26c7..0000000 --- a/forge/src/main/java/de/griefed/addemall/block/ModBlocks.java +++ /dev/null @@ -1,52 +0,0 @@ -package de.griefed.addemall.block; - -import de.griefed.addemall.AddEmAllForge; -import de.griefed.addemall.item.ModItems; -import net.minecraft.world.item.BlockItem; -import net.minecraft.world.item.CreativeModeTab; -import net.minecraft.world.item.Item; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.state.BlockBehaviour; -import net.minecraft.world.level.material.Material; -import net.minecraftforge.eventbus.api.IEventBus; -import net.minecraftforge.registries.DeferredRegister; -import net.minecraftforge.registries.ForgeRegistries; -import net.minecraftforge.registries.RegistryObject; - -import java.util.function.Supplier; - -public class ModBlocks { - public static final DeferredRegister BLOCKS = - DeferredRegister.create(ForgeRegistries.BLOCKS, AddEmAllForge.MOD_ID); - - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - public static final RegistryObject TUTORIAL_STONE_BLOCK = registerBlock("tutorial_stone_block", - () -> new Block(BlockBehaviour.Properties.of(Material.STONE) - .strength(6f).lightLevel(state -> 0).explosionResistance(8f) - .requiresCorrectToolForDrops()), CreativeModeTab.TAB_BUILDING_BLOCKS); - - public static final RegistryObject TUTORIAL_WOOL_BLOCK = registerBlock("tutorial_wool_block", - () -> new Block(BlockBehaviour.Properties.of(Material.WOOL) - .strength(4f).lightLevel(state -> 5).explosionResistance(6f) - ), CreativeModeTab.TAB_BUILDING_BLOCKS); - - public static final RegistryObject TUTORIAL_SAND_BLOCK = registerBlock("tutorial_sand_block", - () -> new Block(BlockBehaviour.Properties.of(Material.SAND) - .strength(2f).lightLevel(state -> 12).explosionResistance(2f) - .instabreak()), CreativeModeTab.TAB_BUILDING_BLOCKS); - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - - private static RegistryObject registerBlock(String name, Supplier block, CreativeModeTab tab) { - RegistryObject toReturn = BLOCKS.register(name, block); - registerBlockItem(name, toReturn, tab); - return toReturn; - } - - private static RegistryObject registerBlockItem(String name, RegistryObject block, CreativeModeTab tab) { - return ModItems.ITEMS.register(name, () -> new BlockItem(block.get(), new Item.Properties().tab(tab))); - } - - public static void register(IEventBus eventBus) { - BLOCKS.register(eventBus); - } -} diff --git a/forge/src/main/java/de/griefed/addemall/item/ModItems.java b/forge/src/main/java/de/griefed/addemall/item/ModItems.java deleted file mode 100644 index 28aba91..0000000 --- a/forge/src/main/java/de/griefed/addemall/item/ModItems.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.griefed.addemall.item; - -import de.griefed.addemall.AddEmAllForge; -import net.minecraft.world.item.Item; -import net.minecraftforge.eventbus.api.IEventBus; -import net.minecraftforge.registries.DeferredRegister; -import net.minecraftforge.registries.ForgeRegistries; -import net.minecraftforge.registries.RegistryObject; - -public class ModItems { - public static final DeferredRegister ITEMS = - DeferredRegister.create(ForgeRegistries.ITEMS, AddEmAllForge.MOD_ID); - - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - - /*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/ - - public static void register(IEventBus eventBus) { - ITEMS.register(eventBus); - } -} diff --git a/forge/src/main/java/de/griefed/addemall/platform/ForgePlatformHelper.java b/forge/src/main/java/de/griefed/addemall/platform/ForgePlatformHelper.java index 56dcbb7..608be6a 100644 --- a/forge/src/main/java/de/griefed/addemall/platform/ForgePlatformHelper.java +++ b/forge/src/main/java/de/griefed/addemall/platform/ForgePlatformHelper.java @@ -1,11 +1,18 @@ package de.griefed.addemall.platform; +import de.griefed.addemall.AddEmAllForge; import de.griefed.addemall.platform.services.IPlatformHelper; +import net.minecraft.world.item.CreativeModeTab; import net.minecraftforge.fml.ModList; import net.minecraftforge.fml.loading.FMLLoader; public class ForgePlatformHelper implements IPlatformHelper { + @Override + public CreativeModeTab getCreativeTab() { + return AddEmAllForge.TAB_ADDEMALL; + } + @Override public String getPlatformName() { diff --git a/forge/src/main/resources/META-INF/services/de.griefed.addemall.registry.RegistrationProvider$Factory b/forge/src/main/resources/META-INF/services/de.griefed.addemall.registry.RegistrationProvider$Factory new file mode 100644 index 0000000..baeefb0 --- /dev/null +++ b/forge/src/main/resources/META-INF/services/de.griefed.addemall.registry.RegistrationProvider$Factory @@ -0,0 +1 @@ +de.griefed.addemall.ForgeRegistrationFactory \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 6c1adae..c6478bc 100644 --- a/settings.gradle +++ b/settings.gradle @@ -22,7 +22,7 @@ plugins { // This should match the folder name of the project, or else IDEA may complain (see https://youtrack.jetbrains.com/issue/IDEA-317606) rootProject.name = 'AddEmAll' -include("common") -include("fabric") -include("forge") +include("Common") +include("Fabric") +include("Forge")