Skip to content

Commit

Permalink
improve PlayerListenerCommon
Browse files Browse the repository at this point in the history
  • Loading branch information
burdoto committed Feb 25, 2024
1 parent df51585 commit 2c6cc8d
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.mineaurion.aurionchat.bukkit.command.ChatCommand;
import com.mineaurion.aurionchat.bukkit.listeners.ChatListener;
import com.mineaurion.aurionchat.bukkit.listeners.CommandListener;
import com.mineaurion.aurionchat.bukkit.listeners.LoginListener;
import com.mineaurion.aurionchat.bukkit.listeners.PlayerListener;
import com.mineaurion.aurionchat.common.AbstractAurionChat;
import com.mineaurion.aurionchat.common.config.ConfigurationAdapter;
import com.mineaurion.aurionchat.common.logger.JavaPluginLogger;
Expand Down Expand Up @@ -40,7 +40,7 @@ public void onDisable() {
@Override
protected void registerPlatformListeners() {
PluginManager pluginManager = plugin.getServer().getPluginManager();
pluginManager.registerEvents(new LoginListener(this), plugin);
pluginManager.registerEvents(new PlayerListener(this), plugin);
pluginManager.registerEvents(new CommandListener(this), plugin);
pluginManager.registerEvents(new ChatListener(this), plugin);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package com.mineaurion.aurionchat.bukkit.listeners;

import com.mineaurion.aurionchat.bukkit.AurionChat;
import com.mineaurion.aurionchat.common.listeners.LoginListenerCommon;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;

public class LoginListener extends LoginListenerCommon<AurionChat> implements Listener {

public LoginListener(AurionChat plugin){
super(plugin);
}

@EventHandler(priority = EventPriority.LOW)
public void onPlayerKick(PlayerKickEvent event){
playerLeaving(plugin.getPlayerFactory().wrap(event.getPlayer()));
}

@EventHandler(priority = EventPriority.LOW)
public void onPlayerQuit(PlayerQuitEvent event){
playerLeaving(plugin.getPlayerFactory().wrap(event.getPlayer()));
}

@EventHandler(priority = EventPriority.LOW)
public void onPlayerJoin(PlayerJoinEvent event){
playerJoin(plugin.getPlayerFactory().wrap(event.getPlayer()));
}
}
package com.mineaurion.aurionchat.bukkit.listeners;

import com.mineaurion.aurionchat.bukkit.AurionChat;
import com.mineaurion.aurionchat.common.listeners.PlayerListenerCommon;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;

public class PlayerListener extends PlayerListenerCommon<AurionChat> implements Listener {

public PlayerListener(AurionChat plugin){
super(plugin);
}

@EventHandler(priority = EventPriority.LOW)
public void onPlayerKick(PlayerKickEvent event){
playerLeaving(plugin.getPlayerFactory().wrap(event.getPlayer()));
}

@EventHandler(priority = EventPriority.LOW)
public void onPlayerQuit(PlayerQuitEvent event){
playerLeaving(plugin.getPlayerFactory().wrap(event.getPlayer()));
}

@EventHandler(priority = EventPriority.LOW)
public void onPlayerJoin(PlayerJoinEvent event){
playerJoin(plugin.getPlayerFactory().wrap(event.getPlayer()));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.mineaurion.aurionchat.common.listeners;

import com.mineaurion.aurionchat.api.AurionPacket;
import com.mineaurion.aurionchat.common.AbstractAurionChat;
import com.mineaurion.aurionchat.common.AurionChatPlayer;
import com.mineaurion.aurionchat.common.player.Player;
import net.kyori.adventure.text.Component;

import static com.mineaurion.aurionchat.api.AurionPacket.Type.*;

public abstract class PlayerListenerCommon<T extends AbstractAurionChat> {
public T plugin;

public PlayerListenerCommon(T plugin){
this.plugin = plugin;
}

protected void playerJoin(Player player){
AurionChatPlayer plr = new AurionChatPlayer(player, plugin);
this.plugin.getAurionChatPlayers().putIfAbsent(player.getUUID(), plr);
//todo: needs join/leave message pattern
String txt = plr.getBestName() + " joined the game";
Component component = Component.text(txt);
AurionPacket.Builder packet = AurionPacket.event(plr, JOIN, component)
.detail(txt);
try {
this.plugin.getChatService().send(packet);
} catch (Throwable e) {
plugin.getlogger().severe("Failed to send PlayerJoin event", e);
}
}

protected void playerLeaving(Player player){
AurionChatPlayer plr = this.plugin.getAurionChatPlayers().remove(player.getUUID());
//todo: needs join/leave message pattern
String txt = plr.getBestName() + " left the game";
Component component = Component.text(txt);
AurionPacket.Builder packet = AurionPacket.event(plr, LEAVE, component)
.detail(txt);
try {
this.plugin.getChatService().send(packet);
} catch (Throwable e) {
plugin.getlogger().severe("Failed to send PlayerLeave event", e);
}
}

protected void playerAdvancement(Player player, String sanitizedAdvancementMessage){
AurionChatPlayer plr = this.plugin.getAurionChatPlayers().remove(player.getUUID());
Component component = Component.text(sanitizedAdvancementMessage);
AurionPacket.Builder packet = AurionPacket.event(plr, ADVANCEMENT, component)
.detail(sanitizedAdvancementMessage);
try {
this.plugin.getChatService().send(packet);
} catch (Throwable e) {
plugin.getlogger().severe("Failed to send PlayerAdvancement event", e);
}
}

protected void playerDeath(Player player, String sanitizedDeathMessage){
AurionChatPlayer plr = this.plugin.getAurionChatPlayers().remove(player.getUUID());
Component component = Component.text(sanitizedDeathMessage);
AurionPacket.Builder packet = AurionPacket.event(plr, DEATH, component)
.detail(sanitizedDeathMessage);
try {
this.plugin.getChatService().send(packet);
} catch (Throwable e) {
plugin.getlogger().severe("Failed to send PlayerDeath event", e);
}
}

public T getPlugin(){
return this.plugin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.mineaurion.aurionchat.common.logger.Slf4jPluginLogger;
import com.mineaurion.aurionchat.fabric.command.ChatCommand;
import com.mineaurion.aurionchat.fabric.listeners.ChatListener;
import com.mineaurion.aurionchat.fabric.listeners.LoginListener;
import com.mineaurion.aurionchat.fabric.listeners.PlayerListener;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand Down Expand Up @@ -35,7 +35,7 @@ public void onInitializeServer() {

@Override
protected void registerPlatformListeners() {
LoginListener loginListener = new LoginListener(this);
PlayerListener loginListener = new PlayerListener(this);
ChatListener chatListener = new ChatListener(this);

ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> loginListener.onPlayerJoin(handler.getPlayer()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.mineaurion.aurionchat.fabric.listeners;

import com.mineaurion.aurionchat.common.listeners.LoginListenerCommon;
import com.mineaurion.aurionchat.fabric.AurionChat;
import net.minecraft.server.network.ServerPlayerEntity;

public class LoginListener extends LoginListenerCommon<AurionChat> {
public LoginListener(AurionChat plugin){
super(plugin);
}

public void onPlayerJoin(ServerPlayerEntity player){
playerJoin(plugin.getPlayerFactory().wrap(player));
}

public void onPlayerQuit(ServerPlayerEntity player){
this.playerLeaving(plugin.getPlayerFactory().wrap(player));
}
}
package com.mineaurion.aurionchat.fabric.listeners;

import com.mineaurion.aurionchat.common.listeners.PlayerListenerCommon;
import com.mineaurion.aurionchat.fabric.AurionChat;
import net.minecraft.server.network.ServerPlayerEntity;

public class PlayerListener extends PlayerListenerCommon<AurionChat> {
public PlayerListener(AurionChat plugin){
super(plugin);
}

public void onPlayerJoin(ServerPlayerEntity player){
playerJoin(plugin.getPlayerFactory().wrap(player));
}

public void onPlayerQuit(ServerPlayerEntity player){
this.playerLeaving(plugin.getPlayerFactory().wrap(player));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.mineaurion.aurionchat.common.logger.PluginLogger;
import com.mineaurion.aurionchat.forge.command.ChatCommand;
import com.mineaurion.aurionchat.forge.listeners.ChatListener;
import com.mineaurion.aurionchat.forge.listeners.LoginListener;
import com.mineaurion.aurionchat.forge.listeners.PlayerListener;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minecraftforge.common.MinecraftForge;
Expand Down Expand Up @@ -68,7 +68,7 @@ public void serverStopped(ServerStoppedEvent event) {

@Override
protected void registerPlatformListeners() {
MinecraftForge.EVENT_BUS.register(new LoginListener(this));
MinecraftForge.EVENT_BUS.register(new PlayerListener(this));
MinecraftForge.EVENT_BUS.register(new ChatListener(this));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.mineaurion.aurionchat.forge.listeners;

import com.mineaurion.aurionchat.common.listeners.LoginListenerCommon;
import com.mineaurion.aurionchat.forge.AurionChat;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;


public class LoginListener extends LoginListenerCommon<AurionChat> {

public LoginListener(AurionChat plugin){
super(plugin);
}

@SubscribeEvent
public void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) {
playerJoin(plugin.getPlayerFactory().wrap((ServerPlayer) event.getEntity()));
}

@SubscribeEvent
public void onPlayerQuit(PlayerEvent.PlayerLoggedOutEvent event){
playerLeaving(plugin.getPlayerFactory().wrap((ServerPlayer) event.getEntity()));
}
}
package com.mineaurion.aurionchat.forge.listeners;

import com.mineaurion.aurionchat.common.listeners.PlayerListenerCommon;
import com.mineaurion.aurionchat.forge.AurionChat;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;


public class PlayerListener extends PlayerListenerCommon<AurionChat> {

public PlayerListener(AurionChat plugin){
super(plugin);
}

@SubscribeEvent
public void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) {
playerJoin(plugin.getPlayerFactory().wrap((ServerPlayer) event.getEntity()));
}

@SubscribeEvent
public void onPlayerQuit(PlayerEvent.PlayerLoggedOutEvent event){
playerLeaving(plugin.getPlayerFactory().wrap((ServerPlayer) event.getEntity()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.mineaurion.aurionchat.common.logger.PluginLogger;
import com.mineaurion.aurionchat.sponge.command.ChatCommand;
import com.mineaurion.aurionchat.sponge.listeners.ChatListener;
import com.mineaurion.aurionchat.sponge.listeners.LoginListener;
import com.mineaurion.aurionchat.sponge.listeners.PlayerListener;
import org.apache.logging.log4j.LogManager;
import org.spongepowered.api.Server;
import org.spongepowered.api.Sponge;
Expand Down Expand Up @@ -53,7 +53,7 @@ public void onServerStop(StoppingEngineEvent<Server> event){
@Override
protected void registerPlatformListeners() {
EventManager eventManager = Sponge.eventManager();
eventManager.registerListeners(this.container, new LoginListener(this));
eventManager.registerListeners(this.container, new PlayerListener(this));
eventManager.registerListeners(this.container, new ChatListener(this));
}

Expand Down
Loading

0 comments on commit 2c6cc8d

Please sign in to comment.