Minecraft Player On World Join event - java

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

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?

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

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

I am unable to see the components inside my frame

Hi I'm coding an alarm application.
I have a snooze class which gets called when the alarm sounds. It used to work fine before using JMF.But after using JMF I see only the outer frame of my snooze UI.
I tried starting a new thread for the class but got the same result. I'm pasting here code of the classes in which I think the problem is in.
Please help me solve the problem.
*IsTime class. Which checks whether it is the time to sound alarm. *
package alarm;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class IsTime {
int hrs;
int min;
int sec;
GregorianCalendar clk=new GregorianCalendar();
Calendar gtl= Calendar.getInstance();
mp3 mix=new mp3();
public void makeReady(int h,int m,int s,String ampm){
Calendar c1=Calendar.getInstance();
c1.set(Calendar.HOUR_OF_DAY,h);
c1.set(Calendar.MINUTE, m);
c1.set(Calendar.SECOND,s);
if("PM".equals(ampm)){
c1.set(Calendar.AM_PM, Calendar.PM);
if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.PM){
System.out.println("now is pm");
if(c1.after(Calendar.getInstance())){
check(c1);
}
c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1);
check(c1);
}
}
if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.AM){
System.out.println("now is am");
if(c1.after(Calendar.getInstance())){
check(c1);
}
c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1);
check(c1);
}
if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.PM){
c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1);
check(c1);
}
}
public void check(Calendar ch){
System.out.println("got to check");
System.out.println(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
System.out.println(ch.get(Calendar.HOUR_OF_DAY));
while(!(Calendar.getInstance().after(ch))){
}
Snooze snz=new Snooze();
snz.start();
mix.start();
}
public void makeReady(int mis){
Calendar sz=Calendar.getInstance();
sz.set(Calendar.SECOND, 0);
sz.set(Calendar.MINUTE, Calendar.getInstance().get(Calendar.MINUTE)+mis);
check(sz);
}
}
*Media class which plays song when it is time *
package alarm;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.media.*;
import java.io.*;
import java.net.URL;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
class mp3 extends Thread
{
private URL url;
private MediaLocator mediaLocator;
private Player playMP3;
static String mp3;
static String mp4;
public mp3()
{
try{
System.out.println(System.getProperty("os.name"));
System.out.println("before"+mp3);
if(System.getProperty("os.name").equals("Linux")){
mp4="file://".concat(mp3);
}
else{
mp4="file:///".concat(mp3);
}
System.out.println(mp4);
this.url = new URL(mp4);
}catch(java.net.MalformedURLException e)
{System.out.println(e.getMessage());}
}
public void run()
{
try{
System.out.println(url);
mediaLocator = new MediaLocator(url);
playMP3 = Manager.createPlayer(mediaLocator);
}catch(java.io.IOException e)
{System.out.println(e.getMessage());
}catch(javax.media.NoPlayerException e)
{System.out.println(e.getMessage());}
playMP3.addControllerListener(new ControllerListener()
{
public void controllerUpdate(ControllerEvent e)
{
if (e instanceof EndOfMediaEvent)
{
playMP3.stop();
playMP3.close();
}
}
}
);
playMP3.realize();
playMP3.start();
}
}
*Snooze class where the real problem is *
package alarm;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Snooze extends Thread implements ActionListener{
JFrame sozef=new JFrame("Snooze");
JLabel tl=new JLabel("After");
JLabel ml=new JLabel("mins");
JComboBox tiec=new JComboBox();
JButton soo=new JButton("Snooze");
JButton stp=new JButton("Stop");
mp3 mi=new mp3();
Snooze sz;
public void run(){
sozef.setSize(500,700);
sozef.setLayout(null);
tl.setBounds(50,50,50,50);
tiec.setBounds(50,90,50,30);
ml.setBounds(120,90,50,50);
soo.setBounds(50,130,90,30);
stp.setBounds(50, 170, 90, 30);
JcAdd(tiec);
sozef.add(tl);
sozef.add(tiec);
sozef.add(ml);
sozef.add(soo);
sozef.add(stp);
soo.addActionListener(this);
sozef.setVisible(true);
sozef.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void JcAdd(JComboBox jc){
for(int i=0;i<=10;i++)
jc.addItem(Integer.toString(i));
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==soo){
int ma=Integer.parseInt(tiec.getSelectedItem().toString());
mi.stop();
IsTime sz=new IsTime();
sz.makeReady(ma);
}
mi.stop();
}
} ![this is the frame I see when the snooze is being called][1]
This is not the correct way to implement a delay:
while(!(Calendar.getInstance().after(ch))){
}
In fact, it's going to cause serious problems.
Try calculating the number of milliseconds between your target date and the current time, and passing that value to Thread.sleep. Your computer's case fans will thank you. And it may very well be the case that your busy loop is starving the AWT event thread, thus preventing your frame from showing.

Categories