I am trying to make a plugin and for some reason my onBlockPlace event does not work. Here is my code:
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class test implements Listener {
#EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
Player player = event.getPlayer();
System.out.println("Test!");
player.sendMessage("Test");
}
}
Am I missing something? Please help.
It looks like you already have all the stuff necessary in the listener class. So in your plugin class (the class that extends JavaPlugin) you'll want to use this function. I'll explain the steps but it'll be a mess so you can understand what the code is doing.
Create a new instance of your listener using Listener listener = new test();
Get the server's plugin manager
PluginManager manager = getServer.getPluginManager();
Register the listener with the plugin manager manager.registerEvents(listener, this);
Related
I use java and paper plugin. I made this myself, but it doesn't work well.
source code
package com;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public class [[mainclassname]] extends JavaPlugin implements Listener {
public void onEnable() {
getLogger().warning("Server started.");
testing test = new testing(); // An error occurred that starts from here.
test.onEnable();
getLogger().info("Nice try!"); // check point
}
}
class testing extends JavaPlugin {
public void onEnable() {
getLogger().warning("Hello!");
}
}
build successful.
error : Error occurred while enabling [pluginname] (Is it up to date?)
java.lang.IllegalArgumentException: Plugin already initialized!
The PluginAlreadyInitialized error occours when the .jar file contains more than one subclass of JavaPlugin class.
From here.
Sa as the title says, I have a plugin that detects swear words. What it does now is that it sends a message to the player. But I also want it to execute a command that is already in the game or from another plugin. I'm not sure how to do this. How is that done?
Here is my current code:
package
import com.beam.sweardetector.SwearDetector;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
public class EventsClass implements Listener {
SwearDetector plugin = SwearDetector.getPlugin(SwearDetector.class);
#EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent event) {
Player player = event.getPlayer();
player.sendMessage(ChatColor.GOLD + "This server is running AntiSwear v1.0 by BeamCRASH");
}
#EventHandler
public void chatevent (AsyncPlayerChatEvent event) {
for(String s: event.getMessage().split(" ")) {
if(plugin.getConfig().getStringList("swears").contains(s)) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.DARK_RED + "§lSwearing is not allowed on this server!");
}
}
}
}
Any help will be appreciated.
Thank you!
You can execute command as player :
event.getPlayer().performCommand("mycommand");
But, it will not if the player don't have enough permission.
To run command as console, use this :
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mycommand");
Also, you don't need the / at the beginning of command because it's only to say that it's a command and it can change between players.
Warn: you should run those code in sync. If you are async (specially from AsyncEvent), you should use this code :
Bukkit.getScheduler().runTask(plugin, () -> {
// my code
player.performCommand("kill");
});
I'm trying to make my bot welcome someone whenever someone joins but I can't seem to get it to work. For example(this will appear in an embed by the way):
#Jason joined. You must construct additional pylons.
Can someone help me edit my code so it works please?
Here's my Main code:
import Events.*;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import javax.security.auth.login.LoginException;
public class Main {
public static void main(String[] args) throws LoginException {
JDABuilder jda = JDABuilder.createDefault("I imported key here");
jda.setActivity(Activity.watching("baldness"));
jda.addEventListeners(new Help());
jda.addEventListeners(new PingPong());
jda.addEventListeners(new Clear());
jda.addEventListeners(new Welcome());
jda.build();
}
}
and Here's my Welcome code:
package Events;
import java.util.Random;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Welcome extends ListenerAdapter {
public class GuildMemberJoin extends ListenerAdapter {
String[] messages = {
"[member] joined. You must construct additional pylons.",
"Never gonna give [member] up. Never let [member] down!",
"Hey! Listen! [member] has joined!",
"Ha! [member] has joined! You activated my trap card!",
"We've been expecting you, [member].",
"It's dangerous to go alone, take [member]!",
"Swoooosh. [member] just landed.",
"Brace yourselves. [member] just joined the server.",
"A wild [member] appeared."
};
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
Random rand = new Random();
int number = rand.nextInt(messages.length);
EmbedBuilder join = new EmbedBuilder();
join.setColor(0x66d8ff);
join.setDescription(messages[number].replace("[member]", event.getMember().getAsMention()));
event.getGuild().getDefaultChannel().sendMessage(join.build()).queue();
}
}
}
The documentation fo GuildMemberJoinEvent clearly states:
Requirements
This event requires the GUILD_MEMBERS intent to be enabled.
createDefault(String) and createLight(String) disable this by default!
So you must enable the intent. Read more in the wiki guide Gateway Intents and Member Cache Policy
Additionally, you have a nested class for no reason which means you register the enclosing class as a listener but not the nested class that actually implements it.
Better:
public class Welcome extends ListenerAdapter {
#Override
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
...
}
}
So in my server /me is an enabled command. I wanted to disable this because I don't want people to be able to do this.
I'm learning java, so I decided to code something that disabled /me myself.
So I wrote the following code:
package com.ste999.disableme;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public class Main extends JavaPlugin implements Listener
#Override
public void onEnable() {
getLogger().info("disable me enabled");
PluginManager pm = this.getServer().getPluginManager();
pm.registerEvents(this, (this));
}
#Override
public void onDisable() {
getLogger().info("disable me disabled");
}
#EventHandler
public void OnMe(AsyncPlayerChatEvent event)
{
Player p = event.getPlayer();
if(!p.hasPermission("ste999.me")) {
if (event.getMessage().startsWith("/me")) {
event.setCancelled(true);
p.sendMessage("§4Dont me me!");
}
}
}
}
with the following plugin.yml file:
name: Disable_Me
main: com.ste999.disableme.Main
version: 1.0
load: startup
description: this is should disable me
commands:
Now if someone without op would run /me hello it shouldn't output to the chat and the user should get a message like Dont me me!
But it doesn't. the user is still able to do /me hello without op and the code should prevent that
As I'm fairly new to java this error is probably easy to find, and any help would be much appreciated.
The problem is that AsyncPlayerChatEvent only gets called when actually typing chat messages (not commands). For commands you have to use PlayerCommandPreprocessEvent as wonderfully explained by Mischa in the comments. Changing the event will make it work:
#EventHandler
public void disableMeCommand(PlayerCommandPreprocessEvent event) {
Player p = event.getPlayer();
if(!p.hasPermission("ste999.me")) {
if(event.getMessage().startsWith("/me")) {
event.setCancelled(true);
p.sendMessage("§4Dont me me!");
}
}
}
However, note that PlayerCommandPreprocessEvent should be avoided. Luckily there is another way to disable a command completely in a bukkit server. You should have a commands.yml file located in your server folder. Simply add the "me" alias and set it to null inside the file:
aliases:
me:
- null
I am new to creating minecraft plugins, however not new to programming, I am following a tutorial very thoroughly, the video has good ratings so it is trusted, when watching the video the guy has no problems what so ever (Youtube video on developing minecraft plugins) , so I did some research into solutions but always the line through the code.
Eclipse gives me the option for: #SuppressWarnings("deprecation") which allows the code to be used still but I would rather have no need of that usage.
Basically my question is why is there the need of the line going through the code and how do I find a solution to get rid of it.
Main class:
package com.jc1;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class Core extends JavaPlugin
{
public Permission pPermission = new Permission("playerAbilities.allowed");
#Override
public void onEnable()
{
new BlockListener(this);
PluginManager pm = getServer().getPluginManager();
pm.addPermission(pPermission);
}
#Override
public void onDisable()
{
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("giveitems") && sender instanceof Player)
{
Player p = (Player) sender;
if(p.hasPermission("playerAbilities.allowed"))
{
p.setItemInHand(new ItemStack(Material.DIAMOND_BOOTS));
}
return true;
}
return false;
}
}
Secondary class:
package com.jc1;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class BlockListener implements Listener
{
public BlockListener(Core plugin)
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
#EventHandler
public void onBlockPlace(BlockPlaceEvent e)
{
Player p = e.getPlayer();
if(!p.hasPermission("playerAbilities.allowed"))
{
e.setCancelled(true);
}
}
}
The method is deprecated, meaning that it is not recommended to be used anymore and is most likely replaced with another method.
Methods that are deprecated may still work as intended.
A simple search for the method reveals (this) documentation, stating:
players can duel wield now use the methods for the specific hand instead
which referes to the #see references:
getItemInMainHand() and getItemInOffHand().
Use this:
player.getInventory().getItemInMainHand()
Instead of:
player.getItemInHand()
Hope this helps! :D