Server Selector / Teleporter

Discussion in 'Plugin Development' started by FriskTheDeveloper, Jun 27, 2017.

Thread Status:
Not open for further replies.
  1. Hello my name is Frisk. I am in need of assistance with my latest plugin. I was using this video to work on:

    This is my code i have a problem where when i click it teleports you to the location im looking at but i want it to bring up a gui and i can click an item in the gui and it teleports me to the location. When i left click it does this. When i right click it says in red "Nothing to pass through" I tested this out and it teleported me into a cave.

    Code:
    package Inventory;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    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 teleportInWord(Player player,int x, int y, int z){
         player.teleport(new Location(player.getWorld(),x,y,z));
       }
      
       private void openGUI(Player player) {
         Inventory inv = Bukkit.createInventory(null, 9, ChatColor.AQUA + "Teleporter");
        
         ItemStack survival = new ItemStack(Material.DIAMOND_CHESTPLATE);
         ItemMeta survivalMeta = survival.getItemMeta();
         ItemStack creative = new ItemStack(Material.GRASS);
         ItemMeta creativeMeta = creative.getItemMeta();
        
         survivalMeta.setDisplayName(ChatColor.AQUA + "Factions");
         survival.setItemMeta(survivalMeta);
        
         creativeMeta.setDisplayName(ChatColor.GREEN + "Creative");
         creative.setItemMeta(creativeMeta);
        
         inv.setItem(3, survival);
         inv.setItem(5, creative);
        
         player.openInventory(inv);
       }
      
       @EventHandler
       public void onInventoryClick(InventoryClickEvent event) {
         if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Teleporter"));
         return;
         Player player = (Player) event.getWhoClicked();
         event.setCancelled(true);
        
         if(event.getCurrentItem()==null ||  event.getCurrentItem().getType()==Material.AIR||!event.getCurrentItem().hasItemMeta()){
           player.closeInventory();
           return;
         }
         switch(event.getCurrentItem().getType()) {
         case DIAMOND_CHESTPLATE:
           teleportInWord(player, 50, 50, 50);
           player.closeInventory();
           player.sendMessage(String.format("%sTeleported to %sFactions%s",ChatColor.RED,ChatColor.AQUA,ChatColor.RED));
           break;
         case GRASS:
           teleportInWord(player, 800, 50, -200);
           player.closeInventory();
           player.sendMessage(String.format("%sTeleported to %sCreative%s",ChatColor.RED,ChatColor.GREEN,ChatColor.RED));
         default:
           player.closeInventory();
           break;
         }
       }
      
       @EventHandler
       public void onPlayerJoin(PlayerJoinEvent event) {
         event.getPlayer().getInventory().addItem(new ItemStack(Material.COMPASS));
       }
      
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
       Action a= event.getAction();
       ItemStack is = event.getItem();
       if(a == Action.PHYSICAL || is == null || is.getType()==Material.AIR);
       return;
      
       if (is.getType() == Material.COMPASS)
         openGUI(event.getPlayer());
    }
    }
    Please post what can fix this thank you
     
    Last edited by a moderator: Jun 27, 2017
  2. Offline

    mehboss

    I believe this is worldedit, or another plugin that has a config set on compasses to act as a "//thru" item. Also, youtube videos do help; however, they are a bad habit to get into. ;)
     
  3. Offline

    Zombie_Striker

    @mehboss
    More than that, they normally do more harm than good. Especially when it comes to the BCBroz, youtube videos normally teach bad techniques and are outdated.

    @FriskTheDeveloper
    As Mehboss posted, its a world edit feature. You will need to disable that feature.
     
  4. Offline

    Horsey

    mehboss and Zombie_Striker like this.
  5. Thank you everyone for what you have said. Also if you can, Will using this "explode your server":

    public final Logger logger = Logger.getLogger("Minecraft"); public static Youtube plugin; @Override public vid onDisable() { PluginDescriptionFile pdfFile = this.getDEscription(); this.logger.info(pdfFile.getName() + "Has been Disabled!"); } @Override public void onEnable() { PluginDescriptionFile pdfFile = this.getDescription(); this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion() + "Has been Enabled!");

    P.S.
    I copied this from the Bc Broz video in my browser.
     
  6. Offline

    Zombie_Striker

    @FriskTheDeveloper
    There is a lot wrong with that code:
    1. You should not steal minecraft's logger. If you need to log something (which you don't), then use getLogger();
    2. Nothing should ever be static when it comes to writing bukkit plugins. Doing so shows you are either lazy or are creating an API.
    3. You do not need an instance of your own plugin in a class in this instance. Just remove the plugin line.
    4. It is void, not vid. Void means that the function should not return any variable.
    5. There is no need for the onDisable at all, since you don't need to log your own plugin.
    6. Same for the onEnable, unless you need to register the events for the class (if you have them).
    Do not use the BCBroz. Again, he teaches bad techniques such as this.
     
  7. Thanks for the info. I have been trying to code for months now (About 7-8). I am thinking about learning SQL. Will that be helpful with coding java plugins in any way? @Zombie_Striker @mehboss
     
  8. Offline

    Zombie_Striker

    @FriskTheDeveloper
    Not at all. That is similar to asking if learning Russian will help you learn French; the languages are completely different. The best thing you can do for writing plugins would be to just write plugins; the more time you spend learning the API the more knowledgeable you will be in it.
     
  9. I am now following this guy called source code. He has updated tutorials on java coding. I have learnt alot. Like how to give a player items and if statements and how to give a player health and events such as interacting and moving and if they move or interact with something i can give them health or execute another line of code.

    How do you learn the api quickly? I have the website for the api but it literally does not give much information just 4 or 6 words and thats it! It doesnt explain techniques or how to use it in certain methods etc.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  10. Offline

    Zombie_Striker

    @FriskTheDeveloper
    For learning about all the methods and classes bukkit has to offer, look at this:
    All hail the Java docs!
    https://hub.spigotmc.org/javadocs/bukkit/

    All classes and variables have descriptions which explain what they are used for.


    As for learning techniques, this is something you will have to mostly learn on your own. You can come here and we will help show you better ways to do certain things for specific, but besides that you will need to, in a sense, just think up changes you can make. Most of the improvements that I have made with my plugins while learning were from just re-writing blocks of code in different ways.
     
  11. Thank you for your help. I am glad to know that i have someone helping me!
     
    Last edited by a moderator: Jun 29, 2017
  12. Offline

    Horsey

    @Zombie_Striker The javadocs really are not that descriptive, especially if you're a new programmer :/
     
  13. Horsey i agree.
     
Thread Status:
Not open for further replies.

Share This Page