Skip to content

Commit

Permalink
Implement Daylight Cycle Option (#22)
Browse files Browse the repository at this point in the history
* Update gradle + deps

* Remove wildcard imports

* Implement daylight cycle logic

* Handle old nightTime config

* Remove unneeded code

* Make state list final

* Change to Arrays.asList

* Move to write block

* Revert and unset needsSavings only if its saving
  • Loading branch information
Caedis authored Oct 10, 2023
1 parent 6c9e8db commit 14ee836
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 21 deletions.
18 changes: 10 additions & 8 deletions src/main/java/me/eigenraven/personalspace/gui/GuiEditWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.IntConsumer;
Expand Down Expand Up @@ -41,7 +42,7 @@ public class GuiEditWorld extends GuiScreen {
int biomeCycle = 0;
WButton biomeEditButton;
WToggleButton enableWeather;
WToggleButton enableNightTime;
WCycleButton enableDaylightCycle;
WToggleButton enableClouds;
WButton skyType;
WToggleButton generateTrees;
Expand Down Expand Up @@ -141,17 +142,18 @@ public void initGui() {

this.ySize += 4;

this.enableNightTime = new WToggleButton(
this.enableDaylightCycle = new WCycleButton(
new Rectangle(130, this.ySize, 18, 18),
"",
false,
0,
desiredConfig.isNightTime(),
() -> desiredConfig.setNightTime(enableNightTime.value));
this.enableNightTime.noIcon = Icons.SUN;
this.enableNightTime.yesIcon = Icons.MOON;
this.enableNightTime.setValue(this.enableNightTime.value);
this.rootWidget.addChild(this.enableNightTime);
Arrays.asList(
new WCycleButton.ButtonState(DimensionConfig.DaylightCycle.SUN, Icons.SUN),
new WCycleButton.ButtonState(DimensionConfig.DaylightCycle.MOON, Icons.MOON),
new WCycleButton.ButtonState(DimensionConfig.DaylightCycle.CYCLE, Icons.SUN_MOON)),
desiredConfig.getDaylightCycle().ordinal(),
() -> desiredConfig.setDaylightCycle(enableDaylightCycle.getState()));
this.rootWidget.addChild(this.enableDaylightCycle);
this.skyType = new WButton(
new Rectangle(150, this.ySize, 18, 18),
"?",
Expand Down
1 change: 1 addition & 0 deletions src/main/java/me/eigenraven/personalspace/gui/Icons.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum Icons {
PENCIL(64, 32, 16, 16),
MOON(80, 32, 16, 16),
SUN(96, 32, 16, 16),
SUN_MOON(112, 32, 16, 16),
ALL(0, 0, 256, 256);

public static final double TEXTURE_DIM = 256;
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/me/eigenraven/personalspace/gui/WCycleButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.eigenraven.personalspace.gui;

import java.awt.Rectangle;
import java.util.List;

import me.eigenraven.personalspace.world.DimensionConfig;

public class WCycleButton extends WButton {

final List<ButtonState> stateList;
int currentIndex = 0;
int length = 0;

public WCycleButton(Rectangle position, String text, boolean dropShadow, int color, List<ButtonState> states,
int initialIndex, Runnable onClick) {
super(position, text, dropShadow, color, Icons.CHECKMARK, onClick);
stateList = states;
length = stateList.size();
currentIndex = initialIndex;
this.buttonIcon = stateList.get(currentIndex).icon;
}

public DimensionConfig.DaylightCycle getState() {
return stateList.get(currentIndex).cycle;
}

void next() {
if (++currentIndex >= length) {
currentIndex = 0;
}
this.buttonIcon = stateList.get(currentIndex).icon;
}

@Override
protected boolean mouseClickedImpl(int x, int y, int button) {
next();
return super.mouseClickedImpl(x, y, button);
}

public static class ButtonState {

public final DimensionConfig.DaylightCycle cycle;
public final Icons icon;

ButtonState(DimensionConfig.DaylightCycle cycle, Icons icon) {
this.cycle = cycle;
this.icon = icon;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,26 @@ public String getButtonTooltip() {
}
}

public enum DaylightCycle {

SUN,
MOON,
CYCLE;

DaylightCycle() {

}

public static DaylightCycle fromOrdinal(int ordinal) {
return (ordinal < 0 || ordinal >= values().length) ? DaylightCycle.CYCLE : values()[ordinal];
}
}

private String saveDirOverride = "";
private int skyColor = 0xc0d8ff;
private float starBrightness = 1.0F;
private boolean weatherEnabled = false;
private boolean nightTime = false;
private DaylightCycle daylightCycle = DaylightCycle.CYCLE;
private boolean cloudsEnabled = true;
private SkyType skyType = SkyType.VANILLA;
private boolean generatingVegetation = false;
Expand All @@ -125,7 +140,7 @@ public void writeToPacket(MCDataOutput pkt) {
pkt.writeInt(skyColor);
pkt.writeFloat(starBrightness);
pkt.writeVarInt(getRawBiomeId());
pkt.writeBoolean(nightTime);
pkt.writeVarInt(daylightCycle.ordinal());
pkt.writeBoolean(cloudsEnabled);
pkt.writeVarInt(skyType.ordinal());
pkt.writeBoolean(weatherEnabled);
Expand All @@ -145,7 +160,7 @@ public void readFromPacket(MCDataInput pkt) {
this.setSkyColor(pkt.readInt());
this.setStarBrightness(pkt.readFloat());
this.setBiomeId(BiomeGenBase.getBiomeGenArray()[pkt.readVarInt()].biomeName);
this.setNightTime(pkt.readBoolean());
this.setDaylightCycle(DaylightCycle.fromOrdinal(pkt.readVarInt()));
this.setCloudsEnabled(pkt.readBoolean());
this.setSkyType(SkyType.fromOrdinal(pkt.readVarInt()));
this.setWeatherEnabled(pkt.readBoolean());
Expand Down Expand Up @@ -198,11 +213,18 @@ public int syncWithFile(File file, boolean write, int dimId) {
} else {
setWeatherEnabled(cur.getBoolean());
}
cur = cfg.get(VISUAL, "nightTime", nightTime, "");
cur = cfg.get(VISUAL, "daylightCycle", daylightCycle.ordinal(), "");
if (write) {
cur.set(nightTime);
cur.set(daylightCycle.ordinal());
} else {
setNightTime(cur.getBoolean());
setDaylightCycle(DaylightCycle.fromOrdinal(cur.getInt()));
}
// handle old nightTime config
if (!write && cfg.hasKey(VISUAL, "nightTime")) {
Boolean isNight = cfg.getCategory(VISUAL).get("nightTime").getBoolean();
setDaylightCycle(isNight ? DaylightCycle.MOON : DaylightCycle.SUN);
cfg.getCategory(VISUAL).remove("nightTime");
needsSaving = true;
}
cur = cfg.get(VISUAL, "cloudsEnabled", cloudsEnabled, "");
if (write) {
Expand Down Expand Up @@ -252,8 +274,8 @@ public int syncWithFile(File file, boolean write, int dimId) {
}
if (write) {
cfg.save();
needsSaving = false;
}
needsSaving = false;
return dimId;
}

Expand All @@ -272,7 +294,7 @@ public boolean copyFrom(DimensionConfig source, boolean copySaveInfo, boolean co
if (copyVisualInfo) {
this.setSkyColor(source.getSkyColor());
this.setStarBrightness(source.getStarBrightness());
this.setNightTime(source.isNightTime());
this.setDaylightCycle(source.getDaylightCycle());
this.setCloudsEnabled(source.isCloudsEnabled());
this.setSkyType(source.getSkyType());
this.setWeatherEnabled(source.isWeatherEnabled());
Expand Down Expand Up @@ -391,17 +413,21 @@ public void setWeatherEnabled(boolean weatherEnabled) {
}
}

public boolean isNightTime() {
return nightTime;
public DaylightCycle getDaylightCycle() {
return daylightCycle;
}

public void setNightTime(boolean nightTime) {
if (this.nightTime != nightTime) {
public void setDaylightCycle(DaylightCycle cycle) {
if (this.daylightCycle != cycle) {
this.needsSaving = true;
this.nightTime = nightTime;
this.daylightCycle = cycle;
}
}

public boolean isNightTime() {
return daylightCycle == DaylightCycle.MOON;
}

public boolean isCloudsEnabled() {
return cloudsEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public IChunkProvider createChunkGenerator() {

@SideOnly(Side.CLIENT)
public Vec3 getFogColor(float sunAngle, float timeSinceLastTick) {
if (getConfig().getDaylightCycle() == DimensionConfig.DaylightCycle.CYCLE)
return super.getFogColor(sunAngle, timeSinceLastTick);
int baseColor = this.config.getSkyColor();

float red = (float) (baseColor >> 16 & 255) / 255.0F;
Expand All @@ -99,6 +101,8 @@ public ChunkCoordinates getRandomizedSpawnPoint() {

@Override
public Vec3 getSkyColor(Entity cameraEntity, float partialTicks) {
if (getConfig().getDaylightCycle() == DimensionConfig.DaylightCycle.CYCLE)
return super.getSkyColor(cameraEntity, partialTicks);
return getFogColor(0.0f, partialTicks);
}

Expand Down Expand Up @@ -155,26 +159,37 @@ public String getSaveFolder() {

@Override
public boolean isDaytime() {
if (getConfig().getDaylightCycle() == DimensionConfig.DaylightCycle.CYCLE) return super.isDaytime();

return !this.getConfig().isNightTime();
}

@Override
public float getSunBrightnessFactor(float par1) {
if (getConfig().getDaylightCycle() == DimensionConfig.DaylightCycle.CYCLE)
return super.getSunBrightnessFactor(par1);

return this.getConfig().isNightTime() ? 0.0F : 1.0F;
}

@Override
public float getSunBrightness(float par1) {
if (getConfig().getDaylightCycle() == DimensionConfig.DaylightCycle.CYCLE) return super.getSunBrightness(par1);

return this.getConfig().isNightTime() ? 0.2F : 1.0F;
}

@Override
public float getStarBrightness(float par1) {
if (getConfig().getDaylightCycle() == DimensionConfig.DaylightCycle.CYCLE) return super.getStarBrightness(par1);
return getConfig().getStarBrightness();
}

@Override
public float calculateCelestialAngle(long p_76563_1_, float p_76563_3_) {
if (getConfig().getDaylightCycle() == DimensionConfig.DaylightCycle.CYCLE)
return super.calculateCelestialAngle(p_76563_1_, p_76563_3_);

return this.getConfig().isNightTime() ? 0.5f : 0.0f;
}

Expand Down
Binary file modified src/main/resources/assets/personalspace/textures/widgets.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 14ee836

Please sign in to comment.