From a4ecc95ef81bcb30d9966c3185526de15d4ef577 Mon Sep 17 00:00:00 2001 From: TheFaser Date: Sat, 7 Oct 2023 23:19:41 +0500 Subject: [PATCH] Feature: add spit --- .../net/flectone/commands/CommandSpit.java | 48 +++++++++++++++++++ .../listeners/PlayerSpitListener.java | 38 +++++++++++++++ src/main/resources/config.yml | 18 ++++++- src/main/resources/plugin.yml | 6 +++ 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 src/main/java/net/flectone/commands/CommandSpit.java create mode 100644 src/main/java/net/flectone/listeners/PlayerSpitListener.java diff --git a/src/main/java/net/flectone/commands/CommandSpit.java b/src/main/java/net/flectone/commands/CommandSpit.java new file mode 100644 index 00000000..6763a3de --- /dev/null +++ b/src/main/java/net/flectone/commands/CommandSpit.java @@ -0,0 +1,48 @@ +package net.flectone.commands; + +import net.flectone.misc.commands.FCommand; +import net.flectone.misc.commands.FTabCompleter; +import net.flectone.utils.ObjectUtil; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.LlamaSpit; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public class CommandSpit implements FTabCompleter { + @Override + public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { + + FCommand fCommand = new FCommand(commandSender, command.getName(), s, strings); + if (fCommand.isConsoleMessage()) return true; + + Player player = (Player) commandSender; + Location location = player.getEyeLocation(); + World world = player.getWorld(); + + location.setY(location.getY() - 0.3); + + ObjectUtil.playSound(player, command.getName()); + LlamaSpit spit = (LlamaSpit) world.spawnEntity(location, EntityType.LLAMA_SPIT); + spit.setVelocity(location.getDirection()); + + return true; + } + + @Override + public String getCommandName() { + return "spit"; + } + + @Nullable + @Override + public List onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { + return null; + } +} diff --git a/src/main/java/net/flectone/listeners/PlayerSpitListener.java b/src/main/java/net/flectone/listeners/PlayerSpitListener.java new file mode 100644 index 00000000..538d72fc --- /dev/null +++ b/src/main/java/net/flectone/listeners/PlayerSpitListener.java @@ -0,0 +1,38 @@ +package net.flectone.listeners; + +import net.flectone.Main; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + +import static net.flectone.managers.FileManager.config; + +public class PlayerSpitListener implements Listener { + + @EventHandler + public void onPlayerSpitEvent(PlayerInteractEvent event) { + if (!config.getBoolean("command.spit.enable")) return; + if (!event.getAction().equals(Action.RIGHT_CLICK_AIR)) return; + ItemStack item = event.getItem(); + + if (item == null) return; + + Material configMaterial; + + try { + configMaterial = Material.valueOf(config.getString("command.spit.item").toUpperCase()); + } catch (IllegalArgumentException | NullPointerException exception) { + Main.warning("Item for spit was not found"); + configMaterial = Material.WHITE_DYE; + } + + if (!item.getType().equals(configMaterial)) return; + + Bukkit.dispatchCommand(event.getPlayer(), "spit"); + + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9b48a839..274800f5 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -434,9 +434,20 @@ command: enable: true aliases: [] + spit: + enable: true + aliases: [] + + # allows to use a command if you hold this item in your hand and press RMB + item: "WHITE_DYE" + cool-down: enable: false + spit: + enable: false + time: 5 + permission: "flectonechat.spit.cooldown.immune" kick: enable: false time: 5 @@ -588,12 +599,15 @@ cool-down: # Sound type format # SOUND_NAME:VOLUME:PITCH sound: + spit: + enable: true + type: "ENTITY_LLAMA_SPIT:0.3:1" glass-knocking: enable: true - type: "BLOCK_GLASS_PLACE:3:1" + type: "BLOCK_GLASS_PLACE:1:1" door-knocking: enable: true - type: "BLOCK_WOOD_PLACE:3:1" + type: "BLOCK_WOOD_PLACE:1:1" auto-message: enable: false type: "BLOCK_NOTE_BLOCK_BELL:1:1" diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index cc12beab..1d9efb8a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -153,6 +153,10 @@ commands: usage: /kick permission: flectonechat.kick + spit: + usage: /spit + permission: flectonechat.spit + permissions: flectonechat.reload: default: op @@ -283,4 +287,6 @@ permissions: flectonechat.chat-settings.kick: default: true flectonechat.chat-settings.auto-message: + default: true + flectonechat.spit: default: true \ No newline at end of file