Skip to content

Commit

Permalink
fixes :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oribuin committed Jul 24, 2024
1 parent 57639f0 commit 1a9b8bd
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 84 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group "xyz.oribuin"
version "1.3.1"
version "1.3.2"

java {
toolchain {
Expand Down
102 changes: 57 additions & 45 deletions src/main/java/xyz/oribuin/eternaltags/hook/Expansion.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,77 +27,89 @@ public Expansion(final EternalTags plugin) {
}

@Override
public String onRequest(@Nullable OfflinePlayer offlineUser, @NotNull String params) {
public @Nullable String onPlaceholderRequest(Player player, @NotNull String params) {

// Require a player for these placeholders
if (offlineUser == null)
return "Error: Player is null";
// Regular placeholders :)
if (params.equalsIgnoreCase("total")) return String.valueOf(this.manager.getCachedTags().size());
if (params.equalsIgnoreCase("joined")) return this.joinTags(this.manager.getPlayerTags(player));
if (params.equalsIgnoreCase("unlocked")) return String.valueOf(this.manager.getPlayerTags(player).size());
if (params.equalsIgnoreCase("favorites")) return String.valueOf(this.manager.getUsersFavourites(player.getUniqueId()).size());

// Allow the ability to get any tag from the id
String[] args = params.split("_");
Tag activeTag = this.manager.getUserTag(player);

// Add new specific tags here
if (args.length >= 2) {
String tagId = params.substring(args[0].length() + 1);
Tag tag = this.manager.getTagFromId(tagId);
// Can't use the switch statement here
Tag tag = this.manager.getTagFromId(tagId); // args[1]

if (tag != null) {
return switch (args[0].toLowerCase()) {
case "get" -> this.manager.getDisplayTag(tag, offlineUser, "");
case "get-formatted" -> this.manager.getDisplayTag(tag, offlineUser, this.formattedPlaceholder);
case "has" -> (offlineUser.getPlayer() == null ? "false" : this.manager.canUseTag(offlineUser.getPlayer(), tag) ? "true" : "false");
case "has-unlocked" -> (offlineUser.getPlayer() == null ? "false" : this.manager.canUseTag(offlineUser.getPlayer(), tag) ? Setting.TAG_UNLOCKED_FORMAT.getString() : Setting.TAG_LOCKED_FORMAT.getString());
case "active" -> String.valueOf(this.manager.getOfflineUserTag(offlineUser) == tag);
case "get" -> this.manager.getDisplayTag(tag, player, "");
case "get-formatted" -> this.manager.getDisplayTag(tag, player, this.formattedPlaceholder);
case "has" -> String.valueOf(this.manager.canUseTag(player, tag));
case "has-unlocked" -> this.manager.canUseTag(player, tag) ? Setting.TAG_UNLOCKED_FORMAT.getString() : Setting.TAG_LOCKED_FORMAT.getString();
case "active" -> String.valueOf(activeTag != null && activeTag.getId().equalsIgnoreCase(tag.getId()));
case "description" -> TagsUtils.formatList(tag.getDescription(), Setting.DESCRIPTION_DELIMITER.getString());
default -> "Unknown Placeholder";
default -> null;
};
}
}

// This is the only tag that doesn't require a player
if (params.equalsIgnoreCase("total"))
return String.valueOf(this.manager.getCachedTags().size());
// Return the result of the placeholder
return this.result(params, player, activeTag);
}

Player player = offlineUser.getPlayer();
if (player == null)
return "Error: Player is null";
/**
* Parse all the placeholders and return the result
*
* @param param The placeholder to parse
* @param offlinePlayer The player to parse the placeholder for
* @param tag The tag to parse the placeholder for
*
* @return The result of the placeholder
*/
public String result(String param, OfflinePlayer offlinePlayer, Tag tag) {
if (offlinePlayer == null) return "Error: Player is null"; // Require a player for these placeholders
Player player = offlinePlayer.getPlayer();

Tag activeTag = this.manager.getUserTag(player);
return switch (params.toLowerCase()) {
// Set bracket placeholders to allow \o/ Placeholder Inception \o/
case "tag" -> this.manager.getDisplayTag(activeTag, offlineUser, "");
case "tag_formatted" -> this.manager.getDisplayTag(activeTag, offlineUser, this.formattedPlaceholder);

// We're separating these tags from the other ones because of placeholder inception
case "tag_stripped" -> activeTag != null ? activeTag.getTag() : "";
case "tag_stripped_formatted" -> activeTag != null ? activeTag.getTag() : this.formattedPlaceholder;

// general tag placeholders, unlikely to be used often
case "tag_name" -> activeTag != null ? activeTag.getName() : this.formattedPlaceholder;
case "tag_id" -> activeTag != null ? activeTag.getId() : this.formattedPlaceholder;
case "tag_permission" -> activeTag != null ? activeTag.getPermission() : this.formattedPlaceholder;
case "tag_description" -> activeTag != null ? TagsUtils.formatList(activeTag.getDescription(), Setting.DESCRIPTION_DELIMITER.getString()) : this.formattedPlaceholder;
case "tag_order" -> activeTag != null ? String.valueOf(activeTag.getOrder()) : this.formattedPlaceholder;
case "active" -> String.valueOf(activeTag != null);
case "has-unlocked" -> {
if (activeTag == null)
yield Setting.TAG_LOCKED_FORMAT.getString();

yield this.manager.canUseTag(player, activeTag) ? Setting.TAG_UNLOCKED_FORMAT.getString() : Setting.TAG_LOCKED_FORMAT.getString();
}
if (player == null) return "Error: Player is null"; // Require a player for these placeholders

// These tags are different and dont always want formattedPlaceholder
if (param.equalsIgnoreCase("active")) return String.valueOf(tag != null); // Return true if the tag is not null
if (param.equalsIgnoreCase("tag")) return this.manager.getDisplayTag(tag, player, ""); // Return the tag with the player's tag
if (param.equalsIgnoreCase("tag_stripped")) return tag != null ? tag.getTag() : ""; // Return nothing when the tag is null

// Has unlocked is a double special case
if (param.equalsIgnoreCase("has-unlocked")) {
if (tag == null) return Setting.TAG_LOCKED_FORMAT.getString();

return this.manager.canUseTag(player, tag) ? Setting.TAG_UNLOCKED_FORMAT.getString() : Setting.TAG_LOCKED_FORMAT.getString();
}

// These are the tags that return a number.
case "joined" -> this.joinTags(this.manager.getPlayerTags(offlineUser.getPlayer()));
case "unlocked" -> offlineUser.getPlayer() != null ? String.valueOf(this.manager.getPlayerTags(offlineUser.getPlayer()).size()) : "0";
case "favorites" -> offlineUser.getPlayer() != null ? String.valueOf(this.manager.getUsersFavourites(offlineUser.getUniqueId()).size()) : "0";
if (tag == null) return this.formattedPlaceholder; // Return the formatted placeholder if the tag is null

// Regular tag placeholders
return switch (param) {
case "tag_formatted" -> this.manager.getDisplayTag(tag, player, this.formattedPlaceholder);
case "tag_stripped_formatted" -> tag.getTag();
case "tag_name" -> tag.getName();
case "tag_id" -> tag.getId();
case "tag_permission" -> tag.getPermission();
case "tag_description" -> TagsUtils.formatList(tag.getDescription(), Setting.DESCRIPTION_DELIMITER.getString());
case "tag_order" -> String.valueOf(tag.getOrder());
default -> null;
};

}


/**
* Join all the tags in a single string
*
* @param tags The tags to join
*
* @return The joined tags
*/
public String joinTags(List<Tag> tags) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package xyz.oribuin.eternaltags.listener;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import xyz.oribuin.eternaltags.EternalTags;
import xyz.oribuin.eternaltags.manager.DataManager;
import xyz.oribuin.eternaltags.manager.TagsManager;

import java.util.concurrent.CompletableFuture;

public class PlayerListeners implements Listener {

private final TagsManager manager = EternalTags.getInstance().getManager(TagsManager.class);
private final DataManager dataManager = EternalTags.getInstance().getManager(DataManager.class);

@EventHandler
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onPlayerJoin(PlayerJoinEvent event) {
this.dataManager.loadUser(event.getPlayer().getUniqueId()); // Load the user from the database
this.manager.getUserTag(event.getPlayer()); // Get the user's tag (This will detect default tags or the user's tag)

// Load the user from the database
this.dataManager.loadUser(event.getPlayer().getUniqueId()).thenRun(() -> {
// Update the player's tag
this.manager.getUserTag(event.getPlayer());
});
}

@EventHandler
Expand Down
45 changes: 25 additions & 20 deletions src/main/java/xyz/oribuin/eternaltags/manager/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class DataManager extends AbstractDataManager {

Expand Down Expand Up @@ -183,32 +184,36 @@ public void clearFavourites(UUID uuid) {
*
* @param uuid The player's uuid
*/
public void loadUser(@NotNull UUID uuid) {
TagUser user = new TagUser(uuid);
public CompletableFuture<TagUser> loadUser(@NotNull UUID uuid) {
return CompletableFuture.supplyAsync(() -> {
TagUser user = new TagUser(uuid);

this.async(() -> this.databaseConnector.connect(connection -> {
String selectTag = "SELECT tagID FROM " + this.getTablePrefix() + "tags WHERE player = ?";
this.databaseConnector.connect(connection -> {
String selectTag = "SELECT tagID FROM " + this.getTablePrefix() + "tags WHERE player = ?";

// Load the active tag from the database.
try (PreparedStatement statement = connection.prepareStatement(selectTag)) {
statement.setString(1, uuid.toString());
ResultSet result = statement.executeQuery();
if (result.next()) {
user.setActiveTag(result.getString(1));
// Load the active tag from the database.
try (PreparedStatement statement = connection.prepareStatement(selectTag)) {
statement.setString(1, uuid.toString());
ResultSet result = statement.executeQuery();
if (result.next()) {
user.setActiveTag(result.getString(1));
}
}
}

String favouriteTagsQuery = "SELECT tagID FROM " + this.getTablePrefix() + "favourites WHERE player = ?";
try (PreparedStatement statement = connection.prepareStatement(favouriteTagsQuery)) {
statement.setString(1, uuid.toString());
ResultSet result = statement.executeQuery();
while (result.next()) {
user.getFavourites().add(result.getString(1));
String favouriteTagsQuery = "SELECT tagID FROM " + this.getTablePrefix() + "favourites WHERE player = ?";
try (PreparedStatement statement = connection.prepareStatement(favouriteTagsQuery)) {
statement.setString(1, uuid.toString());
ResultSet result = statement.executeQuery();
while (result.next()) {
user.getFavourites().add(result.getString(1));
}
}
}

this.cachedUsers.put(uuid, user);
}));
this.cachedUsers.put(uuid, user);
});

return user;
});
}

/**
Expand Down
Loading

0 comments on commit 1a9b8bd

Please sign in to comment.