minecraft_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ minecraft_api
2
+ ==================
3
+
4
+ An API for the Raspberry Pi version of minecraft
data/lib/block_type.rb ADDED
@@ -0,0 +1,91 @@
1
+ class BlockType
2
+
3
+ @@all_block_types = {}
4
+
5
+ attr_reader :id
6
+
7
+ def initialize(id)
8
+ @id = id
9
+ @@all_block_types[id] = self
10
+ end
11
+
12
+ def self.find(id)
13
+ @@all_block_types[id]
14
+ end
15
+
16
+ AIR = BlockType.new(0)
17
+ STONE = BlockType.new(1)
18
+ GRASS = BlockType.new(2)
19
+ DIRT = BlockType.new(3)
20
+ COBBLESTONE = BlockType.new(4)
21
+ WOOD_PLANKS = BlockType.new(5)
22
+ SAPLING = BlockType.new(6)
23
+ BEDROCK = BlockType.new(7)
24
+ WATER_FLOWING = BlockType.new(8)
25
+ WATER_STATIONARY = BlockType.new(9)
26
+ LAVA_FLOWING = BlockType.new(10)
27
+ LAVA_STATIONARY = BlockType.new(11)
28
+ SAND = BlockType.new(12)
29
+ GRAVEL = BlockType.new(13)
30
+ GOLD_ORE = BlockType.new(14)
31
+ IRON_ORE = BlockType.new(15)
32
+ COAL_ORE = BlockType.new(16)
33
+ WOOD = BlockType.new(17)
34
+ LEAVES = BlockType.new(18)
35
+ GLASS = BlockType.new(20)
36
+ LAPIS_LAZULI_ORE = BlockType.new(21)
37
+ LAPIS_LAZULI_BLOCK = BlockType.new(22)
38
+ SANDSTONE = BlockType.new(24)
39
+ BED = BlockType.new(26)
40
+ COBWEB = BlockType.new(30)
41
+ GRASS_TALL = BlockType.new(31)
42
+ WOOL = BlockType.new(35)
43
+ FLOWER_YELLOW = BlockType.new(37)
44
+ FLOWER_CYAN = BlockType.new(38)
45
+ MUSHROOM_BROWN = BlockType.new(39)
46
+ MUSHROOM_RED = BlockType.new(40)
47
+ GOLD_BLOCK = BlockType.new(41)
48
+ IRON_BLOCK = BlockType.new(42)
49
+ STONE_SLAB_DOUBLE = BlockType.new(43)
50
+ STONE_SLAB = BlockType.new(44)
51
+ BRICK_BLOCK = BlockType.new(45)
52
+ TNT = BlockType.new(46)
53
+ BOOKSHELF = BlockType.new(47)
54
+ MOSS_STONE = BlockType.new(48)
55
+ OBSIDIAN = BlockType.new(49)
56
+ TORCH = BlockType.new(50)
57
+ FIRE = BlockType.new(51)
58
+ STAIRS_WOOD = BlockType.new(53)
59
+ CHEST = BlockType.new(54)
60
+ DIAMOND_ORE = BlockType.new(56)
61
+ DIAMOND_BLOCK = BlockType.new(57)
62
+ CRAFTING_TABLE = BlockType.new(58)
63
+ FARMLAND = BlockType.new(60)
64
+ FURNACE_INACTIVE = BlockType.new(61)
65
+ FURNACE_ACTIVE = BlockType.new(62)
66
+ DOOR_WOOD = BlockType.new(64)
67
+ LADDER = BlockType.new(65)
68
+ STAIRS_COBBLESTONE = BlockType.new(67)
69
+ DOOR_IRON = BlockType.new(71)
70
+ REDSTONE_ORE = BlockType.new(73)
71
+ SNOW = BlockType.new(78)
72
+ ICE = BlockType.new(79)
73
+ SNOW_BLOCK = BlockType.new(80)
74
+ CACTUS = BlockType.new(81)
75
+ CLAY = BlockType.new(82)
76
+ SUGAR_CANE = BlockType.new(83)
77
+ FENCE = BlockType.new(85)
78
+ GLOWSTONE_BLOCK = BlockType.new(89)
79
+ BEDROCK_INVISIBLE = BlockType.new(95)
80
+ STONE_BRICK = BlockType.new(98)
81
+ GLASS_PANE = BlockType.new(102)
82
+ MELON = BlockType.new(103)
83
+ FENCE_GATE = BlockType.new(107)
84
+ GLOWING_OBSIDIAN = BlockType.new(246)
85
+ NETHER_REACTOR_CORE = BlockType.new(247)
86
+
87
+ def ==(another_block_type)
88
+ @id == another_block_type.id
89
+ end
90
+
91
+ end
@@ -0,0 +1,79 @@
1
+ require 'socket'
2
+ require 'io/wait'
3
+
4
+ class MinecraftApi
5
+
6
+ def initialize(host = 'localhost', port = 4711)
7
+ @socket = TCPSocket.new host, port
8
+ end
9
+
10
+ # The other api's have a method like this
11
+ # I haven't seen it be invoked yet. Perhaps it is not needed
12
+ def drain()
13
+ while @socket.ready?
14
+ puts "**** READ A CHARACTER [#{@socket.gets}]"
15
+ end
16
+ end
17
+
18
+ def send(data)
19
+ drain()
20
+ @socket.puts "#{data}"
21
+ end
22
+
23
+ def send_and_receive(data)
24
+ send(data)
25
+ @socket.gets.chomp
26
+ end
27
+
28
+ def get_block_type(x,y,z)
29
+ response = send_and_receive("world.getBlock(#{x},#{y},#{z})")
30
+ BlockType.find(response.to_i)
31
+ end
32
+
33
+ def set_block(x,y,z,block_type)
34
+ send("world.setBlock(#{x},#{y},#{z},#{block_type.id})")
35
+ end
36
+
37
+ def set_blocks(x1,y1,z1,x2,y2,z2,block_type)
38
+ send("world.setBlocks(#{x1},#{y1},#{z1},#{x2},#{y2},#{z2},#{block_type.id})")
39
+ end
40
+
41
+ def get_height(x,z)
42
+ send_and_receive("world.getHeight(#{x},#{z})").to_i
43
+ end
44
+
45
+ def checkpoint_save
46
+ send("word.checkpoint.save()")
47
+ end
48
+
49
+ def checkpoint_restore
50
+ send("word.checkpoint.restore()")
51
+ end
52
+
53
+ def chat_post(message)
54
+ send("chat.post(#{message})")
55
+ end
56
+
57
+ def camera_mode(mode,x=nil,y=nil,z=nil)
58
+ case mode
59
+ when :normal then send("camera.mode.setNormal()")
60
+ when :third_person then send("camera.mode.setThirdPerson()")
61
+ when :fixed then send("camera.mode.setFixed()")
62
+ when :position then send("camera.mode.setPos(#{x},#{y},#{z})")
63
+ else raise RuntimeError.new("valid camera settings are: :normal, :third_person, :fixed, and :position")
64
+ end
65
+ end
66
+
67
+ def player_set_tile(x,y,z)
68
+ send("player.setTile(#{x},#{y},#{z})")
69
+ end
70
+
71
+ def player_get_tile
72
+ response = send_and_receive("player.getTile()")
73
+ response.split(',').map { |s| s.to_i }
74
+ end
75
+
76
+ def close
77
+ @socket.close
78
+ end
79
+ end
@@ -0,0 +1,12 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ Gem::Specification.new do |s|
3
+ s.name = 'minecraft_api'
4
+ s.description = 'An API for the Raspberry Pi version of Minecraft'
5
+ s.version = '0.0.1'
6
+ s.date = '2013-01-25'
7
+ s.summary = 'Minecraft API'
8
+ s.homepage = 'https://github.com/nhajratw/minecraft_api'
9
+ s.authors = ["Nayan Hajratwala"]
10
+ s.email = ["nayan@chikli.com"]
11
+ s.files = `git ls-files`.split("\n")
12
+ end
@@ -0,0 +1,10 @@
1
+ require 'block_type'
2
+
3
+ describe 'BlockType' do
4
+
5
+ it 'is equivalent to another block type with the same id' do
6
+ bt1 = BlockType.new(0)
7
+ bt2 = BlockType.new(0)
8
+ bt1.should eq(bt2)
9
+ end
10
+ end
@@ -0,0 +1,92 @@
1
+ require 'minecraft_api'
2
+
3
+ describe "Minecraft server" do
4
+
5
+ before(:each) do
6
+ @mcapi = MinecraftApi.new('10.0.1.129', 4711)
7
+ end
8
+
9
+ after(:each) do
10
+ @mcapi.close
11
+ end
12
+
13
+ it "returns a 'Fail' when a bad command is sent" do
14
+ response = @mcapi.send_and_receive 'junk'
15
+ response.should eq('Fail')
16
+ end
17
+
18
+ it "can create a block" do
19
+ @mcapi.set_block(0,0,0,BlockType::WOOD)
20
+ block_type = @mcapi.get_block_type(0,0,0)
21
+ block_type.should eq(BlockType::WOOD)
22
+ end
23
+
24
+ it "can create a block of a different type" do
25
+ @mcapi.set_block(0,0,0,BlockType::SAND)
26
+ block_type = @mcapi.get_block_type(0,0,0)
27
+ block_type.should eq(BlockType::SAND)
28
+ end
29
+
30
+ it "can create multiple blocks" do
31
+ @mcapi.set_blocks(0,0,0,2,2,2,BlockType::SAND)
32
+ # need to figure out assertion here.
33
+ # The assertions on each vector come too fast and end up returning
34
+ # types of 0.
35
+ end
36
+
37
+ it "can get the height of the world" do
38
+ y = @mcapi.get_height(5,5)
39
+ y.should eq(2)
40
+ end
41
+
42
+ it "can checkpoint save" do
43
+ @mcapi.checkpoint_save
44
+ end
45
+
46
+ it "can checkpoint restore" do
47
+ @mcapi.checkpoint_restore
48
+ end
49
+
50
+ it "can send chats" do
51
+ @mcapi.chat_post("hello")
52
+ end
53
+
54
+ describe "Camera API" do
55
+ it "can be set to normal" do
56
+ @mcapi.camera_mode(:normal)
57
+ end
58
+
59
+ it "can be set to third person" do
60
+ @mcapi.camera_mode(:third_person)
61
+ end
62
+
63
+ it "can be set to fixed" do
64
+ @mcapi.camera_mode(:fixed)
65
+ end
66
+
67
+ it "can be set to a certain position" do
68
+ @mcapi.camera_mode(:position,10,20,30)
69
+ end
70
+
71
+ it "throws an error when an invalid mode is specified" do
72
+ expect {
73
+ @mcapi.camera_mode(:fake)
74
+ }.to raise_error(RuntimeError, "valid camera settings are: :normal, :third_person, :fixed, and :position")
75
+ end
76
+ end
77
+
78
+ describe "Player API" do
79
+ it "can put the player on a particular tile" do
80
+ @mcapi.player_set_tile(20,40,60)
81
+ vector = @mcapi.player_get_tile
82
+ vector.should eq([20,40,60])
83
+ end
84
+
85
+ it "can put the player somewhere else" do
86
+ @mcapi.player_set_tile(-5,10,15)
87
+ vector = @mcapi.player_get_tile
88
+ vector.should eq([-5,10,15])
89
+ end
90
+ end
91
+
92
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minecraft_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nayan Hajratwala
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: An API for the Raspberry Pi version of Minecraft
15
+ email:
16
+ - nayan@chikli.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - README.md
23
+ - lib/block_type.rb
24
+ - lib/minecraft_api.rb
25
+ - minecraft_api.gemspec
26
+ - spec/block_type_spec.rb
27
+ - spec/minecraft_api_spec.rb
28
+ homepage: https://github.com/nhajratw/minecraft_api
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.23
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Minecraft API
52
+ test_files: []