Skip to content

Commit

Permalink
Update to 1.19.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Kozlov committed Oct 1, 2022
1 parent 36222a9 commit 3622267
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 176 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Apathy for 1.81.1, requires Fabric API
# Apathy for 1.19.2, requires Fabric API

Make mobs apathetic up until you attack
Literally, thats it.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

plugins {
id 'fabric-loom' version '0.10-SNAPSHOT'
id 'fabric-loom' version '1.0-SNAPSHOT'
id 'maven-publish'
id 'com.github.johnrengelman.shadow' version '7.1.1'
}
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/versions.html
minecraft_version=1.18.1
yarn_mappings=1.18.1+build.1
loader_version=0.12.12
minecraft_version=1.19.2
yarn_mappings=1.19.2+build.8
loader_version=0.14.9

# Mod Properties
mod_version = 1.3.0
mod_version = 1.4.0
maven_group = eu.nk2
archives_base_name = apathy

# Dependencies
fabric_version=0.44.0+1.18
fabric_version=0.60.0+1.19.2
jankson_version=1.2.1
331 changes: 166 additions & 165 deletions src/main/java/eu/nk2/apathy/ApathyMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Pair;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryEntry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -27,169 +28,169 @@

public class ApathyMod implements ModInitializer {

private final Logger logger = LogManager.getLogger("Apathy");

private EntityType.EntityFactory<Entity> getEntityFactory(EntityType<Entity> entityType) {
try {
return ((ApathyMixinEntityTypeAccessor) entityType).getFactory();
} catch (Exception e) {
logger.error("For " + entityType.toString() + ": ", e);
return null;
}
}

private EntityType<Entity> setEntityFactory(EntityType<Entity> entityType, EntityType.EntityFactory<Entity> entityFactory) {
try {
((ApathyMixinEntityTypeAccessor) entityType).setCustomFactory(entityFactory);
return entityType;
} catch (Exception e) {
logger.error("For " + entityType.toString() + ": ", e);
return null;
}
}

private enum GoalSelectorFieldName {
GOAL_SELECTOR,
TARGET_SELECTOR
}
private Set<Goal> getGoalSetFromMobEntitySelector(MobEntity mobEntity, GoalSelectorFieldName goalSelectorFieldName) {
try {
GoalSelector goalSelector = null;
switch (goalSelectorFieldName) {
case GOAL_SELECTOR:
goalSelector = ((ApathyMixinMobEntityAccessor) mobEntity).getGoalSelector();
break;
case TARGET_SELECTOR:
goalSelector = ((ApathyMixinMobEntityAccessor) mobEntity).getTargetSelector();
break;
}

return ((ApathyMixinGoalSelectorAccessor) goalSelector).getGoals();
} catch (Exception e) {
logger.error("For " + mobEntity.toString() + ": ", e);
return null;
}
}

private boolean isPlayerActiveTargetGoal(ActiveTargetGoal<?> followTargetGoal) {
try {
Class<?> targetClass = ((ApathyMixinActiveTargetGoalAccessor) followTargetGoal).getTargetClass();
return targetClass.equals(PlayerEntity.class);
} catch (Exception e) {
logger.error("For " + followTargetGoal.toString() + ": ", e);
return false;
}
}

private void updateMobSetApathyGoal(MobEntity entity, Set<Goal> targetSelectorSet, int priority, ActiveTargetGoal<PlayerEntity> followTargetGoal, ApathyConfig.ApathyBehaviourType apathyBehaviourType) {
ApathyMixinActiveTargetGoalAccessor followTargetGoalAccessor = (ApathyMixinActiveTargetGoalAccessor) followTargetGoal;
ApathyMixinTrackTargetGoalAccessor trackTargetGoalAccessor = (ApathyMixinTrackTargetGoalAccessor) followTargetGoal;

logger.debug("[" + entity + "] Applied " + apathyBehaviourType);
if(apathyBehaviourType instanceof ApathyConfig.ApathyBehaviourDoNotFollowType) {
targetSelectorSet.add(new PrioritizedGoal(
priority,
new ApathyDoNotActiveTargetGoal<>(
entity,
PlayerEntity.class,
followTargetGoalAccessor.getReciprocalChance(),
trackTargetGoalAccessor.getCheckVisibility(),
trackTargetGoalAccessor.getCheckVisibility(),
followTargetGoalAccessor.getTargetPredicate()
)
));
} else if(apathyBehaviourType instanceof ApathyConfig.ApathyBehaviourIfBlockBrokenType) {
ApathyConfig.ApathyBehaviourIfBlockBrokenType ifBlockBrokenBehaviour = (ApathyConfig.ApathyBehaviourIfBlockBrokenType) apathyBehaviourType;
targetSelectorSet.add(new PrioritizedGoal(
priority,
new ApathyIfBlockBrokenActiveTargetGoal(
entity,
followTargetGoalAccessor.getReciprocalChance(),
trackTargetGoalAccessor.getCheckVisibility(),
trackTargetGoalAccessor.getCheckVisibility(),
followTargetGoalAccessor.getTargetPredicate(),
ifBlockBrokenBehaviour.getMaximalReactionDistance(),
ifBlockBrokenBehaviour.getReactionBlock()
)
));
} else if(apathyBehaviourType instanceof ApathyConfig.ApathyBehaviourIfItemSelectedType) {
ApathyConfig.ApathyBehaviourIfItemSelectedType ifItemSelectedBehaviour = (ApathyConfig.ApathyBehaviourIfItemSelectedType) apathyBehaviourType;
targetSelectorSet.add(new PrioritizedGoal(
priority,
new ApathyIfItemSelectedActiveTargetGoal(
entity,
followTargetGoalAccessor.getReciprocalChance(),
trackTargetGoalAccessor.getCheckVisibility(),
trackTargetGoalAccessor.getCheckVisibility(),
followTargetGoalAccessor.getTargetPredicate(),
ifItemSelectedBehaviour.getMaximalReactionDistance(),
ifItemSelectedBehaviour.getReactionItem(),
ifItemSelectedBehaviour.getReactionItemCount()
)
));
}
}

private void updateMobSetApathyGoals(ApathyConfig apathyConfig, MobEntity entity, Set<Goal> targetSelectorSet, int priority, ActiveTargetGoal<PlayerEntity> followTargetGoal) {
List<ApathyConfig.ApathyBehaviourType> defaultApathyBehaviourType = apathyConfig.getApathyBehaviourTypeMap().getOrDefault(null, new ArrayList<>());
List<ApathyConfig.ApathyBehaviourType> mobSpecificApathyBehaviourType = apathyConfig.getApathyBehaviourTypeMap().get(Registry.ENTITY_TYPE.getId(entity.getType()));

if(mobSpecificApathyBehaviourType == null) {
defaultApathyBehaviourType.forEach(apathyBehaviourType -> updateMobSetApathyGoal(
entity,
targetSelectorSet,
priority,
followTargetGoal,
apathyBehaviourType
));
} else {
mobSpecificApathyBehaviourType.forEach(apathyBehaviourType -> updateMobSetApathyGoal(
entity,
targetSelectorSet,
priority,
followTargetGoal,
apathyBehaviourType
));
}
}

@Override
public void onInitialize() {
ApathyConfig apathyConfig;
try {
apathyConfig = ApathyConfigLoader.loadConfig();
} catch (IOException e) {
apathyConfig = new ApathyConfig(new HashMap<>());
}

logger.info("Loaded with config: " + apathyConfig);

ApathyConfig finalApathyConfig = apathyConfig;
Registry.ENTITY_TYPE.getEntries()
.stream()
.map(Map.Entry::getValue)
.map((entityType) -> new Pair<>((EntityType<Entity>) entityType, getEntityFactory((EntityType<Entity>) entityType)))
.map((entityTypeToFactory) -> setEntityFactory(entityTypeToFactory.getLeft(), (type, world) -> {
Entity entity = entityTypeToFactory.getRight().create(type, world);

if (entity instanceof MobEntity) {
Set<Goal> targetSelectorSet = getGoalSetFromMobEntitySelector((MobEntity) entity, GoalSelectorFieldName.TARGET_SELECTOR);
if(targetSelectorSet != null)
targetSelectorSet.stream()
.map((goal) -> (PrioritizedGoal) goal)
.filter((goal) -> goal.getGoal() instanceof ActiveTargetGoal)
.filter((goal) -> (isPlayerActiveTargetGoal((ActiveTargetGoal) goal.getGoal())))
.collect(Collectors.toList())
.forEach((PrioritizedGoal goal) -> {
ActiveTargetGoal<PlayerEntity> followTargetGoal = (ActiveTargetGoal<PlayerEntity>) goal.getGoal();

targetSelectorSet.remove(goal);
updateMobSetApathyGoals(finalApathyConfig, (MobEntity) entity, targetSelectorSet, goal.getPriority(), followTargetGoal);
});
}

return entity;
}))
.forEach((entityType) -> {});
}
private final Logger logger = LogManager.getLogger("Apathy");

private EntityType.EntityFactory<Entity> getEntityFactory(EntityType<Entity> entityType) {
try {
return ((ApathyMixinEntityTypeAccessor) entityType).getFactory();
} catch (Exception e) {
logger.error("For " + entityType.toString() + ": ", e);
return null;
}
}

private EntityType<Entity> setEntityFactory(EntityType<Entity> entityType, EntityType.EntityFactory<Entity> entityFactory) {
try {
((ApathyMixinEntityTypeAccessor) entityType).setCustomFactory(entityFactory);
return entityType;
} catch (Exception e) {
logger.error("For " + entityType.toString() + ": ", e);
return null;
}
}

private enum GoalSelectorFieldName {
GOAL_SELECTOR,
TARGET_SELECTOR
}
private Set<Goal> getGoalSetFromMobEntitySelector(MobEntity mobEntity, GoalSelectorFieldName goalSelectorFieldName) {
try {
GoalSelector goalSelector = null;
switch (goalSelectorFieldName) {
case GOAL_SELECTOR:
goalSelector = ((ApathyMixinMobEntityAccessor) mobEntity).getGoalSelector();
break;
case TARGET_SELECTOR:
goalSelector = ((ApathyMixinMobEntityAccessor) mobEntity).getTargetSelector();
break;
}

return ((ApathyMixinGoalSelectorAccessor) goalSelector).getGoals();
} catch (Exception e) {
logger.error("For " + mobEntity.toString() + ": ", e);
return null;
}
}

private boolean isPlayerActiveTargetGoal(ActiveTargetGoal<?> followTargetGoal) {
try {
Class<?> targetClass = ((ApathyMixinActiveTargetGoalAccessor) followTargetGoal).getTargetClass();
return targetClass.equals(PlayerEntity.class);
} catch (Exception e) {
logger.error("For " + followTargetGoal.toString() + ": ", e);
return false;
}
}

private void updateMobSetApathyGoal(MobEntity entity, Set<Goal> targetSelectorSet, int priority, ActiveTargetGoal<PlayerEntity> followTargetGoal, ApathyConfig.ApathyBehaviourType apathyBehaviourType) {
ApathyMixinActiveTargetGoalAccessor followTargetGoalAccessor = (ApathyMixinActiveTargetGoalAccessor) followTargetGoal;
ApathyMixinTrackTargetGoalAccessor trackTargetGoalAccessor = (ApathyMixinTrackTargetGoalAccessor) followTargetGoal;

logger.debug("[" + entity + "] Applied " + apathyBehaviourType);
if(apathyBehaviourType instanceof ApathyConfig.ApathyBehaviourDoNotFollowType) {
targetSelectorSet.add(new PrioritizedGoal(
priority,
new ApathyDoNotActiveTargetGoal<>(
entity,
PlayerEntity.class,
followTargetGoalAccessor.getReciprocalChance(),
trackTargetGoalAccessor.getCheckVisibility(),
trackTargetGoalAccessor.getCheckVisibility(),
followTargetGoalAccessor.getTargetPredicate()
)
));
} else if(apathyBehaviourType instanceof ApathyConfig.ApathyBehaviourIfBlockBrokenType) {
ApathyConfig.ApathyBehaviourIfBlockBrokenType ifBlockBrokenBehaviour = (ApathyConfig.ApathyBehaviourIfBlockBrokenType) apathyBehaviourType;
targetSelectorSet.add(new PrioritizedGoal(
priority,
new ApathyIfBlockBrokenActiveTargetGoal(
entity,
followTargetGoalAccessor.getReciprocalChance(),
trackTargetGoalAccessor.getCheckVisibility(),
trackTargetGoalAccessor.getCheckVisibility(),
followTargetGoalAccessor.getTargetPredicate(),
ifBlockBrokenBehaviour.getMaximalReactionDistance(),
ifBlockBrokenBehaviour.getReactionBlock()
)
));
} else if(apathyBehaviourType instanceof ApathyConfig.ApathyBehaviourIfItemSelectedType) {
ApathyConfig.ApathyBehaviourIfItemSelectedType ifItemSelectedBehaviour = (ApathyConfig.ApathyBehaviourIfItemSelectedType) apathyBehaviourType;
targetSelectorSet.add(new PrioritizedGoal(
priority,
new ApathyIfItemSelectedActiveTargetGoal(
entity,
followTargetGoalAccessor.getReciprocalChance(),
trackTargetGoalAccessor.getCheckVisibility(),
trackTargetGoalAccessor.getCheckVisibility(),
followTargetGoalAccessor.getTargetPredicate(),
ifItemSelectedBehaviour.getMaximalReactionDistance(),
ifItemSelectedBehaviour.getReactionItem(),
ifItemSelectedBehaviour.getReactionItemCount()
)
));
}
}

private void updateMobSetApathyGoals(ApathyConfig apathyConfig, MobEntity entity, Set<Goal> targetSelectorSet, int priority, ActiveTargetGoal<PlayerEntity> followTargetGoal) {
List<ApathyConfig.ApathyBehaviourType> defaultApathyBehaviourType = apathyConfig.getApathyBehaviourTypeMap().getOrDefault(null, new ArrayList<>());
List<ApathyConfig.ApathyBehaviourType> mobSpecificApathyBehaviourType = apathyConfig.getApathyBehaviourTypeMap().get(Registry.ENTITY_TYPE.getId(entity.getType()));

if(mobSpecificApathyBehaviourType == null) {
defaultApathyBehaviourType.forEach(apathyBehaviourType -> updateMobSetApathyGoal(
entity,
targetSelectorSet,
priority,
followTargetGoal,
apathyBehaviourType
));
} else {
mobSpecificApathyBehaviourType.forEach(apathyBehaviourType -> updateMobSetApathyGoal(
entity,
targetSelectorSet,
priority,
followTargetGoal,
apathyBehaviourType
));
}
}

@Override
public void onInitialize() {
ApathyConfig apathyConfig;
try {
apathyConfig = ApathyConfigLoader.loadConfig();
} catch (IOException e) {
apathyConfig = new ApathyConfig(new HashMap<>());
}

logger.info("Loaded with config: " + apathyConfig);

ApathyConfig finalApathyConfig = apathyConfig;
Registry.ENTITY_TYPE
.streamEntries()
.map(RegistryEntry.Reference::value)
.map((entityType) -> new Pair<>((EntityType<Entity>) entityType, getEntityFactory((EntityType<Entity>) entityType)))
.map((entityTypeToFactory) -> setEntityFactory(entityTypeToFactory.getLeft(), (type, world) -> {
Entity entity = entityTypeToFactory.getRight().create(type, world);

if (entity instanceof MobEntity) {
Set<Goal> targetSelectorSet = getGoalSetFromMobEntitySelector((MobEntity) entity, GoalSelectorFieldName.TARGET_SELECTOR);
if(targetSelectorSet != null)
targetSelectorSet.stream()
.map((goal) -> (PrioritizedGoal) goal)
.filter((goal) -> goal.getGoal() instanceof ActiveTargetGoal)
.filter((goal) -> (isPlayerActiveTargetGoal((ActiveTargetGoal) goal.getGoal())))
.toList()
.forEach((PrioritizedGoal goal) -> {
ActiveTargetGoal<PlayerEntity> followTargetGoal = (ActiveTargetGoal<PlayerEntity>) goal.getGoal();

targetSelectorSet.remove(goal);
updateMobSetApathyGoals(finalApathyConfig, (MobEntity) entity, targetSelectorSet, goal.getPriority(), followTargetGoal);
});
}

return entity;
}))
.forEach((entityType) -> {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ApathyIfBlockBrokenActiveTargetGoal extends ActiveTargetGoal<Player
private final float maximalReactionDistance;
private final Block reactionBlock;

private UUID onBlockBrokenHandlerId;
private final UUID onBlockBrokenHandlerId;
private UUID onLivingEntityDeadHandlerId;

private final Map<UUID, BlockState> playerMemory = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ApathyIfItemSelectedActiveTargetGoal extends ActiveTargetGoal<Playe
private final Item reactionItem;
private final int reactionItemCount;

private UUID onHandStackChangedHandlerId;
private final UUID onHandStackChangedHandlerId;
private UUID onLivingEntityDeadHandlerId;

private final Map<UUID, ItemStack> playerMemory = new HashMap<>();
Expand Down
Loading

0 comments on commit 3622267

Please sign in to comment.