Skip to content

Commit

Permalink
move specific content to the commons module
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Sep 29, 2024
1 parent 9749dd5 commit edc1b88
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 72 deletions.
2 changes: 2 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
}

dependencies {
api(project(":api"))

compileOnly(libs.vital.common)
compileOnly(libs.jetbrains)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.badbones69.crazycrates.api.objects.other;
package com.badbones69.crazycrates.common;

import com.badbones69.crazycrates.common.impl.Settings;
import com.ryderbelserion.vital.common.utils.FileUtil;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import us.crazycrew.crazycrates.CratesProvider;
import us.crazycrew.crazycrates.api.users.UserManager;
Expand All @@ -16,88 +16,59 @@ public class Server implements IServer {
private final File directory;
private final File crates;

private UserManager userManager;
private Options options;

public Server(@NotNull final File directory) {
this.directory = directory;
this.crates = new File(this.directory, "crates");

ConfigManager.load(this.directory);
}

/**
* Loads the plugin.
*/
@ApiStatus.Internal
private UserManager userManager;
private Settings settings;

public void apply() {
this.options = new Options();
ConfigManager.load(this.directory);

this.settings = new Settings();

// Register default provider.
CratesProvider.register(this);
}

@ApiStatus.Internal
public void setUserManager(@NotNull final UserManager userManager) {
if (this.userManager != null) return;

this.userManager = userManager;
}

/**
* Disables the plugin.
*/
@ApiStatus.Internal
public void disable() {
// Unregister default provider.
CratesProvider.unregister();
}

/**
* Reloads the plugin.
*/
@Override
public void reload() {
ConfigManager.refresh();
}

/**
* @return the crates folder
*/
@Override
public @NotNull final File getCrateFolder() {
return this.crates;
}

/**
* @return the data folder
*/
@Override
public @NotNull File getDataFolder() {
return this.directory;
}

/**
* @return the list of files in the crates folder
*/
@Override
public @NotNull final List<String> getCrateFiles() {
return FileUtil.getFiles(getCrateFolder(), ".yml", false);
}

/**
* @return the user manager
*/
@Override
public @NotNull final UserManager getUserManager() {
return this.userManager;
}

/**
* @return available config options
*/
@Override
public @NotNull final ISettings getSettings() {
return this.options;
return this.settings;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.badbones69.crazycrates.api.objects.other;
package com.badbones69.crazycrates.common.impl;

import ch.jalu.configme.SettingsManager;
import com.badbones69.crazycrates.common.config.ConfigManager;
import com.badbones69.crazycrates.common.config.impl.ConfigKeys;
import us.crazycrew.crazycrates.platform.ISettings;
import java.util.List;

public class Options implements ISettings {
public class Settings implements ISettings {

private final SettingsManager config = ConfigManager.getConfig();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.badbones69.crazycrates.common.utils;

import com.badbones69.crazycrates.common.config.ConfigManager;
import com.badbones69.crazycrates.common.config.impl.ConfigKeys;
import org.jetbrains.annotations.NotNull;

public class Methods {

public static String getPrefix() {
return ConfigManager.getConfig().getProperty(ConfigKeys.command_prefix);
}

public static String getPrefix(@NotNull final String msg) {
if (msg.isEmpty()) return "";

return getPrefix() + msg;
}
}
1 change: 0 additions & 1 deletion paper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ dependencies {
}

implementation(project(":common"))
implementation(project(":api"))

compileOnly(libs.excellentcrates)
compileOnly(libs.nightcore)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.badbones69.crazycrates;

import com.badbones69.crazycrates.api.objects.other.Server;
import com.badbones69.crazycrates.common.Server;
import com.badbones69.crazycrates.utils.MiscUtils;
import com.badbones69.crazycrates.commands.CommandManager;
import com.badbones69.crazycrates.listeners.BrokeLocationsListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.badbones69.crazycrates.api.enums.Messages;
import com.badbones69.crazycrates.api.objects.Crate;
import com.badbones69.crazycrates.api.objects.crates.CrateLocation;
import com.badbones69.crazycrates.common.utils.Methods;
import com.badbones69.crazycrates.utils.MsgUtils;
import com.badbones69.crazycrates.commands.crates.types.BaseCommand;
import dev.triumphteam.cmd.bukkit.annotation.Permission;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void set(Player player, @ArgName("crate") @Suggestion("crates") String cr
final Map<String, String> placeholders = new HashMap<>();

placeholders.put("{crate}", crate.getCrateName());
placeholders.put("{prefix}", MsgUtils.getPrefix());
placeholders.put("{prefix}", Methods.getPrefix());

// this has to use sendRichMessage as it is a list.
Messages.created_physical_crate.sendRichMessage(player, placeholders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.badbones69.crazycrates.CrazyCrates;
import com.badbones69.crazycrates.common.config.ConfigManager;
import com.badbones69.crazycrates.common.config.impl.ConfigKeys;
import com.badbones69.crazycrates.common.utils.Methods;
import com.badbones69.crazycrates.managers.BukkitUserManager;
import com.badbones69.crazycrates.managers.events.EventManager;
import com.badbones69.crazycrates.tasks.crates.CrateManager;
Expand Down Expand Up @@ -89,7 +90,7 @@ public void onCrateOpen(CrateOpenEvent event) {
if (!broadcastMessage.isBlank()) {
final String builder = Support.placeholder_api.isEnabled() ? PlaceholderAPI.setPlaceholders(player, broadcastMessage) : broadcastMessage;

this.plugin.getServer().broadcast(AdvUtil.parse(builder.replaceAll("%crate%", fancyName).replaceAll("%prefix%", MsgUtils.getPrefix()).replaceAll("%player%", player.getName())));
this.plugin.getServer().broadcast(AdvUtil.parse(builder.replaceAll("%crate%", fancyName).replaceAll("%prefix%", Methods.getPrefix()).replaceAll("%player%", player.getName())));
}
}

Expand All @@ -103,9 +104,9 @@ public void onCrateOpen(CrateOpenEvent event) {
String builder;

if (Support.placeholder_api.isEnabled() ) {
builder = PlaceholderAPI.setPlaceholders(player, line.replaceAll("%crate%", fileName).replaceAll("%prefix%", MsgUtils.getPrefix()).replaceAll("%player%", player.getName()));
builder = PlaceholderAPI.setPlaceholders(player, line.replaceAll("%crate%", fileName).replaceAll("%prefix%", Methods.getPrefix()).replaceAll("%player%", player.getName()));
} else {
builder = line.replaceAll("%crate%", fileName).replaceAll("%prefix%", MsgUtils.getPrefix()).replaceAll("%player%", player.getName());
builder = line.replaceAll("%crate%", fileName).replaceAll("%prefix%", Methods.getPrefix()).replaceAll("%player%", player.getName());
}

this.plugin.getServer().dispatchCommand(this.plugin.getServer().getConsoleSender(), builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.badbones69.crazycrates.api.events.PlayerPrizeEvent;
import com.badbones69.crazycrates.api.events.PlayerReceiveKeyEvent;
import com.badbones69.crazycrates.api.builders.ItemBuilder;
import com.badbones69.crazycrates.common.utils.Methods;
import com.badbones69.crazycrates.managers.events.EventManager;
import com.badbones69.crazycrates.managers.events.enums.EventType;
import com.ryderbelserion.vital.paper.api.enums.Support;
Expand Down Expand Up @@ -324,7 +325,7 @@ public void onMysteryBoxClick(InventoryClickEvent event) {
if (!broadcastMessage.isBlank()) {
String builder = Support.placeholder_api.isEnabled() ? PlaceholderAPI.setPlaceholders(player, broadcastMessage) : broadcastMessage;

this.plugin.getServer().broadcast(AdvUtil.parse(builder.replaceAll("%crate%", fancyName).replaceAll("%prefix%", MsgUtils.getPrefix()).replaceAll("%player%", player.getName())));
this.plugin.getServer().broadcast(AdvUtil.parse(builder.replaceAll("%crate%", fancyName).replaceAll("%prefix%", Methods.getPrefix()).replaceAll("%player%", player.getName())));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.badbones69.crazycrates.api.enums.Permissions;
import com.badbones69.crazycrates.api.builders.ItemBuilder;
import com.badbones69.crazycrates.api.enums.misc.Files;
import com.badbones69.crazycrates.common.utils.Methods;
import com.ryderbelserion.vital.common.utils.FileUtil;
import com.ryderbelserion.vital.paper.api.enums.Support;
import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable;
Expand Down Expand Up @@ -294,7 +295,7 @@ public static void failedToTakeKey(@NotNull final CommandSender player, @NotNull
"",
" <yellow>-> <light_purple>Not enough keys.",
" <yellow>-> <light_purple>Key is in off hand."
).forEach(line -> player.sendRichMessage(MsgUtils.getPrefix(line)));
).forEach(line -> player.sendRichMessage(Methods.getPrefix(line)));
}
}

Expand Down
26 changes: 2 additions & 24 deletions paper/src/main/java/com/badbones69/crazycrates/utils/MsgUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import com.badbones69.crazycrates.common.config.ConfigManager;
import com.badbones69.crazycrates.common.config.impl.ConfigKeys;
import static com.badbones69.crazycrates.common.utils.Methods.getPrefix;
import static java.util.regex.Matcher.quoteReplacement;

public class MsgUtils {

public static void sendMessage(CommandSender commandSender, @NotNull final String message, final boolean prefixToggle) {
if (message.isEmpty()) return;

String prefix = getPrefix();
final String prefix = getPrefix();

final boolean sendPrefix = !prefix.isEmpty() && prefixToggle;

Expand All @@ -36,25 +35,4 @@ public static void sendMessage(CommandSender commandSender, @NotNull final Strin
commandSender.sendRichMessage(message);
}
}

/**
* Get the prefix.
*
* @return the {@link String}
*/
public static @NotNull String getPrefix() {
return ConfigManager.getConfig().getProperty(ConfigKeys.command_prefix);
}

/**
* Get prefix with message.
*
* @param msg the message
* @return the message with prefix
*/
public static @NotNull String getPrefix(@NotNull final String msg) {
if (msg.isEmpty()) return "";

return getPrefix() + msg;
}
}

0 comments on commit edc1b88

Please sign in to comment.