Skip to content

Commit

Permalink
Update mod icon and other finishing touches
Browse files Browse the repository at this point in the history
  • Loading branch information
ArkoSammy12 committed Jul 25, 2024
1 parent 6591b06 commit dcba4fc
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ import java.nio.file.Path
object PublicEnderChest : ModInitializer {

const val MOD_ID: String = "publicenderchest"
const val PUBLIC_INVENTORY_FILE_NAME: String = "public-inventory"
val LOGGER: Logger = LoggerFactory.getLogger(MOD_ID)
val CONFIG_MANAGER: ConfigManager = TomlConfigManager(MOD_ID, SettingGroups.settingGroups, ConfigSettings.settingBuilders)
val INVENTORY_MANAGER: PublicInventoryManager = PublicInventoryManager()
val USING_PUBLIC_INVENTORY: AttachmentType<Boolean> = run<AttachmentType<Boolean>> {
lateinit var DATABASE_MANAGER: InventoryDatabaseManager
private set
val USING_PUBLIC_INVENTORY: AttachmentType<Boolean> = run {
val builder: AttachmentRegistry.Builder<Boolean> = AttachmentRegistryImpl.builder()
builder.copyOnDeath().persistent(Codec.BOOL).initializer {true}.buildAndRegister(Identifier.of(MOD_ID, "using_public_inventory"))
}
lateinit var DATABASE_MANAGER: InventoryDatabaseManager
private set

override fun onInitialize() {
DefaultConfigRegistrar.registerConfigManager(CONFIG_MANAGER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ object CommandManager {
parentNode.addChild(openPublicInventoryNode)
DatabaseCommands.registerCommands(parentNode)


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object DatabaseCommands {
.build()

val pageArgumentNode: ArgumentCommandNode<ServerCommandSource, Int> = CommandManager
.argument("page", IntegerArgumentType.integer())
.argument("page", IntegerArgumentType.integer(0))
.requires { src -> src.hasPermissionLevel(4) }
.executes { ctx ->
val player: ServerPlayerEntity = ctx.source.playerOrThrow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ class PublicInventory(private val itemStacks: DefaultedList<ItemStack> = Default
return false
}
return true

}

override fun markDirty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ class PublicInventoryManager(inputPublicInventory: PublicInventory = PublicInven

override fun tick(server: MinecraftServer) {
if (publicInventory.dirty) {
serializer.writeManager(CODEC, PublicEnderChest.PUBLIC_INVENTORY_FILE_NAME, server)
serializer.writeManager(CODEC, PUBLIC_INVENTORY_FILE_NAME, server)
}
}

override fun onServerStarting(server: MinecraftServer) {
val inventoryManager: PublicInventoryManager = serializer.readManager(CODEC, PublicEnderChest.PUBLIC_INVENTORY_FILE_NAME, server) ?: return
val inventoryManager: PublicInventoryManager = serializer.readManager(CODEC, PUBLIC_INVENTORY_FILE_NAME, server) ?: return
this.publicInventory = inventoryManager.publicInventory
}

override fun onServerStopping(server: MinecraftServer) {
serializer.writeManager(CODEC, PublicEnderChest.PUBLIC_INVENTORY_FILE_NAME, server, true)
serializer.writeManager(CODEC, PUBLIC_INVENTORY_FILE_NAME, server, true)
}

override fun onBlockInteractedListener(player: PlayerEntity, world: World, hand: Hand, hitResult: BlockHitResult): ActionResult {
Expand Down Expand Up @@ -85,13 +85,16 @@ class PublicInventoryManager(inputPublicInventory: PublicInventory = PublicInven
}

companion object {

private const val PUBLIC_INVENTORY_FILE_NAME: String = "public-inventory"
val CODEC: Codec<PublicInventoryManager> = RecordCodecBuilder.create { instance ->
instance.group(
PublicInventory.CODEC.fieldOf("public_inventory").forGetter { manager -> manager.publicInventory }
).apply(instance) { publicInventory ->
PublicInventoryManager(publicInventory)
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,15 @@ class InventoryDatabaseManager(server: MinecraftServer) {
return deletedRows
}


companion object {

const val MAIN_TABLE_NAME = "public_inventory_interactions"
private const val DATABASE_FILE_NAME = "${PublicEnderChest.MOD_ID}.db"

fun getDatabaseFileUrl(server: MinecraftServer) : String =
private fun getDatabaseFileUrl(server: MinecraftServer) : String =
"jdbc:sqlite:${getModFolderPath(server).resolve(DATABASE_FILE_NAME)}"

}



}

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.registry.Registries
import net.minecraft.registry.RegistryKey
import net.minecraft.registry.RegistryKeys
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.text.MutableText
import net.minecraft.util.Identifier
Expand Down Expand Up @@ -32,7 +31,9 @@ sealed interface InventoryInteractionLog {

companion object {

fun of(logActionType: InventoryInteractionType, player: ServerPlayerEntity, itemStack: ItemStack, quantity: Int) : InventoryInteractionLog {
val DTF: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy, hh:mm:ss a")

fun of(inventoryInteractionType: InventoryInteractionType, player: ServerPlayerEntity, itemStack: ItemStack, quantity: Int) : InventoryInteractionLog {
val now: LocalDateTime = LocalDateTime.now()
val playerName: String = player.name.string
val uuid: String = player.uuid.toString()
Expand All @@ -44,7 +45,7 @@ sealed interface InventoryInteractionLog {
} else {
itemStackRegistryKey.value
}
return when (logActionType) {
return when (inventoryInteractionType) {
InventoryInteractionType.ITEM_REMOVE -> ItemRemoveLog(playerName, uuid, itemStackId, quantity, now)
InventoryInteractionType.ITEM_INSERT -> ItemInsertLog(playerName, uuid, itemStackId, quantity, now)
}
Expand All @@ -68,8 +69,6 @@ sealed interface InventoryInteractionLog {
return result
}

val DTF: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy, hh:mm:ss a")

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class ItemInsertLog(

override fun getLogText(): MutableText {
val duration: Duration = Duration.between(timestamp, LocalDateTime.now())
val durationText: MutableText = Text.literal("${InventoryInteractionLog.formatElapsedTime(duration)} ")
val timestampText: MutableText = Text.literal("${InventoryInteractionLog.formatElapsedTime(duration)} ")
.setStyle(Style.EMPTY.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal(timestamp.format(InventoryInteractionLog.DTF)))))
.formatted(Formatting.DARK_AQUA)
val playerText: MutableText = Text.literal("$playerName ")
val playerNameText: MutableText = Text.literal("$playerName ")
.setStyle(Style.EMPTY.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal(uuid))).withClickEvent(ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, uuid)))
.formatted(Formatting.AQUA)
val interactedInventoryText: MutableText = Text.literal("inserted ")
Expand All @@ -31,7 +31,7 @@ class ItemInsertLog(
val itemText: MutableText = Text.literal("$itemStackId")
.formatted(Formatting.GREEN)

return Text.empty().append(durationText).append(playerText).append(interactedInventoryText).append(quantityText).append(itemText)
return Text.empty().append(timestampText).append(playerNameText).append(interactedInventoryText).append(quantityText).append(itemText)
}

override fun consumeDbConnection(connection: Connection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class ItemRemoveLog(

override fun getLogText(): MutableText {
val duration: Duration = Duration.between(timestamp, LocalDateTime.now())
val durationText: MutableText = Text.literal("${InventoryInteractionLog.formatElapsedTime(duration)} ")
val timestampText: MutableText = Text.literal("${InventoryInteractionLog.formatElapsedTime(duration)} ")
.setStyle(Style.EMPTY.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal(timestamp.format(InventoryInteractionLog.DTF)))))
.formatted(Formatting.DARK_AQUA)
val playerText: MutableText = Text.literal("$playerName ")
val playerNameText: MutableText = Text.literal("$playerName ")
.setStyle(Style.EMPTY.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal(uuid))).withClickEvent(ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, uuid)))
.formatted(Formatting.AQUA)
val interactedInventoryText: MutableText = Text.literal("removed ")
Expand All @@ -31,7 +31,7 @@ class ItemRemoveLog(
val itemText: MutableText = Text.literal("$itemStackId")
.formatted(Formatting.GREEN)

return Text.empty().append(durationText).append(playerText).append(interactedInventoryText).append(quantityText).append(itemText)
return Text.empty().append(timestampText).append(playerNameText).append(interactedInventoryText).append(quantityText).append(itemText)
}

override fun consumeDbConnection(connection: Connection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.util.*
class JsonInventoryManagerSerializer<T : InventoryManager>(override val inventoryManager: T) : InventoryManagerSerializer<T> {

override fun writeManager(codec: Codec<in T>, fileName: String, server: MinecraftServer, logWrite: Boolean) {
val filePath: Path = getModFolderPath(server).resolve(fileName + FILE_SUFFIX)
val filePath: Path = getModFolderPath(server).resolve(getPathNameForFile(fileName))
val encodedManager: DataResult<JsonElement> = codec.encodeStart(JsonOps.COMPRESSED, inventoryManager)
val encodedJson: JsonElement = encodedManager.getOrThrow { e ->
throw IllegalStateException("Error attempting to encode inventory manager: $e")
Expand All @@ -33,7 +33,7 @@ class JsonInventoryManagerSerializer<T : InventoryManager>(override val inventor
}

override fun readManager(codec: Codec<out T>, fileName: String, server: MinecraftServer): T? {
val filePath: Path = getModFolderPath(server).resolve(fileName + FILE_SUFFIX)
val filePath: Path = getModFolderPath(server).resolve(getPathNameForFile(fileName))
try {
if (!Files.exists(filePath)) {
PublicEnderChest.LOGGER.warn("Public ender chest file not found! Creating new one at $filePath")
Expand All @@ -55,7 +55,7 @@ class JsonInventoryManagerSerializer<T : InventoryManager>(override val inventor

companion object {

private const val FILE_SUFFIX = ".json"
fun getPathNameForFile(fileName: String) : String = "$fileName.json"

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.util.*
class NbtInventoryManagerSerializer<T : InventoryManager>(override val inventoryManager: T) : InventoryManagerSerializer<T> {

override fun writeManager(codec: Codec<in T>, fileName: String, server: MinecraftServer, logWrite: Boolean) {
val filePath: Path = getModFolderPath(server).resolve(fileName + FILE_SUFFIX)
val filePath: Path = getModFolderPath(server).resolve(getPathNameForFile(fileName))
val encodedManager: DataResult<NbtElement> = codec.encodeStart(NbtOps.INSTANCE, inventoryManager)
val encodedNbt: NbtElement = encodedManager.getOrThrow { e ->
throw IllegalStateException("Error attempting to encode inventory manager: $e")
Expand All @@ -37,7 +37,7 @@ class NbtInventoryManagerSerializer<T : InventoryManager>(override val inventory
}

override fun readManager(codec: Codec<out T>, fileName: String, server: MinecraftServer): T? {
val filePath: Path = getModFolderPath(server).resolve(fileName + FILE_SUFFIX)
val filePath: Path = getModFolderPath(server).resolve(getPathNameForFile(fileName))
try {
if (!Files.exists(filePath)) {
PublicEnderChest.LOGGER.warn("Public ender chest file not found! Creating new one at $filePath")
Expand All @@ -57,7 +57,7 @@ class NbtInventoryManagerSerializer<T : InventoryManager>(override val inventory

companion object {

private const val FILE_SUFFIX: String = ".nbt"
fun getPathNameForFile(fileName: String) : String = "$fileName.nbt"

}

Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/xd/arkosammy/publicenderchest/util/Events.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ import xd.arkosammy.publicenderchest.util.ducks.ServerPlayerEntityDuck
object Events {

fun registerEvents() {

CommandRegistrationCallback.EVENT.register(CommandManager::registerCommands)
UseBlockCallback.EVENT.register(::onBlockInteracted)
UseItemCallback.EVENT.register(::onItemInteracted)
ServerTickEvents.END_SERVER_TICK.register(::onServerTick)
PayloadTypeRegistry.playC2S().register(OpenPublicInventoryPayload.PACKET_ID, OpenPublicInventoryPayload.PACKET_CODEC)
ServerPlayNetworking.registerGlobalReceiver(OpenPublicInventoryPayload.PACKET_ID, ::handleOpenInventoryPayload)

}

private fun onServerTick(server: MinecraftServer) {
Expand Down

0 comments on commit dcba4fc

Please sign in to comment.