Skip to content
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ let package = Package(
]
),

.target(
name: "API"
),

.target(
name: "MinecraftUtilities",
dependencies: [
Expand Down
84 changes: 84 additions & 0 deletions Sources/API/ChatColor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

public enum ChatColor: String, CaseIterable, Sendable {
/// The special character that prefixes all chat color codes.
case specialChar

case bold
case italic
case magic
case reset
case strikethrough
case underline

case aqua
case black
case blue
case darkAqua
case darkBlue
case darkGray
case darkGreen
case darkPurple
case darkRed
case gold
case gray
case green
case lightPurple
case red
case white
case yellow
}

// MARK: Char
extension ChatColor {
// TODO: support
}

// MARK: Is format
extension ChatColor {
/// Whether this is a format code.
public var isFormat: Bool {
switch self {
case .specialChar, .bold, .italic, .magic, .reset, .strikethrough, .underline:
true
default:
false
}
}
}

// MARK: Is color
extension ChatColor {
/// Whether this is a color code.
public var isColor: Bool {
!isFormat
}
}

// MARK: Strip
extension ChatColor {
/// Strips the given input of all color codes.
///
/// - Returns: A copy of the input excluding any coloring.
public static func strip(_ input: String) -> String {
// TODO: implement
return input
}
}

// MARK: Translate
extension ChatColor {
/// Copies and translates the input using a color code character.
///
/// - Parameters:
/// - char: Color code character to replace. Example: `&`.
/// - input: Text you want to translate using the given color code.
///
/// - Warning: The color code character will only be replaced if it is immediately followed by 0-9, A-F, a-f, K-O, k-o, R or r.
public static func translate(
char: Character = "&",
input: String
) -> String {
// TODO: implement
return input
}
}
15 changes: 15 additions & 0 deletions Sources/API/Difficulty.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

/// Various difficulty levels.
public enum Difficulty: Sendable {
/// Hostile mobs spawn, enemies deal less damage than `normal`, the hunger bar does deplete and starving deals up to 5 hearts of damage.
case easy

/// Hostile mobs spawn, enemies deal greater damage than `normal`, the hunger bar does deplete and starving can kill players.
case hard

/// Hostile mobs spawn, enemies deal normal amounts of damage, the hunger bar does deplete and starving deals up to 9.5 hearts of damage.
case normal

/// Players regain health over time, hostile mobs don't spawn, the hunger bar does not deplete.
case peaceful
}
33 changes: 33 additions & 0 deletions Sources/API/Location.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

/// A 3-dimensional position in a world.
public struct Location: Sendable {
/// The 'x' coordinate of this location.
public var x:Float

/// The 'y' coordinate of this location.
public var y:Float

/// The 'z' coordinate of this location.
public var z:Float

public var yaw:Float
public var pitch:Float
}

// MARK: Whole coordinates
extension Location {
/// Floored 'x' coordinate of this location.
public var blockX: Int {
Int(x)
}

/// Floored 'y' coordinate of this location.
public var blockY: Int {
Int(y)
}

/// Floored 'z' coordinate of this location.
public var blockZ: Int {
Int(z)
}
}
33 changes: 33 additions & 0 deletions Sources/API/World.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

public struct World: Sendable {

/// The spawn location for this world.
public var spawnLocation:Location

/// Releative in-game time of this world.
public var time:Int

/// View distance used for this world.
public var viewDistance:Int

public var difficulty:Difficulty

var flags:Flags.RawValue
}

// MARK: Flags
extension World {
enum Flags: UInt16 {
case animalsCanSpawn = 1
case monstersCanSpawn = 2
case pvpIsAllowed = 4
case hasCeiling = 8
case hasSkylight = 16
case autoSaves = 32
case bedWorks = 64
case hardcore = 128
case natural = 256
case piglinSafe = 512
case respawnAnchorWorks = 1024
}
}
5 changes: 5 additions & 0 deletions Sources/API/advancement/Advancement.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

public struct Advancement: Sendable {
public let criteria:[String]
public let requirements:AdvancementRequirements
}
53 changes: 53 additions & 0 deletions Sources/API/advancement/AdvancementDisplay.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

/// Information about how an advancement is displayed.
public struct AdvancementDisplay: Sendable {
/// The X position of the advancement in the advancement screen.
public let x:Float

/// The Y position of the advancement in the advancement screen.
public let y:Float

/// Display type for the advancement.
public let type:AdvancementDisplayType

/// Bit-packed boolean values.
var _flags:Flags.RawValue

/// The `ItemStack` that is shown for the advancement.
//public let icon:ItemStack // TODO: support

/// Title of the advancement without color codes.
public let title:String

/// Description of the advancement without color codes.
public let description:String
}

// MARK: Flags
extension AdvancementDisplay {
enum Flags: UInt8, Sendable {
case shouldShowToast = 1
case shouldAnnounceChat = 2
case isHidden = 4
}

func isFlag(_ flag: Flags) -> Bool {
_flags & flag.rawValue != 0
}

/// Whether to show a toast to the player when the advancement is completed.
public var shouldShowToast: Bool {
isFlag(.shouldShowToast)
}

/// Whether to announce in the chat when the advancement is completed.
public var shouldAnnounceChat: Bool {
isFlag(.shouldAnnounceChat)
}

/// Whether to hide the advancement and all its children from the advancement screen until the advancement has been completed.
/// Has no effect on root advancements, but still affects all their children.
public var isHidden: Bool {
isFlag(.isHidden)
}
}
20 changes: 20 additions & 0 deletions Sources/API/advancement/AdvancementDisplayType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

public enum AdvancementDisplayType: String, CaseIterable, Sendable {
/// Challenge icons have a stylised icon frame.
case challenge

/// Goal icons have a rounded icon frame.
case goal

/// Normal icons have a square icon frame.
case normal
}

// MARK: ChatColor
extension AdvancementDisplayType {
/// The `ChatColor` used by Minecraft for this advancement.
public var chatColor: ChatColor {
// TODO: fix
.gold
}
}
60 changes: 60 additions & 0 deletions Sources/API/advancement/AdvancementProgress.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

#if canImport(FoundationEssentials)
import FoundationEssentials
#elseif canImport(Foundation)
import Foundation
#endif

/// Status of an advancement for a player.
public struct AdvancementProgress: Sendable {
/// Criteria that has been awarded.
var _awardedCriteria:[String:Date]

/// Criteria that has not been awarded.
var _remainingCriteria:[String]
}

// MARK: Criteria
extension AdvancementProgress {
public var awardedCriteria: Dictionary<String, Date> {
_read {
yield _awardedCriteria
}
}

/// - Returns: the `Date` the specified criteria was awarded.
public func dateAwardedForCriteria(_ criteria: String) -> Date? {
return _awardedCriteria[criteria]
}

/// Marks the specified criteria as awarded at the current time.
///
/// - Returns: `true` if awarded; `false` if criteria does not exist or already awarded.
@discardableResult
public mutating func awardCriteria(_ criteria: String) -> Bool {
if awardedCriteria[criteria] != nil {
return false
}
_awardedCriteria[criteria] = Date.now
return true
}

/// Marks the specified criteria as uncompleted.
///
/// - Returns: `true` if removed; `false` if criteria does not exist or not awarded.
public mutating func revokeCriteria(_ criteria: String) -> Bool {
if _awardedCriteria[criteria] == nil {
return false
}
_awardedCriteria[criteria] = nil
return true
}
}

// MARK: Awarded Criteria
extension AdvancementProgress {
struct AwardedCriteria: Sendable {
let id:String
let timestamp:Date
}
}
5 changes: 5 additions & 0 deletions Sources/API/advancement/AdvancementRequirement.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

public struct AdvancementRequirement: Sendable {
public let requiredCriteria:[String]
public let isStrict:Bool
}
4 changes: 4 additions & 0 deletions Sources/API/advancement/AdvancementRequirements.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

public struct AdvancementRequirements: Sendable {
public let requirements:[AdvancementRequirement]
}
Loading