Skip to content

Instantly share code, notes, and snippets.

@Cryptite
Last active September 25, 2019 17:20
Show Gist options
  • Save Cryptite/07c450716c14f69f9e5ce240623c7072 to your computer and use it in GitHub Desktop.
Save Cryptite/07c450716c14f69f9e5ce240623c7072 to your computer and use it in GitHub Desktop.
Location lookup
import java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import java.util.concurrent.TimeUnit
class Location {
public String world;
public double x, y, z;
Location(String string) {
if (string == null) return;
String[] elems = string.split(",");
world = elems[0];
x = Double.parseDouble(elems[1]);
y = Double.parseDouble(elems[2]);
z = Double.parseDouble(elems[3]);
}
@Override
int hashCode() {
return Objects.hash(world, x, y, z);
}
@Override
boolean equals(Object other) {
if (!(other instanceof Location)) {
return false;
}
Location o = (Location) other;
return x == o.x && y == o.y && z == o.z && world == o.world;
}
}
Location testBlock = new Location("south,5576,175,1707");
try {
long now = System.nanoTime();
Class.forName("org.sqlite.JDBC");
Connection dbConnection = DriverManager.getConnection("jdbc:sqlite:C:/Users/tmiller/Documents/Loka/pts/plugins/TheArtifact/trees.db");
Set<Location> placedBlocks = new HashSet<>();
PreparedStatement psql = dbConnection.prepareStatement("SELECT * FROM trees");
ResultSet result = psql.executeQuery();
while (result.next()) {
String location = String.format("%s,%d,%d,%d",
result.getString(4),
result.getInt(1),
result.getInt(2),
result.getInt(3));
placedBlocks.add(new Location(location));
}
psql.close();
println "Loaded " + placedBlocks.size() + " blocks in " + TimeUnit.MILLISECONDS.convert(System.nanoTime() - now, TimeUnit.NANOSECONDS) + "ms";
now = System.nanoTime();
println "Does exist? " + placedBlocks.contains(testBlock) + " checked in " + (System.nanoTime() - now) + " nanos";
dbConnection.close();
} catch (SQLException | ClassNotFoundException e) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment