2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-12-22 18:18:01 +00:00

first commit

This commit is contained in:
Tux 2013-09-29 16:16:47 -04:00
commit d0f5a24f38
7 changed files with 468 additions and 0 deletions

36
.gitignore vendored Normal file
View File

@ -0,0 +1,36 @@
# Eclipse stuff
/.classpath
/.project
/.settings
# netbeans
/nbproject
/nbactions.xml
/nb-configuration.xml
# we use maven!
/build.xml
# maven
/target
/dependency-reduced-pom.xml
*/target
*/dependency-reduced-pom.xml
# vim
.*.sw[a-p]
# various other potential build files
/build
/bin
/dist
/manifest.mf
# Mac filesystem dust
/.DS_Store
# intellij
*.iml
*.ipr
*.iws
.idea/

14
LICENSE Normal file
View File

@ -0,0 +1,14 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.

View File

@ -0,0 +1,4 @@
Copyright © ${project.inceptionYear} ${owner} <${email}>
This work is free. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.

131
pom.xml Normal file
View File

@ -0,0 +1,131 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright © 2013 tuxed <write@imaginarycode.com>
This work is free. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>RedisBungee</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>bungeecord-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
<repository>
<id>bungeeyaml-repo</id>
<url>http://repo.craftminecraft.net/content/repositories/releases</url>
</repository>
</repositories>
<inceptionYear>2013</inceptionYear>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>redis.clients:jedis</include>
<include>commons-pool:commons-pool</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>redis.clients</pattern>
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedis</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.commons.pool</pattern>
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.commonspool</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>2.3</version>
<configuration>
<header>license-maven-friendly.txt</header>
<properties>
<owner>tuxed</owner>
<year>2013</year>
<email>write@imaginarycode.com</email>
</properties>
<excludes>
<exclude>README.md</exclude>
<exclude>LICENSE</exclude>
<exclude>.gitignore</exclude>
<exclude>src/test/resources/**</exclude>
<exclude>src/main/resources/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.6.2-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.craftminecraft.bungee</groupId>
<artifactId>bungeeyaml</artifactId>
<version>1.2.3</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>0.12.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,273 @@
/**
* Copyright © 2013 tuxed <write@imaginarycode.com>
* This work is free. You can redistribute it and/or modify it under the
* terms of the Do What The Fuck You Want To Public License, Version 2,
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
*/
package com.imaginarycode.minecraft.redisbungee;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.event.ProxyPingEvent;
import net.md_5.bungee.api.event.ServerConnectedEvent;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler;
import org.yaml.snakeyaml.Yaml;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class RedisBungee extends Plugin implements Listener {
private static JedisPool pool;
private static String serverId;
private static List<String> servers = Lists.newArrayList();
private static RedisBungee plugin;
public static int getCount() {
int count = 0;
count += plugin.getProxy().getOnlineCount();
if (pool != null) {
Jedis rsc = pool.getResource();
try {
for (String i : servers) {
if (i.equals(serverId)) continue;
if (rsc.exists("server:" + i + ":playerCount"))
count += Integer.valueOf(rsc.get("server:" + i + ":playerCount"));
}
} finally {
pool.returnResource(rsc);
}
}
return count;
}
public static Set<String> getPlayers() {
Set<String> players = Sets.newHashSet();
for (ProxiedPlayer pp : plugin.getProxy().getPlayers()) {
players.add(pp.getName());
}
if (pool != null) {
Jedis rsc = pool.getResource();
try {
for (String i : servers) {
if (i.equals(serverId)) continue;
for (String p : rsc.smembers("server:" + i + ":usersOnline")) {
if (!players.contains(p)) {
players.add(p);
}
}
}
} finally {
pool.returnResource(rsc);
}
}
return ImmutableSet.copyOf(players);
}
public static ServerInfo getServerFor(ProxiedPlayer pp) {
return getServerFor(pp.getName());
}
public static ServerInfo getServerFor(String name) {
ServerInfo server = null;
if (plugin.getProxy().getPlayer(name) != null) return plugin.getProxy().getPlayer(name).getServer().getInfo();
if (pool != null) {
Jedis tmpRsc = pool.getResource();
try {
if (tmpRsc.hexists("player:" + name, "server"))
server = plugin.getProxy().getServerInfo(tmpRsc.hget("player:" + name, "server"));
} finally {
pool.returnResource(tmpRsc);
}
}
return server;
}
public static long getLastOnline(ProxiedPlayer pp) {
return getLastOnline(pp.getName());
}
private static long getUnixTimestamp() {
return TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
public static long getLastOnline(String name) {
long time = 0L;
if (plugin.getProxy().getPlayer(name) != null) return time;
if (pool != null) {
Jedis tmpRsc = pool.getResource();
try {
if (tmpRsc.hexists("player:" + name, "online"))
time = Long.valueOf(tmpRsc.hget("player:" + name, "online"));
} finally {
pool.returnResource(tmpRsc);
}
}
return time;
}
@Override
public void onEnable() {
plugin = this;
try {
loadConfig();
} catch (IOException e) {
e.printStackTrace();
}
if (pool != null) {
Jedis tmpRsc = pool.getResource();
try {
tmpRsc.set("server:" + serverId + ":playerCount", "0"); // reset
for (String i : tmpRsc.smembers("server:" + serverId + ":usersOnline")) {
tmpRsc.srem("server:" + serverId + ":usersOnline", i);
}
} finally {
pool.returnResource(tmpRsc);
}
getProxy().getScheduler().schedule(this, new Runnable() {
@Override
public void run() {
Jedis rsc = pool.getResource();
try {
rsc.set("server:" + serverId + ":playerCount", String.valueOf(getProxy().getOnlineCount()));
} finally {
pool.returnResource(rsc);
}
}
}, 3, 3, TimeUnit.SECONDS);
getProxy().getPluginManager().registerCommand(this, new Command("glist") {
@Override
public void execute(CommandSender sender, String[] args) {
sender.sendMessage(ChatColor.YELLOW + String.valueOf(getCount()) + " player(s) are currently online.");
if (args.length > 0 && args[0].equals("showall")) {
sender.sendMessage(ChatColor.YELLOW + "Players: " + Joiner.on(", ").join(getPlayers()));
} else {
sender.sendMessage(ChatColor.YELLOW + "To see all players online, use /glist showall.");
}
}
});
getProxy().getPluginManager().registerListener(this, this);
}
}
@Override
public void onDisable() {
pool.destroy();
}
private void loadConfig() throws IOException {
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
}
File exampleConfig = new File(getDataFolder(), "config.yml");
if (!exampleConfig.exists()) {
exampleConfig.createNewFile();
InputStream in = getResourceAsStream("example_config.yml");
OutputStream out = new FileOutputStream(exampleConfig);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
}
File file = new File(getDataFolder() + File.separator + "config.yml");
Yaml yaml = new Yaml();
Map rawYaml = (Map) yaml.load(new FileInputStream(file));
String redisServer = ((String) rawYaml.get("redis-server"));
serverId = ((String) rawYaml.get("server-id"));
List<?> tmp = (List<?>) rawYaml.get("linked-servers");
if (tmp != null)
for (Object i : tmp) {
if (i instanceof String) {
servers.add((String) i);
}
}
if (redisServer != null) {
if (!redisServer.equals("")) {
pool = new JedisPool(new JedisPoolConfig(), redisServer);
}
}
}
@EventHandler
public void onPlayerConnect(final PostLoginEvent event) {
if (pool != null) {
Jedis rsc = pool.getResource();
try {
rsc.sadd("server:" + serverId + ":usersOnline", event.getPlayer().getName());
rsc.hset("player:" + event.getPlayer().getName(), "online", "0");
rsc.hset("player:" + event.getPlayer().getName(), "online", (String)getProxy().getServers().keySet().toArray()[0]);
} finally {
pool.returnResource(rsc);
}
getProxy().getScheduler().schedule(this, new Runnable() {
@Override
public void run() {
Jedis rsc = pool.getResource();
try {
rsc.hset("player:" + event.getPlayer().getName(), "server", event.getPlayer().getServer().getInfo().getName());
} finally {
pool.returnResource(rsc);
}
}
}, 3, TimeUnit.SECONDS);
}
}
@EventHandler
public void onPlayerDisconnect(PlayerDisconnectEvent event) {
if (pool != null) {
Jedis rsc = pool.getResource();
try {
rsc.srem("server:" + serverId + ":usersOnline", event.getPlayer().getName());
rsc.hset("player:" + event.getPlayer().getName(), "online", String.valueOf(getUnixTimestamp()));
rsc.hdel("player:" + event.getPlayer().getName(), "server");
} finally {
pool.returnResource(rsc);
}
}
}
@EventHandler
public void onServerChange(ServerConnectedEvent event) {
if (pool != null) {
Jedis rsc = pool.getResource();
try {
rsc.hset("player:" + event.getPlayer().getName(), "server", event.getServer().getInfo().getName());
} finally {
pool.returnResource(rsc);
}
}
}
@EventHandler
public void onPing(ProxyPingEvent event) {
ServerPing old = event.getResponse();
int players = getCount();
ServerPing newResponse = new ServerPing(old.getProtocolVersion(), old.getGameVersion(), old.getMotd(), players, old.getMaxPlayers());
event.setResponse(newResponse);
}
}

View File

@ -0,0 +1,6 @@
redis-server: 127.0.0.1
server-id: iluvbungee
linked-servers:
- dastank
- americancoffee
- guavaissweet

View File

@ -0,0 +1,4 @@
name: RedisBungee
main: com.imaginarycode.minecraft.redisbungee.RedisBungee
version: 0.1
author: tuxed