Skip to content

Commit

Permalink
add global broadcast to CrateExample.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Sep 9, 2024
1 parent 14385f6 commit 8983eb8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public class Crate {

private @NotNull final SettingsManager config = ConfigManager.getConfig();

private boolean broadcastToggle = false;
private List<String> broadcastMessages = new ArrayList<>();
private String broadcastPermission = "";

/**
* @param name The name of the crate.
* @param crateType The crate type of the crate.
Expand Down Expand Up @@ -124,6 +128,11 @@ public Crate(@NotNull final String name,
this.requiredKeys = requiredKeys;
this.prizeMessage = prizeMessage;
this.prizeCommands = prizeCommands;

this.broadcastToggle = this.file.getBoolean("Crate.Settings.Broadcast.Toggle", false);
this.broadcastMessages = this.file.getStringList("Crate.Settings.Broadcast.Messages");
this.broadcastPermission = this.file.getString("Crate.Settings.Broadcast.Permission", "");

this.prizes = prizes;
this.crateType = crateType;
this.previewToggle = file.getBoolean("Crate.Preview.Toggle", false);
Expand Down Expand Up @@ -220,7 +229,19 @@ public final boolean isPreviewTierToggle() {
public @NotNull final AbstractCrateManager getManager() {
return this.manager;
}


public final boolean isBroadcastToggle() {
return this.broadcastToggle;
}

public final String getBroadcastPermission() {
return this.broadcastPermission;
}

public final List<String> getBroadcastMessages() {
return this.broadcastMessages;
}

/**
* Set the preview lines for a Crate.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,25 +297,35 @@ public final boolean hasPermission(@NotNull final Player player) {
}

public void broadcast(final Crate crate) {
if (!this.broadcast) return;

final String fancyName = crate.getCrateName();
final String prizeName = getPrizeName();
final String strippedName = getStrippedName();

if (this.broadcast) {
this.plugin.getServer().getOnlinePlayers().forEach(player -> {
if (!this.broadcastPermission.isEmpty() && player.hasPermission(this.broadcastPermission)) return;

this.broadcastMessages.forEach(message -> sendMessage(player, message, fancyName));
});

return;
}

this.plugin.getServer().getOnlinePlayers().forEach(player -> {
if (!this.broadcastPermission.isEmpty() && player.hasPermission(this.broadcastPermission)) return;

this.broadcastMessages.forEach(message -> player.sendMessage(AdvUtil.parse(message, new HashMap<>() {{
put("%player%", player.getName());
put("%crate%", fancyName);
put("%reward%", prizeName);
put("%maxpulls%", String.valueOf(maxPulls));
put("%reward_stripped%", strippedName);
}}, player)));
if (!crate.getBroadcastPermission().isEmpty() && player.hasPermission(crate.getBroadcastPermission())) return;

crate.getBroadcastMessages().forEach(message -> sendMessage(player, message, fancyName));
});
}

private void sendMessage(final Player player, final String message, final String fancyName) {
player.sendMessage(AdvUtil.parse(message, new HashMap<>() {{
put("%player%", player.getName());
put("%crate%", fancyName);
put("%reward%", getPrizeName());
put("%maxpulls%", String.valueOf(getMaxPulls()));
put("%reward_stripped%", getStrippedName());
}}, player));
}

private @NotNull ItemBuilder display() {
ItemBuilder builder = new ItemBuilder();

Expand Down
11 changes: 11 additions & 0 deletions paper/src/main/resources/crates/CrateExample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ Crate:
# A default command if the prize doesn't have any commands
# i.e. Commands: [] or the value isn't there.
Prize-Commands: []
# Global Settings
Settings:
# Broadcast a message to the server
Broadcast:
# If the messages should be sent.
Toggle: false
# The messages to broadcast.
Messages:
- '<red>%player% won the prize <yellow>%reward%.'
# If the player has this permission, they don't get the broadcast.
Permission: 'your_permission'
# Item the crate is in the GUI
Item: "diamond"
# The custom model data of the item, -1 is disabled.
Expand Down

0 comments on commit 8983eb8

Please sign in to comment.