Skip to content

Commit

Permalink
Always attempt to create the mod's directory in the current world fol…
Browse files Browse the repository at this point in the history
…der if it doesn't exist, whenever this path is accessed. Change the name of the public ender chest display name
  • Loading branch information
ArkoSammy12 committed Jul 26, 2024
1 parent 122a8ae commit 1cb9f2d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import xd.arkosammy.publicenderchest.config.SettingGroups
import xd.arkosammy.publicenderchest.inventory.PublicInventoryManager
import xd.arkosammy.publicenderchest.logging.InventoryDatabaseManager
import xd.arkosammy.publicenderchest.util.Events
import java.nio.file.Files
import java.nio.file.Path

object PublicEnderChest : ModInitializer {

const val MOD_ID: String = "publicenderchest"
const val PUBLIC_INVENTORY_NAME = "Public Ender Chest"
val LOGGER: Logger = LoggerFactory.getLogger(MOD_ID)
val CONFIG_MANAGER: ConfigManager = TomlConfigManager(MOD_ID, SettingGroups.settingGroups, ConfigSettings.settingBuilders)
val INVENTORY_MANAGER: PublicInventoryManager = PublicInventoryManager()
Expand Down Expand Up @@ -62,7 +64,17 @@ object PublicEnderChest : ModInitializer {

}

fun getModFolderPath(server: MinecraftServer) : Path = server.getSavePath(WorldSavePath.ROOT).resolve(MOD_ID)
fun getModFolderPath(server: MinecraftServer) : Path {
val path: Path = server.getSavePath(WorldSavePath.ROOT).resolve(MOD_ID)
try {
if (!Files.exists(path)) {
Files.createDirectory(path)
}
} catch (e: Exception) {
PublicEnderChest.LOGGER.error("Error attempting to create mod directory: $e")
}
return path
}

fun BlockState.isEnderChest() : Boolean = this.isOf(Blocks.ENDER_CHEST)
fun ItemStack.isEnderChest() : Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object CommandManager {
.requires { src -> src.hasPermissionLevel(4) }
.executes { ctx ->
val player: ServerPlayerEntity = ctx.source.player ?: return@executes Command.SINGLE_SUCCESS
(player as ServerPlayerEntityDuck).`publicenderchest$openInventory`("Public Inventory", PublicEnderChest.INVENTORY_MANAGER.publicInventory)
(player as ServerPlayerEntityDuck).`publicenderchest$openInventory`(PublicEnderChest.PUBLIC_INVENTORY_NAME, PublicEnderChest.INVENTORY_MANAGER.publicInventory)
Command.SINGLE_SUCCESS
}
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PublicInventoryManager(inputPublicInventory: PublicInventory = PublicInven
if (!publicInventory.canPlayerUse(player)) {
return ActionResult.PASS
}
(player as ServerPlayerEntityDuck).`publicenderchest$openInventory`("Public Inventory", publicInventory)
(player as ServerPlayerEntityDuck).`publicenderchest$openInventory`(PublicEnderChest.PUBLIC_INVENTORY_NAME, publicInventory)
return ActionResult.SUCCESS
}

Expand All @@ -80,7 +80,7 @@ class PublicInventoryManager(inputPublicInventory: PublicInventory = PublicInven
if (!publicInventory.canPlayerUse(player)) {
return TypedActionResult.pass(heldStack)
}
(player as ServerPlayerEntityDuck).`publicenderchest$openInventory`("Public Inventory", publicInventory)
(player as ServerPlayerEntityDuck).`publicenderchest$openInventory`(PublicEnderChest.PUBLIC_INVENTORY_NAME, publicInventory)
return TypedActionResult.success(heldStack)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class JsonInventoryManagerSerializer<T : InventoryManager>(override val inventor
try {
if (!Files.exists(filePath)) {
PublicEnderChest.LOGGER.warn("Public ender chest file not found! Creating new one at $filePath")
Files.createDirectory(getModFolderPath(server))
Files.createFile(filePath)
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class NbtInventoryManagerSerializer<T : InventoryManager>(override val inventory
try {
if (!Files.exists(filePath)) {
PublicEnderChest.LOGGER.warn("Public ender chest file not found! Creating new one at $filePath")
Files.createDirectory(getModFolderPath(server))
Files.createFile(filePath)
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object Events {
if (!PublicEnderChest.INVENTORY_MANAGER.publicInventory.canPlayerUse(player)) {
return
}
(player as ServerPlayerEntityDuck).`publicenderchest$openInventory`("Public Inventory", PublicEnderChest.INVENTORY_MANAGER.publicInventory)
(player as ServerPlayerEntityDuck).`publicenderchest$openInventory`(PublicEnderChest.PUBLIC_INVENTORY_NAME, PublicEnderChest.INVENTORY_MANAGER.publicInventory)
}

private fun onBlockInteracted(player: PlayerEntity, world: World, hand: Hand, hitResult: BlockHitResult) : ActionResult {
Expand Down

0 comments on commit 1cb9f2d

Please sign in to comment.