Class: CraftingTable::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/crafting_table/item.rb

Overview

A class representing a single item. An item, in this case, can be any block, tool, loot etc.

Since:

  • 0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, item_id, damage_value = 0) ⇒ Item

Create a new Item.

Examples:

Spruce Wood

item = Item.new('Spruce Wood', 17, 1)

Glass

item = Item.new('Glass', 20, :any)

Parameters:

  • name (String)

    The item’s name.

  • damage_value (Integer, Symbol) (defaults to: 0)

    The item’s damage / meta value.

  • item_id (Integer)

    The item’s ID.

Since:

  • 0.1



24
25
26
# File 'lib/crafting_table/item.rb', line 24

def initialize(name, item_id, damage_value = 0)
  @name, @item_id, @damage_value = name, item_id, damage_value
end

Instance Attribute Details

#damage_valueObject (readonly)

Since:

  • 0.1



11
12
13
# File 'lib/crafting_table/item.rb', line 11

def damage_value
  @damage_value
end

#item_idObject (readonly)

Since:

  • 0.1



11
12
13
# File 'lib/crafting_table/item.rb', line 11

def item_id
  @item_id
end

#nameObject (readonly)

Since:

  • 0.1



11
12
13
# File 'lib/crafting_table/item.rb', line 11

def name
  @name
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare two items for equality.

Two items are considered equal, if their name, item ID and damage value are equal.

Parameters:

  • other (Item)

    Item which to compare for equality.

Returns:

  • (Boolean)

    Whether the two items are equal.

Since:

  • 0.1



34
35
36
# File 'lib/crafting_table/item.rb', line 34

def ==(other)
  name == other.name && item_id == other.item_id && damage_value == other.damage_value
end

#hashInteger

Return reasonable hash value of this item.

Returns:

  • (Integer)

    Hash of item’s name, id and damage value.

Since:

  • 0.2



45
46
47
# File 'lib/crafting_table/item.rb', line 45

def hash
  [name, item_id, damage_value].hash
end

#identifierArray<Integer>

Return a unique identifier of this item. The identifier is an array, containing its ID and damage value

Returns:

  • (Array<Integer>)

    Unique identifier of this item.

Since:

  • 0.2



55
56
57
# File 'lib/crafting_table/item.rb', line 55

def identifier
  [item_id, damage_value]
end