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

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

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

Armorstand Teleporting After Reloging(Spigot)

Why does my armorstand teleport after relogging into my server? For example, before relogging: https://imgur.com/a/gQQcLb9, and after relogging: https://imgur.com/a/xOeAJOz. I'm trying to make it so the armorstand will teleport once you right click a bone without having to relog.
Here's my Main code:
package com.jason.sbitems;
import com.jason.sbitems.bows.Bonemerang;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
public class SBItems extends JavaPlugin {
#Override
public void onEnable() {
getServer().getPluginManager().registerEvents(new Bonemerang(), this);
ItemManager.init();
getCommand("get").setExecutor(new GetCommand());
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[SBItems Plugin]: Plugin is Enabled!");
}
#Override
public void onDisable() {
getServer().getConsoleSender().sendMessage(ChatColor.RED + "[SBItems Plugin]: Plugin is Disabled!");
}
}
Here's my class code:
import org.bukkit.Material;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
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;
import org.bukkit.util.EulerAngle;
import org.bukkit.util.Vector;
public class Bonemerang implements Listener {
#EventHandler
public static void onPlayerInteract(PlayerInteractEvent e) {
Player p = e.getPlayer();
if (e.getAction() == Action.RIGHT_CLICK_AIR) {
if (p.getInventory().getItemInMainHand().equals(new ItemStack(Material.BONE))) {
ArmorStand stand = (ArmorStand) p.getWorld().spawnEntity(p.getLocation(), EntityType.ARMOR_STAND);
Vector teleportTo = p.getLocation().getDirection().normalize().multiply(10);
stand.hasArms();
stand.setGravity(false);
stand.setItemInHand(new ItemStack(Material.BONE));
stand.setRightArmPose(new EulerAngle(Math.toRadians(0), Math.toRadians(120), Math.toRadians(0)));
stand.teleport(p.getLocation().add(teleportTo));
}
}
}
}
Also, is there a way to make it so that it will teleport after 10 seconds?

Minecraft Player On World Join event

Im newbie to Java, how can i handle current player joined the world?
package page.a0x77.kubecraft;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.player.*;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
#Mod(
modid = Kubecraft.MOD_ID,
name = Kubecraft.MOD_NAME,
version = Kubecraft.VERSION
)
public class Kubecraft {
#SubscribeEvent
public void playerLoggedInEvent(EntityJoinWorldEvent event) {
// ClientCommandHandler.instance.executeCommand(Minecraft.getMinecraft().player, "your command");
System.out.println("TEST");
}
}
I want to make auto auth on player joined, send message to chat on join.
You should use:
#EventBusSubscriber
public static class Class {
#SubscribeEvent
public static void onEvent(EntityJoinWorldEvent event) {
if ((event.getEntity() instanceof PlayerEntity)) {
LogManager.getLogger().info("Joined!");
}
}
}
I thought maybe you'd need the instance of the player to be able to get it to work.
...
#Mod(
modid = Kubecraft.MOD_ID,
name = Kubecraft.MOD_NAME,
version = Kubecraft.VERSION
)
public class Kubecraft {
...
#SubscribeEvent
public static void onEvent(EntityJoinWorldEvent event) {
Timer timer = new Timer(3000, new ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent e) {
if(!sent) Minecraft.getMinecraft().player.sendChatMessage("/setblock ~ ~ ~ grass");
sent = true;
}
});
timer.setRepeats(false); // Only execute once
if(!sent) {
timer.start();
}
}
}
...

Lightning Arrow but the lightning does spawn in

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).

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