From 1b9afa176359caac3102e203fbdb529739924755 Mon Sep 17 00:00:00 2001 From: ictye Date: Wed, 17 Apr 2024 22:33:38 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=B3=95=E8=A1=93=E9=87=8B=E6=94=BE?= =?UTF-8?q?=E7=9B=B8=E9=97=9C):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复了法杖施法逻辑,优化了物品栏管理,并生成了语言和战利品表(方塊自身掉落)数据代碼内生成。 --- .../89b86ab0e66f527166d98df92ddbcf5416ed58f6 | 2 ++ .../the_origin_of_magic/lang/en_us.json | 15 ++++---- .../the_origin_of_magic/Contents/AllItem.java | 30 ++++++++++++---- .../the_origin_of_magic/ModDataGenerator.java | 11 ------ .../foundation/Items/Staff/StdStaff.java | 36 +++++++++---------- .../Datagen/BlockLootTable.java | 17 +++++++++ .../infrastructure/Datagen/DefaultLang.java | 31 ++++++++++++++++ .../Datagen/ModDataGenerator.java | 21 +++++++++++ .../utils/MagicInventory.java | 23 ++++++++++++ .../lang/en_us.existing.json | 10 ++++++ src/main/resources/fabric.mod.json | 11 +++--- 11 files changed, 158 insertions(+), 49 deletions(-) create mode 100644 src/main/generated/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 rename src/main/{resources => generated}/assets/the_origin_of_magic/lang/en_us.json (79%) delete mode 100644 src/main/java/com/ictye/the_origin_of_magic/ModDataGenerator.java create mode 100644 src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/BlockLootTable.java create mode 100644 src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/DefaultLang.java create mode 100644 src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/ModDataGenerator.java create mode 100644 src/main/resources/assets/the_origin_of_magic/lang/en_us.existing.json diff --git a/src/main/generated/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 b/src/main/generated/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 new file mode 100644 index 0000000..80c8180 --- /dev/null +++ b/src/main/generated/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 @@ -0,0 +1,2 @@ +// 1.19.2 2024-04-17T20:37:27.0952932 Language +84f246a88b9c3dc4c5d0c98a56c4de5529eabe74 assets\the_origin_of_magic\lang\en_us.json diff --git a/src/main/resources/assets/the_origin_of_magic/lang/en_us.json b/src/main/generated/assets/the_origin_of_magic/lang/en_us.json similarity index 79% rename from src/main/resources/assets/the_origin_of_magic/lang/en_us.json rename to src/main/generated/assets/the_origin_of_magic/lang/en_us.json index 62d78ef..7f444d1 100644 --- a/src/main/resources/assets/the_origin_of_magic/lang/en_us.json +++ b/src/main/generated/assets/the_origin_of_magic/lang/en_us.json @@ -1,13 +1,14 @@ { - "item.the_origin_of_magic.test_staff": "Test Staff", - "item.the_origin_of_magic.deadwood_staff": "Deadwood Staff", "block.the_origin_of_magic.magic_workbench": "Magic Workbench", - "text.the_origin_of_magic.magic_work_station.staff_state": "Staff State", + "item.the_origin_of_magic.deadwood_staff": "Deadwood Staff", + "item.the_origin_of_magic.hoglin_entity_limiter": "Hoglin Entity Limiter", "item.the_origin_of_magic.test_magic": "Test Magic", - "text.the_origin_of_magic.not_staff": "This is not a staff , have no magic energy.So you can't edit it with magic work station", + "item.the_origin_of_magic.test_staff": "Test Staff", + "itemGroup.the_origin_of_magic.normal": "The Origin Of The Magic", "text.the_origin_of_magic.empty_staff": "Maybe...... I means that the staff on your hand is empty,no magic in it and can't cast any magic,you need to use the Magic Workbench to edit this staff", - "text.the_origin_of_magic.staff_scattering": "Spell Scatter", + "text.the_origin_of_magic.magic_work_station.staff_state": "Staff State", + "text.the_origin_of_magic.not_staff": "This is not a staff , have no magic energy.So you can't edit it with magic work station", "text.the_origin_of_magic.staff_capacity": "Staff Capacity", - "itemGroup.the_origin_of_magic.normal": "The Origin Of The Magic", - "text.the_origin_of_magic.staff_casting": "Casting Num" + "text.the_origin_of_magic.staff_casting": "Casting Num", + "text.the_origin_of_magic.staff_scattering": "Spell Scatter" } \ No newline at end of file diff --git a/src/main/java/com/ictye/the_origin_of_magic/Contents/AllItem.java b/src/main/java/com/ictye/the_origin_of_magic/Contents/AllItem.java index 940bb70..9246b21 100644 --- a/src/main/java/com/ictye/the_origin_of_magic/Contents/AllItem.java +++ b/src/main/java/com/ictye/the_origin_of_magic/Contents/AllItem.java @@ -14,6 +14,8 @@ import net.minecraft.util.Rarity; import net.minecraft.util.registry.Registry; +import java.util.Map; + /** * 所有物品和物品組都應該寫在這個類裏方便管理和調用 */ @@ -38,7 +40,23 @@ public class AllItem { */ public static final Item TEST_MAGIC = new TestMagicEntityItem(new FabricItemSettings().maxCount(1).group(TheOriginOfMagicItemGroup), AllEntity.TEST_MAGIC_ENTITY_TYPE); // 測試魔法 - public static final Item HOFLIN_ENTITY_LIMITER = new UndeadEntityLimiterItem(new FabricItemSettings().maxCount(1).group(TheOriginOfMagicItemGroup).maxDamage(100).rarity(Rarity.COMMON),null); + public static final Item HOGLIN_ENTITY_LIMITER = new UndeadEntityLimiterItem(new FabricItemSettings().maxCount(1).group(TheOriginOfMagicItemGroup).maxDamage(100).rarity(Rarity.COMMON),null); + + + public static final Map ItemMap = Map.of( + "deadwood_staff",DEADWOOD_STAFF, + "test_staff",TEST_STAFF, + "test_magic",TEST_MAGIC, + "hoglin_entity_limiter", HOGLIN_ENTITY_LIMITER + ); + + public static final Map ItemTrans = Map.of( + "Deadwood Staff",DEADWOOD_STAFF, + "Test Staff",TEST_STAFF, + "Test Magic",TEST_MAGIC, + "Hoglin Entity Limiter", HOGLIN_ENTITY_LIMITER + ); + private static void registerItem(String name, Item item){ Registry.register(Registry.ITEM, new Identifier(the_origin_of_magic.Mod_Id, name), item); } @@ -47,11 +65,9 @@ public static void registerItems(){ for (String name : AllBlock.BlockItems.keySet()) { registerItem(name, AllBlock.BlockItems.get(name)); } - - // 注冊其他物品 - registerItem("test_staff",TEST_STAFF); - registerItem("test_magic",TEST_MAGIC); - registerItem("deadwood_staff",DEADWOOD_STAFF); - registerItem("hoglin_entity_limiter", HOFLIN_ENTITY_LIMITER); + // 注冊物品 + for (String name : ItemMap.keySet()) { + registerItem(name, ItemMap.get(name)); + } } } diff --git a/src/main/java/com/ictye/the_origin_of_magic/ModDataGenerator.java b/src/main/java/com/ictye/the_origin_of_magic/ModDataGenerator.java deleted file mode 100644 index db446f8..0000000 --- a/src/main/java/com/ictye/the_origin_of_magic/ModDataGenerator.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.ictye.the_origin_of_magic; - -import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint; -import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator; - -public class ModDataGenerator implements DataGeneratorEntrypoint { - @Override - public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) { - - } -} diff --git a/src/main/java/com/ictye/the_origin_of_magic/foundation/Items/Staff/StdStaff.java b/src/main/java/com/ictye/the_origin_of_magic/foundation/Items/Staff/StdStaff.java index 76bcc0e..eb9e52c 100644 --- a/src/main/java/com/ictye/the_origin_of_magic/foundation/Items/Staff/StdStaff.java +++ b/src/main/java/com/ictye/the_origin_of_magic/foundation/Items/Staff/StdStaff.java @@ -287,8 +287,10 @@ public TypedActionResult use(World world, PlayerEntity user, Hand han List magicList = summonMagic(Magics,user,world,count); // 生成法術實體 for(StdThrownMagic MagicEntity:magicList){ + float finalSpeed = getSpeed(); // 計算最終速度 float finalScattering = getScattering(); // 計算最終散射 + MagicEntity.setVelocity(user, user.getPitch(), user.getYaw(), 0.0F, finalSpeed, finalScattering); // 設置參數 if(world.spawnEntity(MagicEntity)){ // 生成法術實體并且破壞物品 @@ -308,20 +310,13 @@ public TypedActionResult use(World world, PlayerEntity user, Hand han * @return 法術列表 */ private List summonMagic(MagicInventory inventory, PlayerEntity user , World world, int count){ - /* - FIXME: - 在解析附加魔法的時候存在邏輯錯誤,可能導致死循環 - */ List magicItemList = new ArrayList<>(); - int startSlot = castCount; // 起始格子 + List limiterList = new ArrayList<>(); - do { - ItemStack magic = inventory.getStack(castCount); // 獲取魔法物品堆棧 - Item magicItem = magic.getItem(); // 魔法物品 - //檢查是否為空 - if (magicItem == Items.AIR || magic == ItemStack.EMPTY){ - updateCastCount(); + for(int i = inventory.size() + 1; i > 0 && count > 0 ;i --){ + Item magicItem = inventory.next().getItem(); // 魔法物品 + if (magicItem == Items.AIR){ continue; } if(((StdMagicItem)magicItem).getMagic(user,world,exolisionRate,hartRate) instanceof StdThrownMagic MagicEntity){ @@ -330,16 +325,19 @@ private List summonMagic(MagicInventory inventory, PlayerEntity if(addition > 0){ MagicEntity.setAdditionTrigger(summonMagic(inventory,user,world,addition)); } - count --; - updateCastCount(); magicItemList.add(MagicEntity); - }else if(((StdMagicItem)magicItem).getMagic(user,world,exolisionRate,hartRate) instanceof StdMagicLimiter limiter){ - StdThrownMagic lastmagic = magicItemList.get(magicItemList.size()-1); - lastmagic.addLimiter(limiter); - count --; - updateCastCount(); + count--; + } if(((StdMagicItem)magicItem).getMagic(user,world,exolisionRate,hartRate) instanceof StdMagicLimiter limiter){ + limiterList.add(limiter); + count--; } - }while (count > 0 && startSlot != castCount ); + } + + for(StdMagicLimiter limiter :limiterList){ + for(StdThrownMagic magic :magicItemList){ + magic.addLimiter(limiter); + } + } return magicItemList; } diff --git a/src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/BlockLootTable.java b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/BlockLootTable.java new file mode 100644 index 0000000..2def680 --- /dev/null +++ b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/BlockLootTable.java @@ -0,0 +1,17 @@ +package com.ictye.the_origin_of_magic.infrastructure.Datagen; + +import com.ictye.the_origin_of_magic.Contents.AllBlock; +import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator; +import net.fabricmc.fabric.api.datagen.v1.provider.FabricBlockLootTableProvider; + +public class BlockLootTable extends FabricBlockLootTableProvider { + + public BlockLootTable(FabricDataGenerator dataGenerator) { + super(dataGenerator); + } + + @Override + protected void generateBlockLootTables() { + addDrop(AllBlock.MAGIC_WORKSTATION); + } +} diff --git a/src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/DefaultLang.java b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/DefaultLang.java new file mode 100644 index 0000000..019ae6d --- /dev/null +++ b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/DefaultLang.java @@ -0,0 +1,31 @@ +package com.ictye.the_origin_of_magic.infrastructure.Datagen; + +import com.ictye.the_origin_of_magic.Contents.AllItem; +import com.ictye.the_origin_of_magic.the_origin_of_magic; +import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator; +import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider; +import java.nio.file.Path; + +public class DefaultLang extends FabricLanguageProvider { + protected DefaultLang(FabricDataGenerator dataGenerator) { + super(dataGenerator, "en_us"); + } + + @Override + public void generateTranslations(TranslationBuilder translationBuilder) { + // 生成代碼内翻譯 + for(String name : AllItem.ItemTrans.keySet()){ + translationBuilder.add(AllItem.ItemTrans.get(name), name); + } + + the_origin_of_magic.LOGGER.info("Adding existing language file!"); + + // 添加預製的語言文件 + try { + Path existingFilePath = dataGenerator.getModContainer().findPath("assets/the_origin_of_magic/lang/en_us.existing.json").get(); + translationBuilder.add(existingFilePath); + } catch (Exception e) { + throw new RuntimeException("Failed to add existing language file!", e); + } + } +} diff --git a/src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/ModDataGenerator.java b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/ModDataGenerator.java new file mode 100644 index 0000000..3af357e --- /dev/null +++ b/src/main/java/com/ictye/the_origin_of_magic/infrastructure/Datagen/ModDataGenerator.java @@ -0,0 +1,21 @@ +package com.ictye.the_origin_of_magic.infrastructure.Datagen; + +import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint; +import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator; + +import java.io.IOException; + +public class ModDataGenerator implements DataGeneratorEntrypoint { + @Override + public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) { + + fabricDataGenerator.addProvider(DefaultLang::new); + fabricDataGenerator.addProvider(BlockLootTable::new); + + try { + fabricDataGenerator.run(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/com/ictye/the_origin_of_magic/utils/MagicInventory.java b/src/main/java/com/ictye/the_origin_of_magic/utils/MagicInventory.java index f574a4a..7de6be5 100644 --- a/src/main/java/com/ictye/the_origin_of_magic/utils/MagicInventory.java +++ b/src/main/java/com/ictye/the_origin_of_magic/utils/MagicInventory.java @@ -6,9 +6,12 @@ /** * 存放魔法的物品欄,用於法術施放什麽的 + * 支持隊列操作,可以循環 */ public class MagicInventory extends SimpleInventory { + int count = 0; + public MagicInventory(int size) { super(size); } @@ -25,4 +28,24 @@ public boolean canInsert(ItemStack stack) { return super.canInsert(stack) && stack.getItem() instanceof StdMagicItem; } + private void updateCastCount(){ + if (size() != 1){ + if (count + 1 > size()){ + count = -1; + updateCastCount(); + }else { + count = count + 1; + } + } + } + + public ItemStack next(){ + int c = count; + updateCastCount(); + return getStack(c); + } + + public void resetCount(){ + count = 0; + } } diff --git a/src/main/resources/assets/the_origin_of_magic/lang/en_us.existing.json b/src/main/resources/assets/the_origin_of_magic/lang/en_us.existing.json new file mode 100644 index 0000000..f5b86fe --- /dev/null +++ b/src/main/resources/assets/the_origin_of_magic/lang/en_us.existing.json @@ -0,0 +1,10 @@ +{ + "block.the_origin_of_magic.magic_workbench": "Magic Workbench", + "text.the_origin_of_magic.magic_work_station.staff_state": "Staff State", + "text.the_origin_of_magic.not_staff": "This is not a staff , have no magic energy.So you can't edit it with magic work station", + "text.the_origin_of_magic.empty_staff": "Maybe...... I means that the staff on your hand is empty,no magic in it and can't cast any magic,you need to use the Magic Workbench to edit this staff", + "text.the_origin_of_magic.staff_scattering": "Spell Scatter", + "text.the_origin_of_magic.staff_capacity": "Staff Capacity", + "itemGroup.the_origin_of_magic.normal": "The Origin Of The Magic", + "text.the_origin_of_magic.staff_casting": "Casting Num" +} \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 74f5924..aba6cdf 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -22,8 +22,12 @@ ], "main": [ "com.ictye.the_origin_of_magic.the_origin_of_magic" - ],"fabric-gametest": [ + ], + "fabric-gametest": [ "com.ictye.the_origin_of_magic.GameTest.GameTestMain" + ], + "fabric-datagen": [ + "com.ictye.the_origin_of_magic.infrastructure.Datagen.ModDataGenerator" ] }, "mixins": [ @@ -33,8 +37,5 @@ "fabricloader": ">=${loader_version}", "fabric": "*", "minecraft": "${minecraft_version}" - }, - "fabric-datagen": [ - "com.ictye.the_origin_of_magic.ModDataGenerator" -] + } }