Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
SpraxDev committed Mar 16, 2024
2 parents 9a9572e + c2dacac commit b10ed2c
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 80 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.songoda</groupId>
<artifactId>EpicHeads</artifactId>
<version>4.1.0</version>
<version>4.1.1</version>

<name>EpicHeads</name>
<description>Allows you and your players to search over 19,000 unique and artistic heads – Perfect for building immersive masterpieces.</description>
Expand Down
52 changes: 26 additions & 26 deletions src/main/java/com/craftaro/epicheads/EpicHeads.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.craftaro.epicheads.commands.CommandUrl;
import com.craftaro.epicheads.database.DataHelper;
import com.craftaro.epicheads.database.migrations._1_InitialMigration;
import com.craftaro.epicheads.database.migrations._2_FixAutoIncrementMigration;
import com.craftaro.epicheads.head.Category;
import com.craftaro.epicheads.head.Head;
import com.craftaro.epicheads.head.HeadManager;
Expand Down Expand Up @@ -56,7 +57,6 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class EpicHeads extends SongodaPlugin {
Expand All @@ -68,6 +68,8 @@ public class EpicHeads extends SongodaPlugin {

private DatabaseConnector databaseConnector;

private boolean doneLoadingHeads = false;

/**
* @deprecated Use {@link #getPlugin(Class)} instead
*/
Expand Down Expand Up @@ -147,7 +149,7 @@ public void onDataLoad() {
this.databaseConnector = new SQLiteConnector(this);
this.getLogger().info("Data handler connected using SQLite.");

initDatabase(new _1_InitialMigration(this));
initDatabase(new _1_InitialMigration(), new _2_FixAutoIncrementMigration());
DataHelper.init(this.dataManager);

Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
Expand Down Expand Up @@ -180,12 +182,8 @@ public void onDataLoad() {

if (storage.containsGroup("local")) {
for (StorageRow row : storage.getRowsByGroup("local")) {
String tagStr = row.get("category").asString();

Optional<Category> tagOptional = this.headManager.getCategories().stream()
.filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst();

Category category = tagOptional.orElseGet(() -> new Category(tagStr));
String categoryName = row.get("category").asString();
Category category = this.headManager.getOrCreateCategoryByName(categoryName);

Head head = new Head(row.get("id").asInt(),
row.get("name").asString(),
Expand Down Expand Up @@ -220,6 +218,8 @@ public void onDataLoad() {
DataHelper.getLocalHeads((heads) -> {
this.headManager.addLocalHeads(heads);
getLogger().info("Loaded " + this.headManager.getHeads().size() + " heads");

this.doneLoadingHeads = true;
});

DataHelper.getDisabledHeads((ids) -> {
Expand Down Expand Up @@ -250,38 +250,34 @@ private void downloadHeads() {
private boolean loadHeads() {
try {
this.headManager.clear();
this.headManager.addCategory(new Category(getLocale()
.getMessage("general.word.latestpack").getMessage(), true));
this.headManager.addCategory(new Category(getLocale().getMessage("general.word.latestpack").getMessage(), true));

JSONParser parser = new JSONParser();
JSONArray jsonArray = (JSONArray) parser.parse(new FileReader(getDataFolder() + "/heads.json"));

for (Object o : jsonArray) {
JSONObject jsonObject = (JSONObject) o;

String categoryStr = (String) jsonObject.get("tags");
Optional<Category> tagOptional = this.headManager.getCategories().stream().filter(t -> t.getName().equalsIgnoreCase(categoryStr)).findFirst();

Category category = tagOptional.orElseGet(() -> new Category(categoryStr));
String headName = (String) jsonObject.get("name");
String headPack = (String) jsonObject.get("pack");
if (headName == null || headName.equals("null") || (headPack != null && headPack.equals("null"))) {
continue;
}

int id = Integer.parseInt((String) jsonObject.get("id"));
String categoryName = (String) jsonObject.get("tags");
Category category = this.headManager.getOrCreateCategoryByName(categoryName);

Head head = new Head(id,
(String) jsonObject.get("name"),
Head head = new Head(
Integer.parseInt((String) jsonObject.get("id")),
headName,
(String) jsonObject.get("url"),
category,
false,
(String) jsonObject.get("pack"),
Byte.parseByte((String) jsonObject.get("staff_picked")));

if (head.getName() == null || head.getName().equals("null")
|| head.getPack() != null && head.getPack().equals("null")) continue;

if (!tagOptional.isPresent())
this.headManager.addCategory(category);
headPack,
Byte.parseByte((String) jsonObject.get("staff_picked"))
);
this.headManager.addHead(head);
}

} catch (IOException | ParseException ex) {
getLogger().warning(() -> {
if (ex instanceof ParseException) {
Expand Down Expand Up @@ -335,4 +331,8 @@ public PlayerManager getPlayerManager() {
public DatabaseConnector getDatabaseConnector() {
return this.databaseConnector;
}

public boolean isDoneLoadingHeads() {
return this.doneLoadingHeads;
}
}
15 changes: 5 additions & 10 deletions src/main/java/com/craftaro/epicheads/commands/CommandAdd.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class CommandAdd extends AbstractCommand {
private final EpicHeads plugin;
Expand All @@ -37,19 +36,15 @@ protected ReturnType runCommand(CommandSender sender, String... args) {
return ReturnType.FAILURE;
}

List<Category> categories = headManager.getCategories().stream().filter(category1 -> category1.getName().equals(categoryStr)).collect(Collectors.toList());

Category category = categories.isEmpty() ? new Category(categoryStr) : categories.get(0);

Category category = headManager.getOrCreateCategoryByName(categoryStr);
Head head = new Head(headManager.getNextLocalId(), name, url, category, true, null, (byte) 0);
headManager.addLocalHead(head);
DataHelper.createLocalHead(head);

this.plugin.getLocale().getMessage("command.add.success")
.processPlaceholder("name", name).sendPrefixedMessage(sender);
if (categories.isEmpty()) {
this.plugin.reloadConfig();
}
this.plugin.getLocale()
.getMessage("command.add.success")
.processPlaceholder("name", name)
.sendPrefixedMessage(sender);
return ReturnType.SUCCESS;
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/craftaro/epicheads/database/DataHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.craftaro.core.database.DataManager;
import com.craftaro.core.database.DatabaseConnector;
import com.craftaro.epicheads.EpicHeads;
import com.craftaro.epicheads.head.Category;
import com.craftaro.epicheads.head.Head;
import com.craftaro.epicheads.players.EPlayer;
import com.craftaro.third_party.org.jooq.Query;
Expand Down Expand Up @@ -120,6 +119,7 @@ public static void getLocalHeads(Consumer<List<Head>> callback) {
List<Head> heads = new ArrayList<>();
dslContext.select()
.from(DSL.table(getTablePrefix() + "local_heads"))
.orderBy(DSL.field("id asc"))
.fetch()
.forEach(record -> {
int id = record.get(DSL.field("id", Integer.class));
Expand All @@ -130,7 +130,7 @@ public static void getLocalHeads(Consumer<List<Head>> callback) {
Head head = new Head(id,
name,
url,
new Category(categoryString),
EpicHeads.getInstance().getHeadManager().getOrCreateCategoryByName(categoryString),
true,
null,
(byte) 0);
Expand Down Expand Up @@ -205,4 +205,8 @@ public static void saveAllPlayers() {
});
});
}

public static boolean isInitialized() {
return dataManager != null;
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package com.craftaro.epicheads.database.migrations;

import com.craftaro.core.database.DataMigration;
import com.craftaro.core.database.MySQLConnector;
import com.craftaro.epicheads.EpicHeads;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class _1_InitialMigration extends DataMigration {
private final EpicHeads epicHeads;

public _1_InitialMigration(EpicHeads epicHeads) {
public _1_InitialMigration() {
super(1);
this.epicHeads = epicHeads;
}

@Override
public void migrate(Connection connection, String tablePrefix) throws SQLException {
String autoIncrement = this.epicHeads.getDatabaseConnector() instanceof MySQLConnector ? " AUTO_INCREMENT" : "";

// Create player profiles
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE IF NOT EXISTS " + tablePrefix + "players (" +
Expand All @@ -31,7 +24,7 @@ public void migrate(Connection connection, String tablePrefix) throws SQLExcepti
// Create local heads table
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE IF NOT EXISTS " + tablePrefix + "local_heads (" +
"id INTEGER PRIMARY KEY" + autoIncrement + ", " +
"id INTEGER PRIMARY KEY AUTO_INCREMENT, " +
"category VARCHAR(48) NOT NULL, " +
"name VARCHAR(64) NOT NULL," +
"url VARCHAR(256) " +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.craftaro.epicheads.database.migrations;

import com.craftaro.core.database.DataMigration;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class _2_FixAutoIncrementMigration extends DataMigration {
public _2_FixAutoIncrementMigration() {
super(2);
}

@Override
public void migrate(Connection connection, String tablePrefix) throws SQLException {
try (Statement statement = connection.createStatement()) {
statement.execute("ALTER TABLE " + tablePrefix + "local_heads MODIFY COLUMN id INTEGER AUTO_INCREMENT");
}
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/craftaro/epicheads/gui/GUIOverview.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ private void showPage() {
int numTemplates = this.plugin.getHeadManager().getCategories().size();
this.pages = (int) Math.floor(numTemplates / 21.0);

List<Category> categories = this.plugin.getHeadManager().getCategories().stream().skip((this.page - 1) * (this.rows - 1) * 9).limit((this.rows - 1) * 9)
List<Category> categories = this.plugin.getHeadManager()
.getCategories()
.stream()
.skip((this.page - 1) * (this.rows - 1) * 9)
.limit((this.rows - 1) * 9)
.collect(Collectors.toList());

int add = 0;
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/com/craftaro/epicheads/head/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.craftaro.epicheads.EpicHeads;

import java.util.Objects;

public class Category {
private final String name;
private boolean latestPack = false;
private final boolean latestPack;

public Category(String name) {
this.name = name;
this(name, false);
}

public Category(String name, boolean latestPack) {
Expand All @@ -31,4 +33,26 @@ public int getCount() {
.filter(head -> head.getCategory() == this)
.count());
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}

Category category = (Category) obj;

String comparableThisName = this.name != null ? this.name.toLowerCase() : null;
String comparableCategoryName = category.name != null ? category.name.toLowerCase() : null;
return this.latestPack == category.latestPack && Objects.equals(comparableThisName, comparableCategoryName);
}

@Override
public int hashCode() {
String name = this.name != null ? this.name.toLowerCase() : null;
return Objects.hash(name, this.latestPack);
}
}
35 changes: 32 additions & 3 deletions src/main/java/com/craftaro/epicheads/head/HeadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,24 @@ public List<Head> getHeadsByQuery(String query) {
if (!category.getName().equalsIgnoreCase(query)) {
continue;
}
return getHeads().stream().filter(head -> head.getCategory() == category).collect(Collectors.toList());

return getHeads()
.stream()
.filter(head -> head.getCategory() == category)
.collect(Collectors.toList());
}
}
return result;
}

public List<Head> getHeadsByCategory(Category category) {
return getHeads().stream().filter(head -> head.getCategory() == category).collect(Collectors.toList());
List<Head> list = new ArrayList<>();
for (Head head : getHeads()) {
if (head.getCategory().equals(category)) {
list.add(head);
}
}
return list;
}

public List<Head> getHeads() {
Expand Down Expand Up @@ -122,14 +131,34 @@ public void removeLocalHead(Head head) {
}

public Category addCategory(Category category) {
this.registeredCategories.add(category);
if (!this.registeredCategories.contains(category)) {
this.registeredCategories.add(category);
}
return category;
}

public List<Category> getCategories() {
return Collections.unmodifiableList(this.registeredCategories);
}

public Category getCategory(String name) {
for (Category category : this.registeredCategories) {
if (category.getName().equalsIgnoreCase(name)) {
return category;
}
}
return null;
}

public Category getOrCreateCategoryByName(String name) {
for (Category category : this.registeredCategories) {
if (category.getName().equalsIgnoreCase(name)) {
return category;
}
}
return addCategory(new Category(name));
}

public void clear() {
this.registeredHeads.clear();
this.localRegisteredHeads.clear();
Expand Down
Loading

0 comments on commit b10ed2c

Please sign in to comment.