Skip to content

Working with Files

cooffeeRequired edited this page May 9, 2025 · 4 revisions

Working with Files πŸ“

This guide covers how to work with JSON files using skJson.

File Operations πŸ”§

Creating New Files

# Create empty JSON file
create json file "data.json"

# Create file with content
create json file "data.json" and write to it "{'name': 'John'}"

# Create with configuration
create json file "data.json" with configuration[replace=true, encoding=UTF-8]

Binding Files

# Bind JSON file to a storage ID
bind json file "data.json" as "my-storage"

# Bind with watcher
bind json file "data.json" as "my-storage" and let bind storage watcher

Unbinding Files

# Unbind storage
unbind json storage id "my-storage"

File Watchers πŸ‘€

Setting Up Watchers

# Bind storage watcher
bind storage watcher to "my-storage"

# Unbind storage watcher
unbind storage watcher from "my-storage"

Watcher Events

on json watcher file change:
    set {_file} to event-file
    set {_uuid} to event-uuid
    set {_content} to event-json
    send "File %{_file}% has been changed!"

File Management πŸ“‚

Checking File Existence

if json file "data.json" exists:
    send "File exists!"

Getting All JSON Files

# Get all JSON files in a directory
set {_files::*} to all json files from dir "plugins/Skript/data"

File Path Operations

# Get file path from watcher event
on json watcher file change:
    set {_path} to event-file
    send "Changed file: %{_path}%"

Best Practices πŸ’‘

  1. Always use meaningful storage IDs
  2. Implement proper error handling
  3. Use file watchers for real-time updates
  4. Keep file paths consistent
  5. Use appropriate file encoding
  6. Handle file permissions correctly
  7. Backup important files regularly

Examples πŸ“š

Player Data Storage

# Create player data file
create json file "plugins/Skript/playerdata/%player's uuid%.json"

# Bind to storage
bind json file "plugins/Skript/playerdata/%player's uuid%.json" as "player-%player's uuid%"

# Save player data
set {_data} to json from "{'name': '%player%', 'lastLogin': '%now%'}"
set json storage of id "player-%player's uuid%" to {_data}
save json storage id "player-%player's uuid%"

Configuration File

# Create config file
create json file "plugins/Skript/config.json" and write to it "{'debug': false, 'maxPlayers': 100}"

# Bind with watcher
bind json file "plugins/Skript/config.json" as "config" and let bind storage watcher

# Watch for changes
on json watcher file change:
    if event-uuid is "config":
        send "Configuration has been updated!"

File System Operations

# List all JSON files in directory
set {_files::*} to all json files from dir "plugins/Skript/data"

# Process each file
loop {_files::*}:
    set {_json} to json from file loop-value
    if json type of {_json} is json-object:
        send "Processing file: %loop-value%"

Common Issues and Solutions πŸ”

File Not Found

# Always check if file exists
if json file "data.json" exists:
    set {_json} to json from file "data.json"
else:
    send "File not found!"

Permission Issues

# Make sure the plugin has write permissions
create json file "data.json" with configuration[replace=true]

File Encoding

# Specify encoding when creating file
create json file "data.json" with configuration[encoding=UTF-8]

Next Steps πŸ‘£

Note

For more information about Markdown formatting and alerts, see GitHub's discussion on Markdown alerts.