Skip to content

Commit

Permalink
Add PlayerAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Apr 13, 2022
1 parent b15a98f commit 32db7d3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.azisaba.azipluginmessaging.api;

import net.azisaba.azipluginmessaging.api.entity.PlayerAdapter;
import net.azisaba.azipluginmessaging.api.server.PacketSender;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -29,6 +30,19 @@ public interface AziPluginMessaging {
@NotNull
Server getServer();

/**
* Returns the player adapter for class.
* <p>Generally, class should be one of these (depending on the environment):
* <ul>
* <li>org.bukkit.entity.Player</li>
* <li>com.velocitypowered.api.proxy.Player</li>
* </ul>
* @param clazz the platform dependent player class
* @return the player adapter
* @param <T> the player class
*/
<T> PlayerAdapter<T> getPlayerAdapter(@NotNull Class<T> clazz);

interface Proxy {
/**
* Returns the packet sender instance for provided server.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.azisaba.azipluginmessaging.api.entity;

import org.jetbrains.annotations.NotNull;

public interface PlayerAdapter<T> {
/**
* Returns the player object from platform dependent player object.
* @param player the platform dependent player
* @return the player object
*/
@NotNull
Player get(@NotNull T player);
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "net.azisaba.azipluginmessaging"
version = "1.0.0"
version = "1.1.0"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import net.azisaba.azipluginmessaging.api.AziPluginMessaging;
import net.azisaba.azipluginmessaging.api.Logger;
import net.azisaba.azipluginmessaging.api.entity.PlayerAdapter;
import net.azisaba.azipluginmessaging.api.server.PacketSender;
import net.azisaba.azipluginmessaging.spigot.entity.PlayerImpl;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
Expand Down Expand Up @@ -33,6 +35,13 @@ public AziPluginMessagingSpigot(@NotNull SpigotPlugin plugin) {
return server;
}

@SuppressWarnings("unchecked")
@Override
public <T> PlayerAdapter<T> getPlayerAdapter(@NotNull Class<T> clazz) {
if (!Player.class.equals(clazz)) throw new IllegalArgumentException("This environment does not support " + clazz.getTypeName());
return (PlayerAdapter<T>) (PlayerAdapter<Player>) PlayerImpl::new;
}

public static class ServerImpl implements Server {
@Override
public @NotNull PacketSender getPacketSender() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package net.azisaba.azipluginmessaging.velocity;

import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import net.azisaba.azipluginmessaging.api.AziPluginMessaging;
import net.azisaba.azipluginmessaging.api.Logger;
import net.azisaba.azipluginmessaging.api.entity.PlayerAdapter;
import net.azisaba.azipluginmessaging.api.protocol.Protocol;
import net.azisaba.azipluginmessaging.api.server.PacketSender;
import net.azisaba.azipluginmessaging.velocity.entity.PlayerImpl;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
Expand Down Expand Up @@ -36,6 +39,13 @@ public AziPluginMessagingVelocity(@NotNull ProxyServer server, @NotNull org.slf4
return new Server() {};
}

@SuppressWarnings("unchecked")
@Override
public <T> PlayerAdapter<T> getPlayerAdapter(@NotNull Class<T> clazz) {
if (!Player.class.equals(clazz)) throw new IllegalArgumentException("This environment does not support " + clazz.getTypeName());
return (PlayerAdapter<T>) (PlayerAdapter<Player>) PlayerImpl::new;
}

public class ProxyImpl implements Proxy {
@Override
public @NotNull Optional<PacketSender> getPacketSenderForServer(@NotNull String serverName) {
Expand Down

0 comments on commit 32db7d3

Please sign in to comment.