Skip to content

Commit

Permalink
fix: Finally, at last, working component library
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed May 18, 2024
1 parent 01fcdf5 commit 2422df2
Show file tree
Hide file tree
Showing 61 changed files with 3,234 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.xpdustry.distributor.api.key.ContentTypeKey;
import com.xpdustry.distributor.api.plugin.MindustryPlugin;
import com.xpdustry.distributor.api.plugin.PluginAware;
import com.xpdustry.distributor.api.translation.TranslationArguments;
import io.leangen.geantyref.TypeToken;
import java.util.Objects;
import mindustry.game.Team;
Expand Down Expand Up @@ -87,7 +88,7 @@ public MindustryCommandManager(
final var source = DistributorProvider.get().getGlobalTranslationSource();
final var locale = this.senderMapper().reverse(sender).getLocale();
final var translation = source.getTranslation(caption.key(), locale);
return translation == null ? null : translation.formatEmpty();
return translation == null ? null : translation.format(TranslationArguments.empty());
});

this.parserRegistry().registerParser(PlayerParser.playerParser());
Expand Down Expand Up @@ -140,7 +141,11 @@ public final void descriptionMapper(final DescriptionMapper<Description> descrip
@Override
public boolean hasPermission(final @NonNull C sender, final String permission) {
return permission.isEmpty()
|| senderMapper().reverse(sender).getPermission(permission).asBoolean();
|| senderMapper()
.reverse(sender)
.getPermissions()
.getPermission(permission)
.asBoolean();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package com.xpdustry.distributor.api;

import com.xpdustry.distributor.api.audience.AudienceProvider;
import com.xpdustry.distributor.api.component.codec.StringComponentDecoder;
import com.xpdustry.distributor.api.component.codec.StringComponentEncoder;
import com.xpdustry.distributor.api.event.EventBus;
import com.xpdustry.distributor.api.permission.PermissionReader;
import com.xpdustry.distributor.api.player.PlayerLookup;
Expand All @@ -38,4 +41,14 @@ public interface Distributor {
PlayerLookup getPlayerLookup();

PluginScheduler getPluginScheduler();

StringComponentEncoder getMindustryEncoder();

StringComponentEncoder getAnsiEncoder();

StringComponentEncoder getPlainTextEncoder();

StringComponentDecoder getMindustryDecoder();

AudienceProvider getAudienceProvider();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Distributor, a feature-rich framework for Mindustry plugins.
*
* Copyright (C) 2024 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.api.audience;

import arc.struct.ObjectSet;
import arc.struct.Seq;
import com.xpdustry.distributor.api.collection.MindustryCollections;
import com.xpdustry.distributor.api.component.Component;
import com.xpdustry.distributor.api.key.Key;
import com.xpdustry.distributor.api.key.TypedKey;
import com.xpdustry.distributor.api.metadata.MetadataContainer;
import com.xpdustry.distributor.api.permission.PermissionProvider;
import com.xpdustry.distributor.api.player.MUUID;
import java.net.URI;
import java.util.Collection;
import java.util.Locale;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import mindustry.game.Team;

public interface Audience {

TypedKey<String> NAME = TypedKey.of("name", Key.DISTRIBUTOR_NAMESPACE, String.class);

TypedKey<String> DISPLAY_NAME = TypedKey.of("display-name", Key.DISTRIBUTOR_NAMESPACE, String.class);

TypedKey<MUUID> MUUID = TypedKey.of("muuid", Key.DISTRIBUTOR_NAMESPACE, MUUID.class);

TypedKey<Locale> LOCALE = TypedKey.of("locale", Key.DISTRIBUTOR_NAMESPACE, Locale.class);

TypedKey<Team> TEAM = TypedKey.of("team", Key.DISTRIBUTOR_NAMESPACE, Team.class);

static Audience of(final Audience... audiences) {
if (audiences.length == 0) {
return Audience.empty();
} else if (audiences.length == 1) {
return audiences[0];
} else {
return stream(() -> Stream.of(audiences).flatMap(Audience::getAudiences));
}
}

static Audience of(final Iterable<Audience> audiences) {
if (audiences instanceof Seq<Audience> seq) {
return stream(() -> MindustryCollections.mutableList(seq).stream().flatMap(Audience::getAudiences));
} else if (audiences instanceof ObjectSet<Audience> set) {
return stream(() -> MindustryCollections.mutableSet(set).stream().flatMap(Audience::getAudiences));
} else if (audiences instanceof Collection<Audience> collection) {
return stream(() -> collection.stream().flatMap(Audience::getAudiences));
} else {
return stream(
() -> StreamSupport.stream(audiences.spliterator(), false).flatMap(Audience::getAudiences));
}
}

static Audience empty() {
return EmptyAudience.INSTANCE;
}

static Collector<Audience, ?, Audience> collectToAudience() {
return Collectors.collectingAndThen(Collectors.toList(), Audience::of);
}

private static Audience stream(final StreamAudience audience) {
return audience;
}

default void sendMessage(final String message) {}

default void sendMessage(final Component component) {}

default void sendMessage(final String message, final String unformatted, final Audience sender) {}

default void sendMessage(final Component component, final Component unformatted, final Audience sender) {}

default void sendWarning(final String message) {}

default void sendWarning(final Component component) {}

default void showHUDText(final String message) {}

default void showHUDText(final Component component) {}

default void hideHUDText() {}

default void sendNotification(final String message, final char icon) {}

default void sendNotification(final Component component, final char icon) {}

default void sendAnnouncement(final String message) {}

default void sendAnnouncement(final Component component) {}

default void openURI(final URI uri) {}

default MetadataContainer getMetadata() {
return MetadataContainer.empty();
}

default Stream<Audience> getAudiences() {
return Stream.of(this);
}

default PermissionProvider getPermissions() {
return PermissionProvider.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Distributor, a feature-rich framework for Mindustry plugins.
*
* Copyright (C) 2024 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.api.audience;

import com.xpdustry.distributor.api.player.MUUID;
import mindustry.game.Team;
import mindustry.gen.Player;

public interface AudienceProvider {

Audience getPlayer(final MUUID muuid);

Audience getPlayer(final Player player);

Audience getServer();

Audience getPlayers();

default Audience getTeam(final Team team) {
return getPlayers()
.getAudiences()
.filter(audience ->
audience.getMetadata().getMetadata(Audience.TEAM).orElse(null) == team)
.collect(Audience.collectToAudience());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Distributor, a feature-rich framework for Mindustry plugins.
*
* Copyright (C) 2024 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.api.audience;

import java.util.stream.Stream;

final class EmptyAudience implements Audience {

static final Audience INSTANCE = new EmptyAudience();

private EmptyAudience() {}

@Override
public Stream<Audience> getAudiences() {
return Stream.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Distributor, a feature-rich framework for Mindustry plugins.
*
* Copyright (C) 2024 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.api.audience;

import com.xpdustry.distributor.api.component.Component;
import java.net.URI;
import java.util.stream.Stream;

@FunctionalInterface
public interface StreamAudience extends Audience {

@Override
Stream<Audience> getAudiences();

@Override
default void sendMessage(final String message) {
getAudiences().forEach(audience -> audience.sendMessage(message));
}

@Override
default void sendMessage(final Component component) {
getAudiences().forEach(audience -> audience.sendMessage(component));
}

@Override
default void sendMessage(final String message, final String unformatted, final Audience sender) {
getAudiences().forEach(audience -> audience.sendMessage(message, unformatted, sender));
}

@Override
default void sendMessage(final Component component, final Component unformatted, final Audience sender) {
getAudiences().forEach(audience -> audience.sendMessage(component, unformatted, sender));
}

@Override
default void sendWarning(final String message) {
getAudiences().forEach(audience -> audience.sendWarning(message));
}

@Override
default void sendWarning(final Component component) {
getAudiences().forEach(audience -> audience.sendWarning(component));
}

@Override
default void showHUDText(final String message) {
getAudiences().forEach(audience -> audience.showHUDText(message));
}

@Override
default void showHUDText(final Component component) {
getAudiences().forEach(audience -> audience.showHUDText(component));
}

@Override
default void hideHUDText() {
getAudiences().forEach(Audience::hideHUDText);
}

@Override
default void sendNotification(final String message, final char icon) {
getAudiences().forEach(audience -> audience.sendNotification(message, icon));
}

@Override
default void sendNotification(final Component component, final char icon) {
getAudiences().forEach(audience -> audience.sendNotification(component, icon));
}

@Override
default void sendAnnouncement(final String message) {
getAudiences().forEach(audience -> audience.sendAnnouncement(message));
}

@Override
default void sendAnnouncement(final Component component) {
getAudiences().forEach(audience -> audience.sendAnnouncement(component));
}

@Override
default void openURI(final URI uri) {
getAudiences().forEach(audience -> audience.openURI(uri));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*/
package com.xpdustry.distributor.api.command;

import com.xpdustry.distributor.api.permission.TriState;
import com.xpdustry.distributor.api.audience.Audience;
import com.xpdustry.distributor.api.permission.PermissionProvider;
import java.util.Locale;
import mindustry.gen.Player;

Expand Down Expand Up @@ -82,15 +83,17 @@ static CommandSender server() {
Player getPlayer();

/**
* Returns the permission state of the given permission.
*
* @param permission the permission to check
* @return the permission state
* Returns the permissions of this command sender.
*/
TriState getPermission(final String permission);
PermissionProvider getPermissions();

/**
* Returns the locale of this command sender.
*/
Locale getLocale();

/**
* Returns the audience of this command sender.
*/
Audience getAudience();
}
Loading

0 comments on commit 2422df2

Please sign in to comment.