From c7ec5d32bc65bf27eed7f8412cb3da618493faab Mon Sep 17 00:00:00 2001 From: Senior-S Date: Wed, 21 Aug 2024 21:29:28 -0300 Subject: [PATCH] feat: New conditions for titles, added support for Lucky's Armory --- VERSION | 2 +- gradle.properties | 2 +- .../justlevelingfork/JustLevelingFork.java | 5 +- .../common/capability/AptitudeCapability.java | 10 ++- .../conditions/AdvancementCondition.java | 33 +++++++++ .../config/conditions/AptitudeCondition.java | 37 ++++++++++ .../conditions/BlockMinedCondition.java | 42 +++++++++++ .../config/conditions/ConditionImpl.java | 29 ++++++++ .../config/conditions/DimensionCondition.java | 21 ++++++ .../conditions/EntityKilledByCondition.java | 40 ++++++++++ .../conditions/EntityKilledCondition.java | 40 ++++++++++ .../conditions/ItemBrokenCondition.java | 41 +++++++++++ .../conditions/ItemCraftedCondition.java | 42 +++++++++++ .../conditions/ItemDroppedCondition.java | 42 +++++++++++ .../conditions/ItemPickedUpCondition.java | 42 +++++++++++ .../config/conditions/ItemUsedCondition.java | 42 +++++++++++ .../config/conditions/StatCondition.java | 42 +++++++++++ .../config/models/TitleModel.java | 73 +++---------------- .../handler/HandlerConditions.java | 42 +++++++++++ .../registry/RegistryClientEvents.java | 2 +- .../registry/RegistryCommonEvents.java | 10 +-- .../registry/RegistryTitles.java | 6 +- 22 files changed, 570 insertions(+), 75 deletions(-) create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/AdvancementCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/AptitudeCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/BlockMinedCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/ConditionImpl.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/DimensionCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/EntityKilledByCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/EntityKilledCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/ItemBrokenCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/ItemCraftedCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/ItemDroppedCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/ItemPickedUpCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/ItemUsedCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/config/conditions/StatCondition.java create mode 100644 src/main/java/com/seniors/justlevelingfork/handler/HandlerConditions.java diff --git a/VERSION b/VERSION index e5a4a5e..1cc5f65 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.9 \ No newline at end of file +1.1.0 \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index acb9686..fe40e5a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -43,7 +43,7 @@ mod_name=JustLevelingFork # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=APACHE # The mod version. See https://semver.org/ -mod_version=1.0.9 +mod_version=1.1.0 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/com/seniors/justlevelingfork/JustLevelingFork.java b/src/main/java/com/seniors/justlevelingfork/JustLevelingFork.java index 5602509..e26a8e0 100644 --- a/src/main/java/com/seniors/justlevelingfork/JustLevelingFork.java +++ b/src/main/java/com/seniors/justlevelingfork/JustLevelingFork.java @@ -4,10 +4,7 @@ import com.seniors.justlevelingfork.config.Configuration; import com.seniors.justlevelingfork.config.models.EAptitude; import com.seniors.justlevelingfork.config.models.LockItem; -import com.seniors.justlevelingfork.handler.HandlerCommonConfig; -import com.seniors.justlevelingfork.handler.HandlerConfigCommon; -import com.seniors.justlevelingfork.handler.HandlerCurios; -import com.seniors.justlevelingfork.handler.HandlerLockItemsConfig; +import com.seniors.justlevelingfork.handler.*; import com.seniors.justlevelingfork.integration.CrayfishGunModIntegration; import com.seniors.justlevelingfork.integration.IronsSpellsbooksIntegration; import com.seniors.justlevelingfork.integration.ScorchedGuns2Integration; diff --git a/src/main/java/com/seniors/justlevelingfork/common/capability/AptitudeCapability.java b/src/main/java/com/seniors/justlevelingfork/common/capability/AptitudeCapability.java index 793e6d0..fda1aa7 100644 --- a/src/main/java/com/seniors/justlevelingfork/common/capability/AptitudeCapability.java +++ b/src/main/java/com/seniors/justlevelingfork/common/capability/AptitudeCapability.java @@ -18,8 +18,10 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.Block; import net.minecraftforge.common.util.INBTSerializable; +import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.registries.ForgeRegistries; +import javax.annotation.Nullable; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -88,8 +90,14 @@ public void setCounterAttackTimer(int timer) { this.counterAttackTimer = timer; } + @Nullable public static AptitudeCapability get(Player player) { - return player.getCapability(RegistryCapabilities.APTITUDE).orElseThrow(() -> new IllegalArgumentException("Player " + player.getName() + " does not have Capabilities!")); + LazyOptional capability = player.getCapability(RegistryCapabilities.APTITUDE); + if(capability.isPresent() && capability.resolve().isPresent()){ + return capability.resolve().get(); + } + + return null; } public static AptitudeCapability get() { diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/AdvancementCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/AdvancementCondition.java new file mode 100644 index 0000000..624e75b --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/AdvancementCondition.java @@ -0,0 +1,33 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.JustLevelingFork; +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.advancements.Advancement; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; + +import java.util.Objects; + +public class AdvancementCondition extends ConditionImpl { + + public AdvancementCondition(){ + super("Advancement"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + Advancement advancement = Objects.requireNonNull(serverPlayer.getServer()).getAdvancements().getAdvancement(new ResourceLocation(value.replace("-", "/"))); + if (advancement == null){ + JustLevelingFork.getLOGGER().error(">> Error! Advancement name {} not found!", value); + setProcessedValue(false); + return; + } + + setProcessedValue(serverPlayer.getAdvancements().getOrStartProgress(advancement).isDone()); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + return getProcessedValue(); + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/AptitudeCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/AptitudeCondition.java new file mode 100644 index 0000000..b0435a3 --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/AptitudeCondition.java @@ -0,0 +1,37 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.common.capability.AptitudeCapability; +import com.seniors.justlevelingfork.config.models.EAptitude; +import com.seniors.justlevelingfork.config.models.TitleModel; +import com.seniors.justlevelingfork.registry.RegistryAptitudes; +import net.minecraft.server.level.ServerPlayer; +import org.apache.commons.lang3.StringUtils; + +public class AptitudeCondition extends ConditionImpl { + + public AptitudeCondition() { + super("Aptitude"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + EAptitude aptitude = EAptitude.valueOf(StringUtils.capitalize(value)); + int aptitudeLevel = AptitudeCapability.get(serverPlayer).getAptitudeLevel(RegistryAptitudes.getAptitude(aptitude.toString())); + + setProcessedValue(aptitudeLevel); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + int parsedValue = Integer.parseInt(value); + + return switch (comparator) { + case EQUALS -> getProcessedValue().equals(parsedValue); + case GREATER -> getProcessedValue() > parsedValue; + case LESS -> getProcessedValue() < parsedValue; + case GREATER_OR_EQUAL -> getProcessedValue() >= parsedValue; + case LESS_OR_EQUAL -> getProcessedValue() <= parsedValue; + default -> false; + }; + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/BlockMinedCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/BlockMinedCondition.java new file mode 100644 index 0000000..dd586ff --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/BlockMinedCondition.java @@ -0,0 +1,42 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.JustLevelingFork; +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.stats.Stats; + +import java.util.Optional; + +public class BlockMinedCondition extends ConditionImpl { + + public BlockMinedCondition(){ + super("BlockMined"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + var optionalStat = Optional.ofNullable(ResourceLocation.tryParse(value.toLowerCase())).flatMap(Stats.BLOCK_MINED.getRegistry()::getOptional).map(Stats.BLOCK_MINED::get); + if (optionalStat.isEmpty()) { + JustLevelingFork.getLOGGER().error(">> Error! Block name {} not found!", value); + setProcessedValue(0); + return; + } + + setProcessedValue(serverPlayer.getStats().getValue(Stats.BLOCK_MINED, optionalStat.get().getValue())); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + int parsedValue = Integer.parseInt(value); + + return switch (comparator) { + case EQUALS -> getProcessedValue().equals(parsedValue); + case GREATER -> getProcessedValue() > parsedValue; + case LESS -> getProcessedValue() < parsedValue; + case GREATER_OR_EQUAL -> getProcessedValue() >= parsedValue; + case LESS_OR_EQUAL -> getProcessedValue() <= parsedValue; + default -> false; + }; + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/ConditionImpl.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/ConditionImpl.java new file mode 100644 index 0000000..c0fda22 --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/ConditionImpl.java @@ -0,0 +1,29 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.server.level.ServerPlayer; + +public abstract class ConditionImpl { + + private String _conditionName; + public String getConditionName(){ + return _conditionName; + } + + private T _processedValue; + public T getProcessedValue(){ + return _processedValue; + } + public void setProcessedValue(T value) + { + _processedValue = value; + } + + public ConditionImpl(String conditionName){ + _conditionName = conditionName; + } + + public abstract void ProcessVariable(String value, ServerPlayer serverPlayer); + + public abstract boolean MeetCondition(String value, TitleModel.EComparator comparator); +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/DimensionCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/DimensionCondition.java new file mode 100644 index 0000000..04e5861 --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/DimensionCondition.java @@ -0,0 +1,21 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.server.level.ServerPlayer; + +public class DimensionCondition extends ConditionImpl { + + public DimensionCondition(){ + super("Special"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + setProcessedValue(serverPlayer.level().dimension().location().toString()); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + return getProcessedValue().equalsIgnoreCase(value); + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/EntityKilledByCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/EntityKilledByCondition.java new file mode 100644 index 0000000..ebe3211 --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/EntityKilledByCondition.java @@ -0,0 +1,40 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.JustLevelingFork; +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.stats.Stats; +import net.minecraft.world.entity.EntityType; + +public class EntityKilledByCondition extends ConditionImpl { + + public EntityKilledByCondition(){ + super("EntiyKilledBy"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + var entityType = EntityType.byString(value); + if (entityType.isEmpty()) { + setProcessedValue(0); + JustLevelingFork.getLOGGER().error(">> Error! Entity name {} not found!", value); + return; + } + + setProcessedValue(serverPlayer.getStats().getValue(Stats.ENTITY_KILLED_BY.get(entityType.get()))); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + int parsedValue = Integer.parseInt(value); + + return switch (comparator) { + case EQUALS -> getProcessedValue().equals(parsedValue); + case GREATER -> getProcessedValue() > parsedValue; + case LESS -> getProcessedValue() < parsedValue; + case GREATER_OR_EQUAL -> getProcessedValue() >= parsedValue; + case LESS_OR_EQUAL -> getProcessedValue() <= parsedValue; + default -> false; + }; + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/EntityKilledCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/EntityKilledCondition.java new file mode 100644 index 0000000..6614e76 --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/EntityKilledCondition.java @@ -0,0 +1,40 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.JustLevelingFork; +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.stats.Stats; +import net.minecraft.world.entity.EntityType; + +public class EntityKilledCondition extends ConditionImpl { + + public EntityKilledCondition(){ + super("EntityKilled"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + var entityType = EntityType.byString(value); + if (entityType.isEmpty()) { + JustLevelingFork.getLOGGER().error(">> Error! Entity name {} not found!", value); + setProcessedValue(0); + return; + } + + setProcessedValue(serverPlayer.getStats().getValue(Stats.ENTITY_KILLED.get(entityType.get()))); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + int parsedValue = Integer.parseInt(value); + + return switch (comparator) { + case EQUALS -> getProcessedValue().equals(parsedValue); + case GREATER -> getProcessedValue() > parsedValue; + case LESS -> getProcessedValue() < parsedValue; + case GREATER_OR_EQUAL -> getProcessedValue() >= parsedValue; + case LESS_OR_EQUAL -> getProcessedValue() <= parsedValue; + default -> false; + }; + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemBrokenCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemBrokenCondition.java new file mode 100644 index 0000000..eaff73a --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemBrokenCondition.java @@ -0,0 +1,41 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.JustLevelingFork; +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.stats.Stats; + +import java.util.Optional; + +public class ItemBrokenCondition extends ConditionImpl { + public ItemBrokenCondition(){ + super("ItemBroken"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + var optionalStat = Optional.ofNullable(ResourceLocation.tryParse(value.toLowerCase())).flatMap(Stats.ITEM_BROKEN.getRegistry()::getOptional).map(Stats.ITEM_BROKEN::get); + if (optionalStat.isEmpty()) { + JustLevelingFork.getLOGGER().error(">> Error! Item name {} not found!", value); + setProcessedValue(0); + return; + } + + setProcessedValue(serverPlayer.getStats().getValue(Stats.ITEM_BROKEN, optionalStat.get().getValue())); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + int parsedValue = Integer.parseInt(value); + + return switch (comparator) { + case EQUALS -> getProcessedValue().equals(parsedValue); + case GREATER -> getProcessedValue() > parsedValue; + case LESS -> getProcessedValue() < parsedValue; + case GREATER_OR_EQUAL -> getProcessedValue() >= parsedValue; + case LESS_OR_EQUAL -> getProcessedValue() <= parsedValue; + default -> false; + }; + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemCraftedCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemCraftedCondition.java new file mode 100644 index 0000000..d76ee11 --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemCraftedCondition.java @@ -0,0 +1,42 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.JustLevelingFork; +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.stats.Stats; + +import java.util.Optional; + +public class ItemCraftedCondition extends ConditionImpl { + + public ItemCraftedCondition(){ + super("ItemCrafted"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + var optionalStat = Optional.ofNullable(ResourceLocation.tryParse(value.toLowerCase())).flatMap(Stats.ITEM_CRAFTED.getRegistry()::getOptional).map(Stats.ITEM_CRAFTED::get); + if (optionalStat.isEmpty()) { + JustLevelingFork.getLOGGER().error(">> Error! Item name {} not found!", value); + setProcessedValue(0); + return; + } + + setProcessedValue(serverPlayer.getStats().getValue(Stats.ITEM_CRAFTED, optionalStat.get().getValue())); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + int parsedValue = Integer.parseInt(value); + + return switch (comparator) { + case EQUALS -> getProcessedValue().equals(parsedValue); + case GREATER -> getProcessedValue() > parsedValue; + case LESS -> getProcessedValue() < parsedValue; + case GREATER_OR_EQUAL -> getProcessedValue() >= parsedValue; + case LESS_OR_EQUAL -> getProcessedValue() <= parsedValue; + default -> false; + }; + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemDroppedCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemDroppedCondition.java new file mode 100644 index 0000000..58f9a9a --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemDroppedCondition.java @@ -0,0 +1,42 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.JustLevelingFork; +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.stats.Stats; + +import java.util.Optional; + +public class ItemDroppedCondition extends ConditionImpl { + + public ItemDroppedCondition(){ + super("ItemDropped"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + var optionalStat = Optional.ofNullable(ResourceLocation.tryParse(value.toLowerCase())).flatMap(Stats.ITEM_DROPPED.getRegistry()::getOptional).map(Stats.ITEM_DROPPED::get); + if (optionalStat.isEmpty()) { + JustLevelingFork.getLOGGER().error(">> Error! Item name {} not found!", value); + setProcessedValue(0); + return; + } + + setProcessedValue(serverPlayer.getStats().getValue(Stats.ITEM_DROPPED, optionalStat.get().getValue())); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + int parsedValue = Integer.parseInt(value); + + return switch (comparator) { + case EQUALS -> getProcessedValue().equals(parsedValue); + case GREATER -> getProcessedValue() > parsedValue; + case LESS -> getProcessedValue() < parsedValue; + case GREATER_OR_EQUAL -> getProcessedValue() >= parsedValue; + case LESS_OR_EQUAL -> getProcessedValue() <= parsedValue; + default -> false; + }; + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemPickedUpCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemPickedUpCondition.java new file mode 100644 index 0000000..f6b4305 --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemPickedUpCondition.java @@ -0,0 +1,42 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.JustLevelingFork; +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.stats.Stats; + +import java.util.Optional; + +public class ItemPickedUpCondition extends ConditionImpl { + + public ItemPickedUpCondition(){ + super("ItemPickedUp"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + var optionalStat = Optional.ofNullable(ResourceLocation.tryParse(value.toLowerCase())).flatMap(Stats.ITEM_PICKED_UP.getRegistry()::getOptional).map(Stats.ITEM_PICKED_UP::get); + if (optionalStat.isEmpty()) { + JustLevelingFork.getLOGGER().error(">> Error! Item name {} not found!", value); + setProcessedValue(0); + return; + } + + setProcessedValue(serverPlayer.getStats().getValue(Stats.ITEM_PICKED_UP, optionalStat.get().getValue())); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + int parsedValue = Integer.parseInt(value); + + return switch (comparator) { + case EQUALS -> getProcessedValue().equals(parsedValue); + case GREATER -> getProcessedValue() > parsedValue; + case LESS -> getProcessedValue() < parsedValue; + case GREATER_OR_EQUAL -> getProcessedValue() >= parsedValue; + case LESS_OR_EQUAL -> getProcessedValue() <= parsedValue; + default -> false; + }; + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemUsedCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemUsedCondition.java new file mode 100644 index 0000000..d94c485 --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/ItemUsedCondition.java @@ -0,0 +1,42 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.JustLevelingFork; +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.stats.Stats; + +import java.util.Optional; + +public class ItemUsedCondition extends ConditionImpl { + + public ItemUsedCondition(){ + super("ItemUsed"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + var optionalStat = Optional.ofNullable(ResourceLocation.tryParse(value.toLowerCase())).flatMap(Stats.ITEM_USED.getRegistry()::getOptional).map(Stats.ITEM_USED::get); + if (optionalStat.isEmpty()) { + JustLevelingFork.getLOGGER().error(">> Error! Item name {} not found!", value); + setProcessedValue(0); + return; + } + + setProcessedValue(serverPlayer.getStats().getValue(Stats.ITEM_USED, optionalStat.get().getValue())); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + int parsedValue = Integer.parseInt(value); + + return switch (comparator) { + case EQUALS -> getProcessedValue().equals(parsedValue); + case GREATER -> getProcessedValue() > parsedValue; + case LESS -> getProcessedValue() < parsedValue; + case GREATER_OR_EQUAL -> getProcessedValue() >= parsedValue; + case LESS_OR_EQUAL -> getProcessedValue() <= parsedValue; + default -> false; + }; + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/conditions/StatCondition.java b/src/main/java/com/seniors/justlevelingfork/config/conditions/StatCondition.java new file mode 100644 index 0000000..763473f --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/config/conditions/StatCondition.java @@ -0,0 +1,42 @@ +package com.seniors.justlevelingfork.config.conditions; + +import com.seniors.justlevelingfork.JustLevelingFork; +import com.seniors.justlevelingfork.config.models.TitleModel; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.stats.Stats; + +import java.util.Optional; + +public class StatCondition extends ConditionImpl { + + public StatCondition(){ + super("Stat"); + } + + @Override + public void ProcessVariable(String value, ServerPlayer serverPlayer) { + var optionalStat = Optional.ofNullable(ResourceLocation.tryParse(value.toLowerCase())).flatMap(Stats.CUSTOM.getRegistry()::getOptional).map(Stats.CUSTOM::get); + if (optionalStat.isEmpty()) { + JustLevelingFork.getLOGGER().error(">> Error! Stat name {} not found!", value); + setProcessedValue(0); + return; + } + + setProcessedValue(serverPlayer.getStats().getValue(Stats.CUSTOM, optionalStat.get().getValue())); + } + + @Override + public boolean MeetCondition(String value, TitleModel.EComparator comparator) { + int parsedValue = Integer.parseInt(value); + + return switch (comparator) { + case EQUALS -> getProcessedValue().equals(parsedValue); + case GREATER -> getProcessedValue() > parsedValue; + case LESS -> getProcessedValue() < parsedValue; + case GREATER_OR_EQUAL -> getProcessedValue() >= parsedValue; + case LESS_OR_EQUAL -> getProcessedValue() <= parsedValue; + default -> false; + }; + } +} diff --git a/src/main/java/com/seniors/justlevelingfork/config/models/TitleModel.java b/src/main/java/com/seniors/justlevelingfork/config/models/TitleModel.java index 660b0a9..4e815e6 100644 --- a/src/main/java/com/seniors/justlevelingfork/config/models/TitleModel.java +++ b/src/main/java/com/seniors/justlevelingfork/config/models/TitleModel.java @@ -1,16 +1,13 @@ package com.seniors.justlevelingfork.config.models; import com.seniors.justlevelingfork.JustLevelingFork; -import com.seniors.justlevelingfork.common.capability.AptitudeCapability; -import com.seniors.justlevelingfork.registry.RegistryAptitudes; +import com.seniors.justlevelingfork.config.conditions.ConditionImpl; +import com.seniors.justlevelingfork.handler.HandlerConditions; import com.seniors.justlevelingfork.registry.title.Title; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; -import net.minecraft.stats.Stats; -import net.minecraft.world.entity.EntityType; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.RegistryObject; -import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.List; @@ -70,77 +67,31 @@ public boolean CheckRequirements(ServerPlayer serverPlayer) { JustLevelingFork.getLOGGER().error(">> Error! Title {} have a wrong formatted condition. (General)", TitleId); continue; } - EConditionType conditionType; EComparator comparator; - int value = 0; try { - conditionType = EConditionType.valueOf(StringUtils.capitalize(split[0])); comparator = EComparator.valueOf(split[2].toUpperCase()); - if (conditionType != EConditionType.Special) { - value = Integer.parseInt(split[3]); - } - } catch (NumberFormatException e) { - JustLevelingFork.getLOGGER().error(">> Error! Title {} have a wrong formatted condition. (Value)", TitleId); - continue; } catch (IllegalArgumentException e) { + JustLevelingFork.getLOGGER().error(">> Error! Title {} have a wrong formatted condition. (Comparator)", TitleId); + continue; + } + + Optional> conditionImpl = HandlerConditions.getConditionByName(split[0]); + if(conditionImpl.isEmpty()){ JustLevelingFork.getLOGGER().error(">> Error! Title {} have a wrong formatted condition. (Condition type or Comparator)", TitleId); continue; } - switch (conditionType) { - case Aptitude: - try { - EAptitude aptitude = EAptitude.valueOf(StringUtils.capitalize(split[1])); - int aptitudeLevel = AptitudeCapability.get(serverPlayer).getAptitudeLevel(RegistryAptitudes.getAptitude(aptitude.toString())); - - if (Compare(aptitudeLevel, value, comparator)) { - passedConditions++; - } - } catch (IllegalArgumentException e) { - JustLevelingFork.getLOGGER().error(">> Error! Title {} have a wrong formatted condition. (Aptitude Name)", TitleId); - } - break; - case Stat: - var optionalStat = Optional.ofNullable(ResourceLocation.tryParse(split[1].toLowerCase())).flatMap(Stats.CUSTOM.getRegistry()::getOptional).map(Stats.CUSTOM::get); - if (optionalStat.isEmpty()) { - JustLevelingFork.getLOGGER().error(">> Error! Title {} have a wrong formatted condition. (Stat name)", TitleId); - break; - } - - int statValue = serverPlayer.getStats().getValue(Stats.CUSTOM, optionalStat.get().getValue()); - - if (Compare(statValue, value, comparator)) { - passedConditions++; - } - break; - case EntityKilled: - var entityType = EntityType.byString(split[1].toLowerCase()); - if (entityType.isEmpty()) { - JustLevelingFork.getLOGGER().error(">> Error! Title {} have a wrong formatted condition. (Entity name)", TitleId); - break; - } - - int entityKilledValue = serverPlayer.getStats().getValue(Stats.ENTITY_KILLED.get(entityType.get())); - - if (Compare(entityKilledValue, value, comparator)) { - passedConditions++; - } - break; - case Special: - String dimension = split[3]; - - if(serverPlayer.level().dimension().location().toString().equalsIgnoreCase(dimension)){ - passedConditions++; - } - break; + conditionImpl.get().ProcessVariable(split[1], serverPlayer); + if(conditionImpl.get().MeetCondition(split[3], comparator)){ + passedConditions++; } } return passedConditions == Conditions.size(); } - public RegistryObject Registry(DeferredRegister<Title> TITLES) { + public RegistryObject<Title> registry(DeferredRegister<Title> TITLES) { _title = register(TitleId, Default); return TITLES.register(TitleId, () -> _title); } diff --git a/src/main/java/com/seniors/justlevelingfork/handler/HandlerConditions.java b/src/main/java/com/seniors/justlevelingfork/handler/HandlerConditions.java new file mode 100644 index 0000000..d3f60d9 --- /dev/null +++ b/src/main/java/com/seniors/justlevelingfork/handler/HandlerConditions.java @@ -0,0 +1,42 @@ +package com.seniors.justlevelingfork.handler; + +import com.seniors.justlevelingfork.config.conditions.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class HandlerConditions { + + private static final List<ConditionImpl<?>> Conditions = new ArrayList<>(); + + public static void registerDefaults(){ + registerCondition(new AptitudeCondition()); + registerCondition(new DimensionCondition()); + registerCondition(new EntityKilledCondition()); + registerCondition(new EntityKilledByCondition()); + registerCondition(new StatCondition()); + registerCondition(new BlockMinedCondition()); + registerCondition(new ItemCraftedCondition()); + registerCondition(new ItemUsedCondition()); + registerCondition(new ItemBrokenCondition()); + registerCondition(new ItemPickedUpCondition()); + registerCondition(new ItemDroppedCondition()); + registerCondition(new AdvancementCondition()); + } + + public static void registerCondition(ConditionImpl<?> condition){ + if(Conditions.stream().anyMatch(c -> c.getConditionName().equalsIgnoreCase(condition.getConditionName()))){ + throw new IllegalArgumentException(String.format("Condition with name %s already exists!", condition.getConditionName())); + } + + Conditions.add(condition); + } + + public static Optional<ConditionImpl<?>> getConditionByName(String conditionName){ + return Conditions.stream().filter(c -> c.getConditionName().equalsIgnoreCase(conditionName)).findFirst(); + } + + + +} diff --git a/src/main/java/com/seniors/justlevelingfork/registry/RegistryClientEvents.java b/src/main/java/com/seniors/justlevelingfork/registry/RegistryClientEvents.java index 9e820d0..eea0730 100644 --- a/src/main/java/com/seniors/justlevelingfork/registry/RegistryClientEvents.java +++ b/src/main/java/com/seniors/justlevelingfork/registry/RegistryClientEvents.java @@ -21,7 +21,7 @@ /** - * Registry CLIENT events into the forge mod bus. + * registry CLIENT events into the forge mod bus. */ public class RegistryClientEvents { @SubscribeEvent diff --git a/src/main/java/com/seniors/justlevelingfork/registry/RegistryCommonEvents.java b/src/main/java/com/seniors/justlevelingfork/registry/RegistryCommonEvents.java index 16650bf..cac1337 100644 --- a/src/main/java/com/seniors/justlevelingfork/registry/RegistryCommonEvents.java +++ b/src/main/java/com/seniors/justlevelingfork/registry/RegistryCommonEvents.java @@ -42,10 +42,7 @@ import net.minecraftforge.event.entity.living.LivingDropsEvent; import net.minecraftforge.event.entity.living.LivingEquipmentChangeEvent; import net.minecraftforge.event.entity.living.LivingHurtEvent; -import net.minecraftforge.event.entity.player.AttackEntityEvent; -import net.minecraftforge.event.entity.player.CriticalHitEvent; -import net.minecraftforge.event.entity.player.PlayerEvent; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; +import net.minecraftforge.event.entity.player.*; import net.minecraftforge.event.level.BlockEvent; import net.minecraftforge.event.server.ServerStartingEvent; import net.minecraftforge.eventbus.api.Event; @@ -61,7 +58,7 @@ import java.util.*; /** - * Registry COMMON events into the forge mod bus. + * registry COMMON events into the forge mod bus. */ @Mod.EventBusSubscriber(modid = JustLevelingFork.MOD_ID) public class RegistryCommonEvents { @@ -271,6 +268,9 @@ public void onChangeEquipment(LivingEquipmentChangeEvent event) { if (livingEntity instanceof Player player) { if (!player.isCreative() && event.getSlot().getType() == EquipmentSlot.Type.ARMOR) { AptitudeCapability provider = AptitudeCapability.get(player); + // Check added due some mods make changes to the equipment before the player fully connects + // So if the player haven't fully connect to the server yet, it won't be able to find its capabilities. + if(provider == null) return; ItemStack item = event.getTo(); if (!provider.canUseItem(player, item)) { diff --git a/src/main/java/com/seniors/justlevelingfork/registry/RegistryTitles.java b/src/main/java/com/seniors/justlevelingfork/registry/RegistryTitles.java index eb63974..da11580 100644 --- a/src/main/java/com/seniors/justlevelingfork/registry/RegistryTitles.java +++ b/src/main/java/com/seniors/justlevelingfork/registry/RegistryTitles.java @@ -3,6 +3,7 @@ import com.seniors.justlevelingfork.JustLevelingFork; import com.seniors.justlevelingfork.common.capability.AptitudeCapability; import com.seniors.justlevelingfork.config.models.TitleModel; +import com.seniors.justlevelingfork.handler.HandlerConditions; import com.seniors.justlevelingfork.handler.HandlerTitlesConfig; import com.seniors.justlevelingfork.registry.title.Title; import net.minecraft.core.Registry; @@ -29,10 +30,13 @@ public class RegistryTitles { public static void load(IEventBus eventBus) { HandlerTitlesConfig.HANDLER.instance().titleList.forEach(title -> { - title.Registry(TITLES); + title.registry(TITLES); }); TITLES.register(eventBus); + + // Title conditions + HandlerConditions.registerDefaults(); } private static Title register(String name, boolean requirement) {