Skip to content
Snippets Groups Projects
Commit f32b0841 authored by Spotlight Deveaux's avatar Spotlight Deveaux :fox:
Browse files

Merge pull request #1 from awesomebing1/temp

Minor fixup
parents 01a66d51 e5d7b62e
Branches master
No related tags found
No related merge requests found
/**
* @author Spotlight
* @date Saturday, December 12th, 2015
* @version 0.1.1
* @category Spigot
* @category Plugins
* @license MIT
*/
package io.github.packserver.Cedar;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.InputStreamReader;
import java.io.Reader;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
/**
* A registration plugin, server side.
*
* @author Spotlight
*/
public class Main extends JavaPlugin implements Listener {
/**
......@@ -38,15 +35,12 @@ public class Main extends JavaPlugin implements Listener {
// Create plugin instance and hand it to plugin
plugin = this;
// Try to run websocket server
try {
WebsocketServer.runServer();
} catch (InterruptedException | IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
WebsocketServer.runServer();
// Debug mode
// Find if we are in debug mode or not
InputStream in = getClass().getResourceAsStream("/config.yml");
FileConfiguration config = YamlConfiguration.loadConfiguration(in);
Reader reader = new InputStreamReader(in);
FileConfiguration config = YamlConfiguration.loadConfiguration(reader);
DEBUG = config.getBoolean("debug");
}
......@@ -56,9 +50,9 @@ public class Main extends JavaPlugin implements Listener {
@Override
public void onDisable() {
try {
WebsocketServer.server.stop();
} catch (IOException | InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
WebsocketServer.stopServer();
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
}
......@@ -15,7 +15,7 @@ import org.bukkit.entity.Player;
*
* @author spotlight
*/
public class RankUpManager {
class RankUpManager {
private final Player p;
private final Main plugin;
......@@ -27,10 +27,10 @@ public class RankUpManager {
/**
* "Rank" up player
* @param rank
* @param rank Name of rank
*/
public void handleRankUp(String rank) {
Bukkit.getServer().getOnlinePlayers().stream().forEach((Player player) -> {
Bukkit.getServer().getOnlinePlayers().forEach((Player player) -> {
// If the player isn't the player who got the rank
if (!player.equals(p)) {
// Tell them that the rank player got a rank
......
......@@ -22,7 +22,7 @@ class Utils {
this.username = username;
}
public boolean nameCheck() {
private boolean nameCheck() {
// Null
if (username == null) {
return false;
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package io.github.packserver.Cedar;
/**
*
* @author spotlight
*/
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.logging.Level;
......@@ -25,16 +15,12 @@ import org.java_websocket.server.WebSocketServer;
public class WebsocketServer extends WebSocketServer {
public static WebsocketServer server;
private static WebsocketServer server;
public WebsocketServer(int port) throws UnknownHostException {
private WebsocketServer(int port) {
super(new InetSocketAddress(port));
}
public WebsocketServer(InetSocketAddress address) {
super(address);
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
WebsocketSessionManager.getSessionManager().openSession(conn.getRemoteSocketAddress().getAddress().getHostAddress());
......@@ -50,6 +36,7 @@ public class WebsocketServer extends WebSocketServer {
* Model class for storing the selected few attributes for a Twitter User
* profile.
*/
@SuppressWarnings("CanBeFinal")
class Request {
private String username;
......@@ -98,7 +85,7 @@ public class WebsocketServer extends WebSocketServer {
}
}
public static void runServer() throws InterruptedException, IOException {
public static void runServer() {
WebSocketImpl.DEBUG = false;
int port = 8887;
server = new WebsocketServer(port);
......@@ -129,12 +116,10 @@ public class WebsocketServer extends WebSocketServer {
public void sendData(WebsocketSession session, String data) {
Collection<WebSocket> con = connections();
synchronized (con) {
for (WebSocket c : con) {
if (c.getRemoteSocketAddress().getAddress().getHostAddress().equalsIgnoreCase(session.getHost())) {
Bukkit.getLogger().log(Level.INFO, "Send data packet: {0}", data);
c.send(data);
}
}
con.stream().filter(c -> c.getRemoteSocketAddress().getAddress().getHostAddress().equalsIgnoreCase(session.getHost())).forEachOrdered(c -> {
Bukkit.getLogger().log(Level.INFO, "Send data packet: {0}", data);
c.send(data);
});
}
}
}
......@@ -9,10 +9,10 @@ package io.github.packserver.Cedar;
*
* @author spotlight
*/
public class WebsocketSession {
class WebsocketSession {
String host;
String name;
private final String host;
private String name;
public WebsocketSession(String host) {
this.host = host;
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package io.github.packserver.Cedar;
/**
*
* @author spotlight
*/
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
......@@ -28,10 +19,6 @@ public class WebsocketSessionManager {
return sessionManager;
}
public List<WebsocketSession> getSessions() {
return sessions;
}
public void openSession(String host) {
sessions.add(new WebsocketSession(host));
Bukkit.getLogger().log(Level.INFO, "Opened Websocket session: {0}", getSessionByHost(host));
......@@ -41,11 +28,7 @@ public class WebsocketSessionManager {
sessions.remove(getSessionByHost(host));
}
public void endSessionByName(String name) {
sessions.remove(getSessionByName(name));
}
public WebsocketSession getSessionByHost(String host) {
private WebsocketSession getSessionByHost(String host) {
for (WebsocketSession s : sessions) {
if (s.getHost().equals(host)) {
return s;
......@@ -54,23 +37,4 @@ public class WebsocketSessionManager {
return null;
}
public WebsocketSession getSessionByName(String name) {
for (int i = 0; i < sessions.size(); i++) {
Bukkit.getLogger().log(Level.INFO, "Session gotten:{0}", sessions.get(i));
if (sessions.get(i).getName().equalsIgnoreCase(name)) {
return sessions.get(i);
}
}
return null;
}
public void addSessionUsername(String host, String name) {
Bukkit.getLogger().log(Level.INFO, "Attemption to update session with data: {0} and a host of: {1}", new Object[]{name, host});
for (int i = 0; i < sessions.size(); i++) {
if (sessions.get(i).getHost().equalsIgnoreCase(host)) {
sessions.get(i).setName(name);
Bukkit.getLogger().log(Level.INFO, "Updated Websocket session information: {0}", sessions.get(i));
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment