Skip to content

Commit

Permalink
Finished Kill Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Devlrxxh committed Jul 14, 2024
1 parent 86bbee0 commit a3e79e5
Show file tree
Hide file tree
Showing 19 changed files with 287 additions and 20 deletions.
6 changes: 6 additions & 0 deletions Plugin/src/main/java/dev/lrxh/neptune/Neptune.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.lrxh.neptune.commands.*;
import dev.lrxh.neptune.configs.ConfigManager;
import dev.lrxh.neptune.configs.impl.SettingsLocale;
import dev.lrxh.neptune.cosmetics.CosmeticManager;
import dev.lrxh.neptune.database.DatabaseManager;
import dev.lrxh.neptune.divisions.DivisionManager;
import dev.lrxh.neptune.duel.command.DuelCommand;
Expand All @@ -39,6 +40,7 @@
import dev.lrxh.neptune.providers.tasks.TaskScheduler;
import dev.lrxh.neptune.queue.QueueManager;
import dev.lrxh.neptune.queue.tasks.QueueCheckTask;
import dev.lrxh.neptune.utils.ServerUtils;
import dev.lrxh.neptune.utils.assemble.Assemble;
import dev.lrxh.neptune.utils.menu.MenuManager;
import dev.lrxh.neptune.utils.menu.listener.MenuListener;
Expand Down Expand Up @@ -80,6 +82,7 @@ public final class Neptune extends JavaPlugin {
private EntityHider entityHider;
private DivisionManager divisionManager;
private DatabaseManager databaseManager;
private CosmeticManager cosmeticManager;

public static Neptune get() {
return instance;
Expand Down Expand Up @@ -110,6 +113,7 @@ private void loadManager() {
this.databaseManager = new DatabaseManager();
if (!isEnabled()) return;

this.cosmeticManager = new CosmeticManager();
this.divisionManager = new DivisionManager();
this.profileManager = new ProfileManager();
this.leaderboardManager = new LeaderboardManager();
Expand All @@ -124,6 +128,8 @@ private void loadManager() {

System.gc();
Runtime.getRuntime().freeMemory();

ServerUtils.sendMessage("&aNeptune has loaded");
}

private void initAPIs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public enum CosmeticsLocale implements IDataAccessor {
LIGHTNING_MATERIAL("KILL-EFFECTS.LIGHTNING.MATERIAL", DataType.STRING, "NETHER_STAR"),
FIREWORKS_DISPLAY_NAME("KILL-EFFECTS.FIREWORKS.DISPLAY-NAME", DataType.STRING, "&bFireworks <selected>"),
FIREWORKS_SLOT("KILL-EFFECTS.FIREWORKS.SLOT", DataType.INT, "12"),
FIREWORKS_MATERIAL("KILL-EFFECTS.FIREWORKS.MATERIAL", DataType.STRING, "FIREWORK_ROCKET");
FIREWORKS_MATERIAL("KILL-EFFECTS.FIREWORKS.MATERIAL", DataType.STRING, "FIREWORK_ROCKET"),
DEFAULT_MESSAGE("KILL_MESSAGES.DEFAULT.DISPLAY_NAME", DataType.STRING, "&7Default"),
DEFAULT_MESSAGE_MATERIAL("KILL_MESSAGES.DEFAULT.MATERIAL", DataType.STRING, "BARRIER"),
DEFAULT_MESSAGE_DESCRIPTION("KILL_MESSAGES.DEFAULT.DESCRIPTION", DataType.STRING_LIST, "&7Default kill message."),
DEFAULT_MESSAGE_SLOT("KILL_MESSAGES.DEFAULT.SLOT", DataType.INT, "10"),
DEFAULT_MESSAGE_MESSAGES("KILL_MESSAGES.DEFAULT.MESSAGES", DataType.STRING_LIST, "&c☠ <player> &7was killed by \uD83D\uDDE1 <killer>");

private final String path;
private final String comment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ public enum MenusLocale implements IDataAccessor {
SETTINGS_COSMETICS_LORE("SETTINGS.SETTINGS.COSMETICS.LORE", DataType.STRING_LIST,
"&7Manage unlocked cosmetics",
"",
" &7Current Cosmetic: &b<kill-effect>",
" &7Current Kill Effect: &b<kill-effect>",
" &7Current Kill Message: &b<kill-message>",
" ",
"&aClick to open cosmetics menu"),
SETTINGS_COSMETICS_SLOT("SETTINGS.SETTINGS.COSMETICS.SLOT", DataType.INT, "15"),
Expand All @@ -313,7 +314,7 @@ public enum MenusLocale implements IDataAccessor {
KILL_EFFECTS_NAME("SETTINGS.COSMETICS.KILL-EFFECTS.NAME", DataType.STRING, "&bKill Effects"),
KILL_EFFECTS_LORE("SETTINGS.COSMETICS.KILL-EFFECTS.LORE", DataType.STRING_LIST, "&7View all available kill effects", " ", "&aClick to open"),
KILL_EFFECTS_MATERIAL("SETTINGS.COSMETICS.KILL-EFFECTS.MATERIAL", DataType.STRING, "REDSTONE"),
KILL_EFFECTS_TITLE("SETTINGS.COSMETICS.KILL-EFFECTS.TITLE", DataType.STRING, "&7Kill Effects"),
KILL_EFFECTS_TITLE("SETTINGS.KILL-EFFECTS.TITLE", DataType.STRING, "&7Kill Effects"),
KILL_EFFECTS_SIZE("SETTINGS.KILL-EFFECTS.SIZE", DataType.INT, "27"),
KILL_EFFECTS_FILTER("SETTINGS.KILL-EFFECTS.FILTER-TYPE", "FILL, BORDER, NONE", DataType.STRING, "FILL"),
KILL_EFFECTS_SELECTED_LORE("SETTINGS.KILL-EFFECTS.SELECTED.LORE", DataType.STRING_LIST,
Expand All @@ -340,7 +341,14 @@ public enum MenusLocale implements IDataAccessor {
" &c&l▶ &cDisabled",
" ",
"&aClick to enable."),
SETTINGS_MENU_SOUNDS_SLOT("SETTINGS.MENU-SOUNDS.SLOT", DataType.INT, "16");
SETTINGS_MENU_SOUNDS_SLOT("SETTINGS.MENU-SOUNDS.SLOT", DataType.INT, "16"),
KILL_MESSAGES_TITLE("SETTINGS.KILL-MESSAGES.TITLE", DataType.STRING, "&7Kill Messages"),
KILL_MESSAGES_SIZE("SETTINGS.KILL-MESSAGES.SIZE", DataType.INT, "27"),
KILL_MESSAGES_FILTER("SETTINGS.KILL-MESSAGES.FILTER-TYPE", "FILL, BORDER, NONE", DataType.STRING, "FILL"),
KILL_MESSAGES_SLOT("SETTINGS.COSMETICS.KILL-MESSAGES.SLOT", DataType.INT, "11"),
KILL_MESSAGES_NAME("SETTINGS.COSMETICS.KILL-MESSAGES.NAME", DataType.STRING, "&bKill Messages"),
KILL_MESSAGES_MATERIAL("SETTINGS.COSMETICS.KILL-MESSAGES.MATERIAL", DataType.STRING, "BOOK"),
KILL_MESSAGES_LORE("SETTINGS.COSMETICS.KILL-MESSAGES.LORE", DataType.STRING_LIST, "&7View all available kill messages", " ", "&aClick to open");

private final String path;
private final String comment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
package dev.lrxh.neptune.cosmetics;

public class CosmeticManager {
import dev.lrxh.neptune.Neptune;
import dev.lrxh.neptune.cosmetics.impl.KillMessagePackage;
import dev.lrxh.neptune.providers.manager.IManager;
import dev.lrxh.neptune.utils.ConfigFile;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class CosmeticManager implements IManager {
public final Map<String, KillMessagePackage> deathMessages;
private final Neptune plugin;

public CosmeticManager() {
this.plugin = Neptune.get();
this.deathMessages = new HashMap<>();
load();
}

public void load() {
FileConfiguration config = plugin.getConfigManager().getCosmeticsConfig().getConfiguration();
if (config.contains("KILL_MESSAGES")) {
for (String deathPackageName : getKeys("KILL_MESSAGES")) {
String path = "KILL_MESSAGES." + deathPackageName + ".";
String displayName = config.getString(path + "DISPLAY_NAME");
Material material = Material.getMaterial(Objects.requireNonNull(config.getString(path + "MATERIAL")));
List<String> description = config.getStringList(path + "DESCRIPTION");
int slot = config.getInt(path + "SLOT");
List<String> messages = config.getStringList(path + "MESSAGES");

deathMessages.put(deathPackageName, new KillMessagePackage(deathPackageName, displayName, material, description, slot, messages));
}
}
}

@Override
public ConfigFile getConfigFile() {
return plugin.getConfigManager().getCosmeticsConfig();
}

public KillMessagePackage getDeathMessagePackage(String packageName) {
if (!deathMessages.containsKey(packageName)) {
return getDefault();
}
return deathMessages.get(packageName);
}

public KillMessagePackage getDefault() {
return deathMessages.get("DEFAULT");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import dev.lrxh.neptune.configs.impl.CosmeticsLocale;
import dev.lrxh.neptune.providers.tasks.NeptuneRunnable;
import lombok.Getter;
import org.bukkit.*;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.FireworkMeta;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.lrxh.neptune.cosmetics.impl;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.bukkit.Material;

import java.security.SecureRandom;
import java.util.List;

@Getter
@AllArgsConstructor
public class KillMessagePackage {
private final SecureRandom secureRandom = new SecureRandom();
private String name;
private String displayName;
private Material material;
private List<String> description;
private int slot;
private List<String> messages;

public String getRandomMessage() {
return messages.get(secureRandom.nextInt(messages.size()));
}

public String permission() {
return "neptune.cosmetics.killmessages." + name.toLowerCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.lrxh.neptune.configs.impl.MenusLocale;
import dev.lrxh.neptune.cosmetics.menu.killEffects.KillEffectsMenu;
import dev.lrxh.neptune.cosmetics.menu.killMessages.KillMessagesMenu;
import dev.lrxh.neptune.utils.menu.Button;
import dev.lrxh.neptune.utils.menu.Filter;
import dev.lrxh.neptune.utils.menu.Menu;
Expand Down Expand Up @@ -42,6 +43,13 @@ public Map<Integer, Button> getButtons(Player player) {
MenusLocale.KILL_EFFECTS_LORE.getStringList(),
Material.valueOf(MenusLocale.KILL_EFFECTS_MATERIAL.getString()),
new KillEffectsMenu()));

buttons.put(MenusLocale.KILL_MESSAGES_SLOT.getInt(),
new CosmeticsManagementButton(
MenusLocale.KILL_MESSAGES_NAME.getString(),
MenusLocale.KILL_MESSAGES_LORE.getStringList(),
Material.valueOf(MenusLocale.KILL_MESSAGES_MATERIAL.getString()),
new KillMessagesMenu()));
return buttons;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dev.lrxh.neptune.cosmetics.menu.killMessages;

import dev.lrxh.neptune.configs.impl.CosmeticsLocale;
import dev.lrxh.neptune.configs.impl.MenusLocale;
import dev.lrxh.neptune.cosmetics.impl.KillMessagePackage;
import dev.lrxh.neptune.profile.Profile;
import dev.lrxh.neptune.utils.ItemBuilder;
import dev.lrxh.neptune.utils.menu.Button;
import lombok.AllArgsConstructor;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;

import java.util.List;

@AllArgsConstructor
public class KillMessageButton extends Button {
private final KillMessagePackage killMessagePackage;

@Override
public ItemStack getButtonItem(Player player) {
Profile profile = plugin.getProfileManager().getByUUID(player.getUniqueId());
if (profile == null) return null;
boolean selected = profile.getSettingData().getKillMessagePackage().equals(killMessagePackage);
List<String> lore;

if (player.hasPermission(killMessagePackage.permission())) {
lore = selected ? MenusLocale.KILL_EFFECTS_SELECTED_LORE.getStringList() : MenusLocale.KILL_EFFECTS_UNSELECTED_LORE.getStringList();
} else {
lore = MenusLocale.KILL_EFFECTS_NO_PERMISSION_LORE.getStringList();
}

return new ItemBuilder(killMessagePackage.getMaterial()).name(killMessagePackage.getDisplayName()
.replace("<selected>", selected ? CosmeticsLocale.SELECTED_DISPLAY_NAME.getString() : ""))
.lore(lore)
.clearFlags()
.build();
}

@Override
public void onClick(Player player, ClickType clickType) {
if (!player.hasPermission(killMessagePackage.permission())) return;
plugin.getProfileManager().getByUUID(player.getUniqueId()).getSettingData().setKillMessagePackage(killMessagePackage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package dev.lrxh.neptune.cosmetics.menu.killMessages;

import dev.lrxh.neptune.configs.impl.MenusLocale;
import dev.lrxh.neptune.cosmetics.impl.KillMessagePackage;
import dev.lrxh.neptune.utils.menu.Button;
import dev.lrxh.neptune.utils.menu.Filter;
import dev.lrxh.neptune.utils.menu.Menu;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;

public class KillMessagesMenu extends Menu {
@Override
public String getTitle(Player player) {
return MenusLocale.KILL_MESSAGES_TITLE.getString();
}

@Override
public int getSize() {
return MenusLocale.KILL_MESSAGES_SIZE.getInt();
}

@Override
public Filter getFilter() {
return Filter.valueOf(MenusLocale.KILL_MESSAGES_FILTER.getString());
}

@Override
public boolean isUpdateOnClick() {
return true;
}

@Override
public Map<Integer, Button> getButtons(Player player) {
Map<Integer, Button> buttons = new HashMap<>();

for (KillMessagePackage killMessagePackage : plugin.getCosmeticManager().deathMessages.values()) {
buttons.put(killMessagePackage.getSlot(), new KillMessageButton(killMessagePackage));
}

return buttons;
}
}
20 changes: 17 additions & 3 deletions Plugin/src/main/java/dev/lrxh/neptune/match/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ public void broadcast(MessagesLocale messagesLocale, Replacement... replacements
forEachSpectator(player -> messagesLocale.send(player.getUniqueId(), replacements));
}

public void broadcast(String message) {
forEachParticipant(participant -> participant.sendMessage(message));

forEachSpectator(player -> player.sendMessage(message));
}

public void checkRules() {
forEachParticipant(participant -> {
if (!(this instanceof FfaFightMatch)) {
Expand Down Expand Up @@ -269,9 +275,17 @@ public void setupParticipants() {
}

public void sendDeathMessage(Participant deadParticipant) {
broadcast(deadParticipant.getDeathCause().getMessagesLocale(),
new Replacement("<player>", deadParticipant.getName()),
new Replacement("<killer>", deadParticipant.getLastAttacker() != null ? deadParticipant.getLastAttacker().getName() : ""));
String deathMessage = deadParticipant.getDeathMessage();

if (deathMessage.isEmpty()) {
broadcast(
deadParticipant.getDeathCause().getMessagesLocale(),
new Replacement("<player>", deadParticipant.getName()),
new Replacement("<killer>", deadParticipant.getLastAttackerName())
);
} else {
broadcast(deathMessage);
}
}

public abstract void end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.entity.Player;

import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

@Data
Expand Down Expand Up @@ -121,4 +122,36 @@ public String getHitsDifference(Participant otherParticipant) {
return CC.color("&e(" + (hits - otherParticipant.getHits()) + ")");
}
}

public String getDeathMessage() {
Profile profile = plugin.getProfileManager().getByUUID(playerUUID);
if (profile == null) {
return "";
}

Match match = profile.getMatch();
if (match == null) {
return "";
}

Participant lastAttacker = getLastAttacker();
if (lastAttacker == null) {
return "";
}

Profile attackerProfile = plugin.getProfileManager().getByUUID(lastAttacker.getPlayerUUID());
if (attackerProfile == null || attackerProfile.getSettingData().getKillMessagePackage() == null) {
return "";
}

return attackerProfile.getSettingData().getKillMessagePackage().getRandomMessage()
.replace("<player>", getName())
.replace("<killer>", getLastAttackerName());
}

public String getLastAttackerName() {
return Optional.ofNullable(getLastAttacker())
.map(Participant::getName)
.orElse("");
}
}
Loading

0 comments on commit a3e79e5

Please sign in to comment.