Skip to content

Commit

Permalink
add some javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneBeee committed Oct 4, 2024
1 parent ebd4591 commit c6fd2af
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 6 deletions.
25 changes: 24 additions & 1 deletion src/main/java/com/shanebeestudios/coreapi/event/PacketEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,55 @@
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;

/**
* Abstract class for packet events
*/
@SuppressWarnings("unused")
public abstract class PacketEvent extends Event implements Cancellable {

private final Packet<?> packet;
private final Player player;
private boolean cancelled = false;

public PacketEvent(Packet<?> packet, Player player) {
PacketEvent(Packet<?> packet, Player player) {
super(true);
this.packet = packet;
this.player = player;
}

/**
* Get the packet from the event
*
* @return Packet from event
*/
public Packet<?> getPacket() {
return this.packet;
}

/**
* Get the player that sent/received the packet
*
* @return Player that sent/received packet
*/
public Player getPlayer() {
return this.player;
}

/**
* Check if event was cancelled
*
* @return True if cancelled
*/
@Override
public boolean isCancelled() {
return this.cancelled;
}

/**
* Cancel the event
*
* @param cancel Whether to cancel this event
*/
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
package com.shanebeestudios.coreapi.event;

import com.shanebeestudios.coreapi.listener.PlayerPacketListener;
import net.minecraft.network.protocol.Packet;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

/**
* Called when a packet is received from a client
* <p>To listen to this event you will first need to execute {@link PlayerPacketListener#registerListener(Plugin)}</p>
*/
@SuppressWarnings("unused")
public class PacketInboundEvent extends PacketEvent {

private static final HandlerList HANDLERS = new HandlerList();

/**
* Why do I need to put a description? It's hidden
* @hidden no
* @param packet p
* @param player p
*/
public PacketInboundEvent(Packet<?> packet, Player player) {
super(packet, player);
}

/**
* Get the handler list
*
* @return Handler list
*/
@Override
public @NotNull HandlerList getHandlers() {
return HANDLERS;
}

/**
* Get the handler list
*
* @return Handler list
*/
public static HandlerList getHandlerList() {
return HANDLERS;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
package com.shanebeestudios.coreapi.event;

import com.shanebeestudios.coreapi.listener.PlayerPacketListener;
import net.minecraft.network.protocol.Packet;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

/**
* Called when a packet is sent to a client
* <p>To listen to this event you will first need to execute {@link PlayerPacketListener#registerListener(Plugin)}</p>
*/
@SuppressWarnings("unused")
public class PacketOutboundEvent extends PacketEvent {

private static final HandlerList HANDLERS = new HandlerList();

/**
* Why do I need to put a description? It's hidden
*
* @param packet packet
* @param player player
* @hidden no
*/
public PacketOutboundEvent(Packet<?> packet, Player player) {
super(packet, player);
}

/**
* Get the handler list
*
* @return Handler list
*/
@Override
public @NotNull HandlerList getHandlers() {
return HANDLERS;
}

/**
* Get the handler list
*
* @return Handler list
*/
public static HandlerList getHandlerList() {
return HANDLERS;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Events to listen for
*/
package com.shanebeestudios.coreapi.event;
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,37 @@
import io.netty.channel.ChannelPromise;
import net.minecraft.network.protocol.Packet;
import net.minecraft.server.level.ServerPlayer;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

/**
* Listener for packets
*/
public class PlayerPacketListener implements Listener {

private static boolean registered = false;

/** Register a listener for {@link PacketEvent packet events}
* @param plugin Your plugin to enable this listener
*/
@SuppressWarnings("unused")
public static void registerListener(Plugin plugin) {
if (registered) {
throw new IllegalStateException("Listener is already registered!");
}
Bukkit.getPluginManager().registerEvents(new PlayerPacketListener(), plugin);
registered = true;
}

private PlayerPacketListener() {
}

@EventHandler
private void onPlayerJoin(PlayerJoinEvent event) {
Player bukkitPlayer = event.getPlayer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Listeners that will need to be registered by a {@link org.bukkit.plugin.Plugin plugin}
*/
package com.shanebeestudios.coreapi.listener;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
@SuppressWarnings({"CallToPrintStackTrace", "unused"})
public class ReflectionUtils {

private ReflectionUtils() {
}

/**
* Get the value of a field from an object
*
Expand Down
62 changes: 57 additions & 5 deletions src/main/java/com/shanebeestudios/coreapi/util/TaskUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,89 @@
@SuppressWarnings("unused")
public class TaskUtils {

private TaskUtils() {
}

private static Plugin PLUGIN;
private static final BukkitScheduler SCHEDULER = Bukkit.getScheduler();

/** Initialize this util with a Plugin
/**
* Initialize this util with a Plugin
*
* @param plugin Plugin used in schedulers
*/
public static void init(Plugin plugin) {
if (PLUGIN != null) {
throw new IllegalStateException("Plugin is already initialized.");
}
PLUGIN = plugin;
}

/**
* Run a task at the end of the current tick
*
* @param runnable Task to run
*/
public static void runTaskEndOfTick(Runnable runnable) {
runTaskLater(runnable, 0);
}

public static void runTaskLater(Runnable runnable, long delay) {
SCHEDULER.runTaskLater(PLUGIN, runnable, delay);
}

/**
* Run a task at the end of the current tick async
*
* @param runnable Task to run
*/
public static void runTaskEndOfTickAsynchronously(Runnable runnable) {
runTaskLaterAsynchronously(runnable, 0);
}

/** Run a task later
* @param runnable Task to run
* @param delay Delay in ticks before task runs
*/
public static void runTaskLater(Runnable runnable, long delay) {
if (PLUGIN == null) {
throw new IllegalStateException("Plugin is not initialized.");
}
SCHEDULER.runTaskLater(PLUGIN, runnable, delay);
}

/** Run a task later async
* @param runnable Task to run
* @param delay Delay in ticks before task runs
*/
public static void runTaskLaterAsynchronously(Runnable runnable, long delay) {
if (PLUGIN == null) {
throw new IllegalStateException("Plugin is not initialized.");
}
SCHEDULER.runTaskLaterAsynchronously(PLUGIN, runnable, delay);
}

/**
* Run a task timer
*
* @param runnable Task to run
* @param delay Delay in ticks before task runs
* @param period Period in ticks how often task runs
*/
public static void runTaskTimer(Runnable runnable, long delay, long period) {
if (PLUGIN == null) {
throw new IllegalStateException("Plugin is not initialized.");
}
SCHEDULER.runTaskTimer(PLUGIN, runnable, delay, period);
}

/**
* Run a task timer async
*
* @param runnable Task to run
* @param delay Delay in ticks before task runs
* @param period Period in ticks how often task runs
*/
public static void runTaskTimerAsynchronously(Runnable runnable, long delay, long period) {
if (PLUGIN == null) {
throw new IllegalStateException("Plugin is not initialized.");
}
SCHEDULER.runTaskTimerAsynchronously(PLUGIN, runnable, delay, period);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* General utility classes
*/
package com.shanebeestudios.coreapi.util;

0 comments on commit c6fd2af

Please sign in to comment.