-3

First off I'd like to apologize in advance because I am most likely going to upset a lot of coders here considering my skill-level on coding..

I love to code! It really makes me happy in all honesty and I want to be able to progress and become better but I can't do it alone, at least so I think.

So here's my problem on Eclipse, it keeps telling me that there's plenty of errors all over my lines but whenever I try to fix them (in a way according to a lot of tutorials) it doesn't want to get fixed. I'm thinking that it might be the eclispe program bugging out or something like that! (Or it might just be that I am an idiot and don't know how to code, either of those two.)

So here's my coding, I'm trying to make a Plugin that welcomes a player and gives them a warm message along with some free experience points and a free item.

It has a Listener obviously and here's my coding. Please tell me what's wrong!

Main:

package me.noobpowah;

import.org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class ServerFunctions extends JavaPlugin {

@Override
public void onEnable() {
    new JoinListener(this);
}

@Override
public void onDisable() {

}

public boolean onCommand(CommandSender sender, Command cmd, String label, String[ args]) {

    if (cmd.getName().equalsIgnoreCase("totalexp") && sender instanceof Player) {

        Player player = (Player) sender;

        player.sendMessage(ChatColor.GOLD + "Total EXP: " + player.gettotalExperience());

        return true;

    } else if (cmd.getName().equalsIgnoreCase("exp") && sender instanceof Player) {

        Player player = (Player) sender;

        player.sendMessage(ChatColor.GOLD + "EXP to next level: " + player.getExpToLevel());

        return true;
    }

    return false;
}

}

Listener:

package me.noobpowah;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.ChatColor;
import org.bukkit.inventory;

public class JoinListener {

public JoinListener(ServerFunctions plugin) {
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@EventHandler
public void onEvent(BlockPlaceEvent e) {

    Player player = e.getPlayer();

    e.setJoinMessage(ChatColor.AQUA + "Welcome, " + player.getName() + ", to 
    the server!");

    //If new player
    if (player.hasPlayedBefore() == false) {
        player.sendMessage(ChatColor.Green + "You seem to be new here aren't 
    you? Here! Have a free apple as a welcome gift.");

        //Adds 1 apple to the player's inventory
        player.getInventory().addItem(new ItemStack(Material.APPLE, 1));
    }
}

}

`

Zajo Guld
  • 1
  • 1
  • 1
    Ignore the above, most likely you have an issue with your classpath. Do you see red underlines under any of the text in your code? – Kon Aug 08 '17 at 18:00
  • 1
    Sorry, are your imports highlighted red? Did you import the libraries that you are using? – cunniemm Aug 08 '17 at 18:03
  • I'll bet the JAR for the bukkit package is not in your CLASSPATH. Add it as Eclipse expects you to. – duffymo Aug 08 '17 at 18:04
  • @Kon yeah I see a lot of red underlines on plenty of stuff.. do you want me to tell you every single one? – Zajo Guld Aug 08 '17 at 18:20
  • @cunnniemm Yeah the imports are red and some other stuff as well :o – Zajo Guld Aug 08 '17 at 18:20
  • @ZajoGuld If you see underlines under your `import` statements at the top of the file, you almost certainly have a classpath issue. How is that set? – Kon Aug 08 '17 at 18:21
  • @ZajoGuld Check this link out! You need to import your bukkit.jar file! https://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse – cunniemm Aug 08 '17 at 18:22
  • Edit: Sorry for not mentioning this but I have already fixed the BuildPath on the folder. It has the correct Java Build Path and the Javadoc Location – Zajo Guld Aug 08 '17 at 18:27
  • @cunnniemm hey thanks, I just posted a Edit that I fixed it now! I guess if I rewrite my whole coding it should fix itself? – Zajo Guld Aug 08 '17 at 18:28
  • @ZajoGuld Im not sure if I understand what you are trying to say? If there is still red code then its coded wrong, or you should just hover your mouse over it and see what the error means! – cunniemm Aug 08 '17 at 18:44
  • @cunnniemm I fixed it! Thank you <3 – Zajo Guld Aug 08 '17 at 19:18
  • @ZajoGuld Your `String[ args]` needs to be `String[] args` in the `onCommand` definition. EDIT: Looks like you beat me by a few seconds. Glad it's working. – Kon Aug 08 '17 at 19:19
  • @ZajoGuld Gonna post the fixed code for you give me 5 mins. – cunniemm Aug 08 '17 at 19:29

1 Answers1

0

Solution

This solution just has the syntax errors fixed and a few more imports.

ServerFunctions Class

package me.noobpowah;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class ServerFunctions extends JavaPlugin {

@Override
public void onEnable() {
    new JoinListener(this);
}

@Override
public void onDisable() {

}

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

    if (cmd.getName().equalsIgnoreCase("totalexp") && sender instanceof Player) {

        Player player = (Player) sender;

        player.sendMessage(ChatColor.GOLD + "Total EXP: " + player.getTotalExperience());

        return true;

    } else if (cmd.getName().equalsIgnoreCase("exp") && sender instanceof Player) {

        Player player = (Player) sender;

        player.sendMessage(ChatColor.GOLD + "EXP to next level: " + player.getExpToLevel());

        return true;
    }

    return false;
}

}

JoinListener Class

package me.noobpowah;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.ChatColor;
import org.bukkit.inventory.ItemStack;

public class JoinListener implements Listener {

public JoinListener(ServerFunctions plugin) {
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@EventHandler
public void onEvent(BlockPlaceEvent e) {

    Player player = e.getPlayer();

    e.setJoinMessage(ChatColor.AQUA + "Welcome, " + player.getName() + ", to the server!");

    //If new player
    if (player.hasPlayedBefore() == false) {
        player.sendMessage(ChatColor.GREEN + "You seem to be new here aren't you? Here! Have a free apple as a welcome gift.");

        //Adds 1 apple to the player's inventory
        player.getInventory().addItem(new ItemStack(Material.APPLE, 1));
    }
}

}
cunniemm
  • 679
  • 5
  • 19