added OptionLinks which are shown to the client as 'Server Links'
This commit is contained in:
parent
d7bc440620
commit
247dae0155
@ -19,6 +19,7 @@ import eu.mhsl.craftattack.spawn.appliances.hotbarRefill.HotbarRefill;
|
|||||||
import eu.mhsl.craftattack.spawn.appliances.kick.Kick;
|
import eu.mhsl.craftattack.spawn.appliances.kick.Kick;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.knockDoor.KnockDoor;
|
import eu.mhsl.craftattack.spawn.appliances.knockDoor.KnockDoor;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.maintenance.Maintenance;
|
import eu.mhsl.craftattack.spawn.appliances.maintenance.Maintenance;
|
||||||
|
import eu.mhsl.craftattack.spawn.appliances.optionLinks.OptionLinks;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.outlawed.Outlawed;
|
import eu.mhsl.craftattack.spawn.appliances.outlawed.Outlawed;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.packSelect.PackSelect;
|
import eu.mhsl.craftattack.spawn.appliances.packSelect.PackSelect;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.panicBan.PanicBan;
|
import eu.mhsl.craftattack.spawn.appliances.panicBan.PanicBan;
|
||||||
@ -87,7 +88,8 @@ public final class Main extends JavaPlugin {
|
|||||||
new KnockDoor(),
|
new KnockDoor(),
|
||||||
new PackSelect(),
|
new PackSelect(),
|
||||||
new GlowingBerries(),
|
new GlowingBerries(),
|
||||||
new Maintenance()
|
new Maintenance(),
|
||||||
|
new OptionLinks()
|
||||||
)
|
)
|
||||||
.filter(appliance -> disabledAppliances.stream()
|
.filter(appliance -> disabledAppliances.stream()
|
||||||
.noneMatch(s -> s.equalsIgnoreCase(appliance.getClass().getSimpleName())))
|
.noneMatch(s -> s.equalsIgnoreCase(appliance.getClass().getSimpleName())))
|
||||||
|
@ -0,0 +1,68 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.appliances.optionLinks;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.Main;
|
||||||
|
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.ServerLinks;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
@SuppressWarnings("UnstableApiUsage")
|
||||||
|
public class OptionLinks extends Appliance {
|
||||||
|
record ComponentSupplier() {}
|
||||||
|
record UriSupplier(Player player) {}
|
||||||
|
record UriConsumer(String uri) {}
|
||||||
|
record SuppliedLink(Function<ComponentSupplier, Component> component, Function<UriSupplier, UriConsumer> uri) {}
|
||||||
|
|
||||||
|
List<SuppliedLink> links = List.of(
|
||||||
|
new SuppliedLink(
|
||||||
|
componentSupplier -> Component.text("CraftAttack Homepage", NamedTextColor.GOLD),
|
||||||
|
uriSupplier -> new UriConsumer("https://mhsl.eu/craftattack")
|
||||||
|
),
|
||||||
|
new SuppliedLink(
|
||||||
|
componentSupplier -> Component.text("Regeln", NamedTextColor.GOLD),
|
||||||
|
uriSupplier -> new UriConsumer("https://mhsl.eu/craftattack/rules")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEnable() {
|
||||||
|
Bukkit.getServer().getServerLinks().getLinks()
|
||||||
|
.forEach(serverLink -> Bukkit.getServer().getServerLinks().removeLink(serverLink));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setServerLinks(Player player) {
|
||||||
|
ServerLinks playerLinks = Bukkit.getServerLinks().copy();
|
||||||
|
Main.logger().info("Set server links for " + player.getName());
|
||||||
|
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(Main.instance(), () -> {
|
||||||
|
this.links.forEach(suppliedLink -> {
|
||||||
|
Component component = suppliedLink.component.apply(new ComponentSupplier());
|
||||||
|
String uri = suppliedLink.uri.apply(new UriSupplier(player)).uri;
|
||||||
|
|
||||||
|
try {
|
||||||
|
playerLinks.addLink(component, URI.create(uri));
|
||||||
|
} catch(IllegalArgumentException e) {
|
||||||
|
Main.logger().log(Level.INFO, String.format("Failed to create OptionLink '%s' for player '%s'", uri, player.getName()), e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Main.logger().info("Sent links for " + player.getName());
|
||||||
|
|
||||||
|
player.sendLinks(playerLinks);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @NotNull List<Listener> listeners() {
|
||||||
|
return List.of(new UpdateLinksListener());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.appliances.optionLinks;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.appliance.ApplianceListener;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
|
||||||
|
public class UpdateLinksListener extends ApplianceListener<OptionLinks> {
|
||||||
|
@EventHandler
|
||||||
|
public void onJoin(PlayerJoinEvent event) {
|
||||||
|
getAppliance().setServerLinks(event.getPlayer());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user