Skip to content

Commit 9426188

Browse files
authored
Merge pull request #158 from pylonmc/hydraulic-machines-fixes
Hydraulic machines fixes
2 parents ebe6fdd + 5963432 commit 9426188

6 files changed

Lines changed: 76 additions & 37 deletions

File tree

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ internal object MultiblockCache : Listener {
6767
continue
6868
}
6969

70-
val multiblock = BlockStorage.getAs<PylonMultiblock>(multiblockPosition)!!
71-
if (multiblock.checkFormed()) {
70+
val multiblock = BlockStorage.getAs<PylonMultiblock>(multiblockPosition)
71+
if (multiblock != null && multiblock.checkFormed()) {
7272
formedMultiblocks.add(multiblockPosition)
7373
} else {
7474
formedMultiblocks.remove(multiblockPosition)
@@ -120,13 +120,13 @@ internal object MultiblockCache : Listener {
120120

121121
private fun onBlockModified(block: Block)
122122
= loadedMultiblocksWithComponent(block).forEach {
123-
markDirty(BlockStorage.getAs<PylonMultiblock>(it)!!)
123+
BlockStorage.getAs<PylonMultiblock>(it)?.let { markDirty(it) }
124124
}
125125

126126
private fun loadedMultiblocksWithComponent(block: Block): List<BlockPosition>
127-
= loadedMultiblocksWithComponentsInChunk(block.position.chunk).filter {
128-
BlockStorage.getAs<PylonMultiblock>(it)!!.isPartOfMultiblock(block)
129-
}
127+
= loadedMultiblocksWithComponentsInChunk(block.position.chunk).filter {
128+
BlockStorage.getAs<PylonMultiblock>(it)?.isPartOfMultiblock(block) == true
129+
}
130130

131131
private fun loadedMultiblocksWithComponentsInChunk(chunkPosition: ChunkPosition): Set<BlockPosition>
132132
= multiblocksWithComponentsInChunk[chunkPosition] ?: emptySet()
@@ -135,7 +135,7 @@ internal object MultiblockCache : Listener {
135135
private fun handle(event: PylonChunkBlocksLoadEvent) {
136136
// Refresh existing multiblocks with a component in the chunk that was just loaded
137137
for (multiblockPosition in loadedMultiblocksWithComponentsInChunk(event.chunk.position)) {
138-
refreshFullyLoaded(BlockStorage.getAs<PylonMultiblock>(multiblockPosition)!!)
138+
BlockStorage.getAs<PylonMultiblock>(multiblockPosition)?.let { refreshFullyLoaded(it) }
139139
}
140140

141141
// Add new multiblocks

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,17 @@ interface PylonGuiBlock : PylonBreakHandler, PylonInteractableBlock {
3636
if (
3737
event.action.isRightClick &&
3838
event.hand == EquipmentSlot.HAND &&
39-
event.useInteractedBlock() != Event.Result.DENY
39+
event.useInteractedBlock() != Event.Result.DENY &&
40+
!event.player.isSneaking
4041
) {
4142
event.setUseInteractedBlock(Event.Result.DENY)
42-
if (!event.player.isSneaking) {
43-
event.setUseItemInHand(Event.Result.DENY)
44-
val window = Window.single()
45-
.setGui(gui)
46-
.setTitle(AdventureComponentWrapper((this as PylonBlock).name))
47-
.setViewer(event.player)
48-
.build()
49-
window.open()
50-
}
43+
event.setUseItemInHand(Event.Result.DENY)
44+
val window = Window.single()
45+
.setGui(gui)
46+
.setTitle(AdventureComponentWrapper((this as PylonBlock).name))
47+
.setViewer(event.player)
48+
.build()
49+
window.open()
5150
}
5251
}
5352

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import kotlin.math.min
2929

3030
interface PylonSimpleMultiblock : PylonMultiblock, PylonEntityHolderBlock {
3131

32-
interface Component {
32+
interface MultiblockComponent {
3333
fun matches(block: Block): Boolean
3434
fun spawnGhostBlock(block: Block): UUID
3535
}
@@ -54,7 +54,7 @@ interface PylonSimpleMultiblock : PylonMultiblock, PylonEntityHolderBlock {
5454
}
5555
}
5656

57-
data class VanillaComponent(val material: Material) : Component {
57+
data class VanillaMultiblockComponent(val material: Material) : MultiblockComponent {
5858
override fun matches(block: Block): Boolean
5959
= !BlockStorage.isPylonBlock(block) && block.type == material
6060

@@ -68,7 +68,7 @@ interface PylonSimpleMultiblock : PylonMultiblock, PylonEntityHolderBlock {
6868
}
6969
}
7070

71-
data class PylonComponent(val key: NamespacedKey) : Component {
71+
data class PylonMultiblockComponent(val key: NamespacedKey) : MultiblockComponent {
7272
override fun matches(block: Block): Boolean
7373
= BlockStorage.get(block)?.schema?.key == key
7474

@@ -87,9 +87,9 @@ interface PylonSimpleMultiblock : PylonMultiblock, PylonEntityHolderBlock {
8787
/**
8888
* Rotation will automatically be accounted for.
8989
*/
90-
val components: Map<Vector3i, Component>
90+
val components: Map<Vector3i, MultiblockComponent>
9191

92-
fun validStructures(): List<Map<Vector3i, Component>> = listOf(
92+
fun validStructures(): List<Map<Vector3i, MultiblockComponent>> = listOf(
9393
// 0 degrees
9494
components,
9595
// 90 degrees (anticlockwise)

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

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

33
import com.google.common.base.CaseFormat
4+
import io.github.pylonmc.pylon.core.registry.PylonRegistry
5+
import org.bukkit.Material
6+
import org.bukkit.NamespacedKey
47
import org.bukkit.configuration.ConfigurationSection
8+
import org.bukkit.inventory.ItemStack
59
import kotlin.properties.ReadWriteProperty
610
import kotlin.reflect.KProperty
711

@@ -26,6 +30,45 @@ open class ConfigSection(val internalSection: ConfigurationSection) {
2630
fun getSectionOrThrow(key: String): ConfigSection =
2731
getSection(key) ?: throw KeyNotFoundException(internalSection.currentPath, key)
2832

33+
fun getItem(key: String): ItemStack? {
34+
if (key.contains(':')) {
35+
val namespacedKey = NamespacedKey.fromString(key)
36+
if (namespacedKey != null) {
37+
val pylonItem = PylonRegistry.ITEMS[namespacedKey]
38+
if (pylonItem != null) {
39+
return pylonItem.itemStack
40+
}
41+
}
42+
}
43+
44+
val material = Material.getMaterial(key.uppercase())
45+
if (material != null) {
46+
return ItemStack(material)
47+
}
48+
49+
return null
50+
}
51+
52+
fun getItemOrThrow(key: String): ItemStack {
53+
if (key.contains(':')) {
54+
val namespacedKey = NamespacedKey.fromString(key)
55+
if (namespacedKey != null) {
56+
val pylonItem = PylonRegistry.ITEMS[namespacedKey]
57+
if (pylonItem != null) {
58+
return pylonItem.itemStack
59+
}
60+
error("No such Pylon item $key")
61+
}
62+
}
63+
64+
val material = Material.getMaterial(key.uppercase())
65+
if (material != null) {
66+
return ItemStack(material)
67+
}
68+
69+
error("No such material $key")
70+
}
71+
2972
fun <T> get(key: String, type: Class<out T>): T? {
3073
val value = internalSection.get(key) ?: return null
3174
return type.cast(value)

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/guide/pages/SearchItemsAndFluidsPage.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,23 @@ class SearchItemsAndFluidsPage : SearchPage(
2121

2222
fun getItemButtons(player: Player): MutableList<Pair<Item, String>> = PylonRegistry.ITEMS.getKeys().filter {
2323
!PylonGuide.hiddenItems.contains(it)
24-
}.map {
25-
val translator = AddonTranslator.translators[getAddon(it)]!!
24+
}.mapNotNull { item ->
25+
val translator = AddonTranslator.translators[getAddon(item)]!!
2626
val name = translator.translate(
27-
Component.translatable("pylon.${it.namespace}.item.${it.key}.name"), player.locale()
27+
Component.translatable("pylon.${item.namespace}.item.${item.key}.name"), player.locale()
2828
)
29-
check(name != null)
30-
val plainTextName = serializer.serialize(name).lowercase()
31-
Pair(ItemButton(it), plainTextName)
29+
name?.let { Pair(ItemButton(item), serializer.serialize(name).lowercase()) }
30+
3231
}.toMutableList()
3332

3433
fun getFluidButtons(player: Player): MutableList<Pair<Item, String>> = PylonRegistry.FLUIDS.getKeys().filter {
3534
!PylonGuide.hiddenFluids.contains(it)
36-
}.map {
37-
val translator = AddonTranslator.translators[getAddon(it)]!!
35+
}.mapNotNull { fluid ->
36+
val translator = AddonTranslator.translators[getAddon(fluid)]!!
3837
val name = translator.translate(
39-
Component.translatable("pylon.${it.namespace}.fluid.${it.key}"), player.locale()
38+
Component.translatable("pylon.${fluid.namespace}.fluid.${fluid.key}"), player.locale()
4039
)
41-
check(name != null)
42-
val plainTextName = serializer.serialize(name).lowercase()
43-
Pair(FluidButton(it), plainTextName)
40+
name?.let { Pair(FluidButton(fluid), serializer.serialize(name).lowercase()) }
4441
}.toMutableList()
4542

4643
override fun getItemNamePairs(player: Player, search: String): List<Pair<Item, String>> {

pylon-test/src/main/java/io/github/pylonmc/pylon/test/block/TestPylonSimpleMultiblock.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public TestPylonSimpleMultiblock(Block block, PersistentDataContainer pdc) {
2828
}
2929

3030
@Override
31-
public @NotNull Map<Vector3i, Component> getComponents() {
31+
public @NotNull Map<Vector3i, MultiblockComponent> getComponents() {
3232
return Map.of(
33-
new Vector3i(1, 1, 4), new PylonComponent(Blocks.SIMPLE_BLOCK_KEY),
34-
new Vector3i(2, -1, 0), new PylonComponent(Blocks.SIMPLE_BLOCK_KEY)
33+
new Vector3i(1, 1, 4), new PylonMultiblockComponent(Blocks.SIMPLE_BLOCK_KEY),
34+
new Vector3i(2, -1, 0), new PylonMultiblockComponent(Blocks.SIMPLE_BLOCK_KEY)
3535
);
3636
}
3737
}

0 commit comments

Comments
 (0)