How do I change Gamemode ...

Discussion in 'Spigot Plugin Development' started by ChrisHag, Nov 8, 2017.

  1. Does anyone know how to change game mode when I execute a command? (/gmc)
    I have tried MANY ways and all ways led to the same error.

    fplugin.java:

    package ch.christophers.plugin;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.*;
    import com.coloredcarrot.jsonapi.impl.JsonMsg;



    public class fplugin extends JavaPlugin {

    @SuppressWarnings("unused")
    private fplugin plugin;

    @Override
    public void onEnable() {

    }




    @Override
    public void onDisable() {

    }

    @Override

    public boolean onCommand(CommandSender sender,
    Command command,
    String label,
    String[] args) {
    Player player = (Player) sender;

    if (command.getName().equalsIgnoreCase("dank")) {
    player.sendMessage("DANKOR");
    }
    if (command.getName().equalsIgnoreCase("night")) {
    player.getWorld().setTime(24000);
    JsonMsg msg = new JsonMsg("§4PCR §7>> §6Time set to §c§lNight", true);
    msg.send(player);
    }
    if (command.getName().equalsIgnoreCase("day")) {
    player.getWorld().setTime(0);
    JsonMsg msg = new JsonMsg("§4PCR §7>> §6Time set to §c§lDay", true);
    msg.send(player);
    }
    if (command.getName().equalsIgnoreCase("speedon")) {
    JsonMsg msg = new JsonMsg("§4PCR §7>> §c§lSpeed §6Given", true);
    msg.send(player);
    PotionEffect speed = PotionEffectType.SPEED.createEffect(999999999, 7);
    ((Player)sender).addPotionEffect(speed, true);
    }
    if (command.getName().equalsIgnoreCase("speedoff")) {
    JsonMsg msg = new JsonMsg("§4PCR §7>> §c§lSpeed §6Disabled", true);
    msg.send(player);
    PotionEffect speed = PotionEffectType.SPEED.createEffect(1, 0);
    ((Player)sender).addPotionEffect(speed, true);
    }
    if (command.getName().equalsIgnoreCase("gmc")) {
    JsonMsg msg = new JsonMsg("§4PCR §7>> §6Gamemode set to §c§lCreative", true);
    msg.send(player);
    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "gamemode creative");
    }
    if (command.getName().equalsIgnoreCase("gms")) {
    JsonMsg msg = new JsonMsg("§4PCR §7>> §6Gamemode set to §c§lSurvival", true);
    msg.send(player);
    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "gamemode survival");
    }
    return false;
    }

    }

    plugin.yml:

    main: ch.christophers.plugin.fplugin
    name: FirstPlugin
    version: 0.1
    author: Chris
    description: ChrisHags test plugin
    commands:
    mycommand:
    description: An example command
    usage:
    permission: fplugin.mcom
    permission-message: You do not have the fplugin.mcom permission
    dank:
    description: DANKOR DANKOR ALLOORT
    usage:
    permission: fplugin.dank
    permission-message: You do not have the fplugin.dank permission
    day:
    description: Makes it day
    usage:
    permission: fplugin.day
    permission-message: You do not have the fplugin.day permission
    night:
    description: Makes it night
    usage:
    permission: fplugin.night
    permission-message: You do not have the fplugin.night permission
    speedon:
    description: Not as fast as Flash but will do.
    usage:
    permission: fplugin.speedon
    permission-message: You do not have the fplugin.speedon permission
    speedoff:
    description: Turns your flash like speed off.
    usage:
    permission: fplugin.speedoff
    permission-message: You do not have the fplugin.speedoff permission
    gmc:
    description: Changes YOUR gamemode to creative.
    usage:
    permission: fplugin.gmc
    permission-message: You do not have the fplugin.gmc permission
    gms:
    description: Changes YOUR gamemode to survival.
    usage:
    permission: fplugin.gms
    permission-message: You do not have the fplugin.gms permission




    Console error:

    [17:53:52 ERROR]: Could not load 'plugins\FirstPlugin.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: Cannot find main class `ch.christopher
    s.plugin.fplugin'
    at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:66) ~[spigot-1.12.2.jar:git-Spigot-5695bca-53fccdf]
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:129) ~[spigot-1.12.2.jar:git-Spigot-5695bca-53fccdf]
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:326) ~[spigot-1.12.2.jar:git-Spigot-5695bca-53fccdf]
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:248) [spigot-1.12.2.jar:git-Spigot-5695bca-53fccdf]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.loadPlugins(CraftServer.j
    ava:308) [spigot-1.12.2.jar:git-Spigot-5695bca-53fccdf]
    at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.ja
    va:205) [spigot-1.12.2.jar:git-Spigot-5695bca-53fccdf]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.jav
    a:545) [spigot-1.12.2.jar:git-Spigot-5695bca-53fccdf]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_152]
    Caused by: java.lang.ClassNotFoundException: ch.christophers.plugin.fplugin
    at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_152]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:101) ~[spigot-1.12.2.jar:git-Spigot-5695bca-53fccdf]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:86) ~[spigot-1.12.2.jar:git-Spigot-5695bca-53fccdf]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_152]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_152]
    at java.lang.Class.forName0(Native Method) ~[?:1.8.0_152]
    at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_152]
    at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:64) ~[spigot-1.12.2.jar:git-Spigot-5695bca-53fccdf]
    ... 7 more



    That error is very common for me, I try random stuff and fixes it. This time, nothing seems to work for me.
    Sorry if this is a dump post anything, I am fairly new to Plugin development.

    MY API'S I USE:
    JsonAPI - https://www.spigotmc.org/resources/json-api.21035/
     
  2. MiniDigger

    Supporter

    this seems to be an issue with your build process, not with your code.
    open the plugin jar in an archive viewer and see if the main class is located in the right folder (should be in ch/christophers/plugin/)
    how do you build your plugin? what ide (intellij? netbeans? eclipse?) and buildtool (maven? gradle? ant? ide export?) do you use?
     
  3. I use Eclipse and Ant builder thing. What do you mean archive viewer? Could you send me a screenshot if there is anything to screenshot?
     
  4. MiniDigger

    Supporter

    winrar, 7zip, something like that. a jar is basically just a zip, you can open it and look inside.
     
  5. I looks like your "fplugin.java (Main Class)" is not there when you go to export it, can you send me a screenshot of your Path, (Src -> fplugin.java)
     
  6. Should I export my plugin directly to the plugins folder? What do you mean?

    When I opened my Plugins jar file with WinRar I only saw "build.xml", "plugin.yml" and a folder "META-INF"


    Should I export my java file to "src > ch > christophers > plugin" ???
     
    #6 ChrisHag, Nov 9, 2017
    Last edited: Nov 9, 2017
  7. Well, what do you know! I fixed it, I exported it into "src > ch > christophers > plugin" and I renamed the file to "fplugin.jar"

    Code to change game mode to creative:

    if (command.getName().equalsIgnoreCase("gmc")) {
    player.setGameMode(GameMode.CREATIVE);
    }


    The yml for this is set to "gmc"
     
    #7 ChrisHag, Nov 9, 2017
    Last edited: Nov 9, 2017