How to convert a string to a Location Object Bukkit Plugin - bukkit

I was wondering how I can convert a string (Loaded from the config). Convert into a Location object. I've already tried this code:
private Location StringToLocation(String input, Main plugin) {
Location world = new Location(Bukkit.getWorld(plugin.getConfig().getString("world")), 0, 0, 0);
int index;
int LengteString;
String[] myCoordinaat = { "", "", "" };
for (int iLoop = 0; iLoop < 2; iLoop++)
{
index = input.indexOf(",");
LengteString = input.length() - index;
myCoordinaat[iLoop] = input.substring(index, LengteString);
input = input.substring(0, index + 1);
}
myCoordinaat[2]=input;
Bukkit.getLogger().info("x: " + myCoordinaat[0] + " y:" + myCoordinaat[1] + " z:" + myCoordinaat[2]);
world.setX(Double.parseDouble(myCoordinaat[0]));
world.setY(Double.parseDouble(myCoordinaat[1]));
world.setZ(Double.parseDouble(myCoordinaat[2]));
return world;
}
Thanks in advance!

There is a very easy solution to your problem that doesn't even involve you parsing the location yourself.
//Save location
getConfig().set("path.to.location", loc);
saveConfig();
//Load location
Location location = (Location) getConfig().get("path.to.location");
You can basically store the object in the config and then restore it. Just keep in mind that the world needs to be loaded for the Location to be parsed.

Related

Building a css grid with javascript: creating elements, storing them in arrays and append them to other elements

I recently build an masonry gallery with html and css. I used the display: grid; property to make it look like so. Now I am trying to create randomized layouts. Therefor I want to create elements with classes. I want to append / elements to the elements, store those elements inside an array and later append them inside my "masonry" . I put in different console.logs to see whats happening, but I get some either weird or undefined returns. Javascript can be as tricky as it can be fun for beginners, so I hope you guys can help me out.
Thank you all. :)
//arrays and variables
const figures = [];
let newFigures = "";
const divs = [];
let newDivs = "";
function makeCells(){
for(let i = 0; i < 33; i++){
newFigure = document.createElement("figure");
figures[i] = newFigure.classList.add("cell", "cell--" + i);
newDiv = document.createElement("div");
divs[i] = newDiv.setAttribute("id", i);
console.log("log1: " + newFigure.classList);
console.log("log2: " + figures[i]);
}
console.log("log3: " + divs);
console.log("log4: " + figures);
console.log("log5: " + divs);
for(let i=0; i<3; i++){
figures[i] = newFigure.appendChild(newDiv);
}
console.log("figures = " + figures);
console.log("divs = " + divs);
let z = {};
for(let i=0; i<3; i++){
z = figures[i];
document.getElementById("masonry").appendChild(z);
}
console.log(document.getElementById("masonry"));
}
Here is a picture of the corresponding console.logs.
Console Logs
This works as intended:
const figures = [];
let newFigure = "";
const divs = [];
let newDiv = "";
function makeCells(){
for(let i = 0; i < 33; i++){
newFigure = document.createElement("figure");
newDiv = document.createElement("div");
newFigure.appendChild(newDiv);
document.getElementById("masonry").appendChild(newFigure);
figures[i] = newFigure.classList.add("cell", "cell--" + i);
}
console.log(document.getElementById("masonry"));
}
Sorry to bother you guys.
I don't like this line of code, mainly because the add() method does not have a return value.
figures[i] = newFigure.classList.add("cell", "cell--" + i);
I would split it into two statements.
newFigure.classList.add("cell", "cell--" + i);
figures.push(newFigure);

C++ - EncryptMessage not encrypting the correct data

I've been following this tutorial for SSL/TLS online (well its more of reading the guys source code and following along) but I've hit a bumpy road with this EncryptMessage part because it pushes the data out of the way and encrypts the wrong info.
The pbloBuffer that I send it is:
GET / HTTP/1.1\r\n
HOST: www.google.com\r\n\r\n
But when I do pbMessage = pbloBuffer + Sizes.cbHeader; I end up with (even the microsoft websites says to do this)
1\r\n
HOST: www.google.com\r\n\r\n
Now pbMessage is the code above, and that's inserted under SECBUFFER_DATA so it's not even getting the full data. From what I understand SECBUFFER_DATA is the "user" data that the Webserver will decode and process.
Can you find out how to fix this and properly send the encrypted data?
Full source: (This code is experimental as I am trying to understand it before I makes changes)
int Adaptify::EncryptSend(PBYTE pbloBuffer, int Size) {
SECURITY_STATUS scRet{ 0 };
SecBufferDesc Message{ 0 };
SecBuffer Buffers[4]{ 0 };
DWORD cbMessage = 0, cbData = 0;
PBYTE pbMessage = nullptr;
SecPkgContext_StreamSizes Sizes = { 0 };
QueryContextAttributesW(&hContext, SECPKG_ATTR_STREAM_SIZES, &Sizes);
pbMessage = pbloBuffer + Sizes.cbHeader;
cbMessage = (DWORD)strlen((const char*)pbMessage);
Buffers[0].BufferType = SECBUFFER_STREAM_HEADER;
Buffers[0].cbBuffer = Sizes.cbHeader;
Buffers[0].pvBuffer = pbloBuffer;
Buffers[1].BufferType = SECBUFFER_DATA;
Buffers[1].pvBuffer = pbMessage;
Buffers[1].cbBuffer = cbMessage;
Buffers[2].BufferType = SECBUFFER_STREAM_TRAILER;
Buffers[2].cbBuffer = Sizes.cbTrailer;
Buffers[2].pvBuffer = pbMessage + cbMessage;
Buffers[3].BufferType = SECBUFFER_EMPTY;
Buffers[3].cbBuffer = SECBUFFER_EMPTY;
Buffers[3].pvBuffer = SECBUFFER_EMPTY;
Message.cBuffers = 4;
Message.pBuffers = Buffers;
Message.ulVersion = SECBUFFER_VERSION;
scRet = EncryptMessage(&hContext, 0, &Message, 0);
if (send(hSocket, (const char*)pbloBuffer, Buffers[0].cbBuffer + Buffers[1].cbBuffer + Buffers[2].cbBuffer, 0) < 0) {
MessageBox(0, L"Send error", 0, 0);
}
return 0;
}
first - you need call QueryContextAttributesW only once after InitializeSecurityContextW return SEC_E_OK - no sense call it every time, when you need send data. and save result. say inherit your class from SecPkgContext_StreamSizes - class Adaptify : SecPkgContext_StreamSizes; and call on end handshake (once) QueryContextAttributesW(&hContext, SECPKG_ATTR_STREAM_SIZES, this);
about send send data - in your case Buffers[1].pvBuffer of course must point to your actual data pbloBuffer but not to pbloBuffer + Sizes.cbHeader code can be like this:
int Adaptify::EncryptSend(PBYTE pbloBuffer, int Size) {
SECURITY_STATUS ss = SEC_E_INSUFFICIENT_MEMORY;
if (PBYTE Buffer = new BYTE[cbHeader + Size + cbTrailer]) {
memcpy(Buffer + cbHeader, pbloBuffer, Size);
SecBuffer sb[4] = {
{ cbHeader, SECBUFFER_STREAM_HEADER, Buffer},
{ Size, SECBUFFER_DATA, Buffer + cbHeader},
{ cbTrailer, SECBUFFER_STREAM_TRAILER, Buffer + cbHeader + Size},
};
SecBufferDesc sbd = {
SECBUFFER_VERSION, 4, sb
};
if (SEC_E_OK == (ss = ::EncryptMessage(this, 0, &sbd, 0)))) {
if (SOCKET_ERROR == send(hSocket, (const char*)Buffer, sb[0].cbBuffer+sb[1].cbBuffer+sb[2].cbBuffer+sb[3].cbBuffer, 0))
ss = WSAGetLastError();
}
delete [] Buffer;
}
return ss;
}
so you need allocate new buffer with cbHeader + Size + cbTrailer size (wher Size is your actual message Size and copy your message at Buffer + cbHeader

Display Unread Missed calls in android listView

I want to get the details of the unread missed calls list in my android app.
I came up with this code but this shows the whole list of my missed calls not only those which are unread.
lv= (ListView)findViewById(R.id.listView1);
List<String> lst=new ArrayList<String>();
String miss_read="read";
String unread_condition=miss_read+"=0";
String[] projection = {CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME,CallLog.Calls.TYPE};
//String[] projection = {Integer.parseInt(CallLog.Calls.MISSED_TYPE};
//String where = (CallLog.Calls.TYPE+CallLog.Calls.NEW+"="+CallLog.Calls.MISSED_TYPE);
String where = (CallLog.Calls.NEW+" = "+"new"+" AND "+CallLog.Calls.TYPE+" = "+CallLog.Calls.MISSED_TYPE);
Cursor c = this.getContentResolver().query(CallLog.Calls.CONTENT_URI,projection,where, null, null);
int i=0;
if(c.moveToFirst())
{
do
{
if(CallLog.Calls.IS_READ!="is_read")
lst.add(c.getString(0)+"not read");
//i++;
}while(c.moveToNext());
}
ArrayAdapter<String> adpt=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lst);
lv.setAdapter(adpt);
does anybody provide me the solution for my problem...??
Try this.
String[] projection = new String[] { CallLog.Calls.NUMBER,
CallLog.Calls.TYPE, CallLog.Calls.NEW };
String where = (CallLog.Calls.NEW + " = " + "1" + " AND "
+ CallLog.Calls.TYPE + " = " + CallLog.Calls.MISSED_TYPE);
Cursor c = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,
projection, where, null, null);

unhandled exception Access violation writing location in Visual Studio

Is there a way in Visual Studio 2005 to find out which pointer or variable is causing this access violation? I tried to run in debug mode and break when it happens. By looking at the call stacks, it happens in the end of the function (see below). Could using try/catch be able to find out which pointer it is?
EDIT:
Posting my code:
There is a Qt line edit and a checkbox in my application. Toggling the checkbox would switch the data format in the line edit. Like 3'b111 <==> 3'h7. Below is the callback function that is connected to the checkbox stateChanged signal. The exception happens in the end of function, when destructing local variables.
// switch hex/binary format. 15'h0000 <==> 15'b000000000000000
void switchDataFormat(int checkState) {
QLineEdit* writeRegLE = this->getWriteRegLineEdit();
string oldText = writeRegLE->text().toStdString();
string newText = "";
int maxLength;
string regLengthText = oldText.substr(0, oldText.find('\''));
string regValueText = oldText.substr(oldText.find('\'')+2);
int regLength = this->getRegLength();
if (checkState == Qt::Unchecked) {
// switch to binary format
maxLength = regLengthText.size() + 2 + regLength;
string binaryText;
for (int i = 0; i < regValueText.size(); ++i) {
binaryText += hexToBinary(regValueText[i]);
}
newText = regLengthText + "'b" + binaryText.substr(binaryText.size()-regLength); // trimming leading zeros to fit regLength
}
else {
// switch to hex format
maxLength = regLengthText.size() + 2 + regLength/4 + 1;
newText = regLengthText + "'h";
// zero filling to 4*n bits
if (regLength%4 != 0) regValueText = string(regLength%4,'0') + regValueText;
for (int i = 0; i < regValueText.size(); i+=4) {
newText += binaryToHex(regValueText.substr(i,4));
}
}
writeRegLE->setMaxLength(maxLength);
writeRegLE->setText(QString::fromUtf8(newText.c_str()));
}

Ncurses menu with linked string

Id like to have linked string in my ncurses menu to something like:
/bin
/hello
/home
...
And i have vector of components named w_files which have variable name (bin, hello, home, ...) and when i do this:
chdir(w_actDir.c_str());
this->selected = 0;
unsigned int n_choices = w_files.size();
my_items = (ITEM **)calloc(n_choices+1, sizeof(ITEM *));
for(unsigned int i = 0; i < n_choices; ++i){
string toShow = w_files[i]->getName();
my_items[i] = new_item(toShow.c_str(), "");
}
my_menu = new_menu((ITEM**)my_items);
set_menu_mark(my_menu, "");
set_menu_win(my_menu, this->w_window);
set_menu_format(my_menu, LINES-5, 1);
int center = COLS/2;
set_menu_sub(my_menu, derwin(this->w_window, LINES-5, center-5, 3, 1));
post_menu(my_menu);
wrefresh(this->w_window);
its ok, result looks:
bin
hello
home
...
But when change line string toShow = w_files[i]->getName(); to string toShow = "/" + w_files[i]->getName();
Result is:
Can anybody help me, please?
Thank you.
Actually, after posting the comment I had an idea for an answer - the safest way would be appending to toShow string.
Code sample:
string toShow = "/";
toShow.append(w_files[i]->getName());