Skip to content

Commit

Permalink
Add support for & color code syntax (#5)
Browse files Browse the repository at this point in the history
* Add support for & color code syntax

* Add suicide command
  • Loading branch information
Robin Füllgraf authored Apr 6, 2022
1 parent 16d3c98 commit 3cb1006
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.simplyvanilla.nopluginscommand;

import net.simplyvanilla.nopluginscommand.command.CustomTextCommandExecutor;
import net.simplyvanilla.nopluginscommand.command.SuicideCommandExecutor;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.EventHandler;
Expand All @@ -15,6 +17,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

public final class NoPluginsCommand extends JavaPlugin implements Listener {
Expand Down Expand Up @@ -53,6 +56,15 @@ public void onEnable() {
FileConfiguration config = YamlConfiguration.loadConfiguration(configPath.toFile());
FileConfiguration customTextConfig = YamlConfiguration.loadConfiguration(customTextPath.toFile());

String suicideBroadcast = config.getString("suicide-broadcast");

if (suicideBroadcast != null) {
suicideBroadcast = ChatColor.translateAlternateColorCodes('&', suicideBroadcast.trim());
}

SuicideCommandExecutor suicideCommandExecutor = new SuicideCommandExecutor(suicideBroadcast);
getCommand("suicide").setExecutor(suicideCommandExecutor);

this.commandWhitelist = new HashSet<>(config.getStringList("whitelist"));

getServer().getPluginManager().registerEvents(this, this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.simplyvanilla.nopluginscommand.command;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -61,7 +62,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
lines = lines.subList(startIndex, endIndex);
}

lines.forEach(sender::sendMessage);
lines.stream()
.map(line -> ChatColor.translateAlternateColorCodes('&', line))
.forEach(sender::sendMessage);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.simplyvanilla.nopluginscommand.command;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class SuicideCommandExecutor implements CommandExecutor {

private final String messageToBroadcast;

public SuicideCommandExecutor(String messageToBroadcast) {
this.messageToBroadcast = messageToBroadcast;
}

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("This command is only for players!");
return true;
}

player.setHealth(0);
if (messageToBroadcast != null && !messageToBroadcast.isEmpty()) {
Bukkit.broadcastMessage(messageToBroadcast.replaceAll("%name%", player.getName()));
}
return true;
}

}
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
whitelist:
- info
- shop
# %name% is the name of the player who committed suicide
suicide-broadcst: "&7The player &e%name% &4commited suicide"
4 changes: 4 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ api-version: 1.18
commands:
customtext:
description: Allow for admin defined messages
suicide:
description: Suicide a player
aliases:
- kill

0 comments on commit 3cb1006

Please sign in to comment.