From 1d54e718b8e5fbf0e4d5a0344319ab0d840f562b Mon Sep 17 00:00:00 2001 From: Half_nothing <50048293+half-nothing@users.noreply.github.com> Date: Tue, 12 Dec 2023 12:22:43 +0800 Subject: [PATCH] Feat/AddBlockLootTable (#16) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #fix: 优化扳手的行为逻辑,修复扳手工具提示错误 * #feat: DataGen添加战利品表提供器,为可挖掘方块添加战利品表(钻头等多方块结构暂未添加),现在他们可以在挖掘后掉落自身了 --- build.gradle | 1 + .../java/com/hechu/mindustry/Mindustry.java | 4 +- .../hechu/mindustry/MindustryConstants.java | 41 ++- .../hechu/mindustry/config/CommonConfig.java | 5 +- .../hechu/mindustry/config/ConfigHandler.java | 2 +- .../MindustryProcessingIngredient.java | 4 +- .../recipes/MindustryProcessingRecipe.java | 1 - .../hechu/mindustry/datagen/DataGenEvent.java | 256 +----------------- .../MindustryBlockLootSubProvider.java | 29 ++ .../MindustryBlockModelProvider.java} | 19 +- .../MindustryBlockStateProvider.java} | 21 +- .../provider/MindustryBlockTagsProvider.java | 26 ++ .../provider/MindustryItemModelProvider.java | 26 ++ .../provider/MindustryLootTableProvider.java | 36 +++ .../MindustryMutilBlockLootSubProvider.java | 36 +++ .../provider/MindustryRecipeProvider.java | 38 +++ .../java/com/hechu/mindustry/utils/Utils.java | 16 ++ .../mindustry/world/item/tools/Wrench.java | 84 ++++-- .../assets/mindustry/lang/en_US.json | 9 +- .../assets/mindustry/lang/zh_CN.json | 9 +- 20 files changed, 355 insertions(+), 308 deletions(-) create mode 100644 src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockLootSubProvider.java rename src/main/java/com/hechu/mindustry/datagen/{block/TurretBlockModelGenerator.java => provider/MindustryBlockModelProvider.java} (93%) rename src/main/java/com/hechu/mindustry/datagen/{block/TurretBlockStateGenerator.java => provider/MindustryBlockStateProvider.java} (62%) create mode 100644 src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockTagsProvider.java create mode 100644 src/main/java/com/hechu/mindustry/datagen/provider/MindustryItemModelProvider.java create mode 100644 src/main/java/com/hechu/mindustry/datagen/provider/MindustryLootTableProvider.java create mode 100644 src/main/java/com/hechu/mindustry/datagen/provider/MindustryMutilBlockLootSubProvider.java create mode 100644 src/main/java/com/hechu/mindustry/datagen/provider/MindustryRecipeProvider.java diff --git a/build.gradle b/build.gradle index 09497fa..4ba5d4e 100644 --- a/build.gradle +++ b/build.gradle @@ -218,6 +218,7 @@ dependencies { implementation fg.deobf("curse.maven:just-enough-characters-250702:${justEnoughCharacters_id}") implementation fg.deobf("curse.maven:kiwi-303657:${kiwi_fileId}") + implementation("org.apache.commons:commons-io:1.3.2") // annotationProcessor "curse.maven:kiwi-303657:${kiwi_fileId}" // Example mod dependency with JEI - using fg.deobf() ensures the dependency is remapped to your development mappings // The JEI API is declared for compile time use, while the full JEI artifact is used at runtime diff --git a/src/main/java/com/hechu/mindustry/Mindustry.java b/src/main/java/com/hechu/mindustry/Mindustry.java index c5fcca3..01fe5e1 100644 --- a/src/main/java/com/hechu/mindustry/Mindustry.java +++ b/src/main/java/com/hechu/mindustry/Mindustry.java @@ -26,8 +26,8 @@ @Mod(MindustryConstants.MOD_ID) public class Mindustry { public Mindustry() { - MindustryConstants.config_folder = FMLPaths.GAMEDIR.get().resolve("config/" + MindustryConstants.MOD_ID); - Utils.checkFolder(MindustryConstants.config_folder); + MindustryConstants.configFolder = FMLPaths.GAMEDIR.get().resolve("config/" + MindustryConstants.MOD_ID); + Utils.checkFolder(MindustryConstants.configFolder); MindustryConstants.commonConfig = ConfigHandler.readConfig("common", CommonConfig.class); // IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); diff --git a/src/main/java/com/hechu/mindustry/MindustryConstants.java b/src/main/java/com/hechu/mindustry/MindustryConstants.java index 429fbb4..c84af5d 100644 --- a/src/main/java/com/hechu/mindustry/MindustryConstants.java +++ b/src/main/java/com/hechu/mindustry/MindustryConstants.java @@ -9,21 +9,42 @@ public class MindustryConstants { public static final Logger logger = LogUtils.getLogger(); public static final String MOD_ID = "mindustry"; - public static Path config_folder; + public static Path configFolder; public static CommonConfig commonConfig; /** * 用于规范翻译文档 */ - public static final String CHAT = "chat." + MOD_ID + "."; - public static final String CHAT_WARN = CHAT + "warning."; - public static final String CHAT_INFO = CHAT + "info."; - public static final String CHAT_COMMAND = CHAT + "command."; + public enum Chat { + CHAT("chat." + MOD_ID), + CHAT_WARN(CHAT.msg + ".warning."), + CHAT_INFO(CHAT.msg + ".info."), + CHAT_COMMAND(CHAT.msg + ".command."); + public final String msg; - public static final String DESC = "desc." + MOD_ID + "."; - public static final String DESC_INFO = DESC + "info."; - public static final String DESC_FLAVOUR = DESC + "flavour."; + Chat(String string) { + msg = string; + } + } - public static final String GUI = "gui." + MOD_ID + "."; - public static final String GUI_CONFIG = "gui." + MOD_ID + ".config."; + public enum Desc { + DESC("desc." + MOD_ID), + DESC_INFO(DESC.msg + ".info."), + DESC_FLAVOUR(DESC.msg + ".flavour."); + public final String msg; + + Desc(String string) { + msg = string; + } + } + + public enum Gui { + GUI("gui." + MOD_ID), + GUI_CONFIG(GUI.msg + ".config."); + public final String msg; + + Gui(String string) { + msg = string; + } + } } diff --git a/src/main/java/com/hechu/mindustry/config/CommonConfig.java b/src/main/java/com/hechu/mindustry/config/CommonConfig.java index be1a638..def52ba 100644 --- a/src/main/java/com/hechu/mindustry/config/CommonConfig.java +++ b/src/main/java/com/hechu/mindustry/config/CommonConfig.java @@ -5,16 +5,19 @@ import java.nio.file.Path; -public class CommonConfig extends Config{ +public class CommonConfig extends Config { public CommonConfig(String configName, Path configPath) { super(configName, configPath); } + @Expose() @SerializedName("common") private Common common = new Common(); + public Common getCommon() { return this.common; } + public static class Common { @Expose() @SerializedName("mining_speed") diff --git a/src/main/java/com/hechu/mindustry/config/ConfigHandler.java b/src/main/java/com/hechu/mindustry/config/ConfigHandler.java index a4c4f7b..9936b8f 100644 --- a/src/main/java/com/hechu/mindustry/config/ConfigHandler.java +++ b/src/main/java/com/hechu/mindustry/config/ConfigHandler.java @@ -19,7 +19,7 @@ public class ConfigHandler { @Nullable public static T readConfig(String configName, Class configClass) { configName = "%s_%s".formatted(MOD_ID, configName); - Path configPath = config_folder.resolve("%s.json".formatted(configName)); + Path configPath = configFolder.resolve("%s.json".formatted(configName)); if (configPath.toFile().isFile()) { try { return GSON.fromJson(FileUtils.readFileToString(configPath.toFile(), StandardCharsets.UTF_8), diff --git a/src/main/java/com/hechu/mindustry/data/recipes/MindustryProcessingIngredient.java b/src/main/java/com/hechu/mindustry/data/recipes/MindustryProcessingIngredient.java index fd9594c..e81de8b 100644 --- a/src/main/java/com/hechu/mindustry/data/recipes/MindustryProcessingIngredient.java +++ b/src/main/java/com/hechu/mindustry/data/recipes/MindustryProcessingIngredient.java @@ -62,7 +62,7 @@ public boolean isEmpty() { itemStack = itemStackFromJson(jsonObject); } else if (jsonObject.has("tag")) { //TODO - throw new JsonParseException("MindustryProcessingIngredient ݲ֧ Tag"); + throw new JsonParseException("MindustryProcessingIngredient Tag"); // ResourceLocation resourcelocation = new ResourceLocation(GsonHelper.getAsString(json, "tag")); // TagKey tagkey = TagKey.create(Registries.ITEM, resourcelocation); // return new TagValue(tagkey); @@ -92,7 +92,7 @@ public static MindustryProcessingIngredient of(ItemLike item, int count) { } public static @NotNull MindustryProcessingIngredient of(TagKey tag) { - throw new NotImplementedException("MindustryProcessingIngredient ݲ֧ Tag"); + throw new NotImplementedException("MindustryProcessingIngredient �ݲ�֧�� Tag"); } public static MindustryProcessingIngredient of(ItemStack itemStack) { diff --git a/src/main/java/com/hechu/mindustry/data/recipes/MindustryProcessingRecipe.java b/src/main/java/com/hechu/mindustry/data/recipes/MindustryProcessingRecipe.java index e27b3b1..9c3b368 100644 --- a/src/main/java/com/hechu/mindustry/data/recipes/MindustryProcessingRecipe.java +++ b/src/main/java/com/hechu/mindustry/data/recipes/MindustryProcessingRecipe.java @@ -55,7 +55,6 @@ public MindustryProcessingRecipe(ResourceLocation id, String group, int processT } /** - * ȡÿƷӦĵƷ * * @param blockEntity * @return diff --git a/src/main/java/com/hechu/mindustry/datagen/DataGenEvent.java b/src/main/java/com/hechu/mindustry/datagen/DataGenEvent.java index dea03f8..6283ca3 100644 --- a/src/main/java/com/hechu/mindustry/datagen/DataGenEvent.java +++ b/src/main/java/com/hechu/mindustry/datagen/DataGenEvent.java @@ -1,36 +1,15 @@ package com.hechu.mindustry.datagen; -import com.hechu.mindustry.MindustryConstants; -import com.hechu.mindustry.annotation.Block; -import com.hechu.mindustry.data.recipes.MindustryProcessingRecipeBuilder; -import com.hechu.mindustry.kiwi.BlockModule; -import com.hechu.mindustry.kiwi.ItemModule; -import com.hechu.mindustry.kiwi.MutilBlockModule; -import com.hechu.mindustry.kiwi.RecipeModule; -import net.minecraft.core.Direction; +import com.hechu.mindustry.datagen.provider.*; import net.minecraft.core.HolderLookup; -import net.minecraft.core.Vec3i; import net.minecraft.data.DataGenerator; import net.minecraft.data.PackOutput; -import net.minecraft.data.recipes.*; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.Items; -import net.minecraft.world.level.block.state.properties.IntegerProperty; -import net.minecraftforge.client.model.generators.BlockModelProvider; -import net.minecraftforge.client.model.generators.BlockStateProvider; -import net.minecraftforge.client.model.generators.ConfiguredModel; -import net.minecraftforge.client.model.generators.ItemModelProvider; -import net.minecraftforge.common.Tags; -import net.minecraftforge.common.data.BlockTagsProvider; import net.minecraftforge.common.data.ExistingFileHelper; import net.minecraftforge.data.event.GatherDataEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; -import org.jetbrains.annotations.NotNull; -import javax.annotation.Nullable; import java.util.concurrent.CompletableFuture; -import java.util.function.Consumer; @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) @@ -46,237 +25,6 @@ public static void dataGen(GatherDataEvent event) { gen.addProvider(event.includeClient(), new MindustryBlockTagsProvider(packOutput, lookupProvider, existingFileHelper)); gen.addProvider(event.includeClient(), new MindustryItemModelProvider(packOutput, existingFileHelper)); gen.addProvider(event.includeServer(), new MindustryRecipeProvider(packOutput)); - } - - // TODO 方块战利品生成 - public static class MindustryBlockTagsProvider extends BlockTagsProvider { - public MindustryBlockTagsProvider(PackOutput output, CompletableFuture lookupProvider, @Nullable ExistingFileHelper existingFileHelper) { - super(output, lookupProvider, MindustryConstants.MOD_ID, existingFileHelper); - } - - @Override - protected void addTags(HolderLookup.@NotNull Provider pProvider) { - tag(Tags.Blocks.ORES_COAL).add(BlockModule.COAL_ORE_BLOCK.get()); - tag(Tags.Blocks.ORES_COPPER).add(BlockModule.COPPER_ORE_BLOCK.get()); - } - } - - public static class MindustryItemModelProvider extends ItemModelProvider { - public MindustryItemModelProvider(PackOutput packOutput, ExistingFileHelper existingFileHelper) { - super(packOutput, MindustryConstants.MOD_ID, existingFileHelper); - } - - @Override - protected void registerModels() { - ItemModule.getRegisterName().forEach(this::registerItem); - BlockModule.getRegisterName().forEach(this::registerBlockItem); - } - - private void registerItem(String name) { - this.singleTexture(name, new ResourceLocation("item/generated"), "layer0", new ResourceLocation(MindustryConstants.MOD_ID, "item/%s".formatted(name))); - } - - private void registerBlockItem(String name) { - this.withExistingParent(name, new ResourceLocation(MindustryConstants.MOD_ID, "block/%s".formatted(name))); - } - } - - public static class MindustryBlockModelProvider extends BlockModelProvider { - public MindustryBlockModelProvider(PackOutput packOutput, ExistingFileHelper existingFileHelper) { - super(packOutput, MindustryConstants.MOD_ID, existingFileHelper); - } - - @Override - protected void registerModels() { - BlockModule.getRegisterName().forEach(this::registerBlockModel); - registerMutilationModels(); - } - - private void registerBlockModel(String name) { - this.cubeAll(name, new ResourceLocation(MindustryConstants.MOD_ID, "block/%s".formatted(name))); - } - - void registerMutilationModels() { - MutilBlockModule.getBlocks().forEach(block -> { - Vec3i size = block.getSize(); - String name = block.getBlockName(); - int sizeX = size.getX(); - int sizeZ = size.getZ(); - int sizeY = size.getY(); - for (int x = 0; x < sizeX; x++) { - for (int z = 0; z < sizeZ; z++) { - for (int y = 0; y < sizeY; y++) { - int i = x + z * sizeX + y * sizeX * sizeZ; - int finalX = x; - int finalZ = z; - int finalY = y; - var elementBuilder = block.isSingleTexture() ? - this.getBuilder("block/" + name + "/" + name + "_" + i) - .texture("0", "block/" + name + "/" + name) - .texture("particle", new ResourceLocation("minecraft", "block/stone")) - .element().from(0, 0, 0).to(16, 16, 16) - : - this.getBuilder("block/" + name + "/" + name + "_" + i) - .texture("west", "block/" + name + "/" + name + "_west") - .texture("east", "block/" + name + "/" + name + "_east") - .texture("north", "block/" + name + "/" + name + "_north") - .texture("south", "block/" + name + "/" + name + "_south") - .texture("up", "block/" + name + "/" + name + "_up") - .texture("down", "block/" + name + "/" + name + "_down") - .texture("particle", new ResourceLocation("minecraft", "block/stone")) - .element().from(0, 0, 0).to(16, 16, 16); - if (finalX == 0) { - elementBuilder.face(Direction.WEST); - } - if (finalX == sizeX - 1) { - elementBuilder.face(Direction.EAST); - } - if (finalZ == 0) { - elementBuilder.face(Direction.NORTH); - } - if (finalZ == sizeZ - 1) { - elementBuilder.face(Direction.SOUTH); - } - if (finalY == 0) { - elementBuilder.face(Direction.DOWN); - } - if (finalY == sizeY - 1) { - elementBuilder.face(Direction.UP); - } - elementBuilder - .faces((direction, faceBuilder) -> { - switch (direction) { - case WEST -> { - if (finalX == 0) { - float u1 = (16f * (finalZ)) / sizeZ; - float v1 = 16 - (16f * (finalY + 1)) / sizeY; - float u2 = u1 + 16f / sizeZ; - float v2 = v1 + 16f / sizeY; -// float u1 = (16f * finalZ) / sizeZ; -// float v1 = 16 - (16f * finalY) / sizeY; -// float u2 = u1 + 16f / sizeZ; -// float v2 = v1 - 16f / sizeY; - faceBuilder.uvs(u1, v1, u2, v2).texture(block.isSingleTexture() ? "#0" : "#west"); - } - } - case EAST -> { - if (finalX == sizeX - 1) { - float u1 = 16 - (16f * (finalZ + 1)) / sizeZ; - float v1 = 16 - (16f * (finalY + 1)) / sizeY; - float u2 = u1 + 16f / sizeZ; - float v2 = v1 + 16f / sizeY; -// float u1 = 16 - (16f * finalZ) / sizeZ; -// float v1 = 16 - (16f * finalY) / sizeY; -// float u2 = u1 - 16f / sizeZ; -// float v2 = v1 - 16f / sizeY; - faceBuilder.uvs(u1, v1, u2, v2).texture(block.isSingleTexture() ? "#0" : "#east"); - } - } - case NORTH -> { - if (finalZ == 0) { - float u1 = 16 - (16f * (finalX + 1)) / sizeX; - float v1 = 16 - (16f * (finalY + 1)) / sizeY; - float u2 = u1 + 16f / sizeX; - float v2 = v1 + 16f / sizeY; -// float u1 = 16 - (16f * finalX) / sizeX; -// float v1 = 16 - (16f * finalY) / sizeY; -// float u2 = u1 - 16f / sizeX; -// float v2 = v1 - 16f / sizeY; - faceBuilder.uvs(u1, v1, u2, v2).texture(block.isSingleTexture() ? "#0" : "#north"); - } - } - case SOUTH -> { - if (finalZ == sizeZ - 1) { - float u1 = (16f * finalX) / sizeX; - float v1 = 16 - (16f * (finalY + 1)) / sizeY; - float u2 = u1 + 16f / sizeX; - float v2 = v1 + 16f / sizeY; -// float u1 = (16f * finalX) / sizeX; -// float v1 = 16 - (16f * finalY) / sizeY; -// float u2 = u1 + 16f / sizeX; -// float v2 = v1 - 16f / sizeY; - faceBuilder.uvs(u1, v1, u2, v2).texture(block.isSingleTexture() ? "#0" : "#south"); - } - } - case DOWN -> { - if (finalY == 0) { - float u1 = 16 - (16f * finalX) / sizeX; - float v1 = 16 + (16f * (finalZ - 1)) / sizeZ; - float u2 = u1 - 16f / sizeX; - float v2 = v1 - 16f / sizeZ; -// float u1 = 16 - (16f * finalX) / sizeX; -// float v1 = (16f * finalZ) / sizeZ; -// float u2 = u1 - 16f / sizeX; -// float v2 = v1 + 16f / sizeZ; - faceBuilder.uvs(u1, v1, u2, v2).texture(block.isSingleTexture() ? "#0" : "#down"); - } - } - case UP -> { - if (finalY == sizeY - 1) { - float u1 = 16 - (16f * finalX) / sizeX; - float v1 = 16 - (16f * finalZ) / sizeZ; - float u2 = u1 - 16f / sizeX; - float v2 = v1 - 16f / sizeZ; - faceBuilder.uvs(u1, v1, u2, v2).texture(block.isSingleTexture() ? "#0" : "#up"); - } - } - } - }) - .end(); - } - } - } - }); - } - } - - public static class MindustryBlockStateProvider extends BlockStateProvider { - public MindustryBlockStateProvider(PackOutput output, ExistingFileHelper exFileHelper) { - super(output, MindustryConstants.MOD_ID, exFileHelper); - } - - @Override - protected void registerStatesAndModels() { - BlockModule.getBlocks().forEach(this::registerBlockState); - registerMultiblockStatesAndModels(); - } - - private void registerBlockState(net.minecraft.world.level.block.Block block) { - this.simpleBlock(block); - } - - void registerMultiblockStatesAndModels() { - MutilBlockModule.getBlocks().forEach(block -> { - String name = block.getClass().getAnnotation(Block.class).name(); - this.getVariantBuilder(block) - .forAllStates(state -> { - IntegerProperty partProperty = block.getPartProperty(); - int part = state.getValue(partProperty); - return ConfiguredModel.builder() - .modelFile(this.models().getExistingFile(this.modLoc("block/" + name + "/" + name + "_" + part))) - .build(); - }); - }); - } - } - - public static class MindustryRecipeProvider extends RecipeProvider { - - public MindustryRecipeProvider(PackOutput output) { - super(output); - } - - @Override - protected void buildRecipes(@NotNull Consumer writer) { - new MindustryProcessingRecipeBuilder(RecipeModule.KILN_RECIPE_SERIALIZER.get(), RecipeCategory.MISC, Items.GLASS, 8) - .requires(Items.SAND, 8).processTick(80) - .save(writer, new ResourceLocation(MindustryConstants.MOD_ID, "kiln_sand_to_glass")); - new MindustryProcessingRecipeBuilder(RecipeModule.KILN_RECIPE_SERIALIZER.get(), RecipeCategory.MISC, ItemModule.SURGE_ALLOY.get(), 2) - .requires(ItemModule.COPPER.get(),32).processTick(10) - .save(writer, new ResourceLocation(MindustryConstants.MOD_ID, "kiln_copper_to_surge_alloy")); - new MindustryProcessingRecipeBuilder(RecipeModule.KILN_RECIPE_SERIALIZER.get(), RecipeCategory.MISC, Items.DIAMOND, 1) - .requires(ItemModule.COAL.get(), 8).processTick(10) - .save(writer, new ResourceLocation(MindustryConstants.MOD_ID, "kiln_coal_to_diamond")); - } + gen.addProvider(event.includeServer(), new MindustryLootTableProvider(packOutput)); } } diff --git a/src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockLootSubProvider.java b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockLootSubProvider.java new file mode 100644 index 0000000..9634774 --- /dev/null +++ b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockLootSubProvider.java @@ -0,0 +1,29 @@ +package com.hechu.mindustry.datagen.provider; + +import com.hechu.mindustry.kiwi.BlockModule; +import com.hechu.mindustry.kiwi.MutilBlockModule; +import net.minecraft.data.loot.BlockLootSubProvider; +import net.minecraft.world.flag.FeatureFlagSet; +import net.minecraft.world.flag.FeatureFlags; +import net.minecraft.world.item.Item; +import net.minecraft.world.level.block.Block; + +import java.util.Set; +import java.util.stream.Collectors; + +public class MindustryBlockLootSubProvider extends BlockLootSubProvider { + private static final Set knownBlock = BlockModule.getBlocks(); + protected MindustryBlockLootSubProvider() { + super(Set.of(), FeatureFlags.REGISTRY.allFlags()); + } + + @Override + protected void generate() { + BlockModule.getBlocks().forEach(this::dropSelf); + } + + @Override + protected Iterable getKnownBlocks() { + return knownBlock; + } +} diff --git a/src/main/java/com/hechu/mindustry/datagen/block/TurretBlockModelGenerator.java b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockModelProvider.java similarity index 93% rename from src/main/java/com/hechu/mindustry/datagen/block/TurretBlockModelGenerator.java rename to src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockModelProvider.java index 9b0108f..ec8c79b 100644 --- a/src/main/java/com/hechu/mindustry/datagen/block/TurretBlockModelGenerator.java +++ b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockModelProvider.java @@ -1,5 +1,7 @@ -package com.hechu.mindustry.datagen.block; +package com.hechu.mindustry.datagen.provider; +import com.hechu.mindustry.MindustryConstants; +import com.hechu.mindustry.kiwi.BlockModule; import com.hechu.mindustry.kiwi.MutilBlockModule; import net.minecraft.core.Direction; import net.minecraft.core.Vec3i; @@ -8,13 +10,22 @@ import net.minecraftforge.client.model.generators.BlockModelProvider; import net.minecraftforge.common.data.ExistingFileHelper; -public class TurretBlockModelGenerator extends BlockModelProvider { - public TurretBlockModelGenerator(PackOutput output, String modid, ExistingFileHelper existingFileHelper) { - super(output, modid, existingFileHelper); +public class MindustryBlockModelProvider extends BlockModelProvider { + public MindustryBlockModelProvider(PackOutput packOutput, ExistingFileHelper existingFileHelper) { + super(packOutput, MindustryConstants.MOD_ID, existingFileHelper); } @Override protected void registerModels() { + BlockModule.getRegisterName().forEach(this::registerBlockModel); + registerMutilationModels(); + } + + private void registerBlockModel(String name) { + this.cubeAll(name, new ResourceLocation(MindustryConstants.MOD_ID, "block/%s".formatted(name))); + } + + void registerMutilationModels() { MutilBlockModule.getBlocks().forEach(block -> { Vec3i size = block.getSize(); String name = block.getBlockName(); diff --git a/src/main/java/com/hechu/mindustry/datagen/block/TurretBlockStateGenerator.java b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockStateProvider.java similarity index 62% rename from src/main/java/com/hechu/mindustry/datagen/block/TurretBlockStateGenerator.java rename to src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockStateProvider.java index bd2fdb4..039e847 100644 --- a/src/main/java/com/hechu/mindustry/datagen/block/TurretBlockStateGenerator.java +++ b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockStateProvider.java @@ -1,6 +1,8 @@ -package com.hechu.mindustry.datagen.block; +package com.hechu.mindustry.datagen.provider; +import com.hechu.mindustry.MindustryConstants; import com.hechu.mindustry.annotation.Block; +import com.hechu.mindustry.kiwi.BlockModule; import com.hechu.mindustry.kiwi.MutilBlockModule; import net.minecraft.data.PackOutput; import net.minecraft.world.level.block.state.properties.IntegerProperty; @@ -8,13 +10,22 @@ import net.minecraftforge.client.model.generators.ConfiguredModel; import net.minecraftforge.common.data.ExistingFileHelper; -public class TurretBlockStateGenerator extends BlockStateProvider { - public TurretBlockStateGenerator(PackOutput output, String modid, ExistingFileHelper exFileHelper) { - super(output, modid, exFileHelper); +public class MindustryBlockStateProvider extends BlockStateProvider { + public MindustryBlockStateProvider(PackOutput output, ExistingFileHelper exFileHelper) { + super(output, MindustryConstants.MOD_ID, exFileHelper); } @Override protected void registerStatesAndModels() { + BlockModule.getBlocks().forEach(this::registerBlockState); + registerMultiblockStatesAndModels(); + } + + private void registerBlockState(net.minecraft.world.level.block.Block block) { + this.simpleBlock(block); + } + + void registerMultiblockStatesAndModels() { MutilBlockModule.getBlocks().forEach(block -> { String name = block.getClass().getAnnotation(Block.class).name(); this.getVariantBuilder(block) @@ -27,4 +38,4 @@ protected void registerStatesAndModels() { }); }); } -} +} \ No newline at end of file diff --git a/src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockTagsProvider.java b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockTagsProvider.java new file mode 100644 index 0000000..3133c6b --- /dev/null +++ b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryBlockTagsProvider.java @@ -0,0 +1,26 @@ +package com.hechu.mindustry.datagen.provider; + +import com.hechu.mindustry.MindustryConstants; +import com.hechu.mindustry.kiwi.BlockModule; +import net.minecraft.core.HolderLookup; +import net.minecraft.data.PackOutput; +import net.minecraftforge.common.Tags; +import net.minecraftforge.common.data.BlockTagsProvider; +import net.minecraftforge.common.data.ExistingFileHelper; +import org.jetbrains.annotations.NotNull; + +import javax.annotation.Nullable; +import java.util.concurrent.CompletableFuture; + +public class MindustryBlockTagsProvider extends BlockTagsProvider { + public MindustryBlockTagsProvider(PackOutput output, CompletableFuture lookupProvider, @Nullable ExistingFileHelper existingFileHelper) { + super(output, lookupProvider, MindustryConstants.MOD_ID, existingFileHelper); + } + + @Override + protected void addTags(HolderLookup.@NotNull Provider pProvider) { + tag(Tags.Blocks.ORES_COAL).add(BlockModule.COAL_ORE_BLOCK.get()); + tag(Tags.Blocks.ORES_COPPER).add(BlockModule.COPPER_ORE_BLOCK.get()); + } +} + diff --git a/src/main/java/com/hechu/mindustry/datagen/provider/MindustryItemModelProvider.java b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryItemModelProvider.java new file mode 100644 index 0000000..8fd62a2 --- /dev/null +++ b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryItemModelProvider.java @@ -0,0 +1,26 @@ +package com.hechu.mindustry.datagen.provider; + +import com.hechu.mindustry.MindustryConstants; +import com.hechu.mindustry.kiwi.BlockModule; +import com.hechu.mindustry.kiwi.ItemModule; +import net.minecraft.data.PackOutput; +import net.minecraft.resources.ResourceLocation; +import net.minecraftforge.client.model.generators.ItemModelProvider; +import net.minecraftforge.common.data.ExistingFileHelper; + +public class MindustryItemModelProvider extends ItemModelProvider { + public MindustryItemModelProvider(PackOutput packOutput, ExistingFileHelper existingFileHelper) { + super(packOutput, MindustryConstants.MOD_ID, existingFileHelper); + } + @Override + protected void registerModels() { + ItemModule.getRegisterName().forEach(this::registerItem); + BlockModule.getRegisterName().forEach(this::registerBlockItem); + } + private void registerItem(String name) { + this.singleTexture(name, new ResourceLocation("item/generated"), "layer0", new ResourceLocation(MindustryConstants.MOD_ID, "item/%s".formatted(name))); + } + private void registerBlockItem(String name) { + this.withExistingParent(name, new ResourceLocation(MindustryConstants.MOD_ID, "block/%s".formatted(name))); + } +} diff --git a/src/main/java/com/hechu/mindustry/datagen/provider/MindustryLootTableProvider.java b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryLootTableProvider.java new file mode 100644 index 0000000..97b7c5f --- /dev/null +++ b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryLootTableProvider.java @@ -0,0 +1,36 @@ +package com.hechu.mindustry.datagen.provider; + +import com.hechu.mindustry.MindustryConstants; +import com.hechu.mindustry.kiwi.BlockModule; +import com.hechu.mindustry.kiwi.MutilBlockModule; +import net.minecraft.data.PackOutput; +import net.minecraft.data.loot.LootTableProvider; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.level.storage.loot.LootTable; +import net.minecraft.world.level.storage.loot.ValidationContext; +import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class MindustryLootTableProvider extends LootTableProvider { + public MindustryLootTableProvider(PackOutput pOutput) { + super(pOutput, + Stream.concat(BlockModule.getRegisterName() + .stream() + .map(name -> new ResourceLocation(MindustryConstants.MOD_ID, name)), + MutilBlockModule.getRegisterName() + .stream() + .map(name -> new ResourceLocation(MindustryConstants.MOD_ID, name))) + .collect(Collectors.toSet()), + List.of(new SubProviderEntry(MindustryBlockLootSubProvider::new, LootContextParamSets.BLOCK), + new SubProviderEntry(MindustryMutilBlockLootSubProvider::new, LootContextParamSets.BLOCK))); + } + + @Override + protected void validate(Map map, ValidationContext validationcontext) { + map.forEach((key, value) -> value.validate(validationcontext)); + } +} diff --git a/src/main/java/com/hechu/mindustry/datagen/provider/MindustryMutilBlockLootSubProvider.java b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryMutilBlockLootSubProvider.java new file mode 100644 index 0000000..611f406 --- /dev/null +++ b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryMutilBlockLootSubProvider.java @@ -0,0 +1,36 @@ +package com.hechu.mindustry.datagen.provider; + +import com.hechu.mindustry.MindustryConstants; +import com.hechu.mindustry.kiwi.MutilBlockModule; +import net.minecraft.data.loot.BlockLootSubProvider; +import net.minecraft.world.flag.FeatureFlags; +import net.minecraft.world.level.block.Block; + +import java.util.Set; +import java.util.stream.Collectors; + +public class MindustryMutilBlockLootSubProvider extends BlockLootSubProvider { + + private static final Set knownBlock = MutilBlockModule.getBlocks().stream().map(multiblockCoreBlock -> (Block) multiblockCoreBlock).collect(Collectors.toSet()); + protected MindustryMutilBlockLootSubProvider() { + super(Set.of(), FeatureFlags.REGISTRY.allFlags()); + } + + @Override + protected void generate() { + // TODO + // 现在只有三种方块 + // TEST_MULTIBLOCK_CORE + // TEST_TURRET_MULTIBLOCK_ENTITY_BLOCK + // KILN_BLOCK + // 在挖掘后会掉落,即这三种方块拥有战利品表 + // 扳手和炮台计划用扳手拆 + // (其实是non-blocks需要开一个单独的战利品表提供器,狗命要紧,明天起来再写) + MutilBlockModule.getBlocks().forEach(this::dropSelf); + } + + @Override + protected Iterable getKnownBlocks() { + return knownBlock; + } +} diff --git a/src/main/java/com/hechu/mindustry/datagen/provider/MindustryRecipeProvider.java b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryRecipeProvider.java new file mode 100644 index 0000000..7ad0916 --- /dev/null +++ b/src/main/java/com/hechu/mindustry/datagen/provider/MindustryRecipeProvider.java @@ -0,0 +1,38 @@ +package com.hechu.mindustry.datagen.provider; + +import com.hechu.mindustry.MindustryConstants; +import com.hechu.mindustry.data.recipes.MindustryProcessingRecipeBuilder; +import com.hechu.mindustry.kiwi.ItemModule; +import com.hechu.mindustry.kiwi.RecipeModule; +import net.minecraft.data.PackOutput; +import net.minecraft.data.recipes.FinishedRecipe; +import net.minecraft.data.recipes.RecipeCategory; +import net.minecraft.data.recipes.RecipeProvider; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.Items; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Consumer; + +public class MindustryRecipeProvider extends RecipeProvider { + + public MindustryRecipeProvider(PackOutput output) { + super(output); + } + + @Override + protected void buildRecipes(@NotNull Consumer writer) { + new MindustryProcessingRecipeBuilder(RecipeModule.KILN_RECIPE_SERIALIZER.get(), RecipeCategory.MISC, Items.GLASS, 8) + .requires(Items.SAND, 8) + .processTick(80) + .save(writer, new ResourceLocation(MindustryConstants.MOD_ID, "kiln_sand_to_glass")); + new MindustryProcessingRecipeBuilder(RecipeModule.KILN_RECIPE_SERIALIZER.get(), RecipeCategory.MISC, ItemModule.SURGE_ALLOY.get(), 2) + .requires(ItemModule.COPPER.get(),32) + .processTick(10) + .save(writer, new ResourceLocation(MindustryConstants.MOD_ID, "kiln_copper_to_surge_alloy")); + new MindustryProcessingRecipeBuilder(RecipeModule.KILN_RECIPE_SERIALIZER.get(), RecipeCategory.MISC, Items.DIAMOND, 1) + .requires(ItemModule.COAL.get(), 8) + .processTick(10) + .save(writer, new ResourceLocation(MindustryConstants.MOD_ID, "kiln_coal_to_diamond")); + } +} \ No newline at end of file diff --git a/src/main/java/com/hechu/mindustry/utils/Utils.java b/src/main/java/com/hechu/mindustry/utils/Utils.java index 1a09fe5..73a4009 100644 --- a/src/main/java/com/hechu/mindustry/utils/Utils.java +++ b/src/main/java/com/hechu/mindustry/utils/Utils.java @@ -1,9 +1,12 @@ package com.hechu.mindustry.utils; +import com.hechu.mindustry.MindustryConstants; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.Vec3i; +import net.minecraft.network.chat.Component; import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.player.Player; import org.jetbrains.annotations.NotNull; import java.io.IOException; @@ -11,6 +14,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import static com.hechu.mindustry.MindustryConstants.logger; @@ -51,4 +55,16 @@ public static void checkFolder(@NotNull Path folder) { } } } + + public static void chatSendMessage(@NotNull Player target, MindustryConstants.Chat prefix, String translateId, Object... args) { + target.sendSystemMessage(Component.translatable(prefix.msg + translateId, args)); + } + + public static void chatSendInfo(@NotNull Player target, String translateId, Object... args) { + chatSendMessage(target, MindustryConstants.Chat.CHAT_INFO, translateId, args); + } + + public static void chatSendWarn(@NotNull Player target, String translateId, Object... args) { + chatSendMessage(target, MindustryConstants.Chat.CHAT_WARN, translateId, args); + } } diff --git a/src/main/java/com/hechu/mindustry/world/item/tools/Wrench.java b/src/main/java/com/hechu/mindustry/world/item/tools/Wrench.java index 8605792..32c2de5 100644 --- a/src/main/java/com/hechu/mindustry/world/item/tools/Wrench.java +++ b/src/main/java/com/hechu/mindustry/world/item/tools/Wrench.java @@ -1,7 +1,12 @@ package com.hechu.mindustry.world.item.tools; +import com.google.common.collect.Lists; import com.hechu.mindustry.MindustryConstants; +import com.hechu.mindustry.utils.Utils; import com.hechu.mindustry.world.level.block.Equipment.PowerNodeBlockEntity; +import net.minecraft.ChatFormatting; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.client.resources.language.I18n; import net.minecraft.core.BlockPos; import net.minecraft.nbt.Tag; import net.minecraft.network.chat.Component; @@ -10,12 +15,19 @@ import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.AirBlock; +import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraftforge.common.data.ForgeBlockTagsProvider; +import org.jetbrains.annotations.Nullable; import snownee.kiwi.item.ModItem; import snownee.kiwi.util.NBTHelper; +import java.util.List; + /** * @author luobochuanqi */ @@ -33,31 +45,41 @@ public InteractionResult useOn(UseOnContext pContext) { BlockEntity blockEntity = pContext.getLevel().getBlockEntity(clickedPos); ItemStack itemStack = pContext.getItemInHand(); NBTHelper tag = NBTHelper.of(itemStack).get() == null ? NBTHelper.create() : NBTHelper.of(itemStack); - if (!pContext.getLevel().isClientSide() && blockEntity instanceof PowerNodeBlockEntity powerNodeBlockEntity2) { - // 判断当前 nbt 是否已经存储了一个节点的坐标 - if (tag.get().contains(NBT_KEY)) { - // powerNodeBlockEntity1 连接 powerNodeBlockEntity2 - PowerNodeBlockEntity powerNodeBlockEntity1 = (PowerNodeBlockEntity) pContext.getLevel().getBlockEntity(tag.getPos(NBT_KEY)); - if (powerNodeBlockEntity1 != powerNodeBlockEntity2) { - // 如果点击的节点是已经被连接的,那么就取消连接 - if (powerNodeBlockEntity1.getConnectedNodes().contains(powerNodeBlockEntity2) - && powerNodeBlockEntity2.getPassivelyConnectedNodes().contains(powerNodeBlockEntity1)) { - powerNodeBlockEntity1.removeConnectedNode(powerNodeBlockEntity2); - powerNodeBlockEntity2.removePassivelyConnectedNode(powerNodeBlockEntity1); - } else { - powerNodeBlockEntity2.connectFromOtherNode(powerNodeBlockEntity1); - powerNodeBlockEntity1.connectToOtherNode(powerNodeBlockEntity2); - tag.remove(NBT_KEY); - } - } else { - // 否则就可以判断点击的节点是已经存储的节点本身 - pContext.getPlayer().sendSystemMessage(Component.translatable(MindustryConstants.CHAT_WARN + "linkSelf")); - return InteractionResult.PASS; - } - } else { - tag.setPos(NBT_KEY, clickedPos); - } + if (pContext.getLevel().isClientSide()) { + return InteractionResult.SUCCESS; + } + if (!(blockEntity instanceof PowerNodeBlockEntity powerNodeBlockEntityDst)) { + tag.remove(NBT_KEY); + itemStack.setTag(tag.get()); + Utils.chatSendInfo(pContext.getPlayer(), "clear_select_block"); + return InteractionResult.SUCCESS; + } + // 如果shift被按下 + if (Screen.hasShiftDown()) { + tag.setPos(NBT_KEY, clickedPos); itemStack.setTag(tag.get()); + Utils.chatSendInfo(pContext.getPlayer(), "select_block", clickedPos.getX(), clickedPos.getY(), clickedPos.getZ()); + return InteractionResult.SUCCESS; + } + if (!(tag.get().contains(NBT_KEY))) { + return InteractionResult.SUCCESS; + } + // powerNodeBlockEntitySrc 连接 powerNodeBlockEntityDst + PowerNodeBlockEntity powerNodeBlockEntitySrc = (PowerNodeBlockEntity) pContext.getLevel().getBlockEntity(tag.getPos(NBT_KEY)); + if (powerNodeBlockEntitySrc == powerNodeBlockEntityDst) { + Utils.chatSendWarn(pContext.getPlayer(), "link_self"); + return InteractionResult.PASS; + } + // 如果点击的节点是已经被连接的,那么就取消连接 + if (powerNodeBlockEntitySrc.getConnectedNodes().contains(powerNodeBlockEntityDst) + && powerNodeBlockEntityDst.getPassivelyConnectedNodes().contains(powerNodeBlockEntitySrc)) { + powerNodeBlockEntitySrc.removeConnectedNode(powerNodeBlockEntityDst); + powerNodeBlockEntityDst.removePassivelyConnectedNode(powerNodeBlockEntitySrc); + Utils.chatSendInfo(pContext.getPlayer(), "connected"); + } else { + powerNodeBlockEntityDst.connectFromOtherNode(powerNodeBlockEntitySrc); + powerNodeBlockEntitySrc.connectToOtherNode(powerNodeBlockEntityDst); + Utils.chatSendInfo(pContext.getPlayer(), "disconnected"); } return InteractionResult.SUCCESS; } @@ -72,4 +94,18 @@ public InteractionResultHolder use(Level pLevel, Player pPlayer, Inte } return super.use(pLevel, pPlayer, pUsedHand); } + + @Override + public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List tooltip, TooltipFlag flagIn) { + if (tooltip.isEmpty()) { + return; + } + String key = stack.getDescriptionId() + ".tip"; + BlockPos pos = NBTHelper.of(stack).getPos(NBT_KEY); + if (NBTHelper.of(stack).get() == null || pos == null) { + tooltip.add(Component.translatable(key, 0, 0, 0)); + return; + } + tooltip.add(Component.translatable(key, pos.getX(), pos.getY(), pos.getZ())); + } } diff --git a/src/main/resources/assets/mindustry/lang/en_US.json b/src/main/resources/assets/mindustry/lang/en_US.json index c1583ee..d61f4da 100644 --- a/src/main/resources/assets/mindustry/lang/en_US.json +++ b/src/main/resources/assets/mindustry/lang/en_US.json @@ -35,12 +35,17 @@ "config.jade.plugin_mindustry.crafting_block": "Show Factory Progress", "config.jade.plugin_mindustry.drill": "Show Drill Detail", "config.jade.plugin_mindustry.health_block": "Show Block Health", - "item.mindustry.wrench.tip": "Current: d%", + "item.mindustry.wrench.tip": "Current Pos: %d, %d, %d", "itemGroup.mindustry.mindustry": "Mindustry", "itemGroup.mindustry.materials": "Mindustry - Materials", "mindustry.block_health": "BlockHealth : %d / %d", "mindustry.drill_progress": "Progress %d%%", "mindustry.drill_speed": "Mining Speed : %d items/s", "mindustry.stored_item": "Item Store : %d / %d", - "chat.mindustry.warning.linkSelf": "! You cannot connect to the node itself" + + "chat.mindustry.info.connected": "Connect successfully", + "chat.mindustry.info.select_block": "Select block pos: %d, %d, %d", + "chat.mindustry.info.clear_select_block": "Clear select block pos", + "chat.mindustry.info.disconnected": "Disconnect successfully", + "chat.mindustry.warning.link_self": "The node cannot connect to itself" } \ No newline at end of file diff --git a/src/main/resources/assets/mindustry/lang/zh_CN.json b/src/main/resources/assets/mindustry/lang/zh_CN.json index 8131f4c..196cbfe 100644 --- a/src/main/resources/assets/mindustry/lang/zh_CN.json +++ b/src/main/resources/assets/mindustry/lang/zh_CN.json @@ -35,12 +35,17 @@ "config.jade.plugin_mindustry.crafting_block": "显示工厂制作进度", "config.jade.plugin_mindustry.drill": "显示钻头属性", "config.jade.plugin_mindustry.health_block": "显示血量", - "item.mindustry.wrench.tip": "当前存储: d%", + "item.mindustry.wrench.tip": "存储节点: %d, %d, %d", "itemGroup.mindustry.mindustry": "Mindustry", "itemGroup.mindustry.materials": "Mindustry - 材料", "mindustry.block_health": "生命值 : %d / %d", "mindustry.drill_progress": "%d%%已挖掘", "mindustry.drill_speed": "挖掘速度 : %d/秒", "mindustry.stored_item": "已存储物品 : %d / %d", - "chat.mindustry.warning.linkSelf": "你不能连接要连接的节点本身" + + "chat.mindustry.info.connected": "连接成功", + "chat.mindustry.info.select_block": "选择方块坐标: %d, %d, %d", + "chat.mindustry.info.clear_select_block": "清除已选择的方块坐标", + "chat.mindustry.info.disconnected": "连接已断开", + "chat.mindustry.warning.link_self": "节点无法连接自身" } \ No newline at end of file