下面列出了怎么用org.bukkit.util.CachedServerIcon的API类实例代码及写法,或者点击链接到github查看源代码。
public PaperServerListPingEvent(@Nonnull StatusClient client, String motd, int numPlayers, int maxPlayers,
@Nonnull String version, int protocolVersion, @Nullable CachedServerIcon favicon) {
super(client.getAddress().getAddress(), motd, numPlayers, maxPlayers);
this.client = client;
this.numPlayers = numPlayers;
this.version = version;
this.protocolVersion = protocolVersion;
setServerIcon(favicon);
}
/**
* Sets the server icon sent to the client.
*
* @param icon The icon to send to the client, or {@code null} for none
*/
@Override
public void setServerIcon(@Nullable CachedServerIcon icon) {
if (icon != null && icon.isEmpty()) {
// Represent empty icons as null
icon = null;
}
this.favicon = icon;
}
@Override
@Nullable
public CachedServerIcon[] get(Event e) {
CachedServerIcon icon = null;
if ((isServerPingEvent && !isDefault) && PAPER_EVENT_EXISTS)
icon = ((PaperServerListPingEvent) e).getServerIcon();
else
icon = Bukkit.getServerIcon();
if (icon == null || icon.getData() == null)
return null;
return CollectionUtils.array(icon);
}
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (isServerPingEvent && !isDefault) {
if (ScriptLoader.hasDelayBefore.isTrue()) {
Skript.error("Can't change the server icon anymore after the server list ping event has already passed");
return null;
}
if (mode == ChangeMode.SET || mode == ChangeMode.RESET)
return CollectionUtils.array(CachedServerIcon.class);
}
return null;
}
@SuppressWarnings("null")
@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
PaperServerListPingEvent event = (PaperServerListPingEvent) e;
switch (mode) {
case SET:
event.setServerIcon((CachedServerIcon) delta[0]);
break;
case RESET:
event.setServerIcon(Bukkit.getServerIcon());
}
}
@Override
public String convertBukkitIconToBase64(CachedServerIcon icon) {
if (icon == null) {
return null;
}
if (!(icon instanceof CraftIconCache)) {
throw new IllegalArgumentException(icon + " was not created by " + CraftServer.class);
}
return ((CraftIconCache) icon).value;
}
@Override
public Optional<CachedServerIcon> load(FaviconSource source) throws Exception {
// Try loading the favicon
BufferedImage image = FaviconHelper.loadSafely(core, source);
if (image == null) return Optional.absent(); // Favicon loading failed
else return Optional.of(getServer().loadServerIcon(image)); // Success!
}
@Override
public void setServerIcon(CachedServerIcon icon) throws IllegalArgumentException, UnsupportedOperationException {
}
PaperServerListPingEventImpl(MinecraftServer server, StatusClient client, int protocolVersion, @Nullable CachedServerIcon icon) {
super(client, server.getMOTD(), server.getCurrentPlayerCount(), server.getMaxPlayers(),
server.getServerModName() + ' ' + server.getMinecraftVersion(), protocolVersion, icon);
this.server = server;
}
@Override
public Class<? extends CachedServerIcon> getReturnType() {
return CachedServerIcon.class;
}
@Override
@Nullable
public CachedServerIcon[] get(Event e) {
return CollectionUtils.array(EffLoadServerIcon.lastLoaded);
}
@Override
public Class<? extends CachedServerIcon> getReturnType() {
return CachedServerIcon.class;
}
@Override
public CachedServerIcon getServerIcon() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public CachedServerIcon loadServerIcon(File file) throws IllegalArgumentException, Exception {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public CachedServerIcon loadServerIcon(BufferedImage image) throws IllegalArgumentException, Exception {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public CachedServerIcon getServerIcon() {
return null;
}
@Override
public CachedServerIcon loadServerIcon(File file) {
return null;
}
@Override
public CachedServerIcon loadServerIcon(BufferedImage bufferedImage) {
return null;
}
@Override
public void onPing(PingEvent event) {
try {
PingReply reply = event.getReply();
if(config.getBoolean("Enabled")) {
event.cancelPong(true);
event.setCancelled(true);
if(config.getBoolean("MOTD.Enabled")) {
reply.setMOTD(ChatColor.translateAlternateColorCodes('&', this.defaultMOTD));
}
if(config.getBoolean("PlayerCount.Enabled")) {
reply.setProtocolVersion(-1);
reply.setProtocolName(ChatColor.translateAlternateColorCodes('&', this.defaultPlayerCount));
}
event.createNewPacket(reply).send();
String lastMotd = (motds.isEmpty()) ? this.replaceVars(motds.get(motds.size() - 1)) : defaultMOTD;
String lastPlayerCount = (!playerCounts.isEmpty()) ? this.replaceVars(playerCounts.get(playerCounts.size() - 1)) : defaultPlayerCount;
CachedServerIcon lastIcon = (!icons.isEmpty()) ? Bukkit.loadServerIcon(new File(this.plugin.getDataFolder() + icons.get(icons.size() - 1))) : Bukkit.getServerIcon();
int largest = Collections.max(Arrays.asList(motds.size(), playerCounts.size(), icons.size()));
event.createNewPacket(reply).send();
for(int i = 0; i < largest; i++) {
String motd = null;
String playerCount = null;
CachedServerIcon icon = Bukkit.getServerIcon();
if(config.getBoolean("MOTD.Enabled")) {
motd = (motds.size() > i) ? this.replaceVars(motds.get(i)) : lastMotd;
}
if(config.getBoolean("PlayerCount.Enabled")) {
playerCount = (playerCounts.size() > i) ? this.replaceVars(playerCounts.get(i)) : lastPlayerCount;
}
if(config.getBoolean("Icon.Enabled")) {
icon = (icons.size() > i) ? Bukkit.loadServerIcon(new File(this.plugin.getDataFolder() + icons.get(i))) : lastIcon;
}
new AnimationRunnable(event, motd, playerCount, icon).runTaskLater(plugin, i * ticks);
}
new PongRunnable(event).runTaskLater(plugin, largest * ticks);
}
} catch(Exception e) {
e.printStackTrace();
}
}
public AnimationRunnable(PingEvent event, String motd, String playerCount, CachedServerIcon icon) {
this.event = event;
this.motd = motd;
this.playerCount = playerCount;
this.icon = icon;
}
public CachedServerIcon getIcon() {
return icon;
}
@Override
public void setServerIcon(CachedServerIcon icon) {
this.icon = icon;
}
public CachedServerIcon getFavicon(FaviconSource source) {
Optional<CachedServerIcon> result = faviconCache.getUnchecked(source);
return result.isPresent() ? result.get() : null;
}
@Override
public LoadingCache<FaviconSource, Optional<CachedServerIcon>> getFaviconCache() {
return faviconCache;
}
@EventHandler
public void onServerListPing(final ServerListPingEvent event) {
if (bukkit.getCore() == null) return; // Too early, we haven't finished initializing yet
StatusResponse response = bukkit.getCore().createRequest(event.getAddress()).createResponse(
bukkit.getCore().getStatus(), new ResponseFetcher() {
@Override
public Integer getOnlinePlayers() {
return event.getNumPlayers();
}
@Override
public Integer getMaxPlayers() {
return event.getMaxPlayers();
}
@Override
public int getProtocolVersion() {
return -1;
}
});
// Description
String message = response.getDescription();
if (message != null)
event.setMotd(message);
// Max players
Integer max = response.getMaxPlayers();
if (max != null)
event.setMaxPlayers(max);
// Favicon
FaviconSource favicon = response.getFavicon();
if (favicon != null) {
CachedServerIcon icon = bukkit.getFavicon(favicon);
if (icon != null)
try {
event.setServerIcon(icon);
} catch (UnsupportedOperationException ignored) {}
}
}
private void handlePaperServerListPing(final PaperServerListPingEvent event) {
if (bukkit.getCore() == null) return; // Too early, we haven't finished initializing yet
StatusRequest request = bukkit.getCore().createRequest(event.getAddress());
request.setProtocolVersion(event.getClient().getProtocolVersion());
InetSocketAddress host = event.getClient().getVirtualHost();
if (host != null) {
request.setTarget(host);
}
StatusResponse response = request.createResponse(bukkit.getCore().getStatus(),
// Return unknown player counts if it has been hidden
new ResponseFetcher() {
@Override
public Integer getOnlinePlayers() {
return event.shouldHidePlayers() ? null : event.getNumPlayers();
}
@Override
public Integer getMaxPlayers() {
return event.shouldHidePlayers() ? null : event.getMaxPlayers();
}
@Override
public int getProtocolVersion() {
return event.getProtocolVersion();
}
}
);
// Description
String message = response.getDescription();
if (message != null) event.setMotd(message);
// Version name
message = response.getVersion();
if (message != null) event.setVersion(message);
// Protocol version
Integer protocol = response.getProtocolVersion();
if (protocol != null) event.setProtocolVersion(protocol);
if (response.hidePlayers()) {
event.setHidePlayers(true);
} else {
// Online players
Integer count = response.getOnlinePlayers();
if (count != null) event.setNumPlayers(count);
// Max players
count = response.getMaxPlayers();
if (count != null) event.setMaxPlayers(count);
// Player hover
message = response.getPlayerHover();
if (message != null) {
List<PlayerProfile> profiles = event.getPlayerSample();
profiles.clear();
if (!message.isEmpty()) {
if (response.useMultipleSamples()) {
count = response.getDynamicSamples();
List<String> lines = count != null ? Helper.splitLinesCached(message, count) :
Helper.splitLinesCached(message);
for (String line : lines) {
profiles.add(bukkit.getServer().createProfile(line));
}
} else {
profiles.add(bukkit.getServer().createProfile(message));
}
}
}
}
// Favicon
FaviconSource favicon = response.getFavicon();
if (favicon != null) {
CachedServerIcon icon = bukkit.getFavicon(favicon);
if (icon != null)
try {
event.setServerIcon(icon);
} catch (UnsupportedOperationException ignored) {}
}
}
/**
* Gets an instance of the server's default server-icon.
*
* @return the default server-icon; null values may be used by the
* implementation to indicate no defined icon, but this behavior is
* not guaranteed
*/
CachedServerIcon getServerIcon();
/**
* Loads an image from a file, and returns a cached image for the specific
* server-icon.
* <p>
* Size and type are implementation defined. An incompatible file is
* guaranteed to throw an implementation-defined {@link Exception}.
*
* @param file the file to load the from
* @return a cached server-icon that can be used for a {@link
* ServerListPingEvent#setServerIcon(CachedServerIcon)}
* @throws IllegalArgumentException if image is null
* @throws Exception if the image does not meet current server server-icon
* specifications
*/
CachedServerIcon loadServerIcon(File file) throws IllegalArgumentException, Exception;
/**
* Creates a cached server-icon for the specific image.
* <p>
* Size and type are implementation defined. An incompatible file is
* guaranteed to throw an implementation-defined {@link Exception}.
*
* @param image the image to use
* @return a cached server-icon that can be used for a {@link
* ServerListPingEvent#setServerIcon(CachedServerIcon)}
* @throws IllegalArgumentException if image is null
* @throws Exception if the image does not meet current server
* server-icon specifications
*/
CachedServerIcon loadServerIcon(BufferedImage image) throws IllegalArgumentException, Exception;
/**
* Sets the server-icon sent to the client.
*
* @param icon the icon to send to the client
* @throws IllegalArgumentException if the {@link CachedServerIcon} is not
* created by the caller of this event; null may be accepted for some
* implementations
* @throws UnsupportedOperationException if the caller of this event does
* not support setting the server icon
*/
public void setServerIcon(CachedServerIcon icon) throws IllegalArgumentException, UnsupportedOperationException {
throw new UnsupportedOperationException();
}