From dd3e1276ec6afb77df2204d2cdfeea2cc4e25af2 Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Sun, 4 May 2025 19:52:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E6=95=B0=E5=AD=97=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/translations/en.yml | 21 +++++++- .../src/main/resources/translations/zh_cn.yml | 25 +++++++-- .../bukkit/block/BlockEventListener.java | 26 +++++----- .../block/behavior/CropBlockBehavior.java | 3 +- .../item/recipe/RecipeEventListener.java | 2 +- .../bukkit/loot/BukkitVanillaLootManager.java | 1 - .../plugin/command/feature/TestCommand.java | 12 ++--- .../plugin/network/PacketConsumers.java | 1 - .../core/font/AbstractFontManager.java | 13 ++--- .../craftengine/core/item/CustomItem.java | 3 +- .../core/item/ItemBuildContext.java | 15 ++++++ .../craftengine/core/loot/LootContext.java | 17 ++---- .../craftengine/core/loot/LootPool.java | 5 +- .../craftengine/core/loot/LootTable.java | 3 +- .../core/loot/condition/LootConditions.java | 20 +++---- .../function/LootFunctionApplyBonusCount.java | 3 +- .../function/LootFunctionExplosionDecay.java | 9 ++-- .../pack/conflict/matcher/PathMatchers.java | 8 +-- ...> AbstractAdditionalParameterContext.java} | 6 +-- .../plugin/context/AbstractCommonContext.java | 7 +-- .../core/plugin/context/ContextHolder.java | 2 +- .../core/plugin/context/ContextKey.java | 14 ++--- .../plugin/context/PlayerOptionalContext.java | 16 +++++- .../context/condition/AllOfCondition.java | 24 ++++++--- .../context/condition/AnyOfCondition.java | 24 ++++++--- ...dConditions.java => CommonConditions.java} | 4 +- .../context/condition/EmptyCondition.java | 2 +- .../condition/EnchantmentCondition.java | 15 ++++-- .../condition/FallingBlockCondition.java | 2 +- .../context/condition/InvertedCondition.java | 24 +++++++-- .../MatchBlockPropertyCondition.java | 14 ++--- .../context/condition/MatchItemCondition.java | 6 ++- .../context/condition/RandomCondition.java | 13 ++--- .../condition/SurvivesExplosionCondition.java | 2 +- .../condition/TableBonusCondition.java | 6 +-- .../number/ExpressionNumberProvider.java | 52 +++++++++++++++++++ .../context/number/FixedNumberProvider.java | 23 ++++++-- .../context/number/NumberProviderFactory.java | 8 --- .../context/number/NumberProviders.java | 44 +++++++++------- .../context/number/UniformNumberProvider.java | 22 +++++--- .../context/parameter/CommonParameters.java | 3 -- .../gui/category/ItemBrowserManagerImpl.java | 52 +++++++++---------- .../text/minimessage/NamedArgumentTag.java | 11 ++-- .../text/minimessage/PlaceholderTag.java | 7 ++- .../core/registry/BuiltInRegistries.java | 4 +- .../craftengine/core/registry/Registries.java | 4 +- .../craftengine/core/util/RandomUtils.java | 28 +++------- .../craftengine/core/world/chunk/CEChunk.java | 4 +- 48 files changed, 388 insertions(+), 242 deletions(-) rename core/src/main/java/net/momirealms/craftengine/core/plugin/context/{AbstractAdditionalCommonContext.java => AbstractAdditionalParameterContext.java} (81%) rename core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/{SharedConditions.java => CommonConditions.java} (93%) create mode 100644 core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/ExpressionNumberProvider.java delete mode 100644 core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/NumberProviderFactory.java diff --git a/bukkit/loader/src/main/resources/translations/en.yml b/bukkit/loader/src/main/resources/translations/en.yml index 28cf30a1f..6011167dd 100644 --- a/bukkit/loader/src/main/resources/translations/en.yml +++ b/bukkit/loader/src/main/resources/translations/en.yml @@ -70,6 +70,25 @@ warning.config.type.float: "Issue found in file - Failed to load warning.config.type.double: "Issue found in file - Failed to load '': Cannot cast '' to double type for option ''." warning.config.type.quaternionf: "Issue found in file - Failed to load '': Cannot cast '' to Quaternionf type for option ''." warning.config.type.vector3f: "Issue found in file - Failed to load '': Cannot cast '' to Vector3f type for option ''." +warning.config.number.missing_argument: "Issue found in file - The config '' is missing the argument for 'number'." +warning.config.number.invalid_format: "Issue found in file - The config '' is using an invalid number format ''." +warning.config.number.fixed.missing_value: "Issue found in file - The config '' is missing the required 'value' argument for 'constant' number." +warning.config.number.fixed.invalid_value: "Issue found in file - The config '' is using the invalid 'value' argument '' for 'constant' number." +warning.config.number.expression.missing_expression: "Issue found in file - The config '' is missing the required 'expression' argument for 'expression' number." +warning.config.number.uniform.missing_min: "Issue found in file - The config '' is missing the required 'min' argument for 'uniform' number." +warning.config.number.uniform.missing_max: "Issue found in file - The config '' is missing the required 'max' argument for 'uniform' number." +warning.config.condition.all_of.missing_terms: "Issue found in file - The config '' is missing the required 'terms' argument for 'all_of' condition." +warning.config.condition.all_of.invalid_terms_type: "Issue found in file - The config '' has a misconfigured 'all_of' condition, 'terms' should be a map list, current type: ''." +warning.config.condition.any_of.missing_terms: "Issue found in file - The config '' is missing the required 'terms' argument for 'any_of' condition." +warning.config.condition.any_of.invalid_terms_type: "Issue found in file - The config '' has a misconfigured 'any_of' condition, 'terms' should be a map list, current type: ''." +warning.config.condition.inverted.missing_term: "Issue found in file - The config '' is missing the required 'term' argument for 'inverted' condition." +warning.config.condition.inverted.invalid_term_type: "Issue found in file - The config '' has a misconfigured 'inverted' condition, 'term' should be a config section, current type: ''." +warning.config.condition.enchantment.missing_predicate: "Issue found in file - The config '' is missing the required 'predicate' argument for 'enchantment' condition." +warning.config.condition.enchantment.invalid_predicate: "Issue found in file - The config '' is using an invalid enchantment 'predicate' argument ''." +warning.config.condition.match_block_property.missing_properties: "Issue found in file - The config '' is missing the required 'properties' argument for 'match_block_property' condition." +warning.config.condition.match_item.missing_id: "Issue found in file - The config '' is missing the required 'id' argument for 'match_item' condition." +warning.config.condition.table_bonus.missing_enchantment: "Issue found in file - The config '' is missing the required 'enchantment' argument for 'table_bonus' condition." +warning.config.condition.table_bonus.missing_chances: "Issue found in file - The config '' is missing the required 'chances' argument for 'table_bonus' condition." warning.config.structure.not_section: "Issue found in file - The config '' is expected to be a config section while it's actually a(n) ''." warning.config.image.duplicate: "Issue found in file - Duplicated image ''. Please check if there is the same configuration in other files." warning.config.image.missing_height: "Issue found in file - The image '' is missing the required 'height' argument." @@ -228,8 +247,6 @@ warning.config.loot_table.entry.exp.missing_count: "Issue found in file warning.config.loot_table.entry.item.missing_item: "Issue found in file - '' has a misconfigured loot table, entry 'item' is missing the required 'item' argument." warning.config.loot_table.condition.missing_type: "Issue found in file - '' has a misconfigured loot table, one of the conditions is missing the required 'type' argument." warning.config.loot_table.condition.invalid_type: "Issue found in file - '' has a misconfigured loot table, one of the conditions is using an invalid condition type ''." -warning.config.loot_table.condition.table_bonus.missing_enchantment: "Issue found in file - '' has a misconfigured loot table, 'table_bonus' condition is missing the required 'enchantment' argument." -warning.config.loot_table.condition.table_bonus.missing_chances: "Issue found in file - '' has a misconfigured loot table, 'table_bonus' condition is missing the required 'chances' argument." warning.config.loot_table.number.missing_type: "Issue found in file - '' has a misconfigured loot table, one of the numbers is missing the required 'type' argument." warning.config.loot_table.number.invalid_type: "Issue found in file - '' has a misconfigured loot table, one of the numbers is using an invalid number type ''." warning.config.host.missing_type: "Issue found in config.yml at 'resource-pack.delivery.hosting' - Missing required 'type' argument for host." diff --git a/bukkit/loader/src/main/resources/translations/zh_cn.yml b/bukkit/loader/src/main/resources/translations/zh_cn.yml index dff59914a..d2dd13dd8 100644 --- a/bukkit/loader/src/main/resources/translations/zh_cn.yml +++ b/bukkit/loader/src/main/resources/translations/zh_cn.yml @@ -40,11 +40,11 @@ argument.parse.failure.aggregate.failure: "无效的组件 '': 无法从 '' 解析 " argument.parse.failure.namedtextcolor: "'' 不是颜色代码" command.reload.config.success: "重新加载配置完成. 耗时 毫秒 (异步: ms | 同步: ms)" -command.reload.config.failure: "重新加载配置失败,请检查控制台日志" +command.reload.config.failure: "重新加载配置失败, 请检查控制台日志" command.reload.pack.success: "资源包重新加载完成. 耗时 毫秒" -command.reload.pack.failure: "重新加载资源包失败,请检查控制台日志" +command.reload.pack.failure: "重新加载资源包失败, 请检查控制台日志" command.reload.all.success: "全部重新加载完成. 耗时 毫秒 (异步: ms | 同步: ms | 资源包: ms)" -command.reload.all.failure: "重新加载失败,请检查控制台日志" +command.reload.all.failure: "重新加载失败, 请检查控制台日志" command.item.get.success: "获得" command.item.get.failure.not_exist: "'>" command.item.give.success.single: "':'':''>" @@ -70,6 +70,25 @@ warning.config.type.float: "在文件 发现问题 - 无法加 warning.config.type.double: "在文件 发现问题 - 无法加载 '': 无法将 '' 转换为双精度类型 (选项 '')" warning.config.type.quaternionf: "在文件 发现问题 - 无法加载 '': 无法将 '' 转换为四元数类型 (选项 '')" warning.config.type.vector3f: "在文件 发现问题 - 无法加载 '': 无法将 '' 转换为三维向量类型 (选项 '')" +warning.config.number.missing_argument: "在文件 发现问题 - 配置项 '' 缺少数字参数" +warning.config.number.invalid_format: "在文件 发现问题 - 配置项 '' 使用了无效的数字格式 ''" +warning.config.number.fixed.missing_value: "在文件 发现问题 - 配置项 '' 缺少 'constant' 数字类型所需的 'value' 参数" +warning.config.number.fixed.invalid_value: "在文件 发现问题 - 配置项 '' 使用了无效的常量值参数 ''" +warning.config.number.expression.missing_expression: "在文件 发现问题 - 配置项 '' 缺少 'expression' 数字类型所需的 'expression' 参数" +warning.config.number.uniform.missing_min: "在文件 发现问题 - 配置项 '' 缺少 'uniform' 数字类型所需的 'min' 参数" +warning.config.number.uniform.missing_max: "在文件 发现问题 - 配置项 '' 缺少 'uniform' 数字类型所需的 'max' 参数" +warning.config.condition.all_of.missing_terms: "在文件 发现问题 - 配置项 '' 缺少 'all_of' 条件所需的 'terms' 参数" +warning.config.condition.all_of.invalid_terms_type: "在文件 发现问题 - 配置项 '' 的 'all_of' 条件配置错误, 'terms' 应为映射列表, 当前类型: ''" +warning.config.condition.any_of.missing_terms: "在文件 发现问题 - 配置项 '' 缺少 'any_of' 条件所需的 'terms' 参数" +warning.config.condition.any_of.invalid_terms_type: "在文件 发现问题 - 配置项 '' 的 'any_of' 条件配置错误, 'terms' 应为映射列表, 当前类型: ''" +warning.config.condition.inverted.missing_term: "在文件 发现问题 - 配置项 '' 缺少 'inverted' 条件所需的 'term' 参数" +warning.config.condition.inverted.invalid_term_type: "在文件 发现问题 - 配置项 '' 的 'inverted' 条件配置错误, 'term' 应为配置节点, 当前类型: ''" +warning.config.condition.enchantment.missing_predicate: "在文件 发现问题 - 配置项 '' 缺少 'enchantment' 条件所需的 'predicate' 参数" +warning.config.condition.enchantment.invalid_predicate: "在文件 发现问题 - 配置项 '' 使用了无效的附魔 'predicate' 参数 ''" +warning.config.condition.match_block_property.missing_properties: "在文件 发现问题 - 配置项 '' 缺少 'match_block_property' 条件所需的 'properties' 参数" +warning.config.condition.match_item.missing_id: "在文件 发现问题 - 配置项 '' 缺少 'match_item' 条件所需的 'id' 参数" +warning.config.condition.table_bonus.missing_enchantment: "在文件 发现问题 - 配置项 '' 缺少 'table_bonus' 条件所需的 'enchantment' 参数" +warning.config.condition.table_bonus.missing_chances: "在文件 发现问题 - 配置项 '' 缺少 'table_bonus' 条件所需的 'chances' 参数" warning.config.structure.not_section: "在文件 发现问题 - 配置项 '' 应为配置段落 但实际类型为 ''" warning.config.image.duplicate: "在文件 发现问题 - 重复的图片配置 '' 请检查其他文件中是否存在相同配置" warning.config.image.missing_height: "在文件 发现问题 - 图片 '' 缺少必需的 'height' 参数" diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java index 1209ed4b5..edb48354b 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java @@ -150,11 +150,11 @@ public class BlockEventListener implements Listener { } // drop items - ContextHolder.Builder builder = ContextHolder.builder(); - builder.withParameter(CommonParameters.WORLD, world); - builder.withParameter(CommonParameters.LOCATION, vec3d); - builder.withParameter(CommonParameters.PLAYER, serverPlayer); - builder.withOptionalParameter(CommonParameters.TOOL, itemInHand); + ContextHolder.Builder builder = ContextHolder.builder() + .withParameter(CommonParameters.WORLD, world) + .withParameter(CommonParameters.LOCATION, vec3d) + .withParameter(CommonParameters.PLAYER, serverPlayer) + .withOptionalParameter(CommonParameters.TOOL, itemInHand); for (Item item : state.getDrops(builder, world)) { world.dropItemNaturally(vec3d, item); } @@ -171,11 +171,11 @@ public class BlockEventListener implements Listener { BukkitServerPlayer serverPlayer = this.plugin.adapt(player); net.momirealms.craftengine.core.world.World world = new BukkitWorld(player.getWorld()); Vec3d vec3d = new Vec3d(location.getBlockX() + 0.5, location.getBlockY() + 0.5, location.getBlockZ() + 0.5); - ContextHolder.Builder builder = ContextHolder.builder(); - builder.withParameter(CommonParameters.WORLD, world); - builder.withParameter(CommonParameters.LOCATION, vec3d); - builder.withParameter(CommonParameters.PLAYER, serverPlayer); - builder.withOptionalParameter(CommonParameters.TOOL, serverPlayer.getItemInHand(InteractionHand.MAIN_HAND)); + ContextHolder.Builder builder = ContextHolder.builder() + .withParameter(CommonParameters.WORLD, world) + .withParameter(CommonParameters.LOCATION, vec3d) + .withParameter(CommonParameters.PLAYER, serverPlayer) + .withOptionalParameter(CommonParameters.TOOL, serverPlayer.getItemInHand(InteractionHand.MAIN_HAND)); ContextHolder contextHolder = builder.build(); for (LootTable lootTable : it.lootTables()) { for (Item item : lootTable.getRandomItems(contextHolder, world)) { @@ -213,9 +213,9 @@ public class BlockEventListener implements Listener { Location location = block.getLocation(); net.momirealms.craftengine.core.world.World world = new BukkitWorld(block.getWorld()); Vec3d vec3d = new Vec3d(location.getBlockX() + 0.5, location.getBlockY() + 0.5, location.getBlockZ() + 0.5); - ContextHolder.Builder builder = ContextHolder.builder(); - builder.withParameter(CommonParameters.WORLD, world); - builder.withParameter(CommonParameters.LOCATION, vec3d); + ContextHolder.Builder builder = ContextHolder.builder() + .withParameter(CommonParameters.WORLD, world) + .withParameter(CommonParameters.LOCATION, vec3d); for (Item item : immutableBlockState.getDrops(builder, world)) { world.dropItemNaturally(vec3d, item); } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java index 98b5dd75c..0c13c3867 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java @@ -30,7 +30,6 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.Callable; -import java.util.concurrent.ThreadLocalRandom; public class CropBlockBehavior extends BushBlockBehavior { public static final Factory FACTORY = new Factory(); @@ -148,7 +147,7 @@ public class CropBlockBehavior extends BushBlockBehavior { int z = FastNMS.INSTANCE.field$Vec3i$z(pos); net.momirealms.craftengine.core.world.World wrappedWorld = new BukkitWorld(world); - int i = this.getAge(immutableBlockState) + this.boneMealBonus.getInt(new LootContext(wrappedWorld, 1, ThreadLocalRandom.current(), ContextHolder.builder() + int i = this.getAge(immutableBlockState) + this.boneMealBonus.getInt(new LootContext(wrappedWorld, 1, ContextHolder.builder() .withParameter(CommonParameters.WORLD, wrappedWorld) .withParameter(CommonParameters.LOCATION, Vec3d.atCenterOf(new Vec3i(x, y, z))) .build())); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/RecipeEventListener.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/RecipeEventListener.java index ecf349fb6..aaa910a99 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/RecipeEventListener.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/RecipeEventListener.java @@ -752,7 +752,7 @@ public class RecipeEventListener implements Listener { return; } - Item newItem = customItem.buildItem(ItemBuildContext.of(plugin.adapt(player), ContextHolder.EMPTY)); + Item newItem = customItem.buildItem(ItemBuildContext.of(plugin.adapt(player))); int remainingDurability = totalMaxDamage - totalDamage; int newItemDamage = Math.max(0, newItem.maxDamage().get() - remainingDurability); newItem.damage(newItemDamage); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/loot/BukkitVanillaLootManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/loot/BukkitVanillaLootManager.java index db72d7a54..665e1fd2f 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/loot/BukkitVanillaLootManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/loot/BukkitVanillaLootManager.java @@ -70,7 +70,6 @@ public class BukkitVanillaLootManager extends AbstractVanillaLootManager impleme ContextHolder.Builder builder = ContextHolder.builder(); builder.withParameter(CommonParameters.WORLD, world); builder.withParameter(CommonParameters.LOCATION, vec3d); - // TODO Vanilla State Parameter if (VersionHelper.isOrAbove1_20_5()) { if (event.getDamageSource().getCausingEntity() instanceof Player player) { BukkitServerPlayer serverPlayer = this.plugin.adapt(player); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/TestCommand.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/TestCommand.java index c318bac10..116ac584a 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/TestCommand.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/TestCommand.java @@ -1,11 +1,10 @@ package net.momirealms.craftengine.bukkit.plugin.command.feature; -import com.saicone.rtag.util.ChatComponent; -import net.kyori.adventure.text.Component; import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature; -import net.momirealms.craftengine.bukkit.util.ComponentUtils; import net.momirealms.craftengine.core.plugin.CraftEngine; import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager; +import net.momirealms.craftengine.core.plugin.context.ContextHolder; +import net.momirealms.craftengine.core.plugin.context.PlayerOptionalContext; import net.momirealms.craftengine.core.util.AdventureHelper; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -24,10 +23,9 @@ public class TestCommand extends BukkitCommandFeature { .senderType(Player.class) .required("text", StringParser.greedyStringParser()) .handler(context -> { - plugin().senderFactory().wrap(context.sender()).sendMessage(Component.text( - ChatComponent.toTag(ComponentUtils.adventureToMinecraft(AdventureHelper.miniMessage().deserialize(context.get("text")))).toString() - )); - plugin().senderFactory().wrap(context.sender()).sendMessage(AdventureHelper.miniMessage().deserialize(context.get("text"))); + String text = ""; + PlayerOptionalContext context1 = PlayerOptionalContext.of(plugin().adapt(context.sender()), ContextHolder.builder()); + plugin().senderFactory().wrap(context.sender()).sendMessage(AdventureHelper.customMiniMessage().deserialize(text, context1.tagResolvers())); }); } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java index 413ec486b..c611b7049 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java @@ -199,7 +199,6 @@ public class PacketConsumers { buf.writeInt(chunkZ); if (VersionHelper.isOrAbove1_21_5()) { buf.writeVarInt(heightmapsCount); - assert heightmapsMap != null; for (Map.Entry entry : heightmapsMap.entrySet()) { buf.writeVarInt(entry.getKey()); buf.writeLongArray(entry.getValue()); diff --git a/core/src/main/java/net/momirealms/craftengine/core/font/AbstractFontManager.java b/core/src/main/java/net/momirealms/craftengine/core/font/AbstractFontManager.java index c8151079b..aa7a7797e 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/font/AbstractFontManager.java +++ b/core/src/main/java/net/momirealms/craftengine/core/font/AbstractFontManager.java @@ -124,7 +124,7 @@ public abstract class AbstractFontManager implements FontManager { PlayerOptionalContext.of(player, ContextHolder.builder() .withOptionalParameter(EmojiParameters.EMOJI, emoji.emojiImage()) .withParameter(EmojiParameters.KEYWORD, emoji.keywords().get(0)) - .build()).tagResolvers() + ).tagResolvers() ); replacements.put(fragment, AdventureHelper.componentToMiniMessage(content)); } @@ -170,8 +170,7 @@ public abstract class AbstractFontManager implements FontManager { PlayerOptionalContext.of(player, ContextHolder.builder() .withOptionalParameter(EmojiParameters.EMOJI, emoji.emojiImage()) .withParameter(EmojiParameters.KEYWORD, emoji.keywords().get(0)) - .build()) - .tagResolvers()) + ).tagResolvers()) ); if (emojis.size() >= maxTimes) break; } @@ -200,11 +199,9 @@ public abstract class AbstractFontManager implements FontManager { continue; emojis.put(fragment, AdventureHelper.miniMessage().deserialize( emoji.content(), - PlayerOptionalContext.of(player, - ContextHolder.builder() - .withOptionalParameter(EmojiParameters.EMOJI, emoji.emojiImage()) - .withParameter(EmojiParameters.KEYWORD, emoji.keywords().get(0)) - .build() + PlayerOptionalContext.of(player, ContextHolder.builder() + .withOptionalParameter(EmojiParameters.EMOJI, emoji.emojiImage()) + .withParameter(EmojiParameters.KEYWORD, emoji.keywords().get(0)) ).tagResolvers() )); if (emojis.size() >= maxTimes) break; diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/CustomItem.java b/core/src/main/java/net/momirealms/craftengine/core/item/CustomItem.java index af3b05eed..b4ff58f2a 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/CustomItem.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/CustomItem.java @@ -3,7 +3,6 @@ package net.momirealms.craftengine.core.item; import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.item.behavior.ItemBehavior; import net.momirealms.craftengine.core.item.modifier.ItemDataModifier; -import net.momirealms.craftengine.core.plugin.context.ContextHolder; import net.momirealms.craftengine.core.util.Key; import org.jetbrains.annotations.NotNull; @@ -35,7 +34,7 @@ public interface CustomItem extends BuildableItem { } default Item buildItem(Player player) { - return buildItem(ItemBuildContext.of(player, ContextHolder.EMPTY)); + return buildItem(ItemBuildContext.of(player)); } Item buildItem(ItemBuildContext context); diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/ItemBuildContext.java b/core/src/main/java/net/momirealms/craftengine/core/item/ItemBuildContext.java index 8ad1474df..521907dc3 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/ItemBuildContext.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/ItemBuildContext.java @@ -3,9 +3,12 @@ package net.momirealms.craftengine.core.item; import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.plugin.context.ContextHolder; import net.momirealms.craftengine.core.plugin.context.PlayerOptionalContext; +import net.momirealms.craftengine.core.plugin.context.parameter.CommonParameters; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Map; + public class ItemBuildContext extends PlayerOptionalContext { public static final ItemBuildContext EMPTY = new ItemBuildContext(null, ContextHolder.EMPTY); @@ -17,4 +20,16 @@ public class ItemBuildContext extends PlayerOptionalContext { public static ItemBuildContext of(@Nullable Player player, @NotNull ContextHolder contexts) { return new ItemBuildContext(player, contexts); } + + @NotNull + public static ItemBuildContext of(@Nullable Player player, @NotNull ContextHolder.Builder builder) { + if (player != null) builder.withParameter(CommonParameters.PLAYER, player); + return new ItemBuildContext(player, builder.build()); + } + + @NotNull + public static ItemBuildContext of(@Nullable Player player) { + if (player == null) return new ItemBuildContext(null, ContextHolder.EMPTY); + return new ItemBuildContext(player, new ContextHolder(Map.of(CommonParameters.PLAYER, () -> player))); + } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/loot/LootContext.java b/core/src/main/java/net/momirealms/craftengine/core/loot/LootContext.java index 5c14026b1..c67948fdc 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/loot/LootContext.java +++ b/core/src/main/java/net/momirealms/craftengine/core/loot/LootContext.java @@ -1,27 +1,20 @@ package net.momirealms.craftengine.core.loot; -import net.momirealms.craftengine.core.plugin.context.AbstractCommonContext; import net.momirealms.craftengine.core.plugin.context.ContextHolder; +import net.momirealms.craftengine.core.plugin.context.PlayerOptionalContext; +import net.momirealms.craftengine.core.plugin.context.parameter.CommonParameters; import net.momirealms.craftengine.core.world.World; -import java.util.Random; - -public class LootContext extends AbstractCommonContext { +public class LootContext extends PlayerOptionalContext { private final World world; - private final Random randomSource; private final float luck; - public LootContext(World world, float luck, Random randomSource, ContextHolder contexts) { - super(contexts); - this.randomSource = randomSource; + public LootContext(World world, float luck, ContextHolder contexts) { + super(contexts.getOptional(CommonParameters.PLAYER).orElse(null), contexts); this.world = world; this.luck = luck; } - public Random randomSource() { - return this.randomSource; - } - public float luck() { return luck; } diff --git a/core/src/main/java/net/momirealms/craftengine/core/loot/LootPool.java b/core/src/main/java/net/momirealms/craftengine/core/loot/LootPool.java index 28a012dc6..1bc846899 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/loot/LootPool.java +++ b/core/src/main/java/net/momirealms/craftengine/core/loot/LootPool.java @@ -11,9 +11,9 @@ import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.number.NumberProvider; import net.momirealms.craftengine.core.util.MCUtils; import net.momirealms.craftengine.core.util.MutableInt; +import net.momirealms.craftengine.core.util.RandomUtils; import java.util.List; -import java.util.Random; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Predicate; @@ -62,7 +62,6 @@ public class LootPool { } private void addRandomItem(Consumer> lootConsumer, LootContext context) { - Random randomSource = context.randomSource(); List> list = Lists.newArrayList(); MutableInt mutableInt = new MutableInt(0); for (LootEntryContainer lootPoolEntryContainer : this.entryContainers) { @@ -79,7 +78,7 @@ public class LootPool { if (i == 1) { list.get(0).createItem(lootConsumer, context); } else { - int j = randomSource.nextInt(mutableInt.intValue()); + int j = RandomUtils.generateRandomInt(0, mutableInt.intValue()); for (LootEntry loot : list) { j -= loot.getWeight(context.luck()); if (j < 0) { diff --git a/core/src/main/java/net/momirealms/craftengine/core/loot/LootTable.java b/core/src/main/java/net/momirealms/craftengine/core/loot/LootTable.java index b404c7456..8804ac429 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/loot/LootTable.java +++ b/core/src/main/java/net/momirealms/craftengine/core/loot/LootTable.java @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.concurrent.ThreadLocalRandom; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Supplier; @@ -80,7 +79,7 @@ public class LootTable { } public ArrayList> getRandomItems(ContextHolder parameters, World world) { - return this.getRandomItems(new LootContext(world, 1, ThreadLocalRandom.current(), parameters)); + return this.getRandomItems(new LootContext(world, 1, parameters)); } private ArrayList> getRandomItems(LootContext context) { diff --git a/core/src/main/java/net/momirealms/craftengine/core/loot/condition/LootConditions.java b/core/src/main/java/net/momirealms/craftengine/core/loot/condition/LootConditions.java index 8e98d73d2..f3ce5cd7d 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/loot/condition/LootConditions.java +++ b/core/src/main/java/net/momirealms/craftengine/core/loot/condition/LootConditions.java @@ -21,16 +21,16 @@ import java.util.function.Predicate; public class LootConditions { static { - register(SharedConditions.MATCH_ITEM, new MatchItemCondition.FactoryImpl<>()); - register(SharedConditions.MATCH_BLOCK_PROPERTY, new MatchBlockPropertyCondition.FactoryImpl<>()); - register(SharedConditions.TABLE_BONUS, new TableBonusCondition.FactoryImpl<>()); - register(SharedConditions.SURVIVES_EXPLOSION, new SurvivesExplosionCondition.FactoryImpl<>()); - register(SharedConditions.ANY_OF, new AnyOfCondition.FactoryImpl<>(LootConditions::fromMap)); - register(SharedConditions.ALL_OF, new AllOfCondition.FactoryImpl<>(LootConditions::fromMap)); - register(SharedConditions.ENCHANTMENT, new EnchantmentCondition.FactoryImpl<>()); - register(SharedConditions.INVERTED, new InvertedCondition.FactoryImpl<>(LootConditions::fromMap)); - register(SharedConditions.FALLING_BLOCK, new FallingBlockCondition.FactoryImpl<>()); - register(SharedConditions.RANDOM, new RandomCondition.FactoryImpl<>()); + register(CommonConditions.MATCH_ITEM, new MatchItemCondition.FactoryImpl<>()); + register(CommonConditions.MATCH_BLOCK_PROPERTY, new MatchBlockPropertyCondition.FactoryImpl<>()); + register(CommonConditions.TABLE_BONUS, new TableBonusCondition.FactoryImpl<>()); + register(CommonConditions.SURVIVES_EXPLOSION, new SurvivesExplosionCondition.FactoryImpl<>()); + register(CommonConditions.ANY_OF, new AnyOfCondition.FactoryImpl<>(LootConditions::fromMap)); + register(CommonConditions.ALL_OF, new AllOfCondition.FactoryImpl<>(LootConditions::fromMap)); + register(CommonConditions.ENCHANTMENT, new EnchantmentCondition.FactoryImpl<>()); + register(CommonConditions.INVERTED, new InvertedCondition.FactoryImpl<>(LootConditions::fromMap)); + register(CommonConditions.FALLING_BLOCK, new FallingBlockCondition.FactoryImpl<>()); + register(CommonConditions.RANDOM, new RandomCondition.FactoryImpl<>()); } public static void register(Key key, Factory> factory) { diff --git a/core/src/main/java/net/momirealms/craftengine/core/loot/function/LootFunctionApplyBonusCount.java b/core/src/main/java/net/momirealms/craftengine/core/loot/function/LootFunctionApplyBonusCount.java index c8105921f..988839a5b 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/loot/function/LootFunctionApplyBonusCount.java +++ b/core/src/main/java/net/momirealms/craftengine/core/loot/function/LootFunctionApplyBonusCount.java @@ -17,7 +17,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.concurrent.ThreadLocalRandom; public class LootFunctionApplyBonusCount extends AbstractLootConditionalFunction { public static final Factory FACTORY = new Factory<>(); @@ -108,7 +107,7 @@ public class LootFunctionApplyBonusCount extends AbstractLootConditionalFunct @Override public int apply(int initialCount, int enchantmentLevel) { if (enchantmentLevel > 0) { - int i = ThreadLocalRandom.current().nextInt(enchantmentLevel + 2) - 1; + int i = RandomUtils.generateRandomInt(0, enchantmentLevel + 2) - 1; if (i < 0) { i = 0; } diff --git a/core/src/main/java/net/momirealms/craftengine/core/loot/function/LootFunctionExplosionDecay.java b/core/src/main/java/net/momirealms/craftengine/core/loot/function/LootFunctionExplosionDecay.java index 7f0caa415..8e042ff08 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/loot/function/LootFunctionExplosionDecay.java +++ b/core/src/main/java/net/momirealms/craftengine/core/loot/function/LootFunctionExplosionDecay.java @@ -6,8 +6,12 @@ import net.momirealms.craftengine.core.loot.condition.LootConditions; import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.parameter.CommonParameters; import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.RandomUtils; -import java.util.*; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; public class LootFunctionExplosionDecay extends AbstractLootConditionalFunction { public static final Factory FACTORY = new Factory<>(); @@ -20,12 +24,11 @@ public class LootFunctionExplosionDecay extends AbstractLootConditionalFuncti protected Item applyInternal(Item item, LootContext context) { Optional radius = context.getOptionalParameter(CommonParameters.EXPLOSION_RADIUS); if (radius.isPresent()) { - Random random = context.randomSource(); float f = 1f / radius.get(); int amount = item.count(); int survive = 0; for (int j = 0; j < amount; j++) { - if (random.nextFloat() <= f) { + if (RandomUtils.generateRandomFloat(0, 1) <= f) { survive++; } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/pack/conflict/matcher/PathMatchers.java b/core/src/main/java/net/momirealms/craftengine/core/pack/conflict/matcher/PathMatchers.java index 90dc0a83c..a80c9bdde 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/pack/conflict/matcher/PathMatchers.java +++ b/core/src/main/java/net/momirealms/craftengine/core/pack/conflict/matcher/PathMatchers.java @@ -5,7 +5,7 @@ import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.condition.AllOfCondition; import net.momirealms.craftengine.core.plugin.context.condition.AnyOfCondition; import net.momirealms.craftengine.core.plugin.context.condition.InvertedCondition; -import net.momirealms.craftengine.core.plugin.context.condition.SharedConditions; +import net.momirealms.craftengine.core.plugin.context.condition.CommonConditions; import net.momirealms.craftengine.core.plugin.locale.LocalizedException; import net.momirealms.craftengine.core.registry.BuiltInRegistries; import net.momirealms.craftengine.core.registry.Holder; @@ -29,9 +29,9 @@ public class PathMatchers { public static final Key PATTERN = Key.of("craftengine:pattern"); static { - register(SharedConditions.ANY_OF, new AnyOfCondition.FactoryImpl<>(PathMatchers::fromMap)); - register(SharedConditions.ALL_OF, new AllOfCondition.FactoryImpl<>(PathMatchers::fromMap)); - register(SharedConditions.INVERTED, new InvertedCondition.FactoryImpl<>(PathMatchers::fromMap)); + register(CommonConditions.ANY_OF, new AnyOfCondition.FactoryImpl<>(PathMatchers::fromMap)); + register(CommonConditions.ALL_OF, new AllOfCondition.FactoryImpl<>(PathMatchers::fromMap)); + register(CommonConditions.INVERTED, new InvertedCondition.FactoryImpl<>(PathMatchers::fromMap)); register(PARENT_PATH_SUFFIX, new PathMatcherParentSuffix.FactoryImpl()); register(PARENT_PATH_PREFIX, new PathMatcherParentPrefix.FactoryImpl()); register(PATTERN, new PathPatternMatcher.FactoryImpl()); diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/AbstractAdditionalCommonContext.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/AbstractAdditionalParameterContext.java similarity index 81% rename from core/src/main/java/net/momirealms/craftengine/core/plugin/context/AbstractAdditionalCommonContext.java rename to core/src/main/java/net/momirealms/craftengine/core/plugin/context/AbstractAdditionalParameterContext.java index 0011e6247..97a3ff9b9 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/AbstractAdditionalCommonContext.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/AbstractAdditionalParameterContext.java @@ -5,15 +5,15 @@ import net.momirealms.craftengine.core.plugin.context.parameter.CommonParameterP import java.util.List; import java.util.Optional; -public abstract class AbstractAdditionalCommonContext extends AbstractCommonContext { +public abstract class AbstractAdditionalParameterContext extends AbstractCommonContext { private final List providers; - public AbstractAdditionalCommonContext(ContextHolder contexts, List providers) { + public AbstractAdditionalParameterContext(ContextHolder contexts, List providers) { super(contexts); this.providers = providers; } - public AbstractAdditionalCommonContext(ContextHolder contexts) { + public AbstractAdditionalParameterContext(ContextHolder contexts) { super(contexts); this.providers = List.of(new CommonParameterProvider()); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/AbstractCommonContext.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/AbstractCommonContext.java index 93332ab6d..9e07b9423 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/AbstractCommonContext.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/AbstractCommonContext.java @@ -1,10 +1,7 @@ package net.momirealms.craftengine.core.plugin.context; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; -import net.momirealms.craftengine.core.plugin.text.minimessage.I18NTag; -import net.momirealms.craftengine.core.plugin.text.minimessage.ImageTag; -import net.momirealms.craftengine.core.plugin.text.minimessage.NamedArgumentTag; -import net.momirealms.craftengine.core.plugin.text.minimessage.ShiftTag; +import net.momirealms.craftengine.core.plugin.text.minimessage.*; import org.jetbrains.annotations.NotNull; import java.util.Optional; @@ -26,7 +23,7 @@ public abstract class AbstractCommonContext implements Context { @NotNull public TagResolver[] tagResolvers() { if (this.tagResolvers == null) { - this.tagResolvers = new TagResolver[]{ShiftTag.INSTANCE, ImageTag.INSTANCE, new I18NTag(this), new NamedArgumentTag(this)}; + this.tagResolvers = new TagResolver[]{ShiftTag.INSTANCE, ImageTag.INSTANCE, new I18NTag(this), new NamedArgumentTag(this), new PlaceholderTag(null)}; } return this.tagResolvers; } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/ContextHolder.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/ContextHolder.java index c14652f7e..e01c447e2 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/ContextHolder.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/ContextHolder.java @@ -13,7 +13,7 @@ public class ContextHolder { public static final ContextHolder EMPTY = ContextHolder.builder().immutable(true).build(); protected final Map, Supplier> params; - protected ContextHolder(Map, Supplier> params) { + public ContextHolder(Map, Supplier> params) { this.params = params; } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/ContextKey.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/ContextKey.java index 4d5529baf..a33dffffd 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/ContextKey.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/ContextKey.java @@ -1,28 +1,22 @@ package net.momirealms.craftengine.core.plugin.context; -import net.momirealms.craftengine.core.util.Key; import org.jetbrains.annotations.NotNull; public class ContextKey { - private final Key id; + private final String id; - public ContextKey(@NotNull Key id) { + public ContextKey(@NotNull String id) { this.id = id; } @NotNull - public Key id() { + public String id() { return id; } - @NotNull - public static ContextKey of(@NotNull Key id) { - return new ContextKey<>(id); - } - @NotNull public static ContextKey of(@NotNull String id) { - return new ContextKey<>(Key.withDefaultNamespace(id, "craftengine")); + return new ContextKey<>(id); } @Override diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/PlayerOptionalContext.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/PlayerOptionalContext.java index 2071848ce..3436e4605 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/PlayerOptionalContext.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/PlayerOptionalContext.java @@ -3,14 +3,16 @@ package net.momirealms.craftengine.core.plugin.context; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.plugin.context.parameter.CommonParameterProvider; +import net.momirealms.craftengine.core.plugin.context.parameter.CommonParameters; import net.momirealms.craftengine.core.plugin.context.parameter.PlayerParameterProvider; import net.momirealms.craftengine.core.plugin.text.minimessage.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; +import java.util.Map; -public class PlayerOptionalContext extends AbstractAdditionalCommonContext implements Context { +public class PlayerOptionalContext extends AbstractAdditionalParameterContext implements Context { public static final PlayerOptionalContext EMPTY = new PlayerOptionalContext(null, ContextHolder.EMPTY); private final Player player; @@ -29,6 +31,18 @@ public class PlayerOptionalContext extends AbstractAdditionalCommonContext imple return new PlayerOptionalContext(player, contexts); } + @NotNull + public static PlayerOptionalContext of(@Nullable Player player, @NotNull ContextHolder.Builder contexts) { + if (player != null) contexts.withParameter(CommonParameters.PLAYER, player); + return new PlayerOptionalContext(player, contexts.build()); + } + + @NotNull + public static PlayerOptionalContext of(@Nullable Player player) { + if (player == null) return EMPTY; + return new PlayerOptionalContext(player, new ContextHolder(Map.of(CommonParameters.PLAYER, () -> player))); + } + @Nullable public Player player() { return this.player; diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/AllOfCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/AllOfCondition.java index eee327cc9..e1b128982 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/AllOfCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/AllOfCondition.java @@ -2,8 +2,11 @@ package net.momirealms.craftengine.core.plugin.context.condition; import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.Context; +import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException; import net.momirealms.craftengine.core.util.Factory; import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.MiscUtils; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; import java.util.ArrayList; import java.util.List; @@ -29,7 +32,7 @@ public class AllOfCondition implements Condition { @Override public Key type() { - return SharedConditions.ALL_OF; + return CommonConditions.ALL_OF; } public static class FactoryImpl implements Factory> { @@ -42,12 +45,21 @@ public class AllOfCondition implements Condition { @SuppressWarnings("unchecked") @Override public Condition create(Map arguments) { - List> terms = (List>) arguments.get("terms"); - List> conditions = new ArrayList<>(); - for (Map term : terms) { - conditions.add(factory.apply(term)); + Object termsArg = ResourceConfigUtils.requireNonNullOrThrow( + ResourceConfigUtils.get(arguments, "terms", "term"), + "warning.config.condition.all_of.missing_terms" + ); + if (termsArg instanceof Map map) { + return new AllOfCondition<>(List.of(factory.apply(MiscUtils.castToMap(map, false)))); + } else if (termsArg instanceof List list) { + List> conditions = new ArrayList<>(); + for (Map term : (List>) list) { + conditions.add(factory.apply(term)); + } + return new AllOfCondition<>(conditions); + } else { + throw new LocalizedResourceConfigException("warning.config.condition.all_of.invalid_terms_type", termsArg.getClass().getSimpleName()); } - return new AllOfCondition<>(conditions); } } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/AnyOfCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/AnyOfCondition.java index 49a279bb7..c0e20d22b 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/AnyOfCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/AnyOfCondition.java @@ -2,8 +2,11 @@ package net.momirealms.craftengine.core.plugin.context.condition; import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.Context; +import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException; import net.momirealms.craftengine.core.util.Factory; import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.MiscUtils; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; import java.util.ArrayList; import java.util.List; @@ -29,7 +32,7 @@ public class AnyOfCondition implements Condition { @Override public Key type() { - return SharedConditions.ANY_OF; + return CommonConditions.ANY_OF; } public static class FactoryImpl implements Factory> { @@ -42,12 +45,21 @@ public class AnyOfCondition implements Condition { @SuppressWarnings("unchecked") @Override public Condition create(Map arguments) { - List> terms = (List>) arguments.get("terms"); - List> conditions = new ArrayList<>(); - for (Map term : terms) { - conditions.add(factory.apply(term)); + Object termsArg = ResourceConfigUtils.requireNonNullOrThrow( + ResourceConfigUtils.get(arguments, "terms", "term"), + "warning.config.condition.any_of.missing_terms" + ); + if (termsArg instanceof Map map) { + return new AnyOfCondition<>(List.of(factory.apply(MiscUtils.castToMap(map, false)))); + } else if (termsArg instanceof List list) { + List> conditions = new ArrayList<>(); + for (Map term : (List>) list) { + conditions.add(factory.apply(term)); + } + return new AnyOfCondition<>(conditions); + } else { + throw new LocalizedResourceConfigException("warning.config.condition.any_of.invalid_terms_type", termsArg.getClass().getSimpleName()); } - return new AnyOfCondition<>(conditions); } } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/SharedConditions.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/CommonConditions.java similarity index 93% rename from core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/SharedConditions.java rename to core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/CommonConditions.java index 67fee2254..a8700127a 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/SharedConditions.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/CommonConditions.java @@ -2,8 +2,8 @@ package net.momirealms.craftengine.core.plugin.context.condition; import net.momirealms.craftengine.core.util.Key; -public final class SharedConditions { - private SharedConditions() {} +public final class CommonConditions { + private CommonConditions() {} public static final Key EMPTY = Key.of("craftengine:empty"); public static final Key ALL_OF = Key.of("craftengine:all_of"); diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/EmptyCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/EmptyCondition.java index 42bc2e520..291e0d14e 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/EmptyCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/EmptyCondition.java @@ -11,7 +11,7 @@ public class EmptyCondition implements Condition { @Override public Key type() { - return SharedConditions.EMPTY; + return CommonConditions.EMPTY; } @Override diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/EnchantmentCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/EnchantmentCondition.java index 8df501f88..57825c36a 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/EnchantmentCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/EnchantmentCondition.java @@ -5,8 +5,10 @@ import net.momirealms.craftengine.core.item.Item; import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.Context; import net.momirealms.craftengine.core.plugin.context.parameter.CommonParameters; +import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException; import net.momirealms.craftengine.core.util.Factory; import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; import java.util.Map; import java.util.Optional; @@ -23,7 +25,7 @@ public class EnchantmentCondition implements Condition @Override public Key type() { - return SharedConditions.ENCHANTMENT; + return CommonConditions.ENCHANTMENT; } @Override @@ -39,9 +41,14 @@ public class EnchantmentCondition implements Condition @Override public Condition create(Map arguments) { - String predicate = (String) arguments.get("predicate"); + String predicate = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("predicate"), "warning.config.condition.enchantment.missing_predicate"); String[] split = predicate.split("(<=|>=|<|>|==|=)", 2); - int level = Integer.parseInt(split[1]); + int level; + try { + level = Integer.parseInt(split[1]); + } catch (NumberFormatException e) { + throw new LocalizedResourceConfigException("warning.config.condition.enchantment.invalid_predicate", e, predicate); + } String operator = predicate.substring(split[0].length(), predicate.length() - split[1].length()); Function expression; switch (operator) { @@ -50,7 +57,7 @@ public class EnchantmentCondition implements Condition case "==", "=" -> expression = (i -> i == level); case "<=" -> expression = (i -> i <= level); case ">=" -> expression = (i -> i >= level); - default -> throw new IllegalArgumentException("Unknown operator: " + operator); + default -> throw new LocalizedResourceConfigException("warning.config.condition.enchantment.invalid_predicate", predicate); } return new EnchantmentCondition<>(Key.of(split[0]), expression); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/FallingBlockCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/FallingBlockCondition.java index a4ee999f4..820fc2820 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/FallingBlockCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/FallingBlockCondition.java @@ -12,7 +12,7 @@ public class FallingBlockCondition implements Condition implements Condition { @Override public Key type() { - return SharedConditions.INVERTED; + return CommonConditions.INVERTED; } public static class FactoryImpl implements Factory> { @@ -35,8 +40,21 @@ public class InvertedCondition implements Condition { @SuppressWarnings("unchecked") @Override public Condition create(Map arguments) { - Map term = (Map) arguments.get("term"); - return new InvertedCondition<>(this.factory.apply(term)); + Object termObj = ResourceConfigUtils.requireNonNullOrThrow( + ResourceConfigUtils.get(arguments, "term", "terms"), + "warning.config.condition.inverted.missing_term" + ); + if (termObj instanceof Map map) { + return new InvertedCondition<>(this.factory.apply(MiscUtils.castToMap(map, false))); + } else if (termObj instanceof List list) { + List> conditions = new ArrayList<>(); + for (Map term : (List>) list) { + conditions.add(factory.apply(term)); + } + return new InvertedCondition<>(new AllOfCondition<>(conditions)); + } else { + throw new LocalizedResourceConfigException("warning.config.condition.inverted.invalid_term_type", termObj.getClass().getSimpleName()); + } } } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchBlockPropertyCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchBlockPropertyCondition.java index e7381ae5d..e1aee710e 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchBlockPropertyCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchBlockPropertyCondition.java @@ -5,9 +5,7 @@ import net.momirealms.craftengine.core.block.properties.Property; import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.Context; import net.momirealms.craftengine.core.plugin.context.parameter.CommonParameters; -import net.momirealms.craftengine.core.util.Factory; -import net.momirealms.craftengine.core.util.Key; -import net.momirealms.craftengine.core.util.Pair; +import net.momirealms.craftengine.core.util.*; import java.util.ArrayList; import java.util.List; @@ -23,7 +21,7 @@ public class MatchBlockPropertyCondition implements Conditi @Override public Key type() { - return SharedConditions.MATCH_BLOCK_PROPERTY; + return CommonConditions.MATCH_BLOCK_PROPERTY; } @Override @@ -45,15 +43,11 @@ public class MatchBlockPropertyCondition implements Conditi public static class FactoryImpl implements Factory> { - @SuppressWarnings("unchecked") @Override public Condition create(Map arguments) { - Map properties = (Map) arguments.get("properties"); - if (properties == null) { - throw new IllegalArgumentException("Missing 'properties' argument for 'match_block_property'"); - } + Object propertyObj = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("properties"), "warning.config.condition.match_block_property.missing_properties"); List> propertyList = new ArrayList<>(); - for (Map.Entry entry : properties.entrySet()) { + for (Map.Entry entry : MiscUtils.castToMap(propertyObj, false).entrySet()) { propertyList.add(new Pair<>(entry.getKey(), entry.getValue().toString())); } return new MatchBlockPropertyCondition<>(propertyList); diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchItemCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchItemCondition.java index 11e2180b8..48a199634 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchItemCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/MatchItemCondition.java @@ -4,6 +4,7 @@ import net.momirealms.craftengine.core.item.Item; import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.Context; import net.momirealms.craftengine.core.plugin.context.parameter.CommonParameters; +import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException; import net.momirealms.craftengine.core.util.Factory; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.MiscUtils; @@ -21,7 +22,7 @@ public class MatchItemCondition implements Condition { @Override public Key type() { - return SharedConditions.MATCH_ITEM; + return CommonConditions.MATCH_ITEM; } @Override @@ -47,6 +48,9 @@ public class MatchItemCondition implements Condition { @Override public Condition create(Map arguments) { List ids = MiscUtils.getAsStringList(arguments.get("id")); + if (ids.isEmpty()) { + throw new LocalizedResourceConfigException("warning.config.condition.match_item.missing_id"); + } boolean regex = (boolean) arguments.getOrDefault("regex", false); return new MatchItemCondition<>(ids, regex); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/RandomCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/RandomCondition.java index 21fe97429..9e7657ea8 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/RandomCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/RandomCondition.java @@ -2,35 +2,36 @@ package net.momirealms.craftengine.core.plugin.context.condition; import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.Context; +import net.momirealms.craftengine.core.plugin.context.number.NumberProvider; +import net.momirealms.craftengine.core.plugin.context.number.NumberProviders; import net.momirealms.craftengine.core.util.Factory; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.RandomUtils; -import net.momirealms.craftengine.core.util.ResourceConfigUtils; import java.util.Map; public class RandomCondition implements Condition { - private final float chance; + private final NumberProvider chance; - public RandomCondition(float chance) { + public RandomCondition(NumberProvider chance) { this.chance = chance; } @Override public Key type() { - return SharedConditions.RANDOM; + return CommonConditions.RANDOM; } @Override public boolean test(CTX ctx) { - return RandomUtils.generateRandomFloat(0, 1) < this.chance; + return RandomUtils.generateRandomFloat(0, 1) < this.chance.getFloat(ctx); } public static class FactoryImpl implements Factory> { @Override public Condition create(Map arguments) { - float provider = ResourceConfigUtils.getAsFloat(arguments.getOrDefault("value", 0.5f), "value"); + NumberProvider provider = NumberProviders.fromObject(arguments.getOrDefault("value", 0.5f)); return new RandomCondition<>(provider); } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/SurvivesExplosionCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/SurvivesExplosionCondition.java index f7f97e135..da6277ded 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/SurvivesExplosionCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/SurvivesExplosionCondition.java @@ -14,7 +14,7 @@ public class SurvivesExplosionCondition implements Conditio @Override public Key type() { - return SharedConditions.SURVIVES_EXPLOSION; + return CommonConditions.SURVIVES_EXPLOSION; } @Override diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/TableBonusCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/TableBonusCondition.java index b1e44389d..880a52bcb 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/TableBonusCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/TableBonusCondition.java @@ -27,7 +27,7 @@ public class TableBonusCondition implements Condition @Override public Key type() { - return SharedConditions.TABLE_BONUS; + return CommonConditions.TABLE_BONUS; } @Override @@ -44,7 +44,7 @@ public class TableBonusCondition implements Condition public Condition create(Map arguments) { Object enchantmentObj = arguments.get("enchantment"); if (enchantmentObj == null) { - throw new LocalizedResourceConfigException("warning.config.loot_table.condition.table_bonus.missing_enchantment"); + throw new LocalizedResourceConfigException("warning.config.condition.table_bonus.missing_enchantment"); } Key enchantmentType = Key.of(enchantmentObj.toString()); Object chances = arguments.get("chances"); @@ -59,7 +59,7 @@ public class TableBonusCondition implements Condition return new TableBonusCondition<>(enchantmentType, values); } } - throw new LocalizedResourceConfigException("warning.config.loot_table.condition.table_bonus.missing_chances"); + throw new LocalizedResourceConfigException("warning.config.condition.table_bonus.missing_chances"); } } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/ExpressionNumberProvider.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/ExpressionNumberProvider.java new file mode 100644 index 000000000..21c2ccabf --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/ExpressionNumberProvider.java @@ -0,0 +1,52 @@ +package net.momirealms.craftengine.core.plugin.context.number; + +import com.ezylang.evalex.EvaluationException; +import com.ezylang.evalex.Expression; +import com.ezylang.evalex.parser.ParseException; +import net.kyori.adventure.text.Component; +import net.momirealms.craftengine.core.plugin.context.Context; +import net.momirealms.craftengine.core.util.AdventureHelper; +import net.momirealms.craftengine.core.util.Factory; +import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; + +import java.util.Map; + +public class ExpressionNumberProvider implements NumberProvider { + public static final FactoryImpl FACTORY = new FactoryImpl(); + private final String expr; + + public ExpressionNumberProvider(String expr) { + this.expr = expr; + } + + @Override + public float getFloat(Context context) { + Component resultComponent = AdventureHelper.customMiniMessage().deserialize(this.expr, context.tagResolvers()); + String resultString = AdventureHelper.plainTextContent(resultComponent); + Expression expression = new Expression(resultString); + try { + return expression.evaluate().getNumberValue().floatValue(); + } catch (EvaluationException | ParseException e) { + throw new RuntimeException("Invalid expression: " + this.expr + " -> " + resultString + " -> Cannot parse", e); + } + } + + @Override + public Key type() { + return NumberProviders.EXPRESSION; + } + + public String expression() { + return this.expr; + } + + public static class FactoryImpl implements Factory { + + @Override + public NumberProvider create(Map arguments) { + String value = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("expression"), "warning.config.number.expression.missing_expression"); + return new ExpressionNumberProvider(value); + } + } +} diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/FixedNumberProvider.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/FixedNumberProvider.java index 0e71e8332..cfbd165f9 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/FixedNumberProvider.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/FixedNumberProvider.java @@ -1,12 +1,16 @@ package net.momirealms.craftengine.core.plugin.context.number; +import com.ezylang.evalex.Expression; import net.momirealms.craftengine.core.plugin.context.Context; +import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException; +import net.momirealms.craftengine.core.util.Factory; import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; import java.util.Map; public class FixedNumberProvider implements NumberProvider { - public static final Factory FACTORY = new Factory(); + public static final FactoryImpl FACTORY = new FactoryImpl(); private final float value; public FixedNumberProvider(float value) { @@ -23,11 +27,22 @@ public class FixedNumberProvider implements NumberProvider { return NumberProviders.FIXED; } - public static class Factory implements NumberProviderFactory { + public static class FactoryImpl implements Factory { + @Override public NumberProvider create(Map arguments) { - Number value = (Number) arguments.get("value"); - return new FixedNumberProvider(value.floatValue()); + String plainOrExpression = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("value"), "warning.config.number.fixed.missing_value"); + try { + float value = Float.parseFloat(plainOrExpression); + return new FixedNumberProvider(value); + } catch (NumberFormatException e) { + Expression expression = new Expression(plainOrExpression); + try { + return new FixedNumberProvider(expression.evaluate().getNumberValue().floatValue()); + } catch (Exception e1) { + throw new LocalizedResourceConfigException("warning.config.number.fixed.invalid_value", e1, plainOrExpression); + } + } } } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/NumberProviderFactory.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/NumberProviderFactory.java deleted file mode 100644 index 0093dfcde..000000000 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/NumberProviderFactory.java +++ /dev/null @@ -1,8 +0,0 @@ -package net.momirealms.craftengine.core.plugin.context.number; - -import java.util.Map; - -public interface NumberProviderFactory { - - NumberProvider create(Map arguments); -} diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/NumberProviders.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/NumberProviders.java index 23552f78c..2d73ce359 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/NumberProviders.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/NumberProviders.java @@ -5,26 +5,30 @@ import net.momirealms.craftengine.core.registry.BuiltInRegistries; import net.momirealms.craftengine.core.registry.Holder; import net.momirealms.craftengine.core.registry.Registries; import net.momirealms.craftengine.core.registry.WritableRegistry; +import net.momirealms.craftengine.core.util.Factory; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.ResourceConfigUtils; import net.momirealms.craftengine.core.util.ResourceKey; -import software.amazon.awssdk.services.s3.endpoints.internal.Value; import java.util.ArrayList; import java.util.List; import java.util.Map; public class NumberProviders { - public static final Key FIXED = Key.of("craftengine:constant"); + public static final Key FIXED = Key.of("craftengine:fixed"); + public static final Key CONSTANT = Key.of("craftengine:constant"); public static final Key UNIFORM = Key.of("craftengine:uniform"); + public static final Key EXPRESSION = Key.of("craftengine:expression"); static { register(FIXED, FixedNumberProvider.FACTORY); + register(CONSTANT, FixedNumberProvider.FACTORY); register(UNIFORM, UniformNumberProvider.FACTORY); + register(EXPRESSION, ExpressionNumberProvider.FACTORY); } - public static void register(Key key, NumberProviderFactory factory) { - Holder.Reference holder = ((WritableRegistry) BuiltInRegistries.NUMBER_PROVIDER_FACTORY) + public static void register(Key key, Factory factory) { + Holder.Reference> holder = ((WritableRegistry>) BuiltInRegistries.NUMBER_PROVIDER_FACTORY) .registerForHolder(new ResourceKey<>(Registries.NUMBER_PROVIDER_FACTORY.location(), key)); holder.bindValue(factory); } @@ -41,7 +45,7 @@ public class NumberProviders { public static NumberProvider fromMap(Map map) { String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.loot_table.number.missing_type"); Key key = Key.withDefaultNamespace(type, Key.DEFAULT_NAMESPACE); - NumberProviderFactory factory = BuiltInRegistries.NUMBER_PROVIDER_FACTORY.getValue(key); + Factory factory = BuiltInRegistries.NUMBER_PROVIDER_FACTORY.getValue(key); if (factory == null) { throw new LocalizedResourceConfigException("warning.config.loot_table.number.invalid_type", type); } @@ -51,31 +55,33 @@ public class NumberProviders { @SuppressWarnings("unchecked") public static NumberProvider fromObject(Object object) { if (object == null) { - throw new NullPointerException("number argument is null"); + throw new LocalizedResourceConfigException("warning.config.number.missing_argument"); } if (object instanceof Number number) { return new FixedNumberProvider(number.floatValue()); - } else if (object instanceof String string) { + } else if (object instanceof Map map) { + return fromMap((Map) map); + } else { + String string = object.toString(); if (string.contains("~")) { int first = string.indexOf('~'); int second = string.indexOf('~', first + 1); if (second == -1) { - try { - float min = Float.parseFloat(string.substring(0, first)); - float max = Float.parseFloat(string.substring(first + 1)); - return new UniformNumberProvider(min, max); - } catch (NumberFormatException e) { - throw e; - } + NumberProvider min = fromObject(string.substring(0, first)); + NumberProvider max = fromObject(string.substring(first + 1)); + return new UniformNumberProvider(min, max); } else { - throw new IllegalArgumentException("Illegal number format: " + string); + throw new LocalizedResourceConfigException("warning.config.number.invalid_format", string); } + } else if (string.contains("<") && string.contains(">") && string.contains(":")) { + return new ExpressionNumberProvider(string); } else { - return new FixedNumberProvider(Float.parseFloat(string)); + try { + return new FixedNumberProvider(Float.parseFloat(string)); + } catch (NumberFormatException e) { + throw new LocalizedResourceConfigException("warning.config.number.invalid_format", e, string); + } } - } else if (object instanceof Map map) { - return fromMap((Map) map); } - throw new IllegalArgumentException("Can't convert " + object + " to " + NumberProvider.class.getSimpleName()); } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/UniformNumberProvider.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/UniformNumberProvider.java index d90880eb9..38cbf80ae 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/UniformNumberProvider.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/number/UniformNumberProvider.java @@ -1,13 +1,15 @@ package net.momirealms.craftengine.core.plugin.context.number; import net.momirealms.craftengine.core.plugin.context.Context; +import net.momirealms.craftengine.core.util.Factory; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.RandomUtils; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; import java.util.Map; public class UniformNumberProvider implements NumberProvider { - public static final Factory FACTORY = new Factory(); + public static final FactoryImpl FACTORY = new FactoryImpl(); private final NumberProvider min; private final NumberProvider max; @@ -16,14 +18,17 @@ public class UniformNumberProvider implements NumberProvider { this.max = max; } - public UniformNumberProvider(float min, float max) { - this.min = new FixedNumberProvider(min); - this.max = new FixedNumberProvider(max); + public NumberProvider max() { + return max; + } + + public NumberProvider min() { + return min; } @Override public int getInt(Context context) { - return RandomUtils.generateRandomInt(this.min.getInt(context), this.max.getInt(context)); + return RandomUtils.generateRandomInt(this.min.getInt(context), this.max.getInt(context) + 1); } @Override @@ -36,11 +41,12 @@ public class UniformNumberProvider implements NumberProvider { return NumberProviders.UNIFORM; } - public static class Factory implements NumberProviderFactory { + public static class FactoryImpl implements Factory { + @Override public NumberProvider create(Map arguments) { - Object min = arguments.getOrDefault("min", 1); - Object max = arguments.getOrDefault("max", 1); + Object min = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("min"), "warning.config.number.uniform.missing_min"); + Object max = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("max"), "warning.config.number.uniform.missing_max"); return new UniformNumberProvider(NumberProviders.fromObject(min), NumberProviders.fromObject(max)); } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/CommonParameters.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/CommonParameters.java index d9e59c0c7..e6c8d24ff 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/CommonParameters.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/CommonParameters.java @@ -1,7 +1,6 @@ package net.momirealms.craftengine.core.plugin.context.parameter; import net.momirealms.craftengine.core.block.ImmutableBlockState; -import net.momirealms.craftengine.core.entity.Entity; import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.item.Item; import net.momirealms.craftengine.core.plugin.context.ContextKey; @@ -13,10 +12,8 @@ public final class CommonParameters { public static final ContextKey RANDOM = ContextKey.of("random"); public static final ContextKey LAST_RANDOM = ContextKey.of("last_random"); - public static final ContextKey LOCATION = ContextKey.of("location"); public static final ContextKey WORLD = ContextKey.of("world"); - public static final ContextKey ENTITY = ContextKey.of("entity"); public static final ContextKey FALLING_BLOCK = ContextKey.of("falling_block"); public static final ContextKey EXPLOSION_RADIUS = ContextKey.of("explosion_radius"); public static final ContextKey PLAYER = ContextKey.of("player"); diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/gui/category/ItemBrowserManagerImpl.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/gui/category/ItemBrowserManagerImpl.java index b4669e24b..9d6f17d16 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/gui/category/ItemBrowserManagerImpl.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/gui/category/ItemBrowserManagerImpl.java @@ -137,7 +137,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(element.gui().currentPage())) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(element.gui().maxPages())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + next)); }, true) ) @@ -147,7 +147,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(element.gui().currentPage())) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(element.gui().maxPages())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + previous)); }, false) ); @@ -177,7 +177,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } }) .build() - .title(AdventureHelper.miniMessage().deserialize(Constants.BROWSER_TITLE, PlayerOptionalContext.of(player, ContextHolder.EMPTY).tagResolvers())) + .title(AdventureHelper.miniMessage().deserialize(Constants.BROWSER_TITLE, PlayerOptionalContext.of(player).tagResolvers())) .refresh() .open(player); } @@ -193,7 +193,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { ) .addIngredient('A', Ingredient.paged()) .addIngredient('=', GuiElement.constant(this.plugin.itemManager().getCustomItem(parentGui != null ? Constants.CATEGORY_BACK : Constants.CATEGORY_EXIT) - .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.EMPTY))) + .map(it -> it.buildItem(ItemBuildContext.of(player))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + (parentGui != null ? Constants.CATEGORY_BACK : Constants.CATEGORY_EXIT))), ((element, click) -> { click.cancel(); @@ -211,7 +211,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(element.gui().currentPage())) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(element.gui().maxPages())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + next)); }, true) ) @@ -221,7 +221,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(element.gui().currentPage())) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(element.gui().maxPages())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + previous)); }, false) ); @@ -292,7 +292,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } }) .build() - .title(AdventureHelper.miniMessage().deserialize(Constants.CATEGORY_TITLE, PlayerOptionalContext.of(player, ContextHolder.EMPTY).tagResolvers())) + .title(AdventureHelper.miniMessage().deserialize(Constants.CATEGORY_TITLE, PlayerOptionalContext.of(player).tagResolvers())) .refresh() .open(player); } @@ -332,7 +332,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } }) : GuiElement.EMPTY) .addIngredient('=', GuiElement.constant(this.plugin.itemManager().getCustomItem(parentGui != null ? Constants.RECIPE_BACK : Constants.RECIPE_EXIT) - .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.EMPTY))) + .map(it -> it.buildItem(ItemBuildContext.of(player))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + (parentGui != null ? Constants.RECIPE_BACK : Constants.RECIPE_EXIT))), ((element, click) -> { click.cancel(); @@ -353,7 +353,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } }) .build() - .title(AdventureHelper.miniMessage().deserialize(Constants.RECIPE_NONE_TITLE, PlayerOptionalContext.of(player, ContextHolder.EMPTY).tagResolvers())) + .title(AdventureHelper.miniMessage().deserialize(Constants.RECIPE_NONE_TITLE, PlayerOptionalContext.of(player).tagResolvers())) .refresh() .open(player); } @@ -431,7 +431,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } }) : GuiElement.EMPTY) .addIngredient('=', GuiElement.constant(this.plugin.itemManager().getCustomItem(parentGui != null ? Constants.RECIPE_BACK : Constants.RECIPE_EXIT) - .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.EMPTY))) + .map(it -> it.buildItem(ItemBuildContext.of(player))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + (parentGui != null ? Constants.RECIPE_BACK : Constants.RECIPE_EXIT))), ((element, click) -> { click.cancel(); @@ -448,7 +448,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(index + 1)) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(recipes.size())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + next)), (e, c) -> { c.cancel(); if (index + 1 < recipes.size()) { @@ -461,7 +461,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(index + 1)) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(recipes.size())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + previous)), (e, c) -> { c.cancel(); if (index > 0) { @@ -578,7 +578,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } }) .build() - .title(AdventureHelper.miniMessage().deserialize(Constants.RECIPE_SMITHING_TRANSFORM_TITLE, PlayerOptionalContext.of(player, ContextHolder.EMPTY).tagResolvers())) + .title(AdventureHelper.miniMessage().deserialize(Constants.RECIPE_SMITHING_TRANSFORM_TITLE, PlayerOptionalContext.of(player).tagResolvers())) .refresh() .open(player); } @@ -664,7 +664,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } })) .addIngredient('=', GuiElement.constant(this.plugin.itemManager().getCustomItem(parentGui != null ? Constants.RECIPE_BACK : Constants.RECIPE_EXIT) - .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.EMPTY))) + .map(it -> it.buildItem(ItemBuildContext.of(player))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + (parentGui != null ? Constants.RECIPE_BACK : Constants.RECIPE_EXIT))), ((element, click) -> { click.cancel(); @@ -681,7 +681,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(index + 1)) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(recipes.size())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + next)), (e, c) -> { c.cancel(); if (index + 1 < recipes.size()) { @@ -694,7 +694,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(index + 1)) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(recipes.size())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + previous)), (e, c) -> { c.cancel(); if (index > 0) { @@ -711,7 +711,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } }) .build() - .title(AdventureHelper.miniMessage().deserialize(Constants.RECIPE_STONECUTTING_TITLE, PlayerOptionalContext.of(player, ContextHolder.EMPTY).tagResolvers())) + .title(AdventureHelper.miniMessage().deserialize(Constants.RECIPE_STONECUTTING_TITLE, PlayerOptionalContext.of(player).tagResolvers())) .refresh() .open(player); } @@ -764,7 +764,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.COOKING_TIME, String.valueOf(recipe.cookingTime())) .withParameter(GuiParameters.COOKING_EXPERIENCE, String.valueOf(recipe.experience())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + Constants.RECIPE_COOKING_INFO)), (e, c) -> c.cancel())) .addIngredient('^', player.hasPermission(GET_ITEM_PERMISSION) ? GuiElement.constant(this.plugin.itemManager().createWrappedItem(Constants.RECIPE_GET_ITEM, player), (e, c) -> { c.cancel(); @@ -803,7 +803,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } })) .addIngredient('=', GuiElement.constant(this.plugin.itemManager().getCustomItem(parentGui != null ? Constants.RECIPE_BACK : Constants.RECIPE_EXIT) - .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.EMPTY))) + .map(it -> it.buildItem(ItemBuildContext.of(player))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + (parentGui != null ? Constants.RECIPE_BACK : Constants.RECIPE_EXIT))), ((element, click) -> { click.cancel(); @@ -820,7 +820,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(index + 1)) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(recipes.size())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + next)), (e, c) -> { c.cancel(); if (index + 1 < recipes.size()) { @@ -833,7 +833,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(index + 1)) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(recipes.size())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + previous)), (e, c) -> { c.cancel(); if (index > 0) { @@ -861,7 +861,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } }) .build() - .title(AdventureHelper.miniMessage().deserialize(title, PlayerOptionalContext.of(player, ContextHolder.EMPTY).tagResolvers())) + .title(AdventureHelper.miniMessage().deserialize(title, PlayerOptionalContext.of(player).tagResolvers())) .refresh() .open(player); } @@ -916,7 +916,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } }) : GuiElement.EMPTY) .addIngredient('=', GuiElement.constant(this.plugin.itemManager().getCustomItem(parentGui != null ? Constants.RECIPE_BACK : Constants.RECIPE_EXIT) - .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.EMPTY))) + .map(it -> it.buildItem(ItemBuildContext.of(player))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + (parentGui != null ? Constants.RECIPE_BACK : Constants.RECIPE_EXIT))), ((element, click) -> { click.cancel(); @@ -933,7 +933,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(index + 1)) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(recipes.size())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + next)), (e, c) -> { c.cancel(); if (index + 1 < recipes.size()) { @@ -946,7 +946,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { .map(it -> it.buildItem(ItemBuildContext.of(player, ContextHolder.builder() .withParameter(GuiParameters.CURRENT_PAGE, String.valueOf(index + 1)) .withParameter(GuiParameters.MAX_PAGE, String.valueOf(recipes.size())) - .build()))) + ))) .orElseThrow(() -> new GuiElementMissingException("Can't find gui element " + previous)), (e, c) -> { c.cancel(); if (index > 0) { @@ -1056,7 +1056,7 @@ public class ItemBrowserManagerImpl implements ItemBrowserManager { } }) .build() - .title(AdventureHelper.miniMessage().deserialize(Constants.RECIPE_CRAFTING_TITLE, PlayerOptionalContext.of(player, ContextHolder.EMPTY).tagResolvers())) + .title(AdventureHelper.miniMessage().deserialize(Constants.RECIPE_CRAFTING_TITLE, PlayerOptionalContext.of(player).tagResolvers())) .refresh() .open(player); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/text/minimessage/NamedArgumentTag.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/text/minimessage/NamedArgumentTag.java index c59067f59..c2db728c5 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/text/minimessage/NamedArgumentTag.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/text/minimessage/NamedArgumentTag.java @@ -7,7 +7,6 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.momirealms.craftengine.core.plugin.context.Context; import net.momirealms.craftengine.core.plugin.context.ContextKey; import net.momirealms.craftengine.core.util.AdventureHelper; -import net.momirealms.craftengine.core.util.Key; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -29,13 +28,13 @@ public class NamedArgumentTag implements TagResolver { if (!has(name)) { return null; } - String argumentKey = arguments.popOr("No argument key provided").toString(); - ContextKey key = ContextKey.of(Key.withDefaultNamespace(argumentKey, Key.DEFAULT_NAMESPACE)); + ContextKey key = ContextKey.of(arguments.popOr("No argument key provided").toString()); Optional optional = this.context.getOptionalParameter(key); - if (optional.isEmpty()) { - throw ctx.newException("Invalid argument key", arguments); + Object value = optional.orElse(null); + if (value == null) { + value = arguments.popOr("No default value provided").toString(); } - return Tag.selfClosingInserting(AdventureHelper.miniMessage().deserialize(String.valueOf(optional.get()), this.context.tagResolvers())); + return Tag.selfClosingInserting(AdventureHelper.miniMessage().deserialize(String.valueOf(value), this.context.tagResolvers())); } @Override diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/text/minimessage/PlaceholderTag.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/text/minimessage/PlaceholderTag.java index 59b331975..07ac1d36d 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/text/minimessage/PlaceholderTag.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/text/minimessage/PlaceholderTag.java @@ -23,8 +23,11 @@ public class PlaceholderTag implements TagResolver { if (!this.has(name) || !CraftEngine.instance().compatibilityManager().hasPlaceholderAPI()) { return null; } - String placeholder = arguments.popOr("No argument placeholder provided").toString(); - String parsed = CraftEngine.instance().compatibilityManager().parse(player, "%" + placeholder + "%"); + String placeholder = "%" + arguments.popOr("No argument placeholder provided") + "%"; + String parsed = CraftEngine.instance().compatibilityManager().parse(player, placeholder); + if (parsed.equals(placeholder)) { + parsed = arguments.popOr("No default papi value provided").toString(); + } return Tag.inserting(AdventureHelper.miniMessage().deserialize(parsed)); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/registry/BuiltInRegistries.java b/core/src/main/java/net/momirealms/craftengine/core/registry/BuiltInRegistries.java index 7a1a2a22a..097387d80 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/registry/BuiltInRegistries.java +++ b/core/src/main/java/net/momirealms/craftengine/core/registry/BuiltInRegistries.java @@ -22,7 +22,7 @@ import net.momirealms.craftengine.core.pack.model.special.SpecialModelFactory; import net.momirealms.craftengine.core.pack.model.tint.TintFactory; import net.momirealms.craftengine.core.plugin.config.template.TemplateArgumentFactory; import net.momirealms.craftengine.core.plugin.context.Condition; -import net.momirealms.craftengine.core.plugin.context.number.NumberProviderFactory; +import net.momirealms.craftengine.core.plugin.context.number.NumberProvider; import net.momirealms.craftengine.core.util.Factory; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.ResourceKey; @@ -35,7 +35,7 @@ public class BuiltInRegistries { public static final Registry> LOOT_FUNCTION_FACTORY = createRegistry(Registries.LOOT_FUNCTION_FACTORY); public static final Registry>> LOOT_CONDITION_FACTORY = createRegistry(Registries.LOOT_CONDITION_FACTORY); public static final Registry> LOOT_ENTRY_CONTAINER_FACTORY = createRegistry(Registries.LOOT_ENTRY_CONTAINER_FACTORY); - public static final Registry NUMBER_PROVIDER_FACTORY = createRegistry(Registries.NUMBER_PROVIDER_FACTORY); + public static final Registry> NUMBER_PROVIDER_FACTORY = createRegistry(Registries.NUMBER_PROVIDER_FACTORY); public static final Registry ITEM_BEHAVIOR_FACTORY = createRegistry(Registries.ITEM_BEHAVIOR_FACTORY); public static final Registry TEMPLATE_ARGUMENT_FACTORY = createRegistry(Registries.TEMPLATE_ARGUMENT_FACTORY); public static final Registry ITEM_MODEL_FACTORY = createRegistry(Registries.ITEM_MODEL_FACTORY); diff --git a/core/src/main/java/net/momirealms/craftengine/core/registry/Registries.java b/core/src/main/java/net/momirealms/craftengine/core/registry/Registries.java index 3bad798af..b67a7b1ad 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/registry/Registries.java +++ b/core/src/main/java/net/momirealms/craftengine/core/registry/Registries.java @@ -22,7 +22,7 @@ import net.momirealms.craftengine.core.pack.model.special.SpecialModelFactory; import net.momirealms.craftengine.core.pack.model.tint.TintFactory; import net.momirealms.craftengine.core.plugin.config.template.TemplateArgumentFactory; import net.momirealms.craftengine.core.plugin.context.Condition; -import net.momirealms.craftengine.core.plugin.context.number.NumberProviderFactory; +import net.momirealms.craftengine.core.plugin.context.number.NumberProvider; import net.momirealms.craftengine.core.util.Factory; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.ResourceKey; @@ -37,7 +37,7 @@ public class Registries { public static final ResourceKey>> LOOT_FUNCTION_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("loot_function_factory")); public static final ResourceKey>> LOOT_ENTRY_CONTAINER_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("loot_entry_container_factory")); public static final ResourceKey>>> LOOT_CONDITION_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("loot_condition_factory")); - public static final ResourceKey> NUMBER_PROVIDER_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("number_provider_factory")); + public static final ResourceKey>> NUMBER_PROVIDER_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("number_provider_factory")); public static final ResourceKey> TEMPLATE_ARGUMENT_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("template_argument_factory")); public static final ResourceKey> ITEM_MODEL_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("item_model_factory")); public static final ResourceKey> TINT_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("tint_factory")); diff --git a/core/src/main/java/net/momirealms/craftengine/core/util/RandomUtils.java b/core/src/main/java/net/momirealms/craftengine/core/util/RandomUtils.java index 34c0b03e7..b659d4c28 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/util/RandomUtils.java +++ b/core/src/main/java/net/momirealms/craftengine/core/util/RandomUtils.java @@ -1,37 +1,25 @@ package net.momirealms.craftengine.core.util; -import java.util.Random; import java.util.concurrent.ThreadLocalRandom; -public class RandomUtils { - private final Random random; +public final class RandomUtils { - private RandomUtils() { - random = ThreadLocalRandom.current(); - } - - private static class SingletonHolder { - private static final RandomUtils INSTANCE = new RandomUtils(); - } - - private static RandomUtils getInstance() { - return SingletonHolder.INSTANCE; - } + private RandomUtils() {} public static double generateRandomDouble(double min, double max) { - return min + (max - min) * getInstance().random.nextDouble(); + return min + (max - min) * ThreadLocalRandom.current().nextDouble(); } public static float generateRandomFloat(float min, float max) { - return min + (max - min) * getInstance().random.nextFloat(); + return min + (max - min) * ThreadLocalRandom.current().nextFloat(); } public static int generateRandomInt(int min, int max) { - return min >= max ? min : getInstance().random.nextInt(max - min + 1) + min; + return min >= max ? min : ThreadLocalRandom.current().nextInt(max - min) + min; } public static boolean generateRandomBoolean() { - return getInstance().random.nextBoolean(); + return ThreadLocalRandom.current().nextBoolean(); } public static double triangle(double mode, double deviation) { @@ -39,7 +27,7 @@ public class RandomUtils { } public static T getRandomElementFromArray(T[] array) { - int index = getInstance().random.nextInt(array.length); + int index = ThreadLocalRandom.current().nextInt(array.length); return array[index]; } @@ -50,7 +38,7 @@ public class RandomUtils { @SuppressWarnings("unchecked") T[] result = (T[]) new Object[count]; for (int i = 0; i < count; i++) { - int index = getInstance().random.nextInt(array.length - i); + int index = ThreadLocalRandom.current().nextInt(array.length - i); result[i] = array[index]; array[index] = array[array.length - i - 1]; } diff --git a/core/src/main/java/net/momirealms/craftengine/core/world/chunk/CEChunk.java b/core/src/main/java/net/momirealms/craftengine/core/world/chunk/CEChunk.java index 1ae57e38b..45b0a52b8 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/world/chunk/CEChunk.java +++ b/core/src/main/java/net/momirealms/craftengine/core/world/chunk/CEChunk.java @@ -47,11 +47,11 @@ public class CEChunk { } public Map blockEntities() { - return blockEntities; + return this.blockEntities; } public boolean dirty() { - return dirty; + return this.dirty; } public void setDirty(boolean dirty) {