Bukkit PlayerDeathEvent - java

why isn't this working? I don't get any response if I've been killed! So as you can see I have tested multiple ways. But no one is working.
package net.gameforce.testing;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
#Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this,this);
getLogger().info("Testing Plugin Started");
}
public void onDisable() {}
#EventHandler
public void onPlayerInteract(PlayerInteractEvent event){
Bukkit.broadcastMessage("test");
event.getPlayer().setExp(100);
}
public void onPlayerDeath (PlayerDeathEvent event){
Bukkit.broadcastMessage("send");
event.getEntity().getPlayer().setExp(1000);
}
public boolean onDeath (PlayerDeathEvent event) {
Player Player = event.getEntity();
Bukkit.broadcastMessage(Player.getKiller().getDisplayName() + ", has killed you!");
if (Player.getKiller() != null) {
Bukkit.broadcastMessage("No Player");
}
else {
Bukkit.broadcastMessage("IDK");
}
return true;
}
}
Have I done something wrong?

Quotation from http://bukkit.gamepedia.com/Event_API_Reference#.40EventHandler
"Before this method can be invoked by Bukkit when the "Event" is fired, we need to annotate it. We do this with EventHandlers."
You've added this annotation to your onPlayerInteract method, but none of your others, such as your onDeath method. If your listener is setup correctly, adding the #EventHandler annotation to these methods will allow bukkit to correctly invoke them, like so:
#EventHandler
public void onPlayerDeath (PlayerDeathEvent event){
Bukkit.broadcastMessage("send");
event.getEntity().getPlayer().setExp(1000);
}

Related

why isn't my config file modifying my servers motd?

So as the title says I need my config file to modify my servers motd, I feel like I've done everything the right way but obviously something is wrong, so let me show you what I'm working with
me.zavage.motd.Main
package me.zavage.motd;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
#Override
public void onEnable() {
saveDefaultConfig();
}
}
me.zavage.motd.listeners.PingListener
package me.zavage.motd.listeners;
import java.io.File;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent;
import me.zavage.motd.Main;
import me.zavage.motd.utils.Utils;
public class PingListener implements Listener {
private Main plugin;
#EventHandler
public void onPing(ServerListPingEvent e) {
e.setMaxPlayers(plugin.getConfig().getInt("maxplayers"));
e.setMotd(Utils.chat(plugin.getConfig().getString("motd" + "\n" + (Utils.chat(plugin.getConfig().getString("motd_line_2"))))));
try {
e.setServerIcon(Bukkit.loadServerIcon(new File(plugin.getConfig().getString("server_icon_path"))));
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
Then we have the config.yml
#can only be a NUMBER with NO DECIMALS!
maxplayers: ''
motd: ''
#next line on motd
motd_line_2:
#create a folder inside the first page of your server files, call it "icon" and drop you server icon in there
#server icon MUST be 64 x 64 pixels
# #NameOfIcon#
#path example "C:\Users\user\Desktop\minecraft test server\icon\icon.png
server_icon_path: ''
If the code that you show if everything that you are using, there is few issues.
You don't register the listener
package me.zavage.motd;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
#Override
public void onEnable() {
saveDefaultConfig();
getServer().getPluginManager().registerEvents(new PingListener(this), this);
}
}
You don't set the plugin variable in the listener, and you don't have constructor
public class PingListener implements Listener {
private Main plugin;
public PingListener(Main plugin) {
this.plugin = plugin;
}
}
Finally, you are searching for the motd\n motd_line_2 message. You have to use this in your ServerListPingEvent listener :
e.setMaxPlayers(plugin.getConfig().getInt("maxplayers"));
e.setMotd(Utils.chat(plugin.getConfig().getString("motd") + "\n" + plugin.getConfig().getString("motd_line_2")));
try {
e.setServerIcon(Bukkit.loadServerIcon(new File(plugin.getConfig().getString("server_icon_path"))));
} catch (Exception exc) {
exc.printStackTrace();
}

Why does the forge event not fire?

I'm trying to see if someone joined a world and then print something to console but the event doesn't seem to fire. Can someone help me? Here is my code:
package pl00py_TR.pl00pyspvpmod;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
#Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, version = Reference.VERSION)
public class Pl00pysPVPMod {
#EventHandler
public void preInit(FMLPreInitializationEvent event) {
}
#EventHandler
public void init(FMLInitializationEvent event) {
}
#EventHandler
public void postInit(FMLPostInitializationEvent event) {
System.out.println("DIRT BLOCK >> " + Blocks.dirt.getUnlocalizedName());
}
public class EventTest {
#SubscribeEvent
public void OnEvent(LivingSpawnEvent event) {
System.out.println("Event Test");
if (event.entity instanceof EntityPlayer) {
System.out.println("Player Joined");
}
}
}
public void RegisterEvents() {
System.out.println("Registering Events...");
MinecraftForge.EVENT_BUS.register(EventTest.class);
}
}
The code is for 1.8.9 and I am trying to find out if someone joins a server/world. I am trying to get the players coords and print them to console but the event doesn't fire or initialize. Could someone explain why this is happening and give me an idea of how to fix this?
Could be wrong but try MinecraftForge.EVENT_BUS.register(new EventTest()); instead of MinecraftForge.EVENT_BUS.register(EventTest.class);
In the documentation for EventBus
void register(java.lang.Object target)

My Minecraft Spigot Plugin Not Reporting Back

I am pretty new to coding java and I have done a few tutorials, which have been great but i don't know why it isn't working in-game. I have tried everything such as changing it and looking at so many different forums. There are two classes for the events(Join and Leave Event) and the main class. I have made sure to check for importing them and errors none for me to see from where I have looked. If anyone can help it would be a blessing.
Code: - Main class:
package me.JimmyClown.FirstPlugin;
import org.bukkit.plugin.java.JavaPlugin;
public class MainClass extends JavaPlugin {
#Override
public void onEnable() {
System.out.println("It works!");
getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
getServer().getPluginManager().registerEvents(new PlayerLeave(), this);
}
#Override
public void onDisable() {
}
}
**Code for the Join-Class:**
package me.JimmyClown.FirstPlugin;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
public class PlayerJoin implements Listener{
#EventHandler
void onPlayerJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();
e.setJoinMessage(ChatColor.BLUE + "Welcome back to the server!" + player.getDisplayName());
}
}
**Code for Leave Class:**
package me.JimmyClown.FirstPlugin;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
public class PlayerLeave implements Listener {
#EventHandler
void onPlayerLeave(PlayerQuitEvent e) {
Player player = e.getPlayer();
e.setQuitMessage(ChatColor.DARK_BLUE + "Awwww, We hope we see you soon" + player.getDisplayName());
}
}
The plugin.yml:
name: FirstProject
version: 1.0
main: me.JimmyClown.FirstPlugin.MainClass
authors: JimmyTheClown
description: This is my first plugin.
Try to set the access modifier of the event methods to public.
public void onPlayerLeave(PlayerQuitEvent e) { }
public void onPlayerJoin(PlayerJoinEvent e) { }

Spigot Unregister event with a command

I made a Plugin that registers an event, I want to make a command that unregisters it, how should I do it, I already searched for 2 h and I found nothing.
I want to make /Pvpeventon to start the event and /Pvpeventoff to turn it off
that is the code I already made:
package me.leopa.R1.FFA;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class MainFFA extends JavaPlugin implements Listener{
#Override
public void onEnable() {
System.out.println("[INFO Leopa] Start");
super.onEnable();
}
#Override
public void onDisable() {
System.out.println("[INFO Leopa] Stop");
super.onDisable();
}
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(command.getName().equalsIgnoreCase("PVPEVENTon")) {
getServer().getPluginManager().registerEvents(this, this);
}
if(command.getName().equalsIgnoreCase("PVPEVENToff")) {
getServer().getPluginManager().
}
return super.onCommand(sender, command, label, args);
}
#EventHandler
public void onDeathPVPEVENT(PlayerDeathEvent pvpevent) {
Player p = pvpevent.getEntity();
p.sendMessage("HI");
}
}`
Instead of unregister the event you should simplify it and add a boolean as variable which turns into false when the pvp should be disabled and to true if pvp is allowed:
//Some Listener class
...
private YourPlugin plugin; //example
...
#EventHandler
public void playerDeath(PlayerDeathEvent event) {
if(plugin.isEventMode()) { //TODO when event mode is on }
}
Plugin class
...
public class YourPlugin extends JavaPlugin {
...
private boolean eventMode; //false per default
...
public boolean toggleEventMode() {
eventMode = !eventMode; //negation so if it is true it will be turned into false if it is false it will be turned to true
return eventMode;
}
public boolean isEventMode() {
return eventMode;
}
}
Command toggle event mode:
//is declared somewhere
boolean eventMode = plugin.toggleEventMode();
//true if eventMode is on false if not.
Note you can also use a setEventMode method.
You also can use the unregisterAll method to unregister all events in a Listener or a Plugin:
HandlerList.unregisterAll(this); //takes a listener or a plugin. In your case you got all stuff in one class it should still work.
Check these methods:
HandlerList#unregister(Plugin)
HandlerList#unregister(Listener)
HandlerList#unregisterAll(Plugin)
HandlerList#unregisterAll(Listener)

Command not working

Today I started on my first big project, which is a Minecraft Spigot plugin for my server called Pixel Network. When creating the /help command I encountered a problem. Whenever I called the command it just returned itself. I know that this is a question frequently asked, but I just couldn't get it to work. Here is my code:
Main Class
package gq.pixelnetwork.main;
import gq.pixelnetwork.listeners.CommandListener;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
public void onEnable() {
System.out.println("If you see this, the Pixel Network plugin is loaded!");
Bukkit.getServer().getPluginManager().registerEvents(new CommandListener(), this);
}
public void onDisable() { System.out.println("If you see this, the Pixel Network plugin is unloaded!"); }
}
And my Command Listener Class
package gq.pixelnetwork.listeners;
import gq.pixelnetwork.modules.Colors;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
public class CommandListener implements Listener{
Colors c = new Colors();
// Returning false will return the command to the sender!
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (cmd.getName().equalsIgnoreCase("help")) {
if (!(sender instanceof Player)) {
sender.sendMessage(c.red + "This is a Player-only command!");
return true;
} else{
openHelp();
return true;
}
}
return true;
}
private void openHelp() {
Inventory helpGUI = Bukkit.createInventory(null, 27, "§a§lHelp Menu");
createDisplay(Material.BOOK, helpGUI, 11, "§7/spawn", "§fUse it to get to Spawn.");
createDisplay(Material.BOOK, helpGUI, 13, "§7/hub", "§fUse it to get to HUB.");
createDisplay(Material.BOOK, helpGUI, 15, "§7/help", "§fUse it to see this menu.");
}
private static void createDisplay(Material material, Inventory inv, int Slot, String name, String lore) {
ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
ArrayList<String> Lore = new ArrayList<String>();
Lore.add(lore);
meta.setLore(Lore);
item.setItemMeta(meta);
inv.setItem(Slot, item);
}
}
The reference to the Colors class is not the problem, as that is just a small 'module' I have made to make using colors easier.
I hope that someone can help me with this.
Thanks in advance,
- Xaaf
Unless the Spigot API has changed significantly since I've used it, your problem is that you're using onCommand in an event handler, not in your JavaPlugin class.
For example, you can have something like this
public class Whatever extends JavaPlugin {
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(command.getName().equalsIgnoreCase("hello")){
sender.sendMessage("Hello!");
return true;
}
return false;
}
}
Which will work fine, but you're registering your CommandListener as an Event Listener, meaning that when bukkit parses the class, it looks for any methods with an #EventHandler annotation and processes them as an event when they're fired.
You can also a Command Executor, which would look something like this:
public class Whatever extends JavaPlugin {
#Override
public void onEnable(){
this.getCommand("test").setExecutor(new MyCommandExecutor(this));
// register the class MyCommandExecutor as the executor for the "test" command
}
#Override
public void onDisable(){
}
}
then have another class like so:
public class MyCommandExecutor implements CommandExecutor {
private final Whatever plugin;
public MyPluginCommandExecutor(Whatever plugin) {
this.plugin = plugin; // Store the plugin in situations where you need it.
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if(command.getName().equalsIgnoreCase("test")){
sender.sendMessage("Hello World!");
return true;
}
return false;
}
}
Essentially, you're trying to implement a CommandExecutor inside an EventListener, which shouldn't work, as far as I'm aware.
You're also returning true by default if the command isn't the one your plugin is handling, which is wrong, you should return false if it's not your command.

Categories