mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-16 09:16:44 +00:00
Added Day/Night Cycle #14
This commit is contained in:
parent
9597a80edf
commit
9dd9b45d2d
4
Plugin NPC/src/net/simon987/npcplugin/HarvestTask.java
Normal file
4
Plugin NPC/src/net/simon987/npcplugin/HarvestTask.java
Normal file
@ -0,0 +1,4 @@
|
||||
package net.simon987.npcplugin;
|
||||
|
||||
public class HarvestTask {
|
||||
}
|
4
Plugin NPC/src/net/simon987/npcplugin/HarvesterNPC.java
Normal file
4
Plugin NPC/src/net/simon987/npcplugin/HarvesterNPC.java
Normal file
@ -0,0 +1,4 @@
|
||||
package net.simon987.npcplugin;
|
||||
|
||||
public class HarvesterNPC {
|
||||
}
|
4
Plugin NPC/src/net/simon987/npcplugin/NPCTask.java
Normal file
4
Plugin NPC/src/net/simon987/npcplugin/NPCTask.java
Normal file
@ -0,0 +1,4 @@
|
||||
package net.simon987.npcplugin;
|
||||
|
||||
public class NPCTask {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package net.simon987.npcplugin;
|
||||
|
||||
public class NonPlayerCharacter {
|
||||
}
|
4
Plugin NPC/src/net/simon987/npcplugin/NpcPlugin.java
Normal file
4
Plugin NPC/src/net/simon987/npcplugin/NpcPlugin.java
Normal file
@ -0,0 +1,4 @@
|
||||
package net.simon987.npcplugin;
|
||||
|
||||
public class NpcPlugin {
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package net.simon987.npcplugin.event;
|
||||
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventListener;
|
||||
import net.simon987.server.event.TickEvent;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
|
||||
public class TickListener implements GameEventListener {
|
||||
@Override
|
||||
public Class getListenedEventType() {
|
||||
return TickEvent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(GameEvent event) {
|
||||
|
||||
LogManager.LOGGER.info("Time is " + ((TickEvent)event).getTime());
|
||||
|
||||
|
||||
// NonPlayerCharacter npc = new HarvesterNPC();
|
||||
// GameServer.INSTANCE.getGameUniverse().getWorld(0,0).getGameObjects().add(npc);
|
||||
}
|
||||
|
||||
}
|
@ -4,8 +4,10 @@ package net.simon987.server;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventDispatcher;
|
||||
import net.simon987.server.event.TickEvent;
|
||||
import net.simon987.server.game.DayNightCycle;
|
||||
import net.simon987.server.game.GameUniverse;
|
||||
import net.simon987.server.game.World;
|
||||
import net.simon987.server.io.FileUtils;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.plugin.PluginManager;
|
||||
import net.simon987.server.plugin.ServerPlugin;
|
||||
@ -34,6 +36,8 @@ public class GameServer implements Runnable {
|
||||
|
||||
private int maxExecutionTime;
|
||||
|
||||
private DayNightCycle dayNightCycle;
|
||||
|
||||
public GameServer() {
|
||||
|
||||
this.config = new ServerConfiguration(new File("config.properties"));
|
||||
@ -43,6 +47,9 @@ public class GameServer implements Runnable {
|
||||
|
||||
maxExecutionTime = config.getInt("user_timeout");
|
||||
|
||||
|
||||
dayNightCycle = new DayNightCycle();
|
||||
|
||||
//Load all plugins in plugins folder, if it doesn't exist, create it
|
||||
File pluginDir = new File("plugins/");
|
||||
File[] pluginDirListing = pluginDir.listFiles();
|
||||
@ -62,6 +69,7 @@ public class GameServer implements Runnable {
|
||||
}
|
||||
|
||||
eventDispatcher = new GameEventDispatcher(pluginManager);
|
||||
eventDispatcher.getListeners().add(dayNightCycle);
|
||||
|
||||
}
|
||||
|
||||
@ -112,7 +120,7 @@ public class GameServer implements Runnable {
|
||||
|
||||
//Dispatch tick event
|
||||
GameEvent event = new TickEvent(gameUniverse.getTime());
|
||||
GameServer.INSTANCE.getEventDispatcher().dispatch(event); //Ignore cancellation
|
||||
eventDispatcher.dispatch(event); //Ignore cancellation
|
||||
|
||||
|
||||
//Process user code
|
||||
@ -212,4 +220,8 @@ public class GameServer implements Runnable {
|
||||
public void setSocketServer(SocketServer socketServer) {
|
||||
this.socketServer = socketServer;
|
||||
}
|
||||
|
||||
public DayNightCycle getDayNightCycle() {
|
||||
return dayNightCycle;
|
||||
}
|
||||
}
|
||||
|
@ -3,16 +3,30 @@ package net.simon987.server.event;
|
||||
import net.simon987.server.plugin.PluginManager;
|
||||
import net.simon987.server.plugin.ServerPlugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class GameEventDispatcher {
|
||||
|
||||
private PluginManager pluginManager;
|
||||
|
||||
private ArrayList<GameEventListener> listeners;
|
||||
|
||||
public GameEventDispatcher(PluginManager pluginManager) {
|
||||
this.pluginManager = pluginManager;
|
||||
listeners = new ArrayList<>(5);
|
||||
}
|
||||
|
||||
public void dispatch(GameEvent event) {
|
||||
|
||||
//Dispatch to 'local' listeners
|
||||
for (GameEventListener listener : listeners) {
|
||||
if (event.getClass().equals(listener.getListenedEventType())) {
|
||||
listener.handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
//Dispatch to plugins
|
||||
for (ServerPlugin plugin : pluginManager.getPlugins()) {
|
||||
for (GameEventListener listener : plugin.getListeners()) {
|
||||
if (event.getClass().equals(listener.getListenedEventType())) {
|
||||
@ -23,4 +37,7 @@ public class GameEventDispatcher {
|
||||
|
||||
}
|
||||
|
||||
public ArrayList<GameEventListener> getListeners() {
|
||||
return listeners;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
package net.simon987.server.game;
|
||||
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventListener;
|
||||
import net.simon987.server.event.TickEvent;
|
||||
|
||||
public class DayNightCycle implements GameEventListener {
|
||||
|
||||
/**
|
||||
* Length of an hour in ticks
|
||||
*/
|
||||
private static final int HOUR_LENGTH = 16;
|
||||
|
||||
//Current time of the day (0-23)
|
||||
private int currentDayTime;
|
||||
|
||||
//Current light intensity (0-10)
|
||||
private int sunIntensity;
|
||||
|
||||
@Override
|
||||
public Class getListenedEventType() {
|
||||
return TickEvent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(GameEvent event) {
|
||||
|
||||
currentDayTime = (int) ((TickEvent) event).getTime() / HOUR_LENGTH % 24;
|
||||
|
||||
// -0.25x² + 6x - 27 with a minimum of 1
|
||||
sunIntensity = Math.max((int) Math.round((-(0.25 * currentDayTime * currentDayTime) + (6 * currentDayTime) - 27)), 0) + 1;
|
||||
}
|
||||
|
||||
public int getCurrentDayTime() {
|
||||
return currentDayTime;
|
||||
}
|
||||
|
||||
public int getSunIntensity() {
|
||||
return sunIntensity;
|
||||
}
|
||||
}
|
@ -1,16 +1,10 @@
|
||||
package net.simon987.server;
|
||||
package net.simon987.server.io;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
@ -48,9 +42,9 @@ public class FileUtils {
|
||||
|
||||
/**
|
||||
* Created a directory if none exists with the specified name
|
||||
*
|
||||
* @param name folder to create
|
||||
* @return true is the file exists or create operation is successful
|
||||
*
|
||||
* @param directory folder to create
|
||||
* @return true is the file exists or create operation is successful
|
||||
*/
|
||||
public static boolean prepDirectory(Path directory) {
|
||||
File file = directory.toFile();
|
||||
@ -67,9 +61,9 @@ public class FileUtils {
|
||||
|
||||
/**
|
||||
* Converts a file into an array of bytes
|
||||
*
|
||||
* @param fileName the file to be converted into bytes
|
||||
* @return the byte array of the given file
|
||||
*
|
||||
* @param path the file to be converted into bytes
|
||||
* @return the byte array of the given file
|
||||
*/
|
||||
public static byte[] bytifyFile(Path path) {
|
||||
byte[] bytes = null;
|
||||
@ -88,9 +82,9 @@ public class FileUtils {
|
||||
/**
|
||||
* Takes in a file that had been converted to a byte[] to be written to a new
|
||||
* zip file
|
||||
*
|
||||
* @param payload
|
||||
* contains data in byte array form to be written, typically a file
|
||||
*
|
||||
* @param data
|
||||
* contains data in byte array form to be written, typically a file
|
||||
* that has been converted with bytifyFile()
|
||||
* @throws IOException
|
||||
* if an error occurs during the write process
|
4
Server/src/net/simon987/server/event/TickEvent.java
Normal file
4
Server/src/net/simon987/server/event/TickEvent.java
Normal file
@ -0,0 +1,4 @@
|
||||
package net.simon987.server.event;
|
||||
|
||||
public class TickEvent {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package net.simon987.server.event;
|
||||
|
||||
public class UpdateEvent {
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
plugins/Plugin NPC.jar
Normal file
BIN
plugins/Plugin NPC.jar
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user