├── .gitignore
├── .travis.yml
├── BlockServer.iml
├── CONTRIBUTING.md
├── LICENSE.txt
├── README.md
├── Syntax.md
├── license-definitions.xml
├── pom.xml
└── src
└── main
├── BlockServer.iml
└── java
└── org
└── blockserver
└── core
├── Server.java
├── event
├── Cancellable.java
├── CancellableImplementation.java
├── EventExecutor.java
├── EventListener.java
├── EventManager.java
├── MessageEventListener.java
├── Priority.java
└── ServerEventListener.java
├── events
├── ServerEvent.java
├── messages
│ ├── MessageEvent.java
│ ├── MessageReceiveEvent.java
│ └── MessageSendEvent.java
├── modules
│ ├── ModuleDisableEvent.java
│ ├── ModuleEnableEvent.java
│ └── ModuleEvent.java
└── packets
│ ├── PacketEvent.java
│ ├── PacketReceiveEvent.java
│ └── PacketSendEvent.java
├── exceptions
├── BlockServerException.java
└── node
│ ├── ExceptionBuilder.java
│ └── ExceptionNode.java
├── module
├── Enableable.java
├── EnableableImplementation.java
├── Module.java
├── ModuleLoader.java
├── ServerModule.java
└── loaders
│ ├── CoreModuleLoader.java
│ └── JarModuleLoader.java
├── modules
├── config
│ └── ConfigModule.java
├── entity
│ ├── Entity.java
│ ├── EntityModule.java
│ ├── entities
│ │ └── ExampleEntity.java
│ └── modules
│ │ └── ExampleEntityModule.java
├── file
│ └── FileModule.java
├── logging
│ └── LoggingModule.java
├── message
│ ├── Message.java
│ ├── MessageModule.java
│ └── messages
│ │ ├── PlayerLoginMessage.java
│ │ └── block
│ │ └── MessageOutBlockChange.java
├── network
│ ├── NetworkConverter.java
│ ├── NetworkModule.java
│ ├── PacketEventModule.java
│ └── pipeline
│ │ ├── NetworkPipelineHandler.java
│ │ ├── PipelineDispatcher.java
│ │ ├── PipelineProvider.java
│ │ ├── PipelineProviderImplementation.java
│ │ └── packet
│ │ ├── BinaryBuffer.java
│ │ └── RawPacket.java
├── player
│ ├── Player.java
│ └── PlayerModule.java
├── scheduler
│ ├── SchedulerModule.java
│ └── TaskData.java
├── serverlist
│ └── ServerListModule.java
├── thread
│ └── ExecutorModule.java
└── world
│ ├── Block.java
│ ├── ChunkPosition.java
│ ├── ChunkProvider.java
│ ├── Material.java
│ ├── World.java
│ ├── WorldAllocation.java
│ ├── WorldModule.java
│ └── positions
│ ├── Location.java
│ └── Vector.java
├── run.java
└── utilities
├── ArchiveUtils.java
├── ByteUtil.java
├── FileUtil.java
├── NetworkUtil.java
└── Skin.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pydevproject
2 | .metadata
3 | .gradle
4 | bin/
5 | build/
6 | tmp/
7 | *.tmp
8 | *.bak
9 | *.swp
10 | *~.nib
11 | local.properties
12 | .loadpath
13 | target/
14 |
15 | # External tool builders
16 | .externalToolBuilders/
17 |
18 | # Locally stored "Eclipse launch configurations"
19 | *.launch
20 |
21 | # CDT-specific
22 | .cproject
23 |
24 | # PDT-specific
25 | .buildpath
26 |
27 | # sbteclipse plugin
28 | .target
29 |
30 | # TeXlipse plugin
31 | .texlipse
32 |
33 | #Intellij
34 | .idea/
35 | *.impl
36 |
37 | # Generated files
38 | data/
39 | logs/
40 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | sudo: false
3 |
4 | jdk:
5 | - oraclejdk8
6 |
7 | install: mvn -version
8 |
9 | script:
10 | - mvn license:check package
11 |
12 | before_script:
13 | - cd $TRAVIS_BUILD_DIR
14 |
--------------------------------------------------------------------------------
/BlockServer.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | BlockServer Contributing Guidelines
2 | ===
3 | ## Contributing code
4 | Please fork the repo and [create a pull request](https://github.com/BlockServerProject/BlockServer/pulls). Please note the following before you start writing code:
5 | * The code should follow the format at [Syntax.md](Syntax.md).
6 | * If you are adding new features, please make sure we want it in our repo currently. If you find it [here](https://github.com/BlockServerProject/BlockServer/issues/112), you can start working on it (if nobody else is assigned for that task).
7 |
8 | ## Creating issues
9 | Issues must only be issues, not questions or support request. Please contact us on [IRC channel #blockserverproject](https://kiwiirc.com/client/chat.freenode.net/?nick=bsguest|?#blockserverproject) if you are having trouble setting up the server.
10 |
11 | If you are facing an issue that nobody else is facing, you are most likely doing wrong. Contact us on [IRC channel #blockserverproject](https://kiwiirc.com/client/chat.freenode.net/?nick=bsguest|?#blockserverproject) for support.
12 |
13 | Please provide enough information for us to debug. We would like to know how that bug happened and your system information. Please mention them in the issue.
14 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 |
9 | This version of the GNU Lesser General Public License incorporates
10 | the terms and conditions of version 3 of the GNU General Public
11 | License, supplemented by the additional permissions listed below.
12 |
13 | 0. Additional Definitions.
14 |
15 | As used herein, "this License" refers to version 3 of the GNU Lesser
16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU
17 | General Public License.
18 |
19 | "The Library" refers to a covered work governed by this License,
20 | other than an Application or a Combined Work as defined below.
21 |
22 | An "Application" is any work that makes use of an interface provided
23 | by the Library, but which is not otherwise based on the Library.
24 | Defining a subclass of a class defined by the Library is deemed a mode
25 | of using an interface provided by the Library.
26 |
27 | A "Combined Work" is a work produced by combining or linking an
28 | Application with the Library. The particular version of the Library
29 | with which the Combined Work was made is also called the "Linked
30 | Version".
31 |
32 | The "Minimal Corresponding Source" for a Combined Work means the
33 | Corresponding Source for the Combined Work, excluding any source code
34 | for portions of the Combined Work that, considered in isolation, are
35 | based on the Application, and not on the Linked Version.
36 |
37 | The "Corresponding Application Code" for a Combined Work means the
38 | object code and/or source code for the Application, including any data
39 | and utility programs needed for reproducing the Combined Work from the
40 | Application, but excluding the System Libraries of the Combined Work.
41 |
42 | 1. Exception to Section 3 of the GNU GPL.
43 |
44 | You may convey a covered work under sections 3 and 4 of this License
45 | without being bound by section 3 of the GNU GPL.
46 |
47 | 2. Conveying Modified Versions.
48 |
49 | If you modify a copy of the Library, and, in your modifications, a
50 | facility refers to a function or data to be supplied by an Application
51 | that uses the facility (other than as an argument passed when the
52 | facility is invoked), then you may convey a copy of the modified
53 | version:
54 |
55 | a) under this License, provided that you make a good faith effort to
56 | ensure that, in the event an Application does not supply the
57 | function or data, the facility still operates, and performs
58 | whatever part of its purpose remains meaningful, or
59 |
60 | b) under the GNU GPL, with none of the additional permissions of
61 | this License applicable to that copy.
62 |
63 | 3. Object Code Incorporating Material from Library Header Files.
64 |
65 | The object code form of an Application may incorporate material from
66 | a header file that is part of the Library. You may convey such object
67 | code under terms of your choice, provided that, if the incorporated
68 | material is not limited to numerical parameters, data structure
69 | layouts and accessors, or small macros, inline functions and templates
70 | (ten or fewer lines in length), you do both of the following:
71 |
72 | a) Give prominent notice with each copy of the object code that the
73 | Library is used in it and that the Library and its use are
74 | covered by this License.
75 |
76 | b) Accompany the object code with a copy of the GNU GPL and this license
77 | document.
78 |
79 | 4. Combined Works.
80 |
81 | You may convey a Combined Work under terms of your choice that,
82 | taken together, effectively do not restrict modification of the
83 | portions of the Library contained in the Combined Work and reverse
84 | engineering for debugging such modifications, if you also do each of
85 | the following:
86 |
87 | a) Give prominent notice with each copy of the Combined Work that
88 | the Library is used in it and that the Library and its use are
89 | covered by this License.
90 |
91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license
92 | document.
93 |
94 | c) For a Combined Work that displays copyright notices during
95 | execution, include the copyright notice for the Library among
96 | these notices, as well as a reference directing the user to the
97 | copies of the GNU GPL and this license document.
98 |
99 | d) Do one of the following:
100 |
101 | 0) Convey the Minimal Corresponding Source under the terms of this
102 | License, and the Corresponding Application Code in a form
103 | suitable for, and under terms that permit, the user to
104 | recombine or relink the Application with a modified version of
105 | the Linked Version to produce a modified Combined Work, in the
106 | manner specified by section 6 of the GNU GPL for conveying
107 | Corresponding Source.
108 |
109 | 1) Use a suitable shared library mechanism for linking with the
110 | Library. A suitable mechanism is one that (a) uses at run time
111 | a copy of the Library already present on the user's computer
112 | system, and (b) will operate properly with a modified version
113 | of the Library that is interface-compatible with the Linked
114 | Version.
115 |
116 | e) Provide Installation Information, but only if you would otherwise
117 | be required to provide such information under section 6 of the
118 | GNU GPL, and only to the extent that such information is
119 | necessary to install and execute a modified version of the
120 | Combined Work produced by recombining or relinking the
121 | Application with a modified version of the Linked Version. (If
122 | you use option 4d0, the Installation Information must accompany
123 | the Minimal Corresponding Source and Corresponding Application
124 | Code. If you use option 4d1, you must provide the Installation
125 | Information in the manner specified by section 6 of the GNU GPL
126 | for conveying Corresponding Source.)
127 |
128 | 5. Combined Libraries.
129 |
130 | You may place library facilities that are a work based on the
131 | Library side by side in a single library together with other library
132 | facilities that are not Applications and are not covered by this
133 | License, and convey such a combined library under terms of your
134 | choice, if you do both of the following:
135 |
136 | a) Accompany the combined library with a copy of the same work based
137 | on the Library, uncombined with any other library facilities,
138 | conveyed under the terms of this License.
139 |
140 | b) Give prominent notice with the combined library that part of it
141 | is a work based on the Library, and explaining where to find the
142 | accompanying uncombined form of the same work.
143 |
144 | 6. Revised Versions of the GNU Lesser General Public License.
145 |
146 | The Free Software Foundation may publish revised and/or new versions
147 | of the GNU Lesser General Public License from time to time. Such new
148 | versions will be similar in spirit to the present version, but may
149 | differ in detail to address new problems or concerns.
150 |
151 | Each version is given a distinguishing version number. If the
152 | Library as you received it specifies that a certain numbered version
153 | of the GNU Lesser General Public License "or any later version"
154 | applies to it, you have the option of following the terms and
155 | conditions either of that published version or of any later version
156 | published by the Free Software Foundation. If the Library as you
157 | received it does not specify a version number of the GNU Lesser
158 | General Public License, you may choose any version of the GNU Lesser
159 | General Public License ever published by the Free Software Foundation.
160 |
161 | If the Library as you received it specifies that a proxy can decide
162 | whether future versions of the GNU Lesser General Public License shall
163 | apply, that proxy's public statement of acceptance of any version is
164 | permanent authorization for you to choose that version for the
165 | Library.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | BlockServer [](https://travis-ci.org/BlockServerProject/BlockServer)
2 | ===========
3 | BlockServer is an open source platform for Minecraft server software. It is very extensible and open source. This repository contains the core which is needed to run and load the extra modules which allow players to connect to the server.
4 |
5 | The BlockServer Project develops modules to support both PC and PE connections. Currently PC is lower priority as our goal is to support PE first.
6 |
--------------------------------------------------------------------------------
/Syntax.md:
--------------------------------------------------------------------------------
1 | Syntax of BlockServer (Java)
2 | ===
3 | * Indents should be in tabs.
4 | * Open braces for block expressions (e.g. methods, classes, if-blocks, for-blocks, etc.) should not occupy an independent line.
5 | * There can be an empty line between methods and/or fields.
6 | * There can be an empty line between two different sections of code.
7 | * Packages must be of or of subpackages of `org.blockserver`.
8 | * The three main code sections in a file (package, import, class/interface/enum declaration) should be separated with empty lines.
9 | * Imports of Java classes and custom classes can have an empty line between.
10 | * Do not make redundant imports like `java.lang.*` imports and unused imports.
11 | * Use Lombok for Getters and Setters
12 | * Use the Apache Logger library to print out text to console, with the following exceptions:
13 | * It is for debug purposes and will not affect user interface.
14 | * The server or the logger is not initialized.
15 | * Constructors, as a good practice, should point to the same `this(...)` constructor in order to avoid bugs.
16 | * Do not add redundant `this.` tokens unless necessary.
17 | * Add the `@Override` and `@SuppressWarnings(...)` annotations if required.
18 | * Unless name duplicated, do not fully qualify class names in class/interface/enum body.
19 |
20 | Example code:
21 |
22 | ```java
23 | package org.blockserver.examples;
24 |
25 | import java.io.File;
26 | import java.io.FilenameFilter;
27 |
28 | import net.blockserver.Server;
29 |
30 | @SuppressWarnings("serial")
31 | class ExampleException extends Exception{
32 | private String ext;
33 |
34 | public ExampleException(){
35 | this("Hello world!");
36 | }
37 | public ExampleException(String message){
38 | this(message, "log");
39 | }
40 | public ExampleException(String message, String ext){
41 | super(message);
42 | this.ext = ext;
43 | }
44 |
45 | public FilenameFilter getFilter(Pattern pattern){
46 | return new Foo(pattern);
47 | }
48 |
49 | private class Foo implements FilenameFilter{
50 | private Pattern pattern;
51 |
52 | public Foo(Pattern pattern){
53 | this.pattern = pattern;
54 | }
55 |
56 | @Override
57 | public boolean accept(File dir, String name){
58 | return pattern.bar(ext);
59 | }
60 | }
61 |
62 | public abstract static class Pattern extends com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern{
63 | public abstract boolean bar(String ext);
64 | }
65 | }
66 | ```
67 |
--------------------------------------------------------------------------------
/license-definitions.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | /*
5 | *
6 | */
7 |
8 |
9 | (\s|\t)*/\*.*$
10 | .*\*/(\s|\t)*$
11 | false
12 | true
13 | false
14 |
15 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
19 |
21 | 4.0.0
22 | org.blockserver
23 | BlockServer
24 | 1.0.0-BETA-SNAPSHOT
25 | jar
26 |
27 |
28 |
29 | snapshot-repo
30 | https://raw.githubusercontent.com/BlockServerProject/MavenRepository/master/snapshots
31 |
32 |
33 |
34 |
35 |
36 | snapshot-repo
37 | https://raw.githubusercontent.com/BlockServerProject/MavenRepository/master/snapshots
38 |
39 |
40 |
41 |
42 |
43 | org.projectlombok
44 | lombok
45 | 1.16.6
46 | provided
47 |
48 |
49 |
50 |
51 |
52 |
53 | org.apache.maven.plugins
54 | maven-compiler-plugin
55 | 3.1
56 |
57 | 1.8
58 | 1.8
59 |
60 |
61 |
62 | com.mycila
63 | license-maven-plugin
64 | 2.11
65 |
66 | com/mycila/maven/plugin/license/templates/LGPL-3.txt
67 |
68 |
69 | license-definitions.xml
70 |
71 |
72 |
73 | src/main/java/**
74 | src/test/java/**
75 | pom.xml
76 |
77 |
78 |
79 |
80 |
81 | check
82 |
83 |
84 |
85 |
86 |
87 | org.apache.maven.plugins
88 | maven-shade-plugin
89 | 2.3
90 |
91 |
92 |
93 | package
94 |
95 | shade
96 |
97 |
98 |
99 |
100 |
102 | org.blockserver.core.run
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
--------------------------------------------------------------------------------
/src/main/BlockServer.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/Server.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core;
18 |
19 | import lombok.Getter;
20 | import lombok.Setter;
21 | import org.blockserver.core.event.EventManager;
22 | import org.blockserver.core.events.modules.ModuleDisableEvent;
23 | import org.blockserver.core.events.modules.ModuleEnableEvent;
24 | import org.blockserver.core.module.EnableableImplementation;
25 | import org.blockserver.core.module.ModuleLoader;
26 | import org.blockserver.core.module.ServerModule;
27 |
28 | import java.util.Collections;
29 | import java.util.HashMap;
30 | import java.util.Map;
31 |
32 | /**
33 | * Represents the core server implementation.
34 | *
35 | * @author BlockServer Team
36 | */
37 | public class Server implements EnableableImplementation {
38 | //TODO Add YAML utils somewhere!!
39 | //Modules
40 | private final Map, ServerModule> modules = new HashMap<>();
41 | @Getter @Setter private EventManager eventManager = new EventManager();
42 |
43 | public Server(ModuleLoader... moduleLoaders) {
44 | for (ModuleLoader moduleLoader : moduleLoaders) {
45 | moduleLoader.setModules(modules, this);
46 | }
47 | }
48 |
49 | @SuppressWarnings("unchecked")
50 | public T getModule(Class moduleClass) {
51 | return (T) modules.get(moduleClass);
52 | }
53 |
54 | public void addModule(ServerModule module) {
55 | modules.put(module.getClass(), module);
56 | }
57 |
58 | @Override
59 | public void enable() {
60 | modules.values().forEach((module) -> {
61 | if (module.isEnabled())
62 | return;
63 | eventManager.fire(new ModuleEnableEvent(this, module), event -> {
64 | if (!event.isCancelled())
65 | module.enable();
66 | });
67 | });
68 | EnableableImplementation.super.enable();
69 | }
70 |
71 | @Override
72 | public void disable() {
73 | modules.values().forEach((module) -> {
74 | if (!module.isEnabled())
75 | return;
76 | eventManager.fire(new ModuleDisableEvent(this, module), event -> {
77 | if (!event.isCancelled())
78 | module.disable();
79 | });
80 | });
81 | EnableableImplementation.super.disable();
82 | }
83 |
84 | public Map, ServerModule> getModules() {
85 | return Collections.unmodifiableMap(modules);
86 | }
87 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/event/Cancellable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.event;
18 |
19 | /**
20 | * Written by Exerosis!
21 | */
22 | public interface Cancellable {
23 | boolean isCancelled();
24 |
25 | void setCancelled(boolean cancelled);
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/event/CancellableImplementation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.event;
18 |
19 | import java.util.Map;
20 | import java.util.WeakHashMap;
21 |
22 | public interface CancellableImplementation extends Cancellable {
23 | Map instances = new WeakHashMap<>();
24 |
25 | @Override
26 | default boolean isCancelled() {
27 | return instances.getOrDefault(this, false);
28 | }
29 |
30 | @Override
31 | default void setCancelled(boolean cancelled) {
32 | instances.put(this, cancelled);
33 | }
34 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/event/EventExecutor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.event;
18 |
19 | public interface EventExecutor {
20 | void execute(T event);
21 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/event/EventListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.event;
18 |
19 |
20 | public class EventListener {
21 | private Class _listenerType;
22 | private Priority _priority = Priority.NORMAL;
23 | private boolean _post;
24 |
25 | public Class getListenerType() {
26 | return _listenerType;
27 | }
28 |
29 | public boolean isPost() {
30 | return _post;
31 | }
32 |
33 | public Priority getPriority() {
34 | return _priority;
35 | }
36 |
37 | public void onEvent(B event) {
38 | }
39 |
40 | public EventListener post() {
41 | _post = !_post;
42 | return this;
43 | }
44 |
45 | public EventListener priority(Priority priority) {
46 | _priority = priority;
47 | return this;
48 | }
49 |
50 | public EventListener register(Class listenerType, EventManager eventManager) {
51 | _listenerType = listenerType;
52 | eventManager.registerListener(this);
53 | return this;
54 | }
55 |
56 | public void unregister(EventManager eventManager) {
57 | eventManager.unregisterListener(this);
58 | }
59 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/event/EventManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.event;
18 |
19 |
20 | import java.util.Set;
21 | import java.util.TreeSet;
22 |
23 | public class EventManager {
24 | private Set> instances = new TreeSet<>((o1, o2) -> Integer.compare(o2.getPriority().ordinal(), o1.getPriority().ordinal()));
25 |
26 | public EventManager() {
27 | }
28 |
29 | public void registerListener(EventListener, ?> listener) {
30 | instances.add(listener);
31 | }
32 |
33 | public void unregisterListener(EventListener listener) {
34 | instances.remove(listener);
35 | }
36 |
37 | @SuppressWarnings("unchecked")
38 | public B fire(Class listenerType, B event, EventExecutor executor) {
39 | instances.stream().filter(l -> l.getListenerType().isAssignableFrom(listenerType)).forEach(l -> {
40 | EventListener listener = (EventListener) l;
41 | if (listener.isPost())
42 | listener.onEvent(event);
43 | else {
44 | if (executor != null)
45 | executor.execute(event);
46 | listener.onEvent(event);
47 | }
48 | });
49 | return event;
50 | }
51 |
52 | public B fire(B event, EventExecutor executor) {
53 | return fire(event.getClass(), event, executor);
54 | }
55 |
56 | public B fire(Class listenerType, B event) {
57 | return fire(listenerType, event, null);
58 | }
59 |
60 | public B fire(B event) {
61 | return fire(event.getClass(), event, null);
62 | }
63 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/event/MessageEventListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.event;
18 |
19 | import org.blockserver.core.Server;
20 | import org.blockserver.core.events.messages.MessageEvent;
21 | import org.blockserver.core.modules.message.Message;
22 |
23 | /**
24 | * Written by Exerosis!
25 | */
26 | public class MessageEventListener extends EventListener> {
27 | public MessageEventListener register(Class listenerType, Server server) {
28 | return (MessageEventListener) register(listenerType, server.getEventManager());
29 | }
30 |
31 | public void unregister(Server server) {
32 | unregister(server.getEventManager());
33 | }
34 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/event/Priority.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.event;
18 |
19 | public enum Priority {
20 |
21 | LOWEST(0), LOW(1), NORMAL(2), HIGH(3), HIGHEST(4), INTERNAL(5);
22 |
23 | private final int slot;
24 |
25 | Priority(int slot) {
26 | this.slot = slot;
27 | }
28 |
29 | public int getSlot() {
30 | return this.slot;
31 | }
32 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/event/ServerEventListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.event;
18 |
19 | import org.blockserver.core.Server;
20 |
21 | /**
22 | * Written by Exerosis!
23 | */
24 | public class ServerEventListener extends EventListener {
25 | public void register(Class listenerType, Server server) {
26 | register(listenerType, server.getEventManager());
27 | }
28 |
29 | public void unregister(Server server) {
30 | unregister(server.getEventManager());
31 | }
32 |
33 | public ServerEventListener post() {
34 | super.post();
35 | return this;
36 | }
37 |
38 | public ServerEventListener priority(Priority priority) {
39 | super.priority(priority);
40 | return this;
41 | }
42 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/events/ServerEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.events;
18 |
19 | import lombok.Getter;
20 | import lombok.Setter;
21 | import org.blockserver.core.Server;
22 |
23 | public class ServerEvent {
24 | @Getter @Setter private Server server;
25 |
26 | public ServerEvent(Server server) {
27 | this.server = server;
28 | }
29 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/events/messages/MessageEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.events.messages;
18 |
19 | import lombok.Getter;
20 | import lombok.Setter;
21 | import org.blockserver.core.event.CancellableImplementation;
22 | import org.blockserver.core.modules.message.Message;
23 |
24 | /**
25 | * Written by Exerosis!
26 | */
27 | public class MessageEvent implements CancellableImplementation {
28 | @Getter @Setter private T message;
29 |
30 | public MessageEvent(T message) {
31 | this.message = message;
32 | }
33 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/events/messages/MessageReceiveEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.events.messages;
18 |
19 | import org.blockserver.core.modules.message.Message;
20 |
21 | public class MessageReceiveEvent extends MessageEvent {
22 | public MessageReceiveEvent(T message) {
23 | super(message);
24 | }
25 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/events/messages/MessageSendEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.events.messages;
18 |
19 | import org.blockserver.core.modules.message.Message;
20 |
21 | public class MessageSendEvent extends MessageEvent {
22 | public MessageSendEvent(T message) {
23 | super(message);
24 | }
25 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/events/modules/ModuleDisableEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.events.modules;
18 |
19 | import org.blockserver.core.Server;
20 | import org.blockserver.core.module.ServerModule;
21 |
22 | public class ModuleDisableEvent extends ModuleEvent {
23 | public ModuleDisableEvent(Server server, ServerModule module) {
24 | super(server, module);
25 | }
26 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/events/modules/ModuleEnableEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.events.modules;
18 |
19 | import org.blockserver.core.Server;
20 | import org.blockserver.core.module.ServerModule;
21 |
22 | public class ModuleEnableEvent extends ModuleEvent {
23 | public ModuleEnableEvent(Server server, ServerModule module) {
24 | super(server, module);
25 | }
26 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/events/modules/ModuleEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.events.modules;
18 |
19 | import lombok.Getter;
20 | import lombok.Setter;
21 | import org.blockserver.core.Server;
22 | import org.blockserver.core.event.CancellableImplementation;
23 | import org.blockserver.core.events.ServerEvent;
24 | import org.blockserver.core.module.ServerModule;
25 |
26 | public class ModuleEvent extends ServerEvent implements CancellableImplementation {
27 | @Getter @Setter private ServerModule module;
28 |
29 | public ModuleEvent(Server server, ServerModule module) {
30 | super(server);
31 | this.module = module;
32 | }
33 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/events/packets/PacketEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.events.packets;
18 |
19 | import lombok.Getter;
20 | import lombok.Setter;
21 | import org.blockserver.core.event.CancellableImplementation;
22 | import org.blockserver.core.modules.network.pipeline.packet.RawPacket;
23 |
24 | /**
25 | * Written by Exerosis!
26 | */
27 | public class PacketEvent implements CancellableImplementation {
28 | @Getter @Setter private RawPacket packet;
29 |
30 | public PacketEvent(RawPacket packet) {
31 | this.packet = packet;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/events/packets/PacketReceiveEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.events.packets;
18 |
19 | import org.blockserver.core.modules.network.pipeline.packet.RawPacket;
20 |
21 | /**
22 | * Written by Exerosis!
23 | */
24 | public class PacketReceiveEvent extends PacketEvent {
25 | public PacketReceiveEvent(RawPacket packet) {
26 | super(packet);
27 | }
28 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/events/packets/PacketSendEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.events.packets;
18 |
19 | import org.blockserver.core.modules.network.pipeline.packet.RawPacket;
20 |
21 | /**
22 | * Written by Exerosis!
23 | */
24 | public class PacketSendEvent extends PacketEvent {
25 | public PacketSendEvent(RawPacket packet) {
26 | super(packet);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/exceptions/BlockServerException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.exceptions;
18 |
19 | import org.blockserver.core.exceptions.node.ExceptionBuilder;
20 |
21 | public class BlockServerException extends RuntimeException {
22 | public BlockServerException(String message) {
23 | super(message);
24 | }
25 |
26 | public BlockServerException(ExceptionBuilder factory) {
27 | super(factory.toString());
28 | }
29 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/exceptions/node/ExceptionBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.exceptions.node;
18 |
19 | public class ExceptionBuilder {
20 | private StringBuilder builder = new StringBuilder("\t");
21 |
22 | public ExceptionBuilder(String name, ExceptionNode... nodes) {
23 | builder.append(name);
24 | for (ExceptionNode node : nodes)
25 | appendNode(node);
26 | }
27 |
28 | public ExceptionBuilder appendNode(ExceptionNode node) {
29 | if (node == null)
30 | return this;
31 | builder.append("\n\t");
32 | builder.append(node.getName());
33 | builder.append(": '");
34 | builder.append(node.getValue());
35 | builder.append('\'');
36 | return this;
37 | }
38 |
39 | @Override
40 | public String toString() {
41 | return builder.toString();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/exceptions/node/ExceptionNode.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.exceptions.node;
18 |
19 | public class ExceptionNode {
20 | private String name = "";
21 | private Object value;
22 |
23 | public ExceptionNode(String name, Object value) {
24 | this.name = name;
25 | this.value = value;
26 | }
27 |
28 | public String getName() {
29 | return name;
30 | }
31 |
32 | public Object getValue() {
33 | return value;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/module/Enableable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.module;
18 |
19 | /**
20 | * Written by Exerosis!
21 | *
22 | * @author BlockServer Team
23 | */
24 | public interface Enableable {
25 | void enable();
26 |
27 | boolean isEnabled();
28 |
29 | void disable();
30 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/module/EnableableImplementation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.module;
18 |
19 | import java.util.Map;
20 | import java.util.WeakHashMap;
21 |
22 | /**
23 | * @author BlockServer Team
24 | * @see org.blockserver.core.module.Enableable
25 | */
26 | public interface EnableableImplementation extends Enableable {
27 | Map instances = new WeakHashMap<>();
28 |
29 | @Override
30 | default void enable() {
31 | instances.put(this, true);
32 | }
33 |
34 | @Override
35 | default boolean isEnabled() {
36 | return instances.getOrDefault(this, false);
37 | }
38 |
39 | @Override
40 | default void disable() {
41 | instances.put(this, false);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/module/Module.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.module;
18 |
19 | /**
20 | * Created by Exerosis.
21 | */
22 | public interface Module extends EnableableImplementation {
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/module/ModuleLoader.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.module;
18 |
19 | import org.blockserver.core.Server;
20 |
21 | import java.util.Map;
22 |
23 | /**
24 | * @author BlockServer Team
25 | * @see org.blockserver.core.module.loaders
26 | */
27 | public interface ModuleLoader {
28 | void setModules(Map, ServerModule> modules, Server server);
29 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/module/ServerModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.module;
18 |
19 | import lombok.Getter;
20 | import org.blockserver.core.Server;
21 |
22 | /**
23 | * Base class for all modules. New modules should implement this class.
24 | *
25 | * @author BlockServer Team
26 | * @see org.blockserver.core.modules
27 | * @see org.blockserver.core.module.EnableableImplementation
28 | */
29 | public class ServerModule implements Module {
30 | @Getter private final Server server;
31 |
32 | public ServerModule(Server server) {
33 | this.server = server;
34 | }
35 |
36 | @Override
37 | public String toString() {
38 | return "[" + server.toString() + "] " + getClass().getSimpleName();
39 | }
40 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/module/loaders/CoreModuleLoader.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.module.loaders;
18 |
19 | import org.blockserver.core.Server;
20 | import org.blockserver.core.module.ModuleLoader;
21 | import org.blockserver.core.module.ServerModule;
22 | import org.blockserver.core.modules.config.ConfigModule;
23 | import org.blockserver.core.modules.file.FileModule;
24 | import org.blockserver.core.modules.logging.LoggingModule;
25 | import org.blockserver.core.modules.network.NetworkModule;
26 | import org.blockserver.core.modules.player.PlayerModule;
27 | import org.blockserver.core.modules.scheduler.SchedulerModule;
28 | import org.blockserver.core.modules.thread.ExecutorModule;
29 |
30 | import java.util.Map;
31 |
32 | /**
33 | * @author BlockServer Team
34 | * @see org.blockserver.core.module.ModuleLoader
35 | */
36 | public class CoreModuleLoader implements ModuleLoader {
37 |
38 | @Override
39 | public void setModules(Map, ServerModule> modules, Server server) {
40 | int start = modules.size();
41 |
42 | //Logging Module
43 | LoggingModule loggingModule = new LoggingModule(server);
44 | loggingModule.info("[CoreModuleLoader]: LoggingModule online, continuing load with logging capabilities!");
45 |
46 | //No Depends
47 | FileModule fileModule = new FileModule(server);
48 | PlayerModule playerModule = new PlayerModule(server);
49 | NetworkModule networkModule = new NetworkModule(server);
50 |
51 | //Single Module Depends
52 | ConfigModule configModule = new ConfigModule(server, fileModule);
53 | ExecutorModule executorModule = new ExecutorModule(server, configModule);
54 | SchedulerModule schedulerModule = new SchedulerModule(server, executorModule);
55 |
56 | //Multiple Module Depends
57 |
58 |
59 | //Module Adds
60 | //No Depends
61 | modules.put(fileModule.getClass(), fileModule);
62 | modules.put(loggingModule.getClass(), loggingModule);
63 | modules.put(networkModule.getClass(), networkModule);
64 | modules.put(playerModule.getClass(), playerModule);
65 |
66 | //Single Module Depends
67 | modules.put(configModule.getClass(), configModule);
68 | modules.put(executorModule.getClass(), executorModule);
69 | modules.put(schedulerModule.getClass(), schedulerModule);
70 |
71 | loggingModule.info("[CoreModuleLoader]: Loaded " + (modules.size() - start) + " core modules.");
72 | }
73 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/module/loaders/JarModuleLoader.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.module.loaders;
18 |
19 |
20 | import org.blockserver.core.Server;
21 | import org.blockserver.core.module.ModuleLoader;
22 | import org.blockserver.core.module.ServerModule;
23 |
24 | import java.io.File;
25 | import java.io.IOException;
26 | import java.io.InputStream;
27 | import java.lang.reflect.InvocationTargetException;
28 | import java.net.URL;
29 | import java.net.URLClassLoader;
30 | import java.util.Map;
31 | import java.util.Properties;
32 | import java.util.jar.JarFile;
33 |
34 | /**
35 | * ServerModule Loader that can load modules from JARs
36 | *
37 | * @author BlockServer Team
38 | * @see org.blockserver.core.module.ModuleLoader
39 | */
40 | public class JarModuleLoader implements ModuleLoader {
41 | @SuppressWarnings({"unchecked", "deprecation"})
42 | @Override
43 | public void setModules(Map, ServerModule> modules, Server server) {
44 | File moduleFolder = new File("Modules");
45 | if (moduleFolder.mkdirs())
46 | System.err.println("Could not find modules folder, created modules folder!");
47 | File[] files = moduleFolder.listFiles();
48 | System.err.println(files.length);
49 | if (files == null || files.length <= 0)
50 | return;
51 |
52 | for (File file : files) {
53 | System.err.println(file.getName());
54 | if (file.getName().endsWith(".jar")) {
55 | try {
56 | JarFile jar = new JarFile(file);
57 | Properties jarProp = getJarProperties(jar);
58 | URLClassLoader loader = new URLClassLoader(new URL[]{file.toURL()});
59 | String className = jarProp.getProperty("mainClass", "default");
60 | try {
61 | Class clazz = loader.loadClass(className);
62 | try {
63 | ServerModule module = (ServerModule) clazz.getConstructor(Server.class).newInstance(server);
64 | modules.put(module.getClass(), module);
65 | System.out.println("[ServerModule Loader]: Loaded " + file.getName());
66 | } catch (ClassCastException e) {
67 | System.err.println("[ServerModule Loader]: Failed to load main class for " + file.getName() + ": main class does not extend ServerModule.");
68 | } catch (NoSuchMethodException | InvocationTargetException e) {
69 | System.err.println("[ServerModule Loader]: Failed to load main class for " + file.getName() + ": " + e.getClass().getSimpleName() + " -> " + e.getMessage());
70 | }
71 | } catch (ClassNotFoundException e) {
72 | if (className.equals("default")) {
73 | System.err.println("[ServerModule Loader]: Failed to load main class for " + file.getName() + ": main class not specified.");
74 | }
75 | System.err.println("[ServerModule Loader]: Failed to load main class for " + file.getName() + ": ClassNotFoundException -> " + e.getMessage());
76 | } catch (InstantiationException | IllegalAccessException e) {
77 | System.err.println("[ServerModule Loader]: Failed to load main class for " + file.getName() + ": " + e.getClass().getSimpleName() + " -> " + e.getMessage());
78 | }
79 | } catch (IOException e) {
80 | e.printStackTrace();
81 | }
82 | }
83 | }
84 | }
85 |
86 | private Properties getJarProperties(JarFile jar) throws IOException {
87 | InputStream stream = jar.getInputStream(jar.getJarEntry("module.properties"));
88 | Properties p = new Properties();
89 | p.load(stream);
90 | return p;
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/config/ConfigModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.config;
18 |
19 | import org.blockserver.core.Server;
20 | import org.blockserver.core.module.ServerModule;
21 | import org.blockserver.core.modules.file.FileModule;
22 |
23 | /**
24 | * Created by Exerosis.
25 | */
26 | public class ConfigModule extends ServerModule {
27 | private final FileModule fileModule;
28 |
29 | public ConfigModule(Server server, FileModule fileModule) {
30 | super(server);
31 | this.fileModule = fileModule;
32 | }
33 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/entity/Entity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.entity;
18 |
19 | import org.blockserver.core.modules.world.positions.Location;
20 |
21 | import java.util.Collections;
22 | import java.util.HashMap;
23 | import java.util.Map;
24 |
25 | /**
26 | * Written by Exerosis!
27 | *
28 | * @author BlockServer Team
29 | */
30 | public class Entity {
31 | private final Map, EntityModule> modules = new HashMap<>();
32 | private float x;
33 | private float y;
34 | private float z;
35 |
36 | //TODO deal with locations and what not!!
37 | public Entity(float x, float y, float z) {
38 | this.x = x;
39 | this.y = y;
40 | this.z = z;
41 | modules.values().forEach(EntityModule::enable);
42 | }
43 |
44 | public Entity(Location location) {
45 | this(location.getX(), location.getY(), location.getZ());
46 | }
47 |
48 |
49 | public void addModule(EntityModule module) {
50 | modules.put(module.getClass(), module);
51 | }
52 |
53 | public void removeModule(Class extends EntityModule> moduleClass) {
54 | modules.remove(moduleClass);
55 | }
56 |
57 | public void removeModule(EntityModule module) {
58 | removeModule(module.getClass());
59 | }
60 |
61 | public EntityModule getModule(Class extends EntityModule> moduleClass) {
62 | return modules.get(moduleClass);
63 | }
64 |
65 | public void destroy() {
66 | modules.values().forEach(EntityModule::disable);
67 | }
68 |
69 |
70 | public Map, EntityModule> getModules() {
71 | return Collections.unmodifiableMap(modules);
72 | }
73 |
74 | public Location getLocation() {
75 | return new Location(x, y, z);
76 | }
77 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/entity/EntityModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.entity;
18 |
19 | import lombok.Getter;
20 | import org.blockserver.core.module.Module;
21 |
22 | /**
23 | * Created by Exerosis.
24 | */
25 | public class EntityModule implements Module {
26 | @Getter private final Entity entity;
27 |
28 | public EntityModule(Entity entity) {
29 | this.entity = entity;
30 | }
31 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/entity/entities/ExampleEntity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.entity.entities;
18 |
19 | import org.blockserver.core.modules.entity.Entity;
20 | import org.blockserver.core.modules.entity.modules.ExampleEntityModule;
21 | import org.blockserver.core.modules.world.positions.Location;
22 |
23 | /**
24 | * Created by Exerosis.
25 | */
26 | public class ExampleEntity extends Entity {
27 | public ExampleEntity(Location location) {
28 | super(location);
29 | }
30 |
31 | public ExampleEntity(float x, float y, float z) {
32 | super(x, y, z);
33 | addModule(new ExampleEntityModule(this));
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/entity/modules/ExampleEntityModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.entity.modules;
18 |
19 | import org.blockserver.core.modules.entity.Entity;
20 | import org.blockserver.core.modules.entity.EntityModule;
21 |
22 | /**
23 | * Created by Exerosis.
24 | */
25 | public class ExampleEntityModule extends EntityModule {
26 | public ExampleEntityModule(Entity entity) {
27 | super(entity);
28 | }
29 |
30 | @Override
31 | public void enable() {
32 | super.enable();
33 | }
34 |
35 | @Override
36 | public void disable() {
37 | super.disable();
38 | }
39 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/file/FileModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.file;
18 |
19 | import org.blockserver.core.Server;
20 | import org.blockserver.core.module.ServerModule;
21 |
22 | /**
23 | * Created by Exerosis.
24 | */
25 | public class FileModule extends ServerModule {
26 | public FileModule(Server server) {
27 | super(server);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/logging/LoggingModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.logging;
18 |
19 | import org.blockserver.core.Server;
20 | import org.blockserver.core.module.ServerModule;
21 |
22 | /**
23 | * Logging ServerModule with different log levels. (debug, info, warn, error)
24 | * TODO: Implement SLF4j and/or log4j2
25 | *
26 | * @author BlockServer Team
27 | * @see ServerModule
28 | */
29 | public class LoggingModule extends ServerModule {
30 |
31 | public LoggingModule(Server server) {
32 | super(server);
33 | }
34 |
35 | public void debug(String message) {
36 | System.out.println("[DEBUG]: " + message);
37 | }
38 |
39 | public void info(String message) {
40 | System.out.println("[INFO]: " + message);
41 | }
42 |
43 | public void warn(String message) {
44 | System.out.println("[WARN]: " + message);
45 | }
46 |
47 | public void error(String message) {
48 | System.err.println("[ERROR]: " + message);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/message/Message.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.message;
18 |
19 | import lombok.Getter;
20 | import lombok.Setter;
21 | import org.blockserver.core.modules.player.Player;
22 |
23 | /**
24 | * Written by Exerosis!
25 | */
26 | public class Message {
27 | @Getter final private boolean async;
28 | @Getter @Setter private Player player;
29 |
30 | public Message(Player player) {
31 | this.player = player;
32 | async = false;
33 | }
34 |
35 | public Message(Player player, boolean async) {
36 | this.player = player;
37 | this.async = async;
38 | }
39 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/message/MessageModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.message;
18 |
19 | import org.blockserver.core.Server;
20 | import org.blockserver.core.event.Priority;
21 | import org.blockserver.core.event.ServerEventListener;
22 | import org.blockserver.core.events.messages.MessageReceiveEvent;
23 | import org.blockserver.core.events.messages.MessageSendEvent;
24 | import org.blockserver.core.modules.network.NetworkConverter;
25 | import org.blockserver.core.modules.network.pipeline.NetworkPipelineHandler;
26 | import org.blockserver.core.modules.network.pipeline.PipelineDispatcher;
27 | import org.blockserver.core.modules.network.pipeline.PipelineProviderImplementation;
28 | import org.blockserver.core.modules.network.pipeline.packet.RawPacket;
29 | import org.blockserver.core.modules.thread.ExecutorModule;
30 |
31 | public class MessageModule extends PipelineProviderImplementation implements PipelineDispatcher {
32 | private final ServerEventListener listener;
33 | private final ExecutorModule executorModule;
34 | private final NetworkConverter converter;
35 |
36 | public MessageModule(Server server, ExecutorModule executorModule, NetworkPipelineHandler handler, NetworkConverter converter) {
37 | super(server, handler);
38 | this.executorModule = executorModule;
39 | this.converter = converter;
40 | listener = new ServerEventListener() {
41 | @Override
42 | public void onEvent(MessageSendEvent event) {
43 | if (!event.isCancelled())
44 | provide(converter.toPacket(event.getMessage()));
45 | }
46 | }.priority(Priority.INTERNAL).post();
47 | }
48 |
49 | @Override
50 | public void enable() {
51 | listener.register(MessageSendEvent.class, getServer());
52 | super.enable();
53 | }
54 |
55 | @Override
56 | public void disable() {
57 | listener.unregister(getServer());
58 | super.disable();
59 | }
60 |
61 | @Override
62 | public void dispatch(RawPacket packet) {
63 | executorModule.getExecutorService().execute(() -> getServer().getEventManager().fire(new MessageReceiveEvent<>(converter.toMessage(packet))));
64 | }
65 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/message/messages/PlayerLoginMessage.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.message.messages;
18 |
19 | import org.blockserver.core.modules.message.Message;
20 | import org.blockserver.core.modules.player.Player;
21 | import org.blockserver.core.utilities.Skin;
22 |
23 | import java.util.UUID;
24 |
25 | /**
26 | * Written by Exerosis!
27 | */
28 | public class PlayerLoginMessage extends Message {
29 | public long clientID;
30 | public String username;
31 | public UUID uuid;
32 | public Skin skin;
33 |
34 | public PlayerLoginMessage(Player player) {
35 | super(player);
36 | }
37 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/message/messages/block/MessageOutBlockChange.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.message.messages.block;
18 |
19 | import org.blockserver.core.modules.message.Message;
20 | import org.blockserver.core.modules.player.Player;
21 | import org.blockserver.core.modules.world.Block;
22 |
23 | /**
24 | * Written by Exerosis!
25 | */
26 | public class MessageOutBlockChange extends Message {
27 | public MessageOutBlockChange(Player player, Block... blocks) {
28 | super(player);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/network/NetworkConverter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.network;
18 |
19 |
20 | import org.blockserver.core.modules.message.Message;
21 | import org.blockserver.core.modules.network.pipeline.packet.RawPacket;
22 |
23 | /**
24 | * Written by Exerosis!
25 | */
26 | public interface NetworkConverter {
27 | RawPacket toPacket(Message message);
28 |
29 | Message toMessage(RawPacket packet);
30 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/network/NetworkModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.network;
18 |
19 | import lombok.Getter;
20 | import org.blockserver.core.Server;
21 | import org.blockserver.core.module.ServerModule;
22 | import org.blockserver.core.modules.network.pipeline.NetworkPipelineHandler;
23 |
24 | /**
25 | * Created by Exerosis.
26 | */
27 | public class NetworkModule extends ServerModule {
28 | @Getter private NetworkPipelineHandler inboundHandler = new NetworkPipelineHandler();
29 | @Getter private NetworkPipelineHandler outboundHandler = new NetworkPipelineHandler();
30 |
31 | public NetworkModule(Server server) {
32 | super(server);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/network/PacketEventModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.network;
18 |
19 | import org.blockserver.core.Server;
20 | import org.blockserver.core.event.Priority;
21 | import org.blockserver.core.event.ServerEventListener;
22 | import org.blockserver.core.events.packets.PacketReceiveEvent;
23 | import org.blockserver.core.events.packets.PacketSendEvent;
24 | import org.blockserver.core.modules.network.pipeline.NetworkPipelineHandler;
25 | import org.blockserver.core.modules.network.pipeline.PipelineDispatcher;
26 | import org.blockserver.core.modules.network.pipeline.PipelineProviderImplementation;
27 | import org.blockserver.core.modules.network.pipeline.packet.RawPacket;
28 |
29 | public class PacketEventModule extends PipelineProviderImplementation implements PipelineDispatcher {
30 | private final ServerEventListener listener;
31 |
32 | public PacketEventModule(NetworkPipelineHandler handler, Server server) {
33 | super(server, handler);
34 | listener = new ServerEventListener() {
35 | @Override
36 | public void onEvent(PacketSendEvent event) {
37 | if (!event.isCancelled())
38 | provide(event.getPacket());
39 | }
40 | }.priority(Priority.INTERNAL).post();
41 | }
42 |
43 | @Override
44 | public void enable() {
45 | listener.register(PacketSendEvent.class, getServer());
46 | super.enable();
47 | }
48 |
49 | @Override
50 | public void disable() {
51 | listener.unregister(getServer());
52 | super.disable();
53 | }
54 |
55 | @Override
56 | public void dispatch(RawPacket packet) {
57 | getServer().getEventManager().fire(new PacketReceiveEvent(packet));
58 | }
59 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/network/pipeline/NetworkPipelineHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.network.pipeline;
18 |
19 |
20 | import lombok.Getter;
21 | import org.blockserver.core.modules.network.pipeline.packet.RawPacket;
22 |
23 | import java.util.Collections;
24 | import java.util.HashSet;
25 | import java.util.Set;
26 |
27 | public class NetworkPipelineHandler {
28 | @Getter private final Set dispatchers = Collections.synchronizedSet(new HashSet<>());
29 |
30 | public NetworkPipelineHandler() {
31 |
32 | }
33 |
34 | public void provide(RawPacket packet) {
35 | for (PipelineDispatcher dispatcher : dispatchers) {
36 | dispatcher.dispatch(packet);
37 | }
38 | }
39 |
40 | public void unregisterDispatcher(PipelineDispatcher dispatcher) {
41 | dispatchers.remove(dispatcher);
42 | }
43 |
44 | public void registerDispatcher(PipelineDispatcher dispatcher) {
45 | dispatchers.add(dispatcher);
46 | }
47 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/network/pipeline/PipelineDispatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.network.pipeline;
18 |
19 | import org.blockserver.core.modules.network.pipeline.packet.RawPacket;
20 |
21 | /**
22 | * Written by Exerosis!
23 | */
24 | public interface PipelineDispatcher {
25 | void dispatch(RawPacket packet);
26 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/network/pipeline/PipelineProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.network.pipeline;
18 |
19 | import org.blockserver.core.modules.network.pipeline.packet.RawPacket;
20 |
21 | /**
22 | * Created by Exerosis.
23 | */
24 | public interface PipelineProvider {
25 | void provide(RawPacket packet);
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/network/pipeline/PipelineProviderImplementation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.network.pipeline;
18 |
19 | import lombok.Getter;
20 | import org.blockserver.core.Server;
21 | import org.blockserver.core.module.ServerModule;
22 | import org.blockserver.core.modules.network.pipeline.packet.RawPacket;
23 |
24 | public class PipelineProviderImplementation extends ServerModule implements PipelineProvider {
25 | @Getter private final NetworkPipelineHandler handler;
26 |
27 | public PipelineProviderImplementation(Server server, NetworkPipelineHandler handler) {
28 | super(server);
29 | this.handler = handler;
30 | }
31 |
32 | @Override
33 | public void provide(RawPacket packet) {
34 | handler.provide(packet);
35 | }
36 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/network/pipeline/packet/BinaryBuffer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.network.pipeline.packet;
18 |
19 | /*
20 | import net.redstonelamp.item.Item;
21 | import net.redstonelamp.utils.BinaryUtils;
22 | import org.spout.nbt.CompoundTag;
23 | */
24 |
25 | import java.nio.BufferOverflowException;
26 | import java.nio.ByteBuffer;
27 | import java.nio.ByteOrder;
28 | import java.util.UUID;
29 |
30 | /**
31 | * An NIO buffer class to wrap around a java.nio.ByteBuffer.
32 | *
33 | * This buffer is dynamic, as it changes size when the allocated amount is too small.
34 | *
35 | * This class is originally from the RedstoneLamp Project. It has been modified from the original
36 | * which can be found at: https://github.com/RedstoneLamp/RedstoneLamp/blob/rewrite/src/main/java/net/redstonelamp/nio/BinaryBuffer.java
37 | *
38 | * @author RedstoneLamp Team and BlockServer Team
39 | */
40 | public class BinaryBuffer{
41 | private ByteBuffer bb;
42 |
43 | protected BinaryBuffer(ByteBuffer bb){
44 | this.bb = bb;
45 | }
46 |
47 | /**
48 | * Create a new DynamicByteBuffer wrapped around a byte array with the specified order
49 | *
50 | * @param bytes The byte array to be wrapped around
51 | * @param order The ByteOrder of the buffer, Big Endian or Little Endian.
52 | * @return A new DynamicByteBuffer class, at position zero wrapped around the byte array in the specified order
53 | */
54 | public static BinaryBuffer wrapBytes(byte[] bytes, ByteOrder order){
55 | ByteBuffer bb = ByteBuffer.wrap(bytes);
56 | bb.order(order);
57 | bb.position(0);
58 | return new BinaryBuffer(bb);
59 | }
60 |
61 | /**
62 | * Create a new DynamicByteBuffer with the specified initalSize
and order
63 | *
64 | * The Buffer will grow if an attempt is to put more data than the initalSize
65 | *
66 | * @param initalSize The inital size of the buffer
67 | * @param order The ByteOrder of the buffer, Big Endian or Little Endian
68 | * @return A new DynamicByteBuffer class, at position zero with the specified order
and initalSize
69 | */
70 | public static BinaryBuffer newInstance(int initalSize, ByteOrder order){
71 | ByteBuffer bb = ByteBuffer.allocate(initalSize);
72 | bb.order(order);
73 | bb.position(0);
74 | return new BinaryBuffer(bb);
75 | }
76 |
77 | /**
78 | * Get len
of bytes from the buffer.
79 | *
80 | * @param len The length of bytes to get from the buffer
81 | * @return A byte array of len
bytes
82 | * @throws java.nio.BufferUnderflowException If there is not enough bytes in the buffer to read
83 | */
84 | public byte[] get(int len){
85 | byte[] b = new byte[len];
86 | bb.get(b);
87 | return b;
88 | }
89 |
90 | /**
91 | * Put an amount of bytes into the buffer. The buffer will resize to fit the bytes if the buffer is too small.
92 | *
93 | * @param bytes The byte array to be put into the buffer
94 | */
95 | public void put(byte[] bytes){
96 | try{
97 | bb.put(bytes);
98 | }catch(BufferOverflowException e){
99 | setPosition(0);
100 | byte[] all = get(remaining());
101 | bb = ByteBuffer.allocate(all.length + bytes.length);
102 | bb.put(all);
103 | bb.put(bytes);
104 | }
105 | }
106 |
107 | /**
108 | * Get a single signed byte from the buffer
109 | *
110 | * @return A single unsigned byte
111 | */
112 | public byte getByte(){
113 | return bb.get();
114 | }
115 |
116 | /**
117 | * Get a single signed boolean from the buffer (one byte)
118 | *
119 | * @return A single boolean
120 | */
121 | public boolean getBoolean(){
122 | return bb.get() > 0;
123 | }
124 |
125 | /**
126 | * Get a single unsigned byte from the buffer
127 | *
128 | * @return A single unsigned byte
129 | */
130 | public short getUnsignedByte(){
131 | return (short) (bb.get() & 0xFF);
132 | }
133 |
134 | /**
135 | * Get a single signed short (2 bytes) from the buffer
136 | *
137 | * @return A single signed short
138 | */
139 | public short getShort(){
140 | return bb.getShort();
141 | }
142 |
143 | /**
144 | * Get a single unsigned short (2 bytes) from the buffer
145 | *
146 | * @return A single unsigned short
147 | */
148 | public short getUnsignedShort(){
149 | return (short) (bb.getShort() & 0xFFFF);
150 | }
151 |
152 | /**
153 | * Get a single signed integer (4 bytes) from the buffer
154 | *
155 | * @return A single signed integer
156 | */
157 | public int getInt(){
158 | return bb.getInt();
159 | }
160 |
161 | /**
162 | * Get a single singed long (8 bytes) from the buffer
163 | *
164 | * @return A single signed long
165 | */
166 | public long getLong(){
167 | return bb.getLong();
168 | }
169 |
170 | public float getFloat(){
171 | return bb.getFloat();
172 | }
173 |
174 | public double getDouble(){
175 | return bb.getDouble();
176 | }
177 |
178 | /**
179 | * Gets a Google Protocol Buffers VarInt from the buffer.
180 | * Code is from: https://gist.github.com/thinkofdeath/e975ddee04e9c87faf22
181 | *
182 | * @return The VarInt, as an integer.
183 | */
184 | public int getVarInt(){
185 | int size = 0;
186 | for(int i = 0; ; i += 7){
187 | byte tmp = getByte();
188 | if((tmp & 0x80) == 0 && (i != 4 * 7 || tmp < 1 << 3)){
189 | return size | tmp << i;
190 | }else if(i < 4 * 7){
191 | size |= (tmp & 0x7f) << i;
192 | }
193 | }
194 | }
195 |
196 | /**
197 | * Get a single short prefixed string from the buffer (2 + str bytes)
198 | *
199 | * @return A single short prefixed string
200 | */
201 | public String getString(){
202 | return new String(get(getUnsignedShort()));
203 | }
204 |
205 | /*
206 | public Item getSlot(){
207 | short id = getShort();
208 | if(id <= 0){
209 | return Item.get(0, (short) 0, 0);
210 | }
211 | int count = getByte();
212 | short data = getShort();
213 |
214 | int len = getUnsignedShort();
215 | if(len > 0){
216 | byte[] nbt = get(len);
217 |
218 | Item i = Item.get(id, data, count);
219 | if(i != null){
220 | i.setCompoundTag((CompoundTag) BinaryUtils.readNBTTag(nbt));
221 | }
222 | return i;
223 | }else{
224 | return Item.get(id, data, count);
225 | }
226 | }
227 | */
228 |
229 | /**
230 | * Get a single varint prefixed string from the buffer (varint bytes + str bytes)
231 | *
232 | * @return A single varint prefixed string
233 | */
234 | public String getVarString(){
235 | return new String(get(getVarInt()));
236 | }
237 |
238 | public UUID getUUID(){
239 | return new UUID(bb.getLong(), bb.getLong());
240 | }
241 |
242 | public void putByte(byte b){
243 | put(new byte[]{b});
244 | }
245 |
246 | public void putBoolean(boolean b){
247 | put(new byte[]{(byte) (b ? 1 : 0)});
248 | }
249 |
250 | public void putShort(short s){
251 | put(ByteBuffer.allocate(2).order(getOrder()).putShort(s).array());
252 | }
253 |
254 | public void putInt(int i){
255 | put(ByteBuffer.allocate(4).order(getOrder()).putInt(i).array());
256 | }
257 |
258 | public void putLong(long l){
259 | put(ByteBuffer.allocate(8).order(getOrder()).putLong(l).array());
260 | }
261 |
262 | public void putFloat(float f){
263 | put(ByteBuffer.allocate(4).order(getOrder()).putFloat(f).array());
264 | }
265 |
266 | public void putDouble(double d){
267 | put(ByteBuffer.allocate(8).order(getOrder()).putDouble(d).array());
268 | }
269 |
270 | public void putString(String s){
271 | putShort((short) s.getBytes().length);
272 | put(s.getBytes());
273 | }
274 |
275 | public void putVarString(String s){
276 | putVarInt(s.getBytes().length);
277 | put(s.getBytes());
278 | }
279 |
280 | public void putUUID(UUID uuid){
281 | putLong(uuid.getMostSignificantBits());
282 | putLong(uuid.getLeastSignificantBits());
283 | }
284 |
285 | /*
286 | public void putSlot(Item item){
287 | if(item.getId() == 0){
288 | putShort((short) 0);
289 | return;
290 | }
291 | putShort((short) item.getId());
292 | putByte((byte) item.getCount());
293 | putShort(item.getMeta());
294 |
295 | byte[] nbt = item.getCompoundTag() != null ? BinaryUtils.writeNBT(item.getCompoundTag()) : new byte[0];
296 | putShort((short) nbt.length);
297 | put(nbt);
298 | }
299 | */
300 |
301 | /**
302 | * Puts a Google Protocol Buffers VarInt into the buffer
303 | * Code is from: https://gist.github.com/thinkofdeath/e975ddee04e9c87faf22
304 | *
305 | * @param i The VarInt as an Integer.
306 | */
307 | public void putVarInt(int i){
308 | while(i > 0x7f){
309 | putByte((byte) (i & 0x7f | 0x80));
310 | i >>= 7;
311 | }
312 | putByte((byte) i);
313 | }
314 |
315 | /**
316 | * Get a single line string containing each byte of the buffer in hexadecimal
317 | *
318 | * @return A String containing each byte of the buffer in hexadecimal with no newlines.
319 | */
320 | public String singleLineHexDump(){
321 | StringBuilder sb = new StringBuilder();
322 | byte[] data = bb.array();
323 | for(byte b : data){
324 | sb.append(String.format("%02X", b)).append(" ");
325 | }
326 | return sb.toString();
327 | }
328 |
329 | /**
330 | * Get the ByteOrder of the underlying ByteBuffer
331 | *
332 | * @return The ByteOrder of the ByteBuffer
333 | */
334 | public ByteOrder getOrder(){
335 | return bb.order();
336 | }
337 |
338 | /**
339 | * Set the ByteOrder of the underlying ByteBuffer
340 | *
341 | * @param order The ByteOrder to be set to.
342 | */
343 | public void setOrder(ByteOrder order){
344 | bb.order(order);
345 | }
346 |
347 | /**
348 | * Get the position of the underyling ByteBuffer
349 | *
350 | * @return The position in the buffer
351 | */
352 | public int getPosition() {
353 | return bb.position();
354 | }
355 |
356 | /**
357 | * Set the position of the underlying ByteBuffer
358 | *
359 | * @param position The position in the buffer to be set to
360 | */
361 | public void setPosition(int position) {
362 | bb.position(position);
363 | }
364 |
365 | /**
366 | * Get the amount of bytes remaining in the buffer
367 | *
368 | * @return The amount of remaining bytes in the buffer
369 | */
370 | public int remaining(){
371 | return bb.remaining();
372 | }
373 |
374 | /**
375 | * Get the remaining bytes in the buffer.
376 | *
377 | * NOTE: This DOES increase the position in the buffer.
378 | *
379 | * @return The reamaining bytes in the buffer.
380 | */
381 | public byte[] remainingBytes(){
382 | return get(remaining());
383 | }
384 |
385 | /**
386 | * Get a byte array of the buffer
387 | *
388 | * @return A byte array containing all the bytes in the buffer
389 | */
390 | public byte[] toArray(){
391 | return bb.array();
392 | }
393 |
394 | /**
395 | * Skip bytes
amount of bytes in the buffer (equivalent to setPosition(getPosition() + len))
396 | *
397 | * @param bytes The amount of bytes to skip in the buffer
398 | */
399 | public void skip(int bytes){
400 | setPosition(getPosition() + bytes);
401 | }
402 | }
403 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/network/pipeline/packet/RawPacket.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.network.pipeline.packet;
18 |
19 | import lombok.Getter;
20 | import lombok.Setter;
21 | import org.blockserver.core.event.CancellableImplementation;
22 |
23 | import java.net.InetSocketAddress;
24 |
25 | /**
26 | * Represents a packet recieved or ready to be sent in byte form.
27 | *
28 | * @author BlockServer Team
29 | */
30 | public class RawPacket implements CancellableImplementation {
31 | @Getter @Setter private BinaryBuffer buffer;
32 | @Getter @Setter private InetSocketAddress address;
33 |
34 | public RawPacket(BinaryBuffer buffer, InetSocketAddress address) {
35 | this.buffer = buffer;
36 | this.address = address;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/player/Player.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.player;
18 |
19 | import lombok.Getter;
20 | import lombok.Setter;
21 | import org.blockserver.core.Server;
22 | import org.blockserver.core.modules.message.Message;
23 | import org.blockserver.core.modules.world.positions.Location;
24 |
25 | import java.net.InetSocketAddress;
26 | import java.util.UUID;
27 |
28 | /**
29 | * Represents a Player on the server.
30 | *
31 | * @author BlockServer Team
32 | */
33 | public class Player {
34 | @Getter private final Server server;
35 | @Getter private final InetSocketAddress address;
36 | @Getter private final String name;
37 | @Getter private final UUID UUID;
38 | @Getter @Setter private int x;
39 | @Getter @Setter private int y;
40 | @Getter @Setter private int z;
41 |
42 | public Player(Server server, InetSocketAddress address, String name, UUID UUID) {
43 | this.server = server;
44 | this.address = address;
45 | this.name = name;
46 | this.UUID = UUID;
47 | }
48 |
49 | public Location getLocation() {
50 | return new Location(x, y, z);
51 | }
52 |
53 | public void sendMessage(Message message) {
54 |
55 | }
56 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/player/PlayerModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.player;
18 |
19 | import org.blockserver.core.Server;
20 | import org.blockserver.core.module.ServerModule;
21 | import org.blockserver.core.modules.logging.LoggingModule;
22 | import org.blockserver.core.modules.network.pipeline.PipelineProviderImplementation;
23 |
24 | import java.net.InetSocketAddress;
25 | import java.util.Collections;
26 | import java.util.HashSet;
27 | import java.util.Set;
28 | import java.util.UUID;
29 |
30 | /**
31 | * Module that handles players.
32 | *
33 | * @author BlockServer Team
34 | * @see org.blockserver.core.module.Module
35 | */
36 | public class PlayerModule extends ServerModule {
37 | private final Set players = Collections.synchronizedSet(new HashSet<>());
38 |
39 | public PlayerModule(Server server) {
40 | super(server);
41 | }
42 |
43 | @Override
44 | public void enable() {
45 | super.enable();
46 | }
47 |
48 | @Override
49 | public void disable() {
50 | players.clear();
51 | super.disable();
52 | }
53 |
54 | /**
55 | * Attempts to find a current online {@linkplain Player} with the specified name. If
56 | * there is no {@linkplain Player} found this method will return null.
57 | *
58 | * @param name ({@linkplain String}): The name of the {@linkplain Player} to locate.
59 | * @return player - ({@linkplain Player}): The {@linkplain Player} with the given name or null.
60 | */
61 | public Player getPlayer(String name) {
62 | for (Player player : players) {
63 | if (player.getName().equals(name))
64 | return player;
65 | }
66 | return null;
67 | }
68 |
69 | /**
70 | * Attempts to find a current online {@linkplain Player} with the specified {@linkplain UUID}. If
71 | * there is no {@linkplain Player} found this method will return null.
72 | *
73 | * @param name ({@linkplain UUID}): The {@linkplain UUID} of the {@linkplain Player} to locate.
74 | * @return player - ({@linkplain Player}): The {@linkplain Player} with the given {@linkplain UUID} or null.
75 | */
76 | public Player getPlayer(UUID name) {
77 | for (Player player : players) {
78 | if (player.getUUID().equals(name))
79 | return player;
80 | }
81 | return null;
82 | }
83 |
84 | /**
85 | * Attempts to find a current online {@linkplain Player} with the specified {@linkplain InetSocketAddress}. If
86 | * there is no {@linkplain Player} found this method will return null.
87 | *
88 | * @param address ({@linkplain InetSocketAddress}): The {@linkplain InetSocketAddress} of the {@linkplain Player} to locate.
89 | * @return player - ({@linkplain Player}): The {@linkplain Player} with the given {@linkplain InetSocketAddress} or null.
90 | */
91 | public Player getPlayer(InetSocketAddress address) {
92 | for (Player player : players) {
93 | if (player.getAddress().equals(address))
94 | return player;
95 | }
96 | return null;
97 | }
98 |
99 |
100 | /**
101 | * Opens a new session, and adds the specified {@linkplain Player} to the list of online {@linkplain Player}s.
102 | *
103 | * NOTE: THIS METHOD IS FOR INTERNAL USE ONLY!
104 | *
105 | * @param address ({@linkplain InetSocketAddress}): The new {@linkplain Player}'s {@linkplain InetSocketAddress}.
106 | * @param name ({@linkplain String}): The new {@linkplain Player}'s {@linkplain String}.
107 | * @param UUID ({@linkplain UUID}): The new {@linkplain Player}'s {@linkplain UUID}.
108 | * @param provider {{@linkplain PipelineProviderImplementation}}: The {@linkplain Player}'s {@linkplain PipelineProviderImplementation} that is used
109 | * to communicate with the client.
110 | */
111 | public void internalOpenSession(InetSocketAddress address, String name, UUID UUID, PipelineProviderImplementation provider) {
112 | players.add(new Player(getServer(), address, name, UUID/*, provider*/));
113 | getServer().getModule(LoggingModule.class).debug("New session from " + address.getHostString() + ":" + address.getPort());
114 | }
115 |
116 | /**
117 | * Removes a Player from the list of online players.
118 | *
119 | * NOTE: THIS METHOD IS FOR INTERNAL USE ONLY!
120 | *
121 | * @param player The player to be removed.
122 | */
123 | public void internalCloseSession(Player player) {
124 | players.remove(player);
125 | getServer().getModule(LoggingModule.class).debug("Session " + player.getAddress().getHostString() + ":" + player.getAddress().getPort() + " closed.");
126 | }
127 |
128 | public Set getPlayers() {
129 | return Collections.unmodifiableSet(players);
130 | }
131 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/scheduler/SchedulerModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.scheduler;
18 |
19 | import lombok.Getter;
20 | import org.blockserver.core.Server;
21 | import org.blockserver.core.module.ServerModule;
22 | import org.blockserver.core.modules.thread.ExecutorModule;
23 |
24 | import java.util.HashMap;
25 | import java.util.Map;
26 |
27 | /**
28 | * Written by Exerosis!
29 | *
30 | * @author BlockServer Team
31 | * @see ServerModule
32 | */
33 | public class SchedulerModule extends ServerModule {
34 | @Getter private final Map tasks = new HashMap<>();
35 | private final ExecutorModule executorModule;
36 |
37 | public SchedulerModule(Server server, ExecutorModule executorModule) {
38 | super(server);
39 | this.executorModule = executorModule;
40 | }
41 |
42 | //TODO maybe make this better!
43 | @Override
44 | public void enable() {
45 | executorModule.getExecutorService().execute(() -> {
46 | while (isEnabled()) {
47 | for (Map.Entry entry : tasks.entrySet()) {
48 | TaskData taskData = entry.getValue();
49 | if (taskData.getNextTickTime() > System.currentTimeMillis())
50 | continue;
51 | taskData.repeatTimes--;
52 | //So by doing this every task will be run at the same time... not in series... is that ok?
53 | executorModule.getExecutorService().execute(() -> entry.getKey().run());
54 | //
55 | if (taskData.getRepeatTimes() <= 0)
56 | tasks.remove(entry.getKey());
57 | taskData.setLastTickTime(System.currentTimeMillis());
58 | }
59 | try {
60 | Thread.sleep(1L);
61 | } catch (InterruptedException e) {
62 | e.printStackTrace();
63 | }
64 | }
65 | });
66 | super.enable();
67 | }
68 |
69 | @Override
70 | public void disable() {
71 | tasks.clear();
72 | super.disable();
73 | }
74 |
75 | public void registerTask(Runnable task, double delay) {
76 | registerTask(task, delay, 1);
77 | }
78 |
79 | public void registerTask(Runnable task, int repeatTimes) {
80 | registerTask(task, 1, repeatTimes);
81 | }
82 |
83 | public void registerTask(Runnable task, double delay, int repeatTimes) {
84 | registerTask(task, new TaskData(delay, repeatTimes));
85 | }
86 |
87 | public void registerTask(Runnable task, TaskData taskData) {
88 | synchronized (tasks) {
89 | tasks.put(task, taskData);
90 | }
91 | }
92 |
93 | public TaskData getTaskData(Runnable task) {
94 | synchronized (tasks) {
95 | return tasks.get(task);
96 | }
97 | }
98 |
99 | public void setTaskData(Runnable task, TaskData taskData) {
100 | synchronized (tasks) {
101 | tasks.put(task, taskData);
102 | }
103 | }
104 |
105 | public void setTaskDelay(Runnable task, double delay) {
106 | getTaskData(task).setDelay(delay);
107 | }
108 |
109 | public void setTaskRepeatTimes(Runnable task, int repeatTimes) {
110 | getTaskData(task).setRepeatTimes(repeatTimes);
111 | }
112 |
113 | public double getTaskDelay(Runnable task) {
114 | return getTaskData(task).getDelay();
115 | }
116 |
117 | public int getTaskRepeatTimes(Runnable task) {
118 | return getTaskData(task).getRepeatTimes();
119 | }
120 |
121 | public void cancelTask(Runnable task) {
122 | synchronized (tasks) {
123 | tasks.remove(task);
124 | }
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/scheduler/TaskData.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.scheduler;
18 |
19 | import lombok.Getter;
20 | import lombok.Setter;
21 |
22 | /**
23 | * Written by Exerosis!
24 | *
25 | * @author BlockServer Team
26 | * @see SchedulerModule
27 | */
28 | public class TaskData {
29 | @Getter @Setter protected long lastTickTime;
30 | @Getter @Setter protected double delay;
31 | @Getter @Setter protected int repeatTimes;
32 |
33 | public TaskData(double delay, int repeatTimes) {
34 | this.delay = delay;
35 | this.repeatTimes = repeatTimes;
36 | }
37 |
38 | public long getNextTickTime() {
39 | return lastTickTime + (long) delay;
40 | }
41 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/serverlist/ServerListModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.serverlist;
18 |
19 | import lombok.Getter;
20 | import org.blockserver.core.Server;
21 | import org.blockserver.core.event.ServerEventListener;
22 | import org.blockserver.core.events.packets.PacketEvent;
23 | import org.blockserver.core.module.ServerModule;
24 | import org.blockserver.core.modules.scheduler.SchedulerModule;
25 |
26 | /**
27 | * Written by Exerosis!
28 | *
29 | * @author BlockServer Team
30 | * @see ServerModule
31 | */
32 | public class ServerListModule extends ServerModule {
33 | private final SchedulerModule schedulerModule;
34 | @Getter private final Runnable task;
35 | private final ServerEventListener listener;
36 |
37 | public ServerListModule(Server server, SchedulerModule schedulerModule) {
38 | super(server);
39 | this.schedulerModule = schedulerModule;
40 | task = () -> {
41 | //networkModule.sendPackets();
42 | //send things
43 | };
44 | listener = new ServerEventListener() {
45 | @Override
46 | public void onEvent(PacketEvent event) {
47 | //receive pings
48 | //send pongs
49 | }
50 | };
51 | }
52 |
53 | @Override
54 | public void enable() {
55 | schedulerModule.registerTask(task, 1.0, Integer.MAX_VALUE);
56 | listener.register(PacketEvent.class, getServer());
57 | super.enable();
58 | }
59 |
60 | @Override
61 | public void disable() {
62 | schedulerModule.cancelTask(task);
63 | listener.unregister(getServer());
64 | super.disable();
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/thread/ExecutorModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.thread;
18 |
19 | import lombok.Getter;
20 | import lombok.Setter;
21 | import org.blockserver.core.Server;
22 | import org.blockserver.core.module.ServerModule;
23 | import org.blockserver.core.modules.config.ConfigModule;
24 |
25 | import java.util.concurrent.ExecutorService;
26 | import java.util.concurrent.Executors;
27 |
28 | /**
29 | * Created by Exerosis.
30 | */
31 | public class ExecutorModule extends ServerModule {
32 | private final ConfigModule configModule;
33 | @Getter @Setter private ExecutorService executorService = Executors.newFixedThreadPool(4);
34 |
35 | public ExecutorModule(Server server, ConfigModule configModule) {
36 | super(server);
37 | this.configModule = configModule;
38 | }
39 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/world/Block.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.world;
18 |
19 | import lombok.Getter;
20 | import org.blockserver.core.modules.world.positions.Vector;
21 |
22 | /**
23 | * Written by Exerosis!
24 | *
25 | * @author BlockServer Team
26 | * @see WorldModule
27 | */
28 | public class Block {
29 | @Getter private ChunkPosition chunk;
30 | @Getter private Material material;
31 | @Getter private byte lightLevel;
32 | @Getter private Vector vector;
33 |
34 | /**
35 | * Sets lightlevel between 0 and 15.
36 | *
37 | * @param lightLevel block lightlevel
38 | */
39 | public void setLightLevel(byte lightLevel) {
40 | this.lightLevel = lightLevel;
41 | }
42 |
43 | /**
44 | * Sets block material.
45 | *
46 | * @param material block material
47 | * @see Material
48 | */
49 | public void setMaterial(Material material) {
50 | this.material = material;
51 | /*
52 | for (Player player : world.getPlayers()) {
53 | player.sendMessage(new MessageOutBlockChange(player, this)); //TODO: Material
54 | }
55 | */
56 | }
57 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/world/ChunkPosition.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.world;
18 |
19 | /**
20 | * Written by Exerosis!
21 | *
22 | * @author BlockServer Team
23 | * @see WorldModule
24 | */
25 | public class ChunkPosition {
26 | public Block getBlockAt(int x, int y, int z) {
27 | return null;
28 | }
29 |
30 | public int getChunkID() {
31 | throw new UnsupportedOperationException();
32 | }
33 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/world/ChunkProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.world;
18 |
19 | /**
20 | * Written by Exerosis!
21 | *
22 | * @author BlockServer Team
23 | * @see WorldModule
24 | */
25 | public interface ChunkProvider {
26 | ChunkPosition loadChunkAt(int x, int y);
27 |
28 | void unloadChunkAt(int x, int y);
29 |
30 | void unloadChunk(ChunkPosition chunk);
31 |
32 | ChunkPosition getOrLoadChunkAt(int x, int y);
33 |
34 | boolean isChunkLoaded(int x, int y);
35 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/world/Material.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.world;
18 |
19 | /**
20 | * Written by Exerosis!
21 | *
22 | * @author BlockServer Team
23 | * @see WorldServerModule
24 | */
25 | public enum Material {
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/world/World.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.world;
18 |
19 | import org.blockserver.core.utilities.ByteUtil;
20 |
21 | public class World {
22 | private ChunkPosition[][] loadedChunks;
23 | private WorldAllocation worldAllocation;
24 |
25 | public World(short allocationSize) {
26 | worldAllocation = new WorldAllocation(allocationSize);
27 | }
28 |
29 | public void saveChunk(int x, int y) {
30 |
31 | }
32 |
33 | public short[][][] getChunk(int x, int y) {
34 | return worldAllocation.getChunkAt(loadedChunks[x][y].getChunkID());
35 | }
36 |
37 | public short getBlock(int x, int y, int z) {
38 | short[][][] chunk = getChunk(x / 16, y / 16);
39 | return chunk[x % 16][y % 16][z];
40 | }
41 |
42 | public byte getBlockMaterial(int x, int y, int z) {
43 | return ByteUtil.fromShort(getBlock(x, y, z))[0];
44 | }
45 |
46 | public byte getBlockLightLevel(int x, int y, int z) {
47 | return ByteUtil.fromShort(getBlock(x, y, z))[1];
48 | }
49 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/world/WorldAllocation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.world;
18 |
19 | /**
20 | * Written by Exerosis!
21 | *
22 | * @author BlockServer Team
23 | * @see WorldAllocation
24 | */
25 | public class WorldAllocation {
26 | private short[][][][] memoryAllocation;
27 |
28 | public WorldAllocation(short allocationSize) {
29 | int chunkCount = allocationSize / 131072 - allocationSize % 131072;
30 | memoryAllocation = new short[chunkCount][16][16][256];
31 |
32 | for (int i = 0; i < memoryAllocation.length; i++) {
33 | memoryAllocation[i] = null;
34 | }
35 | }
36 |
37 | public void setChunkAt(int id, short[][][] chunkData) {
38 | memoryAllocation[id] = chunkData;
39 | }
40 |
41 | public short[][][] getChunkAt(int id) {
42 | return memoryAllocation[id];
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/world/WorldModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.world;
18 |
19 | import org.blockserver.core.Server;
20 | import org.blockserver.core.module.ServerModule;
21 |
22 | /**
23 | * Created by Exerosis.
24 | */
25 | public class WorldModule extends ServerModule {
26 |
27 | public WorldModule(Server server) {
28 | super(server);
29 | }
30 |
31 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/world/positions/Location.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.world.positions;
18 |
19 | import lombok.Getter;
20 |
21 | /**
22 | * Written by Exerosis!
23 | *
24 | * @author BlockServer Team
25 | * @see org.blockserver.core.modules.world.positions.Vector
26 | * @see org.blockserver.core.modules.world.WorldModule
27 | */
28 | public class Location extends Vector {
29 | @Getter long yaw;
30 | @Getter long pitch;
31 |
32 | public Location(Vector vector) {
33 | super(vector);
34 | }
35 |
36 | public Location(Location location) {
37 | this(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
38 | }
39 |
40 | public Location(float x, float y, float z) {
41 | super(x, y, z);
42 | }
43 |
44 | public Location(float x, float y, float z, long yaw, long pitch) {
45 | this(x, y, z);
46 | this.yaw = yaw;
47 | this.pitch = pitch;
48 | }
49 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/modules/world/positions/Vector.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.modules.world.positions;
18 |
19 | import lombok.Getter;
20 |
21 | /**
22 | * Written by Exerosis!
23 | *
24 | * @author BlockServer Team
25 | * @see org.blockserver.core.modules.world.WorldModule
26 | */
27 | public class Vector {
28 | @Getter float x;
29 | @Getter float y;
30 | @Getter float z;
31 |
32 | public Vector(Vector vector) {
33 | this(vector.getX(), vector.getY(), vector.getZ());
34 | }
35 |
36 | public Vector(float x, float y, float z) {
37 | this.x = x;
38 | this.y = y;
39 | this.z = z;
40 | }
41 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/run.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core;
18 |
19 | import org.blockserver.core.event.EventListener;
20 | import org.blockserver.core.events.modules.ModuleEnableEvent;
21 | import org.blockserver.core.module.loaders.CoreModuleLoader;
22 | import org.blockserver.core.module.loaders.JarModuleLoader;
23 | import org.blockserver.core.modules.logging.LoggingModule;
24 |
25 | /**
26 | * Main class for the core.
27 | *
28 | * @author BlockServer team
29 | */
30 | public class run {
31 |
32 | public static void main(String[] args) {
33 | Server server = new Server(new CoreModuleLoader(), new JarModuleLoader());
34 |
35 | new EventListener() {
36 | @Override
37 | public void onEvent(ModuleEnableEvent event) {
38 | System.out.println(event.getModule().getClass().getName());
39 | }
40 | }.register(ModuleEnableEvent.class, server.getEventManager());
41 |
42 | Runtime.getRuntime().addShutdownHook(new Thread(server::disable));
43 | server.enable();
44 | server.getModule(LoggingModule.class).info("BlockServer is now running.");
45 | }
46 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/utilities/ArchiveUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.utilities;
18 |
19 | import java.io.*;
20 | import java.net.URL;
21 | import java.nio.file.Files;
22 | import java.nio.file.Paths;
23 | import java.nio.file.StandardCopyOption;
24 | import java.util.zip.ZipEntry;
25 | import java.util.zip.ZipInputStream;
26 |
27 | public class ArchiveUtils {
28 | //TODO Make this whole thing not suck
29 | private ArchiveUtils() {
30 | }
31 |
32 | public static void downloadFileInto(CharSequence stringURL, File directory) {
33 | try {
34 | URL url = new URL(stringURL.toString());
35 | unzipIntoDirectory(url.openStream(), directory);
36 | } catch (IOException e) {
37 | e.printStackTrace();
38 | }
39 | }
40 |
41 | public static File downloadFile(CharSequence stringURL, File directory) {
42 | try {
43 | if (directory.isFile()) {
44 | return null;
45 | }
46 | directory.mkdirs();
47 | URL url = new URL(stringURL.toString());
48 | String urlPath = url.getPath();
49 | String fileName = urlPath.substring(urlPath.lastIndexOf('/') + 1);
50 |
51 | String[] fileComponents = fileName.split("\\.");
52 |
53 | String extension = "";
54 | if (fileComponents.length >= 2)
55 | extension = fileComponents[1];
56 |
57 | if (!extension.equals("zip"))
58 | Files.copy(url.openStream(), Paths.get(directory.getPath() + extension), StandardCopyOption.REPLACE_EXISTING);
59 | else
60 | unzipIntoDirectory(url.openStream(), directory);
61 | return directory;
62 | } catch (Exception ignored) {
63 | }
64 | return null;
65 | }
66 |
67 | public static void copyFile(File from, File into) {
68 | if (!from.exists())
69 | return;
70 | if (from.getPath().equals(into.getPath()))
71 | return;
72 | from.setReadable(true);
73 | if (into.exists())
74 | into.delete();
75 | try {
76 | ByteUtil.writeBuffer(into, new FileInputStream(from), true);
77 | } catch (IOException e) {
78 | e.printStackTrace();
79 | }
80 | }
81 |
82 | public static void unzipIntoDirectory(File file, File directory) {
83 | try {
84 | unzipIntoDirectory(new FileInputStream(file), directory);
85 | } catch (FileNotFoundException e) {
86 | e.printStackTrace();
87 | }
88 | }
89 |
90 | public static void unzipIntoDirectory(InputStream inputStream, File directory) {
91 | if (directory.isFile())
92 | return;
93 | directory.mkdirs();
94 |
95 | try {
96 | inputStream = new BufferedInputStream(inputStream);
97 | inputStream = new ZipInputStream(inputStream);
98 |
99 | for (ZipEntry entry = null; (entry = ((ZipInputStream) inputStream).getNextEntry()) != null; ) {
100 | StringBuilder pathBuilder = new StringBuilder(directory.getPath()).append('/').append(entry.getName());
101 | File file = new File(pathBuilder.toString());
102 |
103 | if (entry.isDirectory()) {
104 | file.mkdirs();
105 | continue;
106 | }
107 |
108 | ByteUtil.write(pathBuilder, inputStream, false);
109 | }
110 | } catch (IOException e) {
111 | e.printStackTrace();
112 | } finally {
113 | ByteUtil.closeQuietly(inputStream);
114 | }
115 | }
116 |
117 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/utilities/ByteUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.utilities;
18 |
19 | import java.io.*;
20 |
21 | public class ByteUtil {
22 |
23 | private ByteUtil() {
24 | }
25 |
26 | public static short toShort(byte... bytes) {
27 | return (short) ((bytes[0] << 8) + bytes[1]);
28 | }
29 |
30 | public static byte[] fromShort(short number) {
31 | return new byte[]{(byte) (number >> 8), (byte) (number & 255)};
32 | }
33 |
34 | public static void write(CharSequence path, InputStream stream, boolean close) {
35 | write(new File(path.toString()), stream, close);
36 | }
37 |
38 | public static void write(File file, InputStream stream, boolean close) {
39 | file.getParentFile().mkdirs();
40 | if (file.exists())
41 | file.delete();
42 |
43 | OutputStream fileOutputStream = null;
44 | try {
45 | fileOutputStream = new FileOutputStream(file);
46 | fileOutputStream = new BufferedOutputStream(fileOutputStream);
47 | // IOUtils.copy(stream, fileOutputStream);
48 |
49 | } catch (IOException e) {
50 | e.printStackTrace();
51 | } finally {
52 | ByteUtil.closeQuietly(fileOutputStream);
53 | if (close)
54 | ByteUtil.closeQuietly(stream);
55 | }
56 | }
57 |
58 | public static void writeBuffer(File file, InputStream inputStream, boolean close) {
59 | OutputStream outputStream = null;
60 | try {
61 | inputStream = new BufferedInputStream(inputStream);
62 |
63 | outputStream = new FileOutputStream(file);
64 | outputStream = new BufferedOutputStream(outputStream);
65 |
66 | //IOUtils.copy(inputStream, outputStream);
67 | } catch (IOException e) {
68 | e.printStackTrace();
69 | } finally {
70 | ByteUtil.closeQuietly(outputStream);
71 | if (close)
72 | ByteUtil.closeQuietly(inputStream);
73 | }
74 | }
75 |
76 | public static void closeQuietly(Closeable closeable) {
77 | if (closeable != null)
78 | try {
79 | if (closeable.getClass().isAssignableFrom(OutputStream.class))
80 | ((OutputStream) closeable).flush();
81 | closeable.close();
82 | } catch (IOException e) {
83 | e.printStackTrace();
84 | }
85 | }
86 |
87 | /**
88 | * Gets bytes from InputStream
89 | *
90 | * @param stream The InputStream
91 | * @return Returns a byte[] representation of given stream
92 | */
93 |
94 | public static byte[] getBytesFromIS(InputStream stream) {
95 |
96 | ByteArrayOutputStream buffer = new ByteArrayOutputStream();
97 | try {
98 | int nRead;
99 | byte[] data = new byte[16384];
100 |
101 | while ((nRead = stream.read(data, 0, data.length)) != -1) {
102 | buffer.write(data, 0, nRead);
103 | }
104 |
105 | buffer.flush();
106 | } catch (Exception e) {
107 | System.err.println("Failed to convert IS to byte[]!");
108 | e.printStackTrace();
109 | }
110 |
111 | return buffer.toByteArray();
112 |
113 | }
114 |
115 | /**
116 | * Gets bytes from class
117 | *
118 | * @param clazz The class
119 | * @return Returns a byte[] representation of given class
120 | */
121 |
122 | public static byte[] getBytesFromClass(Class> clazz) {
123 | return getBytesFromIS(clazz.getClassLoader().getResourceAsStream(clazz.getName().replace('.', '/') + ".class"));
124 | }
125 | }
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/utilities/FileUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.utilities;
18 |
19 | import java.io.File;
20 |
21 | public class FileUtil {
22 | public static File getDirectoryUp(File file, int amount) {
23 | while (amount != 0) {
24 | file = file.getParentFile();
25 | amount--;
26 | }
27 | return file;
28 | }
29 |
30 | public static File searchFolder(File folder, String uniqueContent) {
31 | return searchFolder(folder, uniqueContent, 0);
32 | }
33 |
34 | private static File searchFolder(File folder, String uniqueContent, int depth) {
35 | if (folder != null)
36 | for (File file : folder.listFiles()) {
37 | if (file.getName().contains(uniqueContent))
38 | return file.getParentFile();
39 | if (file.isDirectory()) {
40 | File result = searchFolder(file, uniqueContent, depth++);
41 | if (result != null)
42 | return getDirectoryUp(result, depth - 1);
43 | }
44 | }
45 | return null;
46 | }
47 |
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/utilities/NetworkUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.utilities;
18 |
19 | import java.net.InetAddress;
20 | import java.net.UnknownHostException;
21 |
22 | public final class NetworkUtil {
23 | private NetworkUtil() {
24 |
25 | }
26 |
27 | public static InetAddress getLocalHost() {
28 | try {
29 | return InetAddress.getLocalHost();
30 | } catch (UnknownHostException e) {
31 | e.printStackTrace();
32 | }
33 | return null;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/org/blockserver/core/utilities/Skin.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of BlockServer.
3 | *
4 | * BlockServer is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU Lesser General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * BlockServer is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with BlockServer. If not, see .
16 | */
17 | package org.blockserver.core.utilities;
18 |
19 | /**
20 | * Utility class that represents a Skin.
21 | */
22 | public class Skin {
23 | public byte[] bytes;
24 | public String skinName;
25 | }
26 |
--------------------------------------------------------------------------------