Verified Commit ba2a60ae authored by Yannick Schinko's avatar Yannick Schinko
Browse files

Added Sponge code

parent baef2700
package team.aura_dev.aurasudo.platform.sponge;
import java.nio.file.Path;
import org.spongepowered.api.Platform;
import org.spongepowered.api.Sponge;
import team.aura_dev.aurasudo.api.AuraSudo;
import team.aura_dev.aurasudo.platform.common.AuraSudoBase;
import team.aura_dev.aurasudo.platform.common.player.PlayerManagerCommon;
import team.aura_dev.aurasudo.platform.sponge.listener.PlayerEventListenerSponge;
import team.aura_dev.aurasudo.platform.sponge.player.PlayerManagerSponge;
import team.aura_dev.lib.multiplatformcore.DependencyClassLoader;
import team.aura_dev.lib.multiplatformcore.download.DependencyList;
public class AuraSudoSponge extends AuraSudoBase {
private final AuraSudoSpongeBootstrap plugin;
public AuraSudoSponge(
DependencyClassLoader classLoader, AuraSudoSpongeBootstrap plugin, Path configDir) {
super(classLoader, configDir);
this.plugin = plugin;
// Instance is initialized
AuraSudo.setApi(this);
}
@Override
public String getBasePlatform() {
return "Sponge";
}
@Override
public String getPlatformVariant() {
return Sponge.getPlatform().getContainer(Platform.Component.IMPLEMENTATION).getName();
}
@Override
public DependencyList getEarlyDependencies(DependencyList dependencyList) {
// Super only adds CONFIGURATE_HOCON, which we have
return dependencyList;
}
@Override
protected PlayerManagerCommon generatePlayerManager() {
return new PlayerManagerSponge();
}
@Override
protected void registerEventListeners() {
Sponge.getEventManager().registerListeners(plugin, new PlayerEventListenerSponge(this));
}
}
package team.aura_dev.aurasudo.platform.sponge;
import com.google.inject.Inject;
import java.nio.file.Path;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.plugin.Plugin;
import team.aura_dev.aurasudo.platform.common.AuraSudoBaseBootstrap;
import team.aura_dev.aurasudo.platform.common.AuraSudoBootstrapper;
@Plugin(
id = AuraSudoBootstrapper.ID,
name = AuraSudoBootstrapper.NAME,
version = AuraSudoBootstrapper.VERSION,
description = AuraSudoBootstrapper.DESCRIPTION,
url = AuraSudoBootstrapper.URL,
authors = {AuraSudoBootstrapper.AUTHOR})
public class AuraSudoSpongeBootstrap {
private final AuraSudoBaseBootstrap bootstrappedPlugin;
@Inject
public AuraSudoSpongeBootstrap(@ConfigDir(sharedRoot = false) Path configDir) {
final AuraSudoBootstrapper bootstrapper = new AuraSudoBootstrapper();
bootstrapper.initializePlugin(this, configDir);
bootstrappedPlugin = bootstrapper.getPlugin();
}
@Listener
public void preInit(GamePreInitializationEvent event) {
bootstrappedPlugin.preInitPlugin();
}
@Listener
public void init(GameInitializationEvent event) {
bootstrappedPlugin.initPlugin();
}
}
package team.aura_dev.aurasudo.platform.sponge.listener;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.network.ClientConnectionEvent;
import team.aura_dev.aurasudo.platform.common.AuraSudoBase;
import team.aura_dev.aurasudo.platform.common.player.PlayerManagerCommon;
public class PlayerEventListenerSponge {
private final PlayerManagerCommon playerManager;
public PlayerEventListenerSponge(AuraSudoBase plugin) {
this.playerManager = plugin.getPlayerManager();
}
@Listener(order = Order.EARLY)
public void onPlayerJoinAsync(ClientConnectionEvent.Auth event) {
playerManager.fromNativePlayer(event.getProfile());
}
@Listener(order = Order.LAST)
public void onPlayerLeave(ClientConnectionEvent.Disconnect event) {
playerManager.unloadPlayer(event.getTargetEntity().getUniqueId());
}
}
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
package team.aura_dev.aurasudo.platform.sponge.player;
import java.util.UUID;
import javax.annotation.Nonnull;
import lombok.NonNull;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;
import team.aura_dev.aurasudo.platform.common.player.PlayerDataCommon;
public class PlayerDataSponge extends PlayerDataCommon {
public PlayerDataSponge(@NonNull UUID uuid) {
super(uuid, getPlayerFromUUID(uuid).getName());
}
@Nonnull
@Override
public String getDisplayName() {
return getPlayerFromUUID(uuid).getDisplayNameData().displayName().get().toString();
}
private static Player getPlayerFromUUID(UUID uuid) {
return Sponge.getServer().getPlayer(uuid).get();
}
}
package team.aura_dev.aurasudo.platform.sponge.player;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nonnull;
import lombok.NonNull;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.profile.GameProfile;
import team.aura_dev.aurasudo.api.player.PlayerData;
import team.aura_dev.aurasudo.platform.common.player.PlayerManagerCommon;
public class PlayerManagerSponge extends PlayerManagerCommon {
@Override
protected Optional<PlayerData> generatePlayerData(@Nonnull @NonNull UUID uuid) {
return Optional.of(new PlayerDataSponge(uuid));
}
@Override
protected BasePlayerData nativePlayerToBasePlayerData(Object player)
throws IllegalArgumentException {
UUID uuid;
String playerName;
if (player instanceof BasePlayerData) {
return (BasePlayerData) player;
} else if (player instanceof Player) {
final Player nativePlayer = (Player) player;
uuid = nativePlayer.getUniqueId();
playerName = nativePlayer.getName();
} else if (player instanceof GameProfile) {
final GameProfile nativePlayer = (GameProfile) player;
uuid = nativePlayer.getUniqueId();
playerName = nativePlayer.getName().get();
} else {
throw new IllegalArgumentException(
"The passed player object ("
+ player
+ ") is not of type "
+ Player.class.getName()
+ " or "
+ GameProfile.class.getName());
}
return new BasePlayerData(uuid, playerName);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment