Development Assistance Binoculars help

Discussion in 'Plugin Help/Development/Requests' started by Rostik002, May 25, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Rostik002

    hello, I am trying to create a plugin for when a player right clicks with a golden hoe, it will zoom in and put a pumpkin on his head. It saves what the player had on their head in a variable in order to put it back after he right-clicks again



    Code:
    package me.Rostik002;
    
    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.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class ListenerClass implements Listener {
     
        public ListenerClass(MainClass plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin); 
        }
    
     
        @EventHandler
        public void onClick(PlayerInteractEvent event) {
            boolean isZoom = false;
            Player player = event.getPlayer();
            if (player.getItemInHand().getType() == Material.GOLD_HOE && (event.getAction() == Action.RIGHT_CLICK_BLOCK ||  event.getAction() == Action.RIGHT_CLICK_AIR)){
                ItemStack head = player.getInventory().getHelmet();
                isZoom = true;
                player.getInventory().setHelmet(new ItemStack(Material.PUMPKIN, 1));
       
                player.setWalkSpeed(-0.15F);
             
                if (player.getItemInHand().getType() == Material.GOLD_HOE && (event.getAction() == Action.RIGHT_CLICK_BLOCK ||  event.getAction() == Action.RIGHT_CLICK_AIR && isZoom == true)){
                    player.setWalkSpeed(0.2F);
                    player.getInventory().setHelmet(head);
                 
                }
            }
    
         
         
        }
     
    }
    
     
  2. Offline

    pie_flavor

    @Rostik002 and your problem is...
    one posts in the development assistance forum when they require development assistance. What is your specific problem?
     
    TheLovelySlothMan likes this.
  3. Offline

    Rostik002

    Sorry, you're right, I should have explained it a bit.. I was kinda tired when I sent this.
    I am new to Java, and I have tried making this plugin, but for some reason it does not work, Eclipse did not return any errors/warnings so I could not find the problem.
    I was hoping for some more experienced coders to help me out.

    The plugin is complied properly and the server launches with it successfully, it is just the actuall plugin does not work.
    When I right click with the golden hoe, nothing happens.
     
  4. Offline

    pie_flavor

    @Rostik002 Could you also provide your main class code?
    Pro tip: Instead of using [code], use [code=java]. That way you get code like this.
    Code:
    public class ThisIsAClass extends JavaPlugin {
        static int field;
     
  5. Offline

    Rostik002

    Code:
    package me.Rostik002;
    
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MainClass extends JavaPlugin {
    
        @Override
        public void onEnable() {
            new ListenerClass(this);
        }
       
        @Override
        public void onDisable() {
           
            }
    }
    
     
  6. Offline

    pie_flavor

    @Rostik002 First of all, you don't need to type isZoom == true, you can just put isZoom. == true is useless. Second, you don't even need it in the first place - since the second if-statement is inside the first, and you only change isZoom inside it, isZoom will always be true when it is called on.
    Third, you might understand what you mean with || and && in the same bracket, but the JVM doesn't. Use parentheses.
     
  7. Offline

    Rostik002

    Ive changed my code a bit, but it still doesn't work.
    Code:
    package me.Rostik002;
    
    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.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class ListenerClass implements Listener {
      
        public ListenerClass(MainClass plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);  
        }
    
      
        @EventHandler
        public void onClick(PlayerInteractEvent event) {
          
            Player player = event.getPlayer();
            if (player.getItemInHand().getType() == Material.GOLD_HOE){
                ItemStack head = player.getInventory().getHelmet();
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
              
              
                player.getInventory().setHelmet(new ItemStack(Material.PUMPKIN, 1));
        
                player.setWalkSpeed(-0.15F);
              
              
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK){
                    player.setWalkSpeed(0.2F);
                    player.getInventory().setHelmet(head);
              
                }
              
              
                    else if(event.getAction() == Action.RIGHT_CLICK_AIR){
                        player.setWalkSpeed(0.2F);
                        player.getInventory().setHelmet(head);  
                    }
              
              
              
              
                else if(event.getAction() == Action.RIGHT_CLICK_AIR) {
              
                  
                player.getInventory().setHelmet(new ItemStack(Material.PUMPKIN, 1));
            
                player.setWalkSpeed(-0.15F);
                  
                  
              
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK){
                    player.setWalkSpeed(0.2F);
                    player.getInventory().setHelmet(head);
                }
                    else if(event.getAction() == Action.RIGHT_CLICK_AIR){
                        player.setWalkSpeed(0.2F);
                        player.getInventory().setHelmet(head);  
                  
                }
                }
                }
            }
    }
    }
    
    I have noticed that it does zoom in sometimes but only when spamming right click on a block.
     
    Last edited: May 27, 2015
  8. Offline

    terturl890

    You could always do an update method if all else fails. It may put a little stress on the server

    Main file:
    Code:
    public List<String> scoping = new ArrayList<String>();
    Inside the onEnable():
    Code:
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("Test"), new Runnable() {
                @SuppressWarnings("deprecation")
                @Override
                public void run() {
                    for(Player p : Bukkit.getOnlinePlayers()) {
                        if(scoping.contains(p.getName())) {
                            p.setWalkSpeed(-0.15F);
                        } else {
                            p.setWalkSpeed(0.2F);
                        }
                    }
                }
            }, 20L, 20L);
    
    Listener:
    Code:
    @EventHandler
        public void onClick(PlayerInteractEvent e) {
       
            Player p = e.getPlayer();
            if(p.getItemInHand().getType() == Material.GOLD_HOE) {
                if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    if(!scoping.contains(p.getName())) {
                        scoping.add(p.getName());
                    } else {
                        scoping.remove(p.getName());
                    }
                }
           
            }
        }
    
    this will have an automatic updater that will check if the player is in the scoping list. If they are, they are scoped. If they are not, there walking speed returns to normal. This was all tested and works! I did not add the helmet changing, im pretty sure you can do that ;). You can mess around with the numbers in the repeating task to fit your needs. Hope this helps!

    Also, when registering a listener I would put it on the onEnable like so,

    Code:
    PluginManager pm = Bukkit.getPluginManger();
    
    pm.registerEvents(listener, plugin);
    
    so its all compact and in one place ;) but that's just me
     
  9. Offline

    Rostik002

    OMG Thank you, kind sir! The world needs more people like you, and not people that go "Learn Java, noob"
    I did have to do some troubleshooting because there where errors when I copied it over.
    My code now looks like this (only changed main class) :
    Code:
    package me.Rostik002;
    
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MainClass extends JavaPlugin {
    
        public static List<String> scoping = new ArrayList<String>();
    
        @Override
        public void onEnable() {
            Plugin plugin = this;
            new ListenerClass(this);
        
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("ErebusBinoculars"), new Runnable() {
    
                @Override
                public void run() {
                    for(Player p : Bukkit.getOnlinePlayers()) {
                        if(scoping.contains(p.getName())) {
                            p.setWalkSpeed(-0.15F);
                        } else {
                            p.setWalkSpeed(0.2F);
                        }
                    }
                }
            }, 20L, 20L);
        }
    
        @Override
        public void onDisable() {
        
            }
    }
    
    Just one more question, how do I make it stayed zoomed in for longer, or until I right click again? What happens is that is zooms in/out for a very short time and then goes to normal

    Like a delay between code but without stoping the server.

    (also, a smaller question, how do I get the dark version of [ code ] ? It looks way easier to read imo)
     
  10. Offline

    pie_flavor

    @Rostik002 Maybe add a cooldown of some sort. Atm this would theoretically work if the person only clicked for 1 tick.
     
  11. Offline

    terturl890

    Hmm.... In my code, it would stay zoombed in until you right clicked. Can I see the listener class? Cause you would have to make sure your listener class is similar to what I had. This would make it so you would need to right click again to be taking out of the scoping ArrayList; which in turn, took you out of scoping.
     
Thread Status:
Not open for further replies.

Share This Page