Impl. player_only_battles, v1.21.3

A server-side config option was added to enable "player_only_battles" where mobs
cannot enter battle. (I haven't actually tested this fully since I am only 1
person, so there may be bugs.) This can be edited via the config, or via the
`/tbm-server-edit` command.

Version bumped to 1.21.3 .
This commit is contained in:
Stephen Seo 2022-08-24 12:16:36 +09:00
parent 819aea162a
commit e447116e1d
9 changed files with 99 additions and 6 deletions

View file

@ -1,5 +1,11 @@
# Upcoming changes # Upcoming changes
# Version 1.21.3
Implemented "player-only" battles, which can be enabled in the server-side
config or set using `/tbm-server-edit`. (Somewhat untested because I am only 1
person.)
# Version 1.21.2 # Version 1.21.2
Refactored checking-if-in-battle code from `O(n)` to `O(1)` complexity. Refactored checking-if-in-battle code from `O(n)` to `O(1)` complexity.

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.2" version = "1.21.3"
group = "com.burnedkirby.TurnBasedMinecraft" group = "com.burnedkirby.TurnBasedMinecraft"
archivesBaseName = "TurnBasedMinecraft" archivesBaseName = "TurnBasedMinecraft"

View file

@ -1062,6 +1062,34 @@ public class ClientProxy extends CommonProxy {
.withBold(true)); .withBold(true));
parent.getSiblings().add(sub); parent.getSiblings().add(sub);
sub = Component.literal("player_only_battles ");
sub.setStyle(sub.getStyle()
.withColor(ChatFormatting.YELLOW)
.withBold(true)
.withHoverEvent(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
Component.literal("Disables battle for non-player entities")
)));
parent.getSiblings().add(sub);
sub = Component.literal("enable ");
sub.setStyle(sub.getStyle()
.withColor(ChatFormatting.GREEN)
.withClickEvent(new ClickEvent(
ClickEvent.Action.RUN_COMMAND,
"/tbm-server-edit player_only_battles true"
)));
parent.getSiblings().add(sub);
sub = Component.literal("disable ");
sub.setStyle(sub.getStyle()
.withColor(ChatFormatting.GREEN)
.withClickEvent(new ClickEvent(
ClickEvent.Action.RUN_COMMAND,
"/tbm-server-edit player_only_battles false"
)));
parent.getSiblings().add(sub);
TurnBasedMinecraftMod.proxy.displayComponent(parent); TurnBasedMinecraftMod.proxy.displayComponent(parent);
break; break;
} }

View file

@ -66,11 +66,14 @@ public class BattleManager
attackerCustomName = null; attackerCustomName = null;
} }
// verify that both entities are EntityPlayer and not in creative or has a corresponding EntityInfo // Verify that both entities are EntityPlayer and not in creative or has a corresponding EntityInfo.
// Also check if "player_only_battles" is enabled and both entities are players.
if(!((event.getEntity() instanceof Player && !((Player)event.getEntity()).isCreative()) if(!((event.getEntity() instanceof Player && !((Player)event.getEntity()).isCreative())
|| (config.getEntityInfoReference(receiverClassName) != null || config.getCustomEntityInfoReference(receiverCustomName) != null)) || (config.getEntityInfoReference(receiverClassName) != null || config.getCustomEntityInfoReference(receiverCustomName) != null))
|| !((event.getSource().getEntity() instanceof Player && !((Player)event.getSource().getEntity()).isCreative()) || !((event.getSource().getEntity() instanceof Player && !((Player)event.getSource().getEntity()).isCreative())
|| (config.getEntityInfoReference(attackerClassName) != null || config.getCustomEntityInfoReference(attackerCustomName) != null))) || (config.getEntityInfoReference(attackerClassName) != null || config.getCustomEntityInfoReference(attackerCustomName) != null))
|| (TurnBasedMinecraftMod.proxy.getConfig().isPlayerOnlyBattlesEnabled() &&
(!(event.getEntity() instanceof Player) || !(event.getSource().getEntity() instanceof Player))))
{ {
// logger.debug("BattleManager: Failed first check, attacker is \"" + attackerClassName + "\", defender is \"" + receiverClassName + "\""); // logger.debug("BattleManager: Failed first check, attacker is \"" + attackerClassName + "\", defender is \"" + receiverClassName + "\"");
return false; return false;
@ -176,6 +179,12 @@ public class BattleManager
public void checkTargeted(LivingSetAttackTargetEvent event) public void checkTargeted(LivingSetAttackTargetEvent event)
{ {
// Check if "player_only_battles" is enabled and if both entities are players.
if (TurnBasedMinecraftMod.proxy.getConfig().isPlayerOnlyBattlesEnabled() &&
(!(event.getEntity() instanceof Player) || !(event.getTarget() instanceof Player))) {
return;
}
String targetedCustomName; String targetedCustomName;
try { try {
targetedCustomName = event.getTarget().getCustomName().getString(); targetedCustomName = event.getTarget().getCustomName().getString();

View file

@ -53,6 +53,8 @@ public class Config
private Set<String> possibleIgnoreHurtDamageSources; private Set<String> possibleIgnoreHurtDamageSources;
private Set<String> ignoreHurtDamageSources; private Set<String> ignoreHurtDamageSources;
private boolean playerOnlyBattles = false;
public Config(Logger logger) public Config(Logger logger)
{ {
entityInfoMap = new HashMap<String, EntityInfo>(); entityInfoMap = new HashMap<String, EntityInfo>();
@ -548,6 +550,19 @@ public class Config
logTOMLInvalidValue("server_config.ignore_damage_sources"); logTOMLInvalidValue("server_config.ignore_damage_sources");
} }
try {
Boolean is_only_player_battles_enabled = conf.get("server_config.player_only_battles");
if (is_only_player_battles_enabled != null) {
playerOnlyBattles = is_only_player_battles_enabled;
} else {
playerOnlyBattles = false;
logNotFound("server_config.player_only_battles", "false");
}
} catch (ClassCastException e) {
playerOnlyBattles = false;
logTOMLInvalidValue("server_config.player_only_battles", "false");
}
Collection<com.electronwill.nightconfig.core.Config> entities = null; Collection<com.electronwill.nightconfig.core.Config> entities = null;
try { try {
entities = conf.get("server_config.entity"); entities = conf.get("server_config.entity");
@ -1513,4 +1528,12 @@ public class Config
public boolean removeIgnoreHurtDamageSource(String source) { public boolean removeIgnoreHurtDamageSource(String source) {
return ignoreHurtDamageSources.remove(source); return ignoreHurtDamageSources.remove(source);
} }
public boolean isPlayerOnlyBattlesEnabled() {
return playerOnlyBattles;
}
public void setIsPlayerOnlyBattles(boolean enabled) {
playerOnlyBattles = enabled;
}
} }

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.2"; public static final String VERSION = "1.21.3";
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/";
@ -1589,6 +1589,30 @@ public class TurnBasedMinecraftMod {
c.getSource().sendFailure(Component.literal("Failed to remove type \"" + type + "\" from ignore_damage_sources")); c.getSource().sendFailure(Component.literal("Failed to remove type \"" + type + "\" from ignore_damage_sources"));
return 1; return 1;
})))) }))))
.then(Commands.literal("player_only_battles").executes(c -> {
MutableComponent parent = Component.literal("Use ");
MutableComponent sub = Component.literal("/tbm-server-edit player_only_battles <true/false>");
sub.setStyle(sub.getStyle().withColor(ChatFormatting.YELLOW));
parent.getSiblings().add(sub);
c.getSource().sendSuccess(parent, false);
return 1;
})
.then(Commands.argument("player_only_battles", BoolArgumentType.bool()).executes(c -> {
boolean player_only_battles = BoolArgumentType.getBool(c, "player_only_battles");
TurnBasedMinecraftMod.proxy.getConfig().setIsPlayerOnlyBattles(player_only_battles);
if (!TurnBasedMinecraftMod.proxy.getConfig().updateConfig("server_config.player_only_battles", player_only_battles)) {
TurnBasedMinecraftMod.logger.warn("Failed to set \"server_config.player_only_battles\" in config file!");
c.getSource().sendFailure(Component.literal("Failed to set player_only_battles to \"" + player_only_battles + "\" in config file"));
} else {
MutableComponent response = Component.literal("Successfully set player_only_battles to: ");
MutableComponent sub = Component.literal(String.valueOf(player_only_battles));
sub.setStyle(sub.getStyle().withColor(ChatFormatting.GREEN));
response.getSiblings().add(sub);
c.getSource().sendSuccess(response, true);
}
return 1;
})))
); );
} }

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.2" #mandatory version="1.21.3" #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

@ -100,6 +100,9 @@ ignore_damage_sources = [
"sweetBerryBush" "sweetBerryBush"
] ]
# If this is set to true, only players can enter battle.
player_only_battles = false
# Each "server_config.entity" entry uses the following options: # Each "server_config.entity" entry uses the following options:
# name: full class name of the entity, cannot also have option "custom_name" # name: full class name of the entity, cannot also have option "custom_name"

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.2", "version": "1.21.3",
"mcversion": "1.18.2", "mcversion": "1.18.2",
"url": "", "url": "",
"updateUrl": "", "updateUrl": "",