Skip to content

Commit

Permalink
Add SleepFinishedTimeEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Apr 7, 2024
1 parent 3801c0a commit 527a469
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package io.github.fabricators_of_create.porting_lib.core.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;

import java.util.function.Consumer;

/**
* The base of an event that may be cancelled.
*/
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.fabricators_of_create.porting_lib.level.events;

import io.github.fabricators_of_create.porting_lib.core.event.BaseEvent;
import net.minecraft.world.level.LevelAccessor;

/**
* This event is fired whenever an event involving a {@link LevelAccessor} occurs.
*/
public abstract class LevelEvent extends BaseEvent {
private final LevelAccessor level;

public LevelEvent(LevelAccessor level)
{
this.level = level;
}

/**
* {@return the level this event is affecting}
*/
public LevelAccessor getLevel()
{
return level;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.github.fabricators_of_create.porting_lib.level.events;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.server.level.ServerLevel;

/**
* This event is fired when all players are asleep and the time should be set to day.<br>
* <p>
* setTimeAddition(wakeUpTime) sets a new time that will be added to the dayTime.<br>
*/
public class SleepFinishedTimeEvent extends LevelEvent {
public static final Event<SleepFinishedCallback> SLEEP_FINISHED = EventFactory.createArrayBacked(SleepFinishedCallback.class, callbacks -> event -> {
for (SleepFinishedCallback e : callbacks)
e.onSleepFinished(event);
});
private long newTime;
private final long minTime;

public SleepFinishedTimeEvent(ServerLevel level, long newTime, long minTime) {
super(level);
this.newTime = newTime;
this.minTime = minTime;
}

/**
* @return the new time
*/
public long getNewTime() {
return newTime;
}

/**
* Sets the new time which should be set when all players wake up
*
* @param newTimeIn The new time at wakeup
* @return {@code false} if newTimeIn was lower than current time
*/
public boolean setTimeAddition(long newTimeIn) {
if (minTime > newTimeIn)
return false;
this.newTime = newTimeIn;
return true;
}

@Override
public void sendEvent() {
SLEEP_FINISHED.invoker().onSleepFinished(this);
}

@FunctionalInterface
public interface SleepFinishedCallback {
void onSleepFinished(SleepFinishedTimeEvent event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.fabricators_of_create.porting_lib.level.mixin;

import io.github.fabricators_of_create.porting_lib.level.events.SleepFinishedTimeEvent;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;

import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.world.level.Level;

import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.level.storage.WritableLevelData;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;

import java.util.function.Supplier;

@Mixin(ServerLevel.class)
public abstract class ServerLevelMixin extends Level {
protected ServerLevelMixin(WritableLevelData writableLevelData, ResourceKey<Level> resourceKey, RegistryAccess registryAccess, Holder<DimensionType> holder, Supplier<ProfilerFiller> supplier, boolean bl, boolean bl2, long l, int i) {
super(writableLevelData, resourceKey, registryAccess, holder, supplier, bl, bl2, l, i);
}

@ModifyArg(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerLevel;setDayTime(J)V"))
private long sleepFinishedEvent(long newTime) {
SleepFinishedTimeEvent event = new SleepFinishedTimeEvent((ServerLevel) (Object) this, newTime, getDayTime());
event.sendEvent();
return event.getNewTime();
}
}
7 changes: 7 additions & 0 deletions modules/level_events/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"schemaVersion": 1,
"id": "porting_lib_level_events",
"version": "${version}",
"name": "Porting Lib Level Events",
"description": "Provides common level events for mods."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"required": true,
"minVersion": "0.8",
"package": "io.github.fabricators_of_create.porting_lib.level.mixin",
"compatibilityLevel": "JAVA_17",
"injectors": {
"defaultRequire": 1
},
"mixins": [
"ServerLevelMixin"
]
}

0 comments on commit 527a469

Please sign in to comment.