Refactor entity-in-battle checking, v1.21.2

This commit is contained in:
Stephen Seo 2022-08-23 15:01:14 +09:00
parent 12e6583b1f
commit 819aea162a
7 changed files with 14 additions and 7 deletions

View file

@ -1,5 +1,12 @@
# Upcoming changes # Upcoming changes
# Version 1.21.2
Refactored checking-if-in-battle code from `O(n)` to `O(1)` complexity.
(In other words, utilizes the HashMap's constant time lookup of a key instead of
checking every key's id if the entity's id is the same. This speeds up the
lookup from linear to constant time.)
# Version 1.21.1 # Version 1.21.1
Refactored checking-if-in-battle code to be more efficient. Refactored checking-if-in-battle code to be more efficient.

View file

@ -14,7 +14,7 @@ apply plugin: 'net.minecraftforge.gradle'
//apply plugin: 'eclipse' //apply plugin: 'eclipse'
//apply plugin: 'maven-publish' //apply plugin: 'maven-publish'
version = "1.21.1" version = "1.21.2"
group = "com.burnedkirby.TurnBasedMinecraft" group = "com.burnedkirby.TurnBasedMinecraft"
archivesBaseName = "TurnBasedMinecraft" archivesBaseName = "TurnBasedMinecraft"

View file

@ -351,7 +351,7 @@ public class BattleManager
return result; return result;
} }
public boolean isInBattle(int entityID) { public boolean isInBattle(Entity entity) {
return entityToBattleMap.keySet().parallelStream().anyMatch(pair -> pair.id == entityID); return entityToBattleMap.keySet().contains(new EntityIDDimPair(entity));
} }
} }

View file

@ -9,7 +9,7 @@ public class HurtEventHandler {
CommonProxy proxy = TurnBasedMinecraftMod.proxy; CommonProxy proxy = TurnBasedMinecraftMod.proxy;
if (event.getEntity().level.isClientSide || proxy.getBattleManager() == null) { if (event.getEntity().level.isClientSide || proxy.getBattleManager() == null) {
return; return;
} else if (proxy.getConfig().getIgnoreHurtDamageSources().contains(event.getSource().msgId) && proxy.getBattleManager().isInBattle(event.getEntity().getId())) { } else if (proxy.getConfig().getIgnoreHurtDamageSources().contains(event.getSource().msgId) && proxy.getBattleManager().isInBattle(event.getEntity())) {
event.setCanceled(true); event.setCanceled(true);
} }
} }

View file

@ -39,7 +39,7 @@ import org.apache.logging.log4j.Logger;
public class TurnBasedMinecraftMod { public class TurnBasedMinecraftMod {
public static final String MODID = "com_burnedkirby_turnbasedminecraft"; public static final String MODID = "com_burnedkirby_turnbasedminecraft";
public static final String NAME = "Turn Based Minecraft Mod"; public static final String NAME = "Turn Based Minecraft Mod";
public static final String VERSION = "1.21.1"; public static final String VERSION = "1.21.2";
public static final String CONFIG_FILENAME = "TBM_Config.toml"; public static final String CONFIG_FILENAME = "TBM_Config.toml";
public static final String DEFAULT_CONFIG_FILENAME = "TBM_Config_DEFAULT.toml"; public static final String DEFAULT_CONFIG_FILENAME = "TBM_Config_DEFAULT.toml";
public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/"; public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/";

View file

@ -15,7 +15,7 @@ license="MIT"
# The modid of the mod # The modid of the mod
modId="com_burnedkirby_turnbasedminecraft" #mandatory modId="com_burnedkirby_turnbasedminecraft" #mandatory
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it # The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
version="1.21.1" #mandatory version="1.21.2" #mandatory
# A display name for the mod # A display name for the mod
displayName="TurnBasedMinecraftMod" #mandatory displayName="TurnBasedMinecraftMod" #mandatory
# A URL to query for updates for this mod. See the JSON update specification <here> # A URL to query for updates for this mod. See the JSON update specification <here>

View file

@ -3,7 +3,7 @@
"modid": "com_burnedkirby_turnbasedminecraft", "modid": "com_burnedkirby_turnbasedminecraft",
"name": "Turn Based Minecraft", "name": "Turn Based Minecraft",
"description": "Changes battles to be turn-based.", "description": "Changes battles to be turn-based.",
"version": "1.21.1", "version": "1.21.2",
"mcversion": "1.18.2", "mcversion": "1.18.2",
"url": "", "url": "",
"updateUrl": "", "updateUrl": "",