mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 06:28:54 +00:00
move more shared code to common
This commit is contained in:
10 files changed
+222
-80
No files matched your search
@@ -16,8 +16,12 @@ import net.draycia.carbon.api.util.SourcedAudience;
|
||||
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
||||
import net.draycia.carbon.common.util.CloudUtils;
|
||||
import net.draycia.carbon.common.util.ListenerUtils;
|
||||
import net.draycia.carbon.common.util.PlayerUtils;
|
||||
import net.draycia.carbon.sponge.listeners.SpongeChatListener;
|
||||
import net.draycia.carbon.sponge.listeners.SpongePlayerDeathListener;
|
||||
import net.draycia.carbon.sponge.listeners.SpongePlayerJoinListener;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.moonshine.message.IMessageRenderer;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -38,9 +42,9 @@ import org.spongepowered.plugin.builtin.jvm.Plugin;
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonChatSponge implements CarbonChat {
|
||||
|
||||
private static final Set<Class<?>> LISTENER_CLASSES = Set.of(
|
||||
SpongeChatListener.class
|
||||
);
|
||||
private static final Set<Class<?>> LISTENER_CLASSES = Set.of(SpongeChatListener.class,
|
||||
SpongePlayerDeathListener.class, SpongePlayerJoinListener.class);
|
||||
|
||||
private static final int BSTATS_PLUGIN_ID = 11279;
|
||||
|
||||
private final CarbonMessageService messageService;
|
||||
@@ -79,37 +83,32 @@ public final class CarbonChatSponge implements CarbonChat {
|
||||
for (final Class<?> clazz : LISTENER_CLASSES) {
|
||||
game.eventManager().registerListeners(this.pluginContainer, this.injector.getInstance(clazz));
|
||||
}
|
||||
|
||||
//metricsFactory.make(BSTATS_PLUGIN_ID);
|
||||
|
||||
// Listeners
|
||||
ListenerUtils.registerCommonListeners(this.injector);
|
||||
|
||||
// Commands
|
||||
CloudUtils.registerCommands(this.injector);
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onInitialize(final StartingEngineEvent<Server> event) {
|
||||
// Player data saving
|
||||
Sponge.asyncScheduler().submit(Task.builder()
|
||||
.interval(5, TimeUnit.MINUTES)
|
||||
.plugin(this.pluginContainer)
|
||||
.execute(this::savePlayers)
|
||||
.execute(() -> PlayerUtils.savePlayers(this.carbonServerSponge, this.userManager))
|
||||
.build());
|
||||
|
||||
// Load channels
|
||||
((CarbonChannelRegistry) this.channelRegistry()).loadChannels();
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onDisable(final StoppingEngineEvent<Server> event) {
|
||||
this.savePlayers();
|
||||
}
|
||||
|
||||
private void savePlayers() {
|
||||
for (final var player : this.server().players()) {
|
||||
this.userManager().savePlayer(((WrappedCarbonPlayer) player).carbonPlayerCommon()).thenAccept(result -> {
|
||||
if (result.player() == null) {
|
||||
this.server().console().sendMessage(result.reason());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public UserManager<CarbonPlayerCommon> userManager() {
|
||||
return this.userManager;
|
||||
PlayerUtils.savePlayers(this.carbonServerSponge, this.userManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package net.draycia.carbon.sponge.listeners;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.common.config.PrimaryConfig;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.entity.DestructEntityEvent;
|
||||
import org.spongepowered.api.event.filter.cause.First;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class SpongePlayerDeathListener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final PrimaryConfig primaryConfig;
|
||||
|
||||
@Inject
|
||||
public SpongePlayerDeathListener(
|
||||
final CarbonChat carbonChat,
|
||||
final PrimaryConfig primaryConfig
|
||||
) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.primaryConfig = primaryConfig;
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onPlayerDeath(final DestructEntityEvent.Death event, final @First Player player) {
|
||||
// Early exit in case "hide muted join / leave messages when muted" is disabled
|
||||
if (!this.primaryConfig.hideMutedJoinLeaveQuit()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ComponentPlayerResult<CarbonPlayer> result = this.carbonChat.server().player(player.uniqueId()).join();
|
||||
|
||||
if (result.player() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final CarbonPlayer carbonPlayer = result.player();
|
||||
|
||||
if (!carbonPlayer.muteEntries().isEmpty()) {
|
||||
event.setMessageCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package net.draycia.carbon.sponge.listeners;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.common.config.PrimaryConfig;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.network.ServerSideConnectionEvent;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class SpongePlayerJoinListener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final PrimaryConfig primaryConfig;
|
||||
|
||||
@Inject
|
||||
public SpongePlayerJoinListener(
|
||||
final CarbonChat carbonChat,
|
||||
final PrimaryConfig primaryConfig
|
||||
) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.primaryConfig = primaryConfig;
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onPlayerLogin(final ServerSideConnectionEvent.Join event) {
|
||||
final ComponentPlayerResult<CarbonPlayer> result = this.carbonChat.server().player(event.player().uniqueId()).join();
|
||||
final @Nullable CarbonPlayer player = result.player();
|
||||
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't show join messages when muted
|
||||
if (this.primaryConfig.hideMutedJoinLeaveQuit() && !player.muteEntries().isEmpty()) {
|
||||
event.setMessageCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onPlayerQuit(final ServerSideConnectionEvent.Disconnect event) {
|
||||
// Early exit in case "hide muted join / leave messages when muted" is disabled
|
||||
if (!this.primaryConfig.hideMutedJoinLeaveQuit()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ComponentPlayerResult<CarbonPlayer> result = this.carbonChat.server().player(event.player().uniqueId()).join();
|
||||
|
||||
if (result.player() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final CarbonPlayer player = result.player();
|
||||
|
||||
// Don't show quit messages when muted
|
||||
if (!player.muteEntries().isEmpty()) {
|
||||
event.setMessage(null); // TODO: null? empty? it's not MessageCancellable, which is odd
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user