NullPointerException upon starting my Bukkit plugin - java

I have been getting the following java.lang.NullPointerException when my plugin is being enabled. I don't really see the problem.
Error:
java.lang.NullPointerException
at tk.mypalsgaming.TARDIScraft.TARDIScraft.onDisable(TARDIScraft.java:31)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:219)
at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:481)
at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:401)
at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:394)
at org.bukkit.craftbukkit.v1_6_R2.CraftServer.disablePlugins(CraftServer.java:281)
at net.minecraft.server.v1_6_R2.MinecraftServer.stop(MinecraftServer.java:349)
at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:445)
at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
plugin.yml:
name: TARDIScraft
main: tk.mypalsgaming.TARDIScraft.TARDIScraft
version: 0.0.1
depend: [Vault]
commands:
tardis:
description: TARDIS Command Block and Admin Command
usage: /<command> <TARDIS command> [parameters]
permission: TARDIScraft.admin
permission-message: You are not a TARDIS Admin, so you do not have access to this command.
TARDIScraft.java:
package tk.mypalsgaming.TARDIScraft;
import java.util.logging.Logger;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
public class TARDIScraft extends JavaPlugin {
Logger console = getLogger();
#Override
public void onEnable() {
console.info("Enabling the TARDIS plugin...");
// TODO: onEnable code
}
#Override
public void onDisable() {
console.info("Disabling the TARDIS plugin...");
// TODO: onDisable code
}
public static Permission permission = null;
#SuppressWarnings("unused")
private boolean setupPermissions()
{
RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
if (permissionProvider != null) {
permission = permissionProvider.getProvider();
}
return (permission != null);
}
public void onPlayerJoin(PlayerJoinEvent evt) {
Player player = evt.getPlayer();
if ( player.hasPermission("TARDIScraft.admin") ) {
console.info("Admin " + player.getName() + " has joined the game.");
}
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if ( cmd.getName().equalsIgnoreCase("tardis") ) {
boolean senderIsPlayer;
if ( sender instanceof Player ) {
senderIsPlayer = true;
} else senderIsPlayer = false;
// TODO: tardis Command
if ( args[0].equalsIgnoreCase("admin") ) {
Player playerToAdmin = Bukkit.getPlayer(args[1]);
if ( playerToAdmin != null ) {
permission.playerAdd(playerToAdmin, "TARDIScraft.admin");
}
}
return true;
} else {
return false;
}
}
}

The NullPointerException is from the "getLogger()" call. The parent class "JavaPlugin" has to have initialize() called before the logger exists. Since you are getting the logger at instantiation time rather than after initialize() is called, the parent class returns null.
The documentation suggested simply calling getLogger() inside your onEnable() and onDisable(), likely because initialize() has been called by then.
Note: The source code say to NOT call initialize() yourself!
See:
https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/plugin/java/JavaPlugin.java#L246
http://wiki.bukkit.org/Plugin_Tutorial#Logging_a_message

Related

Minecraft Plugin loading but command is unkown

I have been creating a plugin for Minecraft on Eclipse, but I am encountering an error. The plugin loads fine; I can see it in the console, but it says unknown command when I try and run the command.
Here is my command code:
package me.TheThunder56.helloworld.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import me.TheThunder56.helloworld.Main;
#SuppressWarnings("unused")
public class HelloCommand implements CommandExecutor{
private Main plugin;
public HelloCommand(Main plugin) {
this.plugin = plugin;
plugin.getCommand("sword").setExecutor(this);
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if(!(sender instanceof Player)) {
sender.sendMessage("Only players may execute this command!");
return true;
}
Player p = (Player) sender;
if(p.hasPermission("sword.use")) {
p.performCommand("/give TheThunder56 netherite_sword{Unbreakable:1,Enchantments:[{id:knockback,lvl:1000}]}");
return true;
}else {
p.sendMessage("You do not have permission to execute this command.");
}
return false;
}
}
Here is my Main.java:
package me.TheThunder56.helloworld;
import org.bukkit.plugin.java.JavaPlugin;
import me.TheThunder56.helloworld.commands.HelloCommand;
public class Main extends JavaPlugin{
#Override
public void onEnable() {
getCommand("sword").setExecutor(new HelloCommand(this));
}
}
And here is my plugin.yml file:
name: GodSword
main: me.TheThunder56.helloworld.Main
version: 1.0
author: TheThunder56
commands:
sword:
description: A command to give you a godly sword.
usage: /sword
Please help!
Thanks.
private Main plugin;
public HelloCommand(Main plugin) {
this.plugin = plugin;
plugin.getCommand("sword").setExecutor(this);
public void onEnable() {
getCommand("sword").setExecutor(new HelloCommand(this));
}
Why are you registering your command twice? No need. Only register the command in your onenable. Also, for your method to give the player the sword, you should not use p.performCommand();, because even if they have the permission they still need op to use /give.
Try using this:
ItemStack sword = new ItemStack(Material.NETHERITE_SWORD));
ItemMeta meta = sword.getItemMeta();
meta.addUnsafeEnchantment(Enchantment.KNOCKBACK, 1000, true);
meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
meta.setUnbreakable(true);
meta.setCustomName(ChatColor.GOLD + "GOD Sword"));
sword.setItemMeta(meta);
int i = 0;
while(true){
for(ItemStack it = p.getInventory(){
if(it == null){
p.getInventory().setItem(i, sword);
break;
}else{
i++;
}
}
}

keycloak redirect to update password url "login-actions/required-action"

I am building Identity management portal, using keycloak for authentication , i want to have one change password screen in this portal, when user click on that , it should redirect to keycloak theme update password screen ("http://host:port/auth/relams/<relam-name>/login-actions/required-action?execution=UPDATE_PASSWORD&client_id=test&tab_id=J4X7UdFi")
Can we achieve this in Keycloak after login?, this screen comes if we have Required Action as "UPDATE_PASSWORD" in Keyclaok UserRepresentation Object and before login if this action is there then keycloak itself redirects automatically to Update Password Screen ,
but can we give the Keycloak Update Password Screen directly to the user on UI after he logins.
We need to create RequiredActionProvider
package com.keycloak.password;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.jboss.logging.Logger;
import org.keycloak.authentication.RequiredActionContext;
import org.keycloak.authentication.RequiredActionProvider;
import org.keycloak.common.util.Time;
import org.keycloak.credential.CredentialModel;
import org.keycloak.credential.CredentialProvider;
import org.keycloak.credential.PasswordCredentialProvider;
import org.keycloak.credential.PasswordCredentialProviderFactory;
import org.keycloak.models.UserModel;
public class CustomUpdatePassword implements RequiredActionProvider {
private static final String OPENID_CONNECT_PROTOCOL = "openid-connect";
private static final String CLAIM_VALUE = "claim.value";
private static final Logger LOG = Logger.getLogger(CustomUpdatePassword.class.getName());
#Override
public void evaluateTriggers(RequiredActionContext context) {
try {
//some custom logic add required_action
if (password.getCreatedDate() == null) {
context.getUser().addRequiredAction(UserModel.RequiredAction.UPDATE_PASSWORD);
} else {
long timeElapsed = Time.toMillis(Time.currentTime()) - password.getCreatedDate();
long timeToExpire = TimeUnit.DAYS.toMillis(daysToExpirePassword);
if (timeElapsed > timeToExpire) {
context.getUser().addRequiredAction(UserModel.RequiredAction.UPDATE_PASSWORD);=
}
}
}
} catch (Exception e) {
LOG.error(e.getMessage());
}
}
#Override
public void requiredActionChallenge(RequiredActionContext context) {
}
#Override
public void processAction(RequiredActionContext context) {
}
#Override
public void close() {
}
}
And Factory class
package com.keycloak.password;
import org.keycloak.Config;
import org.keycloak.authentication.RequiredActionFactory;
import org.keycloak.authentication.RequiredActionProvider;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
public class CustomUpdatePasswordFactory implements RequiredActionFactory {
private static final CustomUpdatePassword SINGLETON = new CustomUpdatePassword();
#Override
public String getDisplayText() {
return "Password Update";
}
#Override
public RequiredActionProvider create(KeycloakSession keycloakSession) {
return SINGLETON;
}
#Override
public void init(Config.Scope scope) {
}
#Override
public void postInit(KeycloakSessionFactory keycloakSessionFactory) {
}
#Override
public void close() {
}
#Override
public String getId() {
return "Password Update";
}
}
Then these class jars needs to deploy in the jboss folder
COPY --chown=jboss:root /target/<above_project_jar>-*.jar /opt/jboss/keycloak/providers/

Minecraft Spigot Command Not Enabling in Main Class

My Command (Specifically only my cmd2 command) doesn't register, and the console displays an error when I start the server. The other command, cmd1, works, but cmd2 doesn't. I'm really not sure why, so I came here for help.
Some of my Main Class:
package me.Vamp.Test;
import me.Vamp.Test.Events.EventsClass;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
private Commands commands = new Commands();
#Override
public void onEnable() {
/* Enabler */
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\nTest Plugin has been enabled.\n\n");
/* Events Register */
getServer().getPluginManager().registerEvents(new EventsClass(), this);
/* Commands Register */
getCommand(commands.cmd1).setExecutor(commands);
getCommand(commands.cmd2).setExecutor(commands);
}
}
The following Class (Commands) only show for the errored command (cmd2). If the code for cmd1 is needed, I will show it.
Some of My Command Class:
package me.Vamp.Test;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
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 Commands implements Listener, CommandExecutor {
public String cmd2 = "getpickaxe";
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
/*
/getPickaxe Command
*/
if (cmd.getName().equalsIgnoreCase(cmd2)) {
Player player = (Player) sender;
if (args.length == 0) {
commandGetPickaxe(player);
return true;
} else {
player.sendMessage(Colors.chat("&c&lERROR &cToo many arguments&8."));
return true;
}
}
} else {
sender.sendMessage(Colors.chat("&c&lERROR &cOnly players can use this command&8."));
return true;
}
return false;
}
public void commandGetPickaxe(Player player){
Inventory inv = player.getInventory();
ItemStack item = new ItemStack(Material.WOOD_PICKAXE, 1);
ItemMeta meta = item.getItemMeta();
ArrayList<String> lore = new ArrayList<String>();
meta.setDisplayName(Colors.chat("&3Wooden Pickaxe"));
lore.add(Colors.chat("&7&oThe Starter Pickaxe&8&o."));
meta.setLore(lore);
item.setItemMeta(meta);
inv.addItem(new ItemStack(item));
player.sendMessage(Colors.chat("&8&l» &3You have received a Wooden Pickaxe&8."));
}
}
This is only the display error on my console.
My Console:
Console
Can I suggest you add a multitude of print statements to see what is null?
/* Commands Register */
System.out.println("cmd1 " + commands.cmd1);
System.out.println("cmd2 " + commands.cmd2);
System.out.println("cmdObj " + commands);
getCommand(commands.cmd1).setExecutor(commands);
getCommand(commands.cmd2).setExecutor(commands);
EDIT 1: It seems as though you are missing the command in your plugin.yml?
It's possible that it is a typo, look carefully. If you think everything is perfectly fine, and the error still occurs, please edit you original post and include the plugin.yml file. Thanks!

Bukkit plugin not loaded (only one command)

I'm new in java and bukkit api and server can't load my plugin, error in console: https://pastebin.com/GzgLhHp6. Maybe problem in "private Plugin plugin" whick is not used, eclipse warned. My code:
Plugin.java
package Iaiao.main;
import org.bukkit.plugin.java.JavaPlugin;
public class Plugin extends JavaPlugin {
public void onEnable () {
getCommand("ip").setExecutor(new Commands(this));
getLogger().info("Enabled!");
}
}
Commands.java
package Iaiao.main;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
private Plugin plugin;
public Commands(Plugin plugin) {
this.plugin = plugin;
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if(args.length == 0) {
return false;
}
String name = args[0];
Player p = Bukkit.getPlayer(name);
if(p == null) {
sender.sendMessage("This player is offline or not registered");
return true;
}
sender.sendMessage("Ip: " + p.getAddress().getAddress());
return true;
}
}
plugin.yml
name: Plugin
main: Iaiao.main.Plugin
version: 1.0
commands:
ip:
description: Player's IP
usage: /ip <player>
Your plugin.yml is not valid:
org.bukkit.plugin.InvalidDescriptionException: Invalid plugin.yml
Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '\t(TAB)' that cannot start any token. (Do not use \t(TAB) for indentation)
Somewhere in you plugin.yml is a tab character which must not be used.
Just read the whole error message. It gives you hints where errors occured.

Bukkit won't load the plugin: name is not defined

Here's my code:
package me.chimericalhobo.BlockChanger;
import java.util.ArrayList;
import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class BlockChanger extends JavaPlugin
{
private static final Logger log = Logger.getLogger("Minecraft");
private final BlockChangerListener blockListener = new BlockChangerListener(this);
public final ArrayList<Player> BlockChangerUsers = new ArrayList<Player>();
#Override
public void onEnable()
{
log.info("[BlockChanger] has been enabled!");
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(this.blockListener, this);
}
#Override
public void onDisable()
{
log.info("[BlockChanger] has been disabled!");
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
if(commandLabel.equalsIgnoreCase("BlockChanger"))
toggleBlockChanger(sender);
return true;
}
private void toggleBlockChanger(CommandSender sender)
{
if(!enabled((Player) sender)){
BlockChangerUsers.add((Player) sender);
((Player) sender).sendMessage(ChatColor.BLUE + "BlockChanger has been enabled!");
}
else
{
BlockChangerUsers.remove((Player) sender);
((Player) sender).sendMessage(ChatColor.RED + "BlockChanger has been disabled!");
}
}
public boolean enabled(Player player)
{
return BlockChangerUsers.contains(player);
}
}
Every time I try to load it the command prompt says:
15:53:08 [SEVERE] Could not load 'plugins\BlockChanger.jar' in folder 'plugins'
org.bukkit.plugin.InvalidDescriptionException: name is not defined
In the plugin.yml add this:
name: (Plugin Name)
main: (Package.name.name(or whatever).(MainClass Ex: .Main)) Ex: me.name.plugin.Main
version: (Version number)
Optionally you can add things such as author: (author name).
Look at http://wiki.bukkit.org/Plugin_YAML for more information.
A plugin needs to consist of a name.
In the plugin.yml you will see a field that says:
name:
Here you must insert a name of your plugin.
A complete example of a plugin.yml looks like this:
name: Velocity Jump
main: com.weebly.foxgenesis.Main
version: 1.0
commands:
vjump:
description: make a player velocity jump to you
usage: /vjump <player> [toPlayer] [time]
default: op
For more info please click this link: http://wiki.bukkit.org/Plugin_YAML
Check your plugin.yml.
Are you sure you have set a name: <plugin name> field in it?

Categories