Class: RubyCraft::Region

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

Overview

Enumerable over chunks

Defined Under Namespace

Classes: RegionWritter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ZlibHelper

#compress, #decompress

Methods included from ByteConverter

#arrayToIO, #bytesToInt, #concat, #intBytes, #pad, #stringToByteArray, #stringToIo, #toByteString

Constructor Details

#initialize(bytes) ⇒ Region

Returns a new instance of Region.



89
90
91
92
93
94
# File 'lib/rubycraft/region.rb', line 89

def initialize(bytes)
  raise "Must be an io" if bytes.kind_of?(String)
  @bytes = bytes
  @chunks = Array.new(32) { Array.new(32) }
  readChunks bytes
end

Class Method Details

.fromFile(filename) ⇒ Object



85
86
87
# File 'lib/rubycraft/region.rb', line 85

def self.fromFile(filename)
  new ByteConverter.stringToByteArray IO.read filename
end

Instance Method Details

#chunk(z, x) ⇒ Object



96
97
98
# File 'lib/rubycraft/region.rb', line 96

def chunk(z, x)
  @chunks[z][x]
end

#cube(z, y, x, opts = {}, &block) ⇒ Object



112
113
114
115
116
# File 'lib/rubycraft/region.rb', line 112

def cube(z, y, x, opts = {}, &block)
  c = ChunkCube.new(self, [z, y, x], opts[:width], opts[:length], opts[:height])
  return c unless block_given?
  c.each &block
end

#each(&block) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/rubycraft/region.rb', line 104

def each(&block)
  @chunks.each do |line|
    line.each do |chunk|
      yield chunk
    end
  end
end

#exportTo(io) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/rubycraft/region.rb', line 118

def exportTo(io)
  output = RegionWritter.new io
  chunks = getChunks
  writeChunkOffsets output, chunks
  output.pad blockSize, dummytimestamp
  writeChunks output, chunks
  output.close
end

#exportToFile(filename) ⇒ Object



127
128
129
# File 'lib/rubycraft/region.rb', line 127

def exportToFile(filename)
  File.open(filename, "wb") { |f| exportTo f }
end

#unloadChunk(z, x) ⇒ Object



100
101
102
# File 'lib/rubycraft/region.rb', line 100

def unloadChunk(z, x)
  @chunks[z][x]._unload
end