Skip to content

Commit 0de7e37

Browse files
committed
Modularize the engine (more or less)
1 parent 3ec4707 commit 0de7e37

39 files changed

+492
-397
lines changed

src/main/java/com/flowpowered/api/Client.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
/**
3030
* Represents the client-specific component of the Flow platform.
3131
*/
32-
public interface Client extends Engine {
32+
public interface Client extends EnginePart {
3333

3434
/**
3535
* Gets the renderer that the client is using.

src/main/java/com/flowpowered/api/Engine.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,51 +23,45 @@
2323
*/
2424
package com.flowpowered.api;
2525

26+
import javax.annotation.Nullable;
27+
2628
import org.apache.logging.log4j.Logger;
2729

28-
import com.flowpowered.commons.Named;
29-
import com.flowpowered.events.EventManager;
30-
import com.flowpowered.filesystem.FileSystem;
3130
import com.flowpowered.api.geo.WorldManager;
31+
import com.flowpowered.api.plugins.FlowContext;
3232
import com.flowpowered.api.scheduler.Scheduler;
33+
import com.flowpowered.events.EventManager;
34+
import com.flowpowered.filesystem.FileSystem;
3335
import com.flowpowered.plugins.PluginManager;
3436

3537
/**
3638
* Represents the core of an implementation of an engine (powers a game).
3739
*/
38-
public interface Engine extends Named {
40+
public interface Engine {
3941

4042
/**
4143
* Gets the version.
4244
*
4345
* @return build version
4446
*/
4547
String getVersion();
46-
47-
Platform getPlatform();
4848

4949
Logger getLogger();
5050

51-
/**
52-
* Ends this engine instance safely. All worlds, players, and configuration data is saved, and all threads are ended cleanly.<br/> <br/> Players will be sent a default disconnect message.
53-
*
54-
* @return true for for the first stop
55-
*/
56-
boolean stop();
57-
5851
/**
5952
* Ends this engine instance safely. All worlds, players, and configuration data is saved, and all threads are ended cleanly. <br/> If any players are connected, will kick them with the given reason.
6053
*
6154
* @param reason for stopping the game instance
6255
* @return true for for the first stop
6356
*/
64-
boolean stop(String reason);
57+
boolean stop(@Nullable String reason);
6558

6659
/**
6760
* Returns true if the game is running in debug mode <br/> <br/> To start debug mode, start Flow with -debug
6861
*
6962
* @return true if server is started with the -debug flag, false if not
7063
*/
64+
// TODO: move to Configuration or something
7165
boolean debugMode();
7266

7367
Scheduler getScheduler();
@@ -89,5 +83,8 @@ public interface Engine extends Named {
8983

9084
WorldManager getWorldManager();
9185

92-
PluginManager getPluginManager();
86+
PluginManager<FlowContext> getPluginManager();
87+
88+
@Nullable
89+
<P extends EnginePart> P get(Class<P> part);
9390
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of Flow Engine, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2013 Spout LLC <http://www.spout.org/>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.flowpowered.api;
25+
26+
public interface EnginePart {
27+
}

src/main/java/com/flowpowered/api/Platform.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
package com.flowpowered.api;
2525

2626
/**
27-
* Platform describes whether the plugin was written for the client, the server, or for both.
27+
* Represents a startup argument for the engine. Not used elsewhere; should use Engine#get to get an EnginePart.
2828
*/
2929
public enum Platform {
3030
SERVER(false, true),

src/main/java/com/flowpowered/api/Server.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525

2626
import java.util.Collection;
2727

28-
import com.flowpowered.api.player.Player;
2928
import com.flowpowered.api.geo.ServerWorldManager;
29+
import com.flowpowered.api.player.Player;
3030

3131
/**
32-
* Represents the server-specific implementation.
32+
* Represents the server part of an engine.
3333
*/
34-
public interface Server extends Engine {
34+
public interface Server extends EnginePart {
3535
/**
3636
* Gets all players currently online
3737
*
@@ -76,6 +76,5 @@ public interface Server extends Engine {
7676
*/
7777
Player getPlayer(String name, boolean exact);
7878

79-
@Override
8079
ServerWorldManager getWorldManager();
8180
}

src/main/java/com/flowpowered/api/Singleplayer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727

2828
import com.flowpowered.api.player.Player;
2929

30-
public interface Singleplayer extends Server, Client {
30+
/**
31+
* Represents an EnginePart which has a Player connected to a local server.
32+
*/
33+
public interface Singleplayer extends EnginePart {
3134

3235
@Nullable
3336
Player getPlayer();

src/main/java/com/flowpowered/api/component/entity/EntityObserver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.flowpowered.api.component.entity;
2525

26+
import com.flowpowered.api.Server;
2627
import com.flowpowered.api.component.AbstractObserver;
2728
import com.flowpowered.api.entity.Entity;
2829
import com.flowpowered.api.event.EntityStartObservingChunksEvent;
@@ -81,7 +82,7 @@ public void setSyncDistance(final int syncDistance) {
8182

8283
@Override
8384
public void update() {
84-
if (!engine.getPlatform().isServer()) {
85+
if (engine.get(Server.class) == null) {
8586
return;
8687
}
8788
//Entity changed chunks as observer OR observer status changed so update
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of Flow Engine, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2013 Spout LLC <http://www.spout.org/>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.flowpowered.api.event.engine;
25+
26+
import com.flowpowered.api.EnginePart;
27+
import com.flowpowered.events.SimpleEvent;
28+
29+
public class EnginePartAddedEvent extends SimpleEvent {
30+
private final EnginePart part;
31+
32+
public EnginePartAddedEvent(EnginePart part) {
33+
this.part = part;
34+
}
35+
36+
public EnginePart getPart() {
37+
return part;
38+
}
39+
}

src/main/java/com/flowpowered/api/material/MaterialRegistry.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.concurrent.ConcurrentHashMap;
3030
import java.util.concurrent.atomic.AtomicReference;
3131

32-
import com.flowpowered.api.Engine;
3332
import com.flowpowered.api.Server;
3433
import com.flowpowered.api.material.block.BlockFullState;
3534
import com.flowpowered.api.util.SyncedStringMap;
@@ -56,38 +55,31 @@ public abstract class MaterialRegistry {
5655
}
5756
}
5857

59-
/**
60-
* Sets up the material registry for its first use. May not be called more than once.<br/> This attempts to load the materials.dat file from the 'worlds' directory into memory.<br/>
61-
*
62-
* Can throw an {@link IllegalStateException} if the material registry has already been setup.
63-
*
64-
* @return StringToUniqueIntegerMap of registered materials
65-
*/
66-
public static SyncedStringMap setupRegistry(Engine engine) {
58+
public static void setupServer(Server server) {
6759
if (setup) {
6860
throw new IllegalStateException("Can not setup material registry twice!");
6961
}
70-
if (engine.getPlatform().isServer()) {
71-
setupServer((Server) engine);
72-
} else {
73-
setupClient();
74-
}
75-
76-
setup = true;
77-
return materialRegistry;
78-
}
79-
80-
private static void setupServer(Server server) {
8162
Path serverItemMap = server.getWorldManager().getWorldFolder().resolve("materials.dat");
8263
BinaryFileStore store = new BinaryFileStore(serverItemMap);
8364
materialRegistry = SyncedStringMap.create(null, store, 1, Short.MAX_VALUE, Material.class.getName());
8465
if (Files.exists(serverItemMap)) {
8566
store.load();
8667
}
68+
69+
setup = true;
8770
}
8871

89-
private static void setupClient() {
72+
public static void setupClient() {
73+
if (setup) {
74+
throw new IllegalStateException("Can not setup material registry twice!");
75+
}
9076
materialRegistry = SyncedStringMap.create(null, new MemoryStore<Integer>(), 1, Short.MAX_VALUE, Material.class.getName());
77+
78+
setup = true;
79+
}
80+
81+
public static boolean isSetup() {
82+
return setup;
9183
}
9284

9385
/**

src/main/java/com/flowpowered/api/player/PlayerNetwork.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
import java.util.Set;
3030
import java.util.concurrent.atomic.AtomicReference;
3131

32+
import com.flowpowered.api.Client;
3233
import com.flowpowered.api.geo.LoadOption;
3334
import com.flowpowered.api.geo.cuboid.Chunk;
34-
import com.flowpowered.api.geo.reference.ChunkReference;
3535
import com.flowpowered.api.geo.discrete.Transform;
36+
import com.flowpowered.api.geo.reference.ChunkReference;
3637
import com.flowpowered.api.player.reposition.NullRepositionManager;
3738
import com.flowpowered.api.player.reposition.RepositionManager;
3839
import com.flowpowered.engine.geo.chunk.FlowChunk;
@@ -105,7 +106,7 @@ public void removeChunks(Set<Chunk> toRemove) {
105106
}
106107

107108
public void preSnapshotRun(Transform transform) {
108-
if (session.getEngine().getPlatform().isClient()) {
109+
if (session.getEngine().get(Client.class) != null) {
109110
return;
110111
}
111112

0 commit comments

Comments
 (0)