Skip to content

Commit 2b2264e

Browse files
authored
Merge pull request #177 from pylonmc/core/rewrite-translation
2 parents e83dff8 + c7f7206 commit 2b2264e

34 files changed

Lines changed: 376 additions & 366 deletions

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/PylonCore.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,14 @@ import io.github.pylonmc.pylon.core.block.base.PylonSimpleMultiblock
99
import io.github.pylonmc.pylon.core.block.waila.Waila
1010
import io.github.pylonmc.pylon.core.command.PylonCommand
1111
import io.github.pylonmc.pylon.core.content.debug.DebugWaxedWeatheredCutCopperStairs
12-
import io.github.pylonmc.pylon.core.content.fluid.FluidPipeConnector
13-
import io.github.pylonmc.pylon.core.content.fluid.FluidPipeDisplay
14-
import io.github.pylonmc.pylon.core.content.fluid.FluidPipeMarker
15-
import io.github.pylonmc.pylon.core.content.fluid.FluidPointDisplay
16-
import io.github.pylonmc.pylon.core.content.fluid.FluidPointInteraction
12+
import io.github.pylonmc.pylon.core.content.fluid.*
13+
import io.github.pylonmc.pylon.core.content.guide.PylonGuide
1714
import io.github.pylonmc.pylon.core.entity.EntityListener
1815
import io.github.pylonmc.pylon.core.entity.EntityStorage
1916
import io.github.pylonmc.pylon.core.entity.PylonEntity
2017
import io.github.pylonmc.pylon.core.fluid.connecting.ConnectingService
21-
import io.github.pylonmc.pylon.core.content.guide.PylonGuide
22-
import io.github.pylonmc.pylon.core.i18n.AddonTranslator
2318
import io.github.pylonmc.pylon.core.i18n.MinecraftTranslator
19+
import io.github.pylonmc.pylon.core.i18n.PylonTranslator
2420
import io.github.pylonmc.pylon.core.item.PylonItem
2521
import io.github.pylonmc.pylon.core.item.PylonItemListener
2622
import io.github.pylonmc.pylon.core.item.research.Research
@@ -47,6 +43,7 @@ object PylonCore : JavaPlugin(), PylonAddon {
4743

4844
saveDefaultConfig()
4945

46+
Bukkit.getPluginManager().registerEvents(PylonTranslator, this)
5047
Bukkit.getPluginManager().registerEvents(PylonAddon, this)
5148
registerWithPylon()
5249

@@ -60,7 +57,6 @@ object PylonCore : JavaPlugin(), PylonAddon {
6057
Bukkit.getPluginManager().registerEvents(TickManager, this)
6158
Bukkit.getPluginManager().registerEvents(MultiblockCache, this)
6259
Bukkit.getPluginManager().registerEvents(EntityStorage, this)
63-
Bukkit.getPluginManager().registerEvents(AddonTranslator, this)
6460
Bukkit.getPluginManager().registerEvents(EntityListener, this)
6561
Bukkit.getPluginManager().registerEvents(Waila, this)
6662
Bukkit.getPluginManager().registerEvents(Research, this)

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/addon/PylonAddon.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import io.github.pylonmc.pylon.core.block.BlockStorage
55
import io.github.pylonmc.pylon.core.config.Config
66
import io.github.pylonmc.pylon.core.config.ConfigSection
77
import io.github.pylonmc.pylon.core.entity.EntityStorage
8-
import io.github.pylonmc.pylon.core.i18n.AddonTranslator
8+
import io.github.pylonmc.pylon.core.i18n.PylonTranslator.Companion.translator
99
import io.github.pylonmc.pylon.core.registry.PylonRegistry
1010
import net.kyori.adventure.text.Component
1111
import net.kyori.adventure.text.TranslatableComponent
@@ -66,10 +66,7 @@ interface PylonAddon : Keyed {
6666
*/
6767
fun registerWithPylon() {
6868
PylonRegistry.ADDONS.register(this)
69-
AddonTranslator.register(this)
70-
7169
if (!suppressAddonNameWarning) {
72-
val translator = AddonTranslator.translators[this]!!
7370
for (locale in languages) {
7471
if (!translator.canTranslate("pylon.${key.namespace}.addon", locale)) {
7572
PylonCore.logger.warning("${key.namespace} is missing the 'addon' translation key for ${locale.displayName}")
@@ -125,7 +122,6 @@ interface PylonAddon : Keyed {
125122
PylonRegistry.RECIPE_TYPES.unregisterAllFromAddon(plugin)
126123
PylonRegistry.MOB_DROPS.unregisterAllFromAddon(plugin)
127124
PylonRegistry.ADDONS.unregister(plugin)
128-
AddonTranslator.unregister(plugin)
129125
}
130126
}
131127
}

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/block/PhantomBlock.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import io.github.pylonmc.pylon.core.block.context.BlockCreateContext
44
import io.github.pylonmc.pylon.core.block.context.BlockItemContext
55
import io.github.pylonmc.pylon.core.block.waila.WailaConfig
66
import io.github.pylonmc.pylon.core.datatypes.PylonSerializers
7+
import io.github.pylonmc.pylon.core.i18n.PylonArgument
78
import io.github.pylonmc.pylon.core.item.PylonItem
89
import io.github.pylonmc.pylon.core.item.builder.ItemStackBuilder
910
import io.github.pylonmc.pylon.core.util.pylonKey
1011
import net.kyori.adventure.bossbar.BossBar
11-
import net.kyori.adventure.text.Component
1212
import org.bukkit.Material
1313
import org.bukkit.NamespacedKey
1414
import org.bukkit.block.Block
@@ -50,7 +50,7 @@ class PhantomBlock(
5050
override fun getWaila(player: Player): WailaConfig {
5151
return WailaConfig(
5252
text = name,
53-
placeholders = mapOf("block" to Component.text(erroredBlockKey.toString())),
53+
placeholders = listOf(PylonArgument.of("block", erroredBlockKey.toString())),
5454
color = BossBar.Color.RED
5555
)
5656
}
@@ -74,10 +74,10 @@ class PhantomBlock(
7474
stack.editPersistentDataContainer { pdc -> pdc.set(ErrorItem.BLOCK_KEY, PylonSerializers.NAMESPACED_KEY, erroredBlock) }
7575
}
7676

77-
override fun getPlaceholders(): Map<String, Component> {
77+
override fun getPlaceholders(): List<PylonArgument> {
7878
val block = stack.persistentDataContainer.get(BLOCK_KEY, PylonSerializers.NAMESPACED_KEY)
79-
?: return emptyMap()
80-
return mapOf("block" to Component.text(block.toString()))
79+
?: return emptyList()
80+
return listOf(PylonArgument.of("block", block.toString()))
8181
}
8282

8383
companion object {

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/block/waila/WailaConfig.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package io.github.pylonmc.pylon.core.block.waila
22

3-
import io.github.pylonmc.pylon.core.i18n.PlaceholderAttacher
3+
import io.github.pylonmc.pylon.core.i18n.PylonArgument
4+
import io.github.pylonmc.pylon.core.i18n.PylonArgument.Companion.attachPylonArguments
45
import net.kyori.adventure.bossbar.BossBar
56
import net.kyori.adventure.text.Component
6-
import net.kyori.adventure.text.ComponentLike
77
import org.bukkit.entity.Player
88

99
/**
1010
* The configuration for a WAILA bar, returned by a block or entity
1111
*/
1212
data class WailaConfig @JvmOverloads constructor(
1313
val text: Component,
14-
val placeholders: Map<String, ComponentLike> = emptyMap(),
14+
val placeholders: List<PylonArgument> = emptyList(),
1515
val color: BossBar.Color = BossBar.Color.WHITE,
1616
val style: BossBar.Overlay = BossBar.Overlay.PROGRESS,
1717
val progress: Float = 1F
@@ -21,8 +21,7 @@ data class WailaConfig @JvmOverloads constructor(
2121
internal fun apply(bar: BossBar) {
2222
val player = bar.viewers().singleOrNull() as? Player
2323
if (player != null) {
24-
val attacher = PlaceholderAttacher(placeholders)
25-
bar.name(attacher.render(text, Unit))
24+
bar.name(text.attachPylonArguments(placeholders))
2625
} else {
2726
bar.name(text)
2827
}

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/content/debug/DebugWaxedWeatheredCutCopperStairs.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import io.github.pylonmc.pylon.core.util.pylonKey
1515
import io.papermc.paper.datacomponent.DataComponentTypes
1616
import net.kyori.adventure.audience.Audience
1717
import net.kyori.adventure.text.Component
18-
import net.kyori.adventure.text.TranslationArgument
1918
import org.bukkit.Material
2019
import org.bukkit.event.player.PlayerInteractEntityEvent
2120
import org.bukkit.event.player.PlayerInteractEvent
@@ -102,6 +101,6 @@ class DebugWaxedWeatheredCutCopperStairs(stack: ItemStack)
102101
}
103102
}
104103

105-
private fun Audience.sendDebug(subkey: String, vararg args: TranslationArgument) {
104+
private fun Audience.sendDebug(subkey: String, vararg args: PylonArgument) {
106105
return sendMessage(Component.translatable("pylon.pyloncore.message.debug.$subkey", *args))
107106
}

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/content/fluid/FluidPipe.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ import io.github.pylonmc.pylon.core.block.BlockStorage
44
import io.github.pylonmc.pylon.core.entity.EntityStorage
55
import io.github.pylonmc.pylon.core.fluid.FluidManager
66
import io.github.pylonmc.pylon.core.fluid.PylonFluid
7-
import io.github.pylonmc.pylon.core.fluid.connecting.ConnectingPointInteraction
8-
import io.github.pylonmc.pylon.core.fluid.connecting.ConnectingPointNewBlock
9-
import io.github.pylonmc.pylon.core.fluid.connecting.ConnectingPointPipeConnector
10-
import io.github.pylonmc.pylon.core.fluid.connecting.ConnectingPointPipeMarker
11-
import io.github.pylonmc.pylon.core.fluid.connecting.ConnectingService
7+
import io.github.pylonmc.pylon.core.fluid.connecting.*
128
import io.github.pylonmc.pylon.core.fluid.tags.FluidTemperature
9+
import io.github.pylonmc.pylon.core.i18n.PylonArgument
1310
import io.github.pylonmc.pylon.core.item.PylonItem
1411
import io.github.pylonmc.pylon.core.item.base.PylonInteractor
1512
import io.github.pylonmc.pylon.core.item.base.PylonItemEntityInteractor
1613
import io.github.pylonmc.pylon.core.util.gui.unit.UnitFormat
1714
import io.github.pylonmc.pylon.core.util.position.BlockPosition
1815
import net.kyori.adventure.text.Component
19-
import net.kyori.adventure.text.ComponentLike
2016
import net.kyori.adventure.text.JoinConfiguration
2117
import org.bukkit.Material
2218
import org.bukkit.block.Block
@@ -26,27 +22,26 @@ import org.bukkit.event.player.PlayerInteractEntityEvent
2622
import org.bukkit.event.player.PlayerInteractEvent
2723
import org.bukkit.inventory.EquipmentSlot
2824
import org.bukkit.inventory.ItemStack
29-
import java.util.function.Predicate
3025

3126
open class FluidPipe(stack: ItemStack) : PylonItem(stack), PylonItemEntityInteractor, PylonInteractor {
3227
val material = Material.valueOf(getSettings().getOrThrow<String>("material").uppercase())
3328
val fluidPerSecond = getSettings().getOrThrow<Double>("fluid-per-second")
3429
val allowedFluids = getSettings().getOrThrow<List<String>>("allow-fluids")
3530
.map { s -> FluidTemperature.valueOf(s.uppercase()) }
3631

37-
override fun getPlaceholders(): Map<String, ComponentLike>
38-
= mapOf<String, ComponentLike>(
39-
"fluid_per_second" to UnitFormat.MILLIBUCKETS_PER_SECOND.format(fluidPerSecond),
40-
"fluids" to Component.join(
32+
override fun getPlaceholders(): List<PylonArgument> = listOf(
33+
PylonArgument.of("fluid_per_second", UnitFormat.MILLIBUCKETS_PER_SECOND.format(fluidPerSecond)),
34+
PylonArgument.of(
35+
"fluids", Component.join(
4136
JoinConfiguration.separator(Component.text(", ")),
4237
allowedFluids.map(FluidTemperature::valueText)
4338
)
4439
)
40+
)
4541

46-
fun getPredicate(): Predicate<PylonFluid>
47-
= Predicate<PylonFluid> { fluid ->
48-
fluid.hasTag<FluidTemperature>() && allowedFluids.contains(fluid.getTag<FluidTemperature>())
49-
}
42+
open fun canPass(fluid: PylonFluid): Boolean {
43+
return fluid.hasTag<FluidTemperature>() && fluid.getTag<FluidTemperature>() in allowedFluids
44+
}
5045

5146
override fun onUsedToRightClickEntity(event: PlayerInteractEntityEvent) {
5247
if (event.hand != EquipmentSlot.HAND) {
@@ -65,7 +60,7 @@ open class FluidPipe(stack: ItemStack) : PylonItem(stack), PylonItemEntityIntera
6560
val segment = ConnectingService.placeConnection(event.getPlayer())
6661
if (segment != null) {
6762
FluidManager.setFluidPerSecond(segment, fluidPerSecond)
68-
FluidManager.setFluidPredicate(segment, this.getPredicate())
63+
FluidManager.setFluidPredicate(segment, this::canPass)
6964
}
7065
}
7166
}
@@ -85,11 +80,14 @@ open class FluidPipe(stack: ItemStack) : PylonItem(stack), PylonItemEntityIntera
8580
}
8681
}
8782

88-
if ((action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) && ConnectingService.isConnecting(player)) {
83+
if ((action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) && ConnectingService.isConnecting(
84+
player
85+
)
86+
) {
8987
val segment = ConnectingService.placeConnection(player)
9088
if (segment != null) {
9189
FluidManager.setFluidPerSecond(segment, fluidPerSecond)
92-
FluidManager.setFluidPredicate(segment, this.getPredicate())
90+
FluidManager.setFluidPredicate(segment, this::canPass)
9391
}
9492
}
9593
}

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/content/fluid/FluidPipeConnector.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.github.pylonmc.pylon.core.block.context.BlockCreateContext
88
import io.github.pylonmc.pylon.core.block.waila.WailaConfig
99
import io.github.pylonmc.pylon.core.entity.EntityStorage
1010
import io.github.pylonmc.pylon.core.fluid.FluidPointType
11+
import io.github.pylonmc.pylon.core.i18n.PylonArgument
1112
import io.github.pylonmc.pylon.core.item.PylonItem
1213
import io.github.pylonmc.pylon.core.util.pylonKey
1314
import org.bukkit.block.Block
@@ -48,7 +49,7 @@ class FluidPipeConnector : PylonBlock, PylonEntityHolderBlock {
4849
}
4950

5051
override fun getWaila(player: Player): WailaConfig
51-
= WailaConfig(name, mapOf("pipe" to this.pipe.stack.effectiveName()))
52+
= WailaConfig(name, listOf(PylonArgument.of("pipe", this.pipe.stack.effectiveName())))
5253

5354
val pipe: PylonItem
5455
get() {

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/content/fluid/FluidPipeDisplay.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import io.github.pylonmc.pylon.core.entity.display.ItemDisplayBuilder
77
import io.github.pylonmc.pylon.core.entity.display.transform.LineBuilder
88
import io.github.pylonmc.pylon.core.fluid.FluidManager
99
import io.github.pylonmc.pylon.core.fluid.connecting.ConnectingService
10-
import io.github.pylonmc.pylon.core.content.fluid.FluidPipe
1110
import io.github.pylonmc.pylon.core.item.PylonItem
1211
import io.github.pylonmc.pylon.core.util.pylonKey
1312
import org.bukkit.GameMode
1413
import org.bukkit.entity.ItemDisplay
1514
import org.bukkit.entity.Player
1615
import org.bukkit.persistence.PersistentDataContainer
17-
import java.util.*
16+
import java.util.UUID
1817

1918
class FluidPipeDisplay : PylonEntity<ItemDisplay> {
2019
val pipe: FluidPipe
@@ -36,14 +35,14 @@ class FluidPipeDisplay : PylonEntity<ItemDisplay> {
3635
// we wait for them to load and then set their segments' fluid per second and predicate
3736
EntityStorage.whenEntityLoads(from, FluidPointInteraction::class.java) { interaction ->
3837
FluidManager.setFluidPerSecond(interaction.point.segment, pipe.fluidPerSecond)
39-
FluidManager.setFluidPredicate(interaction.point.segment, pipe.getPredicate())
38+
FluidManager.setFluidPredicate(interaction.point.segment, pipe::canPass)
4039
}
4140

4241
// Technically only need to do this for one of the end points since they're part of the same segment, but
4342
// we do it twice just to be safe
4443
EntityStorage.whenEntityLoads(to, FluidPointInteraction::class.java) { interaction ->
4544
FluidManager.setFluidPerSecond(interaction.point.segment, pipe.fluidPerSecond)
46-
FluidManager.setFluidPredicate(interaction.point.segment, pipe.getPredicate())
45+
FluidManager.setFluidPredicate(interaction.point.segment, pipe::canPass)
4746
}
4847
}
4948

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/content/fluid/FluidPipeMarker.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.github.pylonmc.pylon.core.block.context.BlockCreateContext
99
import io.github.pylonmc.pylon.core.block.waila.WailaConfig
1010
import io.github.pylonmc.pylon.core.datatypes.PylonSerializers
1111
import io.github.pylonmc.pylon.core.entity.EntityStorage
12+
import io.github.pylonmc.pylon.core.i18n.PylonArgument
1213
import io.github.pylonmc.pylon.core.util.pylonKey
1314
import org.bukkit.block.Block
1415
import org.bukkit.entity.Player
@@ -61,7 +62,7 @@ class FluidPipeMarker : PylonBlock, PylonBreakHandler {
6162
}
6263

6364
override fun getWaila(player: Player): WailaConfig
64-
= WailaConfig(name, mapOf(Pair("pipe", getPipeDisplay()!!.pipe.stack.effectiveName())))
65+
= WailaConfig(name, listOf(PylonArgument.of("pipe", getPipeDisplay()!!.pipe.stack.effectiveName())))
6566

6667
companion object {
6768
val KEY = pylonKey("fluid_pipe_marker")

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/fluid/PylonFluid.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.github.pylonmc.pylon.core.fluid
22

33
import io.github.pylonmc.pylon.core.PylonCore
4-
import io.github.pylonmc.pylon.core.i18n.AddonTranslator
4+
import io.github.pylonmc.pylon.core.i18n.PylonTranslator.Companion.translator
55
import io.github.pylonmc.pylon.core.item.builder.ItemStackBuilder
66
import io.github.pylonmc.pylon.core.registry.PylonRegistry
77
import io.github.pylonmc.pylon.core.util.getAddon
@@ -35,11 +35,9 @@ open class PylonFluid(
3535

3636
init {
3737
val addon = PylonRegistry.ADDONS[NamespacedKey(key.namespace, key.namespace)]!!
38-
val translator = AddonTranslator.translators[addon]!!
39-
4038
for (locale in addon.languages) {
4139
val translationKey = "pylon.${key.namespace}.fluid.${key.key}"
42-
check(translator.canTranslate(translationKey, locale)) {
40+
check(addon.translator.canTranslate(translationKey, locale)) {
4341
PylonCore.logger.warning("${key.namespace} is missing a translation key for fluid ${key.key} (locale: ${locale.displayName} | expected translation key: $translationKey")
4442
}
4543
}

0 commit comments

Comments
 (0)