mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Configs and per-user localisations (#149)
Co-authored-by: jmp <[email protected]> Co-authored-by: broccolai <[email protected]>
This commit is contained in:
35 files changed
+557
-223
No files matched your search
+11
-1
@@ -15,7 +15,17 @@ dependencies {
|
||||
|
||||
tasks {
|
||||
runServer {
|
||||
classpath(shadowJar)
|
||||
classpath(shadowJar.map { it.archiveFile })
|
||||
dependsOn(shadowJar)
|
||||
}
|
||||
afterEvaluate {
|
||||
runServer {
|
||||
val path = classpath.toMutableList()
|
||||
path.removeIf {
|
||||
it.name == jar.forUseAtConfigurationTime().get().archiveFile.forUseAtConfigurationTime().get().asFile.name
|
||||
}
|
||||
classpath = objects.fileCollection().from(path)
|
||||
}
|
||||
}
|
||||
shadowJar {
|
||||
dependencies {
|
||||
|
||||
@@ -35,18 +35,18 @@ public final class CarbonChatSpongeEntry {
|
||||
this.game = game;
|
||||
this.pluginContainer = pluginContainer;
|
||||
|
||||
this.injector = injector.createChildInjector(new CarbonChatSpongeModule());
|
||||
this.injector = injector.createChildInjector(injector.getInstance(CarbonChatSpongeModule.class));
|
||||
this.carbon = this.injector.getInstance(CarbonChatSponge.class);
|
||||
|
||||
//metricsFactory.make(BSTATS_PLUGIN_ID);
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onInitialize(final StartingEngineEvent<Server> event) {
|
||||
this.game.eventManager().registerListeners(this.pluginContainer,
|
||||
this.injector.getInstance(SpongeChatListener.class));
|
||||
|
||||
this.carbon.initialize();
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onInitialize(final StartingEngineEvent<Server> event) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,30 +2,40 @@ package net.draycia.carbon.sponge;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import java.nio.file.Path;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.CarbonCommonModule;
|
||||
import net.draycia.carbon.common.CarbonJar;
|
||||
import net.draycia.carbon.common.ForCarbon;
|
||||
import net.draycia.carbon.sponge.users.MemoryUserManagerSponge;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.api.config.ConfigDir;
|
||||
import org.spongepowered.plugin.PluginContainer;
|
||||
|
||||
public class CarbonChatSpongeModule extends AbstractModule {
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonChatSpongeModule extends AbstractModule {
|
||||
|
||||
private final PluginContainer pluginContainer;
|
||||
private final Path configDir;
|
||||
|
||||
@Inject
|
||||
private CarbonChatSpongeModule(
|
||||
final PluginContainer pluginContainer,
|
||||
@ConfigDir(sharedRoot = false) final Path configDir
|
||||
) {
|
||||
this.pluginContainer = pluginContainer;
|
||||
this.configDir = configDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
this.install(new CarbonCommonModule());
|
||||
|
||||
this.bind(Path.class).annotatedWith(ForCarbon.class).toProvider(new Provider<>() {
|
||||
@Inject @ConfigDir(sharedRoot = false) Path path;
|
||||
|
||||
@Override
|
||||
public Path get() {
|
||||
return this.path;
|
||||
}
|
||||
});
|
||||
this.bind(Path.class).annotatedWith(ForCarbon.class).toInstance(this.configDir);
|
||||
this.bind(Path.class).annotatedWith(CarbonJar.class).toInstance(this.pluginContainer.path());
|
||||
this.bind(CarbonChat.class).to(CarbonChatSponge.class);
|
||||
this.bind(CarbonChatSponge.class);
|
||||
this.bind(CarbonServer.class).to(CarbonServerSponge.class);
|
||||
|
||||
@@ -6,19 +6,27 @@ import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.api.Game;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonServerSponge implements CarbonServer {
|
||||
public final class CarbonServerSponge implements CarbonServer {
|
||||
|
||||
private final Game game;
|
||||
private final UserManager userManager;
|
||||
|
||||
@Inject
|
||||
private CarbonChatSponge carbonChatSponge;
|
||||
private CarbonServerSponge(final UserManager userManager, final Game game) {
|
||||
this.userManager = userManager;
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience console() {
|
||||
@@ -29,7 +37,7 @@ public class CarbonServerSponge implements CarbonServer {
|
||||
public Iterable<? extends CarbonPlayer> players() {
|
||||
final var players = new ArrayList<CarbonPlayer>();
|
||||
|
||||
for (final var player : Sponge.server().onlinePlayers()) {
|
||||
for (final var player : this.game.server().onlinePlayers()) {
|
||||
final @Nullable CarbonPlayer carbonPlayer = this.player(player);
|
||||
|
||||
if (carbonPlayer != null) {
|
||||
@@ -42,12 +50,12 @@ public class CarbonServerSponge implements CarbonServer {
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer player(final UUID uuid) {
|
||||
return this.carbonChatSponge.server().player(uuid);
|
||||
return this.userManager.carbonPlayer(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer player(final String username) {
|
||||
return this.carbonChatSponge.server().player(username);
|
||||
return this.userManager.carbonPlayer(username);
|
||||
}
|
||||
|
||||
private @Nullable CarbonPlayer player(final Player player) {
|
||||
|
||||
@@ -2,8 +2,10 @@ package net.draycia.carbon.sponge.listeners;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import java.util.ArrayList;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.common.channels.BasicChatChannel;
|
||||
import net.draycia.carbon.sponge.CarbonChatSponge;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -20,8 +22,16 @@ import static net.kyori.adventure.text.Component.empty;
|
||||
|
||||
public final class SpongeChatListener {
|
||||
|
||||
@Inject private Game game;
|
||||
@Inject private CarbonChatSponge carbonChat;
|
||||
private final Game game;
|
||||
private final CarbonChatSponge carbonChat;
|
||||
private final BasicChatChannel basicChat;
|
||||
|
||||
@Inject
|
||||
private SpongeChatListener(final Game game, final CarbonChatSponge carbonChat, final BasicChatChannel basicChat) {
|
||||
this.game = game;
|
||||
this.carbonChat = carbonChat;
|
||||
this.basicChat = basicChat;
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onPlayerChat(final @NonNull PlayerChatEvent event, final @First Player messageSender) {
|
||||
@@ -38,7 +48,11 @@ public final class SpongeChatListener {
|
||||
}
|
||||
|
||||
final var recipients = new ArrayList<Audience>();
|
||||
final var channel = sender.selectedChannel();
|
||||
var channel = sender.selectedChannel();
|
||||
|
||||
if (channel == null) {
|
||||
channel = this.basicChat;
|
||||
}
|
||||
|
||||
for (final ServerPlayer spongeRecipient : this.game.server().onlinePlayers()) {
|
||||
final var recipient = this.carbonChat.server().player(spongeRecipient.uniqueId());
|
||||
@@ -49,11 +63,10 @@ public final class SpongeChatListener {
|
||||
}
|
||||
|
||||
// console too!
|
||||
// i'm not 100% sure this is "console" but I literally cannot find anything that explicitly says console
|
||||
recipients.add(this.game.systemSubject());
|
||||
|
||||
final var renderers = new ArrayList<KeyedRenderer>();
|
||||
renderers.add(keyedRenderer(key("carbon", "default"), channel.renderer()));
|
||||
renderers.add(keyedRenderer(key("carbon", "default"), channel));
|
||||
|
||||
final var chatEvent = new CarbonChatEvent(sender, event.message(), recipients, renderers);
|
||||
final var result = this.carbonChat.eventHandler().emit(chatEvent);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.draycia.carbon.sponge.users;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
@@ -11,9 +12,13 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.data.Keys;
|
||||
import org.spongepowered.api.data.type.HandTypes;
|
||||
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
import org.spongepowered.api.item.inventory.ItemStack;
|
||||
import org.spongepowered.api.item.inventory.equipment.EquipmentTypes;
|
||||
import org.spongepowered.api.util.locale.LocaleSource;
|
||||
|
||||
import static net.kyori.adventure.text.Component.translatable;
|
||||
import static net.kyori.adventure.text.format.TextDecoration.ITALIC;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonPlayerSponge extends CarbonPlayerCommon {
|
||||
@@ -50,35 +55,44 @@ public final class CarbonPlayerSponge extends CarbonPlayerCommon {
|
||||
return Sponge.server().player(this.uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Locale locale() {
|
||||
return this.player()
|
||||
.map(LocaleSource::locale)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Component createItemHoverComponent() {
|
||||
final ServerPlayer player = this.player().orElse(null);
|
||||
final @Nullable ServerPlayer player = this.player().orElse(null);
|
||||
if (player == null) {
|
||||
return Component.empty();
|
||||
}
|
||||
|
||||
final ItemStack itemStack;
|
||||
|
||||
final ItemStack mainHand = player.itemInHand(HandTypes.MAIN_HAND);
|
||||
|
||||
if (!mainHand.isEmpty()) {
|
||||
itemStack = mainHand;
|
||||
} else {
|
||||
final ItemStack offHand = player.itemInHand(HandTypes.OFF_HAND);
|
||||
|
||||
if (!offHand.isEmpty()) {
|
||||
itemStack = offHand;
|
||||
} else {
|
||||
itemStack = null;
|
||||
}
|
||||
}
|
||||
final @Nullable ItemStack itemStack = player.equipped(EquipmentTypes.MAIN_HAND)
|
||||
.filter(it -> !it.isEmpty())
|
||||
.orElseGet(() -> player.equipped(EquipmentTypes.OFF_HAND).orElse(null));
|
||||
|
||||
if (itemStack == null || itemStack.isEmpty()) {
|
||||
return Component.empty();
|
||||
}
|
||||
|
||||
return itemStack.get(Keys.DISPLAY_NAME)
|
||||
.orElse(itemStack.type().asComponent());
|
||||
return this.fromStack(itemStack);
|
||||
}
|
||||
|
||||
private @NonNull Component fromStack(final @NonNull ItemStack stack) {
|
||||
return stack.get(Keys.DISPLAY_NAME)
|
||||
|
||||
// This is here as a fallback, but really, every ItemStack should
|
||||
// have a DISPLAY_NAME which is already formatted properly for us by the game.
|
||||
.orElseGet(() -> translatable()
|
||||
.key("chat.square_brackets")
|
||||
.args(stack.get(Keys.CUSTOM_NAME)
|
||||
.map(name -> name.decorate(ITALIC))
|
||||
.orElseGet(() -> stack.type().asComponent()))
|
||||
.hoverEvent(stack.createSnapshot())
|
||||
.apply(builder -> stack.get(Keys.ITEM_RARITY).ifPresent(rarity -> builder.color(rarity.color())))
|
||||
.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user