This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
466 additions
and
28 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,5 +33,3 @@ queue: | |
logging: | ||
root: | ||
level: INFO | ||
tc-oc-api-bukkit-BukkitApi: | ||
level: INFO |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package tc.oc.pgm.listing; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.sk89q.minecraft.util.commands.Command; | ||
import com.sk89q.minecraft.util.commands.CommandContext; | ||
import com.sk89q.minecraft.util.commands.CommandException; | ||
import com.sk89q.minecraft.util.commands.CommandPermissions; | ||
import com.sk89q.minecraft.util.commands.SuggestException; | ||
import org.bukkit.command.CommandSender; | ||
|
||
@Singleton | ||
public class ListingCommands { | ||
|
||
private final ListingService listingService; | ||
|
||
@Inject ListingCommands(ListingService listingService) { | ||
this.listingService = listingService; | ||
} | ||
|
||
@Command( | ||
aliases = "announce", | ||
usage = "[on|off]", | ||
desc = "Announce the server to the public listing service", | ||
min = 0, | ||
max = 1 | ||
) | ||
@CommandPermissions("pgm.listing.announce") | ||
public void announce(CommandContext args, CommandSender sender) throws CommandException, SuggestException { | ||
listingService.update("on".equals(args.tryString(0, ImmutableList.of("on", "off")).orElse("on")), sender); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
PGM/src/main/java/tc/oc/pgm/listing/ListingConfiguration.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,37 @@ | ||
package tc.oc.pgm.listing; | ||
|
||
import java.net.URL; | ||
import java.util.OptionalInt; | ||
import javax.annotation.Nullable; | ||
import javax.inject.Inject; | ||
|
||
import tc.oc.commons.core.configuration.ConfigUtils; | ||
import tc.oc.minecraft.api.configuration.Configuration; | ||
import tc.oc.minecraft.api.configuration.ConfigurationSection; | ||
import tc.oc.net.UriUtils; | ||
|
||
class ListingConfiguration { | ||
|
||
private final ConfigurationSection config; | ||
|
||
@Inject ListingConfiguration(Configuration root) { | ||
this.config = root.needSection("announce"); | ||
} | ||
|
||
public boolean enabled() { | ||
return config.getBoolean("enabled", false); | ||
} | ||
|
||
public URL announceUrl() { | ||
return ConfigUtils.getUrl(config, "url", UriUtils.url("https://oc.tc/announce")); | ||
} | ||
|
||
public @Nullable String serverHost() { | ||
return config.getString("server-host"); | ||
} | ||
|
||
public OptionalInt serverPort() { | ||
final int port = config.getInt("server-port", 0); | ||
return port != 0 ? OptionalInt.of(port) : OptionalInt.empty(); | ||
} | ||
} |
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,21 @@ | ||
package tc.oc.pgm.listing; | ||
|
||
import tc.oc.commons.core.commands.CommandBinder; | ||
import tc.oc.commons.core.inject.HybridManifest; | ||
import tc.oc.minecraft.api.event.ListenerBinder; | ||
|
||
public class ListingManifest extends HybridManifest { | ||
|
||
@Override | ||
protected void configure() { | ||
bind(ListingConfiguration.class); | ||
bind(ListingService.class).to(ListingServiceImpl.class); | ||
|
||
final ListenerBinder listeners = new ListenerBinder(binder()); | ||
listeners.bindListener().to(PingListener.class); | ||
listeners.bindListener().to(ListingServiceImpl.class); | ||
|
||
new CommandBinder(binder()) | ||
.register(ListingCommands.class); | ||
} | ||
} |
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,16 @@ | ||
package tc.oc.pgm.listing; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import com.google.common.util.concurrent.ListenableFuture; | ||
import org.bukkit.command.CommandSender; | ||
import tc.oc.api.message.types.Reply; | ||
|
||
public interface ListingService { | ||
|
||
ListenableFuture<Reply> update(boolean online); | ||
|
||
ListenableFuture<Reply> update(boolean online, CommandSender sender); | ||
|
||
@Nullable String sessionDigest(); | ||
} |
118 changes: 118 additions & 0 deletions
118
PGM/src/main/java/tc/oc/pgm/listing/ListingServiceImpl.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,118 @@ | ||
package tc.oc.pgm.listing; | ||
|
||
import java.security.SecureRandom; | ||
import javax.annotation.Nullable; | ||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import com.google.common.util.concurrent.ListenableFuture; | ||
import net.md_5.bungee.api.chat.TranslatableComponent; | ||
import org.apache.commons.codec.binary.Hex; | ||
import org.apache.commons.codec.digest.DigestUtils; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.command.ConsoleCommandSender; | ||
import tc.oc.api.http.HttpClient; | ||
import tc.oc.api.http.HttpOption; | ||
import tc.oc.api.message.types.Reply; | ||
import tc.oc.commons.bukkit.chat.Audiences; | ||
import tc.oc.commons.core.commands.CommandFutureCallback; | ||
import tc.oc.commons.core.concurrent.Flexecutor; | ||
import tc.oc.minecraft.api.event.Enableable; | ||
import tc.oc.minecraft.api.server.LocalServer; | ||
import tc.oc.minecraft.scheduler.Sync; | ||
|
||
@Singleton | ||
class ListingServiceImpl implements ListingService, Enableable { | ||
|
||
private final HttpClient http; | ||
private final ListingConfiguration config; | ||
private final LocalServer localServer; | ||
private final SecureRandom random = new SecureRandom(); | ||
private final Flexecutor executor; | ||
private final Audiences audiences; | ||
private final ConsoleCommandSender console; | ||
|
||
private boolean online; | ||
private @Nullable String sessionId; | ||
private @Nullable String sessionDigest; | ||
|
||
@Inject ListingServiceImpl(HttpClient http, ListingConfiguration config, LocalServer localServer, @Sync(defer = true) Flexecutor executor, Audiences audiences, ConsoleCommandSender console) { | ||
this.http = http; | ||
this.config = config; | ||
this.localServer = localServer; | ||
this.executor = executor; | ||
this.audiences = audiences; | ||
this.console = console; | ||
} | ||
|
||
@Override | ||
public @Nullable String sessionDigest() { | ||
return sessionDigest; | ||
} | ||
|
||
@Override | ||
public void enable() { | ||
if(config.enabled()) { | ||
// Don't announce until we are ready to receive the ping | ||
executor.execute(() -> update(true)); | ||
} | ||
} | ||
|
||
@Override | ||
public void disable() { | ||
if(online) { | ||
update(false); | ||
} | ||
} | ||
|
||
@Override | ||
public ListenableFuture<Reply> update(boolean online) { | ||
return update(online, console); | ||
} | ||
|
||
@Override | ||
public ListenableFuture<Reply> update(boolean online, CommandSender sender) { | ||
this.online = online; | ||
|
||
if(sessionId == null) { | ||
final byte[] bytes = new byte[20]; | ||
random.nextBytes(bytes); | ||
sessionId = Hex.encodeHexString(bytes); | ||
sessionDigest = DigestUtils.sha1Hex(sessionId); | ||
} | ||
|
||
final ListenableFuture<Reply> future = http.post(config.announceUrl().toString(), new ListingUpdate() { | ||
@Override public @Nullable String host() { | ||
return config.serverHost(); | ||
} | ||
|
||
@Override | ||
public int port() { | ||
return config.serverPort().orElseGet(localServer::getPort); | ||
} | ||
|
||
@Override | ||
public boolean online() { | ||
return online; | ||
} | ||
|
||
@Override | ||
public String session() { | ||
return sessionId; | ||
} | ||
}, Reply.class, HttpOption.INFINITE_RETRY); | ||
|
||
executor.callback( | ||
future, | ||
CommandFutureCallback.onSuccess(sender, reply -> { | ||
if(!online) { | ||
sessionId = sessionDigest = null; | ||
} | ||
|
||
audiences.get(sender).sendMessage(new TranslatableComponent(online ? "announce.online" : "announce.offline")); | ||
}) | ||
); | ||
|
||
return future; | ||
} | ||
} |
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,18 @@ | ||
package tc.oc.pgm.listing; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import tc.oc.api.annotations.Serialize; | ||
import tc.oc.api.docs.virtual.Document; | ||
|
||
@Serialize | ||
public interface ListingUpdate extends Document { | ||
|
||
@Nullable String host(); | ||
|
||
int port(); | ||
|
||
boolean online(); | ||
|
||
String session(); | ||
} |
Oops, something went wrong.