Class: RuneRb::Game::Item::Stack

Inherits:
Object
  • Object
show all
Defined in:
app/game/item/stack.rb

Overview

Represents a stack of Items.

Since:

  • 0.9.3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, amount = 1) ⇒ Stack

Called when a new ItemStack is created.

Parameters:

  • id (Integer)

    the id of the Item

  • amount (Integer) (defaults to: 1)

    the initial amount of the stack.

Since:

  • 0.9.3


11
12
13
14
15
# File 'app/game/item/stack.rb', line 11

def initialize(id, amount = 1)
  @id = id
  @definition = id == -1 ? RuneRb::Database::ItemDefinition[0] : RuneRb::Database::ItemDefinition[id]
  @size = amount
end

Instance Attribute Details

#definitionObject (readonly)

Since:

  • 0.9.3


5
6
7
# File 'app/game/item/stack.rb', line 5

def definition
  @definition
end

#sizeObject

Since:

  • 0.9.3


6
7
8
# File 'app/game/item/stack.rb', line 6

def size
  @size
end

Class Method Details

.dump(stack) ⇒ Object

Returns a serialized dump of the passed Stack object.

Parameters:

Since:

  • 0.9.3


31
32
33
# File 'app/game/item/stack.rb', line 31

def dump(stack)
  Oj.dump({ id: stack.id, amount: stack.size }, mode: :compat, use_as_json: true)
end

.restore(data) ⇒ Object

Restores a serialized dump of a Stack object.

Parameters:

  • data (Hash)

    a serialized dump

Since:

  • 0.9.3


37
38
39
# File 'app/game/item/stack.rb', line 37

def restore(data)
  RuneRb::Game::Item::Stack.new(data[:id], data[:amount])
end

Instance Method Details

#inspectObject

An inspection of the Stack's definition.

Since:

  • 0.9.3


24
25
26
# File 'app/game/item/stack.rb', line 24

def inspect
  RuneRb::GLOBAL[:COLOR].blue("[ITEM:] #{RuneRb::GLOBAL[:COLOR].cyan.bold(@definition[:name])} || [COUNT:] #{RuneRb::GLOBAL[:COLOR].cyan.bold(@size)} || [DESC:] #{RuneRb::GLOBAL[:COLOR].green(@definition[:desc])}")
end

#to_json(*_args) ⇒ String

Returns serialized dump of this Stack.

Returns:

  • (String)

    A dump of this Stack

Since:

  • 0.9.3


19
20
21
# File 'app/game/item/stack.rb', line 19

def to_json(*_args)
  RuneRb::Game::Item::Stack.dump(self)
end