Skip to content

Commit

Permalink
✨ random head button. Closes #54
Browse files Browse the repository at this point in the history
Took 33 minutes
  • Loading branch information
kiranhart committed Jan 8, 2025
1 parent 33027ae commit 44aa6ce
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/main/java/ca/tweetzy/skulls/guis/MainGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import ca.tweetzy.skulls.Skulls;
import ca.tweetzy.skulls.api.enums.BaseCategory;
import ca.tweetzy.skulls.api.enums.ViewMode;
import ca.tweetzy.skulls.api.interfaces.Skull;
import ca.tweetzy.skulls.guis.abstraction.SkullsBaseGUI;
import ca.tweetzy.skulls.model.SkullItem;
import ca.tweetzy.skulls.model.StringHelper;
Expand Down Expand Up @@ -115,6 +116,34 @@ public boolean onResult(String string) {
click.manager.showGUI(click.player, new PlayerHeadGUI(this, Skulls.getPlayerManager().findOrCreate(click.player)));
});

if (Settings.RANDOM_HEAD_BUTTON_ENABLED.getBoolean())
setButton(Settings.GUI_MAIN_ITEMS_RANDOM_HEAD_SLOT.getInt(), QuickItem.of(SkullItem.get("skulls:5171"))
.name(TranslationManager.string(Translations.GUI_MAIN_ITEMS_RANDOM_HEAD_NAME))
.lore(TranslationManager.list(Translations.GUI_MAIN_ITEMS_RANDOM_HEAD_LORE, "price", String.format("%,.2f", Settings.RANDOM_HEAD_BUTTON_PRICE.getDouble())))
.make(), click -> {

if (!Skulls.getPlayerManager().playerCanClaim(player)) {
return;
}

final double price = player.hasPermission("skulls.freeskulls") ? 0 : Settings.RANDOM_HEAD_BUTTON_PRICE.getDouble();
final Skull skull = Skulls.getSkullManager().getRandomAllowedSkull(click.player);

if (price <= 0) {
player.getInventory().addItem(skull.getItemStack());
Common.tell(player, TranslationManager.string(Translations.RECEIVED_RANDOM_SKULL, "skull_name", skull.getName())); return;
}

if (!Skulls.getEconomyManager().has(player, price)) {
Common.tell(player, TranslationManager.string(Translations.NO_MONEY));
return;
}

Skulls.getEconomyManager().withdraw(player, price);
player.getInventory().addItem(skull.getItemStack());
Common.tell(player, TranslationManager.string(Translations.RECEIVED_RANDOM_SKULL, "skull_name", skull.getName()));
});

setButton(Settings.GUI_MAIN_ITEMS_FAVOURITES_SLOT.getInt(), QuickItem.of(SkullItem.get("skulls:39696"))
.name(TranslationManager.string(Translations.GUI_MAIN_ITEMS_FAVOURITES_NAME))
.lore(TranslationManager.list(Translations.GUI_MAIN_ITEMS_FAVOURITES_LORE))
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/ca/tweetzy/skulls/manager/SkullManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import lombok.Setter;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -110,7 +111,14 @@ public Skull getRandomSkull() {
return enabledSkulls.get(random.nextInt(enabledSkulls.size()));
}

public List<Skull> getSkullsBySearch(Player player, String phrase) {
public Skull getRandomAllowedSkull(Player player) {
final List<Skull> enabledSkulls = getSkulls().values().stream().filter(skull -> player.hasPermission("skulls.category." + BaseCategory.getById(skull.getCategory()).getId().toLowerCase().replace(" ", "").replace("&", "")) && BaseCategory.getById(skull.getCategory()).isEnabled() && !skull.isBlocked()).toList();
return enabledSkulls.get(random.nextInt(enabledSkulls.size()));
}

public List<Skull> getSkullsBySearch(Player player, String phraseOriginal) {
String phrase = ChatColor.stripColor(phraseOriginal);

int id = -1;
if (phrase.startsWith("id:")) {
if (NumberUtils.isNumber(phrase.split(":")[1])) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/ca/tweetzy/skulls/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public final class Settings extends FlightSettings {
public static final ConfigEntry GENERAL_USAGE_REQUIRES_NO_PERM = create("general usage requires no permission", false, "If true, no permission is required to use except for admin stuff");
public static final ConfigEntry TELL_OP_PATREON_LINK = create("tell ops patron on join", true);
public static final ConfigEntry SKULL_TRACKING = create("track skull placement", true, "If disabled skulls will no longer drop in creative");
public static final ConfigEntry RANDOM_HEAD_BUTTON_ENABLED = create("random head button.enabled", false, "If enabled players can click to receive a random head (only from categories they have access too)");
public static final ConfigEntry RANDOM_HEAD_BUTTON_PRICE = create("random head button.price", 1.0, "The price for using the random head button");

public static final ConfigEntry CATEGORIES_ALPHABET_ENABLED = create("enabled categories.alphabet", true);
public static final ConfigEntry CATEGORIES_ANIMALS_ENABLED = create("enabled categories.animals", true);
Expand All @@ -57,6 +59,7 @@ public final class Settings extends FlightSettings {
public static final ConfigEntry CATEGORIES_PLANTS_ENABLED = create("enabled categories.plants", true);
public static final ConfigEntry CATEGORIES_PLAYER_HEADS_ENABLED = create("enabled categories.player heads", false);


public static final ConfigEntry PLAYER_HEAD_NAME = create("player head.name", "&e%player_name%");
public static final ConfigEntry PLAYER_HEAD_DROP = create("player head.drop enabled", true);
public static final ConfigEntry PLAYER_HEAD_DROP_CHANCE = create("player head.drop chance", 50);
Expand All @@ -79,7 +82,7 @@ public final class Settings extends FlightSettings {
public static final ConfigEntry GUI_MAIN_ITEMS_SEARCH_SLOT = create("gui.main.items.search.slot", 41, "-1 to disable it");
public static final ConfigEntry GUI_MAIN_ITEMS_CUSTOM_CATEGORIES_SLOT = create("gui.main.items.custom categories.slot", 38, "-1 to disable it");
public static final ConfigEntry GUI_MAIN_ITEMS_PLAYER_HEADS_SLOT = create("gui.main.items.player heads.slot", 39, "-1 to disable it");

public static final ConfigEntry GUI_MAIN_ITEMS_RANDOM_HEAD_SLOT = create("gui.main.items.random head.slot", 40, "-1 to disable or turn off completely under 'random head button'");

public static final ConfigEntry GUI_MAIN_ITEMS_ALPHABET_SLOT = create("gui.main.items.alphabet.slot", 11);
public static final ConfigEntry GUI_MAIN_ITEMS_ANIMALS_SLOT = create("gui.main.items.animals.slot", 12);
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/ca/tweetzy/skulls/settings/Translations.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public Translations(@NonNull JavaPlugin plugin) {
public static final TranslationEntry NO_SKULL_INFO = create("misc.no skull info", "&cCould not determine ID of that skull");
public static final TranslationEntry SKULL_TITLE = create("skull.name", "&e%skull_name%");
public static final TranslationEntry ID_TAKEN = create("misc.id taken", "&cThat category id is already in use!");
public static final TranslationEntry LOADING = create("misc.loading", "&cPlease wait a bit longer, still loading heads.");
public static final TranslationEntry CLAIM_DELAY = create("misc.claim delay", "&cYou can claim another head in &7(&e%time_difference%&7)");
public static final TranslationEntry RECEIVED_RANDOM_SKULL = create("misc.received random skull", "&aYou received a random skull &F(&E%skull_name%&f)");


public static final TranslationEntry ALPHABET = create("categories.alphabet", "Alphabet");
Expand Down Expand Up @@ -174,6 +174,17 @@ public Translations(@NonNull JavaPlugin plugin) {
"&e&lClick &8» &7To view your favourites"
);

public static final TranslationEntry GUI_MAIN_ITEMS_RANDOM_HEAD_NAME = create("gui.main.items.random head.name", "<GRADIENT:DD5E89>&lRandom Head</GRADIENT:fbc7d4>");

public static final TranslationEntry GUI_MAIN_ITEMS_RANDOM_HEAD_LORE = create("gui.main.items.random head.lore",
"&7Used to receive a random head from",
"&7a random category that you have access too.",
"",
"&7Cost: &a$%price%",
"",
"&e&lClick &8» &7To receive a random head"
);

public static final TranslationEntry GUI_MAIN_ITEMS_SEARCH_NAME = create("gui.main.items.search.name", "<GRADIENT:DD5E89>&lSearch</GRADIENT:fbc7d4>");

public static final TranslationEntry GUI_MAIN_ITEMS_SEARCH_LORE = create("gui.main.items.search.lore",
Expand Down

0 comments on commit 44aa6ce

Please sign in to comment.