Skip to content

Commit

Permalink
add more events
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Oct 3, 2024
1 parent 21d1186 commit ab9bbc0
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 8 deletions.
15 changes: 13 additions & 2 deletions src/main/java/me/hsgamer/yatpa/command/PlayerTabComplete.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package me.hsgamer.yatpa.command;

import me.hsgamer.yatpa.event.TeleportPlayerListEvent;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

public interface PlayerTabComplete {
static List<String> getPlayerNames() {
List<String> playerNames = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
playerNames.add(player.getName());
}
TeleportPlayerListEvent event = new TeleportPlayerListEvent(playerNames);
Bukkit.getPluginManager().callEvent(event);
return playerNames;
}

static @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException {
if (args.length == 1) {
String argName = args[0];
return Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
return getPlayerNames().stream()
.filter(name -> name.toLowerCase(Locale.ROOT).startsWith(argName.toLowerCase(Locale.ROOT)))
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.hsgamer.hscore.bukkit.utils.MessageUtils;
import me.hsgamer.yatpa.YATPA;
import me.hsgamer.yatpa.event.PreTeleportRequestEvent;
import me.hsgamer.yatpa.request.RequestEntry;
import me.hsgamer.yatpa.request.RequestStatus;
import me.hsgamer.yatpa.request.RequestType;
Expand Down Expand Up @@ -48,7 +49,14 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String commandLab
return false;
}

Player targetPlayer = sender.getServer().getPlayer(args[0]);
String targetName = args[0];
Player targetPlayer = sender.getServer().getPlayer(targetName);

PreTeleportRequestEvent preTeleportRequestEvent = new PreTeleportRequestEvent(senderPlayer, targetName, targetPlayer);
plugin.getServer().getPluginManager().callEvent(preTeleportRequestEvent);
if (preTeleportRequestEvent.isCancelled()) {
return false;
}

if (targetPlayer == null) {
MessageUtils.sendMessage(sender, plugin.getMessageConfig().getPlayerNotFound());
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/me/hsgamer/yatpa/event/PreTeleportRequestEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package me.hsgamer.yatpa.event;

import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PreTeleportRequestEvent extends Event implements Cancellable {
private static final HandlerList HANDLERS = new HandlerList();
private final Player player;
private final String targetPlayerName;
private final @Nullable Player targetPlayer;
private boolean cancelled;

public PreTeleportRequestEvent(Player player, String targetPlayerName, @Nullable Player targetPlayer) {
this.player = player;
this.targetPlayerName = targetPlayerName;
this.targetPlayer = targetPlayer;
}

public static HandlerList getHANDLERS() {
return HANDLERS;
}

public Player getPlayer() {
return player;
}

@Nullable
public Player getTargetPlayer() {
return targetPlayer;
}

public String getTargetPlayerName() {
return targetPlayerName;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}

@Override
public @NotNull HandlerList getHandlers() {
return HANDLERS;
}
}
29 changes: 29 additions & 0 deletions src/main/java/me/hsgamer/yatpa/event/TeleportPlayerListEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.hsgamer.yatpa.event;

import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class TeleportPlayerListEvent extends Event {
private static final HandlerList HANDLERS = new HandlerList();
private final List<String> playerNames;

public TeleportPlayerListEvent(List<String> playerNames) {
this.playerNames = playerNames;
}

public static HandlerList getHandlerList() {
return HANDLERS;
}

public List<String> getPlayerNames() {
return playerNames;
}

@Override
public @NotNull HandlerList getHandlers() {
return HANDLERS;
}
}
10 changes: 5 additions & 5 deletions src/main/java/me/hsgamer/yatpa/teleport/TeleportManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public void init() {
}

Scheduler.plugin(plugin)
.sync()
.runEntityTask(
player,
() -> plugin.getMainConfig().teleportEffect().forEach(effect -> effect.apply(player))
);
.sync()
.runEntityTask(
player,
() -> plugin.getMainConfig().teleportEffect().forEach(effect -> effect.apply(player))
);
}
}, effectPeriod, effectPeriod);
}
Expand Down

0 comments on commit ab9bbc0

Please sign in to comment.