generated from xpdustry/template-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Finally, at last, working component library
- Loading branch information
Showing
61 changed files
with
3,234 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/Audience.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...utor-common-api/src/main/java/com/xpdustry/distributor/api/audience/AudienceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/EmptyAudience.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...ibutor-common-api/src/main/java/com/xpdustry/distributor/api/audience/StreamAudience.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.