Skip to content

Commit

Permalink
New chat decoration
Browse files Browse the repository at this point in the history
also improved kill command interceptor
  • Loading branch information
btarg committed Dec 6, 2023
1 parent 50b3e4b commit 49e3abd
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 28 deletions.
36 changes: 18 additions & 18 deletions src/main/java/io/github/btarg/commands/KillInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.btarg.OrigamiMain;
import io.github.btarg.blockdata.CustomBlockPersistentData;
import io.github.btarg.util.ComponentHelper;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
Expand All @@ -22,7 +23,7 @@ public class KillInterceptor implements Listener {
private static String tempCommand = "";

private static String extractSelector(String command) {
String regex = "@[a-zA-Z_]+\\[[^\\]]*\\]";
String regex = "@[a-zA-Z_]+(\\[[^]]*])?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(command);

Expand All @@ -42,27 +43,26 @@ public void onCommand(PlayerCommandPreprocessEvent event) {
List<Entity> entities = Bukkit.getServer().selectEntities(Bukkit.getServer().getConsoleSender(), selector);
for (Entity entity : entities) {

if (!Objects.equals(tempCommand, event.getMessage())) {
tempCommand = event.getMessage();
event.setCancelled(true);

new BukkitRunnable() {
@Override
public void run() {
tempCommand = "";
}
}.runTaskLater(OrigamiMain.getInstance(), 60); // reset last command after a few seconds

} else {
tempCommand = "";
return;
}

if (CustomBlockPersistentData.getBlockInformation(entity.getChunk()).getBlocksMap().containsValue(entity.getUniqueId().toString())) {
Component component = MiniMessage.miniMessage().deserialize(
"<dark_red><b>WARNING:</b></dark_red> <red>Killing or teleporting Item Displays will break Origami custom blocks placed in your world. Type the command again only if you understand the implications of this!</red>"
);
event.getPlayer().sendMessage(component);
ComponentHelper.sendDecoratedChatMessage(component, event.getPlayer());
if (!Objects.equals(tempCommand, event.getMessage())) {
tempCommand = event.getMessage();
event.setCancelled(true);

new BukkitRunnable() {
@Override
public void run() {
tempCommand = "";
}
}.runTaskLater(OrigamiMain.getInstance(), 60); // reset last command after a few seconds

} else {
tempCommand = "";
return;
}
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/io/github/btarg/commands/RootCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.github.btarg.registry.CustomRecipeRegistry;
import io.github.btarg.registry.RegistryHelper;
import io.github.btarg.resourcepack.ResourcePackGenerator;
import io.github.btarg.util.ComponentHelper;
import io.github.btarg.util.datatypes.BlockPos;
import io.github.btarg.web.JavalinServer;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -57,22 +58,22 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

try {
if (StringUtils.equalsAny(args[1], "blocks", "all")) {
sender.sendMessage("Reloading custom blocks...");
ComponentHelper.sendDecoratedChatMessage("Reloading custom blocks...", sender);
CustomBlockRegistry.clearBlockRegistry();
OrigamiMain.getDefinitionSerializer().loadAndRegister(sender, CustomBlockDefinition.class);
}
if (StringUtils.equalsAny(args[1], "items", "all")) {
sender.sendMessage("Reloading custom items...");
ComponentHelper.sendDecoratedChatMessage("Reloading custom items...", sender);
CustomItemRegistry.clearItemRegistry();
OrigamiMain.getDefinitionSerializer().loadAndRegister(sender, CustomItemDefinition.class);
}
if (StringUtils.equalsAny(args[1], "recipes", "all")) {
sender.sendMessage("Reloading custom recipes...");
ComponentHelper.sendDecoratedChatMessage("Reloading custom recipes...", sender);
CustomRecipeRegistry.clearRecipeRegistry();
OrigamiMain.getDefinitionSerializer().loadAndRegister(sender, CustomRecipeDefinition.class);
}
if (StringUtils.equalsAny(args[1], "resources", "all")) {
sender.sendMessage("Reloading resource pack...");
ComponentHelper.sendDecoratedChatMessage("Reloading resource pack...", sender);
// Generate resource pack and serve with http
try {
JavalinServer.initAndServePack(ResourcePackGenerator.generateResourcePack());
Expand Down Expand Up @@ -102,7 +103,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
sender.sendMessage(String.valueOf(count) + finalString);

} else {
sender.sendMessage("No blocks in this chunk!");
ComponentHelper.sendDecoratedChatMessage("No blocks in this chunk!", sender);
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.btarg.OrigamiMain;
import io.github.btarg.definitions.base.BaseCustomDefinition;
import io.github.btarg.registry.CustomBlockRegistry;
import io.github.btarg.util.ComponentHelper;
import io.github.btarg.util.parsers.ItemParser;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -57,7 +58,7 @@ public CustomBlockDefinition(Map<String, Object> map) {
public void registerDefinition(CommandSender sender) {
CustomBlockRegistry.registerBlock(this);
if (sender != null) {
sender.sendMessage("Registered block: " + this.id);
ComponentHelper.sendDecoratedChatMessage("Registered block: " + this.id, sender);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.btarg.definitions.base.BaseCustomDefinition;
import io.github.btarg.registry.CustomItemRegistry;
import io.github.btarg.util.ComponentHelper;
import io.github.btarg.util.parsers.EnchantmentParser;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand Down Expand Up @@ -68,7 +69,7 @@ private List<PotionEffectType> deserializePotionEffects(Map<String, Object> map)
public void registerDefinition(CommandSender sender) {
CustomItemRegistry.registerItem(this);
if (sender != null) {
sender.sendMessage("Registered item: " + this.id);
ComponentHelper.sendDecoratedChatMessage("Registered item: " + this.id, sender);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.btarg.OrigamiMain;
import io.github.btarg.definitions.base.AbstractBaseDefinition;
import io.github.btarg.registry.CustomRecipeRegistry;
import io.github.btarg.util.ComponentHelper;
import io.github.btarg.util.datatypes.CustomRecipeType;
import io.github.btarg.util.parsers.ItemParser;
import lombok.Getter;
Expand Down Expand Up @@ -104,7 +105,7 @@ else if (split.length == 1) {
public void registerDefinition(CommandSender sender) {
CustomRecipeRegistry.registerRecipe(this);
if (sender != null) {
sender.sendMessage("Registered recipe: " + this.namespacedKey.value());
ComponentHelper.sendDecoratedChatMessage("Registered recipe: " + this.namespacedKey.value(), sender);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.btarg.definitions.CustomRecipeDefinition;
import io.github.btarg.definitions.base.AbstractBaseDefinition;
import io.github.btarg.definitions.base.BaseCustomDefinition;
import io.github.btarg.util.ComponentHelper;
import io.github.btarg.util.ContentPackHelper;
import io.github.btarg.util.NamespacedKeyHelper;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -93,8 +94,8 @@ public void loadAndRegister(CommandSender sender, String parentDirectory, Class<
}

});
if (sender != null)
sender.sendMessage("Registered " + fileCount.get() + " definitions(s)!");

ComponentHelper.sendDecoratedChatMessage("Registered " + fileCount.get() + " definitions(s) from " + parentDirectory, sender);

} catch (IOException e) {
e.printStackTrace();
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/github/btarg/util/ComponentHelper.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.github.btarg.util;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.md_5.bungee.api.chat.TranslatableComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

@SuppressWarnings("deprecation")
public class ComponentHelper {
Expand Down Expand Up @@ -41,6 +43,22 @@ public static Component deserializeGenericComponent(String input) {
return textComponent;
}

public static Component getChatPrefix() {
return MiniMessage.miniMessage().deserialize("<dark_gray>[</dark_gray><gradient:#ca01ba:#ff1c3c>Origami</gradient><dark_gray>]</dark_gray> ");
}

public static void sendDecoratedChatMessage(String message, CommandSender sender) {
if (sender != null) {
sender.sendMessage(getChatPrefix().append(MiniMessage.miniMessage().deserialize(message)));
}
}

public static void sendDecoratedChatMessage(ComponentLike message, CommandSender sender) {
if (sender != null) {
sender.sendMessage(getChatPrefix().append(message));
}
}

public static Component removeItalicsIfAbsent(Component input) {
return input.decorationIfAbsent(TextDecoration.ITALIC, TextDecoration.State.FALSE);
}
Expand Down

0 comments on commit 49e3abd

Please sign in to comment.