From ee633284dfa633ebcf89d15d399ded9d4e31d17c Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 23 May 2021 13:22:18 +0900 Subject: [PATCH] Add experimental support for Pam's HC foods --- Changelog.md | 4 + build.gradle | 2 +- .../TurnBasedMinecraft/common/Battle.java | 43 +++++---- .../common/BattleManager.java | 10 +++ .../common/CommonProxy.java | 89 +++++++++++++++++++ .../common/TurnBasedMinecraftMod.java | 2 +- src/main/resources/META-INF/mods.toml | 2 +- src/main/resources/mcmod.info | 2 +- 8 files changed, 135 insertions(+), 19 deletions(-) diff --git a/Changelog.md b/Changelog.md index c830829..3d17041 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,9 @@ # Upcoming changes +# Version 1.17.1 + +Add experimental support for Pam's Harvestcraft foods. + # Version 1.17 Update mod for Forge 1.16.5-36.1.0 . diff --git a/build.gradle b/build.gradle index cf6b197..00c8933 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ apply plugin: 'eclipse' //apply plugin: 'maven-publish' apply plugin: 'com.github.johnrengelman.shadow' -version = "1.17" +version = "1.17.1" group = "com.burnedkirby.TurnBasedMinecraft" archivesBaseName = "TurnBasedMinecraft" diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java index 5d06b22..8d187c0 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java @@ -1316,26 +1316,39 @@ public class Battle sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getId(), 0, PacketBattleMessage.UsedItemAction.USED_NOTHING.getValue()); break; } - if(targetItem.getItemCategory() == ItemGroup.TAB_FOOD) - { + // check if use item is a food + boolean isFood = false; + // first check other mod foods + for(ItemGroup itemGroup : BattleManager.getOtherFoodItemGroups()) { + if(targetItem.getItemCategory() == itemGroup && targetItem.isEdible()) { + isFood = true; + break; + } + } + if(isFood) { debugLog += " food"; sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getId(), 0, PacketBattleMessage.UsedItemAction.USED_FOOD.getValue(), targetItemStack.getDisplayName().getString()); final Entity nextEntity = next.entity; final int nextItemToUse = next.itemToUse; ((PlayerEntity)nextEntity).inventory.setItem(nextItemToUse, targetItem.finishUsingItem(targetItemStack, nextEntity.level, (LivingEntity)nextEntity)); - } - else if(targetItem instanceof PotionItem) - { - debugLog += " potion"; - sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getId(), 0, PacketBattleMessage.UsedItemAction.USED_POTION.getValue(), targetItemStack.getDisplayName().getString()); - final Entity nextEntity = next.entity; - final int nextItemToUse = next.itemToUse; - ((PlayerEntity)nextEntity).inventory.setItem(nextItemToUse, targetItem.finishUsingItem(targetItemStack, nextEntity.level, (LivingEntity)nextEntity)); - } - else - { - debugLog += " non-consumable"; - sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getId(), 0, PacketBattleMessage.UsedItemAction.USED_INVALID.getValue(), targetItemStack.getDisplayName().getString()); + } else { + // then check vanilla foods + if (targetItem.getItemCategory() == ItemGroup.TAB_FOOD && targetItem.isEdible()) { + debugLog += " food"; + sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getId(), 0, PacketBattleMessage.UsedItemAction.USED_FOOD.getValue(), targetItemStack.getDisplayName().getString()); + final Entity nextEntity = next.entity; + final int nextItemToUse = next.itemToUse; + ((PlayerEntity) nextEntity).inventory.setItem(nextItemToUse, targetItem.finishUsingItem(targetItemStack, nextEntity.level, (LivingEntity) nextEntity)); + } else if (targetItem instanceof PotionItem) { + debugLog += " potion"; + sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getId(), 0, PacketBattleMessage.UsedItemAction.USED_POTION.getValue(), targetItemStack.getDisplayName().getString()); + final Entity nextEntity = next.entity; + final int nextItemToUse = next.itemToUse; + ((PlayerEntity) nextEntity).inventory.setItem(nextItemToUse, targetItem.finishUsingItem(targetItemStack, nextEntity.level, (LivingEntity) nextEntity)); + } else { + debugLog += " non-consumable"; + sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getId(), 0, PacketBattleMessage.UsedItemAction.USED_INVALID.getValue(), targetItemStack.getDisplayName().getString()); + } } break; case SWITCH_ITEM: { diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/BattleManager.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/BattleManager.java index 91a07e5..d76a430 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/BattleManager.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/BattleManager.java @@ -9,6 +9,7 @@ import java.util.Map; import net.minecraft.entity.monster.CreeperEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.item.ItemGroup; import net.minecraft.util.RegistryKey; import net.minecraft.world.World; import net.minecraftforge.common.MinecraftForge; @@ -29,6 +30,7 @@ public class BattleManager private Map recentlyLeftBattle; private BattleUpdater battleUpdater; private Map entityToBattleMap; + private static Collection otherFoodItemGroups = new ArrayList<>(); public BattleManager(Logger logger) { @@ -356,4 +358,12 @@ public class BattleManager } return result; } + + public static void addOtherModItemGroup(ItemGroup itemGroup) { + otherFoodItemGroups.add(itemGroup); + } + + public static Collection getOtherFoodItemGroups() { + return otherFoodItemGroups; + } } \ No newline at end of file diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/CommonProxy.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/CommonProxy.java index 8abfb62..3074f69 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/CommonProxy.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/CommonProxy.java @@ -1,12 +1,16 @@ package com.burnedkirby.TurnBasedMinecraft.common; +import java.lang.reflect.Field; import java.util.*; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemGroup; import net.minecraft.util.RegistryKey; import net.minecraft.util.text.ITextComponent; import net.minecraft.world.DimensionType; import net.minecraft.world.World; +import net.minecraftforge.fml.ModContainer; +import net.minecraftforge.fml.ModList; import net.minecraftforge.fml.server.ServerLifecycleHooks; import org.apache.logging.log4j.Logger; @@ -71,10 +75,95 @@ public class CommonProxy { config = new Config(logger); postInitClient(); + pamsFoodIntegrationLoading(); logger.debug("postInit proxy for com_burnedkirby_turnbasedminecraft"); } protected void postInitClient() {} + + private final void pamsFoodIntegrationLoading() { + // TODO: generalize other mod's food loading via config with a list of mod ids + + // pamhc2foodcore + { + ModList modList = ModList.get(); + Optional pamsFoodCoreContainer = modList.getModContainerById("pamhc2foodcore"); + if (pamsFoodCoreContainer.isPresent()) { + Object pamsFoodCore = pamsFoodCoreContainer.get().getMod(); + try { + Field itemGroupField = pamsFoodCore.getClass().getField("ITEM_GROUP"); + ItemGroup foodItemGroup = (ItemGroup) itemGroupField.get(null); + if(foodItemGroup != null) { + BattleManager.addOtherModItemGroup(foodItemGroup); + } else { + throw new NullPointerException(); + } + } catch (Exception e) { + TurnBasedMinecraftMod.logger.info("Failed to get pamhc2foodcore ITEM_GROUP"); + } + } + } + + // pamhc2crops + { + ModList modList = ModList.get(); + Optional pamsFoodCoreContainer = modList.getModContainerById("pamhc2crops"); + if (pamsFoodCoreContainer.isPresent()) { + Object pamsFoodCore = pamsFoodCoreContainer.get().getMod(); + try { + Field itemGroupField = pamsFoodCore.getClass().getField("ITEM_GROUP"); + ItemGroup foodItemGroup = (ItemGroup) itemGroupField.get(null); + if(foodItemGroup != null) { + BattleManager.addOtherModItemGroup(foodItemGroup); + } else { + throw new NullPointerException(); + } + } catch (Exception e) { + TurnBasedMinecraftMod.logger.info("Failed to get pamhc2crops ITEM_GROUP"); + } + } + } + + // pamhc2trees + { + ModList modList = ModList.get(); + Optional pamsFoodCoreContainer = modList.getModContainerById("pamhc2trees"); + if (pamsFoodCoreContainer.isPresent()) { + Object pamsFoodCore = pamsFoodCoreContainer.get().getMod(); + try { + Field itemGroupField = pamsFoodCore.getClass().getField("ITEM_GROUP"); + ItemGroup foodItemGroup = (ItemGroup) itemGroupField.get(null); + if(foodItemGroup != null) { + BattleManager.addOtherModItemGroup(foodItemGroup); + } else { + throw new NullPointerException(); + } + } catch (Exception e) { + TurnBasedMinecraftMod.logger.info("Failed to get pamhc2trees ITEM_GROUP"); + } + } + } + + // pamhc2foodextended + { + ModList modList = ModList.get(); + Optional pamsFoodCoreContainer = modList.getModContainerById("pamhc2foodextended"); + if (pamsFoodCoreContainer.isPresent()) { + Object pamsFoodCore = pamsFoodCoreContainer.get().getMod(); + try { + Field itemGroupField = pamsFoodCore.getClass().getField("ITEM_GROUP"); + ItemGroup foodItemGroup = (ItemGroup) itemGroupField.get(null); + if(foodItemGroup != null) { + BattleManager.addOtherModItemGroup(foodItemGroup); + } else { + throw new NullPointerException(); + } + } catch (Exception e) { + TurnBasedMinecraftMod.logger.info("Failed to get pamhc2foodextended ITEM_GROUP"); + } + } + } + } public final void setLogger(Logger logger) { diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java index 2c39e92..ecf63d1 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java @@ -35,7 +35,7 @@ public class TurnBasedMinecraftMod { public static final String MODID = "com_burnedkirby_turnbasedminecraft"; public static final String NAME = "Turn Based Minecraft Mod"; - public static final String VERSION = "1.17"; + public static final String VERSION = "1.17.1"; public static final String CONFIG_FILENAME = "TBM_Config.toml"; public static final String DEFAULT_CONFIG_FILENAME = "TBM_Config_DEFAULT.toml"; public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/"; diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index e7c337e..16d644e 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -15,7 +15,7 @@ license="MIT" # The modid of the mod modId="com_burnedkirby_turnbasedminecraft" #mandatory # The version number of the mod - there's a few well known ${} variables useable here or just hardcode it -version="1.17" #mandatory +version="1.17.1" #mandatory # A display name for the mod displayName="TurnBasedMinecraftMod" #mandatory # A URL to query for updates for this mod. See the JSON update specification diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info index e3aab20..297c8dd 100644 --- a/src/main/resources/mcmod.info +++ b/src/main/resources/mcmod.info @@ -3,7 +3,7 @@ "modid": "com_burnedkirby_turnbasedminecraft", "name": "Turn Based Minecraft", "description": "Changes battles to be turn-based.", - "version": "1.17", + "version": "1.17.1", "mcversion": "1.16.3", "url": "", "updateUrl": "",