New topic New reply  Page 1 of 1
 [ 7 posts ] 
User avatar
12 Jun 2011, 14:15
102 Posts

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.

_________________
Look, im just a Signature....

And containing universe boot-up sequence...
Do not alter at ANY circumstances....
If you have any problems, please shut the universe down and restart....

Microsoft (Gods Division)

WHERE IS MY FUCKING COOKIE?!?!
Server Admin Server Admin
User avatar
25 Nov 2009, 22:49
1890 Posts

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?
User avatar
12 Jun 2011, 14:15
102 Posts

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;
    }
   
}

_________________
Look, im just a Signature....

And containing universe boot-up sequence...
Do not alter at ANY circumstances....
If you have any problems, please shut the universe down and restart....

Microsoft (Gods Division)

WHERE IS MY FUCKING COOKIE?!?!
Hacker Hacker
User avatar
20 Mar 2010, 20:58
467 Posts

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
Server Admin Server Admin
User avatar
25 Nov 2009, 22:49
1890 Posts

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
Hacker Hacker
User avatar
20 Mar 2010, 20:58
467 Posts

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". ;)
Server Admin Server Admin
User avatar
25 Nov 2009, 22:49
1890 Posts

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. :)
New topic New reply  Page 1 of 1
 [ 7 posts ] 
Who is online
Users browsing this forum: No registered users and 28 guests