Aprilon
http://aprilon.org/forum/

A bit of HALP.
http://aprilon.org/forum/viewtopic.php?f=6&t=2336
Page 1 of 1

Author:  |LorD| Kiraly [ 14 Aug 2012, 22:38 ]
Post subject:  A bit of HALP.

Hi, i know it's been a while, but i started making bukkit plugins, and i have a list of things i cannot figure out. So, i thought this would be the best place to ask.
First of all: How can i make player-independent classes? Like, if i set them ALL in the main class, if one users toggles something, it toggles for ALL users, but i want them independent. An example with toggleing would be nice.
Second: I cannot get player listeners to work, i mean onPlayerMove() or onPlayerJoin() simply does not react. I implemented the Listener thing, as well as @EventHandler. Still does not do the job.
Third: Run methods from args. What i mean, i have a command. Like /hello. If i add if(args[0]==command){SOMETHINGHERE(args[0]; return true;)}} it skips this part and goes straight to the next thing
Any help is appriciated. Thank you.

Author:  ManIAc [ 16 Aug 2012, 16:05 ]
Post subject:  Re: A bit of HALP.

As far as I know, the way to solve problem 1, is to have an array that keeps track of which player has enabled what. So make a Map or something, and then every time a player tries to do something, you can test if he's "allowed" to do that.
I'm by far not the most experienced in this, but that would be how I would do it. :)
As for 2, might want to post some source code, unless someone else has an idea.
3, I don't think I understand what your problem is? :) Could you explain it in more detail?

Author:  |LorD| Kiraly [ 18 Aug 2012, 19:06 ]
Post subject:  Re: A bit of HALP.

Well, thanks for the replies, but now i pretty much solved all of those things. Now, the other issue i encounter is a plugin which repeats every single event twice, for some reason. Here is my source code:
Spoiler: Show
Code:
import java.util.ArrayList;

import java.util.Random;

import org.bukkit.ChatColor;
import org.bukkit.Material;


import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.weather.WeatherChangeEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class kiralycraft extends JavaPlugin implements Listener{
   ArrayList<String> non_thor_players = new ArrayList<String>();
   boolean weather=false;//<- FALSE = storm
    @EventHandler
   
       public void onRainStart(WeatherChangeEvent event) {
      
          weather=true;
          getServer().broadcastMessage(ChatColor.RED+"A storm is comming... You'd better hide from Thor!");
           return;
      
      }
    @EventHandler
       public void onRainStop(WeatherChangeEvent event){
          
              weather=false;
              getServer().broadcastMessage(ChatColor.RED+"Thor takes a break, and thanks KiralyCraft for making this plugin!");
               return;
          
       }
      
   public String get_a_random_player_name(){
      
      Random random = new Random();
      Player[] players = getServer().getOnlinePlayers();
      if (players.length!=0){
      int randomInt = random.nextInt(players.length);
      String name = players[randomInt].getName();
      
      return name;
      }
      return null;
   }
   public void repeat() {
      if (weather==true){
         
      String name = get_a_random_player_name();
      if(non_thor_players.indexOf(name)==-1){} else{return;}
      if(getServer().getPlayer(name).getLocation().getY()>40){
      getServer().broadcastMessage(ChatColor.RED+"Thor has chosen to torture "+name);
      
      getServer().getPlayer(name).getWorld().strikeLightning(getServer().getPlayer(name).getLocation());
      
      }
      
      }
      return;
      }
   
    public void onEnable(){
      
      
      
        this.getServer().getPluginManager().registerEvents(this, this);
       
        //-------------------------TIMER------------------------
        getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {

            public void run() {
                repeat();
                
            }

        }, 10L, 700L);
        //------------------------------------------------------
       
       
       
    }
    @EventHandler
   public void onBlockPlace (BlockPlaceEvent event){
//       getServer().broadcastMessage(non_thor_players.toString());
       Material mat = event.getBlockPlaced().getType();
       Player player = event.getPlayer();
       if (non_thor_players.indexOf(player.getName())==-1){
       if (mat==Material.EMERALD_BLOCK){
          getServer().broadcastMessage(ChatColor.GOLD+"Player "+player.getName()+" has placed an Emerald Block, he is no longer hit by Thor!");
          non_thor_players.add(player.getName());
          return;
       }
       }
    }
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
       
        if (command.getName().equalsIgnoreCase("torture")){
           getServer().broadcastMessage(ChatColor.RED+"Thor has been called by "+sender.getName());
           repeat();
          
        }
        return true;
    }
   
}

Author:  Bloody [ 18 Aug 2012, 19:24 ]
Post subject:  Re: A bit of HALP.

You mean the weather events onRainStart and onRainStop both fire at the same time? You'r not checking if the weather actually changed to thunderstorm or changed to sunny. ;)
They are both WeatherChangeEvent's so they both get called.

If you just wan't to check if something is in an (Array)List<T> use .contains.

Code:
boolean weather=false;//<- FALSE = storm
Dat codingstyle. :v

Author:  ManIAc [ 18 Aug 2012, 21:23 ]
Post subject:  Re: A bit of HALP.

Blood, stop being an arse and let him use whatever syntax he wants. :D
There are certain "rules", but it's funny that every programmer still does it however he wants. :D

Author:  Bloody [ 18 Aug 2012, 21:34 ]
Post subject:  Re: A bit of HALP.

ManIAc wrote:
There are certain "rules", but it's funny that every programmer still does it however he wants. :D

Of course but once you start using distributed SCMs and collaborate with other developers you need to decide on a coding style everyone uses, in which case readability (C-Style) usually beats speed/lazyness. :D

It's easier to 'weed it out' now than to be booted off a project because "suddenly" your code is unreadable by others. Take it as a "heads up". ;)

Author:  ManIAc [ 19 Aug 2012, 09:52 ]
Post subject:  Re: A bit of HALP.

Of course, but usually when you do projects, you agree on a standard rather than use best practice, since they might be doing stuff differently from the standard, because it fits them better, and then you need to adapt to them, not the other way around. :P
But of course I agree with you, I just like to be cross. :)

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/