Lightning Arrow but the lightning does spawn in - java

So i got this code of somebody and he said it would work i was grateful (still am) but it seems that the code doesnt work in someway
This is my Main file
package me.Pixel;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.entity.Arrow;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
public Main plugin;
public List<String> spells = new ArrayList<String>();
public getTargets getTargets = new getTargets();
#Override
public void onEnable() {
plugin = this;
getCommand("bow").setExecutor(new BowCommand());
}
#EventHandler
public void onEntityShootBow(EntityShootBowEvent event) {
if(event.getProjectile() instanceof Arrow) {
Arrow arrow = (Arrow) event.getProjectile();
new LightningShot(arrow).runTaskTimer(this, 0, 1);
}
}
}
And this is my LightningShot file
package me.Pixel;
import org.bukkit.entity.Arrow;
import org.bukkit.scheduler.BukkitRunnable;
public class LightningShot extends BukkitRunnable {
private Arrow arrow;
private int tick = 1;
public LightningShot(Arrow arrow) {
this.arrow = arrow;
}
#Override
public void run() {
if (arrow == null || arrow.isOnGround() || tick++ > 20 * 10) {
this.cancel();
} else {
arrow.getWorld().strikeLightning(arrow.getLocation());
}
}
}
To be clear This is what i want it to look like but then instead of an Snowball the arrow that comes out of the bow.
I hope you guys can help me. It would be awesome.

It seems that you haven't registered your listener. Even though the listener is your main class you still need to register it in your onEnable method with:
this.getServer().getPluginManager().registerEvents(this, this);
Then the code will spawn lightning at the arrow's location as intended (I tested the code).

Related

PlayerJoinEvent - Create a welcome message Spigot 1.12.2 (Java)

I don't understand why he always returns to me "You never played" then the first time ok, but the second I don't understand
I'm trying to make a condition:
If the player has already played I do nothing: no message the player receives nothing!
If the player has never played : Welcome message + He receives an item + and I get his position to explode fireworks
And I would have liked to add a counter of players who join in the second condition
Commands Class:
package fr.antyss77.pandowelcome;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
public class Commands implements Listener {
private Main main;
#EventHandler
public void PlayerJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();
Location loc = player.getLocation();
boolean hasPlayed = player.hasPlayedBefore();
if (hasPlayed != true) {
player.sendMessage("You've already played !");
} else {
player.sendMessage("You never played !");
player.getInventory().addItem(new ItemStack(Material.SAND, 12));
loc.getWorld().spawnEntity(loc, EntityType.SPLASH_POTION);
}
}
}
Main class:
package fr.antyss77.pandowelcome;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
private String color(String arg) {
return ChatColor.translateAlternateColorCodes('&', arg);
}
public void onEnable() {
Bukkit.getConsoleSender().sendMessage(color("&6[&ePandoWelcomer&6] &ehas just started."));
getServer().getPluginManager().registerEvents(new Commands(), this);
}
public void onDisable() {
Bukkit.getConsoleSender().sendMessage(color("&6[&cPandoWelcomer&6] &ehas been shut down."));
}
}
If anyone can help me understand my mistake I will be infinitely grateful, thank you in advance!
if (hasPlayed != true) should be if (hasPlayed == true) as you have it doing the opposite of what you want.
Your code would look like:
package fr.antyss77.pandowelcome;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
public class Commands implements Listener {
private Main main;
#EventHandler
public void PlayerJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();
Location loc = player.getLocation();
boolean hasPlayed = player.hasPlayedBefore();
if (hasPlayed == true) {
player.sendMessage("You've already played !");
} else {
player.sendMessage("You never played !");
player.getInventory().addItem(new ItemStack(Material.SAND, 12));
loc.getWorld().spawnEntity(loc, EntityType.SPLASH_POTION);
}
}
}

How do you run an event after a advancement is completed?

I'm trying to code a plugin where the border follows a player and doubles everytime they get an advancement. The problem is I don't know how to detect when someone gets an advancement and keep it in the same class. Here is the current code.
survivalBorders.class
package sc458.survivalborders;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldBorder;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class survivalBorders extends JavaPlugin {
public void onEnable() {
World world = Bukkit.getWorld("survival");
WorldBorder border = world.getWorldBorder();
double borderSize = border.getSize();
while(true) {
if(PlayerAdvancementDoneEvent) {
border.setSize(borderSize*2);
}
}
}
public void onDisable() {
}
}
MoveEvent.class
package sc458.survivalborders;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldBorder;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
public class MoveEvent implements Listener {
public void onMove(PlayerMoveEvent e) {
Player p = e.getPlayer();
World world = Bukkit.getWorld("survival");
WorldBorder border = world.getWorldBorder();
int locX = p.getLocation().getBlockX();
int locZ = p.getLocation().getBlockZ();
border.setCenter(locX, locZ);
}
}
Try to call the PlayerMoveEvent with a Listener and then you can just everytime he moves (event gets triggered) -> set the border again. So basically when his coordinates change, the border will immediantly follow you.
public class MoveEvent implements Listener {
#EventHandler
public void onMove(PlayerMoveEvent e) {
Player p = e.getPlayer();
WorldBorder border = world.getWorldBorder();
int locX = p.getLocation().getBlockX();
int locZ = p.getLocation().getBlockZ();
border.setCenter(locX, locZ);
}
}

I want to kill a player if he went higher then 24 blocks

I want to make a Spigot Plugin that kills a Player if he is over 24 Blocks.
I already made a code but it doesn't work
Here is the full code, I don't need to register a new class because I wrote the event in the main class
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class MainFFA extends JavaPlugin implements Listener{
#Override
public void onEnable() {
getServer().getConsoleSender().sendMessage("_____________START_____________");
getServer().getConsoleSender().sendMessage("_____________START_____________");
getServer().getConsoleSender().sendMessage("_____________START_____________");
getServer().getConsoleSender().sendMessage("_____________START_____________");
super.onEnable();
}
#Override
public void onDisable() {
getServer().getConsoleSender().sendMessage("_________STOP_________-");
super.onDisable();
}
#EventHandler
public void onPlayerDead(PlayerMoveEvent event) {
Player p = event.getPlayer();
if(p.getLocation().getBlockY() > 80) {
p.setHealth(0);
}
else {
p.sendMessage("Wenn du das siehst dann bist du unter 80 blöcken hoch");
}
}
}
To solve this issue, you must remember to register your listeners, or else bukkit will never see them.
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
}
Add that line to the onEnable method, and you should be fine

How to run an click event on simple GWT Button via Mockito and make the Test pass

Here is the code:
I know by default this code below overrides the default behavier of the Button as Mock object. So it won't work. I stuck on that.
What's the best practice to pass the Test with an already existing view like that ? How can I run that small part of view within an anonymous inner class like that ? How can I simulate the click event but at the same time keep the code like it is.
I don't want to have a seperate getter, just to invoke button.click() method
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwtmockito.GwtMock;
import com.google.gwtmockito.GwtMockitoTestRunner;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
#RunWith(GwtMockitoTestRunner.class)
public class ExceptionTest {
#GwtMock
private ClickHandler clickHandler;
#GwtMock
private Button btn;
private GUI gui;
#Before
public void setUp() {
when(btn.addClickHandler(any(ClickHandler.class))).thenAnswer(new Answer() {
public Object answer(InvocationOnMock aInvocation) throws Throwable {
clickHandler = (ClickHandler) aInvocation.getArguments()[0];
return null;
}
});
gui = new GUI();
}
#Test
public void runner() {
clickHandler.onClick(new ClickEvent(){});
Assert.assertEquals(1, gui.getSize());
}
}
GUI.java
class GUI extends FlowPanel {
private final Button button = new Button("OK");
private int size;
public GUI() {
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent clickEvent) {
size += 1;
}
});
}
public int getSize(){
return this.size;
}
}

Bukkit Right-Clicking Stone does not return anything

How do you detect if someone has right-clicked block (For example, stone) in Bukkit? I have tried many suggestions but none of them return anything.
Main.java
package me.ben.test;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
#Override
public void onEnable() {
this.getServer().getPluginManager().registerEvents(new Click(), this);
getLogger().info("The Plugin Has Been Enabled!");
}
#Override
public void onDisable() {
getLogger().info("The Plugin Has Been Disabled!");
}
public boolean onCommand(CommandSender sender, Command cmd, String label,
String[] args) {
if (cmd.getName().equalsIgnoreCase("hello") && sender instanceof Player) {
Player player = (Player) sender;
player.sendMessage("Hello, " + player.getName() + "!");
return true;
} else if (cmd.getName().equalsIgnoreCase("isonline")
&& args.length == 1) {
Player target = Bukkit.getServer().getPlayer(args[0]);
if (target == null) {
sender.sendMessage(ChatColor.AQUA + "Player " + args[0]
+ " is not online.");
return true;
} else if (target != null) {
sender.sendMessage(ChatColor.AQUA + "Player " + args[0]
+ " is online.");
return true;
} else return false;
}
return false;
}
}
Click.java
package me.ben.test;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
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.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Click extends JavaPlugin implements Listener {
#EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
event.getPlayer().sendMessage("Heyyyyyyy");
}
}
I have tried other ones but this one was the last one I tried and It seems the simplest.
I added the line into the onEnable section.
You could use an Event and check when the player interacts:
#EventHandler //you NEED to have #EventHandler before any event
public void playerInteract(PlayerInteractEvent e){
//this will be called automatically by bukkit whenever a player interacts
if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
//the player right-clicked a block
Material m = e.getClickedBlock().getType(); //get the block type clicked
if(m.equals(Material.STONE){
//the block clicked was stone.
}
}
}
Then you need to make sure that the class this is in implements Listener:
public class MyListenerClass implements Listener{
//events & other methods
}
and then you need to register events in the onEnable() method of your Main class (the one that extends JavaPlugin)
public class MyMainClass extends JavaPlugin{
#Override
public void onEnable(){
//enabled
this.getServer().getPluginManager().registerEvents(new MyListenerClass(), this));
}
}

Categories