1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

I re-writed the hole plugin!!!

Discussion in 'Plugins/Codes' started by A Christmas Hangar, Dec 1, 2015.

Thread Status:
Not open for further replies.
  1. A Christmas Hangar

    A Christmas Hangar Member

    Joined:
    Sep 15, 2015
    Messages:
    2,545
    Likes Received:
    1,492
    Trophy Points:
    113
    Gender:
    Male
    Location:
    Kepler452B
    Home Page:
    I re-writed the hole plugin!!! and still having errors:
    package Gui.test;


    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Gui extends JavaPlugin implements Listener {

    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
    }

    public void openGUI(Player player) {
    Inventory i = Bukkit.createInventory(null, 9, ChatColor.DARK_BLUE
    + "Kits!");

    ItemStack PvP = new ItemStack(Material.DIAMOND_SWORD);
    ItemMeta PvPMeta = PvP.getItemMeta();

    ItemStack Archer = new ItemStack(Material.BOW);
    ItemMeta ArcherMeta = Archer.getItemMeta();

    PvPMeta.setDisplayName(ChatColor.WHITE + "PvP");
    PvP.setItemMeta(PvPMeta);

    ArcherMeta.setDisplayName(ChatColor.WHITE + "Archer");
    Archer.setItemMeta(ArcherMeta);

    i.setItem(3, PvP);
    i.setItem(5, Archer);

    player.openInventory(i);
    }

    @EventHandler
    public void onInventoryClickEvent(InventoryClickEvent e) {
    if (ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase(
    "Kits!"))
    return;

    Player player = (Player) e.getWhoClicked();
    e.setCancelled(true);

    if (e.getCurrentItem() == null
    || e.getCurrentItem().getType() == Material.AIR
    || !e.getCurrentItem().hasItemMeta()) {
    player.closeInventory();
    return;
    }
    switch (e.getCurrentItem().getType()){
    case DIAMOND_SWORD:
    player.performCommand("Help");
    player.closeInventory();
    player.sendMessage(String.format(
    "%s You received the kit %sPvP%s!", ChatColor.GREEN,
    ChatColor.AQUA, ChatColor.GREEN));
    break;
    case BOW:
    player.performCommand("Spawn");
    player.closeInventory();
    player.sendMessage(String.format(
    "%s You received the kit %sArcher%s!", ChatColor.GREEN,
    ChatColor.AQUA, ChatColor.GREEN));
    break;
    default:
    player.closeInventory();
    break;
    }

    }

    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent e) {
    e.getPlayer().getInventory().addItem(new ItemStack(Material.COMPASS));
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
    Action a = e.getAction();
    if (a == Action.PHYSICAL || e.getItem() == null
    || e.getItem().getType() == Material.AIR)
    return;

    if (e.getItem().getType() == Material.COMPASS) {
    openGUI(e.getPlayer());
    }
    }
    @error_dan_overflow @TrinityDEV @GingerGeek @rubik_cube_man @Joe Henry
     
  2. Mattuki

    Mattuki Member

    Joined:
    Mar 21, 2015
    Messages:
    451
    Likes Received:
    153
    Trophy Points:
    43
    Gender:
    Male
    :v pls explain this to all the persons who want to know about this :3
     
    red_lion likes this.
  3. Robke28

    Robke28 Member

    Joined:
    Oct 4, 2014
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Hi there,
    Your fault is pretty simple and think you just overlooked it

    if (ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase(
    "Kits!"))
    return;

    Here you are returning if the inventory is named Kits, but you wanna do the reverse, so just change it to:

    if (!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase(
    "Kits!")){
    return;}
     
  4. Mattuki

    Mattuki Member

    Joined:
    Mar 21, 2015
    Messages:
    451
    Likes Received:
    153
    Trophy Points:
    43
    Gender:
    Male
    one day i will know about what are you talking xd
     
  5. DanOF

    DanOF Member

    Joined:
    Aug 20, 2015
    Messages:
    335
    Likes Received:
    230
    Trophy Points:
    43
    Gender:
    Male
    Location:
    New Jersey, USA
    Home Page:
    @Hangar555 I recommend you go to www.spigotmc.org for plugin help, plenty of very experienced devs there and you will get fast replies.
     
  6. A Christmas Hangar

    A Christmas Hangar Member

    Joined:
    Sep 15, 2015
    Messages:
    2,545
    Likes Received:
    1,492
    Trophy Points:
    113
    Gender:
    Male
    Location:
    Kepler452B
    Home Page:
    I tried, I prefer to post here, before see 1656647246641.2 mens saying "learn Java" "feel free to learn Java" to "We don't want noobs" or "go away, this is only for pros" (they don't have any cool plugin, I mean, for what you want a portable [and bugged] workbench without perms... So, yes I prefer that you say "I can't help you" or GingerGeek saying "You have too many errors, noob"

    But thanks, @error_dan_overflow
     
  7. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    127
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    Maybe post your stacktrace
     
    DanOF likes this.
  8. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    127
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    That doesnt make sense at all..... "I still have an error", he didnt said, help my code is not working correct my event isnt fired at all. So plez, no. And yes, maybe he misplaced the '!', but still it wouldnt fix his error, which in this case we dont have. So how are you even to try solving an error, without a stacktrace, thats my opinion

    And eh, @Hangar555 some people at SpigotMC have working plugins where they put time and afford in it, example https://www.spigotmc.org/resources/healthbar.15230/ *shameless selfpromotion xd*
     
  9. ExtendedHorizons

    ExtendedHorizons Member

    Joined:
    Oct 4, 2014
    Messages:
    206
    Likes Received:
    166
    Trophy Points:
    43
    Gender:
    Male
    Occupation:
    Drawing 24/7
    Location:
    Utah
    Home Page:
    Hopefully this isn't to much spoon feeding, @Hangar555 I fixed the code for you, and added comments of where and why I did what. Read them!
    Code:
    package gui.test; //never capitalize package names its not proper syntax
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class SpoonFeeding extends JavaPlugin implements Listener {
        /*
        If this doesn't work, you might not be setting up your plugin.yml correctly
        Also you don't need all of these return statements, Id really only suggest those if you have code after that would be called,
        but since I made it all in a few if statements, if it doesn't get called, it doesn't get called.
        This imo is a lot better then having a bunch of if statements in the code, rather then returning it even though the method doesn't have a return type.
    
        Sorry for the rough explanation.
        */
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this); //Registers the event
        }
    
        public void openGUI(Player player) {
            //I wouldn't suggest naming the Inventory to just "i" because later on you might have more variables, ect. Having them named properly helps
            Inventory inventory = Bukkit.createInventory(null, 9, ChatColor.DARK_BLUE + "Kits"); //The name of the inventory used to be "Kits!" when you were checking if the name was just "Kits" later.
    
            //Camel cased these
            ItemStack pvp = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta pvpMeta = pvp.getItemMeta();
    
            ItemStack archer = new ItemStack(Material.BOW);
            ItemMeta archerMeta = archer.getItemMeta();
    
            pvpMeta.setDisplayName(ChatColor.WHITE + "PvP");
            pvp.setItemMeta(pvpMeta);
    
            archerMeta.setDisplayName(ChatColor.WHITE + "Archer");
            archer.setItemMeta(archerMeta);
    
            inventory.setItem(3, pvp);
            inventory.setItem(5, archer);
    
            player.openInventory(inventory);
        }
    
        @EventHandler
        public void onInventoryClickEvent(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            if (ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("kits")) { // checking if there inventory name (without color) is "kits"
                if (event.getCurrentItem().hasItemMeta()) { //If the clicked item has ItemMeta (you dont need to check if the item is null because if it is, it wouldn't have item meta
                    switch (event.getCurrentItem().getType()) { //I personally don't use a switch instead of if statements, but this works.
                        case DIAMOND_SWORD:
                            player.closeInventory();
                            player.performCommand("help"); // I guess you just wanted to test if this part worked by performing this command? I would suggest just adding a debug message like Bukkit.broadcastMessage("DIAMOND_SWORD was clicked in the kits inventory");
                            player.sendMessage(String.format("%sYou received the kit %sPvP%s!", ChatColor.GREEN, ChatColor.AQUA, ChatColor.GREEN));
                            break;
                        case BOW:
                            player.closeInventory();
                            player.performCommand("spawn");
                            player.sendMessage(String.format("%sYou received the kit %sArcher%s!", ChatColor.GREEN, ChatColor.AQUA, ChatColor.GREEN));
                            break;
                        default:
                            player.closeInventory(); //This will close the inventory whenever the item type isn't a diamond sword or a bow, but ok
                            break;
                    }
                }
                event.setCancelled(true); //Makes it so they cant move items in the kits inventory around (still calls the if statements)
            }
        }
    
        @EventHandler
        public void onPlayerJoinEvent(PlayerJoinEvent event) {
            event.getPlayer().getInventory().addItem(new ItemStack(Material.COMPASS)); //Add the compass to the inventory
        }
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            if(event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { //If the action is right clicking air, or a block
                if (event.getItem().getType().equals(Material.COMPASS)) { //If the item the player interacted with is a compass
                    openGUI(event.getPlayer()); //Open the GUI
                }
            }
        }
    }
     
    Last edited: Dec 9, 2015
  10. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    127
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    Yeh, go feed them code, they will learn from it! Goodjob!
     
    DanOF likes this.
  11. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    127
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    Before you think that I'm an asshole, let me just explain why this is not the correct way to help him

    ••
    package gui.test; //never capitalize package names
    And why is that wrong? Doenst make sense to just tell him its wrong without giving an argument why its wrong.

    ••
    switch(event.getCurrentItem().hasItemMeta()) { //If the clicked item has ItemMeta (you dont need to check if the item is null because if it is, it wouldn't have item meta

    And why do we need to check this? Maybe you should tell him something about an NPE that will be thrown without checking

    ••
    event.getCurrentItem().getType()) { //I personally don't use a switch instead of if statements, but this works.

    What is a switch? You dont even tell him what it does or what its used for. You could link him to the javadocs for switch. And explain him why you need a break; etcetc

    Im tired of typing, because Im on my phone. But yeh lack of details. And you dont give him extra information sources *cough* docs.

    Its okay that you are trying to help, but dont help by rewriting his code and just giving it to him, its just a scream of attention, "look i made ur amazingly good, i procode" Seriously dont spoonfeed, no one will learn from it.

    No hate, just my opinion
     
    CasparWolf likes this.
  12. YounB

    YounB Helper Staff Member Helper

    Joined:
    May 13, 2014
    Messages:
    5,948
    Likes Received:
    6,549
    Trophy Points:
    113
    Gender:
    Male
    Occupation:
    ¯\_(ツ)_/¯
    Location:
    London, England
    I am almost 100% sure that experienced devs won't diss you, lol.
     
    TrinityDEV likes this.
  13. ExtendedHorizons

    ExtendedHorizons Member

    Joined:
    Oct 4, 2014
    Messages:
    206
    Likes Received:
    166
    Trophy Points:
    43
    Gender:
    Male
    Occupation:
    Drawing 24/7
    Location:
    Utah
    Home Page:
    sorry *edits the package thing adds "its proper syntax"* <3 Btw I don't "Hate your opinion" sorry that I didn't explain everything with perfect detail. But if I was where he was, this would've been enough to make me understand. And I suck at explaining code :p but ye sorry
     
  14. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    127
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    Its okay, but Spoon feeding is just a nah
     
  15. ExtendedHorizons

    ExtendedHorizons Member

    Joined:
    Oct 4, 2014
    Messages:
    206
    Likes Received:
    166
    Trophy Points:
    43
    Gender:
    Male
    Occupation:
    Drawing 24/7
    Location:
    Utah
    Home Page:
    Yeah I guess there is no way to guarantee that the person who asked for it will learn anything if you do it that way.
     
  16. Robke28

    Robke28 Member

    Joined:
    Oct 4, 2014
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Because this plugin only consists of a Main class, I copy pasted it in Intellij, added the things I said he had to change and then tried the plugin out myself (and it worked).
    I agree the way I told him to make those changes could be better, but what I said was right.
    If you would see the stacktrace you would see that nothing is wrong cause there are no errors.
    The only reason the gui didn't open was because he returned (went out of the method) when the name was the name of the item he wanted to use to open the gui, so a simple ! in front would change that and make the plugin work.
     
  17. A Christmas Hangar

    A Christmas Hangar Member

    Joined:
    Sep 15, 2015
    Messages:
    2,545
    Likes Received:
    1,492
    Trophy Points:
    113
    Gender:
    Male
    Location:
    Kepler452B
    Home Page:
    Well, guys. See the date before commenting. I know my errors now!
     
    Robke28 likes this.
  18. A Christmas Hangar

    A Christmas Hangar Member

    Joined:
    Sep 15, 2015
    Messages:
    2,545
    Likes Received:
    1,492
    Trophy Points:
    113
    Gender:
    Male
    Location:
    Kepler452B
    Home Page:
    But thanks anywere
     
  19. Stefanvld

    Stefanvld Sr. Mod Staff Member Sr. Moderator

    Joined:
    May 24, 2014
    Messages:
    10,690
    Likes Received:
    4,996
    Trophy Points:
    113
    Gender:
    Male
    Occupation:
    i do stuff
    Location:
    Behind you.
    Locked then.
     
Thread Status:
Not open for further replies.