Skip to content

Commit 084b77e

Browse files
author
Artemis
committed
Small refactors and consistency checks
1 parent 37ec01a commit 084b77e

10 files changed

Lines changed: 69 additions & 80 deletions

File tree

include/mcpp/block.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace mcpp {
2424
};
2525

2626

27-
//using script to extract ids from https://minecraft-ids.grahamedgecombe.com/
27+
// Using script to extract ids from https://minecraft-ids.grahamedgecombe.com/
2828

2929
/**
3030
* Struct of static block objects that allows for a "search" of sorts, callable using Blocks::TYPE
@@ -213,10 +213,8 @@ namespace mcpp {
213213
static constexpr BlockType COBBLESTONE_MONSTER_EGG = BlockType(97, 1);
214214
static constexpr BlockType STONE_BRICK_MONSTER_EGG = BlockType(97, 2);
215215
static constexpr BlockType MOSSY_STONE_BRICK_MONSTER_EGG = BlockType(97, 3);
216-
static constexpr BlockType CRACKED_STONE_BRICK_MONSTER_EGG = BlockType(97,
217-
4);
218-
static constexpr BlockType CHISELED_STONE_BRICK_MONSTER_EGG = BlockType(97,
219-
5);
216+
static constexpr BlockType CRACKED_STONE_BRICK_MONSTER_EGG = BlockType(97, 4);
217+
static constexpr BlockType CHISELED_STONE_BRICK_MONSTER_EGG = BlockType(97, 5);
220218
static constexpr BlockType STONE_BRICKS = BlockType(98);
221219
static constexpr BlockType MOSSY_STONE_BRICKS = BlockType(98, 1);
222220
static constexpr BlockType CRACKED_STONE_BRICKS = BlockType(98, 2);
@@ -313,14 +311,12 @@ namespace mcpp {
313311
static constexpr BlockType WHITE_STAINED_GLASS_PANE = BlockType(160);
314312
static constexpr BlockType ORANGE_STAINED_GLASS_PANE = BlockType(160, 1);
315313
static constexpr BlockType MAGENTA_STAINED_GLASS_PANE = BlockType(160, 2);
316-
static constexpr BlockType LIGHT_BLUE_STAINED_GLASS_PANE = BlockType(160,
317-
3);
314+
static constexpr BlockType LIGHT_BLUE_STAINED_GLASS_PANE = BlockType(160, 3);
318315
static constexpr BlockType YELLOW_STAINED_GLASS_PANE = BlockType(160, 4);
319316
static constexpr BlockType LIME_STAINED_GLASS_PANE = BlockType(160, 5);
320317
static constexpr BlockType PINK_STAINED_GLASS_PANE = BlockType(160, 6);
321318
static constexpr BlockType GRAY_STAINED_GLASS_PANE = BlockType(160, 7);
322-
static constexpr BlockType LIGHT_GRAY_STAINED_GLASS_PANE = BlockType(160,
323-
8);
319+
static constexpr BlockType LIGHT_GRAY_STAINED_GLASS_PANE = BlockType(160, 8);
324320
static constexpr BlockType CYAN_STAINED_GLASS_PANE = BlockType(160, 9);
325321
static constexpr BlockType PURPLE_STAINED_GLASS_PANE = BlockType(160, 10);
326322
static constexpr BlockType BLUE_STAINED_GLASS_PANE = BlockType(160, 11);

include/mcpp/connection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace mcpp {
99
std::string lastSent;
1010
static const std::string FailedCommandResponse;
1111

12-
[[nodiscard]] static bool checkCommandFailed(const std::string& result) ;
12+
[[nodiscard]] static bool checkCommandFailed(const std::string& result);
1313

1414
static std::string resolveHostname(const std::string& hostname);
1515

@@ -31,7 +31,7 @@ namespace mcpp {
3131

3232
ss << prefix << "(";
3333

34-
// iterate over args pack
34+
// Iterate over args pack
3535
((ss << args << ','), ...);
3636
// Remove trailing comma
3737
ss.seekp(-1, std::ios_base::end);

include/mcpp/entity.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#pragma once
22

33
#include <string>
4+
45
namespace mcpp {
56
class Entity {
67
public:
7-
constexpr Entity(int id, const char* name = nullptr):id(id), name(name){};
8-
bool operator==(Entity &other) const{
8+
constexpr Entity(int id, const char *name = nullptr) : id(id), name(name) {};
9+
10+
bool operator==(Entity& other) const {
911
return this->id == other.id;
1012
}
13+
1114
private:
1215
int id;
13-
const char* name;
16+
const char *name;
1417
};
1518

1619

include/mcpp/mcpp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <string_view>
44
#include <memory>
5+
#include <vector>
56
#include "connection.h"
67
#include "block.h"
78
#include "util.h"
@@ -20,6 +21,7 @@ namespace mcpp {
2021
unflattenHeightsArray(const Coordinate& loc1,
2122
const Coordinate& loc2,
2223
const std::vector<int>& inVector);
24+
2325
public:
2426
/**
2527
* Represents the main endpoint for interaction with the minecraft world.
@@ -80,7 +82,7 @@ namespace mcpp {
8082
*/
8183
BlockType getBlockWithData(const Coordinate& loc);
8284

83-
//TODO: specify the way to iterate through the returned list in correct order
85+
// TODO: specify the way to iterate through the returned list in correct order
8486
/**
8587
* Returns a vector of the BlockTypes at the requested cuboid. Be careful with order of iteration when
8688
* parsing the returned data.

include/mcpp/util.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#pragma once
22

3-
#include <vector>
4-
5-
#include "block.h"
6-
73
namespace mcpp {
84
/**
95
* Represented using integers since sub-unit coordinates are not of particular relevance. Allows for

src/block.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include "../include/mcpp/block.h"
22

33
namespace mcpp {
4-
bool BlockType::operator==(const BlockType &other) const {
4+
bool BlockType::operator==(const BlockType& other) const {
55
return this->id == other.id && this->data == other.data;
66
}
77

88
BlockType BlockType::withData(int newData) const {
9-
return { this->id, newData };
9+
return {this->id, newData};
1010
}
1111
}

src/connection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace mcpp {
1313
uint16_t port) {
1414
std::string ipAddress = resolveHostname(address_str);
1515

16-
// using std libs only to avoid dependency on socket lib
16+
// Using std libs only to avoid dependency on socket lib
1717
socketHandle = socket(AF_INET, SOCK_STREAM, 0);
1818
if (socketHandle == -1) {
1919
throw std::runtime_error("Failed to create socket.");

src/mcpp.cpp

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#include "../include/mcpp/mcpp.h"
2-
#include "../include/mcpp/util.h"
3-
4-
#include <string_view>
52
#include <string>
63
#include <vector>
74

@@ -41,20 +38,20 @@ namespace mcpp {
4138
}
4239

4340
Coordinate MinecraftConnection::getPlayerPosition() {
44-
//TODO remove currently required empty string that ensures formatting
41+
// TODO remove currently required empty string that ensures formatting
4542
std::string returnString = conn->sendReceiveCommand("player.getPos", "");
4643
std::vector<int> parsedInts;
4744
splitCommaStringToInts(returnString, parsedInts);
4845
return Coordinate(parsedInts[0], parsedInts[1], parsedInts[2]);
4946
}
5047

51-
//TODO tile variants of getPos and setPos
48+
// TODO tile variants of getPos and setPos
5249

5350

5451
void MinecraftConnection::setBlock(const Coordinate& loc,
5552
const BlockType& blockType) {
5653
conn->sendCommand("world.setBlock", loc.x, loc.y, loc.z, blockType.id,
57-
blockType.data);
54+
blockType.data);
5855
}
5956

6057

@@ -64,13 +61,13 @@ namespace mcpp {
6461
auto [x, y, z] = loc1;
6562
auto [x2, y2, z2] = loc2;
6663
conn->sendCommand("world.setBlocks", x, y, z, x2, y2, z2, blockType.id,
67-
blockType.data);
64+
blockType.data);
6865
}
6966

7067

7168
BlockType MinecraftConnection::getBlock(const Coordinate& loc) {
7269
std::string returnValue = conn->sendReceiveCommand("world.getBlock",
73-
loc.x, loc.y, loc.z);
70+
loc.x, loc.y, loc.z);
7471
return {std::stoi(returnValue)};
7572
}
7673

@@ -81,7 +78,7 @@ namespace mcpp {
8178
std::vector<int> parsedInts;
8279
splitCommaStringToInts(returnString, parsedInts);
8380

84-
//data and id
81+
// Data and id
8582
return {parsedInts[0], parsedInts[1]};
8683
}
8784

@@ -90,10 +87,10 @@ namespace mcpp {
9087
MinecraftConnection::getBlocks(const Coordinate& loc1,
9188
const Coordinate& loc2) {
9289
std::string returnValue = conn->sendReceiveCommand("world.getBlocks",
93-
loc1.x, loc1.y,
94-
loc1.z,
95-
loc2.x, loc2.y,
96-
loc2.z);
90+
loc1.x, loc1.y,
91+
loc1.z,
92+
loc2.x, loc2.y,
93+
loc2.z);
9794
std::stringstream ss(returnValue);
9895
std::string container;
9996
std::vector<BlockType> returnVector;
@@ -106,15 +103,15 @@ namespace mcpp {
106103
}
107104

108105

109-
std::vector<std::vector<std::vector<BlockType>>>
106+
std::vector<std::vector<std::vector<BlockType>>>
110107
MinecraftConnection::getBlocksWithData(
111108
const Coordinate& loc1, const Coordinate& loc2) {
112109
std::string returnValue = conn->sendReceiveCommand(
113110
"world.getBlocksWithData",
114111
loc1.x, loc1.y, loc1.z,
115112
loc2.x, loc2.y, loc2.z);
116113

117-
// received in format 1,2;1,2;1,2 where 1,2 is a block of type 1 and data 2
114+
// Received in format 1,2;1,2;1,2 where 1,2 is a block of type 1 and data 2
118115
std::vector<BlockType> result;
119116
std::stringstream stream(returnValue);
120117

@@ -141,17 +138,17 @@ namespace mcpp {
141138

142139
int MinecraftConnection::getHeight(int x, int z) {
143140
std::string returnValue = conn->sendReceiveCommand("world.getHeight", x,
144-
z);
141+
z);
145142
return stoi(returnValue);
146143
}
147144

148145

149-
std::vector<std::vector<int>>
146+
std::vector<std::vector<int>>
150147
MinecraftConnection::getHeights(const Coordinate& loc1,
151-
const Coordinate& loc2) {
148+
const Coordinate& loc2) {
152149
std::string returnValue = conn->sendReceiveCommand("world.getHeights",
153-
loc1.x, loc1.z,
154-
loc2.x, loc2.z);
150+
loc1.x, loc1.z,
151+
loc2.x, loc2.z);
155152

156153
// Returned in format "1,2,3,4,5"
157154
std::vector<int> returnVector;
@@ -164,56 +161,53 @@ namespace mcpp {
164161
std::vector<std::vector<std::vector<BlockType>>>
165162
MinecraftConnection::unflattenBlocksArray(const Coordinate& loc1,
166163
const Coordinate& loc2,
167-
const std::vector<BlockType>& inVector)
168-
{
169-
// initialise empty vector of correct size and shape
164+
const std::vector<BlockType>& inVector) {
165+
// Initialise empty vector of correct size and shape
170166
int y_len = abs(loc2.y - loc1.y) + 1;
171167
int x_len = abs(loc2.x - loc1.x) + 1;
172168
int z_len = abs(loc2.z - loc1.z) + 1;
173-
std::vector<std::vector<std::vector<BlockType>>> returnVector (
174-
y_len,
175-
std::vector<std::vector<BlockType>> (
176-
x_len,
177-
std::vector<BlockType> (
178-
z_len,
179-
BlockType(0)
169+
std::vector<std::vector<std::vector<BlockType>>> returnVector(
170+
y_len,
171+
std::vector<std::vector<BlockType>>(
172+
x_len,
173+
std::vector<BlockType>(
174+
z_len,
175+
BlockType(0)
176+
)
180177
)
181-
)
182178
);
183179

184180
int index = 0;
185-
for (int y=0; y < y_len; y++) {
186-
for (int x=0; x < x_len; x++) {
187-
for (int z=0; z < z_len; z++)
188-
{
181+
for (int y = 0; y < y_len; y++) {
182+
for (int x = 0; x < x_len; x++) {
183+
for (int z = 0; z < z_len; z++) {
189184
returnVector[y][x][z] = inVector[index];
190185
index++;
191186
}
192187
}
193188
}
194189

195-
return returnVector;
190+
return returnVector;
196191
}
197192

198193
std::vector<std::vector<int>>
199-
MinecraftConnection::unflattenHeightsArray(const Coordinate& loc1, const Coordinate& loc2, const std::vector<int>& inVector)
200-
{
201-
//initialise empty vector of correct size and shape
194+
MinecraftConnection::unflattenHeightsArray(const Coordinate& loc1, const Coordinate& loc2,
195+
const std::vector<int>& inVector) {
196+
// Initialise empty vector of correct size and shape
202197
int x_len = abs(loc2.x - loc1.x) + 1;
203198
int z_len = abs(loc2.z - loc1.z) + 1;
204199

205-
std::vector<std::vector<int>> returnVector (
206-
x_len,
207-
std::vector<int> (
208-
z_len,
209-
0
210-
)
200+
std::vector<std::vector<int>> returnVector(
201+
x_len,
202+
std::vector<int>(
203+
z_len,
204+
0
205+
)
211206
);
212207

213208
int index = 0;
214-
for (int x=0; x < x_len; x++) {
215-
for (int z=0; z < z_len; z++)
216-
{
209+
for (int x = 0; x < x_len; x++) {
210+
for (int z = 0; z < z_len; z++) {
217211
returnVector[x][z] = inVector[index];
218212
index++;
219213
}

src/util.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "../include/mcpp/util.h"
22

3-
#include <cstdlib>
4-
53
namespace mcpp {
64

75
Coordinate::Coordinate(int x, int y, int z) {

0 commit comments

Comments
 (0)