Skip to content

Commit

Permalink
Implement shorter day/night cycle
Browse files Browse the repository at this point in the history
Resolves #4
  • Loading branch information
matshou committed May 4, 2020
2 parents 5daa593 + d2fefa6 commit 073f2e5
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ out
# gradle
build
.gradle
.composite*

# other
eclipse
Expand Down
4 changes: 3 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 70 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
buildscript {
repositories {
maven { url = 'https://files.minecraftforge.net/maven' }
maven { url = 'https://repo.spongepowered.org/maven' }
jcenter()
mavenCentral()
}
dependencies {
classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true

// https://github.com/SpongePowered/MixinGradle
classpath 'org.spongepowered:mixingradle:0.7-SNAPSHOT'
}
}
apply plugin: 'net.minecraftforge.gradle'
// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
apply plugin: 'org.spongepowered.mixin'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
apply plugin: 'java-library'

version = "${minecraftVersion}-${modVersion}"
group = "io.${groupName}.${modId}"
Expand All @@ -21,6 +26,15 @@ archivesBaseName = project.modId
compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility

repositories {
mavenLocal()
maven { url 'https://repo.spongepowered.org/maven' }
}

mixin {
add sourceSets.main, "mixins.${modId}.refmap.json"
}

minecraft {
// The mappings can be changed at any time, and must be in the following format.
// snapshot_YYYYMMDD Snapshot are built nightly.
Expand All @@ -35,6 +49,7 @@ minecraft {
runs {
client {
workingDirectory project.file('run')
arg "-mixin.config=" + archivesBaseName + ".mixins.json"

// Recommended logging data for a userdev environment
property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
Expand All @@ -51,6 +66,7 @@ minecraft {

server {
workingDirectory project.file('run')
arg "-mixin.config=" + archivesBaseName + ".mixins.json"

// Recommended logging data for a userdev environment
property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
Expand All @@ -75,6 +91,14 @@ dependencies {

// https://mvnrepository.com/artifact/org.jetbrains/annotations
compile group: 'org.jetbrains', name: 'annotations', version: '19.0.0'

// https://github.com/SpongePowered/Mixin
compile "org.spongepowered:mixin:0.8.1-SNAPSHOT"

// When developing with TimeLib in composite build
if (file('.composite').exists()) {
api "io.yooksi:timelib:${timeLibVersion}-api"
}
}

// Get properties into the manifest for reading by the runtime..
Expand Down Expand Up @@ -137,4 +161,49 @@ processResources {
from(sourceSets.main.resources.srcDirs) {
exclude 'META-INF/mods.toml'
}
}

// Enable composite build by renaming .composite file
task enableCompositeBuild {
group = 'tools'
description = 'Enable composite build'
doLast {
File file_enabled = new File(".composite")
if (!file_enabled.exists())
{
File file_disabled = new File('.composite-disable')
if (file_disabled.exists())
{
if (file_disabled.renameTo('.composite')) {
logger.quiet("Composite build enabled")
}
else {
logger.quiet("ERROR: Unable to rename .composite-disable file")
}
}
else if (file_enabled.createNewFile()) {
logger.quiet("Composite build enabled, remember to write path to the build" +
" you want included inside the created '.composite' file")
}
else logger.quiet("ERROR: Unable to create .composite file")
}
else {
logger.quiet("Composite builds are already enabled")
}
}
}

// Disable composite build by renaming .composite file
task disableCompositeBuild {
group = 'tools'
description = 'Disable composite build'
doLast {
File file = new File(".composite")
if (file.renameTo('.composite-disable')) {
logger.quiet("Composite build successfully disabled")
}
else {
logger.quiet("ERROR: Unable to rename .composite file")
}
}
}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ minecraftVersion=1.15.2
forgeVersion=31.1.63
mappingMCVersion=1.15.1
mappingBuild=20200501

timeLibVersion=1.15.2-0.1.1
8 changes: 8 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rootProject.name = "odyssey"

// Special .composite file enables composite builds and contains path data
// run 'enableCompositeBuild' to create and 'disableCompositeBuild' to remove it
File file = new File('.composite')
if (file.exists()) {
includeBuild (file.readLines().get(0))
}
72 changes: 10 additions & 62 deletions src/main/java/io/yooksi/odyssey/Odyssey.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package io.yooksi.odyssey;

import io.yooksi.odyssey.common.Defines;
import io.yooksi.odyssey.core.RegistryHandler;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import io.yooksi.odyssey.config.OdysseyConfig;
import io.yooksi.odyssey.core.TimeCycle;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.stream.Collectors;

@Mod(Defines.MODID)
public class Odyssey {

Expand All @@ -29,67 +21,23 @@ public class Odyssey {
public Odyssey() {

// Initialize mod logger
TMLogger.init(LogManager.getLogger());
ODLogger.init(LogManager.getLogger());

// Register the setup method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
// Register the enqueueIMC method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
// Register the processIMC method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);
// Register the doClientStuff method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);

RegistryHandler.registerDeferred(FMLJavaModLoadingContext.get().getModEventBus());
// Register Mod configuration
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, OdysseyConfig.CLIENT_SPEC);

// Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this);
}

private void setup(final FMLCommonSetupEvent event) {

// some preinit code
LOGGER.info("HELLO FROM PREINIT");
LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
}

private void doClientStuff(final FMLClientSetupEvent event) {

// do something that can only be done on the client
LOGGER.info("Got settings {}", event.getMinecraftSupplier().get().gameSettings);
}

private void enqueueIMC(final InterModEnqueueEvent event) {

// some example code to dispatch IMC to another mod
InterModComms.sendTo("templatemod", "helloworld",
() -> { LOGGER.info("Hello world from the MDK"); return "Hello world";});
}

private void processIMC(final InterModProcessEvent event) {

// some example code to receive and process InterModComms from other mods
LOGGER.info("Got IMC {}", event.getIMCStream().
map(m->m.getMessageSupplier().get()).
collect(Collectors.toList()));
}
// You can use SubscribeEvent and let the Event Bus discover methods to call
@SubscribeEvent
public void onServerStarting(FMLServerStartingEvent event) {

// do something when the server starts
LOGGER.info("HELLO from server starting");
}

// You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
// Event bus for receiving Registry Events)
@Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
public static class RegistryEvents {
LOGGER.info("Pre-initialization phase");

@SubscribeEvent
public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
// register a new block here
LOGGER.info("HELLO from Register Block");
}
// Modify the day/night cycle with config value
TimeCycle.setSpeed(OdysseyConfig.getTimeCycleSpeed());
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/yooksi/odyssey/config/ClientConfig.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package io.yooksi.odyssey.config;

import io.yooksi.odyssey.common.Defines;
import io.yooksi.odyssey.core.TimeCycle;
import net.minecraftforge.common.ForgeConfigSpec;

public class ClientConfig {

public static ForgeConfigSpec.LongValue timeCycleSpeed;

public ClientConfig(ForgeConfigSpec.Builder builder) {

timeCycleSpeed = builder
.comment("Defines the day/night cycle speed proportional to vanilla.")
.translation("config." + Defines.MODID + "timeCycleSpeed")
.defineInRange("timeCycleSpeed", -12,
-TimeCycle.MAX_SPEED, TimeCycle.MAX_SPEED);

// Finish building configurations
builder.build();
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/io/yooksi/odyssey/config/OdysseyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public class OdysseyConfig {
CLIENT = specPair.getLeft();
}

public static void bakeConfig() {
private static long timeCycleSpeed;

public static void bakeConfig() {
timeCycleSpeed = ClientConfig.timeCycleSpeed.get();
}

@SubscribeEvent
Expand All @@ -32,4 +34,8 @@ public static void onModConfigEvent(final ModConfig.ModConfigEvent configEvent)
bakeConfig();
}
}

public static long getTimeCycleSpeed() {
return timeCycleSpeed;
}
}
59 changes: 59 additions & 0 deletions src/main/java/io/yooksi/odyssey/core/TimeCycle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.yooksi.odyssey.core;

public class TimeCycle {

/**
* The maximum speed of the day/night cycle.
*
* This value also represents the maximum proportional length of days to vanilla.
* For example if we wanted the limit the days to be maximally {@code 3} times longer
* or shorter we would set this value to be {@code 3}.
*/
public static final long MAX_SPEED = 72;

/**
* The default speed of the day/night cycle.
* <p>This value also represents the default proportional length of days to vanilla.
*/
public static final long DEFAULT_SPEED = 1;

/**
* The speed of day/night cycle representing the proportional length of days to vanilla.
* <ul>
* <li>Positive values speed up the cycle.</li>
* <li>Negative values slow down the cycle.</li>
* <li>Value of {@code 0} disables the cycle.</li>
* </ul>
* For example, setting this value to {@code 3} would make days {@code 3} times longer,
* while setting it a value of {@code -3} would make them {@code 3} times shorter.
*/
private static long speed = DEFAULT_SPEED;

/**
* Game time of last cycle update in {@code World#advanceTime()}.<br>
* This should be the last recorded time of updating {@code dayTime}.
*/
public static long lastGameTime;

/**
* Sets the time cycle speed to it's default value
*/
public static void resetSpeed() {
setSpeed(DEFAULT_SPEED);
}

/**
* Set the day/night cycle speed to a desired value between
* positive and negative {@link #MAX_SPEED} value.
*/
public static void setSpeed(long value) {
speed = Math.min(Math.max(value, -MAX_SPEED), MAX_SPEED);
}

/**
* @return current day/night cycle speed as defined by Odyssey.
*/
public static long getSpeed() {
return speed;
}
}
Loading

0 comments on commit 073f2e5

Please sign in to comment.