I've recently started a project for a hub server i'm working on, i have everything sorted but there is one thing i want to add that i don't know how to do yet.. If a player shoots another player it will make them disappear for 10 seconds then reappear and it will send the player a message saying ("You Popped" + p.getName());
Listen on the projectile hit, get the shooter, hide the target from the shooter, scheduler something to unhide the player after 10 seconds
Rufflez Shooter.hidePlayer(targetPlayer). Obviously you would replace "Shooter" with the player who shot and "targetPlayer" with the player that was shot..
Can you help me with the targetPlayer please, i don't understand. I get the shooter part but not the targetPlayer thanks
EntityDamageByEntityEvent* Code: @EventHandler public void onPlayerDamageByPlayerArrow(EntityDamageByEntityEvent event) { if((!(event.getDamager() instanceof Arrow)) || (!(event.getEntity() instanceof Player))) return; Arrow a = (Arrow) event.getDamager(); Player shooter = (Player) a.getShooter(); Player shot = (Player) event.getEntity(); //Possibly check if 'shot' is already popped on 'shooter's screen, as 'shot' might be invisible for shooter but will still get damaged by the arrow. //Pop 'shot', as they got shot by 'shooter'. new BukkitRunnable() { @Override public void run() { //Unpop 'shot' from 'shooter'. } }.runTaskLater(plugin, 200L); shooter.sendMessage("You popped " + shot.getName() + "!"); }
Rufflez Did you register the event? How do you have "plugin" initialized? Is this in your main class? Please share your whole "Hub.java" class. in a pastebin
Ugh, please don't spoonfeed people code. They won't learn anything. Code: Caused By: java.lang.IllegalArgumentException: Plugin cannot be null
I haven't initialized the plugin, i haven't worked with this stuff before. How do i initialize it? Thanks
I disagree; People can learn from 'spoonfeeding', it's just a matter of how you give it to them. But I couldn't stand idly by while this person was fed the wrong event for handling this. Edit: I also explained a lot of what I was doing in the code that I was giving him, in hopes he'd actually read the comments. Is the event handling occuring in the class that extends JavaPlugin and has onEnable()? If it does, you can use 'this', if not, I'd do this in your main class: Code: public static PluginNameHere plugin; This takes the instance of your class and puts it in a static variable that can be accessed without using a method from an external class. If the runnable is being used in an external class, you simply type Code: PluginNameHere.plugin in place of the 'plugin' that was in the original code.
Then, "You're using the wrong event. Here is a JavaDoc link to the correct event." Not, "Here is all the correct code, copy-paste my work instead of writing it yourself and learning." Also, you generally shouldn't teach new Java coders to use a static reference like that. They should be avoiding static at all costs in the first place.
I did not tell him to copy-paste my code instead of writing it himself. I simply gave him the code to look it over and see how it would be done. It's completely up to the OP whether or not he chooses to learn from reading the code or not. Obscurity and basic algorithms, while helpful, may not communicate well to others. I don't know about you, but I personally understood how things worked when I saw the completely put-together code when I was learning. It helps me run through the process to see what their thinking might have been. Granted it may not be neither the best way of learning nor the most efficient, but it was how I learned and some people may learn like that too.
Garris0n Drkmaster83 There is a way for getting the Plugin instance that is actually within the BukkitAPI (PluginManager#getPlugin()), which will return the same thing as creating your singleton, just in the form of a 'Plugin' variable. Not sure why people use the singleton as opposed to a method of the API, I guess it'd just be personal preference, I'd use dependency injection to eliminate the use of statics overall (even though Bukkit designs the JavaPlugin as a singleton-like object (having one, global instance, not being able to create more then one of the same)), as DI was just the way I learned and prefer.