Class: RubyCraft::Chunk

Inherits:
Object
  • Object
show all
Includes:
Enumerable, ZlibHelper
Defined in:
lib/rubycraft/chunk.rb

Overview

Chunks are enumerable over blocks

Constant Summary collapse

Width =
16
Length =
16
Height =
128

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ZlibHelper

#compress, #decompress

Constructor Details

#initialize(nbtData) ⇒ Chunk

Returns a new instance of Chunk.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubycraft/chunk.rb', line 21

def initialize(nbtData)
  name, @nbtBody = nbtData
  bytes = level["Blocks"].value.bytes
  @blocks = matrixfromBytes bytes
  @blocks.each_triple_index do |b, z, x, y|
    b.pos = [z, x, y]
  end
  data = level["Data"].value.bytes.to_a
  @blocks.each_with_index do |b, index|
    v = data[index / 2]
    if index % 2 == 0
      b.data = v & 0xF
    else
      b.data = v >> 4
    end
  end
end

Class Method Details

.fromNbt(bytes) ⇒ Object



17
18
19
# File 'lib/rubycraft/chunk.rb', line 17

def self.fromNbt(bytes)
  new NbtHelper.fromNbt bytes
end

Instance Method Details

#[](z, x, y) ⇒ Object



57
58
59
# File 'lib/rubycraft/chunk.rb', line 57

def [](z, x, y)
  @blocks[z, x, y]
end

#[]=(z, x, y, value) ⇒ Object



61
62
63
# File 'lib/rubycraft/chunk.rb', line 61

def []=(z, x, y, value)
  @blocks[z, x, y] = value
end

#block_map(&block) ⇒ Object

Converts all blocks on data do another type. Gives the block and sets the received name



47
48
49
# File 'lib/rubycraft/chunk.rb', line 47

def block_map(&block)
  each { |b| b.name = yield b }
end

#block_type_map(&block) ⇒ Object

Converts all blocks on data do another type. Gives the block name sets the received name



53
54
55
# File 'lib/rubycraft/chunk.rb', line 53

def block_type_map(&block)
  each { |b| b.name = yield b.name.to_sym }
end

#each(&block) ⇒ Object

Iterates over the blocks



40
41
42
# File 'lib/rubycraft/chunk.rb', line 40

def each(&block)
  @blocks.each &block
end

#exportObject



65
66
67
68
69
70
# File 'lib/rubycraft/chunk.rb', line 65

def export
  level["Data"] = byteArray exportLevelData
  level["Blocks"] = byteArray @blocks.map { |b| b.id }
  level["HeightMap"] = byteArray exportHeightMap
  ["", @nbtBody]
end

#toNbtObject



72
73
74
# File 'lib/rubycraft/chunk.rb', line 72

def toNbt
  NbtHelper.toBytes export
end